blob: 01aab038120ed5f9adc5792520c0202089d477fc [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008#include <linux/file.h>
9#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040010#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040011#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040014#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040015#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040016#include <linux/mount.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040017#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040018#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/compat.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020022#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000024#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020025#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000026#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070027#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050028#include <linux/iversion.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040029#include "ctree.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040033#include "print-tree.h"
34#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080036#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020037#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040038#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020039#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010040#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000041#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040042#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070043#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000044#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080045#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046
Hugo Millsabccd002014-01-30 20:17:00 +000047#ifdef CONFIG_64BIT
48/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
49 * structures are incorrect, as the timespec structure from userspace
50 * is 4 bytes too small. We define these alternatives here to teach
51 * the kernel about the 32-bit struct packing.
52 */
53struct btrfs_ioctl_timespec_32 {
54 __u64 sec;
55 __u32 nsec;
56} __attribute__ ((__packed__));
57
58struct btrfs_ioctl_received_subvol_args_32 {
59 char uuid[BTRFS_UUID_SIZE]; /* in */
60 __u64 stransid; /* in */
61 __u64 rtransid; /* out */
62 struct btrfs_ioctl_timespec_32 stime; /* in */
63 struct btrfs_ioctl_timespec_32 rtime; /* out */
64 __u64 flags; /* in */
65 __u64 reserved[16]; /* in */
66} __attribute__ ((__packed__));
67
68#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
69 struct btrfs_ioctl_received_subvol_args_32)
70#endif
71
Josef Bacik2351f432017-09-27 10:43:13 -040072#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
73struct btrfs_ioctl_send_args_32 {
74 __s64 send_fd; /* in */
75 __u64 clone_sources_count; /* in */
76 compat_uptr_t clone_sources; /* in */
77 __u64 parent_root; /* in */
78 __u64 flags; /* in */
79 __u64 reserved[4]; /* in */
80} __attribute__ ((__packed__));
81
82#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
83 struct btrfs_ioctl_send_args_32)
84#endif
Hugo Millsabccd002014-01-30 20:17:00 +000085
Mark Fasheh416161d2013-08-06 11:42:51 -070086static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070087 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
88 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070089
Christoph Hellwig6cbff002009-04-17 10:37:41 +020090/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020091static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
92 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093{
David Sterba1905a0f2018-03-26 18:52:15 +020094 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020096 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097 return flags & ~FS_DIRSYNC_FL;
98 else
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
100}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400101
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
104 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200106static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107{
108 unsigned int iflags = 0;
109
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
124
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900125 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000126 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129
130 return iflags;
131}
132
133/*
134 * Update inode->i_flags based on the btrfs internal flags.
135 */
David Sterba7b6a2212018-03-26 18:40:21 +0200136void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200137{
David Sterba5c57b8b2018-04-23 15:45:18 +0200138 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100139 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140
David Sterba5c57b8b2018-04-23 15:45:18 +0200141 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100142 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200145 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200147 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200149 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_DIRSYNC;
151
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
154 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200155}
156
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
David Sterba5c57b8b2018-04-23 15:45:18 +0200159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
David Sterba5ba76ab2018-03-26 18:52:15 +0200167/* Check if @flags are a supported and valid set of FS_*_FL flags */
168static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000169{
170 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
171 FS_NOATIME_FL | FS_NODUMP_FL | \
172 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000173 FS_NOCOMP_FL | FS_COMPR_FL |
174 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 return -EOPNOTSUPP;
176
177 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
178 return -EINVAL;
179
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return 0;
181}
182
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200183static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
184{
Al Viro496ad9a2013-01-23 17:07:38 -0500185 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400186 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200187 struct btrfs_inode *binode = BTRFS_I(inode);
188 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200189 struct btrfs_trans_handle *trans;
David Sterba5c57b8b2018-04-23 15:45:18 +0200190 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191 int ret;
David Sterba5c57b8b2018-04-23 15:45:18 +0200192 u64 old_flags;
193 unsigned int old_i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600194 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200195
David Sterbabd60ea02014-01-16 15:50:22 +0100196 if (!inode_owner_or_capable(inode))
197 return -EPERM;
198
Li Zefanb83cc962010-12-20 16:04:08 +0800199 if (btrfs_root_readonly(root))
200 return -EROFS;
201
David Sterba5c57b8b2018-04-23 15:45:18 +0200202 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203 return -EFAULT;
204
David Sterba5c57b8b2018-04-23 15:45:18 +0200205 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000206 if (ret)
207 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200208
Jan Karae7848682012-06-12 16:20:32 +0200209 ret = mnt_want_write_file(file);
210 if (ret)
211 return ret;
212
Al Viro59551022016-01-22 15:40:57 -0500213 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200214
David Sterba5c57b8b2018-04-23 15:45:18 +0200215 old_flags = binode->flags;
216 old_i_flags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600217 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800218
David Sterba5c57b8b2018-04-23 15:45:18 +0200219 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
220 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
221 if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222 if (!capable(CAP_LINUX_IMMUTABLE)) {
223 ret = -EPERM;
224 goto out_unlock;
225 }
226 }
227
David Sterba5c57b8b2018-04-23 15:45:18 +0200228 if (fsflags & FS_SYNC_FL)
229 binode->flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200230 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200231 binode->flags &= ~BTRFS_INODE_SYNC;
232 if (fsflags & FS_IMMUTABLE_FL)
233 binode->flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200234 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200235 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
236 if (fsflags & FS_APPEND_FL)
237 binode->flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200239 binode->flags &= ~BTRFS_INODE_APPEND;
240 if (fsflags & FS_NODUMP_FL)
241 binode->flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200242 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200243 binode->flags &= ~BTRFS_INODE_NODUMP;
244 if (fsflags & FS_NOATIME_FL)
245 binode->flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200246 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200247 binode->flags &= ~BTRFS_INODE_NOATIME;
248 if (fsflags & FS_DIRSYNC_FL)
249 binode->flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200250 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200251 binode->flags &= ~BTRFS_INODE_DIRSYNC;
252 if (fsflags & FS_NOCOW_FL) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600253 if (S_ISREG(mode)) {
254 /*
255 * It's safe to turn csums off here, no extents exist.
256 * Otherwise we want the flag to reflect the real COW
257 * status of the file and will not set it.
258 */
259 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200260 binode->flags |= BTRFS_INODE_NODATACOW
261 | BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600262 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200263 binode->flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600264 }
265 } else {
266 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400267 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600268 */
269 if (S_ISREG(mode)) {
270 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200271 binode->flags &= ~(BTRFS_INODE_NODATACOW
David Sterba7e97b8d2012-09-07 05:56:55 -0600272 | BTRFS_INODE_NODATASUM);
273 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200274 binode->flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600275 }
276 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200277
Liu Bo75e7cb72011-03-22 10:12:20 +0000278 /*
279 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
280 * flag may be changed automatically if compression code won't make
281 * things smaller.
282 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200283 if (fsflags & FS_NOCOMP_FL) {
284 binode->flags &= ~BTRFS_INODE_COMPRESS;
285 binode->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000286
287 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
288 if (ret && ret != -ENODATA)
289 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200290 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000291 const char *comp;
292
David Sterba5c57b8b2018-04-23 15:45:18 +0200293 binode->flags |= BTRFS_INODE_COMPRESS;
294 binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000295
David Sterba93370502017-10-31 17:32:41 +0100296 comp = btrfs_compress_type2str(fs_info->compress_type);
297 if (!comp || comp[0] == 0)
298 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
299
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000300 ret = btrfs_set_prop(inode, "btrfs.compression",
301 comp, strlen(comp), 0);
302 if (ret)
303 goto out_drop;
304
Li Zefanebcb9042011-04-15 03:03:17 +0000305 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100306 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
307 if (ret && ret != -ENODATA)
308 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200309 binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000310 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200311
Li Zefan4da6f1a2011-12-29 13:39:50 +0800312 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800313 if (IS_ERR(trans)) {
314 ret = PTR_ERR(trans);
315 goto out_drop;
316 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200317
David Sterba7b6a2212018-03-26 18:40:21 +0200318 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400319 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700320 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200321 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400323 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800324 out_drop:
325 if (ret) {
David Sterba5c57b8b2018-04-23 15:45:18 +0200326 binode->flags = old_flags;
327 inode->i_flags = old_i_flags;
Li Zefanf062abf02011-12-29 13:36:45 +0800328 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200329
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200330 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500331 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200332 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000333 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200334}
335
David Sterba19f93b32018-03-26 19:42:05 +0200336/*
337 * Translate btrfs internal inode flags to xflags as expected by the
338 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
339 * silently dropped.
340 */
341static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
342{
343 unsigned int xflags = 0;
344
345 if (flags & BTRFS_INODE_APPEND)
346 xflags |= FS_XFLAG_APPEND;
347 if (flags & BTRFS_INODE_IMMUTABLE)
348 xflags |= FS_XFLAG_IMMUTABLE;
349 if (flags & BTRFS_INODE_NOATIME)
350 xflags |= FS_XFLAG_NOATIME;
351 if (flags & BTRFS_INODE_NODUMP)
352 xflags |= FS_XFLAG_NODUMP;
353 if (flags & BTRFS_INODE_SYNC)
354 xflags |= FS_XFLAG_SYNC;
355
356 return xflags;
357}
358
359/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
360static int check_xflags(unsigned int flags)
361{
362 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
363 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
364 return -EOPNOTSUPP;
365 return 0;
366}
367
David Sterbae4202ac2018-03-26 19:51:16 +0200368/*
369 * Set the xflags from the internal inode flags. The remaining items of fsxattr
370 * are zeroed.
371 */
372static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
373{
374 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
375 struct fsxattr fa;
376
377 memset(&fa, 0, sizeof(fa));
378 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
379
380 if (copy_to_user(arg, &fa, sizeof(fa)))
381 return -EFAULT;
382
383 return 0;
384}
385
David Sterba025f2122018-03-26 19:51:16 +0200386static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
387{
388 struct inode *inode = file_inode(file);
389 struct btrfs_inode *binode = BTRFS_I(inode);
390 struct btrfs_root *root = binode->root;
391 struct btrfs_trans_handle *trans;
392 struct fsxattr fa;
393 unsigned old_flags;
394 unsigned old_i_flags;
395 int ret = 0;
396
397 if (!inode_owner_or_capable(inode))
398 return -EPERM;
399
400 if (btrfs_root_readonly(root))
401 return -EROFS;
402
403 memset(&fa, 0, sizeof(fa));
404 if (copy_from_user(&fa, arg, sizeof(fa)))
405 return -EFAULT;
406
407 ret = check_xflags(fa.fsx_xflags);
408 if (ret)
409 return ret;
410
411 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
412 return -EOPNOTSUPP;
413
414 ret = mnt_want_write_file(file);
415 if (ret)
416 return ret;
417
418 inode_lock(inode);
419
420 old_flags = binode->flags;
421 old_i_flags = inode->i_flags;
422
423 /* We need the capabilities to change append-only or immutable inode */
424 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
425 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
426 !capable(CAP_LINUX_IMMUTABLE)) {
427 ret = -EPERM;
428 goto out_unlock;
429 }
430
431 if (fa.fsx_xflags & FS_XFLAG_SYNC)
432 binode->flags |= BTRFS_INODE_SYNC;
433 else
434 binode->flags &= ~BTRFS_INODE_SYNC;
435 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
436 binode->flags |= BTRFS_INODE_IMMUTABLE;
437 else
438 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
439 if (fa.fsx_xflags & FS_XFLAG_APPEND)
440 binode->flags |= BTRFS_INODE_APPEND;
441 else
442 binode->flags &= ~BTRFS_INODE_APPEND;
443 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
444 binode->flags |= BTRFS_INODE_NODUMP;
445 else
446 binode->flags &= ~BTRFS_INODE_NODUMP;
447 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
448 binode->flags |= BTRFS_INODE_NOATIME;
449 else
450 binode->flags &= ~BTRFS_INODE_NOATIME;
451
452 /* 1 item for the inode */
453 trans = btrfs_start_transaction(root, 1);
454 if (IS_ERR(trans)) {
455 ret = PTR_ERR(trans);
456 goto out_unlock;
457 }
458
459 btrfs_sync_inode_flags_to_i_flags(inode);
460 inode_inc_iversion(inode);
461 inode->i_ctime = current_time(inode);
462 ret = btrfs_update_inode(trans, root, inode);
463
464 btrfs_end_transaction(trans);
465
466out_unlock:
467 if (ret) {
468 binode->flags = old_flags;
469 inode->i_flags = old_i_flags;
470 }
471
472 inode_unlock(inode);
473 mnt_drop_write_file(file);
474
475 return ret;
476}
477
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200478static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
479{
Al Viro496ad9a2013-01-23 17:07:38 -0500480 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200481
482 return put_user(inode->i_generation, arg);
483}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400484
Li Dongyangf7039b12011-03-24 10:24:28 +0000485static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400487 struct inode *inode = file_inode(file);
488 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000489 struct btrfs_device *device;
490 struct request_queue *q;
491 struct fstrim_range range;
492 u64 minlen = ULLONG_MAX;
493 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500494 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000495 int ret;
496
497 if (!capable(CAP_SYS_ADMIN))
498 return -EPERM;
499
Xiao Guangrong1f781602011-04-20 10:09:16 +0000500 rcu_read_lock();
501 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
502 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000503 if (!device->bdev)
504 continue;
505 q = bdev_get_queue(device->bdev);
506 if (blk_queue_discard(q)) {
507 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100508 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000509 minlen);
510 }
511 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000512 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200513
Li Dongyangf7039b12011-03-24 10:24:28 +0000514 if (!num_devices)
515 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000516 if (copy_from_user(&range, arg, sizeof(range)))
517 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000518 if (range.start > total_bytes ||
519 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200520 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000521
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200522 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000523 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400524 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000525 if (ret < 0)
526 return ret;
527
528 if (copy_to_user(arg, &range, sizeof(range)))
529 return -EFAULT;
530
531 return 0;
532}
533
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200534int btrfs_is_empty_uuid(u8 *uuid)
535{
Chris Mason46e0f662013-11-15 12:14:55 +0100536 int i;
537
538 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
539 if (uuid[i])
540 return 0;
541 }
542 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200543}
544
Miao Xied5c12072013-02-28 10:04:33 +0000545static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400546 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100547 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200548 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000549 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400550{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400551 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400552 struct btrfs_trans_handle *trans;
553 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100554 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555 struct btrfs_inode_item *inode_item;
556 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000557 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400558 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000559 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700560 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900561 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400562 int ret;
563 int err;
564 u64 objectid;
565 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500566 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200567 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400568
David Sterba49a3c4d2016-03-24 17:49:22 +0100569 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
570 if (!root_item)
571 return -ENOMEM;
572
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400573 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400574 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100575 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000576
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800577 /*
578 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400579 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800580 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100581 if (btrfs_qgroup_level(objectid)) {
582 ret = -ENOSPC;
583 goto fail_free;
584 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800585
Miao Xied5c12072013-02-28 10:04:33 +0000586 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400587 /*
Miao Xied5c12072013-02-28 10:04:33 +0000588 * The same as the snapshot creation, please see the comment
589 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400590 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800591 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000592 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100593 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594
Miao Xied5c12072013-02-28 10:04:33 +0000595 trans = btrfs_start_transaction(root, 0);
596 if (IS_ERR(trans)) {
597 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100598 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100599 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000600 }
601 trans->block_rsv = &block_rsv;
602 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400603
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400604 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200605 if (ret)
606 goto fail;
607
David Sterba4d75f8a2014-06-15 01:54:12 +0200608 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400609 if (IS_ERR(leaf)) {
610 ret = PTR_ERR(leaf);
611 goto fail;
612 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400613
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400614 btrfs_mark_buffer_dirty(leaf);
615
David Sterba49a3c4d2016-03-24 17:49:22 +0100616 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800617 btrfs_set_stack_inode_generation(inode_item, 1);
618 btrfs_set_stack_inode_size(inode_item, 3);
619 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400620 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400621 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800622 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623
David Sterba49a3c4d2016-03-24 17:49:22 +0100624 btrfs_set_root_flags(root_item, 0);
625 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800626 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000627
David Sterba49a3c4d2016-03-24 17:49:22 +0100628 btrfs_set_root_bytenr(root_item, leaf->start);
629 btrfs_set_root_generation(root_item, trans->transid);
630 btrfs_set_root_level(root_item, 0);
631 btrfs_set_root_refs(root_item, 1);
632 btrfs_set_root_used(root_item, leaf->len);
633 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634
David Sterba49a3c4d2016-03-24 17:49:22 +0100635 btrfs_set_root_generation_v2(root_item,
636 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200637 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100638 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
639 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
640 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
641 root_item->ctime = root_item->otime;
642 btrfs_set_root_ctransid(root_item, trans->transid);
643 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400644
Chris Mason925baed2008-06-25 16:01:30 -0400645 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400646 free_extent_buffer(leaf);
647 leaf = NULL;
648
David Sterba49a3c4d2016-03-24 17:49:22 +0100649 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400650
651 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400652 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200653 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400654 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100655 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656 if (ret)
657 goto fail;
658
Yan, Zheng76dda932009-09-21 16:00:26 -0400659 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400660 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100661 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100662 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400663 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100664 goto fail;
665 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400666
667 btrfs_record_root_in_trans(trans, new_root);
668
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000669 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700670 if (ret) {
671 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400672 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700673 goto fail;
674 }
675
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530676 mutex_lock(&new_root->objectid_mutex);
677 new_root->highest_objectid = new_dirid;
678 mutex_unlock(&new_root->objectid_mutex);
679
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400680 /*
681 * insert the directory item
682 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200683 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100684 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400685 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 goto fail;
687 }
Chris Mason3de45862008-11-17 21:02:50 -0500688
689 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200690 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500691 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100692 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400693 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400694 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100695 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500696
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200697 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500698 ret = btrfs_update_inode(trans, root, dir);
699 BUG_ON(ret);
700
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400701 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400702 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200703 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500704 BUG_ON(ret);
705
Lu Fengqicdb345a2018-05-29 15:01:53 +0800706 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400707 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200708 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400709 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200710
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400711fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100712 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000713 trans->block_rsv = NULL;
714 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100715 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800716
Sage Weil72fd0322010-10-29 15:41:32 -0400717 if (async_transid) {
718 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400719 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000720 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400721 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400722 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400723 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400724 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400725 if (err && !ret)
726 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500727
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900728 if (!ret) {
729 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800730 if (IS_ERR(inode))
731 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900732 d_instantiate(dentry, inode);
733 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400734 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100735
736fail_free:
737 kfree(root_item);
738 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400739}
740
Miao Xiee9662f72013-02-28 10:01:15 +0000741static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100742 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000743 u64 *async_transid, bool readonly,
744 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400745{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400746 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000747 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400748 struct btrfs_pending_snapshot *pending_snapshot;
749 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000750 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400751
Miao Xie27cdeb72014-04-02 19:51:05 +0800752 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400753 return -EINVAL;
754
David Sterba23269bf2017-02-13 11:03:44 +0100755 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100756 if (!pending_snapshot)
757 return -ENOMEM;
758
David Sterbab0c0ea62015-11-10 18:54:00 +0100759 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100760 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100761 pending_snapshot->path = btrfs_alloc_path();
762 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100763 ret = -ENOMEM;
764 goto free_pending;
765 }
766
David Sterbaea14b57f2017-06-22 02:19:11 +0200767 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100768 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600769 /* wait for no snapshot writes */
770 wait_event(root->subv_writers->wait,
771 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800772
Nikolay Borisov76f32e22018-04-23 10:54:14 +0300773 ret = btrfs_start_delalloc_inodes(root);
Miao Xie6a038432013-05-15 07:48:24 +0000774 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100775 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000776
Chris Mason6374e57a2017-06-23 09:48:21 -0700777 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000778
Miao Xie66d8f3d2012-09-06 04:02:28 -0600779 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
780 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000781 /*
782 * 1 - parent dir inode
783 * 2 - dir entries
784 * 1 - root item
785 * 2 - root ref/backref
786 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200787 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000788 */
789 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200790 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400791 false);
Miao Xied5c12072013-02-28 10:04:33 +0000792 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100793 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000794
Yan, Zhenga22285a2010-05-16 10:48:46 -0400795 pending_snapshot->dentry = dentry;
796 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800797 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000798 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000799 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400800
Miao Xied5c12072013-02-28 10:04:33 +0000801 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400802 if (IS_ERR(trans)) {
803 ret = PTR_ERR(trans);
804 goto fail;
805 }
806
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400807 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400808 list_add(&pending_snapshot->list,
809 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400810 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400811 if (async_transid) {
812 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400813 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000814 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400815 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400816 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400817 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400818 }
Miao Xieaec80302013-03-04 09:44:29 +0000819 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400820 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400821
822 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400823 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000824 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400825
Chris Masond3797302014-10-15 13:50:56 -0700826 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
827 if (ret)
828 goto fail;
829
David Howells2b0143b2015-03-17 22:25:59 +0000830 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000831 if (IS_ERR(inode)) {
832 ret = PTR_ERR(inode);
833 goto fail;
834 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900835
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000836 d_instantiate(dentry, inode);
837 ret = 0;
838fail:
David Sterba7775c812017-02-10 19:18:18 +0100839 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100840dec_and_free:
David Sterbaea14b57f2017-06-22 02:19:11 +0200841 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100842 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100843free_pending:
844 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100845 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100846 kfree(pending_snapshot);
847
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400848 return ret;
849}
850
Sage Weil4260f7c2010-10-29 15:46:43 -0400851/* copy of may_delete in fs/namei.c()
852 * Check whether we can remove a link victim from directory dir, check
853 * whether the type of victim is right.
854 * 1. We can't do it if dir is read-only (done in permission())
855 * 2. We should have write and exec permissions on dir
856 * 3. We can't remove anything from append-only dir
857 * 4. We can't do anything with immutable dir (done in permission())
858 * 5. If the sticky bit on dir is set we should either
859 * a. be owner of dir, or
860 * b. be owner of victim, or
861 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400862 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400863 * links pointing to it.
864 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
865 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
866 * 9. We can't remove a root or mountpoint.
867 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
868 * nfs_async_unlink().
869 */
870
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530871static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400872{
873 int error;
874
David Howells2b0143b2015-03-17 22:25:59 +0000875 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400876 return -ENOENT;
877
David Howells2b0143b2015-03-17 22:25:59 +0000878 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400879 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400880
881 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
882 if (error)
883 return error;
884 if (IS_APPEND(dir))
885 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000886 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
887 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400888 return -EPERM;
889 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000890 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400891 return -ENOTDIR;
892 if (IS_ROOT(victim))
893 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000894 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400895 return -EISDIR;
896 if (IS_DEADDIR(dir))
897 return -ENOENT;
898 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
899 return -EBUSY;
900 return 0;
901}
902
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400903/* copy of may_create in fs/namei.c() */
904static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
905{
David Howells2b0143b2015-03-17 22:25:59 +0000906 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400907 return -EEXIST;
908 if (IS_DEADDIR(dir))
909 return -ENOENT;
910 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
911}
912
913/*
914 * Create a new subvolume below @parent. This is largely modeled after
915 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
916 * inside this filesystem so it's quite a bit simpler.
917 */
Al Viro92872092016-11-20 19:34:31 -0500918static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100919 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400920 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200921 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000922 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400923{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400924 struct inode *dir = d_inode(parent->dentry);
925 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400926 struct dentry *dentry;
927 int error;
928
Al Viro00235412016-05-26 00:05:12 -0400929 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
930 if (error == -EINTR)
931 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400932
933 dentry = lookup_one_len(name, parent->dentry, namelen);
934 error = PTR_ERR(dentry);
935 if (IS_ERR(dentry))
936 goto out_unlock;
937
Yan, Zheng76dda932009-09-21 16:00:26 -0400938 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400939 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600940 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400941
Chris Mason9c520572012-12-17 14:26:57 -0500942 /*
943 * even if this name doesn't exist, we may get hash collisions.
944 * check for them now when we can safely fail
945 */
946 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
947 dir->i_ino, name,
948 namelen);
949 if (error)
950 goto out_dput;
951
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400952 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400953
954 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
955 goto out_up_read;
956
Chris Mason3de45862008-11-17 21:02:50 -0500957 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100958 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200959 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500960 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000961 error = create_subvol(dir, dentry, name, namelen,
962 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500963 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400964 if (!error)
965 fsnotify_mkdir(dir, dentry);
966out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400967 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400968out_dput:
969 dput(dentry);
970out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500971 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400972 return error;
973}
974
Chris Mason4cb53002011-05-24 15:35:30 -0400975/*
976 * When we're defragging a range, we don't want to kick it off again
977 * if it is really just waiting for delalloc to send it down.
978 * If we find a nice big extent or delalloc range for the bytes in the
979 * file you want to defrag, we return 0 to let you know to skip this
980 * part of the file
981 */
David Sterbaaab110a2014-07-29 17:32:10 +0200982static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400983{
984 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
985 struct extent_map *em = NULL;
986 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
987 u64 end;
988
989 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300990 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -0400991 read_unlock(&em_tree->lock);
992
993 if (em) {
994 end = extent_map_end(em);
995 free_extent_map(em);
996 if (end - offset > thresh)
997 return 0;
998 }
999 /* if we already have a nice delalloc here, just stop */
1000 thresh /= 2;
1001 end = count_range_bits(io_tree, &offset, offset + thresh,
1002 thresh, EXTENT_DELALLOC, 1);
1003 if (end >= thresh)
1004 return 0;
1005 return 1;
1006}
1007
1008/*
1009 * helper function to walk through a file and find extents
1010 * newer than a specific transid, and smaller than thresh.
1011 *
1012 * This is used by the defragging code to find new and small
1013 * extents
1014 */
1015static int find_new_extents(struct btrfs_root *root,
1016 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001017 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001018{
1019 struct btrfs_path *path;
1020 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001021 struct extent_buffer *leaf;
1022 struct btrfs_file_extent_item *extent;
1023 int type;
1024 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001025 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001026
1027 path = btrfs_alloc_path();
1028 if (!path)
1029 return -ENOMEM;
1030
David Sterbaa4689d22011-05-31 17:08:14 +00001031 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001032 min_key.type = BTRFS_EXTENT_DATA_KEY;
1033 min_key.offset = *off;
1034
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301035 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001036 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001037 if (ret != 0)
1038 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001039process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001040 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001041 goto none;
1042 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1043 goto none;
1044
1045 leaf = path->nodes[0];
1046 extent = btrfs_item_ptr(leaf, path->slots[0],
1047 struct btrfs_file_extent_item);
1048
1049 type = btrfs_file_extent_type(leaf, extent);
1050 if (type == BTRFS_FILE_EXTENT_REG &&
1051 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1052 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1053 *off = min_key.offset;
1054 btrfs_free_path(path);
1055 return 0;
1056 }
1057
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001058 path->slots[0]++;
1059 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1060 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1061 goto process_slot;
1062 }
1063
Chris Mason4cb53002011-05-24 15:35:30 -04001064 if (min_key.offset == (u64)-1)
1065 goto none;
1066
1067 min_key.offset++;
1068 btrfs_release_path(path);
1069 }
1070none:
1071 btrfs_free_path(path);
1072 return -ENOENT;
1073}
1074
Li Zefan6c282eb2012-06-11 16:03:35 +08001075static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001076{
1077 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001078 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001079 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001080 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001081
1082 /*
1083 * hopefully we have this extent in the tree already, try without
1084 * the full extent lock
1085 */
1086 read_lock(&em_tree->lock);
1087 em = lookup_extent_mapping(em_tree, start, len);
1088 read_unlock(&em_tree->lock);
1089
1090 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001091 struct extent_state *cached = NULL;
1092 u64 end = start + len - 1;
1093
Chris Mason940100a2010-03-10 10:52:59 -05001094 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001095 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001096 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001097 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001098
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001099 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001100 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001101 }
1102
Li Zefan6c282eb2012-06-11 16:03:35 +08001103 return em;
1104}
1105
1106static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1107{
1108 struct extent_map *next;
1109 bool ret = true;
1110
1111 /* this is the last extent */
1112 if (em->start + em->len >= i_size_read(inode))
1113 return false;
1114
1115 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001116 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1117 ret = false;
1118 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001119 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001120 ret = false;
1121
1122 free_extent_map(next);
1123 return ret;
1124}
1125
David Sterbaaab110a2014-07-29 17:32:10 +02001126static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001127 u64 *last_len, u64 *skip, u64 *defrag_end,
1128 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001129{
1130 struct extent_map *em;
1131 int ret = 1;
1132 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001133 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001134
1135 /*
1136 * make sure that once we start defragging an extent, we keep on
1137 * defragging it
1138 */
1139 if (start < *defrag_end)
1140 return 1;
1141
1142 *skip = 0;
1143
1144 em = defrag_lookup_extent(inode, start);
1145 if (!em)
1146 return 0;
1147
Chris Mason940100a2010-03-10 10:52:59 -05001148 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001149 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001150 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001151 goto out;
1152 }
1153
Liu Bo4a3560c2015-08-07 16:48:41 +08001154 if (!*defrag_end)
1155 prev_mergeable = false;
1156
Li Zefan6c282eb2012-06-11 16:03:35 +08001157 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001158 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001159 * we hit a real extent, if it is big or the next extent is not a
1160 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001161 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001162 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001163 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001164 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001165out:
Chris Mason940100a2010-03-10 10:52:59 -05001166 /*
1167 * last_len ends up being a counter of how many bytes we've defragged.
1168 * every time we choose not to defrag an extent, we reset *last_len
1169 * so that the next tiny extent will force a defrag.
1170 *
1171 * The end result of this is that tiny extents before a single big
1172 * extent will force at least part of that big extent to be defragged.
1173 */
1174 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001175 *defrag_end = extent_map_end(em);
1176 } else {
1177 *last_len = 0;
1178 *skip = extent_map_end(em);
1179 *defrag_end = 0;
1180 }
1181
1182 free_extent_map(em);
1183 return ret;
1184}
1185
Chris Mason4cb53002011-05-24 15:35:30 -04001186/*
1187 * it doesn't do much good to defrag one or two pages
1188 * at a time. This pulls in a nice chunk of pages
1189 * to COW and defrag.
1190 *
1191 * It also makes sure the delalloc code has enough
1192 * dirty data to avoid making new small extents as part
1193 * of the defrag
1194 *
1195 * It's a good idea to start RA on this range
1196 * before calling this.
1197 */
1198static int cluster_pages_for_defrag(struct inode *inode,
1199 struct page **pages,
1200 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001201 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001202{
Chris Mason4cb53002011-05-24 15:35:30 -04001203 unsigned long file_end;
1204 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001205 u64 page_start;
1206 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001207 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001208 int ret;
1209 int i;
1210 int i_done;
1211 struct btrfs_ordered_extent *ordered;
1212 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001213 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001214 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001215 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001216
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001217 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001218 if (!isize || start_index > file_end)
1219 return 0;
1220
1221 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001222
Qu Wenruo364ecf32017-02-27 15:10:38 +08001223 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001224 start_index << PAGE_SHIFT,
1225 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001226 if (ret)
1227 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001228 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001229 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001230
1231 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001232 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001233 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001234again:
Josef Bacika94733d2011-07-11 10:47:06 -04001235 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001236 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001237 if (!page)
1238 break;
1239
Miao Xie600a45e2012-02-16 15:01:24 +08001240 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001241 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001242 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001243 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001244 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001245 ordered = btrfs_lookup_ordered_extent(inode,
1246 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001247 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001248 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001249 if (!ordered)
1250 break;
1251
1252 unlock_page(page);
1253 btrfs_start_ordered_extent(inode, ordered, 1);
1254 btrfs_put_ordered_extent(ordered);
1255 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001256 /*
1257 * we unlocked the page above, so we need check if
1258 * it was released or not.
1259 */
1260 if (page->mapping != inode->i_mapping) {
1261 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001262 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001263 goto again;
1264 }
Miao Xie600a45e2012-02-16 15:01:24 +08001265 }
1266
Chris Mason4cb53002011-05-24 15:35:30 -04001267 if (!PageUptodate(page)) {
1268 btrfs_readpage(NULL, page);
1269 lock_page(page);
1270 if (!PageUptodate(page)) {
1271 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001272 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001273 ret = -EIO;
1274 break;
1275 }
1276 }
Miao Xie600a45e2012-02-16 15:01:24 +08001277
Miao Xie600a45e2012-02-16 15:01:24 +08001278 if (page->mapping != inode->i_mapping) {
1279 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001280 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001281 goto again;
1282 }
1283
Chris Mason4cb53002011-05-24 15:35:30 -04001284 pages[i] = page;
1285 i_done++;
1286 }
1287 if (!i_done || ret)
1288 goto out;
1289
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001290 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001291 goto out;
1292
1293 /*
1294 * so now we have a nice long stream of locked
1295 * and up to date pages, lets wait on them
1296 */
1297 for (i = 0; i < i_done; i++)
1298 wait_on_page_writeback(pages[i]);
1299
1300 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001301 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001302
1303 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001304 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001305 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1306 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001307 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001308 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001309
Liu Bo1f12bd02012-03-29 09:57:44 -04001310 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001311 spin_lock(&BTRFS_I(inode)->lock);
1312 BTRFS_I(inode)->outstanding_extents++;
1313 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001314 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001315 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001316 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001317 }
1318
1319
Liu Bo9e8a4a82012-09-05 19:10:51 -06001320 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001321 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001322
1323 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001324 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001325
1326 for (i = 0; i < i_done; i++) {
1327 clear_page_dirty_for_io(pages[i]);
1328 ClearPageChecked(pages[i]);
1329 set_page_extent_mapped(pages[i]);
1330 set_page_dirty(pages[i]);
1331 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001332 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001333 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001334 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1335 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001336 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001337 return i_done;
1338out:
1339 for (i = 0; i < i_done; i++) {
1340 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001341 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001342 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001343 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001344 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001345 page_cnt << PAGE_SHIFT, true);
1346 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1347 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001348 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001349 return ret;
1350
1351}
1352
1353int btrfs_defrag_file(struct inode *inode, struct file *file,
1354 struct btrfs_ioctl_defrag_range_args *range,
1355 u64 newer_than, unsigned long max_to_defrag)
1356{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001358 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001359 struct file_ra_state *ra = NULL;
1360 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001361 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001362 u64 last_len = 0;
1363 u64 skip = 0;
1364 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001365 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001366 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001367 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001368 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001369 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001370 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001371 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001372 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001373 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001374 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001375 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001376 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001377
Liu Bo0abd5b12013-04-16 09:20:28 +00001378 if (isize == 0)
1379 return 0;
1380
1381 if (range->start >= isize)
1382 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001383
David Sterba1e2ef462017-07-17 20:01:59 +02001384 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001385 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1386 return -EINVAL;
1387 if (range->compress_type)
1388 compress_type = range->compress_type;
1389 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001390
Liu Bo0abd5b12013-04-16 09:20:28 +00001391 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001392 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001393
Chris Mason4cb53002011-05-24 15:35:30 -04001394 /*
David Sterba0a52d102017-06-22 03:22:58 +02001395 * If we were not given a file, allocate a readahead context. As
1396 * readahead is just an optimization, defrag will work without it so
1397 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001398 */
1399 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001400 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001401 if (ra)
1402 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001403 } else {
1404 ra = &file->f_ra;
1405 }
1406
David Sterba63e727e2017-06-22 03:13:02 +02001407 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001408 if (!pages) {
1409 ret = -ENOMEM;
1410 goto out_ra;
1411 }
1412
1413 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001414 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001415 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001416 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001417 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001418 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001419 }
1420
Chris Mason4cb53002011-05-24 15:35:30 -04001421 if (newer_than) {
1422 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001423 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001424 if (!ret) {
1425 range->start = newer_off;
1426 /*
1427 * we always align our defrag to help keep
1428 * the extents in the file evenly spaced
1429 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001430 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001431 } else
1432 goto out_ra;
1433 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001434 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001435 }
1436 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301437 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001438
Li Zefan2a0f7f52011-10-10 15:43:34 -04001439 /*
1440 * make writeback starts from i, so the defrag range can be
1441 * written sequentially.
1442 */
1443 if (i < inode->i_mapping->writeback_index)
1444 inode->i_mapping->writeback_index = i;
1445
Chris Masonf7f43cc2011-10-11 11:41:40 -04001446 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001447 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001448 /*
1449 * make sure we stop running if someone unmounts
1450 * the FS
1451 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001452 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001453 break;
1454
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001455 if (btrfs_defrag_cancelled(fs_info)) {
1456 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001457 ret = -EAGAIN;
1458 break;
1459 }
1460
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001461 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001462 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001463 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001464 unsigned long next;
1465 /*
1466 * the should_defrag function tells us how much to skip
1467 * bump our counter by the suggested amount
1468 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001469 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001470 i = max(i + 1, next);
1471 continue;
1472 }
Li Zefan008873e2011-09-02 15:57:07 +08001473
1474 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001475 cluster = (PAGE_ALIGN(defrag_end) >>
1476 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001477 cluster = min(cluster, max_cluster);
1478 } else {
1479 cluster = max_cluster;
1480 }
1481
Li Zefan008873e2011-09-02 15:57:07 +08001482 if (i + cluster > ra_index) {
1483 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001484 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001485 page_cache_sync_readahead(inode->i_mapping, ra,
1486 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301487 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001488 }
Chris Mason940100a2010-03-10 10:52:59 -05001489
Al Viro59551022016-01-22 15:40:57 -05001490 inode_lock(inode);
David Sterba1e2ef462017-07-17 20:01:59 +02001491 if (do_compress)
David Sterbaeec63c62017-07-17 19:41:31 +02001492 BTRFS_I(inode)->defrag_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001493 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001494 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001495 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001496 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001497 }
Chris Mason940100a2010-03-10 10:52:59 -05001498
Chris Mason4cb53002011-05-24 15:35:30 -04001499 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001500 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001501 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001502
1503 if (newer_than) {
1504 if (newer_off == (u64)-1)
1505 break;
1506
Liu Boe1f041e2012-03-29 09:57:45 -04001507 if (ret > 0)
1508 i += ret;
1509
Chris Mason4cb53002011-05-24 15:35:30 -04001510 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001511 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001512
Byongho Leeee221842015-12-15 01:42:10 +09001513 ret = find_new_extents(root, inode, newer_than,
1514 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001515 if (!ret) {
1516 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001517 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001518 } else {
1519 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520 }
Chris Mason4cb53002011-05-24 15:35:30 -04001521 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001522 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001523 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001524 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001525 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001526 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001527 last_len = 0;
1528 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001529 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001530 }
1531
Filipe Mananadec8ef92014-03-01 10:55:54 +00001532 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001533 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001534 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1535 &BTRFS_I(inode)->runtime_flags))
1536 filemap_flush(inode->i_mapping);
1537 }
Chris Mason1e701a32010-03-11 09:42:04 -05001538
Li Zefan1a419d82010-10-25 15:12:50 +08001539 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001540 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001541 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1542 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001543 }
1544
Diego Calleja60ccf822011-09-01 16:33:57 +02001545 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001546
Chris Mason4cb53002011-05-24 15:35:30 -04001547out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001548 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001549 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001550 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001551 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001552 }
Chris Mason4cb53002011-05-24 15:35:30 -04001553 if (!file)
1554 kfree(ra);
1555 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001556 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001557}
1558
Miao Xie198605a2012-11-26 08:43:45 +00001559static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001560 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001561{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001562 struct inode *inode = file_inode(file);
1563 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001564 u64 new_size;
1565 u64 old_size;
1566 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001567 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001568 struct btrfs_ioctl_vol_args *vol_args;
1569 struct btrfs_trans_handle *trans;
1570 struct btrfs_device *device = NULL;
1571 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001572 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573 char *devstr = NULL;
1574 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001575 int mod = 0;
1576
Chris Masone441d542009-01-05 16:57:23 -05001577 if (!capable(CAP_SYS_ADMIN))
1578 return -EPERM;
1579
Miao Xie198605a2012-11-26 08:43:45 +00001580 ret = mnt_want_write_file(file);
1581 if (ret)
1582 return ret;
1583
David Sterba171938e2017-03-28 14:44:21 +02001584 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001585 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001586 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001587 }
1588
Li Zefandae7b662009-04-08 15:06:54 +08001589 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001590 if (IS_ERR(vol_args)) {
1591 ret = PTR_ERR(vol_args);
1592 goto out;
1593 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001594
1595 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001596
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001597 sizestr = vol_args->name;
1598 devstr = strchr(sizestr, ':');
1599 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001600 sizestr = devstr + 1;
1601 *devstr = '\0';
1602 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001603 ret = kstrtoull(devstr, 10, &devid);
1604 if (ret)
1605 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001606 if (!devid) {
1607 ret = -EINVAL;
1608 goto out_free;
1609 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001610 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001611 }
Miao Xiedba60f32012-12-21 09:19:51 +00001612
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001613 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001615 btrfs_info(fs_info, "resizer unable to find device %llu",
1616 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001617 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001618 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001619 }
Miao Xiedba60f32012-12-21 09:19:51 +00001620
Anand Jainebbede42017-12-04 12:54:52 +08001621 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001622 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001623 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001624 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001625 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001626 goto out_free;
1627 }
1628
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001629 if (!strcmp(sizestr, "max"))
1630 new_size = device->bdev->bd_inode->i_size;
1631 else {
1632 if (sizestr[0] == '-') {
1633 mod = -1;
1634 sizestr++;
1635 } else if (sizestr[0] == '+') {
1636 mod = 1;
1637 sizestr++;
1638 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001639 new_size = memparse(sizestr, &retptr);
1640 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001641 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001642 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001643 }
1644 }
1645
Anand Jain401e29c2017-12-04 12:54:55 +08001646 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001647 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001648 goto out_free;
1649 }
1650
Miao Xie7cc8e582014-09-03 21:35:38 +08001651 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001652
1653 if (mod < 0) {
1654 if (new_size > old_size) {
1655 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001656 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001657 }
1658 new_size = old_size - new_size;
1659 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001660 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001661 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001662 goto out_free;
1663 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001664 new_size = old_size + new_size;
1665 }
1666
Byongho Leeee221842015-12-15 01:42:10 +09001667 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001668 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001669 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001670 }
1671 if (new_size > device->bdev->bd_inode->i_size) {
1672 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001673 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001674 }
1675
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001676 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001677
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001678 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1679 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001680
1681 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001682 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001683 if (IS_ERR(trans)) {
1684 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001685 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001686 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001687 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001688 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001689 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001690 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001691 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001692
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001693out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001694 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001695out:
David Sterba171938e2017-03-28 14:44:21 +02001696 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001697 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001698 return ret;
1699}
1700
Sage Weil72fd0322010-10-29 15:41:32 -04001701static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001702 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001703 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001704 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001705{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001707 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001708
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001709 if (!S_ISDIR(file_inode(file)->i_mode))
1710 return -ENOTDIR;
1711
Liu Boa874a632012-06-29 03:58:46 -06001712 ret = mnt_want_write_file(file);
1713 if (ret)
1714 goto out;
1715
Sage Weil72fd0322010-10-29 15:41:32 -04001716 namelen = strlen(name);
1717 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001718 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001719 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001720 }
1721
Chris Mason16780ca2012-02-20 22:14:55 -05001722 if (name[0] == '.' &&
1723 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1724 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001725 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001726 }
1727
Chris Mason3de45862008-11-17 21:02:50 -05001728 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001729 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001730 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001731 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001732 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001733 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001734 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001735 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001736 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001737 }
1738
Al Viro496ad9a2013-01-23 17:07:38 -05001739 src_inode = file_inode(src.file);
1740 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001741 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001742 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001743 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001744 } else if (!inode_owner_or_capable(src_inode)) {
1745 /*
1746 * Subvolume creation is not restricted, but snapshots
1747 * are limited to own subvolumes only
1748 */
1749 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001750 } else {
1751 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1752 BTRFS_I(src_inode)->root,
1753 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001754 }
Al Viro2903ff02012-08-28 12:52:22 -04001755 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001756 }
Liu Boa874a632012-06-29 03:58:46 -06001757out_drop_write:
1758 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001759out:
Sage Weil72fd0322010-10-29 15:41:32 -04001760 return ret;
1761}
1762
1763static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001764 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001765{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001766 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001767 int ret;
1768
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001769 if (!S_ISDIR(file_inode(file)->i_mode))
1770 return -ENOTDIR;
1771
Li Zefanfa0d2b92010-12-20 15:53:28 +08001772 vol_args = memdup_user(arg, sizeof(*vol_args));
1773 if (IS_ERR(vol_args))
1774 return PTR_ERR(vol_args);
1775 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001776
Li Zefanfa0d2b92010-12-20 15:53:28 +08001777 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001778 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001779 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001780
Li Zefanfa0d2b92010-12-20 15:53:28 +08001781 kfree(vol_args);
1782 return ret;
1783}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001784
Li Zefanfa0d2b92010-12-20 15:53:28 +08001785static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1786 void __user *arg, int subvol)
1787{
1788 struct btrfs_ioctl_vol_args_v2 *vol_args;
1789 int ret;
1790 u64 transid = 0;
1791 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001792 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001793 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001794
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001795 if (!S_ISDIR(file_inode(file)->i_mode))
1796 return -ENOTDIR;
1797
Li Zefanfa0d2b92010-12-20 15:53:28 +08001798 vol_args = memdup_user(arg, sizeof(*vol_args));
1799 if (IS_ERR(vol_args))
1800 return PTR_ERR(vol_args);
1801 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001802
Li Zefanb83cc962010-12-20 16:04:08 +08001803 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001804 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1805 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001806 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001807 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001808 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001809
1810 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1811 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001812 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1813 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001814 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001815 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001816 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001817 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001818 }
1819 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1820 if (IS_ERR(inherit)) {
1821 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001822 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001823 }
1824 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001825
1826 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001827 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001828 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001829 if (ret)
1830 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001831
Dan Carpenterc47ca322014-09-04 14:09:15 +03001832 if (ptr && copy_to_user(arg +
1833 offsetof(struct btrfs_ioctl_vol_args_v2,
1834 transid),
1835 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001836 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001837
1838free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001839 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001840free_args:
1841 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001842 return ret;
1843}
1844
Li Zefan0caa1022010-12-20 16:30:25 +08001845static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1846 void __user *arg)
1847{
Al Viro496ad9a2013-01-23 17:07:38 -05001848 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001849 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001850 struct btrfs_root *root = BTRFS_I(inode)->root;
1851 int ret = 0;
1852 u64 flags = 0;
1853
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001854 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001855 return -EINVAL;
1856
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001857 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001858 if (btrfs_root_readonly(root))
1859 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001860 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001861
1862 if (copy_to_user(arg, &flags, sizeof(flags)))
1863 ret = -EFAULT;
1864
1865 return ret;
1866}
1867
1868static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1869 void __user *arg)
1870{
Al Viro496ad9a2013-01-23 17:07:38 -05001871 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001872 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001873 struct btrfs_root *root = BTRFS_I(inode)->root;
1874 struct btrfs_trans_handle *trans;
1875 u64 root_flags;
1876 u64 flags;
1877 int ret = 0;
1878
David Sterbabd60ea02014-01-16 15:50:22 +01001879 if (!inode_owner_or_capable(inode))
1880 return -EPERM;
1881
Liu Bob9ca0662012-06-29 03:58:49 -06001882 ret = mnt_want_write_file(file);
1883 if (ret)
1884 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001885
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001886 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001887 ret = -EINVAL;
1888 goto out_drop_write;
1889 }
Li Zefan0caa1022010-12-20 16:30:25 +08001890
Liu Bob9ca0662012-06-29 03:58:49 -06001891 if (copy_from_user(&flags, arg, sizeof(flags))) {
1892 ret = -EFAULT;
1893 goto out_drop_write;
1894 }
Li Zefan0caa1022010-12-20 16:30:25 +08001895
Liu Bob9ca0662012-06-29 03:58:49 -06001896 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1897 ret = -EINVAL;
1898 goto out_drop_write;
1899 }
Li Zefan0caa1022010-12-20 16:30:25 +08001900
Liu Bob9ca0662012-06-29 03:58:49 -06001901 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1902 ret = -EOPNOTSUPP;
1903 goto out_drop_write;
1904 }
Li Zefan0caa1022010-12-20 16:30:25 +08001905
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001906 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001907
1908 /* nothing to do */
1909 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001910 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001911
1912 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001913 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001914 btrfs_set_root_flags(&root->root_item,
1915 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001916 } else {
1917 /*
1918 * Block RO -> RW transition if this subvolume is involved in
1919 * send
1920 */
1921 spin_lock(&root->root_item_lock);
1922 if (root->send_in_progress == 0) {
1923 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001924 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001925 spin_unlock(&root->root_item_lock);
1926 } else {
1927 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001928 btrfs_warn(fs_info,
1929 "Attempt to set subvolume %llu read-write during send",
1930 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001931 ret = -EPERM;
1932 goto out_drop_sem;
1933 }
1934 }
Li Zefan0caa1022010-12-20 16:30:25 +08001935
1936 trans = btrfs_start_transaction(root, 1);
1937 if (IS_ERR(trans)) {
1938 ret = PTR_ERR(trans);
1939 goto out_reset;
1940 }
1941
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001942 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001943 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001944 if (ret < 0) {
1945 btrfs_end_transaction(trans);
1946 goto out_reset;
1947 }
Li Zefan0caa1022010-12-20 16:30:25 +08001948
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001949 ret = btrfs_commit_transaction(trans);
1950
Li Zefan0caa1022010-12-20 16:30:25 +08001951out_reset:
1952 if (ret)
1953 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001954out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001955 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001956out_drop_write:
1957 mnt_drop_write_file(file);
1958out:
Li Zefan0caa1022010-12-20 16:30:25 +08001959 return ret;
1960}
1961
Chris Masonac8e9812010-02-28 15:39:26 -05001962static noinline int key_in_sk(struct btrfs_key *key,
1963 struct btrfs_ioctl_search_key *sk)
1964{
Chris Masonabc6e132010-03-18 12:10:08 -04001965 struct btrfs_key test;
1966 int ret;
1967
1968 test.objectid = sk->min_objectid;
1969 test.type = sk->min_type;
1970 test.offset = sk->min_offset;
1971
1972 ret = btrfs_comp_cpu_keys(key, &test);
1973 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001974 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001975
1976 test.objectid = sk->max_objectid;
1977 test.type = sk->max_type;
1978 test.offset = sk->max_offset;
1979
1980 ret = btrfs_comp_cpu_keys(key, &test);
1981 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001982 return 0;
1983 return 1;
1984}
1985
Jeff Mahoneydf397562016-06-21 20:18:21 -04001986static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001987 struct btrfs_key *key,
1988 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001989 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001990 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001991 unsigned long *sk_offset,
1992 int *num_found)
1993{
1994 u64 found_transid;
1995 struct extent_buffer *leaf;
1996 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001997 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001998 unsigned long item_off;
1999 unsigned long item_len;
2000 int nritems;
2001 int i;
2002 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002003 int ret = 0;
2004
2005 leaf = path->nodes[0];
2006 slot = path->slots[0];
2007 nritems = btrfs_header_nritems(leaf);
2008
2009 if (btrfs_header_generation(leaf) > sk->max_transid) {
2010 i = nritems;
2011 goto advance_key;
2012 }
2013 found_transid = btrfs_header_generation(leaf);
2014
2015 for (i = slot; i < nritems; i++) {
2016 item_off = btrfs_item_ptr_offset(leaf, i);
2017 item_len = btrfs_item_size_nr(leaf, i);
2018
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002019 btrfs_item_key_to_cpu(leaf, key, i);
2020 if (!key_in_sk(key, sk))
2021 continue;
2022
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002023 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002024 if (*num_found) {
2025 ret = 1;
2026 goto out;
2027 }
Chris Masonac8e9812010-02-28 15:39:26 -05002028
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002029 /*
2030 * return one empty item back for v1, which does not
2031 * handle -EOVERFLOW
2032 */
2033
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002034 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002035 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002036 ret = -EOVERFLOW;
2037 }
Chris Masonac8e9812010-02-28 15:39:26 -05002038
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002039 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002040 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002041 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002042 }
2043
Chris Masonac8e9812010-02-28 15:39:26 -05002044 sh.objectid = key->objectid;
2045 sh.offset = key->offset;
2046 sh.type = key->type;
2047 sh.len = item_len;
2048 sh.transid = found_transid;
2049
2050 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002051 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2052 ret = -EFAULT;
2053 goto out;
2054 }
2055
Chris Masonac8e9812010-02-28 15:39:26 -05002056 *sk_offset += sizeof(sh);
2057
2058 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002059 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002060 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002061 if (read_extent_buffer_to_user(leaf, up,
2062 item_off, item_len)) {
2063 ret = -EFAULT;
2064 goto out;
2065 }
2066
Chris Masonac8e9812010-02-28 15:39:26 -05002067 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002068 }
Hugo Millse2156862011-05-14 17:43:41 +00002069 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002070
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002071 if (ret) /* -EOVERFLOW from above */
2072 goto out;
2073
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002074 if (*num_found >= sk->nr_items) {
2075 ret = 1;
2076 goto out;
2077 }
Chris Masonac8e9812010-02-28 15:39:26 -05002078 }
2079advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002080 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002081 test.objectid = sk->max_objectid;
2082 test.type = sk->max_type;
2083 test.offset = sk->max_offset;
2084 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2085 ret = 1;
2086 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002087 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002088 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002089 key->offset = 0;
2090 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002091 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002092 key->offset = 0;
2093 key->type = 0;
2094 key->objectid++;
2095 } else
2096 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002097out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002098 /*
2099 * 0: all items from this leaf copied, continue with next
2100 * 1: * more items can be copied, but unused buffer is too small
2101 * * all items were found
2102 * Either way, it will stops the loop which iterates to the next
2103 * leaf
2104 * -EOVERFLOW: item was to large for buffer
2105 * -EFAULT: could not copy extent buffer back to userspace
2106 */
Chris Masonac8e9812010-02-28 15:39:26 -05002107 return ret;
2108}
2109
2110static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002111 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002112 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002113 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002114{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002115 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002116 struct btrfs_root *root;
2117 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002118 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002119 int ret;
2120 int num_found = 0;
2121 unsigned long sk_offset = 0;
2122
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002123 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2124 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002125 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002126 }
Gerhard Heift12544442014-01-30 16:23:58 +01002127
Chris Masonac8e9812010-02-28 15:39:26 -05002128 path = btrfs_alloc_path();
2129 if (!path)
2130 return -ENOMEM;
2131
2132 if (sk->tree_id == 0) {
2133 /* search the root of the inode that was passed */
2134 root = BTRFS_I(inode)->root;
2135 } else {
2136 key.objectid = sk->tree_id;
2137 key.type = BTRFS_ROOT_ITEM_KEY;
2138 key.offset = (u64)-1;
2139 root = btrfs_read_fs_root_no_name(info, &key);
2140 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002141 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002142 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002143 }
2144 }
2145
2146 key.objectid = sk->min_objectid;
2147 key.type = sk->min_type;
2148 key.offset = sk->min_offset;
2149
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302150 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002151 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002152 if (ret != 0) {
2153 if (ret > 0)
2154 ret = 0;
2155 goto err;
2156 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002157 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002158 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002159 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002160 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002161 break;
2162
2163 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002164 if (ret > 0)
2165 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002166err:
2167 sk->nr_items = num_found;
2168 btrfs_free_path(path);
2169 return ret;
2170}
2171
2172static noinline int btrfs_ioctl_tree_search(struct file *file,
2173 void __user *argp)
2174{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002175 struct btrfs_ioctl_search_args __user *uargs;
2176 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002177 struct inode *inode;
2178 int ret;
2179 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002180
2181 if (!capable(CAP_SYS_ADMIN))
2182 return -EPERM;
2183
Gerhard Heiftba346b32014-01-30 16:24:02 +01002184 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002185
Gerhard Heiftba346b32014-01-30 16:24:02 +01002186 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2187 return -EFAULT;
2188
2189 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002190
Al Viro496ad9a2013-01-23 17:07:38 -05002191 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002192 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002193
2194 /*
2195 * In the origin implementation an overflow is handled by returning a
2196 * search header with a len of zero, so reset ret.
2197 */
2198 if (ret == -EOVERFLOW)
2199 ret = 0;
2200
Gerhard Heiftba346b32014-01-30 16:24:02 +01002201 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002202 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002203 return ret;
2204}
2205
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002206static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2207 void __user *argp)
2208{
2209 struct btrfs_ioctl_search_args_v2 __user *uarg;
2210 struct btrfs_ioctl_search_args_v2 args;
2211 struct inode *inode;
2212 int ret;
2213 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002214 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002215
2216 if (!capable(CAP_SYS_ADMIN))
2217 return -EPERM;
2218
2219 /* copy search header and buffer size */
2220 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2221 if (copy_from_user(&args, uarg, sizeof(args)))
2222 return -EFAULT;
2223
2224 buf_size = args.buf_size;
2225
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002226 /* limit result size to 16MB */
2227 if (buf_size > buf_limit)
2228 buf_size = buf_limit;
2229
2230 inode = file_inode(file);
2231 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002232 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002233 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2234 ret = -EFAULT;
2235 else if (ret == -EOVERFLOW &&
2236 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2237 ret = -EFAULT;
2238
Yan, Zheng76dda932009-09-21 16:00:26 -04002239 return ret;
2240}
2241
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002242/*
Chris Masonac8e9812010-02-28 15:39:26 -05002243 * Search INODE_REFs to identify path name of 'dirid' directory
2244 * in a 'tree_id' tree. and sets path name to 'name'.
2245 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002246static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2247 u64 tree_id, u64 dirid, char *name)
2248{
2249 struct btrfs_root *root;
2250 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002251 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002252 int ret = -1;
2253 int slot;
2254 int len;
2255 int total_len = 0;
2256 struct btrfs_inode_ref *iref;
2257 struct extent_buffer *l;
2258 struct btrfs_path *path;
2259
2260 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2261 name[0]='\0';
2262 return 0;
2263 }
2264
2265 path = btrfs_alloc_path();
2266 if (!path)
2267 return -ENOMEM;
2268
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002269 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002270
2271 key.objectid = tree_id;
2272 key.type = BTRFS_ROOT_ITEM_KEY;
2273 key.offset = (u64)-1;
2274 root = btrfs_read_fs_root_no_name(info, &key);
2275 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002276 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002277 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002278 }
2279
2280 key.objectid = dirid;
2281 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002282 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002283
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302284 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002285 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2286 if (ret < 0)
2287 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002288 else if (ret > 0) {
2289 ret = btrfs_previous_item(root, path, dirid,
2290 BTRFS_INODE_REF_KEY);
2291 if (ret < 0)
2292 goto out;
2293 else if (ret > 0) {
2294 ret = -ENOENT;
2295 goto out;
2296 }
2297 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002298
2299 l = path->nodes[0];
2300 slot = path->slots[0];
2301 btrfs_item_key_to_cpu(l, &key, slot);
2302
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002303 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2304 len = btrfs_inode_ref_name_len(l, iref);
2305 ptr -= len + 1;
2306 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002307 if (ptr < name) {
2308 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002309 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002310 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002311
2312 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302313 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002314
2315 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2316 break;
2317
David Sterbab3b4aa72011-04-21 01:20:15 +02002318 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002319 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002320 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002321 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002322 }
Li Zefan77906a502011-07-14 03:16:00 +00002323 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302324 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002325 ret = 0;
2326out:
2327 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002328 return ret;
2329}
2330
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002331static int btrfs_search_path_in_tree_user(struct inode *inode,
2332 struct btrfs_ioctl_ino_lookup_user_args *args)
2333{
2334 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2335 struct super_block *sb = inode->i_sb;
2336 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2337 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2338 u64 dirid = args->dirid;
2339 unsigned long item_off;
2340 unsigned long item_len;
2341 struct btrfs_inode_ref *iref;
2342 struct btrfs_root_ref *rref;
2343 struct btrfs_root *root;
2344 struct btrfs_path *path;
2345 struct btrfs_key key, key2;
2346 struct extent_buffer *leaf;
2347 struct inode *temp_inode;
2348 char *ptr;
2349 int slot;
2350 int len;
2351 int total_len = 0;
2352 int ret;
2353
2354 path = btrfs_alloc_path();
2355 if (!path)
2356 return -ENOMEM;
2357
2358 /*
2359 * If the bottom subvolume does not exist directly under upper_limit,
2360 * construct the path in from the bottom up.
2361 */
2362 if (dirid != upper_limit.objectid) {
2363 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2364
2365 key.objectid = treeid;
2366 key.type = BTRFS_ROOT_ITEM_KEY;
2367 key.offset = (u64)-1;
2368 root = btrfs_read_fs_root_no_name(fs_info, &key);
2369 if (IS_ERR(root)) {
2370 ret = PTR_ERR(root);
2371 goto out;
2372 }
2373
2374 key.objectid = dirid;
2375 key.type = BTRFS_INODE_REF_KEY;
2376 key.offset = (u64)-1;
2377 while (1) {
2378 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2379 if (ret < 0) {
2380 goto out;
2381 } else if (ret > 0) {
2382 ret = btrfs_previous_item(root, path, dirid,
2383 BTRFS_INODE_REF_KEY);
2384 if (ret < 0) {
2385 goto out;
2386 } else if (ret > 0) {
2387 ret = -ENOENT;
2388 goto out;
2389 }
2390 }
2391
2392 leaf = path->nodes[0];
2393 slot = path->slots[0];
2394 btrfs_item_key_to_cpu(leaf, &key, slot);
2395
2396 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2397 len = btrfs_inode_ref_name_len(leaf, iref);
2398 ptr -= len + 1;
2399 total_len += len + 1;
2400 if (ptr < args->path) {
2401 ret = -ENAMETOOLONG;
2402 goto out;
2403 }
2404
2405 *(ptr + len) = '/';
2406 read_extent_buffer(leaf, ptr,
2407 (unsigned long)(iref + 1), len);
2408
2409 /* Check the read+exec permission of this directory */
2410 ret = btrfs_previous_item(root, path, dirid,
2411 BTRFS_INODE_ITEM_KEY);
2412 if (ret < 0) {
2413 goto out;
2414 } else if (ret > 0) {
2415 ret = -ENOENT;
2416 goto out;
2417 }
2418
2419 leaf = path->nodes[0];
2420 slot = path->slots[0];
2421 btrfs_item_key_to_cpu(leaf, &key2, slot);
2422 if (key2.objectid != dirid) {
2423 ret = -ENOENT;
2424 goto out;
2425 }
2426
2427 temp_inode = btrfs_iget(sb, &key2, root, NULL);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002428 if (IS_ERR(temp_inode)) {
2429 ret = PTR_ERR(temp_inode);
2430 goto out;
2431 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002432 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2433 iput(temp_inode);
2434 if (ret) {
2435 ret = -EACCES;
2436 goto out;
2437 }
2438
2439 if (key.offset == upper_limit.objectid)
2440 break;
2441 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2442 ret = -EACCES;
2443 goto out;
2444 }
2445
2446 btrfs_release_path(path);
2447 key.objectid = key.offset;
2448 key.offset = (u64)-1;
2449 dirid = key.objectid;
2450 }
2451
2452 memmove(args->path, ptr, total_len);
2453 args->path[total_len] = '\0';
2454 btrfs_release_path(path);
2455 }
2456
2457 /* Get the bottom subvolume's name from ROOT_REF */
2458 root = fs_info->tree_root;
2459 key.objectid = treeid;
2460 key.type = BTRFS_ROOT_REF_KEY;
2461 key.offset = args->treeid;
2462 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2463 if (ret < 0) {
2464 goto out;
2465 } else if (ret > 0) {
2466 ret = -ENOENT;
2467 goto out;
2468 }
2469
2470 leaf = path->nodes[0];
2471 slot = path->slots[0];
2472 btrfs_item_key_to_cpu(leaf, &key, slot);
2473
2474 item_off = btrfs_item_ptr_offset(leaf, slot);
2475 item_len = btrfs_item_size_nr(leaf, slot);
2476 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2477 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2478 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2479 ret = -EINVAL;
2480 goto out;
2481 }
2482
2483 /* Copy subvolume's name */
2484 item_off += sizeof(struct btrfs_root_ref);
2485 item_len -= sizeof(struct btrfs_root_ref);
2486 read_extent_buffer(leaf, args->name, item_off, item_len);
2487 args->name[item_len] = 0;
2488
2489out:
2490 btrfs_free_path(path);
2491 return ret;
2492}
2493
Chris Masonac8e9812010-02-28 15:39:26 -05002494static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2495 void __user *argp)
2496{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002497 struct btrfs_ioctl_ino_lookup_args *args;
2498 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002499 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002500
Julia Lawall2354d08f2010-10-29 15:14:18 -04002501 args = memdup_user(argp, sizeof(*args));
2502 if (IS_ERR(args))
2503 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002504
Al Viro496ad9a2013-01-23 17:07:38 -05002505 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002506
David Sterba01b810b2015-05-12 19:14:49 +02002507 /*
2508 * Unprivileged query to obtain the containing subvolume root id. The
2509 * path is reset so it's consistent with btrfs_search_path_in_tree.
2510 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002511 if (args->treeid == 0)
2512 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2513
David Sterba01b810b2015-05-12 19:14:49 +02002514 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2515 args->name[0] = 0;
2516 goto out;
2517 }
2518
2519 if (!capable(CAP_SYS_ADMIN)) {
2520 ret = -EPERM;
2521 goto out;
2522 }
2523
Chris Masonac8e9812010-02-28 15:39:26 -05002524 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2525 args->treeid, args->objectid,
2526 args->name);
2527
David Sterba01b810b2015-05-12 19:14:49 +02002528out:
Chris Masonac8e9812010-02-28 15:39:26 -05002529 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2530 ret = -EFAULT;
2531
2532 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002533 return ret;
2534}
2535
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002536/*
2537 * Version of ino_lookup ioctl (unprivileged)
2538 *
2539 * The main differences from ino_lookup ioctl are:
2540 *
2541 * 1. Read + Exec permission will be checked using inode_permission() during
2542 * path construction. -EACCES will be returned in case of failure.
2543 * 2. Path construction will be stopped at the inode number which corresponds
2544 * to the fd with which this ioctl is called. If constructed path does not
2545 * exist under fd's inode, -EACCES will be returned.
2546 * 3. The name of bottom subvolume is also searched and filled.
2547 */
2548static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2549{
2550 struct btrfs_ioctl_ino_lookup_user_args *args;
2551 struct inode *inode;
2552 int ret;
2553
2554 args = memdup_user(argp, sizeof(*args));
2555 if (IS_ERR(args))
2556 return PTR_ERR(args);
2557
2558 inode = file_inode(file);
2559
2560 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2561 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2562 /*
2563 * The subvolume does not exist under fd with which this is
2564 * called
2565 */
2566 kfree(args);
2567 return -EACCES;
2568 }
2569
2570 ret = btrfs_search_path_in_tree_user(inode, args);
2571
2572 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2573 ret = -EFAULT;
2574
2575 kfree(args);
2576 return ret;
2577}
2578
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002579/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2580static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2581{
2582 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2583 struct btrfs_fs_info *fs_info;
2584 struct btrfs_root *root;
2585 struct btrfs_path *path;
2586 struct btrfs_key key;
2587 struct btrfs_root_item *root_item;
2588 struct btrfs_root_ref *rref;
2589 struct extent_buffer *leaf;
2590 unsigned long item_off;
2591 unsigned long item_len;
2592 struct inode *inode;
2593 int slot;
2594 int ret = 0;
2595
2596 path = btrfs_alloc_path();
2597 if (!path)
2598 return -ENOMEM;
2599
2600 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2601 if (!subvol_info) {
2602 btrfs_free_path(path);
2603 return -ENOMEM;
2604 }
2605
2606 inode = file_inode(file);
2607 fs_info = BTRFS_I(inode)->root->fs_info;
2608
2609 /* Get root_item of inode's subvolume */
2610 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2611 key.type = BTRFS_ROOT_ITEM_KEY;
2612 key.offset = (u64)-1;
2613 root = btrfs_read_fs_root_no_name(fs_info, &key);
2614 if (IS_ERR(root)) {
2615 ret = PTR_ERR(root);
2616 goto out;
2617 }
2618 root_item = &root->root_item;
2619
2620 subvol_info->treeid = key.objectid;
2621
2622 subvol_info->generation = btrfs_root_generation(root_item);
2623 subvol_info->flags = btrfs_root_flags(root_item);
2624
2625 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2626 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2627 BTRFS_UUID_SIZE);
2628 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2629 BTRFS_UUID_SIZE);
2630
2631 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2632 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2633 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2634
2635 subvol_info->otransid = btrfs_root_otransid(root_item);
2636 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2637 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2638
2639 subvol_info->stransid = btrfs_root_stransid(root_item);
2640 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2641 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2642
2643 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2644 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2645 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2646
2647 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2648 /* Search root tree for ROOT_BACKREF of this subvolume */
2649 root = fs_info->tree_root;
2650
2651 key.type = BTRFS_ROOT_BACKREF_KEY;
2652 key.offset = 0;
2653 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2654 if (ret < 0) {
2655 goto out;
2656 } else if (path->slots[0] >=
2657 btrfs_header_nritems(path->nodes[0])) {
2658 ret = btrfs_next_leaf(root, path);
2659 if (ret < 0) {
2660 goto out;
2661 } else if (ret > 0) {
2662 ret = -EUCLEAN;
2663 goto out;
2664 }
2665 }
2666
2667 leaf = path->nodes[0];
2668 slot = path->slots[0];
2669 btrfs_item_key_to_cpu(leaf, &key, slot);
2670 if (key.objectid == subvol_info->treeid &&
2671 key.type == BTRFS_ROOT_BACKREF_KEY) {
2672 subvol_info->parent_id = key.offset;
2673
2674 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2675 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2676
2677 item_off = btrfs_item_ptr_offset(leaf, slot)
2678 + sizeof(struct btrfs_root_ref);
2679 item_len = btrfs_item_size_nr(leaf, slot)
2680 - sizeof(struct btrfs_root_ref);
2681 read_extent_buffer(leaf, subvol_info->name,
2682 item_off, item_len);
2683 } else {
2684 ret = -ENOENT;
2685 goto out;
2686 }
2687 }
2688
2689 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2690 ret = -EFAULT;
2691
2692out:
2693 btrfs_free_path(path);
2694 kzfree(subvol_info);
2695 return ret;
2696}
2697
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002698/*
2699 * Return ROOT_REF information of the subvolume containing this inode
2700 * except the subvolume name.
2701 */
2702static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2703{
2704 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2705 struct btrfs_root_ref *rref;
2706 struct btrfs_root *root;
2707 struct btrfs_path *path;
2708 struct btrfs_key key;
2709 struct extent_buffer *leaf;
2710 struct inode *inode;
2711 u64 objectid;
2712 int slot;
2713 int ret;
2714 u8 found;
2715
2716 path = btrfs_alloc_path();
2717 if (!path)
2718 return -ENOMEM;
2719
2720 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2721 if (IS_ERR(rootrefs)) {
2722 btrfs_free_path(path);
2723 return PTR_ERR(rootrefs);
2724 }
2725
2726 inode = file_inode(file);
2727 root = BTRFS_I(inode)->root->fs_info->tree_root;
2728 objectid = BTRFS_I(inode)->root->root_key.objectid;
2729
2730 key.objectid = objectid;
2731 key.type = BTRFS_ROOT_REF_KEY;
2732 key.offset = rootrefs->min_treeid;
2733 found = 0;
2734
2735 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2736 if (ret < 0) {
2737 goto out;
2738 } else if (path->slots[0] >=
2739 btrfs_header_nritems(path->nodes[0])) {
2740 ret = btrfs_next_leaf(root, path);
2741 if (ret < 0) {
2742 goto out;
2743 } else if (ret > 0) {
2744 ret = -EUCLEAN;
2745 goto out;
2746 }
2747 }
2748 while (1) {
2749 leaf = path->nodes[0];
2750 slot = path->slots[0];
2751
2752 btrfs_item_key_to_cpu(leaf, &key, slot);
2753 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2754 ret = 0;
2755 goto out;
2756 }
2757
2758 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2759 ret = -EOVERFLOW;
2760 goto out;
2761 }
2762
2763 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2764 rootrefs->rootref[found].treeid = key.offset;
2765 rootrefs->rootref[found].dirid =
2766 btrfs_root_ref_dirid(leaf, rref);
2767 found++;
2768
2769 ret = btrfs_next_item(root, path);
2770 if (ret < 0) {
2771 goto out;
2772 } else if (ret > 0) {
2773 ret = -EUCLEAN;
2774 goto out;
2775 }
2776 }
2777
2778out:
2779 if (!ret || ret == -EOVERFLOW) {
2780 rootrefs->num_items = found;
2781 /* update min_treeid for next search */
2782 if (found)
2783 rootrefs->min_treeid =
2784 rootrefs->rootref[found - 1].treeid + 1;
2785 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2786 ret = -EFAULT;
2787 }
2788
2789 kfree(rootrefs);
2790 btrfs_free_path(path);
2791
2792 return ret;
2793}
2794
Yan, Zheng76dda932009-09-21 16:00:26 -04002795static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2796 void __user *arg)
2797{
Al Viro54563d42013-09-01 15:57:51 -04002798 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002799 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002800 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002801 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002802 struct inode *inode;
2803 struct btrfs_root *root = BTRFS_I(dir)->root;
2804 struct btrfs_root *dest = NULL;
2805 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002806 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002807 int err = 0;
2808
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002809 if (!S_ISDIR(dir->i_mode))
2810 return -ENOTDIR;
2811
Yan, Zheng76dda932009-09-21 16:00:26 -04002812 vol_args = memdup_user(arg, sizeof(*vol_args));
2813 if (IS_ERR(vol_args))
2814 return PTR_ERR(vol_args);
2815
2816 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2817 namelen = strlen(vol_args->name);
2818 if (strchr(vol_args->name, '/') ||
2819 strncmp(vol_args->name, "..", namelen) == 0) {
2820 err = -EINVAL;
2821 goto out;
2822 }
2823
Al Viroa561be72011-11-23 11:57:51 -05002824 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002825 if (err)
2826 goto out;
2827
David Sterba521e0542014-04-15 16:41:44 +02002828
Al Viro00235412016-05-26 00:05:12 -04002829 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2830 if (err == -EINTR)
2831 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002832 dentry = lookup_one_len(vol_args->name, parent, namelen);
2833 if (IS_ERR(dentry)) {
2834 err = PTR_ERR(dentry);
2835 goto out_unlock_dir;
2836 }
2837
David Howells2b0143b2015-03-17 22:25:59 +00002838 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002839 err = -ENOENT;
2840 goto out_dput;
2841 }
2842
David Howells2b0143b2015-03-17 22:25:59 +00002843 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002844 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302845 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002846 /*
2847 * Regular user. Only allow this with a special mount
2848 * option, when the user has write+exec access to the
2849 * subvol root, and when rmdir(2) would have been
2850 * allowed.
2851 *
2852 * Note that this is _not_ check that the subvol is
2853 * empty or doesn't contain data that we wouldn't
2854 * otherwise be able to delete.
2855 *
2856 * Users who want to delete empty subvols should try
2857 * rmdir(2).
2858 */
2859 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002860 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002861 goto out_dput;
2862
2863 /*
2864 * Do not allow deletion if the parent dir is the same
2865 * as the dir to be deleted. That means the ioctl
2866 * must be called on the dentry referencing the root
2867 * of the subvol, not a random directory contained
2868 * within it.
2869 */
2870 err = -EINVAL;
2871 if (root == dest)
2872 goto out_dput;
2873
2874 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2875 if (err)
2876 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002877 }
2878
Miao Xie5c39da52012-10-22 11:39:53 +00002879 /* check if subvolume may be deleted by a user */
2880 err = btrfs_may_delete(dir, dentry, 1);
2881 if (err)
2882 goto out_dput;
2883
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002884 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002885 err = -EINVAL;
2886 goto out_dput;
2887 }
2888
Al Viro59551022016-01-22 15:40:57 -05002889 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002890 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002891 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002892 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002893 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002894
Yan, Zheng76dda932009-09-21 16:00:26 -04002895out_dput:
2896 dput(dentry);
2897out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002898 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002899out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002900 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002901out:
2902 kfree(vol_args);
2903 return err;
2904}
2905
Chris Mason1e701a32010-03-11 09:42:04 -05002906static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002907{
Al Viro496ad9a2013-01-23 17:07:38 -05002908 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002909 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002910 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002911 int ret;
2912
Ilya Dryomov25122d12013-01-20 15:57:57 +02002913 ret = mnt_want_write_file(file);
2914 if (ret)
2915 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002916
Ilya Dryomov25122d12013-01-20 15:57:57 +02002917 if (btrfs_root_readonly(root)) {
2918 ret = -EROFS;
2919 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002920 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002921
2922 switch (inode->i_mode & S_IFMT) {
2923 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002924 if (!capable(CAP_SYS_ADMIN)) {
2925 ret = -EPERM;
2926 goto out;
2927 }
Eric Sandeende78b512013-01-31 18:21:12 +00002928 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002929 break;
2930 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002931 if (!(file->f_mode & FMODE_WRITE)) {
2932 ret = -EINVAL;
2933 goto out;
2934 }
Chris Mason1e701a32010-03-11 09:42:04 -05002935
2936 range = kzalloc(sizeof(*range), GFP_KERNEL);
2937 if (!range) {
2938 ret = -ENOMEM;
2939 goto out;
2940 }
2941
2942 if (argp) {
2943 if (copy_from_user(range, argp,
2944 sizeof(*range))) {
2945 ret = -EFAULT;
2946 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002947 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002948 }
2949 /* compression requires us to start the IO */
2950 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2951 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2952 range->extent_thresh = (u32)-1;
2953 }
2954 } else {
2955 /* the rest are all set to zero by kzalloc */
2956 range->len = (u64)-1;
2957 }
Al Viro496ad9a2013-01-23 17:07:38 -05002958 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002959 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002960 if (ret > 0)
2961 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002962 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002963 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002964 default:
2965 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002966 }
Chris Masone441d542009-01-05 16:57:23 -05002967out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002968 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002969 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002970}
2971
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002972static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002973{
2974 struct btrfs_ioctl_vol_args *vol_args;
2975 int ret;
2976
Chris Masone441d542009-01-05 16:57:23 -05002977 if (!capable(CAP_SYS_ADMIN))
2978 return -EPERM;
2979
David Sterba171938e2017-03-28 14:44:21 +02002980 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002981 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002982
Li Zefandae7b662009-04-08 15:06:54 +08002983 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002984 if (IS_ERR(vol_args)) {
2985 ret = PTR_ERR(vol_args);
2986 goto out;
2987 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002988
Mark Fasheh5516e592008-07-24 12:20:14 -04002989 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002990 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002991
Anand Jain43d20762014-07-01 00:58:56 +08002992 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002993 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002994
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002995 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002996out:
David Sterba171938e2017-03-28 14:44:21 +02002997 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002998 return ret;
2999}
3000
Anand Jain6b526ed2016-02-13 10:01:39 +08003001static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003002{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 struct inode *inode = file_inode(file);
3004 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003005 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003006 int ret;
3007
Chris Masone441d542009-01-05 16:57:23 -05003008 if (!capable(CAP_SYS_ADMIN))
3009 return -EPERM;
3010
Miao Xieda249272012-11-26 08:44:50 +00003011 ret = mnt_want_write_file(file);
3012 if (ret)
3013 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003014
Li Zefandae7b662009-04-08 15:06:54 +08003015 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003016 if (IS_ERR(vol_args)) {
3017 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003018 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003019 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003020
Anand Jain6b526ed2016-02-13 10:01:39 +08003021 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003022 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3023 ret = -EOPNOTSUPP;
3024 goto out;
3025 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003026
David Sterba171938e2017-03-28 14:44:21 +02003027 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003028 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3029 goto out;
3030 }
3031
David Sterba735654e2016-02-15 18:15:21 +01003032 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003033 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003034 } else {
3035 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003036 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003037 }
David Sterba171938e2017-03-28 14:44:21 +02003038 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003039
Anand Jain6b526ed2016-02-13 10:01:39 +08003040 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003041 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003042 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003043 vol_args->devid);
3044 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003045 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003046 vol_args->name);
3047 }
Anand Jain183860f2013-05-17 10:52:45 +00003048out:
3049 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003050err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003051 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003052 return ret;
3053}
3054
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003055static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3056{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003057 struct inode *inode = file_inode(file);
3058 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003059 struct btrfs_ioctl_vol_args *vol_args;
3060 int ret;
3061
3062 if (!capable(CAP_SYS_ADMIN))
3063 return -EPERM;
3064
3065 ret = mnt_want_write_file(file);
3066 if (ret)
3067 return ret;
3068
David Sterba171938e2017-03-28 14:44:21 +02003069 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003070 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003071 goto out_drop_write;
3072 }
3073
3074 vol_args = memdup_user(arg, sizeof(*vol_args));
3075 if (IS_ERR(vol_args)) {
3076 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003077 goto out;
3078 }
3079
David Sterba58d7bbf2016-05-04 14:10:47 +02003080 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003081 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003082
3083 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003084 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003085 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003086out:
David Sterba171938e2017-03-28 14:44:21 +02003087 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003088out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003089 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003090
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003091 return ret;
3092}
3093
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003094static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3095 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003096{
Li Zefan027ed2f2011-06-08 08:27:56 +00003097 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003098 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003099 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003100 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003101
Li Zefan027ed2f2011-06-08 08:27:56 +00003102 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3103 if (!fi_args)
3104 return -ENOMEM;
3105
David Sterbad03262c2017-06-16 00:09:21 +02003106 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003107 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003108
David Sterbad03262c2017-06-16 00:09:21 +02003109 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003110 if (device->devid > fi_args->max_id)
3111 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003112 }
David Sterbad03262c2017-06-16 00:09:21 +02003113 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003114
David Sterbad03262c2017-06-16 00:09:21 +02003115 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003116 fi_args->nodesize = fs_info->nodesize;
3117 fi_args->sectorsize = fs_info->sectorsize;
3118 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003119
Li Zefan027ed2f2011-06-08 08:27:56 +00003120 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3121 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003122
Li Zefan027ed2f2011-06-08 08:27:56 +00003123 kfree(fi_args);
3124 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003125}
3126
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003127static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3128 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003129{
3130 struct btrfs_ioctl_dev_info_args *di_args;
3131 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003132 int ret = 0;
3133 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003134
Jan Schmidt475f6382011-03-11 15:41:01 +01003135 di_args = memdup_user(arg, sizeof(*di_args));
3136 if (IS_ERR(di_args))
3137 return PTR_ERR(di_args);
3138
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003139 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003140 s_uuid = di_args->uuid;
3141
David Sterbac5593ca2017-06-16 00:09:21 +02003142 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003143 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01003144
3145 if (!dev) {
3146 ret = -ENODEV;
3147 goto out;
3148 }
3149
3150 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003151 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3152 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003153 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003154 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04003155 struct rcu_string *name;
3156
Josef Bacik606686e2012-06-04 14:03:51 -04003157 name = rcu_dereference(dev->name);
Xiongfeng Wang6670d4c2018-01-08 19:51:22 +08003158 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003159 di_args->path[sizeof(di_args->path) - 1] = 0;
3160 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003161 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003162 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003163
3164out:
David Sterbac5593ca2017-06-16 00:09:21 +02003165 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003166 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3167 ret = -EFAULT;
3168
3169 kfree(di_args);
3170 return ret;
3171}
3172
Mark Fashehf4414602015-06-30 14:42:05 -07003173static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07003174{
3175 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07003176
Mark Fasheh416161d2013-08-06 11:42:51 -07003177 page = grab_cache_page(inode->i_mapping, index);
3178 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00003179 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07003180
3181 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00003182 int ret;
3183
3184 ret = btrfs_readpage(NULL, page);
3185 if (ret)
3186 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07003187 lock_page(page);
3188 if (!PageUptodate(page)) {
3189 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003190 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00003191 return ERR_PTR(-EIO);
3192 }
3193 if (page->mapping != inode->i_mapping) {
3194 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003195 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00003196 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07003197 }
3198 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003199
3200 return page;
3201}
3202
Mark Fashehf4414602015-06-30 14:42:05 -07003203static int gather_extent_pages(struct inode *inode, struct page **pages,
3204 int num_pages, u64 off)
3205{
3206 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003207 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003208
3209 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00003210again:
Mark Fashehf4414602015-06-30 14:42:05 -07003211 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00003212 if (IS_ERR(pages[i])) {
3213 int err = PTR_ERR(pages[i]);
3214
3215 if (err == -EAGAIN)
3216 goto again;
3217 pages[i] = NULL;
3218 return err;
3219 }
Mark Fashehf4414602015-06-30 14:42:05 -07003220 }
3221 return 0;
3222}
3223
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003224static int lock_extent_range(struct inode *inode, u64 off, u64 len,
3225 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003226{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003227 /*
3228 * Do any pending delalloc/csum calculations on inode, one way or
3229 * another, and lock file content.
3230 * The locking order is:
3231 *
3232 * 1) pages
3233 * 2) range in the inode's io tree
3234 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003235 while (1) {
3236 struct btrfs_ordered_extent *ordered;
3237 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
3238 ordered = btrfs_lookup_first_ordered_extent(inode,
3239 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003240 if ((!ordered ||
3241 ordered->file_offset + ordered->len <= off ||
3242 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003243 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003244 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
3245 if (ordered)
3246 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003247 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003248 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003249 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
3250 if (ordered)
3251 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003252 if (!retry_range_locking)
3253 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003254 btrfs_wait_ordered_range(inode, off, len);
3255 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003256 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003257}
3258
Mark Fashehf4414602015-06-30 14:42:05 -07003259static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07003260{
Al Viro59551022016-01-22 15:40:57 -05003261 inode_unlock(inode1);
3262 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07003263}
3264
Mark Fashehf4414602015-06-30 14:42:05 -07003265static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
3266{
3267 if (inode1 < inode2)
3268 swap(inode1, inode2);
3269
Al Viro59551022016-01-22 15:40:57 -05003270 inode_lock_nested(inode1, I_MUTEX_PARENT);
3271 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07003272}
3273
3274static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3275 struct inode *inode2, u64 loff2, u64 len)
3276{
3277 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3278 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3279}
3280
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003281static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3282 struct inode *inode2, u64 loff2, u64 len,
3283 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07003284{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003285 int ret;
3286
Mark Fasheh416161d2013-08-06 11:42:51 -07003287 if (inode1 < inode2) {
3288 swap(inode1, inode2);
3289 swap(loff1, loff2);
3290 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003291 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
3292 if (ret)
3293 return ret;
3294 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
3295 if (ret)
3296 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
3297 loff1 + len - 1);
3298 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003299}
3300
3301struct cmp_pages {
3302 int num_pages;
3303 struct page **src_pages;
3304 struct page **dst_pages;
3305};
3306
3307static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3308{
3309 int i;
3310 struct page *pg;
3311
3312 for (i = 0; i < cmp->num_pages; i++) {
3313 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003314 if (pg) {
3315 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003316 put_page(pg);
Naohiro Aota97b19172018-07-13 23:07:20 +09003317 cmp->src_pages[i] = NULL;
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003318 }
Mark Fashehf4414602015-06-30 14:42:05 -07003319 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003320 if (pg) {
3321 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003322 put_page(pg);
Naohiro Aota97b19172018-07-13 23:07:20 +09003323 cmp->dst_pages[i] = NULL;
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003324 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003325 }
Mark Fashehf4414602015-06-30 14:42:05 -07003326}
3327
3328static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3329 struct inode *dst, u64 dst_loff,
3330 u64 len, struct cmp_pages *cmp)
3331{
3332 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003333 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003334
Mark Fashehf4414602015-06-30 14:42:05 -07003335 cmp->num_pages = num_pages;
Mark Fashehf4414602015-06-30 14:42:05 -07003336
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003337 ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003338 if (ret)
3339 goto out;
3340
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003341 ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003342
3343out:
3344 if (ret)
3345 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09003346 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003347}
3348
David Sterba1a287cf2017-02-10 20:15:10 +01003349static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003350{
3351 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003352 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003353 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003354 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003355 void *addr, *dst_addr;
3356
Mark Fashehf4414602015-06-30 14:42:05 -07003357 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003358 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003359 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003360 cmp_len = len;
3361
Mark Fashehf4414602015-06-30 14:42:05 -07003362 BUG_ON(i >= cmp->num_pages);
3363
3364 src_page = cmp->src_pages[i];
3365 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003366 ASSERT(PageLocked(src_page));
3367 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003368
Mark Fasheh416161d2013-08-06 11:42:51 -07003369 addr = kmap_atomic(src_page);
3370 dst_addr = kmap_atomic(dst_page);
3371
3372 flush_dcache_page(src_page);
3373 flush_dcache_page(dst_page);
3374
3375 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003376 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003377
3378 kunmap_atomic(addr);
3379 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003380
3381 if (ret)
3382 break;
3383
Mark Fasheh416161d2013-08-06 11:42:51 -07003384 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003385 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003386 }
3387
3388 return ret;
3389}
3390
Mark Fashehe1d227a2015-06-08 15:05:25 -07003391static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3392 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003393{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003394 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003395 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3396
Mark Fashehe1d227a2015-06-08 15:05:25 -07003397 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003398 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003399
3400 /* if we extend to eof, continue to block boundary */
3401 if (off + len == inode->i_size)
3402 *plen = len = ALIGN(inode->i_size, bs) - off;
3403
Mark Fasheh416161d2013-08-06 11:42:51 -07003404 /* Check that we are block aligned - btrfs_clone() requires this */
3405 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3406 return -EINVAL;
3407
3408 return 0;
3409}
3410
Timofey Titovets39739092018-05-02 08:15:36 +03003411static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003412 struct inode *dst, u64 dst_loff,
3413 struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003414{
3415 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003416 u64 len = olen;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003417 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003418 u64 same_lock_start = 0;
3419 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003420
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003421 ret = extent_same_check_offsets(src, loff, &len, olen);
3422 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003423 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003424
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003425 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3426 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003427 return ret;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003428
3429 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003430 /*
3431 * Single inode case wants the same checks, except we
3432 * don't want our length pushed out past i_size as
3433 * comparing that data range makes no sense.
3434 *
3435 * extent_same_check_offsets() will do this for an
3436 * unaligned length at i_size, so catch it here and
3437 * reject the request.
3438 *
3439 * This effectively means we require aligned extents
3440 * for the single-inode case, whereas the other cases
3441 * allow an unaligned length so long as it ends at
3442 * i_size.
3443 */
Timofey Titovets39739092018-05-02 08:15:36 +03003444 if (len != olen)
3445 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003446
3447 /* Check for overlapping ranges */
Timofey Titovets39739092018-05-02 08:15:36 +03003448 if (dst_loff + len > loff && dst_loff < loff + len)
3449 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003450
3451 same_lock_start = min_t(u64, loff, dst_loff);
3452 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003453 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003454
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003455again:
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003456 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp);
Mark Fashehf4414602015-06-30 14:42:05 -07003457 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003458 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003459
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003460 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003461 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3462 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003463 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003464 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3465 false);
3466 /*
3467 * If one of the inodes has dirty pages in the respective range or
3468 * ordered extents, we need to flush dellaloc and wait for all ordered
3469 * extents in the range. We must unlock the pages and the ranges in the
3470 * io trees to avoid deadlocks when flushing delalloc (requires locking
3471 * pages) and when waiting for ordered extents to complete (they require
3472 * range locking).
3473 */
3474 if (ret == -EAGAIN) {
3475 /*
3476 * Ranges in the io trees already unlocked. Now unlock all
3477 * pages before waiting for all IO to complete.
3478 */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003479 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003480 if (same_inode) {
3481 btrfs_wait_ordered_range(src, same_lock_start,
3482 same_lock_len);
3483 } else {
3484 btrfs_wait_ordered_range(src, loff, len);
3485 btrfs_wait_ordered_range(dst, dst_loff, len);
3486 }
3487 goto again;
3488 }
3489 ASSERT(ret == 0);
3490 if (WARN_ON(ret)) {
3491 /* ranges in the io trees already unlocked */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003492 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003493 return ret;
3494 }
Mark Fashehf4414602015-06-30 14:42:05 -07003495
Mark Fasheh207910d2015-06-30 14:42:04 -07003496 /* pass original length for comparison so we stay within i_size */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003497 ret = btrfs_cmp_data(olen, cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003498 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003499 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003500
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003501 if (same_inode)
3502 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3503 same_lock_start + same_lock_len - 1);
3504 else
3505 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003506
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003507 btrfs_cmp_data_free(cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003508
3509 return ret;
3510}
3511
Timofey Titovetsb6728762018-05-02 08:15:37 +03003512#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3513
Timofey Titovets39739092018-05-02 08:15:36 +03003514static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3515 struct inode *dst, u64 dst_loff)
3516{
3517 int ret;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003518 struct cmp_pages cmp;
3519 int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
Timofey Titovets39739092018-05-02 08:15:36 +03003520 bool same_inode = (src == dst);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003521 u64 i, tail_len, chunk_count;
Timofey Titovets39739092018-05-02 08:15:36 +03003522
3523 if (olen == 0)
3524 return 0;
3525
3526 if (same_inode)
3527 inode_lock(src);
3528 else
3529 btrfs_double_inode_lock(src, dst);
3530
3531 /* don't make the dst file partly checksummed */
3532 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3533 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3534 ret = -EINVAL;
3535 goto out_unlock;
3536 }
3537
Timofey Titovetsb6728762018-05-02 08:15:37 +03003538 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3539 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003540 if (chunk_count == 0)
3541 num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
3542
3543 /*
3544 * If deduping ranges in the same inode, locking rules make it
3545 * mandatory to always lock pages in ascending order to avoid deadlocks
3546 * with concurrent tasks (such as starting writeback/delalloc).
3547 */
3548 if (same_inode && dst_loff < loff)
3549 swap(loff, dst_loff);
3550
3551 /*
3552 * We must gather up all the pages before we initiate our extent
3553 * locking. We use an array for the page pointers. Size of the array is
3554 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
3555 */
David Sterbabf5091c2018-05-11 17:57:54 +02003556 cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3557 GFP_KERNEL | __GFP_ZERO);
3558 cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3559 GFP_KERNEL | __GFP_ZERO);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003560 if (!cmp.src_pages || !cmp.dst_pages) {
David Sterbabf5091c2018-05-11 17:57:54 +02003561 ret = -ENOMEM;
3562 goto out_free;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003563 }
Timofey Titovetsb6728762018-05-02 08:15:37 +03003564
3565 for (i = 0; i < chunk_count; i++) {
3566 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003567 dst, dst_loff, &cmp);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003568 if (ret)
Lu Fengqi22883dd2018-06-19 14:54:38 +08003569 goto out_free;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003570
3571 loff += BTRFS_MAX_DEDUPE_LEN;
3572 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3573 }
3574
3575 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003576 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3577 dst_loff, &cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003578
Lu Fengqi22883dd2018-06-19 14:54:38 +08003579out_free:
3580 kvfree(cmp.src_pages);
3581 kvfree(cmp.dst_pages);
3582
Mark Fasheh416161d2013-08-06 11:42:51 -07003583out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003584 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003585 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003586 else
3587 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003588
3589 return ret;
3590}
3591
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003592ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3593 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003594{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003595 struct inode *src = file_inode(src_file);
3596 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003597 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003598 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003599
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003600 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003601 /*
3602 * Btrfs does not support blocksize < page_size. As a
3603 * result, btrfs_cmp_data() won't correctly handle
3604 * this situation without an update.
3605 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003606 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003607 }
3608
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003609 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3610 if (res)
3611 return res;
3612 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003613}
3614
Filipe Mananaf82a9902014-06-01 01:50:28 +01003615static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3616 struct inode *inode,
3617 u64 endoff,
3618 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003619 const u64 olen,
3620 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003621{
3622 struct btrfs_root *root = BTRFS_I(inode)->root;
3623 int ret;
3624
3625 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003626 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003627 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003628 /*
3629 * We round up to the block size at eof when determining which
3630 * extents to clone above, but shouldn't round up the file size.
3631 */
3632 if (endoff > destoff + olen)
3633 endoff = destoff + olen;
3634 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003635 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003636
3637 ret = btrfs_update_inode(trans, root, inode);
3638 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003639 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003640 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003641 goto out;
3642 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003643 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003644out:
3645 return ret;
3646}
3647
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003648static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003649 const struct btrfs_trans_handle *trans,
3650 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003651 const u64 hole_offset,
3652 const u64 hole_len)
3653{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003654 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003655 struct extent_map *em;
3656 int ret;
3657
3658 em = alloc_extent_map();
3659 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003660 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003661 return;
3662 }
3663
Filipe Manana14f59792014-06-29 21:45:40 +01003664 if (path) {
3665 struct btrfs_file_extent_item *fi;
3666
3667 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3668 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003669 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003670 em->generation = -1;
3671 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3672 BTRFS_FILE_EXTENT_INLINE)
3673 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003674 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003675 } else {
3676 em->start = hole_offset;
3677 em->len = hole_len;
3678 em->ram_bytes = em->len;
3679 em->orig_start = hole_offset;
3680 em->block_start = EXTENT_MAP_HOLE;
3681 em->block_len = 0;
3682 em->orig_block_len = 0;
3683 em->compress_type = BTRFS_COMPRESS_NONE;
3684 em->generation = trans->transid;
3685 }
3686
3687 while (1) {
3688 write_lock(&em_tree->lock);
3689 ret = add_extent_mapping(em_tree, em, 1);
3690 write_unlock(&em_tree->lock);
3691 if (ret != -EEXIST) {
3692 free_extent_map(em);
3693 break;
3694 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003695 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003696 em->start + em->len - 1, 0);
3697 }
3698
David Sterbaee39b432014-09-30 01:33:33 +02003699 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003700 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003701}
3702
Filipe Manana8039d872015-10-13 15:15:00 +01003703/*
3704 * Make sure we do not end up inserting an inline extent into a file that has
3705 * already other (non-inline) extents. If a file has an inline extent it can
3706 * not have any other extents and the (single) inline extent must start at the
3707 * file offset 0. Failing to respect these rules will lead to file corruption,
3708 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3709 *
3710 * We can have extents that have been already written to disk or we can have
3711 * dirty ranges still in delalloc, in which case the extent maps and items are
3712 * created only when we run delalloc, and the delalloc ranges might fall outside
3713 * the range we are currently locking in the inode's io tree. So we check the
3714 * inode's i_size because of that (i_size updates are done while holding the
3715 * i_mutex, which we are holding here).
3716 * We also check to see if the inode has a size not greater than "datal" but has
3717 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3718 * protected against such concurrent fallocate calls by the i_mutex).
3719 *
3720 * If the file has no extents but a size greater than datal, do not allow the
3721 * copy because we would need turn the inline extent into a non-inline one (even
3722 * with NO_HOLES enabled). If we find our destination inode only has one inline
3723 * extent, just overwrite it with the source inline extent if its size is less
3724 * than the source extent's size, or we could copy the source inline extent's
3725 * data into the destination inode's inline extent if the later is greater then
3726 * the former.
3727 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003728static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003729 struct btrfs_trans_handle *trans,
3730 struct btrfs_path *path,
3731 struct btrfs_key *new_key,
3732 const u64 drop_start,
3733 const u64 datal,
3734 const u64 skip,
3735 const u64 size,
3736 char *inline_data)
3737{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003738 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003739 struct btrfs_root *root = BTRFS_I(dst)->root;
3740 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003741 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003742 int ret;
3743 struct btrfs_key key;
3744
3745 if (new_key->offset > 0)
3746 return -EOPNOTSUPP;
3747
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003748 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003749 key.type = BTRFS_EXTENT_DATA_KEY;
3750 key.offset = 0;
3751 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3752 if (ret < 0) {
3753 return ret;
3754 } else if (ret > 0) {
3755 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3756 ret = btrfs_next_leaf(root, path);
3757 if (ret < 0)
3758 return ret;
3759 else if (ret > 0)
3760 goto copy_inline_extent;
3761 }
3762 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003763 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003764 key.type == BTRFS_EXTENT_DATA_KEY) {
3765 ASSERT(key.offset > 0);
3766 return -EOPNOTSUPP;
3767 }
3768 } else if (i_size_read(dst) <= datal) {
3769 struct btrfs_file_extent_item *ei;
3770 u64 ext_len;
3771
3772 /*
3773 * If the file size is <= datal, make sure there are no other
3774 * extents following (can happen do to an fallocate call with
3775 * the flag FALLOC_FL_KEEP_SIZE).
3776 */
3777 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3778 struct btrfs_file_extent_item);
3779 /*
3780 * If it's an inline extent, it can not have other extents
3781 * following it.
3782 */
3783 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3784 BTRFS_FILE_EXTENT_INLINE)
3785 goto copy_inline_extent;
3786
3787 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3788 if (ext_len > aligned_end)
3789 return -EOPNOTSUPP;
3790
3791 ret = btrfs_next_item(root, path);
3792 if (ret < 0) {
3793 return ret;
3794 } else if (ret == 0) {
3795 btrfs_item_key_to_cpu(path->nodes[0], &key,
3796 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003797 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003798 key.type == BTRFS_EXTENT_DATA_KEY)
3799 return -EOPNOTSUPP;
3800 }
3801 }
3802
3803copy_inline_extent:
3804 /*
3805 * We have no extent items, or we have an extent at offset 0 which may
3806 * or may not be inlined. All these cases are dealt the same way.
3807 */
3808 if (i_size_read(dst) > datal) {
3809 /*
3810 * If the destination inode has an inline extent...
3811 * This would require copying the data from the source inline
3812 * extent into the beginning of the destination's inline extent.
3813 * But this is really complex, both extents can be compressed
3814 * or just one of them, which would require decompressing and
3815 * re-compressing data (which could increase the new compressed
3816 * size, not allowing the compressed data to fit anymore in an
3817 * inline extent).
3818 * So just don't support this case for now (it should be rare,
3819 * we are not really saving space when cloning inline extents).
3820 */
3821 return -EOPNOTSUPP;
3822 }
3823
3824 btrfs_release_path(path);
3825 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3826 if (ret)
3827 return ret;
3828 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3829 if (ret)
3830 return ret;
3831
3832 if (skip) {
3833 const u32 start = btrfs_file_extent_calc_inline_size(0);
3834
3835 memmove(inline_data + start, inline_data + start + skip, datal);
3836 }
3837
3838 write_extent_buffer(path->nodes[0], inline_data,
3839 btrfs_item_ptr_offset(path->nodes[0],
3840 path->slots[0]),
3841 size);
3842 inode_add_bytes(dst, datal);
3843
3844 return 0;
3845}
3846
Mark Fasheh32b7c682013-08-06 11:42:49 -07003847/**
3848 * btrfs_clone() - clone a range from inode file to another
3849 *
3850 * @src: Inode to clone from
3851 * @inode: Inode to clone to
3852 * @off: Offset within source to start clone from
3853 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003854 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003855 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003856 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003857 */
3858static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003859 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003860 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003861{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003862 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003863 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003864 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003865 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003866 struct btrfs_trans_handle *trans;
3867 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003868 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003869 u32 nritems;
3870 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003871 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003872 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003873 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003874
3875 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003876 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3877 if (!buf)
3878 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003879
3880 path = btrfs_alloc_path();
3881 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003882 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003883 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003884 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003885
David Sterbae4058b52015-11-27 16:31:35 +01003886 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003887 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003888 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003889 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003890 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003891
3892 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003893 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003894
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003895 /*
3896 * note the key will change type as we walk through the
3897 * tree.
3898 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003899 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003900 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3901 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003902 if (ret < 0)
3903 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003904 /*
3905 * First search, if no extent item that starts at offset off was
3906 * found but the previous item is an extent item, it's possible
3907 * it might overlap our target range, therefore process it.
3908 */
3909 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3910 btrfs_item_key_to_cpu(path->nodes[0], &key,
3911 path->slots[0] - 1);
3912 if (key.type == BTRFS_EXTENT_DATA_KEY)
3913 path->slots[0]--;
3914 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003915
Yan Zhengae01a0a2008-08-04 23:23:47 -04003916 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003917process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003918 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003919 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003920 if (ret < 0)
3921 goto out;
3922 if (ret > 0)
3923 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003924 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003925 }
3926 leaf = path->nodes[0];
3927 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003928
Yan Zhengae01a0a2008-08-04 23:23:47 -04003929 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003930 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003931 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003932 break;
3933
David Sterba962a2982014-06-04 18:41:45 +02003934 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003935 struct btrfs_file_extent_item *extent;
3936 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003937 u32 size;
3938 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003939 u64 disko = 0, diskl = 0;
3940 u64 datao = 0, datal = 0;
3941 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003942 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003943
Sage Weilc5c9cd42008-11-12 14:32:25 -05003944 extent = btrfs_item_ptr(leaf, slot,
3945 struct btrfs_file_extent_item);
3946 comp = btrfs_file_extent_compression(leaf, extent);
3947 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003948 if (type == BTRFS_FILE_EXTENT_REG ||
3949 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003950 disko = btrfs_file_extent_disk_bytenr(leaf,
3951 extent);
3952 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3953 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003954 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003955 datal = btrfs_file_extent_num_bytes(leaf,
3956 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003957 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3958 /* take upper bound, may be compressed */
3959 datal = btrfs_file_extent_ram_bytes(leaf,
3960 extent);
3961 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003962
Filipe Manana2c463822014-05-31 02:31:05 +01003963 /*
3964 * The first search might have left us at an extent
3965 * item that ends before our target range's start, can
3966 * happen if we have holes and NO_HOLES feature enabled.
3967 */
3968 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003969 path->slots[0]++;
3970 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003971 } else if (key.offset >= off + len) {
3972 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003973 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003974 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003975 size = btrfs_item_size_nr(leaf, slot);
3976 read_extent_buffer(leaf, buf,
3977 btrfs_item_ptr_offset(leaf, slot),
3978 size);
3979
3980 btrfs_release_path(path);
3981 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003982
Zheng Yan31840ae2008-09-23 13:14:14 -04003983 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003984 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003985 if (off <= key.offset)
3986 new_key.offset = key.offset + destoff - off;
3987 else
3988 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003989
Sage Weilb6f34092011-09-20 14:48:51 -04003990 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003991 * Deal with a hole that doesn't have an extent item
3992 * that represents it (NO_HOLES feature enabled).
3993 * This hole is either in the middle of the cloning
3994 * range or at the beginning (fully overlaps it or
3995 * partially overlaps it).
3996 */
3997 if (new_key.offset != last_dest_end)
3998 drop_start = last_dest_end;
3999 else
4000 drop_start = new_key.offset;
4001
4002 /*
Sage Weilb6f34092011-09-20 14:48:51 -04004003 * 1 - adjusting old extent (we may have to split it)
4004 * 1 - add new extent
4005 * 1 - inode update
4006 */
4007 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004008 if (IS_ERR(trans)) {
4009 ret = PTR_ERR(trans);
4010 goto out;
4011 }
4012
Chris Masonc8a894d2009-06-27 21:07:03 -04004013 if (type == BTRFS_FILE_EXTENT_REG ||
4014 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04004015 /*
4016 * a | --- range to clone ---| b
4017 * | ------------- extent ------------- |
4018 */
4019
Antonio Ospite93915582014-06-04 14:03:48 +02004020 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04004021 if (key.offset + datal > off + len)
4022 datal = off + len - key.offset;
4023
Antonio Ospite93915582014-06-04 14:03:48 +02004024 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025 if (off > key.offset) {
4026 datao += off - key.offset;
4027 datal -= off - key.offset;
4028 }
4029
Josef Bacik5dc562c2012-08-17 13:14:17 -04004030 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01004031 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04004033 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004034 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02004035 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08004036 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04004037 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004038 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004039 goto out;
4040 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004041
Sage Weilc5c9cd42008-11-12 14:32:25 -05004042 ret = btrfs_insert_empty_item(trans, root, path,
4043 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004044 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004045 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004046 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004047 goto out;
4048 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05004049
4050 leaf = path->nodes[0];
4051 slot = path->slots[0];
4052 write_extent_buffer(leaf, buf,
4053 btrfs_item_ptr_offset(leaf, slot),
4054 size);
4055
4056 extent = btrfs_item_ptr(leaf, slot,
4057 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004058
Sage Weilc5c9cd42008-11-12 14:32:25 -05004059 /* disko == 0 means it's a hole */
4060 if (!disko)
4061 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004062
4063 btrfs_set_file_extent_offset(leaf, extent,
4064 datao);
4065 btrfs_set_file_extent_num_bytes(leaf, extent,
4066 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07004067
Sage Weilc5c9cd42008-11-12 14:32:25 -05004068 if (disko) {
4069 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004070 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004071 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004072 disko, diskl, 0,
4073 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004074 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004075 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004076 if (ret) {
4077 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004078 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004079 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004080 goto out;
4081
4082 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05004083 }
4084 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
4085 u64 skip = 0;
4086 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01004087
Sage Weilc5c9cd42008-11-12 14:32:25 -05004088 if (off > key.offset) {
4089 skip = off - key.offset;
4090 new_key.offset += skip;
4091 }
Chris Masond3977122009-01-05 21:25:51 -05004092
Liu Boaa42ffd2012-09-18 03:52:23 -06004093 if (key.offset + datal > off + len)
4094 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05004095
Sage Weilc5c9cd42008-11-12 14:32:25 -05004096 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05004097 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004098 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004099 goto out;
4100 }
4101 size -= skip + trim;
4102 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004103
David Sterba4a0ab9d2017-02-10 20:18:49 +01004104 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01004105 trans, path,
4106 &new_key,
4107 drop_start,
4108 datal,
4109 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02004111 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07004112 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01004113 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004114 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004115 goto out;
4116 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05004117 leaf = path->nodes[0];
4118 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05004119 }
4120
Filipe Manana7ffbb592014-06-09 03:48:05 +01004121 /* If we have an implicit hole (NO_HOLES feature). */
4122 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02004123 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01004124 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01004125 new_key.offset - drop_start);
4126
Nikolay Borisova2f392e2017-02-20 13:51:04 +02004127 clone_update_extent_map(BTRFS_I(inode), trans,
4128 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01004129
Sage Weilc5c9cd42008-11-12 14:32:25 -05004130 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02004131 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004132
Filipe Manana62e23902014-08-08 02:47:06 +01004133 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004134 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004135 ret = clone_finish_inode_update(trans, inode,
4136 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07004137 destoff, olen,
4138 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004139 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004140 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01004141 if (new_key.offset + datal >= destoff + len)
4142 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004143 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004144 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01004145 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08004146
4147 if (fatal_signal_pending(current)) {
4148 ret = -EINTR;
4149 goto out;
4150 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004151 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004152 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004153
Filipe Mananaf82a9902014-06-01 01:50:28 +01004154 if (last_dest_end < destoff + len) {
4155 /*
4156 * We have an implicit hole (NO_HOLES feature is enabled) that
4157 * fully or partially overlaps our cloning range at its end.
4158 */
4159 btrfs_release_path(path);
4160
4161 /*
4162 * 1 - remove extent(s)
4163 * 1 - inode update
4164 */
4165 trans = btrfs_start_transaction(root, 2);
4166 if (IS_ERR(trans)) {
4167 ret = PTR_ERR(trans);
4168 goto out;
4169 }
4170 ret = btrfs_drop_extents(trans, root, inode,
4171 last_dest_end, destoff + len, 1);
4172 if (ret) {
4173 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04004174 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004175 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004176 goto out;
4177 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02004178 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
4179 last_dest_end,
4180 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004181 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07004182 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004183 }
4184
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004185out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07004186 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02004187 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004188 return ret;
4189}
4190
Zach Brown3db11b22015-11-10 16:53:32 -05004191static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
4192 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07004193{
Al Viro54563d42013-09-01 15:57:51 -04004194 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05004195 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004196 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004197 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004198 int ret;
4199 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004200 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05004201 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004202
4203 /*
4204 * TODO:
4205 * - split compressed inline extents. annoying: we need to
4206 * decompress into destination's address_space (the file offset
4207 * may change, so source mapping won't do), then recompress (or
4208 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08004209 *
4210 * - split destination inode's inline extents. The inline extents can
4211 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07004212 */
4213
Mark Fasheh32b7c682013-08-06 11:42:49 -07004214 if (btrfs_root_readonly(root))
4215 return -EROFS;
4216
Zach Brown3db11b22015-11-10 16:53:32 -05004217 if (file_src->f_path.mnt != file->f_path.mnt ||
4218 src->i_sb != inode->i_sb)
4219 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004220
Mark Fasheh32b7c682013-08-06 11:42:49 -07004221 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05004222 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004223
4224 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07004225 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004226 } else {
Al Viro59551022016-01-22 15:40:57 -05004227 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004228 }
4229
Omar Sandovalb5c40d52018-05-22 15:02:12 -07004230 /* don't make the dst file partly checksummed */
4231 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
4232 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
4233 ret = -EINVAL;
4234 goto out_unlock;
4235 }
4236
Mark Fasheh32b7c682013-08-06 11:42:49 -07004237 /* determine range to clone */
4238 ret = -EINVAL;
4239 if (off + len > src->i_size || off + len < off)
4240 goto out_unlock;
4241 if (len == 0)
4242 olen = len = src->i_size - off;
4243 /* if we extend to eof, continue to block boundary */
4244 if (off + len == src->i_size)
4245 len = ALIGN(src->i_size, bs) - off;
4246
Filipe Mananaccccf3d62015-03-30 18:23:59 +01004247 if (len == 0) {
4248 ret = 0;
4249 goto out_unlock;
4250 }
4251
Mark Fasheh32b7c682013-08-06 11:42:49 -07004252 /* verify the end result is block aligned */
4253 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
4254 !IS_ALIGNED(destoff, bs))
4255 goto out_unlock;
4256
4257 /* verify if ranges are overlapped within the same file */
4258 if (same_inode) {
4259 if (destoff + len > off && destoff < off + len)
4260 goto out_unlock;
4261 }
4262
4263 if (destoff > inode->i_size) {
4264 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
4265 if (ret)
4266 goto out_unlock;
4267 }
4268
Filipe Mananac125b8b2014-05-23 05:03:34 +01004269 /*
4270 * Lock the target range too. Right after we replace the file extent
4271 * items in the fs tree (which now point to the cloned data), we might
4272 * have a worker replace them with extent items relative to a write
4273 * operation that was issued before this clone operation (i.e. confront
4274 * with inode.c:btrfs_finish_ordered_io).
4275 */
4276 if (same_inode) {
4277 u64 lock_start = min_t(u64, off, destoff);
4278 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004279
Filipe Mananae0bd70c2016-01-27 10:20:58 +00004280 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01004281 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00004282 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
4283 true);
4284 }
4285 ASSERT(ret == 0);
4286 if (WARN_ON(ret)) {
4287 /* ranges in the io trees already unlocked */
4288 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01004289 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07004290
Mark Fasheh1c919a52015-06-30 14:42:08 -07004291 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004292
Filipe Mananac125b8b2014-05-23 05:03:34 +01004293 if (same_inode) {
4294 u64 lock_start = min_t(u64, off, destoff);
4295 u64 lock_end = max_t(u64, off, destoff) + len - 1;
4296
4297 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
4298 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07004299 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01004300 }
4301 /*
4302 * Truncate page cache pages so that future reads will see the cloned
4303 * data immediately and not the previous data.
4304 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05304305 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004306 round_down(destoff, PAGE_SIZE),
4307 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004308out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07004309 if (!same_inode)
4310 btrfs_double_inode_unlock(src, inode);
4311 else
Al Viro59551022016-01-22 15:40:57 -05004312 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05004313 return ret;
4314}
4315
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004316int btrfs_clone_file_range(struct file *src_file, loff_t off,
4317 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05004318{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004319 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004320}
4321
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004322static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4323{
Al Viro496ad9a2013-01-23 17:07:38 -05004324 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004325 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004326 struct btrfs_root *root = BTRFS_I(inode)->root;
4327 struct btrfs_root *new_root;
4328 struct btrfs_dir_item *di;
4329 struct btrfs_trans_handle *trans;
4330 struct btrfs_path *path;
4331 struct btrfs_key location;
4332 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004333 u64 objectid = 0;
4334 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004335 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004336
4337 if (!capable(CAP_SYS_ADMIN))
4338 return -EPERM;
4339
Miao Xie3c04ce02012-11-26 08:43:07 +00004340 ret = mnt_want_write_file(file);
4341 if (ret)
4342 return ret;
4343
4344 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4345 ret = -EFAULT;
4346 goto out;
4347 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004348
4349 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304350 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004351
4352 location.objectid = objectid;
4353 location.type = BTRFS_ROOT_ITEM_KEY;
4354 location.offset = (u64)-1;
4355
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004356 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004357 if (IS_ERR(new_root)) {
4358 ret = PTR_ERR(new_root);
4359 goto out;
4360 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004361 if (!is_fstree(new_root->objectid)) {
4362 ret = -ENOENT;
4363 goto out;
4364 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004365
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004366 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004367 if (!path) {
4368 ret = -ENOMEM;
4369 goto out;
4370 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004371 path->leave_spinning = 1;
4372
4373 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004374 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004375 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004376 ret = PTR_ERR(trans);
4377 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004378 }
4379
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004380 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4381 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004382 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004383 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004384 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004385 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004386 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004387 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004388 ret = -ENOENT;
4389 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004390 }
4391
4392 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4393 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4394 btrfs_mark_buffer_dirty(path->nodes[0]);
4395 btrfs_free_path(path);
4396
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004397 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004398 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004399out:
4400 mnt_drop_write_file(file);
4401 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004402}
4403
Su Yuec065f5b12018-04-02 17:24:11 +08004404static void get_block_group_info(struct list_head *groups_list,
4405 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004406{
4407 struct btrfs_block_group_cache *block_group;
4408
4409 space->total_bytes = 0;
4410 space->used_bytes = 0;
4411 space->flags = 0;
4412 list_for_each_entry(block_group, groups_list, list) {
4413 space->flags = block_group->flags;
4414 space->total_bytes += block_group->key.offset;
4415 space->used_bytes +=
4416 btrfs_block_group_used(&block_group->item);
4417 }
4418}
4419
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004420static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4421 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004422{
4423 struct btrfs_ioctl_space_args space_args;
4424 struct btrfs_ioctl_space_info space;
4425 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004426 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004427 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004428 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004429 static const u64 types[] = {
4430 BTRFS_BLOCK_GROUP_DATA,
4431 BTRFS_BLOCK_GROUP_SYSTEM,
4432 BTRFS_BLOCK_GROUP_METADATA,
4433 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4434 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004435 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004436 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004437 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004438 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004439 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004440
4441 if (copy_from_user(&space_args,
4442 (struct btrfs_ioctl_space_args __user *)arg,
4443 sizeof(space_args)))
4444 return -EFAULT;
4445
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004446 for (i = 0; i < num_types; i++) {
4447 struct btrfs_space_info *tmp;
4448
4449 info = NULL;
4450 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004451 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004452 list) {
4453 if (tmp->flags == types[i]) {
4454 info = tmp;
4455 break;
4456 }
4457 }
4458 rcu_read_unlock();
4459
4460 if (!info)
4461 continue;
4462
4463 down_read(&info->groups_sem);
4464 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4465 if (!list_empty(&info->block_groups[c]))
4466 slot_count++;
4467 }
4468 up_read(&info->groups_sem);
4469 }
Josef Bacik1406e432010-01-13 18:19:06 +00004470
David Sterba36523e952014-02-07 14:34:12 +01004471 /*
4472 * Global block reserve, exported as a space_info
4473 */
4474 slot_count++;
4475
Chris Mason7fde62b2010-03-16 15:40:10 -04004476 /* space_slots == 0 means they are asking for a count */
4477 if (space_args.space_slots == 0) {
4478 space_args.total_spaces = slot_count;
4479 goto out;
4480 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004481
Dan Rosenberg51788b12011-02-14 16:04:23 -05004482 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004483
Chris Mason7fde62b2010-03-16 15:40:10 -04004484 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004485
Chris Mason7fde62b2010-03-16 15:40:10 -04004486 /* we generally have at most 6 or so space infos, one for each raid
4487 * level. So, a whole page should be more than enough for everyone
4488 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004489 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004490 return -ENOMEM;
4491
4492 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004493 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004494 if (!dest)
4495 return -ENOMEM;
4496 dest_orig = dest;
4497
4498 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004499 for (i = 0; i < num_types; i++) {
4500 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004501
Dan Rosenberg51788b12011-02-14 16:04:23 -05004502 if (!slot_count)
4503 break;
4504
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004505 info = NULL;
4506 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004507 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004508 list) {
4509 if (tmp->flags == types[i]) {
4510 info = tmp;
4511 break;
4512 }
4513 }
4514 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004515
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004516 if (!info)
4517 continue;
4518 down_read(&info->groups_sem);
4519 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4520 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004521 get_block_group_info(&info->block_groups[c],
4522 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004523 memcpy(dest, &space, sizeof(space));
4524 dest++;
4525 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004526 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004527 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004528 if (!slot_count)
4529 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004530 }
4531 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004532 }
Josef Bacik1406e432010-01-13 18:19:06 +00004533
David Sterba36523e952014-02-07 14:34:12 +01004534 /*
4535 * Add global block reserve
4536 */
4537 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004538 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004539
4540 spin_lock(&block_rsv->lock);
4541 space.total_bytes = block_rsv->size;
4542 space.used_bytes = block_rsv->size - block_rsv->reserved;
4543 spin_unlock(&block_rsv->lock);
4544 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4545 memcpy(dest, &space, sizeof(space));
4546 space_args.total_spaces++;
4547 }
4548
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004549 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004550 (arg + sizeof(struct btrfs_ioctl_space_args));
4551
4552 if (copy_to_user(user_dest, dest_orig, alloc_size))
4553 ret = -EFAULT;
4554
4555 kfree(dest_orig);
4556out:
4557 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004558 ret = -EFAULT;
4559
4560 return ret;
4561}
4562
Miao Xie9a8c28b2012-11-26 08:40:43 +00004563static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4564 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004565{
Sage Weil46204592010-10-29 15:41:32 -04004566 struct btrfs_trans_handle *trans;
4567 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004568 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004569
Miao Xied4edf392013-02-20 09:17:06 +00004570 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004571 if (IS_ERR(trans)) {
4572 if (PTR_ERR(trans) != -ENOENT)
4573 return PTR_ERR(trans);
4574
4575 /* No running transaction, don't bother */
4576 transid = root->fs_info->last_trans_committed;
4577 goto out;
4578 }
Sage Weil46204592010-10-29 15:41:32 -04004579 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004580 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004581 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004582 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004583 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004584 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004585out:
Sage Weil46204592010-10-29 15:41:32 -04004586 if (argp)
4587 if (copy_to_user(argp, &transid, sizeof(transid)))
4588 return -EFAULT;
4589 return 0;
4590}
4591
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004592static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004593 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004594{
Sage Weil46204592010-10-29 15:41:32 -04004595 u64 transid;
4596
4597 if (argp) {
4598 if (copy_from_user(&transid, argp, sizeof(transid)))
4599 return -EFAULT;
4600 } else {
4601 transid = 0; /* current trans */
4602 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004603 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004604}
4605
Miao Xieb8e95482012-11-26 08:48:01 +00004606static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004607{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004608 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004609 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004610 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004611
4612 if (!capable(CAP_SYS_ADMIN))
4613 return -EPERM;
4614
4615 sa = memdup_user(arg, sizeof(*sa));
4616 if (IS_ERR(sa))
4617 return PTR_ERR(sa);
4618
Miao Xieb8e95482012-11-26 08:48:01 +00004619 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4620 ret = mnt_want_write_file(file);
4621 if (ret)
4622 goto out;
4623 }
4624
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004625 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004626 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4627 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004628
4629 if (copy_to_user(arg, sa, sizeof(*sa)))
4630 ret = -EFAULT;
4631
Miao Xieb8e95482012-11-26 08:48:01 +00004632 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4633 mnt_drop_write_file(file);
4634out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004635 kfree(sa);
4636 return ret;
4637}
4638
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004639static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004640{
4641 if (!capable(CAP_SYS_ADMIN))
4642 return -EPERM;
4643
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004644 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004645}
4646
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004647static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004648 void __user *arg)
4649{
4650 struct btrfs_ioctl_scrub_args *sa;
4651 int ret;
4652
4653 if (!capable(CAP_SYS_ADMIN))
4654 return -EPERM;
4655
4656 sa = memdup_user(arg, sizeof(*sa));
4657 if (IS_ERR(sa))
4658 return PTR_ERR(sa);
4659
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004660 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004661
4662 if (copy_to_user(arg, sa, sizeof(*sa)))
4663 ret = -EFAULT;
4664
4665 kfree(sa);
4666 return ret;
4667}
4668
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004669static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004670 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004671{
4672 struct btrfs_ioctl_get_dev_stats *sa;
4673 int ret;
4674
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004675 sa = memdup_user(arg, sizeof(*sa));
4676 if (IS_ERR(sa))
4677 return PTR_ERR(sa);
4678
David Sterbab27f7c02012-06-22 06:30:39 -06004679 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4680 kfree(sa);
4681 return -EPERM;
4682 }
4683
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004684 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004685
4686 if (copy_to_user(arg, sa, sizeof(*sa)))
4687 ret = -EFAULT;
4688
4689 kfree(sa);
4690 return ret;
4691}
4692
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004693static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4694 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004695{
4696 struct btrfs_ioctl_dev_replace_args *p;
4697 int ret;
4698
4699 if (!capable(CAP_SYS_ADMIN))
4700 return -EPERM;
4701
4702 p = memdup_user(arg, sizeof(*p));
4703 if (IS_ERR(p))
4704 return PTR_ERR(p);
4705
4706 switch (p->cmd) {
4707 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004708 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004709 ret = -EROFS;
4710 goto out;
4711 }
David Sterba171938e2017-03-28 14:44:21 +02004712 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004713 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004714 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004715 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004716 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004717 }
4718 break;
4719 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004720 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004721 ret = 0;
4722 break;
4723 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004724 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004725 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004726 break;
4727 default:
4728 ret = -EINVAL;
4729 break;
4730 }
4731
4732 if (copy_to_user(arg, p, sizeof(*p)))
4733 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004734out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004735 kfree(p);
4736 return ret;
4737}
4738
Jan Schmidtd7728c92011-07-07 16:48:38 +02004739static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4740{
4741 int ret = 0;
4742 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004743 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004744 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004745 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004746 struct inode_fs_paths *ipath = NULL;
4747 struct btrfs_path *path;
4748
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004749 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004750 return -EPERM;
4751
4752 path = btrfs_alloc_path();
4753 if (!path) {
4754 ret = -ENOMEM;
4755 goto out;
4756 }
4757
4758 ipa = memdup_user(arg, sizeof(*ipa));
4759 if (IS_ERR(ipa)) {
4760 ret = PTR_ERR(ipa);
4761 ipa = NULL;
4762 goto out;
4763 }
4764
4765 size = min_t(u32, ipa->size, 4096);
4766 ipath = init_ipath(size, root, path);
4767 if (IS_ERR(ipath)) {
4768 ret = PTR_ERR(ipath);
4769 ipath = NULL;
4770 goto out;
4771 }
4772
4773 ret = paths_from_inode(ipa->inum, ipath);
4774 if (ret < 0)
4775 goto out;
4776
4777 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004778 rel_ptr = ipath->fspath->val[i] -
4779 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004780 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004781 }
4782
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004783 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4784 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004785 if (ret) {
4786 ret = -EFAULT;
4787 goto out;
4788 }
4789
4790out:
4791 btrfs_free_path(path);
4792 free_ipath(ipath);
4793 kfree(ipa);
4794
4795 return ret;
4796}
4797
4798static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4799{
4800 struct btrfs_data_container *inodes = ctx;
4801 const size_t c = 3 * sizeof(u64);
4802
4803 if (inodes->bytes_left >= c) {
4804 inodes->bytes_left -= c;
4805 inodes->val[inodes->elem_cnt] = inum;
4806 inodes->val[inodes->elem_cnt + 1] = offset;
4807 inodes->val[inodes->elem_cnt + 2] = root;
4808 inodes->elem_cnt += 3;
4809 } else {
4810 inodes->bytes_missing += c - inodes->bytes_left;
4811 inodes->bytes_left = 0;
4812 inodes->elem_missed += 3;
4813 }
4814
4815 return 0;
4816}
4817
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004818static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004819 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004820{
4821 int ret = 0;
4822 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004823 struct btrfs_ioctl_logical_ino_args *loi;
4824 struct btrfs_data_container *inodes = NULL;
4825 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004826 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004827
4828 if (!capable(CAP_SYS_ADMIN))
4829 return -EPERM;
4830
4831 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304832 if (IS_ERR(loi))
4833 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004834
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004835 if (version == 1) {
4836 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004837 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004838 } else {
4839 /* All reserved bits must be 0 for now */
4840 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4841 ret = -EINVAL;
4842 goto out_loi;
4843 }
4844 /* Only accept flags we have defined so far */
4845 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4846 ret = -EINVAL;
4847 goto out_loi;
4848 }
4849 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004850 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004851 }
4852
Jan Schmidtd7728c92011-07-07 16:48:38 +02004853 path = btrfs_alloc_path();
4854 if (!path) {
4855 ret = -ENOMEM;
4856 goto out;
4857 }
4858
Jan Schmidtd7728c92011-07-07 16:48:38 +02004859 inodes = init_data_container(size);
4860 if (IS_ERR(inodes)) {
4861 ret = PTR_ERR(inodes);
4862 inodes = NULL;
4863 goto out;
4864 }
4865
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004866 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004867 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004868 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004869 ret = -ENOENT;
4870 if (ret < 0)
4871 goto out;
4872
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004873 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4874 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004875 if (ret)
4876 ret = -EFAULT;
4877
4878out:
4879 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004880 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004881out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004882 kfree(loi);
4883
4884 return ret;
4885}
4886
David Sterba008ef092018-03-21 02:05:27 +01004887void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004888 struct btrfs_ioctl_balance_args *bargs)
4889{
4890 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4891
4892 bargs->flags = bctl->flags;
4893
David Sterba3009a622018-03-21 01:31:04 +01004894 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004895 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4896 if (atomic_read(&fs_info->balance_pause_req))
4897 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004898 if (atomic_read(&fs_info->balance_cancel_req))
4899 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004900
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004901 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4902 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4903 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004904
David Sterba008ef092018-03-21 02:05:27 +01004905 spin_lock(&fs_info->balance_lock);
4906 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4907 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004908}
4909
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004910static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004911{
Al Viro496ad9a2013-01-23 17:07:38 -05004912 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004913 struct btrfs_fs_info *fs_info = root->fs_info;
4914 struct btrfs_ioctl_balance_args *bargs;
4915 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004916 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004917 int ret;
4918
4919 if (!capable(CAP_SYS_ADMIN))
4920 return -EPERM;
4921
Liu Boe54bfa312012-06-29 03:58:48 -06004922 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004923 if (ret)
4924 return ret;
4925
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004926again:
David Sterba171938e2017-03-28 14:44:21 +02004927 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004928 mutex_lock(&fs_info->balance_mutex);
4929 need_unlock = true;
4930 goto locked;
4931 }
4932
4933 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004934 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004935 * (1) some other op is running
4936 * (2) balance is running
4937 * (3) balance is paused -- special case (think resume)
4938 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004939 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004940 if (fs_info->balance_ctl) {
4941 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004942 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004943 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004944 /*
4945 * Lock released to allow other waiters to continue,
4946 * we'll reexamine the status again.
4947 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004948 mutex_lock(&fs_info->balance_mutex);
4949
4950 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004951 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004952 /* this is (3) */
4953 need_unlock = false;
4954 goto locked;
4955 }
4956
4957 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004958 goto again;
4959 } else {
4960 /* this is (2) */
4961 mutex_unlock(&fs_info->balance_mutex);
4962 ret = -EINPROGRESS;
4963 goto out;
4964 }
4965 } else {
4966 /* this is (1) */
4967 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004968 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004969 goto out;
4970 }
4971
4972locked:
David Sterba171938e2017-03-28 14:44:21 +02004973 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004974
4975 if (arg) {
4976 bargs = memdup_user(arg, sizeof(*bargs));
4977 if (IS_ERR(bargs)) {
4978 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004979 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004980 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004981
4982 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4983 if (!fs_info->balance_ctl) {
4984 ret = -ENOTCONN;
4985 goto out_bargs;
4986 }
4987
4988 bctl = fs_info->balance_ctl;
4989 spin_lock(&fs_info->balance_lock);
4990 bctl->flags |= BTRFS_BALANCE_RESUME;
4991 spin_unlock(&fs_info->balance_lock);
4992
4993 goto do_balance;
4994 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004995 } else {
4996 bargs = NULL;
4997 }
4998
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004999 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005000 ret = -EINPROGRESS;
5001 goto out_bargs;
5002 }
5003
David Sterba8d2db782015-11-04 15:38:29 +01005004 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005005 if (!bctl) {
5006 ret = -ENOMEM;
5007 goto out_bargs;
5008 }
5009
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005010 if (arg) {
5011 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
5012 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
5013 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
5014
5015 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02005016 } else {
5017 /* balance everything - no filters */
5018 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005019 }
5020
David Sterba8eb93452015-10-12 16:55:54 +02005021 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
5022 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02005023 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02005024 }
5025
Ilya Dryomovde322262012-01-16 22:04:49 +02005026do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005027 /*
David Sterba149196a2018-03-20 20:23:09 +01005028 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
5029 * btrfs_balance. bctl is freed in reset_balance_state, or, if
5030 * restriper was paused all the way until unmount, in free_fs_info.
5031 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005032 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005033 need_unlock = false;
5034
David Sterba6fcf6e22018-05-07 17:44:03 +02005035 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02005036 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005037
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005038 if (arg) {
5039 if (copy_to_user(arg, bargs, sizeof(*bargs)))
5040 ret = -EFAULT;
5041 }
5042
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02005043out_bctl:
5044 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005045out_bargs:
5046 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005047out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005048 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005049 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02005050 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005051out:
Liu Boe54bfa312012-06-29 03:58:48 -06005052 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005053 return ret;
5054}
5055
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005056static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005057{
5058 if (!capable(CAP_SYS_ADMIN))
5059 return -EPERM;
5060
5061 switch (cmd) {
5062 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005063 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02005064 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005065 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005066 }
5067
5068 return -EINVAL;
5069}
5070
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005071static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005072 void __user *arg)
5073{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005074 struct btrfs_ioctl_balance_args *bargs;
5075 int ret = 0;
5076
5077 if (!capable(CAP_SYS_ADMIN))
5078 return -EPERM;
5079
5080 mutex_lock(&fs_info->balance_mutex);
5081 if (!fs_info->balance_ctl) {
5082 ret = -ENOTCONN;
5083 goto out;
5084 }
5085
David Sterba8d2db782015-11-04 15:38:29 +01005086 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005087 if (!bargs) {
5088 ret = -ENOMEM;
5089 goto out;
5090 }
5091
David Sterba008ef092018-03-21 02:05:27 +01005092 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005093
5094 if (copy_to_user(arg, bargs, sizeof(*bargs)))
5095 ret = -EFAULT;
5096
5097 kfree(bargs);
5098out:
5099 mutex_unlock(&fs_info->balance_mutex);
5100 return ret;
5101}
5102
Miao Xie905b0dd2012-11-26 08:50:11 +00005103static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005104{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005105 struct inode *inode = file_inode(file);
5106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02005107 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02005108 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005109
5110 if (!capable(CAP_SYS_ADMIN))
5111 return -EPERM;
5112
Miao Xie905b0dd2012-11-26 08:50:11 +00005113 ret = mnt_want_write_file(file);
5114 if (ret)
5115 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005116
5117 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005118 if (IS_ERR(sa)) {
5119 ret = PTR_ERR(sa);
5120 goto drop_write;
5121 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005122
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005123 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02005124
5125 switch (sa->cmd) {
5126 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03005127 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02005128 break;
5129 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03005130 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02005131 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02005132 default:
5133 ret = -EINVAL;
5134 break;
5135 }
5136
Arne Jansen5d13a372011-09-14 15:53:51 +02005137 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005138 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00005139drop_write:
5140 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005141 return ret;
5142}
5143
Miao Xie905b0dd2012-11-26 08:50:11 +00005144static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005145{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005146 struct inode *inode = file_inode(file);
5147 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5148 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005149 struct btrfs_ioctl_qgroup_assign_args *sa;
5150 struct btrfs_trans_handle *trans;
5151 int ret;
5152 int err;
5153
5154 if (!capable(CAP_SYS_ADMIN))
5155 return -EPERM;
5156
Miao Xie905b0dd2012-11-26 08:50:11 +00005157 ret = mnt_want_write_file(file);
5158 if (ret)
5159 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005160
5161 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005162 if (IS_ERR(sa)) {
5163 ret = PTR_ERR(sa);
5164 goto drop_write;
5165 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005166
5167 trans = btrfs_join_transaction(root);
5168 if (IS_ERR(trans)) {
5169 ret = PTR_ERR(trans);
5170 goto out;
5171 }
5172
Arne Jansen5d13a372011-09-14 15:53:51 +02005173 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08005174 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02005175 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08005176 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02005177 }
5178
Qu Wenruoe082f562015-02-27 16:24:28 +08005179 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005180 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08005181 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005182 btrfs_handle_fs_error(fs_info, err,
5183 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005184 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005185 if (err && !ret)
5186 ret = err;
5187
5188out:
5189 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005190drop_write:
5191 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005192 return ret;
5193}
5194
Miao Xie905b0dd2012-11-26 08:50:11 +00005195static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005196{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005197 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005198 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005199 struct btrfs_ioctl_qgroup_create_args *sa;
5200 struct btrfs_trans_handle *trans;
5201 int ret;
5202 int err;
5203
5204 if (!capable(CAP_SYS_ADMIN))
5205 return -EPERM;
5206
Miao Xie905b0dd2012-11-26 08:50:11 +00005207 ret = mnt_want_write_file(file);
5208 if (ret)
5209 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005210
5211 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005212 if (IS_ERR(sa)) {
5213 ret = PTR_ERR(sa);
5214 goto drop_write;
5215 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005216
Miao Xied86e56c2012-11-15 11:35:41 +00005217 if (!sa->qgroupid) {
5218 ret = -EINVAL;
5219 goto out;
5220 }
5221
Arne Jansen5d13a372011-09-14 15:53:51 +02005222 trans = btrfs_join_transaction(root);
5223 if (IS_ERR(trans)) {
5224 ret = PTR_ERR(trans);
5225 goto out;
5226 }
5227
Arne Jansen5d13a372011-09-14 15:53:51 +02005228 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08005229 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02005230 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08005231 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02005232 }
5233
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005234 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005235 if (err && !ret)
5236 ret = err;
5237
5238out:
5239 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005240drop_write:
5241 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005242 return ret;
5243}
5244
Miao Xie905b0dd2012-11-26 08:50:11 +00005245static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005246{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005247 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005248 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005249 struct btrfs_ioctl_qgroup_limit_args *sa;
5250 struct btrfs_trans_handle *trans;
5251 int ret;
5252 int err;
5253 u64 qgroupid;
5254
5255 if (!capable(CAP_SYS_ADMIN))
5256 return -EPERM;
5257
Miao Xie905b0dd2012-11-26 08:50:11 +00005258 ret = mnt_want_write_file(file);
5259 if (ret)
5260 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005261
5262 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005263 if (IS_ERR(sa)) {
5264 ret = PTR_ERR(sa);
5265 goto drop_write;
5266 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005267
5268 trans = btrfs_join_transaction(root);
5269 if (IS_ERR(trans)) {
5270 ret = PTR_ERR(trans);
5271 goto out;
5272 }
5273
5274 qgroupid = sa->qgroupid;
5275 if (!qgroupid) {
5276 /* take the current subvol as qgroup */
5277 qgroupid = root->root_key.objectid;
5278 }
5279
Lu Fengqif0042d52018-07-18 14:45:35 +08005280 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005281
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005282 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005283 if (err && !ret)
5284 ret = err;
5285
5286out:
5287 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005288drop_write:
5289 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005290 return ret;
5291}
5292
Jan Schmidt2f232032013-04-25 16:04:51 +00005293static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5294{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005295 struct inode *inode = file_inode(file);
5296 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005297 struct btrfs_ioctl_quota_rescan_args *qsa;
5298 int ret;
5299
5300 if (!capable(CAP_SYS_ADMIN))
5301 return -EPERM;
5302
5303 ret = mnt_want_write_file(file);
5304 if (ret)
5305 return ret;
5306
5307 qsa = memdup_user(arg, sizeof(*qsa));
5308 if (IS_ERR(qsa)) {
5309 ret = PTR_ERR(qsa);
5310 goto drop_write;
5311 }
5312
5313 if (qsa->flags) {
5314 ret = -EINVAL;
5315 goto out;
5316 }
5317
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005318 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005319
5320out:
5321 kfree(qsa);
5322drop_write:
5323 mnt_drop_write_file(file);
5324 return ret;
5325}
5326
5327static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005329 struct inode *inode = file_inode(file);
5330 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005331 struct btrfs_ioctl_quota_rescan_args *qsa;
5332 int ret = 0;
5333
5334 if (!capable(CAP_SYS_ADMIN))
5335 return -EPERM;
5336
David Sterba8d2db782015-11-04 15:38:29 +01005337 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005338 if (!qsa)
5339 return -ENOMEM;
5340
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005341 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005342 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005343 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005344 }
5345
5346 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5347 ret = -EFAULT;
5348
5349 kfree(qsa);
5350 return ret;
5351}
5352
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005353static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005355 struct inode *inode = file_inode(file);
5356 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005357
5358 if (!capable(CAP_SYS_ADMIN))
5359 return -EPERM;
5360
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005361 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005362}
5363
Hugo Millsabccd002014-01-30 20:17:00 +00005364static long _btrfs_ioctl_set_received_subvol(struct file *file,
5365 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005366{
Al Viro496ad9a2013-01-23 17:07:38 -05005367 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005368 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005369 struct btrfs_root *root = BTRFS_I(inode)->root;
5370 struct btrfs_root_item *root_item = &root->root_item;
5371 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07005372 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005373 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005374 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005375
David Sterbabd60ea02014-01-16 15:50:22 +01005376 if (!inode_owner_or_capable(inode))
5377 return -EPERM;
5378
Alexander Block8ea05e32012-07-25 17:35:53 +02005379 ret = mnt_want_write_file(file);
5380 if (ret < 0)
5381 return ret;
5382
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005383 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005384
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005385 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005386 ret = -EINVAL;
5387 goto out;
5388 }
5389
5390 if (btrfs_root_readonly(root)) {
5391 ret = -EROFS;
5392 goto out;
5393 }
5394
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005395 /*
5396 * 1 - root item
5397 * 2 - uuid items (received uuid + subvol uuid)
5398 */
5399 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005400 if (IS_ERR(trans)) {
5401 ret = PTR_ERR(trans);
5402 trans = NULL;
5403 goto out;
5404 }
5405
5406 sa->rtransid = trans->transid;
5407 sa->rtime.sec = ct.tv_sec;
5408 sa->rtime.nsec = ct.tv_nsec;
5409
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005410 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5411 BTRFS_UUID_SIZE);
5412 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005413 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005414 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005415 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5416 root->root_key.objectid);
5417 if (ret && ret != -ENOENT) {
5418 btrfs_abort_transaction(trans, ret);
5419 btrfs_end_transaction(trans);
5420 goto out;
5421 }
5422 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005423 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5424 btrfs_set_root_stransid(root_item, sa->stransid);
5425 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005426 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5427 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5428 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5429 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005430
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005431 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005432 &root->root_key, &root->root_item);
5433 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005434 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005435 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005436 }
5437 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005438 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005439 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5440 root->root_key.objectid);
5441 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005442 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005443 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005444 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005445 }
5446 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005447 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005448out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005449 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005450 mnt_drop_write_file(file);
5451 return ret;
5452}
5453
5454#ifdef CONFIG_64BIT
5455static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5456 void __user *arg)
5457{
5458 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5459 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5460 int ret = 0;
5461
5462 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305463 if (IS_ERR(args32))
5464 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005465
David Sterba8d2db782015-11-04 15:38:29 +01005466 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005467 if (!args64) {
5468 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005469 goto out;
5470 }
5471
5472 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5473 args64->stransid = args32->stransid;
5474 args64->rtransid = args32->rtransid;
5475 args64->stime.sec = args32->stime.sec;
5476 args64->stime.nsec = args32->stime.nsec;
5477 args64->rtime.sec = args32->rtime.sec;
5478 args64->rtime.nsec = args32->rtime.nsec;
5479 args64->flags = args32->flags;
5480
5481 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5482 if (ret)
5483 goto out;
5484
5485 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5486 args32->stransid = args64->stransid;
5487 args32->rtransid = args64->rtransid;
5488 args32->stime.sec = args64->stime.sec;
5489 args32->stime.nsec = args64->stime.nsec;
5490 args32->rtime.sec = args64->rtime.sec;
5491 args32->rtime.nsec = args64->rtime.nsec;
5492 args32->flags = args64->flags;
5493
5494 ret = copy_to_user(arg, args32, sizeof(*args32));
5495 if (ret)
5496 ret = -EFAULT;
5497
5498out:
5499 kfree(args32);
5500 kfree(args64);
5501 return ret;
5502}
5503#endif
5504
5505static long btrfs_ioctl_set_received_subvol(struct file *file,
5506 void __user *arg)
5507{
5508 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5509 int ret = 0;
5510
5511 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305512 if (IS_ERR(sa))
5513 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005514
5515 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5516
5517 if (ret)
5518 goto out;
5519
Alexander Block8ea05e32012-07-25 17:35:53 +02005520 ret = copy_to_user(arg, sa, sizeof(*sa));
5521 if (ret)
5522 ret = -EFAULT;
5523
5524out:
5525 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005526 return ret;
5527}
5528
jeff.liu867ab662013-01-05 02:48:01 +00005529static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5530{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005531 struct inode *inode = file_inode(file);
5532 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005533 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005534 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005535 char label[BTRFS_LABEL_SIZE];
5536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005537 spin_lock(&fs_info->super_lock);
5538 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5539 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005540
5541 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005542
5543 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005544 btrfs_warn(fs_info,
5545 "label is too long, return the first %zu bytes",
5546 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005547 }
5548
jeff.liu867ab662013-01-05 02:48:01 +00005549 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005550
5551 return ret ? -EFAULT : 0;
5552}
5553
jeff.liua8bfd4a2013-01-05 02:48:08 +00005554static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005556 struct inode *inode = file_inode(file);
5557 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5558 struct btrfs_root *root = BTRFS_I(inode)->root;
5559 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005560 struct btrfs_trans_handle *trans;
5561 char label[BTRFS_LABEL_SIZE];
5562 int ret;
5563
5564 if (!capable(CAP_SYS_ADMIN))
5565 return -EPERM;
5566
5567 if (copy_from_user(label, arg, sizeof(label)))
5568 return -EFAULT;
5569
5570 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005571 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005572 "unable to set label with more than %d bytes",
5573 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005574 return -EINVAL;
5575 }
5576
5577 ret = mnt_want_write_file(file);
5578 if (ret)
5579 return ret;
5580
jeff.liua8bfd4a2013-01-05 02:48:08 +00005581 trans = btrfs_start_transaction(root, 0);
5582 if (IS_ERR(trans)) {
5583 ret = PTR_ERR(trans);
5584 goto out_unlock;
5585 }
5586
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005587 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005588 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005589 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005590 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005591
5592out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005593 mnt_drop_write_file(file);
5594 return ret;
5595}
5596
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005597#define INIT_FEATURE_FLAGS(suffix) \
5598 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5599 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5600 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5601
David Sterbad5131b62016-02-17 15:26:27 +01005602int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005603{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005604 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005605 INIT_FEATURE_FLAGS(SUPP),
5606 INIT_FEATURE_FLAGS(SAFE_SET),
5607 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5608 };
5609
5610 if (copy_to_user(arg, &features, sizeof(features)))
5611 return -EFAULT;
5612
5613 return 0;
5614}
5615
5616static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5617{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005618 struct inode *inode = file_inode(file);
5619 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5620 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005621 struct btrfs_ioctl_feature_flags features;
5622
5623 features.compat_flags = btrfs_super_compat_flags(super_block);
5624 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5625 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5626
5627 if (copy_to_user(arg, &features, sizeof(features)))
5628 return -EFAULT;
5629
5630 return 0;
5631}
5632
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005633static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005634 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005635 u64 change_mask, u64 flags, u64 supported_flags,
5636 u64 safe_set, u64 safe_clear)
5637{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005638 const char *type = btrfs_feature_set_names[set];
5639 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005640 u64 disallowed, unsupported;
5641 u64 set_mask = flags & change_mask;
5642 u64 clear_mask = ~flags & change_mask;
5643
5644 unsupported = set_mask & ~supported_flags;
5645 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005646 names = btrfs_printable_features(set, unsupported);
5647 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005648 btrfs_warn(fs_info,
5649 "this kernel does not support the %s feature bit%s",
5650 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005651 kfree(names);
5652 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005653 btrfs_warn(fs_info,
5654 "this kernel does not support %s bits 0x%llx",
5655 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005656 return -EOPNOTSUPP;
5657 }
5658
5659 disallowed = set_mask & ~safe_set;
5660 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005661 names = btrfs_printable_features(set, disallowed);
5662 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005663 btrfs_warn(fs_info,
5664 "can't set the %s feature bit%s while mounted",
5665 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005666 kfree(names);
5667 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005668 btrfs_warn(fs_info,
5669 "can't set %s bits 0x%llx while mounted",
5670 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005671 return -EPERM;
5672 }
5673
5674 disallowed = clear_mask & ~safe_clear;
5675 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005676 names = btrfs_printable_features(set, disallowed);
5677 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005678 btrfs_warn(fs_info,
5679 "can't clear the %s feature bit%s while mounted",
5680 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005681 kfree(names);
5682 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005683 btrfs_warn(fs_info,
5684 "can't clear %s bits 0x%llx while mounted",
5685 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005686 return -EPERM;
5687 }
5688
5689 return 0;
5690}
5691
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005692#define check_feature(fs_info, change_mask, flags, mask_base) \
5693check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005694 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5695 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5696 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5697
5698static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5699{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005700 struct inode *inode = file_inode(file);
5701 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5702 struct btrfs_root *root = BTRFS_I(inode)->root;
5703 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005704 struct btrfs_ioctl_feature_flags flags[2];
5705 struct btrfs_trans_handle *trans;
5706 u64 newflags;
5707 int ret;
5708
5709 if (!capable(CAP_SYS_ADMIN))
5710 return -EPERM;
5711
5712 if (copy_from_user(flags, arg, sizeof(flags)))
5713 return -EFAULT;
5714
5715 /* Nothing to do */
5716 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5717 !flags[0].incompat_flags)
5718 return 0;
5719
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005720 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005721 flags[1].compat_flags, COMPAT);
5722 if (ret)
5723 return ret;
5724
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005725 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005726 flags[1].compat_ro_flags, COMPAT_RO);
5727 if (ret)
5728 return ret;
5729
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005730 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005731 flags[1].incompat_flags, INCOMPAT);
5732 if (ret)
5733 return ret;
5734
David Sterba7ab19622016-05-04 11:32:00 +02005735 ret = mnt_want_write_file(file);
5736 if (ret)
5737 return ret;
5738
David Sterba8051aa12014-02-07 14:34:04 +01005739 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005740 if (IS_ERR(trans)) {
5741 ret = PTR_ERR(trans);
5742 goto out_drop_write;
5743 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005744
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005745 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005746 newflags = btrfs_super_compat_flags(super_block);
5747 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5748 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5749 btrfs_set_super_compat_flags(super_block, newflags);
5750
5751 newflags = btrfs_super_compat_ro_flags(super_block);
5752 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5753 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5754 btrfs_set_super_compat_ro_flags(super_block, newflags);
5755
5756 newflags = btrfs_super_incompat_flags(super_block);
5757 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5758 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5759 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005760 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005761
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005762 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005763out_drop_write:
5764 mnt_drop_write_file(file);
5765
5766 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005767}
5768
Josef Bacik2351f432017-09-27 10:43:13 -04005769static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5770{
5771 struct btrfs_ioctl_send_args *arg;
5772 int ret;
5773
5774 if (compat) {
5775#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5776 struct btrfs_ioctl_send_args_32 args32;
5777
5778 ret = copy_from_user(&args32, argp, sizeof(args32));
5779 if (ret)
5780 return -EFAULT;
5781 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5782 if (!arg)
5783 return -ENOMEM;
5784 arg->send_fd = args32.send_fd;
5785 arg->clone_sources_count = args32.clone_sources_count;
5786 arg->clone_sources = compat_ptr(args32.clone_sources);
5787 arg->parent_root = args32.parent_root;
5788 arg->flags = args32.flags;
5789 memcpy(arg->reserved, args32.reserved,
5790 sizeof(args32.reserved));
5791#else
5792 return -ENOTTY;
5793#endif
5794 } else {
5795 arg = memdup_user(argp, sizeof(*arg));
5796 if (IS_ERR(arg))
5797 return PTR_ERR(arg);
5798 }
5799 ret = btrfs_ioctl_send(file, arg);
5800 kfree(arg);
5801 return ret;
5802}
5803
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005804long btrfs_ioctl(struct file *file, unsigned int
5805 cmd, unsigned long arg)
5806{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005807 struct inode *inode = file_inode(file);
5808 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5809 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005810 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005811
5812 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005813 case FS_IOC_GETFLAGS:
5814 return btrfs_ioctl_getflags(file, argp);
5815 case FS_IOC_SETFLAGS:
5816 return btrfs_ioctl_setflags(file, argp);
5817 case FS_IOC_GETVERSION:
5818 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005819 case FITRIM:
5820 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005821 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005822 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005823 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005824 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005825 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005826 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005827 case BTRFS_IOC_SUBVOL_CREATE_V2:
5828 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005829 case BTRFS_IOC_SNAP_DESTROY:
5830 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005831 case BTRFS_IOC_SUBVOL_GETFLAGS:
5832 return btrfs_ioctl_subvol_getflags(file, argp);
5833 case BTRFS_IOC_SUBVOL_SETFLAGS:
5834 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005835 case BTRFS_IOC_DEFAULT_SUBVOL:
5836 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005837 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005838 return btrfs_ioctl_defrag(file, NULL);
5839 case BTRFS_IOC_DEFRAG_RANGE:
5840 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005841 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005842 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005843 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005844 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005845 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005846 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005847 case BTRFS_IOC_RM_DEV_V2:
5848 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005849 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005850 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005851 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005852 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005853 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005854 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005855 case BTRFS_IOC_TREE_SEARCH:
5856 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005857 case BTRFS_IOC_TREE_SEARCH_V2:
5858 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005859 case BTRFS_IOC_INO_LOOKUP:
5860 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005861 case BTRFS_IOC_INO_PATHS:
5862 return btrfs_ioctl_ino_to_path(root, argp);
5863 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005864 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5865 case BTRFS_IOC_LOGICAL_INO_V2:
5866 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005867 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005868 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005869 case BTRFS_IOC_SYNC: {
5870 int ret;
5871
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005872 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005873 if (ret)
5874 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005875 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005876 /*
5877 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005878 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005879 * processing uncleaned subvols.
5880 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005881 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005882 return ret;
5883 }
Sage Weil46204592010-10-29 15:41:32 -04005884 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005885 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005886 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005887 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005888 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005889 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005890 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005891 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005892 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005893 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005894 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005895 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005896 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005897 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005898 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005899 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005900 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5901 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005902#ifdef CONFIG_64BIT
5903 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5904 return btrfs_ioctl_set_received_subvol_32(file, argp);
5905#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005906 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005907 return _btrfs_ioctl_send(file, argp, false);
5908#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5909 case BTRFS_IOC_SEND_32:
5910 return _btrfs_ioctl_send(file, argp, true);
5911#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005912 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005913 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005914 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005915 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005916 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005917 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005918 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005919 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005920 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005921 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005922 case BTRFS_IOC_QUOTA_RESCAN:
5923 return btrfs_ioctl_quota_rescan(file, argp);
5924 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5925 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005926 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5927 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005928 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005929 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005930 case BTRFS_IOC_GET_FSLABEL:
5931 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005932 case BTRFS_IOC_SET_FSLABEL:
5933 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005934 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005935 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005936 case BTRFS_IOC_GET_FEATURES:
5937 return btrfs_ioctl_get_features(file, argp);
5938 case BTRFS_IOC_SET_FEATURES:
5939 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005940 case FS_IOC_FSGETXATTR:
5941 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005942 case FS_IOC_FSSETXATTR:
5943 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005944 case BTRFS_IOC_GET_SUBVOL_INFO:
5945 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005946 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5947 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005948 case BTRFS_IOC_INO_LOOKUP_USER:
5949 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005950 }
5951
5952 return -ENOTTY;
5953}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005954
5955#ifdef CONFIG_COMPAT
5956long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5957{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005958 /*
5959 * These all access 32-bit values anyway so no further
5960 * handling is necessary.
5961 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005962 switch (cmd) {
5963 case FS_IOC32_GETFLAGS:
5964 cmd = FS_IOC_GETFLAGS;
5965 break;
5966 case FS_IOC32_SETFLAGS:
5967 cmd = FS_IOC_SETFLAGS;
5968 break;
5969 case FS_IOC32_GETVERSION:
5970 cmd = FS_IOC_GETVERSION;
5971 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005972 }
5973
5974 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5975}
5976#endif