blob: ba1608d0cd8ec3c3b7114febdb5b078b05c3cda3 [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"
Josef Bacik8719aaa2019-06-18 16:09:16 -040046#include "space-info.h"
Josef Bacik86736342019-06-19 15:12:00 -040047#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040048#include "block-group.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040049
Hugo Millsabccd002014-01-30 20:17:00 +000050#ifdef CONFIG_64BIT
51/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
52 * structures are incorrect, as the timespec structure from userspace
53 * is 4 bytes too small. We define these alternatives here to teach
54 * the kernel about the 32-bit struct packing.
55 */
56struct btrfs_ioctl_timespec_32 {
57 __u64 sec;
58 __u32 nsec;
59} __attribute__ ((__packed__));
60
61struct btrfs_ioctl_received_subvol_args_32 {
62 char uuid[BTRFS_UUID_SIZE]; /* in */
63 __u64 stransid; /* in */
64 __u64 rtransid; /* out */
65 struct btrfs_ioctl_timespec_32 stime; /* in */
66 struct btrfs_ioctl_timespec_32 rtime; /* out */
67 __u64 flags; /* in */
68 __u64 reserved[16]; /* in */
69} __attribute__ ((__packed__));
70
71#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
72 struct btrfs_ioctl_received_subvol_args_32)
73#endif
74
Josef Bacik2351f432017-09-27 10:43:13 -040075#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
76struct btrfs_ioctl_send_args_32 {
77 __s64 send_fd; /* in */
78 __u64 clone_sources_count; /* in */
79 compat_uptr_t clone_sources; /* in */
80 __u64 parent_root; /* in */
81 __u64 flags; /* in */
82 __u64 reserved[4]; /* in */
83} __attribute__ ((__packed__));
84
85#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
86 struct btrfs_ioctl_send_args_32)
87#endif
Hugo Millsabccd002014-01-30 20:17:00 +000088
Mark Fasheh416161d2013-08-06 11:42:51 -070089static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070090 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
91 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070092
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020094static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
95 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020096{
David Sterba1905a0f2018-03-26 18:52:15 +020097 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020098 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020099 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200100 return flags & ~FS_DIRSYNC_FL;
101 else
102 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
103}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400104
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200106 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
107 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200108 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200109static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200110{
111 unsigned int iflags = 0;
112
113 if (flags & BTRFS_INODE_SYNC)
114 iflags |= FS_SYNC_FL;
115 if (flags & BTRFS_INODE_IMMUTABLE)
116 iflags |= FS_IMMUTABLE_FL;
117 if (flags & BTRFS_INODE_APPEND)
118 iflags |= FS_APPEND_FL;
119 if (flags & BTRFS_INODE_NODUMP)
120 iflags |= FS_NODUMP_FL;
121 if (flags & BTRFS_INODE_NOATIME)
122 iflags |= FS_NOATIME_FL;
123 if (flags & BTRFS_INODE_DIRSYNC)
124 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000125 if (flags & BTRFS_INODE_NODATACOW)
126 iflags |= FS_NOCOW_FL;
127
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900128 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000129 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900130 else if (flags & BTRFS_INODE_COMPRESS)
131 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200132
133 return iflags;
134}
135
136/*
137 * Update inode->i_flags based on the btrfs internal flags.
138 */
David Sterba7b6a2212018-03-26 18:40:21 +0200139void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140{
David Sterba5c57b8b2018-04-23 15:45:18 +0200141 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100142 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143
David Sterba5c57b8b2018-04-23 15:45:18 +0200144 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100145 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200146 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100147 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200148 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100149 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200150 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100151 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200152 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100153 new_fl |= S_DIRSYNC;
154
155 set_mask_bits(&inode->i_flags,
156 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
157 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200158}
159
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
161{
David Sterba5c57b8b2018-04-23 15:45:18 +0200162 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
163 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200164
165 if (copy_to_user(arg, &flags, sizeof(flags)))
166 return -EFAULT;
167 return 0;
168}
169
David Sterba5ba76ab2018-03-26 18:52:15 +0200170/* Check if @flags are a supported and valid set of FS_*_FL flags */
171static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000172{
173 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
174 FS_NOATIME_FL | FS_NODUMP_FL | \
175 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000176 FS_NOCOMP_FL | FS_COMPR_FL |
177 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000178 return -EOPNOTSUPP;
179
180 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
181 return -EINVAL;
182
Liu Bo75e7cb72011-03-22 10:12:20 +0000183 return 0;
184}
185
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200186static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
187{
Al Viro496ad9a2013-01-23 17:07:38 -0500188 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400189 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200190 struct btrfs_inode *binode = BTRFS_I(inode);
191 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200192 struct btrfs_trans_handle *trans;
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700193 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194 int ret;
Anand Jainff9fef552019-04-20 19:48:53 +0800195 const char *comp = NULL;
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800196 u32 binode_flags = binode->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200197
David Sterbabd60ea02014-01-16 15:50:22 +0100198 if (!inode_owner_or_capable(inode))
199 return -EPERM;
200
Li Zefanb83cc962010-12-20 16:04:08 +0800201 if (btrfs_root_readonly(root))
202 return -EROFS;
203
David Sterba5c57b8b2018-04-23 15:45:18 +0200204 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200205 return -EFAULT;
206
David Sterba5c57b8b2018-04-23 15:45:18 +0200207 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000208 if (ret)
209 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200210
Jan Karae7848682012-06-12 16:20:32 +0200211 ret = mnt_want_write_file(file);
212 if (ret)
213 return ret;
214
Al Viro59551022016-01-22 15:40:57 -0500215 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200216
David Sterba5c57b8b2018-04-23 15:45:18 +0200217 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700218 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
219 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
220 if (ret)
221 goto out_unlock;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222
David Sterba5c57b8b2018-04-23 15:45:18 +0200223 if (fsflags & FS_SYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800224 binode_flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200225 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800226 binode_flags &= ~BTRFS_INODE_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200227 if (fsflags & FS_IMMUTABLE_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800228 binode_flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200229 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800230 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200231 if (fsflags & FS_APPEND_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800232 binode_flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200233 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800234 binode_flags &= ~BTRFS_INODE_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200235 if (fsflags & FS_NODUMP_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800236 binode_flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200237 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800238 binode_flags &= ~BTRFS_INODE_NODUMP;
David Sterba5c57b8b2018-04-23 15:45:18 +0200239 if (fsflags & FS_NOATIME_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800240 binode_flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200241 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800242 binode_flags &= ~BTRFS_INODE_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200243 if (fsflags & FS_DIRSYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800244 binode_flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200245 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800246 binode_flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200247 if (fsflags & FS_NOCOW_FL) {
Anand Jain44e51942019-04-20 19:48:57 +0800248 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600249 /*
250 * It's safe to turn csums off here, no extents exist.
251 * Otherwise we want the flag to reflect the real COW
252 * status of the file and will not set it.
253 */
254 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800255 binode_flags |= BTRFS_INODE_NODATACOW |
256 BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600257 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800258 binode_flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600259 }
260 } else {
261 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400262 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600263 */
Anand Jain44e51942019-04-20 19:48:57 +0800264 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600265 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800266 binode_flags &= ~(BTRFS_INODE_NODATACOW |
267 BTRFS_INODE_NODATASUM);
David Sterba7e97b8d2012-09-07 05:56:55 -0600268 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800269 binode_flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600270 }
271 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200272
Liu Bo75e7cb72011-03-22 10:12:20 +0000273 /*
274 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
275 * flag may be changed automatically if compression code won't make
276 * things smaller.
277 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200278 if (fsflags & FS_NOCOMP_FL) {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800279 binode_flags &= ~BTRFS_INODE_COMPRESS;
280 binode_flags |= BTRFS_INODE_NOCOMPRESS;
David Sterba5c57b8b2018-04-23 15:45:18 +0200281 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000282
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700283 if (IS_SWAPFILE(inode)) {
284 ret = -ETXTBSY;
285 goto out_unlock;
286 }
287
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800288 binode_flags |= BTRFS_INODE_COMPRESS;
289 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000290
David Sterba93370502017-10-31 17:32:41 +0100291 comp = btrfs_compress_type2str(fs_info->compress_type);
292 if (!comp || comp[0] == 0)
293 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
Li Zefanebcb9042011-04-15 03:03:17 +0000294 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800295 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000296 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200297
Anand Jainff9fef552019-04-20 19:48:53 +0800298 /*
299 * 1 for inode item
300 * 2 for properties
301 */
302 trans = btrfs_start_transaction(root, 3);
Li Zefanf062abf02011-12-29 13:36:45 +0800303 if (IS_ERR(trans)) {
304 ret = PTR_ERR(trans);
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800305 goto out_unlock;
Li Zefanf062abf02011-12-29 13:36:45 +0800306 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200307
Anand Jainff9fef552019-04-20 19:48:53 +0800308 if (comp) {
309 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
310 strlen(comp), 0);
311 if (ret) {
312 btrfs_abort_transaction(trans, ret);
313 goto out_end_trans;
314 }
Anand Jainff9fef552019-04-20 19:48:53 +0800315 } else {
316 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
317 0, 0);
318 if (ret && ret != -ENODATA) {
319 btrfs_abort_transaction(trans, ret);
320 goto out_end_trans;
321 }
322 }
323
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800324 binode->flags = binode_flags;
David Sterba7b6a2212018-03-26 18:40:21 +0200325 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400326 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700327 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200328 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200329
Anand Jainff9fef552019-04-20 19:48:53 +0800330 out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400331 btrfs_end_transaction(trans);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200332 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500333 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200334 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000335 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200336}
337
David Sterba19f93b32018-03-26 19:42:05 +0200338/*
339 * Translate btrfs internal inode flags to xflags as expected by the
340 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
341 * silently dropped.
342 */
343static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
344{
345 unsigned int xflags = 0;
346
347 if (flags & BTRFS_INODE_APPEND)
348 xflags |= FS_XFLAG_APPEND;
349 if (flags & BTRFS_INODE_IMMUTABLE)
350 xflags |= FS_XFLAG_IMMUTABLE;
351 if (flags & BTRFS_INODE_NOATIME)
352 xflags |= FS_XFLAG_NOATIME;
353 if (flags & BTRFS_INODE_NODUMP)
354 xflags |= FS_XFLAG_NODUMP;
355 if (flags & BTRFS_INODE_SYNC)
356 xflags |= FS_XFLAG_SYNC;
357
358 return xflags;
359}
360
361/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
362static int check_xflags(unsigned int flags)
363{
364 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
365 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
366 return -EOPNOTSUPP;
367 return 0;
368}
369
David Sterbae4202ac2018-03-26 19:51:16 +0200370/*
371 * Set the xflags from the internal inode flags. The remaining items of fsxattr
372 * are zeroed.
373 */
374static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
375{
376 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
377 struct fsxattr fa;
378
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700379 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
David Sterbae4202ac2018-03-26 19:51:16 +0200380 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;
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700392 struct fsxattr fa, old_fa;
David Sterba025f2122018-03-26 19:51:16 +0200393 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
David Sterba025f2122018-03-26 19:51:16 +0200403 if (copy_from_user(&fa, arg, sizeof(fa)))
404 return -EFAULT;
405
406 ret = check_xflags(fa.fsx_xflags);
407 if (ret)
408 return ret;
409
410 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
411 return -EOPNOTSUPP;
412
413 ret = mnt_want_write_file(file);
414 if (ret)
415 return ret;
416
417 inode_lock(inode);
418
419 old_flags = binode->flags;
420 old_i_flags = inode->i_flags;
421
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700422 simple_fill_fsxattr(&old_fa,
423 btrfs_inode_flags_to_xflags(binode->flags));
424 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
425 if (ret)
David Sterba025f2122018-03-26 19:51:16 +0200426 goto out_unlock;
David Sterba025f2122018-03-26 19:51:16 +0200427
428 if (fa.fsx_xflags & FS_XFLAG_SYNC)
429 binode->flags |= BTRFS_INODE_SYNC;
430 else
431 binode->flags &= ~BTRFS_INODE_SYNC;
432 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
433 binode->flags |= BTRFS_INODE_IMMUTABLE;
434 else
435 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
436 if (fa.fsx_xflags & FS_XFLAG_APPEND)
437 binode->flags |= BTRFS_INODE_APPEND;
438 else
439 binode->flags &= ~BTRFS_INODE_APPEND;
440 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
441 binode->flags |= BTRFS_INODE_NODUMP;
442 else
443 binode->flags &= ~BTRFS_INODE_NODUMP;
444 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
445 binode->flags |= BTRFS_INODE_NOATIME;
446 else
447 binode->flags &= ~BTRFS_INODE_NOATIME;
448
449 /* 1 item for the inode */
450 trans = btrfs_start_transaction(root, 1);
451 if (IS_ERR(trans)) {
452 ret = PTR_ERR(trans);
453 goto out_unlock;
454 }
455
456 btrfs_sync_inode_flags_to_i_flags(inode);
457 inode_inc_iversion(inode);
458 inode->i_ctime = current_time(inode);
459 ret = btrfs_update_inode(trans, root, inode);
460
461 btrfs_end_transaction(trans);
462
463out_unlock:
464 if (ret) {
465 binode->flags = old_flags;
466 inode->i_flags = old_i_flags;
467 }
468
469 inode_unlock(inode);
470 mnt_drop_write_file(file);
471
472 return ret;
473}
474
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200475static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
476{
Al Viro496ad9a2013-01-23 17:07:38 -0500477 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200478
479 return put_user(inode->i_generation, arg);
480}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400481
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -0300482static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
483 void __user *arg)
Li Dongyangf7039b12011-03-24 10:24:28 +0000484{
Li Dongyangf7039b12011-03-24 10:24:28 +0000485 struct btrfs_device *device;
486 struct request_queue *q;
487 struct fstrim_range range;
488 u64 minlen = ULLONG_MAX;
489 u64 num_devices = 0;
490 int ret;
491
492 if (!capable(CAP_SYS_ADMIN))
493 return -EPERM;
494
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000495 /*
496 * If the fs is mounted with nologreplay, which requires it to be
497 * mounted in RO mode as well, we can not allow discard on free space
498 * inside block groups, because log trees refer to extents that are not
499 * pinned in a block group's free space cache (pinning the extents is
500 * precisely the first phase of replaying a log tree).
501 */
502 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
503 return -EROFS;
504
Xiao Guangrong1f781602011-04-20 10:09:16 +0000505 rcu_read_lock();
506 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
507 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000508 if (!device->bdev)
509 continue;
510 q = bdev_get_queue(device->bdev);
511 if (blk_queue_discard(q)) {
512 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100513 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000514 minlen);
515 }
516 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000517 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200518
Li Dongyangf7039b12011-03-24 10:24:28 +0000519 if (!num_devices)
520 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000521 if (copy_from_user(&range, arg, sizeof(range)))
522 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800523
524 /*
525 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
526 * block group is in the logical address space, which can be any
527 * sectorsize aligned bytenr in the range [0, U64_MAX].
528 */
529 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200530 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000531
532 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400533 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000534 if (ret < 0)
535 return ret;
536
537 if (copy_to_user(arg, &range, sizeof(range)))
538 return -EFAULT;
539
540 return 0;
541}
542
David Sterbae1f60a62019-10-01 19:57:39 +0200543int __pure btrfs_is_empty_uuid(u8 *uuid)
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200544{
Chris Mason46e0f662013-11-15 12:14:55 +0100545 int i;
546
547 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
548 if (uuid[i])
549 return 0;
550 }
551 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200552}
553
Miao Xied5c12072013-02-28 10:04:33 +0000554static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400555 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100556 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200557 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000558 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400559{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400560 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400561 struct btrfs_trans_handle *trans;
562 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100563 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400564 struct btrfs_inode_item *inode_item;
565 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000566 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400567 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000568 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700569 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900570 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400571 int ret;
572 int err;
573 u64 objectid;
574 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500575 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200576 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400577
David Sterba49a3c4d2016-03-24 17:49:22 +0100578 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
579 if (!root_item)
580 return -ENOMEM;
581
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400582 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400583 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100584 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000585
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800586 /*
587 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400588 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800589 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100590 if (btrfs_qgroup_level(objectid)) {
591 ret = -ENOSPC;
592 goto fail_free;
593 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800594
Miao Xied5c12072013-02-28 10:04:33 +0000595 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400596 /*
Miao Xied5c12072013-02-28 10:04:33 +0000597 * The same as the snapshot creation, please see the comment
598 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400599 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800600 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000601 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100602 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400603
Miao Xied5c12072013-02-28 10:04:33 +0000604 trans = btrfs_start_transaction(root, 0);
605 if (IS_ERR(trans)) {
606 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100607 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100608 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000609 }
610 trans->block_rsv = &block_rsv;
611 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400612
Lu Fengqia93774222018-07-18 14:45:41 +0800613 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200614 if (ret)
615 goto fail;
616
David Sterba4d75f8a2014-06-15 01:54:12 +0200617 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400618 if (IS_ERR(leaf)) {
619 ret = PTR_ERR(leaf);
620 goto fail;
621 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400622
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623 btrfs_mark_buffer_dirty(leaf);
624
David Sterba49a3c4d2016-03-24 17:49:22 +0100625 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800626 btrfs_set_stack_inode_generation(inode_item, 1);
627 btrfs_set_stack_inode_size(inode_item, 3);
628 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400629 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400630 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800631 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400632
David Sterba49a3c4d2016-03-24 17:49:22 +0100633 btrfs_set_root_flags(root_item, 0);
634 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800635 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000636
David Sterba49a3c4d2016-03-24 17:49:22 +0100637 btrfs_set_root_bytenr(root_item, leaf->start);
638 btrfs_set_root_generation(root_item, trans->transid);
639 btrfs_set_root_level(root_item, 0);
640 btrfs_set_root_refs(root_item, 1);
641 btrfs_set_root_used(root_item, leaf->len);
642 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400643
David Sterba49a3c4d2016-03-24 17:49:22 +0100644 btrfs_set_root_generation_v2(root_item,
645 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200646 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100647 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
648 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
649 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
650 root_item->ctime = root_item->otime;
651 btrfs_set_root_ctransid(root_item, trans->transid);
652 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400653
Chris Mason925baed2008-06-25 16:01:30 -0400654 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400655 free_extent_buffer(leaf);
656 leaf = NULL;
657
David Sterba49a3c4d2016-03-24 17:49:22 +0100658 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400659
660 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400661 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200662 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400663 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100664 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400665 if (ret)
666 goto fail;
667
Yan, Zheng76dda932009-09-21 16:00:26 -0400668 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100670 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400672 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100673 goto fail;
674 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400675
676 btrfs_record_root_in_trans(trans, new_root);
677
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000678 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700679 if (ret) {
680 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400681 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700682 goto fail;
683 }
684
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530685 mutex_lock(&new_root->objectid_mutex);
686 new_root->highest_objectid = new_dirid;
687 mutex_unlock(&new_root->objectid_mutex);
688
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400689 /*
690 * insert the directory item
691 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200692 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100693 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400694 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100695 goto fail;
696 }
Chris Mason3de45862008-11-17 21:02:50 -0500697
Lu Fengqi684572d2018-08-04 21:10:57 +0800698 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500699 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100700 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400701 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400702 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100703 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500704
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200705 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500706 ret = btrfs_update_inode(trans, root, dir);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500707 if (ret) {
708 btrfs_abort_transaction(trans, ret);
709 goto fail;
710 }
Yan Zheng52c26172009-01-05 15:43:43 -0500711
Lu Fengqi6025c192018-08-01 11:32:29 +0800712 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200713 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500714 if (ret) {
715 btrfs_abort_transaction(trans, ret);
716 goto fail;
717 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500718
Lu Fengqicdb345a2018-05-29 15:01:53 +0800719 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400720 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200721 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400722 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200723
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400724fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100725 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000726 trans->block_rsv = NULL;
727 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100728 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800729
Sage Weil72fd0322010-10-29 15:41:32 -0400730 if (async_transid) {
731 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400732 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000733 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400734 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400735 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400736 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400737 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738 if (err && !ret)
739 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500740
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900741 if (!ret) {
742 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800743 if (IS_ERR(inode))
744 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900745 d_instantiate(dentry, inode);
746 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400747 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100748
749fail_free:
750 kfree(root_item);
751 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752}
753
Miao Xiee9662f72013-02-28 10:01:15 +0000754static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100755 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000756 u64 *async_transid, bool readonly,
757 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400759 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000760 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400761 struct btrfs_pending_snapshot *pending_snapshot;
762 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000763 int ret;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800764 bool snapshot_force_cow = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400765
Miao Xie27cdeb72014-04-02 19:51:05 +0800766 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400767 return -EINVAL;
768
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700769 if (atomic_read(&root->nr_swapfiles)) {
770 btrfs_warn(fs_info,
771 "cannot snapshot subvolume with active swapfile");
772 return -ETXTBSY;
773 }
774
David Sterba23269bf2017-02-13 11:03:44 +0100775 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100776 if (!pending_snapshot)
777 return -ENOMEM;
778
David Sterbab0c0ea62015-11-10 18:54:00 +0100779 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100780 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100781 pending_snapshot->path = btrfs_alloc_path();
782 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100783 ret = -ENOMEM;
784 goto free_pending;
785 }
786
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800787 /*
788 * Force new buffered writes to reserve space even when NOCOW is
789 * possible. This is to avoid later writeback (running dealloc) to
790 * fallback to COW mode and unexpectedly fail with ENOSPC.
791 */
David Sterbaea14b57f2017-06-22 02:19:11 +0200792 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100793 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600794 /* wait for no snapshot writes */
795 wait_event(root->subv_writers->wait,
796 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800797
Ethan Lien3cd24c62018-11-01 14:49:03 +0800798 ret = btrfs_start_delalloc_snapshot(root);
Miao Xie6a038432013-05-15 07:48:24 +0000799 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100800 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000801
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800802 /*
803 * All previous writes have started writeback in NOCOW mode, so now
804 * we force future writes to fallback to COW mode during snapshot
805 * creation.
806 */
807 atomic_inc(&root->snapshot_force_cow);
808 snapshot_force_cow = true;
809
Chris Mason6374e57a2017-06-23 09:48:21 -0700810 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000811
Miao Xie66d8f3d2012-09-06 04:02:28 -0600812 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
813 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000814 /*
815 * 1 - parent dir inode
816 * 2 - dir entries
817 * 1 - root item
818 * 2 - root ref/backref
819 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200820 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000821 */
822 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200823 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400824 false);
Miao Xied5c12072013-02-28 10:04:33 +0000825 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100826 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000827
Yan, Zhenga22285a2010-05-16 10:48:46 -0400828 pending_snapshot->dentry = dentry;
829 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800830 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000831 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000832 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400833
Miao Xied5c12072013-02-28 10:04:33 +0000834 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400835 if (IS_ERR(trans)) {
836 ret = PTR_ERR(trans);
837 goto fail;
838 }
839
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400840 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400841 list_add(&pending_snapshot->list,
842 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400843 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400844 if (async_transid) {
845 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400846 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000847 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400848 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400849 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400850 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400851 }
Miao Xieaec80302013-03-04 09:44:29 +0000852 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400853 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400854
855 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400856 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000857 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858
Chris Masond3797302014-10-15 13:50:56 -0700859 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
860 if (ret)
861 goto fail;
862
David Howells2b0143b2015-03-17 22:25:59 +0000863 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000864 if (IS_ERR(inode)) {
865 ret = PTR_ERR(inode);
866 goto fail;
867 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900868
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000869 d_instantiate(dentry, inode);
870 ret = 0;
871fail:
David Sterba7775c812017-02-10 19:18:18 +0100872 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100873dec_and_free:
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800874 if (snapshot_force_cow)
875 atomic_dec(&root->snapshot_force_cow);
David Sterbaea14b57f2017-06-22 02:19:11 +0200876 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100877 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100878free_pending:
879 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100880 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100881 kfree(pending_snapshot);
882
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400883 return ret;
884}
885
Sage Weil4260f7c2010-10-29 15:46:43 -0400886/* copy of may_delete in fs/namei.c()
887 * Check whether we can remove a link victim from directory dir, check
888 * whether the type of victim is right.
889 * 1. We can't do it if dir is read-only (done in permission())
890 * 2. We should have write and exec permissions on dir
891 * 3. We can't remove anything from append-only dir
892 * 4. We can't do anything with immutable dir (done in permission())
893 * 5. If the sticky bit on dir is set we should either
894 * a. be owner of dir, or
895 * b. be owner of victim, or
896 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400897 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400898 * links pointing to it.
899 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
900 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
901 * 9. We can't remove a root or mountpoint.
902 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
903 * nfs_async_unlink().
904 */
905
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530906static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400907{
908 int error;
909
David Howells2b0143b2015-03-17 22:25:59 +0000910 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400911 return -ENOENT;
912
David Howells2b0143b2015-03-17 22:25:59 +0000913 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400914 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400915
916 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
917 if (error)
918 return error;
919 if (IS_APPEND(dir))
920 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000921 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
922 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400923 return -EPERM;
924 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000925 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400926 return -ENOTDIR;
927 if (IS_ROOT(victim))
928 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000929 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400930 return -EISDIR;
931 if (IS_DEADDIR(dir))
932 return -ENOENT;
933 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
934 return -EBUSY;
935 return 0;
936}
937
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400938/* copy of may_create in fs/namei.c() */
939static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
940{
David Howells2b0143b2015-03-17 22:25:59 +0000941 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400942 return -EEXIST;
943 if (IS_DEADDIR(dir))
944 return -ENOENT;
945 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
946}
947
948/*
949 * Create a new subvolume below @parent. This is largely modeled after
950 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
951 * inside this filesystem so it's quite a bit simpler.
952 */
Al Viro92872092016-11-20 19:34:31 -0500953static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100954 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400955 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200956 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000957 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400958{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400959 struct inode *dir = d_inode(parent->dentry);
960 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400961 struct dentry *dentry;
962 int error;
963
Al Viro00235412016-05-26 00:05:12 -0400964 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
965 if (error == -EINTR)
966 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400967
968 dentry = lookup_one_len(name, parent->dentry, namelen);
969 error = PTR_ERR(dentry);
970 if (IS_ERR(dentry))
971 goto out_unlock;
972
Yan, Zheng76dda932009-09-21 16:00:26 -0400973 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400974 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600975 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400976
Chris Mason9c520572012-12-17 14:26:57 -0500977 /*
978 * even if this name doesn't exist, we may get hash collisions.
979 * check for them now when we can safely fail
980 */
981 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
982 dir->i_ino, name,
983 namelen);
984 if (error)
985 goto out_dput;
986
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400987 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400988
989 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
990 goto out_up_read;
991
Chris Mason3de45862008-11-17 21:02:50 -0500992 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100993 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200994 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500995 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000996 error = create_subvol(dir, dentry, name, namelen,
997 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500998 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400999 if (!error)
1000 fsnotify_mkdir(dir, dentry);
1001out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001002 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001003out_dput:
1004 dput(dentry);
1005out_unlock:
Al Viro59551022016-01-22 15:40:57 -05001006 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001007 return error;
1008}
1009
Chris Mason4cb53002011-05-24 15:35:30 -04001010/*
1011 * When we're defragging a range, we don't want to kick it off again
1012 * if it is really just waiting for delalloc to send it down.
1013 * If we find a nice big extent or delalloc range for the bytes in the
1014 * file you want to defrag, we return 0 to let you know to skip this
1015 * part of the file
1016 */
David Sterbaaab110a2014-07-29 17:32:10 +02001017static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001018{
1019 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1020 struct extent_map *em = NULL;
1021 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1022 u64 end;
1023
1024 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001025 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001026 read_unlock(&em_tree->lock);
1027
1028 if (em) {
1029 end = extent_map_end(em);
1030 free_extent_map(em);
1031 if (end - offset > thresh)
1032 return 0;
1033 }
1034 /* if we already have a nice delalloc here, just stop */
1035 thresh /= 2;
1036 end = count_range_bits(io_tree, &offset, offset + thresh,
1037 thresh, EXTENT_DELALLOC, 1);
1038 if (end >= thresh)
1039 return 0;
1040 return 1;
1041}
1042
1043/*
1044 * helper function to walk through a file and find extents
1045 * newer than a specific transid, and smaller than thresh.
1046 *
1047 * This is used by the defragging code to find new and small
1048 * extents
1049 */
1050static int find_new_extents(struct btrfs_root *root,
1051 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001052 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001053{
1054 struct btrfs_path *path;
1055 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001056 struct extent_buffer *leaf;
1057 struct btrfs_file_extent_item *extent;
1058 int type;
1059 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001060 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001061
1062 path = btrfs_alloc_path();
1063 if (!path)
1064 return -ENOMEM;
1065
David Sterbaa4689d22011-05-31 17:08:14 +00001066 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001067 min_key.type = BTRFS_EXTENT_DATA_KEY;
1068 min_key.offset = *off;
1069
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301070 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001071 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001072 if (ret != 0)
1073 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001074process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001075 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001076 goto none;
1077 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1078 goto none;
1079
1080 leaf = path->nodes[0];
1081 extent = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_file_extent_item);
1083
1084 type = btrfs_file_extent_type(leaf, extent);
1085 if (type == BTRFS_FILE_EXTENT_REG &&
1086 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1087 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1088 *off = min_key.offset;
1089 btrfs_free_path(path);
1090 return 0;
1091 }
1092
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001093 path->slots[0]++;
1094 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1095 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1096 goto process_slot;
1097 }
1098
Chris Mason4cb53002011-05-24 15:35:30 -04001099 if (min_key.offset == (u64)-1)
1100 goto none;
1101
1102 min_key.offset++;
1103 btrfs_release_path(path);
1104 }
1105none:
1106 btrfs_free_path(path);
1107 return -ENOENT;
1108}
1109
Li Zefan6c282eb2012-06-11 16:03:35 +08001110static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001111{
1112 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001113 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001114 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001115 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001116
1117 /*
1118 * hopefully we have this extent in the tree already, try without
1119 * the full extent lock
1120 */
1121 read_lock(&em_tree->lock);
1122 em = lookup_extent_mapping(em_tree, start, len);
1123 read_unlock(&em_tree->lock);
1124
1125 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001126 struct extent_state *cached = NULL;
1127 u64 end = start + len - 1;
1128
Chris Mason940100a2010-03-10 10:52:59 -05001129 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001130 lock_extent_bits(io_tree, start, end, &cached);
Omar Sandoval39b07b52019-12-02 17:34:23 -08001131 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
David Sterbae43bbe52017-12-12 21:43:52 +01001132 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001133
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001134 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001135 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001136 }
1137
Li Zefan6c282eb2012-06-11 16:03:35 +08001138 return em;
1139}
1140
1141static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1142{
1143 struct extent_map *next;
1144 bool ret = true;
1145
1146 /* this is the last extent */
1147 if (em->start + em->len >= i_size_read(inode))
1148 return false;
1149
1150 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001151 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1152 ret = false;
1153 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001154 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001155 ret = false;
1156
1157 free_extent_map(next);
1158 return ret;
1159}
1160
David Sterbaaab110a2014-07-29 17:32:10 +02001161static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001162 u64 *last_len, u64 *skip, u64 *defrag_end,
1163 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001164{
1165 struct extent_map *em;
1166 int ret = 1;
1167 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001168 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001169
1170 /*
1171 * make sure that once we start defragging an extent, we keep on
1172 * defragging it
1173 */
1174 if (start < *defrag_end)
1175 return 1;
1176
1177 *skip = 0;
1178
1179 em = defrag_lookup_extent(inode, start);
1180 if (!em)
1181 return 0;
1182
Chris Mason940100a2010-03-10 10:52:59 -05001183 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001184 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001185 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001186 goto out;
1187 }
1188
Liu Bo4a3560c2015-08-07 16:48:41 +08001189 if (!*defrag_end)
1190 prev_mergeable = false;
1191
Li Zefan6c282eb2012-06-11 16:03:35 +08001192 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001193 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001194 * we hit a real extent, if it is big or the next extent is not a
1195 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001196 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001197 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001198 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001199 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001200out:
Chris Mason940100a2010-03-10 10:52:59 -05001201 /*
1202 * last_len ends up being a counter of how many bytes we've defragged.
1203 * every time we choose not to defrag an extent, we reset *last_len
1204 * so that the next tiny extent will force a defrag.
1205 *
1206 * The end result of this is that tiny extents before a single big
1207 * extent will force at least part of that big extent to be defragged.
1208 */
1209 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001210 *defrag_end = extent_map_end(em);
1211 } else {
1212 *last_len = 0;
1213 *skip = extent_map_end(em);
1214 *defrag_end = 0;
1215 }
1216
1217 free_extent_map(em);
1218 return ret;
1219}
1220
Chris Mason4cb53002011-05-24 15:35:30 -04001221/*
1222 * it doesn't do much good to defrag one or two pages
1223 * at a time. This pulls in a nice chunk of pages
1224 * to COW and defrag.
1225 *
1226 * It also makes sure the delalloc code has enough
1227 * dirty data to avoid making new small extents as part
1228 * of the defrag
1229 *
1230 * It's a good idea to start RA on this range
1231 * before calling this.
1232 */
1233static int cluster_pages_for_defrag(struct inode *inode,
1234 struct page **pages,
1235 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001236 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001237{
Chris Mason4cb53002011-05-24 15:35:30 -04001238 unsigned long file_end;
1239 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001240 u64 page_start;
1241 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001242 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001243 int ret;
1244 int i;
1245 int i_done;
1246 struct btrfs_ordered_extent *ordered;
1247 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001248 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001249 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001250 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001251
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001252 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001253 if (!isize || start_index > file_end)
1254 return 0;
1255
1256 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001257
Qu Wenruo364ecf32017-02-27 15:10:38 +08001258 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001259 start_index << PAGE_SHIFT,
1260 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001261 if (ret)
1262 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001263 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001264 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001265
1266 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001267 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001268 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001269again:
Josef Bacika94733d2011-07-11 10:47:06 -04001270 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001271 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001272 if (!page)
1273 break;
1274
Miao Xie600a45e2012-02-16 15:01:24 +08001275 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001276 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001277 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001278 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001279 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001280 ordered = btrfs_lookup_ordered_extent(inode,
1281 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001282 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001283 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001284 if (!ordered)
1285 break;
1286
1287 unlock_page(page);
1288 btrfs_start_ordered_extent(inode, ordered, 1);
1289 btrfs_put_ordered_extent(ordered);
1290 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001291 /*
1292 * we unlocked the page above, so we need check if
1293 * it was released or not.
1294 */
1295 if (page->mapping != inode->i_mapping) {
1296 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001297 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001298 goto again;
1299 }
Miao Xie600a45e2012-02-16 15:01:24 +08001300 }
1301
Chris Mason4cb53002011-05-24 15:35:30 -04001302 if (!PageUptodate(page)) {
1303 btrfs_readpage(NULL, page);
1304 lock_page(page);
1305 if (!PageUptodate(page)) {
1306 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001307 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001308 ret = -EIO;
1309 break;
1310 }
1311 }
Miao Xie600a45e2012-02-16 15:01:24 +08001312
Miao Xie600a45e2012-02-16 15:01:24 +08001313 if (page->mapping != inode->i_mapping) {
1314 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001315 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001316 goto again;
1317 }
1318
Chris Mason4cb53002011-05-24 15:35:30 -04001319 pages[i] = page;
1320 i_done++;
1321 }
1322 if (!i_done || ret)
1323 goto out;
1324
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001325 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001326 goto out;
1327
1328 /*
1329 * so now we have a nice long stream of locked
1330 * and up to date pages, lets wait on them
1331 */
1332 for (i = 0; i < i_done; i++)
1333 wait_on_page_writeback(pages[i]);
1334
1335 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001336 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001337
1338 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001339 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001340 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
Omar Sandovale1821632019-08-15 14:04:04 -07001341 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1342 EXTENT_DEFRAG, 0, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001343
Liu Bo1f12bd02012-03-29 09:57:44 -04001344 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001345 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001346 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001347 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001348 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001349 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001350 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001351 }
1352
1353
Liu Bo9e8a4a82012-09-05 19:10:51 -06001354 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001355 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001356
1357 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001358 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001359
1360 for (i = 0; i < i_done; i++) {
1361 clear_page_dirty_for_io(pages[i]);
1362 ClearPageChecked(pages[i]);
1363 set_page_extent_mapped(pages[i]);
1364 set_page_dirty(pages[i]);
1365 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001366 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001367 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08001368 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001369 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001370 return i_done;
1371out:
1372 for (i = 0; i < i_done; i++) {
1373 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001374 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001375 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001376 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001377 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001378 page_cnt << PAGE_SHIFT, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08001379 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001380 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001381 return ret;
1382
1383}
1384
1385int btrfs_defrag_file(struct inode *inode, struct file *file,
1386 struct btrfs_ioctl_defrag_range_args *range,
1387 u64 newer_than, unsigned long max_to_defrag)
1388{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001389 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001390 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001391 struct file_ra_state *ra = NULL;
1392 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001393 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001394 u64 last_len = 0;
1395 u64 skip = 0;
1396 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001397 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001398 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001399 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001400 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001401 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001402 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001403 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001404 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001405 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001406 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001407 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001408 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001409
Liu Bo0abd5b12013-04-16 09:20:28 +00001410 if (isize == 0)
1411 return 0;
1412
1413 if (range->start >= isize)
1414 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001415
David Sterba1e2ef462017-07-17 20:01:59 +02001416 if (do_compress) {
Chengguang Xuce96b7f2019-10-10 15:59:57 +08001417 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Li Zefan1a419d82010-10-25 15:12:50 +08001418 return -EINVAL;
1419 if (range->compress_type)
1420 compress_type = range->compress_type;
1421 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001422
Liu Bo0abd5b12013-04-16 09:20:28 +00001423 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001424 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001425
Chris Mason4cb53002011-05-24 15:35:30 -04001426 /*
David Sterba0a52d102017-06-22 03:22:58 +02001427 * If we were not given a file, allocate a readahead context. As
1428 * readahead is just an optimization, defrag will work without it so
1429 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001430 */
1431 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001432 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001433 if (ra)
1434 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001435 } else {
1436 ra = &file->f_ra;
1437 }
1438
David Sterba63e727e2017-06-22 03:13:02 +02001439 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001440 if (!pages) {
1441 ret = -ENOMEM;
1442 goto out_ra;
1443 }
1444
1445 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001446 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001447 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001448 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001449 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001450 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001451 }
1452
Chris Mason4cb53002011-05-24 15:35:30 -04001453 if (newer_than) {
1454 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001455 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001456 if (!ret) {
1457 range->start = newer_off;
1458 /*
1459 * we always align our defrag to help keep
1460 * the extents in the file evenly spaced
1461 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001462 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001463 } else
1464 goto out_ra;
1465 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001466 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001467 }
1468 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301469 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001470
Li Zefan2a0f7f52011-10-10 15:43:34 -04001471 /*
1472 * make writeback starts from i, so the defrag range can be
1473 * written sequentially.
1474 */
1475 if (i < inode->i_mapping->writeback_index)
1476 inode->i_mapping->writeback_index = i;
1477
Chris Masonf7f43cc2011-10-11 11:41:40 -04001478 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001479 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001480 /*
1481 * make sure we stop running if someone unmounts
1482 * the FS
1483 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001484 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001485 break;
1486
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001487 if (btrfs_defrag_cancelled(fs_info)) {
1488 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001489 ret = -EAGAIN;
1490 break;
1491 }
1492
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001493 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001494 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001495 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001496 unsigned long next;
1497 /*
1498 * the should_defrag function tells us how much to skip
1499 * bump our counter by the suggested amount
1500 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001501 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001502 i = max(i + 1, next);
1503 continue;
1504 }
Li Zefan008873e2011-09-02 15:57:07 +08001505
1506 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001507 cluster = (PAGE_ALIGN(defrag_end) >>
1508 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001509 cluster = min(cluster, max_cluster);
1510 } else {
1511 cluster = max_cluster;
1512 }
1513
Li Zefan008873e2011-09-02 15:57:07 +08001514 if (i + cluster > ra_index) {
1515 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001516 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001517 page_cache_sync_readahead(inode->i_mapping, ra,
1518 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301519 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001520 }
Chris Mason940100a2010-03-10 10:52:59 -05001521
Al Viro59551022016-01-22 15:40:57 -05001522 inode_lock(inode);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001523 if (IS_SWAPFILE(inode)) {
1524 ret = -ETXTBSY;
1525 } else {
1526 if (do_compress)
1527 BTRFS_I(inode)->defrag_compress = compress_type;
1528 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1529 }
Liu Boecb8bea2012-03-29 09:57:44 -04001530 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001531 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001532 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001533 }
Chris Mason940100a2010-03-10 10:52:59 -05001534
Chris Mason4cb53002011-05-24 15:35:30 -04001535 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001536 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001537 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001538
1539 if (newer_than) {
1540 if (newer_off == (u64)-1)
1541 break;
1542
Liu Boe1f041e2012-03-29 09:57:45 -04001543 if (ret > 0)
1544 i += ret;
1545
Chris Mason4cb53002011-05-24 15:35:30 -04001546 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001547 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001548
Byongho Leeee221842015-12-15 01:42:10 +09001549 ret = find_new_extents(root, inode, newer_than,
1550 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001551 if (!ret) {
1552 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001553 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001554 } else {
1555 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001556 }
Chris Mason4cb53002011-05-24 15:35:30 -04001557 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001558 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001559 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001560 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001561 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001562 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001563 last_len = 0;
1564 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001565 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001566 }
1567
Filipe Mananadec8ef92014-03-01 10:55:54 +00001568 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001569 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001570 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1571 &BTRFS_I(inode)->runtime_flags))
1572 filemap_flush(inode->i_mapping);
1573 }
Chris Mason1e701a32010-03-11 09:42:04 -05001574
Li Zefan1a419d82010-10-25 15:12:50 +08001575 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001576 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001577 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1578 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001579 }
1580
Diego Calleja60ccf822011-09-01 16:33:57 +02001581 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001582
Chris Mason4cb53002011-05-24 15:35:30 -04001583out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001584 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001585 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001586 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001587 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001588 }
Chris Mason4cb53002011-05-24 15:35:30 -04001589 if (!file)
1590 kfree(ra);
1591 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001592 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001593}
1594
Miao Xie198605a2012-11-26 08:43:45 +00001595static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001596 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001597{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001598 struct inode *inode = file_inode(file);
1599 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001600 u64 new_size;
1601 u64 old_size;
1602 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001603 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001604 struct btrfs_ioctl_vol_args *vol_args;
1605 struct btrfs_trans_handle *trans;
1606 struct btrfs_device *device = NULL;
1607 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001608 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001609 char *devstr = NULL;
1610 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001611 int mod = 0;
1612
Chris Masone441d542009-01-05 16:57:23 -05001613 if (!capable(CAP_SYS_ADMIN))
1614 return -EPERM;
1615
Miao Xie198605a2012-11-26 08:43:45 +00001616 ret = mnt_want_write_file(file);
1617 if (ret)
1618 return ret;
1619
David Sterba171938e2017-03-28 14:44:21 +02001620 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001621 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001622 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001623 }
1624
Li Zefandae7b662009-04-08 15:06:54 +08001625 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001626 if (IS_ERR(vol_args)) {
1627 ret = PTR_ERR(vol_args);
1628 goto out;
1629 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001630
1631 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001632
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 sizestr = vol_args->name;
1634 devstr = strchr(sizestr, ':');
1635 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001636 sizestr = devstr + 1;
1637 *devstr = '\0';
1638 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001639 ret = kstrtoull(devstr, 10, &devid);
1640 if (ret)
1641 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001642 if (!devid) {
1643 ret = -EINVAL;
1644 goto out_free;
1645 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001646 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001647 }
Miao Xiedba60f32012-12-21 09:19:51 +00001648
Anand Jain09ba3bc2019-01-19 14:48:55 +08001649 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001650 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001651 btrfs_info(fs_info, "resizer unable to find device %llu",
1652 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001653 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001654 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001655 }
Miao Xiedba60f32012-12-21 09:19:51 +00001656
Anand Jainebbede42017-12-04 12:54:52 +08001657 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001658 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001659 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001660 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001661 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001662 goto out_free;
1663 }
1664
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001665 if (!strcmp(sizestr, "max"))
1666 new_size = device->bdev->bd_inode->i_size;
1667 else {
1668 if (sizestr[0] == '-') {
1669 mod = -1;
1670 sizestr++;
1671 } else if (sizestr[0] == '+') {
1672 mod = 1;
1673 sizestr++;
1674 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001675 new_size = memparse(sizestr, &retptr);
1676 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001677 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001678 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001679 }
1680 }
1681
Anand Jain401e29c2017-12-04 12:54:55 +08001682 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001683 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001684 goto out_free;
1685 }
1686
Miao Xie7cc8e582014-09-03 21:35:38 +08001687 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001688
1689 if (mod < 0) {
1690 if (new_size > old_size) {
1691 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001692 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001693 }
1694 new_size = old_size - new_size;
1695 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001696 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001697 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001698 goto out_free;
1699 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001700 new_size = old_size + new_size;
1701 }
1702
Byongho Leeee221842015-12-15 01:42:10 +09001703 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001704 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001705 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 }
1707 if (new_size > device->bdev->bd_inode->i_size) {
1708 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001709 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001710 }
1711
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001712 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001713
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001714 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1715 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001716
1717 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001718 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001719 if (IS_ERR(trans)) {
1720 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001721 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001722 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001723 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001724 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001725 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001726 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001727 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001728
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001729out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001730 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001731out:
David Sterba171938e2017-03-28 14:44:21 +02001732 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001733 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001734 return ret;
1735}
1736
Sage Weil72fd0322010-10-29 15:41:32 -04001737static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001738 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001739 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001740 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001741{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001742 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001743 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001744
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001745 if (!S_ISDIR(file_inode(file)->i_mode))
1746 return -ENOTDIR;
1747
Liu Boa874a632012-06-29 03:58:46 -06001748 ret = mnt_want_write_file(file);
1749 if (ret)
1750 goto out;
1751
Sage Weil72fd0322010-10-29 15:41:32 -04001752 namelen = strlen(name);
1753 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001754 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001755 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001756 }
1757
Chris Mason16780ca2012-02-20 22:14:55 -05001758 if (name[0] == '.' &&
1759 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1760 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001761 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001762 }
1763
Chris Mason3de45862008-11-17 21:02:50 -05001764 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001765 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001766 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001767 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001768 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001769 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001770 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001771 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001772 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001773 }
1774
Al Viro496ad9a2013-01-23 17:07:38 -05001775 src_inode = file_inode(src.file);
1776 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001777 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001778 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001779 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001780 } else if (!inode_owner_or_capable(src_inode)) {
1781 /*
1782 * Subvolume creation is not restricted, but snapshots
1783 * are limited to own subvolumes only
1784 */
1785 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001786 } else {
1787 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1788 BTRFS_I(src_inode)->root,
1789 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001790 }
Al Viro2903ff02012-08-28 12:52:22 -04001791 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001792 }
Liu Boa874a632012-06-29 03:58:46 -06001793out_drop_write:
1794 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001795out:
Sage Weil72fd0322010-10-29 15:41:32 -04001796 return ret;
1797}
1798
1799static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001800 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001801{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001802 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001803 int ret;
1804
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001805 if (!S_ISDIR(file_inode(file)->i_mode))
1806 return -ENOTDIR;
1807
Li Zefanfa0d2b92010-12-20 15:53:28 +08001808 vol_args = memdup_user(arg, sizeof(*vol_args));
1809 if (IS_ERR(vol_args))
1810 return PTR_ERR(vol_args);
1811 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001812
Li Zefanfa0d2b92010-12-20 15:53:28 +08001813 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001814 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001815 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001816
Li Zefanfa0d2b92010-12-20 15:53:28 +08001817 kfree(vol_args);
1818 return ret;
1819}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001820
Li Zefanfa0d2b92010-12-20 15:53:28 +08001821static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1822 void __user *arg, int subvol)
1823{
1824 struct btrfs_ioctl_vol_args_v2 *vol_args;
1825 int ret;
1826 u64 transid = 0;
1827 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001828 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001829 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001830
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001831 if (!S_ISDIR(file_inode(file)->i_mode))
1832 return -ENOTDIR;
1833
Li Zefanfa0d2b92010-12-20 15:53:28 +08001834 vol_args = memdup_user(arg, sizeof(*vol_args));
1835 if (IS_ERR(vol_args))
1836 return PTR_ERR(vol_args);
1837 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001838
Li Zefanb83cc962010-12-20 16:04:08 +08001839 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001840 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1841 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001842 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001843 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001844 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001845
Nikolay Borisovebc87352019-08-26 17:34:24 +03001846 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1847 struct inode *inode = file_inode(file);
1848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1849
1850 btrfs_warn(fs_info,
1851"SNAP_CREATE_V2 ioctl with CREATE_ASYNC is deprecated and will be removed in kernel 5.7");
1852
Li Zefanfa0d2b92010-12-20 15:53:28 +08001853 ptr = &transid;
Nikolay Borisovebc87352019-08-26 17:34:24 +03001854 }
Li Zefanb83cc962010-12-20 16:04:08 +08001855 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1856 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001857 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001858 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001859 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001860 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001861 }
1862 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1863 if (IS_ERR(inherit)) {
1864 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001865 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001866 }
1867 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001868
1869 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001870 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001871 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001872 if (ret)
1873 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001874
Dan Carpenterc47ca322014-09-04 14:09:15 +03001875 if (ptr && copy_to_user(arg +
1876 offsetof(struct btrfs_ioctl_vol_args_v2,
1877 transid),
1878 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001879 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001880
1881free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001882 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001883free_args:
1884 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001885 return ret;
1886}
1887
Li Zefan0caa1022010-12-20 16:30:25 +08001888static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1889 void __user *arg)
1890{
Al Viro496ad9a2013-01-23 17:07:38 -05001891 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001893 struct btrfs_root *root = BTRFS_I(inode)->root;
1894 int ret = 0;
1895 u64 flags = 0;
1896
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001897 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001898 return -EINVAL;
1899
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001900 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001901 if (btrfs_root_readonly(root))
1902 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001903 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001904
1905 if (copy_to_user(arg, &flags, sizeof(flags)))
1906 ret = -EFAULT;
1907
1908 return ret;
1909}
1910
1911static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1912 void __user *arg)
1913{
Al Viro496ad9a2013-01-23 17:07:38 -05001914 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001915 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001916 struct btrfs_root *root = BTRFS_I(inode)->root;
1917 struct btrfs_trans_handle *trans;
1918 u64 root_flags;
1919 u64 flags;
1920 int ret = 0;
1921
David Sterbabd60ea02014-01-16 15:50:22 +01001922 if (!inode_owner_or_capable(inode))
1923 return -EPERM;
1924
Liu Bob9ca0662012-06-29 03:58:49 -06001925 ret = mnt_want_write_file(file);
1926 if (ret)
1927 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001928
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001929 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001930 ret = -EINVAL;
1931 goto out_drop_write;
1932 }
Li Zefan0caa1022010-12-20 16:30:25 +08001933
Liu Bob9ca0662012-06-29 03:58:49 -06001934 if (copy_from_user(&flags, arg, sizeof(flags))) {
1935 ret = -EFAULT;
1936 goto out_drop_write;
1937 }
Li Zefan0caa1022010-12-20 16:30:25 +08001938
Liu Bob9ca0662012-06-29 03:58:49 -06001939 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1940 ret = -EINVAL;
1941 goto out_drop_write;
1942 }
Li Zefan0caa1022010-12-20 16:30:25 +08001943
Liu Bob9ca0662012-06-29 03:58:49 -06001944 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1945 ret = -EOPNOTSUPP;
1946 goto out_drop_write;
1947 }
Li Zefan0caa1022010-12-20 16:30:25 +08001948
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001949 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001950
1951 /* nothing to do */
1952 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001953 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001954
1955 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001956 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001957 btrfs_set_root_flags(&root->root_item,
1958 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001959 } else {
1960 /*
1961 * Block RO -> RW transition if this subvolume is involved in
1962 * send
1963 */
1964 spin_lock(&root->root_item_lock);
1965 if (root->send_in_progress == 0) {
1966 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001967 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001968 spin_unlock(&root->root_item_lock);
1969 } else {
1970 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001971 btrfs_warn(fs_info,
1972 "Attempt to set subvolume %llu read-write during send",
1973 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001974 ret = -EPERM;
1975 goto out_drop_sem;
1976 }
1977 }
Li Zefan0caa1022010-12-20 16:30:25 +08001978
1979 trans = btrfs_start_transaction(root, 1);
1980 if (IS_ERR(trans)) {
1981 ret = PTR_ERR(trans);
1982 goto out_reset;
1983 }
1984
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001985 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001986 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001987 if (ret < 0) {
1988 btrfs_end_transaction(trans);
1989 goto out_reset;
1990 }
Li Zefan0caa1022010-12-20 16:30:25 +08001991
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001992 ret = btrfs_commit_transaction(trans);
1993
Li Zefan0caa1022010-12-20 16:30:25 +08001994out_reset:
1995 if (ret)
1996 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001997out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001998 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001999out_drop_write:
2000 mnt_drop_write_file(file);
2001out:
Li Zefan0caa1022010-12-20 16:30:25 +08002002 return ret;
2003}
2004
Chris Masonac8e9812010-02-28 15:39:26 -05002005static noinline int key_in_sk(struct btrfs_key *key,
2006 struct btrfs_ioctl_search_key *sk)
2007{
Chris Masonabc6e132010-03-18 12:10:08 -04002008 struct btrfs_key test;
2009 int ret;
2010
2011 test.objectid = sk->min_objectid;
2012 test.type = sk->min_type;
2013 test.offset = sk->min_offset;
2014
2015 ret = btrfs_comp_cpu_keys(key, &test);
2016 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002017 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002018
2019 test.objectid = sk->max_objectid;
2020 test.type = sk->max_type;
2021 test.offset = sk->max_offset;
2022
2023 ret = btrfs_comp_cpu_keys(key, &test);
2024 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002025 return 0;
2026 return 1;
2027}
2028
Jeff Mahoneydf397562016-06-21 20:18:21 -04002029static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002030 struct btrfs_key *key,
2031 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002032 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002033 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002034 unsigned long *sk_offset,
2035 int *num_found)
2036{
2037 u64 found_transid;
2038 struct extent_buffer *leaf;
2039 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002040 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002041 unsigned long item_off;
2042 unsigned long item_len;
2043 int nritems;
2044 int i;
2045 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002046 int ret = 0;
2047
2048 leaf = path->nodes[0];
2049 slot = path->slots[0];
2050 nritems = btrfs_header_nritems(leaf);
2051
2052 if (btrfs_header_generation(leaf) > sk->max_transid) {
2053 i = nritems;
2054 goto advance_key;
2055 }
2056 found_transid = btrfs_header_generation(leaf);
2057
2058 for (i = slot; i < nritems; i++) {
2059 item_off = btrfs_item_ptr_offset(leaf, i);
2060 item_len = btrfs_item_size_nr(leaf, i);
2061
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002062 btrfs_item_key_to_cpu(leaf, key, i);
2063 if (!key_in_sk(key, sk))
2064 continue;
2065
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002066 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002067 if (*num_found) {
2068 ret = 1;
2069 goto out;
2070 }
Chris Masonac8e9812010-02-28 15:39:26 -05002071
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002072 /*
2073 * return one empty item back for v1, which does not
2074 * handle -EOVERFLOW
2075 */
2076
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002077 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002078 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002079 ret = -EOVERFLOW;
2080 }
Chris Masonac8e9812010-02-28 15:39:26 -05002081
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002082 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002083 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002084 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002085 }
2086
Chris Masonac8e9812010-02-28 15:39:26 -05002087 sh.objectid = key->objectid;
2088 sh.offset = key->offset;
2089 sh.type = key->type;
2090 sh.len = item_len;
2091 sh.transid = found_transid;
2092
2093 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002094 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2095 ret = -EFAULT;
2096 goto out;
2097 }
2098
Chris Masonac8e9812010-02-28 15:39:26 -05002099 *sk_offset += sizeof(sh);
2100
2101 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002102 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002103 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002104 if (read_extent_buffer_to_user(leaf, up,
2105 item_off, item_len)) {
2106 ret = -EFAULT;
2107 goto out;
2108 }
2109
Chris Masonac8e9812010-02-28 15:39:26 -05002110 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002111 }
Hugo Millse2156862011-05-14 17:43:41 +00002112 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002113
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002114 if (ret) /* -EOVERFLOW from above */
2115 goto out;
2116
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002117 if (*num_found >= sk->nr_items) {
2118 ret = 1;
2119 goto out;
2120 }
Chris Masonac8e9812010-02-28 15:39:26 -05002121 }
2122advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002123 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002124 test.objectid = sk->max_objectid;
2125 test.type = sk->max_type;
2126 test.offset = sk->max_offset;
2127 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2128 ret = 1;
2129 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002130 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002131 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002132 key->offset = 0;
2133 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002134 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002135 key->offset = 0;
2136 key->type = 0;
2137 key->objectid++;
2138 } else
2139 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002140out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002141 /*
2142 * 0: all items from this leaf copied, continue with next
2143 * 1: * more items can be copied, but unused buffer is too small
2144 * * all items were found
2145 * Either way, it will stops the loop which iterates to the next
2146 * leaf
2147 * -EOVERFLOW: item was to large for buffer
2148 * -EFAULT: could not copy extent buffer back to userspace
2149 */
Chris Masonac8e9812010-02-28 15:39:26 -05002150 return ret;
2151}
2152
2153static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002154 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002155 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002156 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002158 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002159 struct btrfs_root *root;
2160 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002161 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002162 int ret;
2163 int num_found = 0;
2164 unsigned long sk_offset = 0;
2165
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002166 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2167 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002168 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002169 }
Gerhard Heift12544442014-01-30 16:23:58 +01002170
Chris Masonac8e9812010-02-28 15:39:26 -05002171 path = btrfs_alloc_path();
2172 if (!path)
2173 return -ENOMEM;
2174
2175 if (sk->tree_id == 0) {
2176 /* search the root of the inode that was passed */
2177 root = BTRFS_I(inode)->root;
2178 } else {
2179 key.objectid = sk->tree_id;
2180 key.type = BTRFS_ROOT_ITEM_KEY;
2181 key.offset = (u64)-1;
2182 root = btrfs_read_fs_root_no_name(info, &key);
2183 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002184 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002185 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002186 }
2187 }
2188
2189 key.objectid = sk->min_objectid;
2190 key.type = sk->min_type;
2191 key.offset = sk->min_offset;
2192
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302193 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002194 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002195 if (ret != 0) {
2196 if (ret > 0)
2197 ret = 0;
2198 goto err;
2199 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002200 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002201 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002202 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002203 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002204 break;
2205
2206 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002207 if (ret > 0)
2208 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002209err:
2210 sk->nr_items = num_found;
2211 btrfs_free_path(path);
2212 return ret;
2213}
2214
2215static noinline int btrfs_ioctl_tree_search(struct file *file,
2216 void __user *argp)
2217{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002218 struct btrfs_ioctl_search_args __user *uargs;
2219 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002220 struct inode *inode;
2221 int ret;
2222 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002223
2224 if (!capable(CAP_SYS_ADMIN))
2225 return -EPERM;
2226
Gerhard Heiftba346b32014-01-30 16:24:02 +01002227 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002228
Gerhard Heiftba346b32014-01-30 16:24:02 +01002229 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2230 return -EFAULT;
2231
2232 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002233
Al Viro496ad9a2013-01-23 17:07:38 -05002234 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002235 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002236
2237 /*
2238 * In the origin implementation an overflow is handled by returning a
2239 * search header with a len of zero, so reset ret.
2240 */
2241 if (ret == -EOVERFLOW)
2242 ret = 0;
2243
Gerhard Heiftba346b32014-01-30 16:24:02 +01002244 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002245 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002246 return ret;
2247}
2248
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002249static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2250 void __user *argp)
2251{
2252 struct btrfs_ioctl_search_args_v2 __user *uarg;
2253 struct btrfs_ioctl_search_args_v2 args;
2254 struct inode *inode;
2255 int ret;
2256 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002257 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002258
2259 if (!capable(CAP_SYS_ADMIN))
2260 return -EPERM;
2261
2262 /* copy search header and buffer size */
2263 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2264 if (copy_from_user(&args, uarg, sizeof(args)))
2265 return -EFAULT;
2266
2267 buf_size = args.buf_size;
2268
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002269 /* limit result size to 16MB */
2270 if (buf_size > buf_limit)
2271 buf_size = buf_limit;
2272
2273 inode = file_inode(file);
2274 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002275 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002276 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2277 ret = -EFAULT;
2278 else if (ret == -EOVERFLOW &&
2279 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2280 ret = -EFAULT;
2281
Yan, Zheng76dda932009-09-21 16:00:26 -04002282 return ret;
2283}
2284
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002285/*
Chris Masonac8e9812010-02-28 15:39:26 -05002286 * Search INODE_REFs to identify path name of 'dirid' directory
2287 * in a 'tree_id' tree. and sets path name to 'name'.
2288 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002289static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2290 u64 tree_id, u64 dirid, char *name)
2291{
2292 struct btrfs_root *root;
2293 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002294 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002295 int ret = -1;
2296 int slot;
2297 int len;
2298 int total_len = 0;
2299 struct btrfs_inode_ref *iref;
2300 struct extent_buffer *l;
2301 struct btrfs_path *path;
2302
2303 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2304 name[0]='\0';
2305 return 0;
2306 }
2307
2308 path = btrfs_alloc_path();
2309 if (!path)
2310 return -ENOMEM;
2311
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002312 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002313
2314 key.objectid = tree_id;
2315 key.type = BTRFS_ROOT_ITEM_KEY;
2316 key.offset = (u64)-1;
2317 root = btrfs_read_fs_root_no_name(info, &key);
2318 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002319 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002320 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002321 }
2322
2323 key.objectid = dirid;
2324 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002325 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002326
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302327 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002328 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2329 if (ret < 0)
2330 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002331 else if (ret > 0) {
2332 ret = btrfs_previous_item(root, path, dirid,
2333 BTRFS_INODE_REF_KEY);
2334 if (ret < 0)
2335 goto out;
2336 else if (ret > 0) {
2337 ret = -ENOENT;
2338 goto out;
2339 }
2340 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002341
2342 l = path->nodes[0];
2343 slot = path->slots[0];
2344 btrfs_item_key_to_cpu(l, &key, slot);
2345
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002346 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2347 len = btrfs_inode_ref_name_len(l, iref);
2348 ptr -= len + 1;
2349 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002350 if (ptr < name) {
2351 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002352 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002353 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002354
2355 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302356 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002357
2358 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2359 break;
2360
David Sterbab3b4aa72011-04-21 01:20:15 +02002361 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002362 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002363 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002364 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002365 }
Li Zefan77906a502011-07-14 03:16:00 +00002366 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302367 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002368 ret = 0;
2369out:
2370 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002371 return ret;
2372}
2373
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002374static int btrfs_search_path_in_tree_user(struct inode *inode,
2375 struct btrfs_ioctl_ino_lookup_user_args *args)
2376{
2377 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2378 struct super_block *sb = inode->i_sb;
2379 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2380 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2381 u64 dirid = args->dirid;
2382 unsigned long item_off;
2383 unsigned long item_len;
2384 struct btrfs_inode_ref *iref;
2385 struct btrfs_root_ref *rref;
2386 struct btrfs_root *root;
2387 struct btrfs_path *path;
2388 struct btrfs_key key, key2;
2389 struct extent_buffer *leaf;
2390 struct inode *temp_inode;
2391 char *ptr;
2392 int slot;
2393 int len;
2394 int total_len = 0;
2395 int ret;
2396
2397 path = btrfs_alloc_path();
2398 if (!path)
2399 return -ENOMEM;
2400
2401 /*
2402 * If the bottom subvolume does not exist directly under upper_limit,
2403 * construct the path in from the bottom up.
2404 */
2405 if (dirid != upper_limit.objectid) {
2406 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2407
2408 key.objectid = treeid;
2409 key.type = BTRFS_ROOT_ITEM_KEY;
2410 key.offset = (u64)-1;
2411 root = btrfs_read_fs_root_no_name(fs_info, &key);
2412 if (IS_ERR(root)) {
2413 ret = PTR_ERR(root);
2414 goto out;
2415 }
2416
2417 key.objectid = dirid;
2418 key.type = BTRFS_INODE_REF_KEY;
2419 key.offset = (u64)-1;
2420 while (1) {
2421 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2422 if (ret < 0) {
2423 goto out;
2424 } else if (ret > 0) {
2425 ret = btrfs_previous_item(root, path, dirid,
2426 BTRFS_INODE_REF_KEY);
2427 if (ret < 0) {
2428 goto out;
2429 } else if (ret > 0) {
2430 ret = -ENOENT;
2431 goto out;
2432 }
2433 }
2434
2435 leaf = path->nodes[0];
2436 slot = path->slots[0];
2437 btrfs_item_key_to_cpu(leaf, &key, slot);
2438
2439 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2440 len = btrfs_inode_ref_name_len(leaf, iref);
2441 ptr -= len + 1;
2442 total_len += len + 1;
2443 if (ptr < args->path) {
2444 ret = -ENAMETOOLONG;
2445 goto out;
2446 }
2447
2448 *(ptr + len) = '/';
2449 read_extent_buffer(leaf, ptr,
2450 (unsigned long)(iref + 1), len);
2451
2452 /* Check the read+exec permission of this directory */
2453 ret = btrfs_previous_item(root, path, dirid,
2454 BTRFS_INODE_ITEM_KEY);
2455 if (ret < 0) {
2456 goto out;
2457 } else if (ret > 0) {
2458 ret = -ENOENT;
2459 goto out;
2460 }
2461
2462 leaf = path->nodes[0];
2463 slot = path->slots[0];
2464 btrfs_item_key_to_cpu(leaf, &key2, slot);
2465 if (key2.objectid != dirid) {
2466 ret = -ENOENT;
2467 goto out;
2468 }
2469
David Sterba4c66e0d2019-10-03 19:09:35 +02002470 temp_inode = btrfs_iget(sb, &key2, root);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002471 if (IS_ERR(temp_inode)) {
2472 ret = PTR_ERR(temp_inode);
2473 goto out;
2474 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002475 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2476 iput(temp_inode);
2477 if (ret) {
2478 ret = -EACCES;
2479 goto out;
2480 }
2481
2482 if (key.offset == upper_limit.objectid)
2483 break;
2484 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2485 ret = -EACCES;
2486 goto out;
2487 }
2488
2489 btrfs_release_path(path);
2490 key.objectid = key.offset;
2491 key.offset = (u64)-1;
2492 dirid = key.objectid;
2493 }
2494
2495 memmove(args->path, ptr, total_len);
2496 args->path[total_len] = '\0';
2497 btrfs_release_path(path);
2498 }
2499
2500 /* Get the bottom subvolume's name from ROOT_REF */
2501 root = fs_info->tree_root;
2502 key.objectid = treeid;
2503 key.type = BTRFS_ROOT_REF_KEY;
2504 key.offset = args->treeid;
2505 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2506 if (ret < 0) {
2507 goto out;
2508 } else if (ret > 0) {
2509 ret = -ENOENT;
2510 goto out;
2511 }
2512
2513 leaf = path->nodes[0];
2514 slot = path->slots[0];
2515 btrfs_item_key_to_cpu(leaf, &key, slot);
2516
2517 item_off = btrfs_item_ptr_offset(leaf, slot);
2518 item_len = btrfs_item_size_nr(leaf, slot);
2519 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2520 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2521 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2522 ret = -EINVAL;
2523 goto out;
2524 }
2525
2526 /* Copy subvolume's name */
2527 item_off += sizeof(struct btrfs_root_ref);
2528 item_len -= sizeof(struct btrfs_root_ref);
2529 read_extent_buffer(leaf, args->name, item_off, item_len);
2530 args->name[item_len] = 0;
2531
2532out:
2533 btrfs_free_path(path);
2534 return ret;
2535}
2536
Chris Masonac8e9812010-02-28 15:39:26 -05002537static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2538 void __user *argp)
2539{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002540 struct btrfs_ioctl_ino_lookup_args *args;
2541 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002542 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002543
Julia Lawall2354d08f2010-10-29 15:14:18 -04002544 args = memdup_user(argp, sizeof(*args));
2545 if (IS_ERR(args))
2546 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002547
Al Viro496ad9a2013-01-23 17:07:38 -05002548 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002549
David Sterba01b810b2015-05-12 19:14:49 +02002550 /*
2551 * Unprivileged query to obtain the containing subvolume root id. The
2552 * path is reset so it's consistent with btrfs_search_path_in_tree.
2553 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002554 if (args->treeid == 0)
2555 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2556
David Sterba01b810b2015-05-12 19:14:49 +02002557 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2558 args->name[0] = 0;
2559 goto out;
2560 }
2561
2562 if (!capable(CAP_SYS_ADMIN)) {
2563 ret = -EPERM;
2564 goto out;
2565 }
2566
Chris Masonac8e9812010-02-28 15:39:26 -05002567 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2568 args->treeid, args->objectid,
2569 args->name);
2570
David Sterba01b810b2015-05-12 19:14:49 +02002571out:
Chris Masonac8e9812010-02-28 15:39:26 -05002572 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2573 ret = -EFAULT;
2574
2575 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002576 return ret;
2577}
2578
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002579/*
2580 * Version of ino_lookup ioctl (unprivileged)
2581 *
2582 * The main differences from ino_lookup ioctl are:
2583 *
2584 * 1. Read + Exec permission will be checked using inode_permission() during
2585 * path construction. -EACCES will be returned in case of failure.
2586 * 2. Path construction will be stopped at the inode number which corresponds
2587 * to the fd with which this ioctl is called. If constructed path does not
2588 * exist under fd's inode, -EACCES will be returned.
2589 * 3. The name of bottom subvolume is also searched and filled.
2590 */
2591static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2592{
2593 struct btrfs_ioctl_ino_lookup_user_args *args;
2594 struct inode *inode;
2595 int ret;
2596
2597 args = memdup_user(argp, sizeof(*args));
2598 if (IS_ERR(args))
2599 return PTR_ERR(args);
2600
2601 inode = file_inode(file);
2602
2603 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2604 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2605 /*
2606 * The subvolume does not exist under fd with which this is
2607 * called
2608 */
2609 kfree(args);
2610 return -EACCES;
2611 }
2612
2613 ret = btrfs_search_path_in_tree_user(inode, args);
2614
2615 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2616 ret = -EFAULT;
2617
2618 kfree(args);
2619 return ret;
2620}
2621
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002622/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2623static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2624{
2625 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2626 struct btrfs_fs_info *fs_info;
2627 struct btrfs_root *root;
2628 struct btrfs_path *path;
2629 struct btrfs_key key;
2630 struct btrfs_root_item *root_item;
2631 struct btrfs_root_ref *rref;
2632 struct extent_buffer *leaf;
2633 unsigned long item_off;
2634 unsigned long item_len;
2635 struct inode *inode;
2636 int slot;
2637 int ret = 0;
2638
2639 path = btrfs_alloc_path();
2640 if (!path)
2641 return -ENOMEM;
2642
2643 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2644 if (!subvol_info) {
2645 btrfs_free_path(path);
2646 return -ENOMEM;
2647 }
2648
2649 inode = file_inode(file);
2650 fs_info = BTRFS_I(inode)->root->fs_info;
2651
2652 /* Get root_item of inode's subvolume */
2653 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2654 key.type = BTRFS_ROOT_ITEM_KEY;
2655 key.offset = (u64)-1;
2656 root = btrfs_read_fs_root_no_name(fs_info, &key);
2657 if (IS_ERR(root)) {
2658 ret = PTR_ERR(root);
2659 goto out;
2660 }
2661 root_item = &root->root_item;
2662
2663 subvol_info->treeid = key.objectid;
2664
2665 subvol_info->generation = btrfs_root_generation(root_item);
2666 subvol_info->flags = btrfs_root_flags(root_item);
2667
2668 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2669 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2670 BTRFS_UUID_SIZE);
2671 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2672 BTRFS_UUID_SIZE);
2673
2674 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2675 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2676 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2677
2678 subvol_info->otransid = btrfs_root_otransid(root_item);
2679 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2680 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2681
2682 subvol_info->stransid = btrfs_root_stransid(root_item);
2683 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2684 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2685
2686 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2687 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2688 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2689
2690 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2691 /* Search root tree for ROOT_BACKREF of this subvolume */
2692 root = fs_info->tree_root;
2693
2694 key.type = BTRFS_ROOT_BACKREF_KEY;
2695 key.offset = 0;
2696 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2697 if (ret < 0) {
2698 goto out;
2699 } else if (path->slots[0] >=
2700 btrfs_header_nritems(path->nodes[0])) {
2701 ret = btrfs_next_leaf(root, path);
2702 if (ret < 0) {
2703 goto out;
2704 } else if (ret > 0) {
2705 ret = -EUCLEAN;
2706 goto out;
2707 }
2708 }
2709
2710 leaf = path->nodes[0];
2711 slot = path->slots[0];
2712 btrfs_item_key_to_cpu(leaf, &key, slot);
2713 if (key.objectid == subvol_info->treeid &&
2714 key.type == BTRFS_ROOT_BACKREF_KEY) {
2715 subvol_info->parent_id = key.offset;
2716
2717 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2718 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2719
2720 item_off = btrfs_item_ptr_offset(leaf, slot)
2721 + sizeof(struct btrfs_root_ref);
2722 item_len = btrfs_item_size_nr(leaf, slot)
2723 - sizeof(struct btrfs_root_ref);
2724 read_extent_buffer(leaf, subvol_info->name,
2725 item_off, item_len);
2726 } else {
2727 ret = -ENOENT;
2728 goto out;
2729 }
2730 }
2731
2732 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2733 ret = -EFAULT;
2734
2735out:
2736 btrfs_free_path(path);
2737 kzfree(subvol_info);
2738 return ret;
2739}
2740
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002741/*
2742 * Return ROOT_REF information of the subvolume containing this inode
2743 * except the subvolume name.
2744 */
2745static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2746{
2747 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2748 struct btrfs_root_ref *rref;
2749 struct btrfs_root *root;
2750 struct btrfs_path *path;
2751 struct btrfs_key key;
2752 struct extent_buffer *leaf;
2753 struct inode *inode;
2754 u64 objectid;
2755 int slot;
2756 int ret;
2757 u8 found;
2758
2759 path = btrfs_alloc_path();
2760 if (!path)
2761 return -ENOMEM;
2762
2763 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2764 if (IS_ERR(rootrefs)) {
2765 btrfs_free_path(path);
2766 return PTR_ERR(rootrefs);
2767 }
2768
2769 inode = file_inode(file);
2770 root = BTRFS_I(inode)->root->fs_info->tree_root;
2771 objectid = BTRFS_I(inode)->root->root_key.objectid;
2772
2773 key.objectid = objectid;
2774 key.type = BTRFS_ROOT_REF_KEY;
2775 key.offset = rootrefs->min_treeid;
2776 found = 0;
2777
2778 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2779 if (ret < 0) {
2780 goto out;
2781 } else if (path->slots[0] >=
2782 btrfs_header_nritems(path->nodes[0])) {
2783 ret = btrfs_next_leaf(root, path);
2784 if (ret < 0) {
2785 goto out;
2786 } else if (ret > 0) {
2787 ret = -EUCLEAN;
2788 goto out;
2789 }
2790 }
2791 while (1) {
2792 leaf = path->nodes[0];
2793 slot = path->slots[0];
2794
2795 btrfs_item_key_to_cpu(leaf, &key, slot);
2796 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2797 ret = 0;
2798 goto out;
2799 }
2800
2801 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2802 ret = -EOVERFLOW;
2803 goto out;
2804 }
2805
2806 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2807 rootrefs->rootref[found].treeid = key.offset;
2808 rootrefs->rootref[found].dirid =
2809 btrfs_root_ref_dirid(leaf, rref);
2810 found++;
2811
2812 ret = btrfs_next_item(root, path);
2813 if (ret < 0) {
2814 goto out;
2815 } else if (ret > 0) {
2816 ret = -EUCLEAN;
2817 goto out;
2818 }
2819 }
2820
2821out:
2822 if (!ret || ret == -EOVERFLOW) {
2823 rootrefs->num_items = found;
2824 /* update min_treeid for next search */
2825 if (found)
2826 rootrefs->min_treeid =
2827 rootrefs->rootref[found - 1].treeid + 1;
2828 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2829 ret = -EFAULT;
2830 }
2831
2832 kfree(rootrefs);
2833 btrfs_free_path(path);
2834
2835 return ret;
2836}
2837
Yan, Zheng76dda932009-09-21 16:00:26 -04002838static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2839 void __user *arg)
2840{
Al Viro54563d42013-09-01 15:57:51 -04002841 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002842 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002843 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002844 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002845 struct inode *inode;
2846 struct btrfs_root *root = BTRFS_I(dir)->root;
2847 struct btrfs_root *dest = NULL;
2848 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002849 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002850 int err = 0;
2851
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002852 if (!S_ISDIR(dir->i_mode))
2853 return -ENOTDIR;
2854
Yan, Zheng76dda932009-09-21 16:00:26 -04002855 vol_args = memdup_user(arg, sizeof(*vol_args));
2856 if (IS_ERR(vol_args))
2857 return PTR_ERR(vol_args);
2858
2859 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2860 namelen = strlen(vol_args->name);
2861 if (strchr(vol_args->name, '/') ||
2862 strncmp(vol_args->name, "..", namelen) == 0) {
2863 err = -EINVAL;
2864 goto out;
2865 }
2866
Al Viroa561be72011-11-23 11:57:51 -05002867 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002868 if (err)
2869 goto out;
2870
David Sterba521e0542014-04-15 16:41:44 +02002871
Al Viro00235412016-05-26 00:05:12 -04002872 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2873 if (err == -EINTR)
2874 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002875 dentry = lookup_one_len(vol_args->name, parent, namelen);
2876 if (IS_ERR(dentry)) {
2877 err = PTR_ERR(dentry);
2878 goto out_unlock_dir;
2879 }
2880
David Howells2b0143b2015-03-17 22:25:59 +00002881 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002882 err = -ENOENT;
2883 goto out_dput;
2884 }
2885
David Howells2b0143b2015-03-17 22:25:59 +00002886 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002887 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302888 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002889 /*
2890 * Regular user. Only allow this with a special mount
2891 * option, when the user has write+exec access to the
2892 * subvol root, and when rmdir(2) would have been
2893 * allowed.
2894 *
2895 * Note that this is _not_ check that the subvol is
2896 * empty or doesn't contain data that we wouldn't
2897 * otherwise be able to delete.
2898 *
2899 * Users who want to delete empty subvols should try
2900 * rmdir(2).
2901 */
2902 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002903 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002904 goto out_dput;
2905
2906 /*
2907 * Do not allow deletion if the parent dir is the same
2908 * as the dir to be deleted. That means the ioctl
2909 * must be called on the dentry referencing the root
2910 * of the subvol, not a random directory contained
2911 * within it.
2912 */
2913 err = -EINVAL;
2914 if (root == dest)
2915 goto out_dput;
2916
2917 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2918 if (err)
2919 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002920 }
2921
Miao Xie5c39da52012-10-22 11:39:53 +00002922 /* check if subvolume may be deleted by a user */
2923 err = btrfs_may_delete(dir, dentry, 1);
2924 if (err)
2925 goto out_dput;
2926
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002927 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002928 err = -EINVAL;
2929 goto out_dput;
2930 }
2931
Al Viro59551022016-01-22 15:40:57 -05002932 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002933 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002934 inode_unlock(inode);
Amir Goldstein46008d92019-05-26 17:34:03 +03002935 if (!err) {
2936 fsnotify_rmdir(dir, dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002937 d_delete(dentry);
Amir Goldstein46008d92019-05-26 17:34:03 +03002938 }
Liu Bofa6ac872013-02-20 14:10:23 +00002939
Yan, Zheng76dda932009-09-21 16:00:26 -04002940out_dput:
2941 dput(dentry);
2942out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002943 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002944out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002945 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002946out:
2947 kfree(vol_args);
2948 return err;
2949}
2950
Chris Mason1e701a32010-03-11 09:42:04 -05002951static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002952{
Al Viro496ad9a2013-01-23 17:07:38 -05002953 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002954 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002955 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002956 int ret;
2957
Ilya Dryomov25122d12013-01-20 15:57:57 +02002958 ret = mnt_want_write_file(file);
2959 if (ret)
2960 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002961
Ilya Dryomov25122d12013-01-20 15:57:57 +02002962 if (btrfs_root_readonly(root)) {
2963 ret = -EROFS;
2964 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002965 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002966
2967 switch (inode->i_mode & S_IFMT) {
2968 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002969 if (!capable(CAP_SYS_ADMIN)) {
2970 ret = -EPERM;
2971 goto out;
2972 }
Eric Sandeende78b512013-01-31 18:21:12 +00002973 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002974 break;
2975 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02002976 /*
2977 * Note that this does not check the file descriptor for write
2978 * access. This prevents defragmenting executables that are
2979 * running and allows defrag on files open in read-only mode.
2980 */
2981 if (!capable(CAP_SYS_ADMIN) &&
2982 inode_permission(inode, MAY_WRITE)) {
2983 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05002984 goto out;
2985 }
Chris Mason1e701a32010-03-11 09:42:04 -05002986
2987 range = kzalloc(sizeof(*range), GFP_KERNEL);
2988 if (!range) {
2989 ret = -ENOMEM;
2990 goto out;
2991 }
2992
2993 if (argp) {
2994 if (copy_from_user(range, argp,
2995 sizeof(*range))) {
2996 ret = -EFAULT;
2997 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002998 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002999 }
3000 /* compression requires us to start the IO */
3001 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3002 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3003 range->extent_thresh = (u32)-1;
3004 }
3005 } else {
3006 /* the rest are all set to zero by kzalloc */
3007 range->len = (u64)-1;
3008 }
Al Viro496ad9a2013-01-23 17:07:38 -05003009 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08003010 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003011 if (ret > 0)
3012 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003013 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003014 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003015 default:
3016 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003017 }
Chris Masone441d542009-01-05 16:57:23 -05003018out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003019 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003020 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003021}
3022
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003023static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003024{
3025 struct btrfs_ioctl_vol_args *vol_args;
3026 int ret;
3027
Chris Masone441d542009-01-05 16:57:23 -05003028 if (!capable(CAP_SYS_ADMIN))
3029 return -EPERM;
3030
David Sterba171938e2017-03-28 14:44:21 +02003031 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003032 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003033
Li Zefandae7b662009-04-08 15:06:54 +08003034 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003035 if (IS_ERR(vol_args)) {
3036 ret = PTR_ERR(vol_args);
3037 goto out;
3038 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003039
Mark Fasheh5516e592008-07-24 12:20:14 -04003040 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003041 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003042
Anand Jain43d20762014-07-01 00:58:56 +08003043 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003044 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003045
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003046 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003047out:
David Sterba171938e2017-03-28 14:44:21 +02003048 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003049 return ret;
3050}
3051
Anand Jain6b526ed2016-02-13 10:01:39 +08003052static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003053{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003054 struct inode *inode = file_inode(file);
3055 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003056 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003057 int ret;
3058
Chris Masone441d542009-01-05 16:57:23 -05003059 if (!capable(CAP_SYS_ADMIN))
3060 return -EPERM;
3061
Miao Xieda249272012-11-26 08:44:50 +00003062 ret = mnt_want_write_file(file);
3063 if (ret)
3064 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003065
Li Zefandae7b662009-04-08 15:06:54 +08003066 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003067 if (IS_ERR(vol_args)) {
3068 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003069 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003070 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003071
Anand Jain6b526ed2016-02-13 10:01:39 +08003072 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003073 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3074 ret = -EOPNOTSUPP;
3075 goto out;
3076 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003077
David Sterba171938e2017-03-28 14:44:21 +02003078 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003079 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3080 goto out;
3081 }
3082
David Sterba735654e2016-02-15 18:15:21 +01003083 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003084 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003085 } else {
3086 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003087 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003088 }
David Sterba171938e2017-03-28 14:44:21 +02003089 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003090
Anand Jain6b526ed2016-02-13 10:01:39 +08003091 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003092 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003093 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003094 vol_args->devid);
3095 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003096 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003097 vol_args->name);
3098 }
Anand Jain183860f2013-05-17 10:52:45 +00003099out:
3100 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003101err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003102 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003103 return ret;
3104}
3105
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003106static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3107{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003108 struct inode *inode = file_inode(file);
3109 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003110 struct btrfs_ioctl_vol_args *vol_args;
3111 int ret;
3112
3113 if (!capable(CAP_SYS_ADMIN))
3114 return -EPERM;
3115
3116 ret = mnt_want_write_file(file);
3117 if (ret)
3118 return ret;
3119
David Sterba171938e2017-03-28 14:44:21 +02003120 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003121 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003122 goto out_drop_write;
3123 }
3124
3125 vol_args = memdup_user(arg, sizeof(*vol_args));
3126 if (IS_ERR(vol_args)) {
3127 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003128 goto out;
3129 }
3130
David Sterba58d7bbf2016-05-04 14:10:47 +02003131 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003132 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003133
3134 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003135 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003136 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003137out:
David Sterba171938e2017-03-28 14:44:21 +02003138 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003139out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003140 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003141
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003142 return ret;
3143}
3144
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003145static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3146 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003147{
Li Zefan027ed2f2011-06-08 08:27:56 +00003148 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003149 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003150 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003151 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003152
Li Zefan027ed2f2011-06-08 08:27:56 +00003153 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3154 if (!fi_args)
3155 return -ENOMEM;
3156
David Sterbad03262c2017-06-16 00:09:21 +02003157 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003158 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003159
David Sterbad03262c2017-06-16 00:09:21 +02003160 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003161 if (device->devid > fi_args->max_id)
3162 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003163 }
David Sterbad03262c2017-06-16 00:09:21 +02003164 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003165
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003166 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003167 fi_args->nodesize = fs_info->nodesize;
3168 fi_args->sectorsize = fs_info->sectorsize;
3169 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003170
Li Zefan027ed2f2011-06-08 08:27:56 +00003171 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3172 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003173
Li Zefan027ed2f2011-06-08 08:27:56 +00003174 kfree(fi_args);
3175 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003176}
3177
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003178static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3179 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003180{
3181 struct btrfs_ioctl_dev_info_args *di_args;
3182 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003183 int ret = 0;
3184 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003185
Jan Schmidt475f6382011-03-11 15:41:01 +01003186 di_args = memdup_user(arg, sizeof(*di_args));
3187 if (IS_ERR(di_args))
3188 return PTR_ERR(di_args);
3189
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003190 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003191 s_uuid = di_args->uuid;
3192
David Sterbac5593ca2017-06-16 00:09:21 +02003193 rcu_read_lock();
Anand Jaine4319cd2019-01-17 23:32:31 +08003194 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08003195 NULL, true);
Jan Schmidt475f6382011-03-11 15:41:01 +01003196
3197 if (!dev) {
3198 ret = -ENODEV;
3199 goto out;
3200 }
3201
3202 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003203 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3204 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003205 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003206 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003207 strncpy(di_args->path, rcu_str_deref(dev->name),
3208 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003209 di_args->path[sizeof(di_args->path) - 1] = 0;
3210 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003211 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003212 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003213
3214out:
David Sterbac5593ca2017-06-16 00:09:21 +02003215 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003216 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3217 ret = -EFAULT;
3218
3219 kfree(di_args);
3220 return ret;
3221}
3222
Filipe Mananad8b55242019-01-08 11:43:07 +00003223static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3224 struct inode *inode2, u64 loff2, u64 len)
3225{
3226 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3227 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3228}
3229
3230static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3231 struct inode *inode2, u64 loff2, u64 len)
3232{
3233 if (inode1 < inode2) {
3234 swap(inode1, inode2);
3235 swap(loff1, loff2);
3236 } else if (inode1 == inode2 && loff2 < loff1) {
3237 swap(loff1, loff2);
3238 }
3239 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3240 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3241}
3242
Filipe Manana57a50e22018-12-12 18:05:59 +00003243static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
Filipe Manana34a28e32018-12-07 15:25:38 +00003244 struct inode *dst, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003245{
Filipe Manana831d2fa2019-12-16 18:26:56 +00003246 const u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Mark Fasheh416161d2013-08-06 11:42:51 -07003247 int ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003248
Filipe Manana34a28e32018-12-07 15:25:38 +00003249 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003250 * Lock destination range to serialize with concurrent readpages() and
3251 * source range to serialize with relocation.
Filipe Manana34a28e32018-12-07 15:25:38 +00003252 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003253 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Filipe Manana831d2fa2019-12-16 18:26:56 +00003254 ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1);
Filipe Mananad8b55242019-01-08 11:43:07 +00003255 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Timofey Titovets39739092018-05-02 08:15:36 +03003256
3257 return ret;
3258}
3259
Timofey Titovetsb6728762018-05-02 08:15:37 +03003260#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3261
Timofey Titovets39739092018-05-02 08:15:36 +03003262static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3263 struct inode *dst, u64 dst_loff)
3264{
3265 int ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003266 u64 i, tail_len, chunk_count;
Filipe Manana62d54f32019-04-22 16:43:42 +01003267 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3268
3269 spin_lock(&root_dst->root_item_lock);
3270 if (root_dst->send_in_progress) {
3271 btrfs_warn_rl(root_dst->fs_info,
3272"cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3273 root_dst->root_key.objectid,
3274 root_dst->send_in_progress);
3275 spin_unlock(&root_dst->root_item_lock);
3276 return -EAGAIN;
3277 }
3278 root_dst->dedupe_in_progress++;
3279 spin_unlock(&root_dst->root_item_lock);
Timofey Titovets39739092018-05-02 08:15:36 +03003280
Timofey Titovetsb6728762018-05-02 08:15:37 +03003281 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3282 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003283
Timofey Titovetsb6728762018-05-02 08:15:37 +03003284 for (i = 0; i < chunk_count; i++) {
3285 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Filipe Manana34a28e32018-12-07 15:25:38 +00003286 dst, dst_loff);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003287 if (ret)
Filipe Manana62d54f32019-04-22 16:43:42 +01003288 goto out;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003289
3290 loff += BTRFS_MAX_DEDUPE_LEN;
3291 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3292 }
3293
3294 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003295 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
Filipe Manana34a28e32018-12-07 15:25:38 +00003296 dst_loff);
Filipe Manana62d54f32019-04-22 16:43:42 +01003297out:
3298 spin_lock(&root_dst->root_item_lock);
3299 root_dst->dedupe_in_progress--;
3300 spin_unlock(&root_dst->root_item_lock);
Mark Fasheh416161d2013-08-06 11:42:51 -07003301
3302 return ret;
3303}
3304
Filipe Mananaf82a9902014-06-01 01:50:28 +01003305static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3306 struct inode *inode,
3307 u64 endoff,
3308 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003309 const u64 olen,
3310 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003311{
3312 struct btrfs_root *root = BTRFS_I(inode)->root;
3313 int ret;
3314
3315 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003316 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003317 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003318 /*
3319 * We round up to the block size at eof when determining which
3320 * extents to clone above, but shouldn't round up the file size.
3321 */
3322 if (endoff > destoff + olen)
3323 endoff = destoff + olen;
Josef Bacik790a1d42020-01-17 09:02:19 -05003324 if (endoff > inode->i_size) {
3325 i_size_write(inode, endoff);
Josef Bacikd923afe2020-01-17 09:02:23 -05003326 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik790a1d42020-01-17 09:02:19 -05003327 }
Filipe Mananaf82a9902014-06-01 01:50:28 +01003328
3329 ret = btrfs_update_inode(trans, root, inode);
3330 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003331 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003332 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003333 goto out;
3334 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003335 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003336out:
3337 return ret;
3338}
3339
Filipe Manana8039d872015-10-13 15:15:00 +01003340/*
3341 * Make sure we do not end up inserting an inline extent into a file that has
3342 * already other (non-inline) extents. If a file has an inline extent it can
3343 * not have any other extents and the (single) inline extent must start at the
3344 * file offset 0. Failing to respect these rules will lead to file corruption,
3345 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3346 *
3347 * We can have extents that have been already written to disk or we can have
3348 * dirty ranges still in delalloc, in which case the extent maps and items are
3349 * created only when we run delalloc, and the delalloc ranges might fall outside
3350 * the range we are currently locking in the inode's io tree. So we check the
3351 * inode's i_size because of that (i_size updates are done while holding the
3352 * i_mutex, which we are holding here).
3353 * We also check to see if the inode has a size not greater than "datal" but has
3354 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3355 * protected against such concurrent fallocate calls by the i_mutex).
3356 *
3357 * If the file has no extents but a size greater than datal, do not allow the
3358 * copy because we would need turn the inline extent into a non-inline one (even
3359 * with NO_HOLES enabled). If we find our destination inode only has one inline
3360 * extent, just overwrite it with the source inline extent if its size is less
3361 * than the source extent's size, or we could copy the source inline extent's
3362 * data into the destination inode's inline extent if the later is greater then
3363 * the former.
3364 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003365static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003366 struct btrfs_trans_handle *trans,
3367 struct btrfs_path *path,
3368 struct btrfs_key *new_key,
3369 const u64 drop_start,
3370 const u64 datal,
3371 const u64 skip,
3372 const u64 size,
3373 char *inline_data)
3374{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003376 struct btrfs_root *root = BTRFS_I(dst)->root;
3377 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003378 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003379 int ret;
3380 struct btrfs_key key;
3381
3382 if (new_key->offset > 0)
3383 return -EOPNOTSUPP;
3384
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003385 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003386 key.type = BTRFS_EXTENT_DATA_KEY;
3387 key.offset = 0;
3388 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3389 if (ret < 0) {
3390 return ret;
3391 } else if (ret > 0) {
3392 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3393 ret = btrfs_next_leaf(root, path);
3394 if (ret < 0)
3395 return ret;
3396 else if (ret > 0)
3397 goto copy_inline_extent;
3398 }
3399 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003400 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003401 key.type == BTRFS_EXTENT_DATA_KEY) {
3402 ASSERT(key.offset > 0);
3403 return -EOPNOTSUPP;
3404 }
3405 } else if (i_size_read(dst) <= datal) {
3406 struct btrfs_file_extent_item *ei;
3407 u64 ext_len;
3408
3409 /*
3410 * If the file size is <= datal, make sure there are no other
3411 * extents following (can happen do to an fallocate call with
3412 * the flag FALLOC_FL_KEEP_SIZE).
3413 */
3414 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3415 struct btrfs_file_extent_item);
3416 /*
3417 * If it's an inline extent, it can not have other extents
3418 * following it.
3419 */
3420 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3421 BTRFS_FILE_EXTENT_INLINE)
3422 goto copy_inline_extent;
3423
3424 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3425 if (ext_len > aligned_end)
3426 return -EOPNOTSUPP;
3427
3428 ret = btrfs_next_item(root, path);
3429 if (ret < 0) {
3430 return ret;
3431 } else if (ret == 0) {
3432 btrfs_item_key_to_cpu(path->nodes[0], &key,
3433 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003434 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003435 key.type == BTRFS_EXTENT_DATA_KEY)
3436 return -EOPNOTSUPP;
3437 }
3438 }
3439
3440copy_inline_extent:
3441 /*
3442 * We have no extent items, or we have an extent at offset 0 which may
3443 * or may not be inlined. All these cases are dealt the same way.
3444 */
3445 if (i_size_read(dst) > datal) {
3446 /*
3447 * If the destination inode has an inline extent...
3448 * This would require copying the data from the source inline
3449 * extent into the beginning of the destination's inline extent.
3450 * But this is really complex, both extents can be compressed
3451 * or just one of them, which would require decompressing and
3452 * re-compressing data (which could increase the new compressed
3453 * size, not allowing the compressed data to fit anymore in an
3454 * inline extent).
3455 * So just don't support this case for now (it should be rare,
3456 * we are not really saving space when cloning inline extents).
3457 */
3458 return -EOPNOTSUPP;
3459 }
3460
3461 btrfs_release_path(path);
3462 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3463 if (ret)
3464 return ret;
3465 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3466 if (ret)
3467 return ret;
3468
3469 if (skip) {
3470 const u32 start = btrfs_file_extent_calc_inline_size(0);
3471
3472 memmove(inline_data + start, inline_data + start + skip, datal);
3473 }
3474
3475 write_extent_buffer(path->nodes[0], inline_data,
3476 btrfs_item_ptr_offset(path->nodes[0],
3477 path->slots[0]),
3478 size);
3479 inode_add_bytes(dst, datal);
Filipe Manana690a5db2019-07-05 11:09:50 +01003480 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags);
Filipe Manana8039d872015-10-13 15:15:00 +01003481
3482 return 0;
3483}
3484
Mark Fasheh32b7c682013-08-06 11:42:49 -07003485/**
3486 * btrfs_clone() - clone a range from inode file to another
3487 *
3488 * @src: Inode to clone from
3489 * @inode: Inode to clone to
3490 * @off: Offset within source to start clone from
3491 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003492 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003493 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003494 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003495 */
3496static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003497 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003498 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003499{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003500 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003501 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003502 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003503 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003504 struct btrfs_trans_handle *trans;
3505 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003506 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003507 u32 nritems;
3508 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003509 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003510 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003511 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003512
3513 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003514 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3515 if (!buf)
3516 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003517
3518 path = btrfs_alloc_path();
3519 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003520 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003521 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003522 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003523
David Sterbae4058b52015-11-27 16:31:35 +01003524 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003525 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003526 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003527 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003528 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003529
3530 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003531 u64 next_key_min_offset = key.offset + 1;
Filipe Mananab64119b2019-07-02 15:23:07 +01003532 struct btrfs_file_extent_item *extent;
3533 int type;
3534 u32 size;
3535 struct btrfs_key new_key;
3536 u64 disko = 0, diskl = 0;
3537 u64 datao = 0, datal = 0;
3538 u8 comp;
3539 u64 drop_start;
Filipe Mananadf858e72015-03-31 14:56:46 +01003540
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003541 /*
3542 * note the key will change type as we walk through the
3543 * tree.
3544 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003545 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003546 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3547 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003548 if (ret < 0)
3549 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003550 /*
3551 * First search, if no extent item that starts at offset off was
3552 * found but the previous item is an extent item, it's possible
3553 * it might overlap our target range, therefore process it.
3554 */
3555 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3556 btrfs_item_key_to_cpu(path->nodes[0], &key,
3557 path->slots[0] - 1);
3558 if (key.type == BTRFS_EXTENT_DATA_KEY)
3559 path->slots[0]--;
3560 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003561
Yan Zhengae01a0a2008-08-04 23:23:47 -04003562 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003563process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003564 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003565 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003566 if (ret < 0)
3567 goto out;
3568 if (ret > 0)
3569 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003570 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003571 }
3572 leaf = path->nodes[0];
3573 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003574
Yan Zhengae01a0a2008-08-04 23:23:47 -04003575 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003576 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003577 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003578 break;
3579
Filipe Mananab64119b2019-07-02 15:23:07 +01003580 ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
Zheng Yan31840ae2008-09-23 13:14:14 -04003581
Filipe Mananab64119b2019-07-02 15:23:07 +01003582 extent = btrfs_item_ptr(leaf, slot,
3583 struct btrfs_file_extent_item);
3584 comp = btrfs_file_extent_compression(leaf, extent);
3585 type = btrfs_file_extent_type(leaf, extent);
3586 if (type == BTRFS_FILE_EXTENT_REG ||
3587 type == BTRFS_FILE_EXTENT_PREALLOC) {
3588 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
3589 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
3590 datao = btrfs_file_extent_offset(leaf, extent);
3591 datal = btrfs_file_extent_num_bytes(leaf, extent);
3592 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3593 /* Take upper bound, may be compressed */
3594 datal = btrfs_file_extent_ram_bytes(leaf, extent);
3595 }
3596
3597 /*
3598 * The first search might have left us at an extent item that
3599 * ends before our target range's start, can happen if we have
3600 * holes and NO_HOLES feature enabled.
3601 */
3602 if (key.offset + datal <= off) {
3603 path->slots[0]++;
3604 goto process_slot;
3605 } else if (key.offset >= off + len) {
3606 break;
3607 }
3608 next_key_min_offset = key.offset + datal;
3609 size = btrfs_item_size_nr(leaf, slot);
3610 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot),
3611 size);
3612
3613 btrfs_release_path(path);
3614 path->leave_spinning = 0;
3615
3616 memcpy(&new_key, &key, sizeof(new_key));
3617 new_key.objectid = btrfs_ino(BTRFS_I(inode));
3618 if (off <= key.offset)
3619 new_key.offset = key.offset + destoff - off;
3620 else
3621 new_key.offset = destoff;
3622
3623 /*
3624 * Deal with a hole that doesn't have an extent item that
3625 * represents it (NO_HOLES feature enabled).
3626 * This hole is either in the middle of the cloning range or at
3627 * the beginning (fully overlaps it or partially overlaps it).
3628 */
3629 if (new_key.offset != last_dest_end)
3630 drop_start = last_dest_end;
3631 else
3632 drop_start = new_key.offset;
3633
3634 if (type == BTRFS_FILE_EXTENT_REG ||
3635 type == BTRFS_FILE_EXTENT_PREALLOC) {
3636 struct btrfs_clone_extent_info clone_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003637
Filipe Manana2c463822014-05-31 02:31:05 +01003638 /*
Filipe Mananab64119b2019-07-02 15:23:07 +01003639 * a | --- range to clone ---| b
3640 * | ------------- extent ------------- |
Filipe Manana2c463822014-05-31 02:31:05 +01003641 */
Filipe Mananab64119b2019-07-02 15:23:07 +01003642
3643 /* Subtract range b */
3644 if (key.offset + datal > off + len)
3645 datal = off + len - key.offset;
3646
3647 /* Subtract range a */
3648 if (off > key.offset) {
3649 datao += off - key.offset;
3650 datal -= off - key.offset;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003651 }
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003652
Filipe Mananab64119b2019-07-02 15:23:07 +01003653 clone_info.disk_offset = disko;
3654 clone_info.disk_len = diskl;
3655 clone_info.data_offset = datao;
3656 clone_info.data_len = datal;
3657 clone_info.file_offset = new_key.offset;
3658 clone_info.extent_buf = buf;
3659 clone_info.item_size = size;
3660 ret = btrfs_punch_hole_range(inode, path,
Filipe Manana690a5db2019-07-05 11:09:50 +01003661 drop_start,
3662 new_key.offset + datal - 1,
3663 &clone_info, &trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003664 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003665 goto out;
Filipe Mananab64119b2019-07-02 15:23:07 +01003666 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3667 u64 skip = 0;
3668 u64 trim = 0;
3669
3670 if (off > key.offset) {
3671 skip = off - key.offset;
3672 new_key.offset += skip;
3673 }
3674
3675 if (key.offset + datal > off + len)
3676 trim = key.offset + datal - (off + len);
3677
3678 if (comp && (skip || trim)) {
3679 ret = -EINVAL;
3680 goto out;
3681 }
3682 size -= skip + trim;
3683 datal -= skip + trim;
3684
3685 /*
3686 * If our extent is inline, we know we will drop or
3687 * adjust at most 1 extent item in the destination root.
3688 *
3689 * 1 - adjusting old extent (we may have to split it)
3690 * 1 - add new extent
3691 * 1 - inode update
3692 */
3693 trans = btrfs_start_transaction(root, 3);
3694 if (IS_ERR(trans)) {
3695 ret = PTR_ERR(trans);
3696 goto out;
3697 }
3698
3699 ret = clone_copy_inline_extent(inode, trans, path,
3700 &new_key, drop_start,
3701 datal, skip, size, buf);
3702 if (ret) {
3703 if (ret != -EOPNOTSUPP)
3704 btrfs_abort_transaction(trans, ret);
3705 btrfs_end_transaction(trans);
3706 goto out;
3707 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003708 }
Filipe Mananab64119b2019-07-02 15:23:07 +01003709
3710 btrfs_release_path(path);
3711
3712 last_dest_end = ALIGN(new_key.offset + datal,
3713 fs_info->sectorsize);
3714 ret = clone_finish_inode_update(trans, inode, last_dest_end,
3715 destoff, olen, no_time_update);
3716 if (ret)
3717 goto out;
3718 if (new_key.offset + datal >= destoff + len)
3719 break;
3720
David Sterbab3b4aa72011-04-21 01:20:15 +02003721 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003722 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003723
3724 if (fatal_signal_pending(current)) {
3725 ret = -EINTR;
3726 goto out;
3727 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003728 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003729 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003730
Filipe Mananaf82a9902014-06-01 01:50:28 +01003731 if (last_dest_end < destoff + len) {
3732 /*
Filipe Manana147271e2019-12-05 16:58:41 +00003733 * We have an implicit hole that fully or partially overlaps our
3734 * cloning range at its end. This means that we either have the
3735 * NO_HOLES feature enabled or the implicit hole happened due to
3736 * mixing buffered and direct IO writes against this file.
Filipe Mananaf82a9902014-06-01 01:50:28 +01003737 */
3738 btrfs_release_path(path);
Filipe Manana690a5db2019-07-05 11:09:50 +01003739 path->leave_spinning = 0;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003740
Filipe Manana690a5db2019-07-05 11:09:50 +01003741 ret = btrfs_punch_hole_range(inode, path,
3742 last_dest_end, destoff + len - 1,
Filipe Manana147271e2019-12-05 16:58:41 +00003743 NULL, &trans);
Filipe Manana690a5db2019-07-05 11:09:50 +01003744 if (ret)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003745 goto out;
Filipe Manana690a5db2019-07-05 11:09:50 +01003746
Filipe Mananaf82a9902014-06-01 01:50:28 +01003747 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003748 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003749 }
3750
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003751out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003752 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003753 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003754 return ret;
3755}
3756
Zach Brown3db11b22015-11-10 16:53:32 -05003757static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3758 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003759{
Al Viro54563d42013-09-01 15:57:51 -04003760 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003761 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003762 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003763 int ret;
3764 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003765 u64 bs = fs_info->sb->s_blocksize;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003766
3767 /*
3768 * TODO:
3769 * - split compressed inline extents. annoying: we need to
3770 * decompress into destination's address_space (the file offset
3771 * may change, so source mapping won't do), then recompress (or
3772 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003773 *
3774 * - split destination inode's inline extents. The inline extents can
3775 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003776 */
3777
Filipe Mananaac765f82018-11-05 11:14:17 +00003778 /*
Filipe Manana34a28e32018-12-07 15:25:38 +00003779 * VFS's generic_remap_file_range_prep() protects us from cloning the
3780 * eof block into the middle of a file, which would result in corruption
3781 * if the file size is not blocksize aligned. So we don't need to check
3782 * for that case here.
Filipe Mananaac765f82018-11-05 11:14:17 +00003783 */
Filipe Manana34a28e32018-12-07 15:25:38 +00003784 if (off + len == src->i_size)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003785 len = ALIGN(src->i_size, bs) - off;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003786
3787 if (destoff > inode->i_size) {
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003788 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3789
Mark Fasheh32b7c682013-08-06 11:42:49 -07003790 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3791 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003792 return ret;
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003793 /*
3794 * We may have truncated the last block if the inode's size is
3795 * not sector size aligned, so we need to wait for writeback to
3796 * complete before proceeding further, otherwise we can race
3797 * with cloning and attempt to increment a reference to an
3798 * extent that no longer exists (writeback completed right after
3799 * we found the previous extent covering eof and before we
3800 * attempted to increment its reference count).
3801 */
3802 ret = btrfs_wait_ordered_range(inode, wb_start,
3803 destoff - wb_start);
3804 if (ret)
3805 return ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003806 }
3807
Filipe Mananac125b8b2014-05-23 05:03:34 +01003808 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003809 * Lock destination range to serialize with concurrent readpages() and
3810 * source range to serialize with relocation.
Filipe Mananac125b8b2014-05-23 05:03:34 +01003811 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003812 btrfs_double_extent_lock(src, off, inode, destoff, len);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003813 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Filipe Mananad8b55242019-01-08 11:43:07 +00003814 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003815 /*
3816 * Truncate page cache pages so that future reads will see the cloned
3817 * data immediately and not the previous data.
3818 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303819 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003820 round_down(destoff, PAGE_SIZE),
3821 round_up(destoff + len, PAGE_SIZE) - 1);
Filipe Manana34a28e32018-12-07 15:25:38 +00003822
3823 return ret;
3824}
3825
3826static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3827 struct file *file_out, loff_t pos_out,
3828 loff_t *len, unsigned int remap_flags)
3829{
3830 struct inode *inode_in = file_inode(file_in);
3831 struct inode *inode_out = file_inode(file_out);
3832 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3833 bool same_inode = inode_out == inode_in;
3834 u64 wb_len;
3835 int ret;
3836
3837 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3838 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3839
3840 if (btrfs_root_readonly(root_out))
3841 return -EROFS;
3842
3843 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3844 inode_in->i_sb != inode_out->i_sb)
3845 return -EXDEV;
3846 }
3847
Filipe Manana500710d2018-12-12 18:05:56 +00003848 /* don't make the dst file partly checksummed */
3849 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3850 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003851 return -EINVAL;
Filipe Manana500710d2018-12-12 18:05:56 +00003852 }
3853
Filipe Manana34a28e32018-12-07 15:25:38 +00003854 /*
3855 * Now that the inodes are locked, we need to start writeback ourselves
3856 * and can not rely on the writeback from the VFS's generic helper
3857 * generic_remap_file_range_prep() because:
3858 *
3859 * 1) For compression we must call filemap_fdatawrite_range() range
3860 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3861 * helper only calls it once;
3862 *
3863 * 2) filemap_fdatawrite_range(), called by the generic helper only
3864 * waits for the writeback to complete, i.e. for IO to be done, and
3865 * not for the ordered extents to complete. We need to wait for them
3866 * to complete so that new file extent items are in the fs tree.
3867 */
3868 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3869 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3870 else
3871 wb_len = ALIGN(*len, bs);
3872
3873 /*
3874 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3875 * any in progress could create its ordered extents after we wait for
3876 * existing ordered extents below).
3877 */
3878 inode_dio_wait(inode_in);
3879 if (!same_inode)
3880 inode_dio_wait(inode_out);
3881
Qu Wenruoa94d1d02019-05-08 18:49:58 +08003882 /*
3883 * Workaround to make sure NOCOW buffered write reach disk as NOCOW.
3884 *
3885 * Btrfs' back references do not have a block level granularity, they
3886 * work at the whole extent level.
3887 * NOCOW buffered write without data space reserved may not be able
3888 * to fall back to CoW due to lack of data space, thus could cause
3889 * data loss.
3890 *
3891 * Here we take a shortcut by flushing the whole inode, so that all
3892 * nocow write should reach disk as nocow before we increase the
3893 * reference of the extent. We could do better by only flushing NOCOW
3894 * data, but that needs extra accounting.
3895 *
3896 * Also we don't need to check ASYNC_EXTENT, as async extent will be
3897 * CoWed anyway, not affecting nocow part.
3898 */
3899 ret = filemap_flush(inode_in->i_mapping);
3900 if (ret < 0)
3901 return ret;
3902
Filipe Manana34a28e32018-12-07 15:25:38 +00003903 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
3904 wb_len);
3905 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003906 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00003907 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
3908 wb_len);
3909 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003910 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00003911
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003912 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
Filipe Manana34a28e32018-12-07 15:25:38 +00003913 len, remap_flags);
Zach Brown3db11b22015-11-10 16:53:32 -05003914}
3915
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003916loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
3917 struct file *dst_file, loff_t destoff, loff_t len,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11003918 unsigned int remap_flags)
Zach Brown3db11b22015-11-10 16:53:32 -05003919{
Filipe Manana34a28e32018-12-07 15:25:38 +00003920 struct inode *src_inode = file_inode(src_file);
3921 struct inode *dst_inode = file_inode(dst_file);
3922 bool same_inode = dst_inode == src_inode;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003923 int ret;
3924
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11003925 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
3926 return -EINVAL;
3927
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003928 if (same_inode)
3929 inode_lock(src_inode);
3930 else
3931 lock_two_nondirectories(src_inode, dst_inode);
3932
Filipe Manana34a28e32018-12-07 15:25:38 +00003933 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
3934 &len, remap_flags);
3935 if (ret < 0 || len == 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003936 goto out_unlock;
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11003937
Filipe Manana34a28e32018-12-07 15:25:38 +00003938 if (remap_flags & REMAP_FILE_DEDUP)
3939 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
3940 else
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003941 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
Filipe Manana34a28e32018-12-07 15:25:38 +00003942
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003943out_unlock:
Filipe Manana34a28e32018-12-07 15:25:38 +00003944 if (same_inode)
3945 inode_unlock(src_inode);
3946 else
Filipe Manana4ea748e2019-02-26 12:06:09 +00003947 unlock_two_nondirectories(src_inode, dst_inode);
Filipe Manana34a28e32018-12-07 15:25:38 +00003948
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003949 return ret < 0 ? ret : len;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003950}
3951
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003952static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3953{
Al Viro496ad9a2013-01-23 17:07:38 -05003954 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003955 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003956 struct btrfs_root *root = BTRFS_I(inode)->root;
3957 struct btrfs_root *new_root;
3958 struct btrfs_dir_item *di;
3959 struct btrfs_trans_handle *trans;
3960 struct btrfs_path *path;
3961 struct btrfs_key location;
3962 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003963 u64 objectid = 0;
3964 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003965 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003966
3967 if (!capable(CAP_SYS_ADMIN))
3968 return -EPERM;
3969
Miao Xie3c04ce02012-11-26 08:43:07 +00003970 ret = mnt_want_write_file(file);
3971 if (ret)
3972 return ret;
3973
3974 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3975 ret = -EFAULT;
3976 goto out;
3977 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003978
3979 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303980 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003981
3982 location.objectid = objectid;
3983 location.type = BTRFS_ROOT_ITEM_KEY;
3984 location.offset = (u64)-1;
3985
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003986 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003987 if (IS_ERR(new_root)) {
3988 ret = PTR_ERR(new_root);
3989 goto out;
3990 }
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003991 if (!is_fstree(new_root->root_key.objectid)) {
satoru takeuchi6d6d2822017-09-12 22:42:52 +09003992 ret = -ENOENT;
3993 goto out;
3994 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003995
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003996 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003997 if (!path) {
3998 ret = -ENOMEM;
3999 goto out;
4000 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004001 path->leave_spinning = 1;
4002
4003 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004004 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004005 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004006 ret = PTR_ERR(trans);
4007 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004008 }
4009
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004010 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4011 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004012 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004013 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004014 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004015 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004016 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004017 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004018 ret = -ENOENT;
4019 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004020 }
4021
4022 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4023 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4024 btrfs_mark_buffer_dirty(path->nodes[0]);
4025 btrfs_free_path(path);
4026
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004027 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004028 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004029out:
4030 mnt_drop_write_file(file);
4031 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004032}
4033
Su Yuec065f5b12018-04-02 17:24:11 +08004034static void get_block_group_info(struct list_head *groups_list,
4035 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004036{
David Sterba32da53862019-10-29 19:20:18 +01004037 struct btrfs_block_group *block_group;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004038
4039 space->total_bytes = 0;
4040 space->used_bytes = 0;
4041 space->flags = 0;
4042 list_for_each_entry(block_group, groups_list, list) {
4043 space->flags = block_group->flags;
David Sterbab3470b52019-10-23 18:48:22 +02004044 space->total_bytes += block_group->length;
David Sterbabf38be62019-10-23 18:48:11 +02004045 space->used_bytes += block_group->used;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004046 }
4047}
4048
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004049static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4050 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004051{
4052 struct btrfs_ioctl_space_args space_args;
4053 struct btrfs_ioctl_space_info space;
4054 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004055 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004056 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004057 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004058 static const u64 types[] = {
4059 BTRFS_BLOCK_GROUP_DATA,
4060 BTRFS_BLOCK_GROUP_SYSTEM,
4061 BTRFS_BLOCK_GROUP_METADATA,
4062 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4063 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004064 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004065 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004066 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004067 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004068 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004069
4070 if (copy_from_user(&space_args,
4071 (struct btrfs_ioctl_space_args __user *)arg,
4072 sizeof(space_args)))
4073 return -EFAULT;
4074
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004075 for (i = 0; i < num_types; i++) {
4076 struct btrfs_space_info *tmp;
4077
4078 info = NULL;
4079 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004080 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004081 list) {
4082 if (tmp->flags == types[i]) {
4083 info = tmp;
4084 break;
4085 }
4086 }
4087 rcu_read_unlock();
4088
4089 if (!info)
4090 continue;
4091
4092 down_read(&info->groups_sem);
4093 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4094 if (!list_empty(&info->block_groups[c]))
4095 slot_count++;
4096 }
4097 up_read(&info->groups_sem);
4098 }
Josef Bacik1406e432010-01-13 18:19:06 +00004099
David Sterba36523e952014-02-07 14:34:12 +01004100 /*
4101 * Global block reserve, exported as a space_info
4102 */
4103 slot_count++;
4104
Chris Mason7fde62b2010-03-16 15:40:10 -04004105 /* space_slots == 0 means they are asking for a count */
4106 if (space_args.space_slots == 0) {
4107 space_args.total_spaces = slot_count;
4108 goto out;
4109 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004110
Dan Rosenberg51788b12011-02-14 16:04:23 -05004111 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004112
Chris Mason7fde62b2010-03-16 15:40:10 -04004113 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004114
Chris Mason7fde62b2010-03-16 15:40:10 -04004115 /* we generally have at most 6 or so space infos, one for each raid
4116 * level. So, a whole page should be more than enough for everyone
4117 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004118 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004119 return -ENOMEM;
4120
4121 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004122 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004123 if (!dest)
4124 return -ENOMEM;
4125 dest_orig = dest;
4126
4127 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004128 for (i = 0; i < num_types; i++) {
4129 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004130
Dan Rosenberg51788b12011-02-14 16:04:23 -05004131 if (!slot_count)
4132 break;
4133
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004134 info = NULL;
4135 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004136 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004137 list) {
4138 if (tmp->flags == types[i]) {
4139 info = tmp;
4140 break;
4141 }
4142 }
4143 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004144
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004145 if (!info)
4146 continue;
4147 down_read(&info->groups_sem);
4148 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4149 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004150 get_block_group_info(&info->block_groups[c],
4151 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004152 memcpy(dest, &space, sizeof(space));
4153 dest++;
4154 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004155 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004156 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004157 if (!slot_count)
4158 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004159 }
4160 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004161 }
Josef Bacik1406e432010-01-13 18:19:06 +00004162
David Sterba36523e952014-02-07 14:34:12 +01004163 /*
4164 * Add global block reserve
4165 */
4166 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004167 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004168
4169 spin_lock(&block_rsv->lock);
4170 space.total_bytes = block_rsv->size;
4171 space.used_bytes = block_rsv->size - block_rsv->reserved;
4172 spin_unlock(&block_rsv->lock);
4173 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4174 memcpy(dest, &space, sizeof(space));
4175 space_args.total_spaces++;
4176 }
4177
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004178 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004179 (arg + sizeof(struct btrfs_ioctl_space_args));
4180
4181 if (copy_to_user(user_dest, dest_orig, alloc_size))
4182 ret = -EFAULT;
4183
4184 kfree(dest_orig);
4185out:
4186 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004187 ret = -EFAULT;
4188
4189 return ret;
4190}
4191
Miao Xie9a8c28b2012-11-26 08:40:43 +00004192static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4193 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004194{
Sage Weil46204592010-10-29 15:41:32 -04004195 struct btrfs_trans_handle *trans;
4196 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004197 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004198
Miao Xied4edf392013-02-20 09:17:06 +00004199 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004200 if (IS_ERR(trans)) {
4201 if (PTR_ERR(trans) != -ENOENT)
4202 return PTR_ERR(trans);
4203
4204 /* No running transaction, don't bother */
4205 transid = root->fs_info->last_trans_committed;
4206 goto out;
4207 }
Sage Weil46204592010-10-29 15:41:32 -04004208 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004209 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004210 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004211 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004212 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004213 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004214out:
Sage Weil46204592010-10-29 15:41:32 -04004215 if (argp)
4216 if (copy_to_user(argp, &transid, sizeof(transid)))
4217 return -EFAULT;
4218 return 0;
4219}
4220
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004221static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004222 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004223{
Sage Weil46204592010-10-29 15:41:32 -04004224 u64 transid;
4225
4226 if (argp) {
4227 if (copy_from_user(&transid, argp, sizeof(transid)))
4228 return -EFAULT;
4229 } else {
4230 transid = 0; /* current trans */
4231 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004232 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004233}
4234
Miao Xieb8e95482012-11-26 08:48:01 +00004235static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004236{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004237 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004238 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004239 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004240
4241 if (!capable(CAP_SYS_ADMIN))
4242 return -EPERM;
4243
4244 sa = memdup_user(arg, sizeof(*sa));
4245 if (IS_ERR(sa))
4246 return PTR_ERR(sa);
4247
Miao Xieb8e95482012-11-26 08:48:01 +00004248 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4249 ret = mnt_want_write_file(file);
4250 if (ret)
4251 goto out;
4252 }
4253
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004254 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004255 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4256 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004257
Filipe Manana5afe6ce2020-01-16 11:29:20 +00004258 /*
4259 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
4260 * error. This is important as it allows user space to know how much
4261 * progress scrub has done. For example, if scrub is canceled we get
4262 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
4263 * space. Later user space can inspect the progress from the structure
4264 * btrfs_ioctl_scrub_args and resume scrub from where it left off
4265 * previously (btrfs-progs does this).
4266 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
4267 * then return -EFAULT to signal the structure was not copied or it may
4268 * be corrupt and unreliable due to a partial copy.
4269 */
4270 if (copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004271 ret = -EFAULT;
4272
Miao Xieb8e95482012-11-26 08:48:01 +00004273 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4274 mnt_drop_write_file(file);
4275out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004276 kfree(sa);
4277 return ret;
4278}
4279
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004280static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004281{
4282 if (!capable(CAP_SYS_ADMIN))
4283 return -EPERM;
4284
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004285 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004286}
4287
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004288static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004289 void __user *arg)
4290{
4291 struct btrfs_ioctl_scrub_args *sa;
4292 int ret;
4293
4294 if (!capable(CAP_SYS_ADMIN))
4295 return -EPERM;
4296
4297 sa = memdup_user(arg, sizeof(*sa));
4298 if (IS_ERR(sa))
4299 return PTR_ERR(sa);
4300
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004301 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004302
Filipe Manana4fa99b02018-12-14 19:45:13 +00004303 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004304 ret = -EFAULT;
4305
4306 kfree(sa);
4307 return ret;
4308}
4309
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004310static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004311 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004312{
4313 struct btrfs_ioctl_get_dev_stats *sa;
4314 int ret;
4315
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004316 sa = memdup_user(arg, sizeof(*sa));
4317 if (IS_ERR(sa))
4318 return PTR_ERR(sa);
4319
David Sterbab27f7c02012-06-22 06:30:39 -06004320 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4321 kfree(sa);
4322 return -EPERM;
4323 }
4324
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004325 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004326
Filipe Mananaeee99572018-12-14 19:45:22 +00004327 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004328 ret = -EFAULT;
4329
4330 kfree(sa);
4331 return ret;
4332}
4333
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004334static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4335 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004336{
4337 struct btrfs_ioctl_dev_replace_args *p;
4338 int ret;
4339
4340 if (!capable(CAP_SYS_ADMIN))
4341 return -EPERM;
4342
4343 p = memdup_user(arg, sizeof(*p));
4344 if (IS_ERR(p))
4345 return PTR_ERR(p);
4346
4347 switch (p->cmd) {
4348 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004349 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004350 ret = -EROFS;
4351 goto out;
4352 }
David Sterba171938e2017-03-28 14:44:21 +02004353 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004354 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004355 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004356 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004357 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004358 }
4359 break;
4360 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004361 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004362 ret = 0;
4363 break;
4364 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004365 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004366 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004367 break;
4368 default:
4369 ret = -EINVAL;
4370 break;
4371 }
4372
Filipe Mananad3a53282019-01-08 11:42:09 +00004373 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004374 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004375out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004376 kfree(p);
4377 return ret;
4378}
4379
Jan Schmidtd7728c92011-07-07 16:48:38 +02004380static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4381{
4382 int ret = 0;
4383 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004384 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004385 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004386 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004387 struct inode_fs_paths *ipath = NULL;
4388 struct btrfs_path *path;
4389
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004390 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004391 return -EPERM;
4392
4393 path = btrfs_alloc_path();
4394 if (!path) {
4395 ret = -ENOMEM;
4396 goto out;
4397 }
4398
4399 ipa = memdup_user(arg, sizeof(*ipa));
4400 if (IS_ERR(ipa)) {
4401 ret = PTR_ERR(ipa);
4402 ipa = NULL;
4403 goto out;
4404 }
4405
4406 size = min_t(u32, ipa->size, 4096);
4407 ipath = init_ipath(size, root, path);
4408 if (IS_ERR(ipath)) {
4409 ret = PTR_ERR(ipath);
4410 ipath = NULL;
4411 goto out;
4412 }
4413
4414 ret = paths_from_inode(ipa->inum, ipath);
4415 if (ret < 0)
4416 goto out;
4417
4418 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004419 rel_ptr = ipath->fspath->val[i] -
4420 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004421 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004422 }
4423
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004424 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4425 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004426 if (ret) {
4427 ret = -EFAULT;
4428 goto out;
4429 }
4430
4431out:
4432 btrfs_free_path(path);
4433 free_ipath(ipath);
4434 kfree(ipa);
4435
4436 return ret;
4437}
4438
4439static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4440{
4441 struct btrfs_data_container *inodes = ctx;
4442 const size_t c = 3 * sizeof(u64);
4443
4444 if (inodes->bytes_left >= c) {
4445 inodes->bytes_left -= c;
4446 inodes->val[inodes->elem_cnt] = inum;
4447 inodes->val[inodes->elem_cnt + 1] = offset;
4448 inodes->val[inodes->elem_cnt + 2] = root;
4449 inodes->elem_cnt += 3;
4450 } else {
4451 inodes->bytes_missing += c - inodes->bytes_left;
4452 inodes->bytes_left = 0;
4453 inodes->elem_missed += 3;
4454 }
4455
4456 return 0;
4457}
4458
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004459static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004460 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004461{
4462 int ret = 0;
4463 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004464 struct btrfs_ioctl_logical_ino_args *loi;
4465 struct btrfs_data_container *inodes = NULL;
4466 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004467 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004468
4469 if (!capable(CAP_SYS_ADMIN))
4470 return -EPERM;
4471
4472 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304473 if (IS_ERR(loi))
4474 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004475
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004476 if (version == 1) {
4477 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004478 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004479 } else {
4480 /* All reserved bits must be 0 for now */
4481 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4482 ret = -EINVAL;
4483 goto out_loi;
4484 }
4485 /* Only accept flags we have defined so far */
4486 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4487 ret = -EINVAL;
4488 goto out_loi;
4489 }
4490 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004491 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004492 }
4493
Jan Schmidtd7728c92011-07-07 16:48:38 +02004494 path = btrfs_alloc_path();
4495 if (!path) {
4496 ret = -ENOMEM;
4497 goto out;
4498 }
4499
Jan Schmidtd7728c92011-07-07 16:48:38 +02004500 inodes = init_data_container(size);
4501 if (IS_ERR(inodes)) {
4502 ret = PTR_ERR(inodes);
4503 inodes = NULL;
4504 goto out;
4505 }
4506
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004507 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004508 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004509 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004510 ret = -ENOENT;
4511 if (ret < 0)
4512 goto out;
4513
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004514 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4515 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004516 if (ret)
4517 ret = -EFAULT;
4518
4519out:
4520 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004521 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004522out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004523 kfree(loi);
4524
4525 return ret;
4526}
4527
David Sterba008ef092018-03-21 02:05:27 +01004528void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004529 struct btrfs_ioctl_balance_args *bargs)
4530{
4531 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4532
4533 bargs->flags = bctl->flags;
4534
David Sterba3009a622018-03-21 01:31:04 +01004535 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004536 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4537 if (atomic_read(&fs_info->balance_pause_req))
4538 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004539 if (atomic_read(&fs_info->balance_cancel_req))
4540 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004541
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004542 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4543 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4544 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004545
David Sterba008ef092018-03-21 02:05:27 +01004546 spin_lock(&fs_info->balance_lock);
4547 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4548 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004549}
4550
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004551static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004552{
Al Viro496ad9a2013-01-23 17:07:38 -05004553 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004554 struct btrfs_fs_info *fs_info = root->fs_info;
4555 struct btrfs_ioctl_balance_args *bargs;
4556 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004557 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004558 int ret;
4559
4560 if (!capable(CAP_SYS_ADMIN))
4561 return -EPERM;
4562
Liu Boe54bfa312012-06-29 03:58:48 -06004563 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004564 if (ret)
4565 return ret;
4566
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004567again:
David Sterba171938e2017-03-28 14:44:21 +02004568 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004569 mutex_lock(&fs_info->balance_mutex);
4570 need_unlock = true;
4571 goto locked;
4572 }
4573
4574 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004575 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004576 * (1) some other op is running
4577 * (2) balance is running
4578 * (3) balance is paused -- special case (think resume)
4579 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004580 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004581 if (fs_info->balance_ctl) {
4582 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004583 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004584 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004585 /*
4586 * Lock released to allow other waiters to continue,
4587 * we'll reexamine the status again.
4588 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004589 mutex_lock(&fs_info->balance_mutex);
4590
4591 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004592 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004593 /* this is (3) */
4594 need_unlock = false;
4595 goto locked;
4596 }
4597
4598 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004599 goto again;
4600 } else {
4601 /* this is (2) */
4602 mutex_unlock(&fs_info->balance_mutex);
4603 ret = -EINPROGRESS;
4604 goto out;
4605 }
4606 } else {
4607 /* this is (1) */
4608 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004609 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004610 goto out;
4611 }
4612
4613locked:
David Sterba171938e2017-03-28 14:44:21 +02004614 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004615
4616 if (arg) {
4617 bargs = memdup_user(arg, sizeof(*bargs));
4618 if (IS_ERR(bargs)) {
4619 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004620 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004621 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004622
4623 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4624 if (!fs_info->balance_ctl) {
4625 ret = -ENOTCONN;
4626 goto out_bargs;
4627 }
4628
4629 bctl = fs_info->balance_ctl;
4630 spin_lock(&fs_info->balance_lock);
4631 bctl->flags |= BTRFS_BALANCE_RESUME;
4632 spin_unlock(&fs_info->balance_lock);
4633
4634 goto do_balance;
4635 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004636 } else {
4637 bargs = NULL;
4638 }
4639
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004640 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004641 ret = -EINPROGRESS;
4642 goto out_bargs;
4643 }
4644
David Sterba8d2db782015-11-04 15:38:29 +01004645 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004646 if (!bctl) {
4647 ret = -ENOMEM;
4648 goto out_bargs;
4649 }
4650
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004651 if (arg) {
4652 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4653 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4654 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4655
4656 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004657 } else {
4658 /* balance everything - no filters */
4659 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004660 }
4661
David Sterba8eb93452015-10-12 16:55:54 +02004662 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4663 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004664 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004665 }
4666
Ilya Dryomovde322262012-01-16 22:04:49 +02004667do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004668 /*
David Sterba149196a2018-03-20 20:23:09 +01004669 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4670 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4671 * restriper was paused all the way until unmount, in free_fs_info.
4672 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004673 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004674 need_unlock = false;
4675
David Sterba6fcf6e22018-05-07 17:44:03 +02004676 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004677 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004678
Filipe Mananad00c2d92019-01-08 11:42:01 +00004679 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004680 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4681 ret = -EFAULT;
4682 }
4683
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004684out_bctl:
4685 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004686out_bargs:
4687 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004688out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004689 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004690 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004691 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004692out:
Liu Boe54bfa312012-06-29 03:58:48 -06004693 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004694 return ret;
4695}
4696
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004697static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004698{
4699 if (!capable(CAP_SYS_ADMIN))
4700 return -EPERM;
4701
4702 switch (cmd) {
4703 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004704 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004705 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004706 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004707 }
4708
4709 return -EINVAL;
4710}
4711
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004712static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004713 void __user *arg)
4714{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004715 struct btrfs_ioctl_balance_args *bargs;
4716 int ret = 0;
4717
4718 if (!capable(CAP_SYS_ADMIN))
4719 return -EPERM;
4720
4721 mutex_lock(&fs_info->balance_mutex);
4722 if (!fs_info->balance_ctl) {
4723 ret = -ENOTCONN;
4724 goto out;
4725 }
4726
David Sterba8d2db782015-11-04 15:38:29 +01004727 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004728 if (!bargs) {
4729 ret = -ENOMEM;
4730 goto out;
4731 }
4732
David Sterba008ef092018-03-21 02:05:27 +01004733 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004734
4735 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4736 ret = -EFAULT;
4737
4738 kfree(bargs);
4739out:
4740 mutex_unlock(&fs_info->balance_mutex);
4741 return ret;
4742}
4743
Miao Xie905b0dd2012-11-26 08:50:11 +00004744static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004745{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004746 struct inode *inode = file_inode(file);
4747 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004748 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004749 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004750
4751 if (!capable(CAP_SYS_ADMIN))
4752 return -EPERM;
4753
Miao Xie905b0dd2012-11-26 08:50:11 +00004754 ret = mnt_want_write_file(file);
4755 if (ret)
4756 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004757
4758 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004759 if (IS_ERR(sa)) {
4760 ret = PTR_ERR(sa);
4761 goto drop_write;
4762 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004763
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004764 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004765
4766 switch (sa->cmd) {
4767 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004768 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004769 break;
4770 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004771 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004772 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004773 default:
4774 ret = -EINVAL;
4775 break;
4776 }
4777
Arne Jansen5d13a372011-09-14 15:53:51 +02004778 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004779 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004780drop_write:
4781 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004782 return ret;
4783}
4784
Miao Xie905b0dd2012-11-26 08:50:11 +00004785static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004786{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004787 struct inode *inode = file_inode(file);
4788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4789 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004790 struct btrfs_ioctl_qgroup_assign_args *sa;
4791 struct btrfs_trans_handle *trans;
4792 int ret;
4793 int err;
4794
4795 if (!capable(CAP_SYS_ADMIN))
4796 return -EPERM;
4797
Miao Xie905b0dd2012-11-26 08:50:11 +00004798 ret = mnt_want_write_file(file);
4799 if (ret)
4800 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004801
4802 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004803 if (IS_ERR(sa)) {
4804 ret = PTR_ERR(sa);
4805 goto drop_write;
4806 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004807
4808 trans = btrfs_join_transaction(root);
4809 if (IS_ERR(trans)) {
4810 ret = PTR_ERR(trans);
4811 goto out;
4812 }
4813
Arne Jansen5d13a372011-09-14 15:53:51 +02004814 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004815 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004816 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004817 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004818 }
4819
Qu Wenruoe082f562015-02-27 16:24:28 +08004820 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004821 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004822 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004823 btrfs_handle_fs_error(fs_info, err,
4824 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004825 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004826 if (err && !ret)
4827 ret = err;
4828
4829out:
4830 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004831drop_write:
4832 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004833 return ret;
4834}
4835
Miao Xie905b0dd2012-11-26 08:50:11 +00004836static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004837{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004838 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004839 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004840 struct btrfs_ioctl_qgroup_create_args *sa;
4841 struct btrfs_trans_handle *trans;
4842 int ret;
4843 int err;
4844
4845 if (!capable(CAP_SYS_ADMIN))
4846 return -EPERM;
4847
Miao Xie905b0dd2012-11-26 08:50:11 +00004848 ret = mnt_want_write_file(file);
4849 if (ret)
4850 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004851
4852 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004853 if (IS_ERR(sa)) {
4854 ret = PTR_ERR(sa);
4855 goto drop_write;
4856 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004857
Miao Xied86e56c2012-11-15 11:35:41 +00004858 if (!sa->qgroupid) {
4859 ret = -EINVAL;
4860 goto out;
4861 }
4862
Arne Jansen5d13a372011-09-14 15:53:51 +02004863 trans = btrfs_join_transaction(root);
4864 if (IS_ERR(trans)) {
4865 ret = PTR_ERR(trans);
4866 goto out;
4867 }
4868
Arne Jansen5d13a372011-09-14 15:53:51 +02004869 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004870 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004871 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004872 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004873 }
4874
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004875 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004876 if (err && !ret)
4877 ret = err;
4878
4879out:
4880 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004881drop_write:
4882 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004883 return ret;
4884}
4885
Miao Xie905b0dd2012-11-26 08:50:11 +00004886static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004887{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004888 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004889 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004890 struct btrfs_ioctl_qgroup_limit_args *sa;
4891 struct btrfs_trans_handle *trans;
4892 int ret;
4893 int err;
4894 u64 qgroupid;
4895
4896 if (!capable(CAP_SYS_ADMIN))
4897 return -EPERM;
4898
Miao Xie905b0dd2012-11-26 08:50:11 +00004899 ret = mnt_want_write_file(file);
4900 if (ret)
4901 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004902
4903 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004904 if (IS_ERR(sa)) {
4905 ret = PTR_ERR(sa);
4906 goto drop_write;
4907 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004908
4909 trans = btrfs_join_transaction(root);
4910 if (IS_ERR(trans)) {
4911 ret = PTR_ERR(trans);
4912 goto out;
4913 }
4914
4915 qgroupid = sa->qgroupid;
4916 if (!qgroupid) {
4917 /* take the current subvol as qgroup */
4918 qgroupid = root->root_key.objectid;
4919 }
4920
Lu Fengqif0042d52018-07-18 14:45:35 +08004921 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004922
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004923 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004924 if (err && !ret)
4925 ret = err;
4926
4927out:
4928 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004929drop_write:
4930 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004931 return ret;
4932}
4933
Jan Schmidt2f232032013-04-25 16:04:51 +00004934static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004936 struct inode *inode = file_inode(file);
4937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004938 struct btrfs_ioctl_quota_rescan_args *qsa;
4939 int ret;
4940
4941 if (!capable(CAP_SYS_ADMIN))
4942 return -EPERM;
4943
4944 ret = mnt_want_write_file(file);
4945 if (ret)
4946 return ret;
4947
4948 qsa = memdup_user(arg, sizeof(*qsa));
4949 if (IS_ERR(qsa)) {
4950 ret = PTR_ERR(qsa);
4951 goto drop_write;
4952 }
4953
4954 if (qsa->flags) {
4955 ret = -EINVAL;
4956 goto out;
4957 }
4958
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004959 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004960
4961out:
4962 kfree(qsa);
4963drop_write:
4964 mnt_drop_write_file(file);
4965 return ret;
4966}
4967
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004968static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4969 void __user *arg)
Jan Schmidt2f232032013-04-25 16:04:51 +00004970{
Jan Schmidt2f232032013-04-25 16:04:51 +00004971 struct btrfs_ioctl_quota_rescan_args *qsa;
4972 int ret = 0;
4973
4974 if (!capable(CAP_SYS_ADMIN))
4975 return -EPERM;
4976
David Sterba8d2db782015-11-04 15:38:29 +01004977 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004978 if (!qsa)
4979 return -ENOMEM;
4980
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004981 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00004982 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004983 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004984 }
4985
4986 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4987 ret = -EFAULT;
4988
4989 kfree(qsa);
4990 return ret;
4991}
4992
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004993static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4994 void __user *arg)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004995{
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004996 if (!capable(CAP_SYS_ADMIN))
4997 return -EPERM;
4998
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004999 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005000}
5001
Hugo Millsabccd002014-01-30 20:17:00 +00005002static long _btrfs_ioctl_set_received_subvol(struct file *file,
5003 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005004{
Al Viro496ad9a2013-01-23 17:07:38 -05005005 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005006 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005007 struct btrfs_root *root = BTRFS_I(inode)->root;
5008 struct btrfs_root_item *root_item = &root->root_item;
5009 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07005010 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005011 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005012 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005013
David Sterbabd60ea02014-01-16 15:50:22 +01005014 if (!inode_owner_or_capable(inode))
5015 return -EPERM;
5016
Alexander Block8ea05e32012-07-25 17:35:53 +02005017 ret = mnt_want_write_file(file);
5018 if (ret < 0)
5019 return ret;
5020
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005021 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005022
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005023 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005024 ret = -EINVAL;
5025 goto out;
5026 }
5027
5028 if (btrfs_root_readonly(root)) {
5029 ret = -EROFS;
5030 goto out;
5031 }
5032
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005033 /*
5034 * 1 - root item
5035 * 2 - uuid items (received uuid + subvol uuid)
5036 */
5037 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005038 if (IS_ERR(trans)) {
5039 ret = PTR_ERR(trans);
5040 trans = NULL;
5041 goto out;
5042 }
5043
5044 sa->rtransid = trans->transid;
5045 sa->rtime.sec = ct.tv_sec;
5046 sa->rtime.nsec = ct.tv_nsec;
5047
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005048 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5049 BTRFS_UUID_SIZE);
5050 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005051 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005052 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005053 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5054 root->root_key.objectid);
5055 if (ret && ret != -ENOENT) {
5056 btrfs_abort_transaction(trans, ret);
5057 btrfs_end_transaction(trans);
5058 goto out;
5059 }
5060 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005061 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5062 btrfs_set_root_stransid(root_item, sa->stransid);
5063 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005064 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5065 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5066 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5067 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005068
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005069 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005070 &root->root_key, &root->root_item);
5071 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005072 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005073 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005074 }
5075 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005076 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005077 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5078 root->root_key.objectid);
5079 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005080 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005081 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005082 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005083 }
5084 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005085 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005086out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005087 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005088 mnt_drop_write_file(file);
5089 return ret;
5090}
5091
5092#ifdef CONFIG_64BIT
5093static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5094 void __user *arg)
5095{
5096 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5097 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5098 int ret = 0;
5099
5100 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305101 if (IS_ERR(args32))
5102 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005103
David Sterba8d2db782015-11-04 15:38:29 +01005104 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005105 if (!args64) {
5106 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005107 goto out;
5108 }
5109
5110 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5111 args64->stransid = args32->stransid;
5112 args64->rtransid = args32->rtransid;
5113 args64->stime.sec = args32->stime.sec;
5114 args64->stime.nsec = args32->stime.nsec;
5115 args64->rtime.sec = args32->rtime.sec;
5116 args64->rtime.nsec = args32->rtime.nsec;
5117 args64->flags = args32->flags;
5118
5119 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5120 if (ret)
5121 goto out;
5122
5123 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5124 args32->stransid = args64->stransid;
5125 args32->rtransid = args64->rtransid;
5126 args32->stime.sec = args64->stime.sec;
5127 args32->stime.nsec = args64->stime.nsec;
5128 args32->rtime.sec = args64->rtime.sec;
5129 args32->rtime.nsec = args64->rtime.nsec;
5130 args32->flags = args64->flags;
5131
5132 ret = copy_to_user(arg, args32, sizeof(*args32));
5133 if (ret)
5134 ret = -EFAULT;
5135
5136out:
5137 kfree(args32);
5138 kfree(args64);
5139 return ret;
5140}
5141#endif
5142
5143static long btrfs_ioctl_set_received_subvol(struct file *file,
5144 void __user *arg)
5145{
5146 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5147 int ret = 0;
5148
5149 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305150 if (IS_ERR(sa))
5151 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005152
5153 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5154
5155 if (ret)
5156 goto out;
5157
Alexander Block8ea05e32012-07-25 17:35:53 +02005158 ret = copy_to_user(arg, sa, sizeof(*sa));
5159 if (ret)
5160 ret = -EFAULT;
5161
5162out:
5163 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005164 return ret;
5165}
5166
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005167static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
5168 void __user *arg)
jeff.liu867ab662013-01-05 02:48:01 +00005169{
Anand Jaina1b83ac2013-07-19 17:39:32 +08005170 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005171 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005172 char label[BTRFS_LABEL_SIZE];
5173
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005174 spin_lock(&fs_info->super_lock);
5175 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5176 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005177
5178 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005179
5180 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005181 btrfs_warn(fs_info,
5182 "label is too long, return the first %zu bytes",
5183 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005184 }
5185
jeff.liu867ab662013-01-05 02:48:01 +00005186 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005187
5188 return ret ? -EFAULT : 0;
5189}
5190
jeff.liua8bfd4a2013-01-05 02:48:08 +00005191static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5192{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005193 struct inode *inode = file_inode(file);
5194 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5195 struct btrfs_root *root = BTRFS_I(inode)->root;
5196 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005197 struct btrfs_trans_handle *trans;
5198 char label[BTRFS_LABEL_SIZE];
5199 int ret;
5200
5201 if (!capable(CAP_SYS_ADMIN))
5202 return -EPERM;
5203
5204 if (copy_from_user(label, arg, sizeof(label)))
5205 return -EFAULT;
5206
5207 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005208 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005209 "unable to set label with more than %d bytes",
5210 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005211 return -EINVAL;
5212 }
5213
5214 ret = mnt_want_write_file(file);
5215 if (ret)
5216 return ret;
5217
jeff.liua8bfd4a2013-01-05 02:48:08 +00005218 trans = btrfs_start_transaction(root, 0);
5219 if (IS_ERR(trans)) {
5220 ret = PTR_ERR(trans);
5221 goto out_unlock;
5222 }
5223
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005224 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005225 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005226 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005227 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005228
5229out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005230 mnt_drop_write_file(file);
5231 return ret;
5232}
5233
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005234#define INIT_FEATURE_FLAGS(suffix) \
5235 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5236 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5237 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5238
David Sterbad5131b62016-02-17 15:26:27 +01005239int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005240{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005241 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005242 INIT_FEATURE_FLAGS(SUPP),
5243 INIT_FEATURE_FLAGS(SAFE_SET),
5244 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5245 };
5246
5247 if (copy_to_user(arg, &features, sizeof(features)))
5248 return -EFAULT;
5249
5250 return 0;
5251}
5252
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005253static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
5254 void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005255{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005256 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005257 struct btrfs_ioctl_feature_flags features;
5258
5259 features.compat_flags = btrfs_super_compat_flags(super_block);
5260 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5261 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5262
5263 if (copy_to_user(arg, &features, sizeof(features)))
5264 return -EFAULT;
5265
5266 return 0;
5267}
5268
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005269static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005270 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005271 u64 change_mask, u64 flags, u64 supported_flags,
5272 u64 safe_set, u64 safe_clear)
5273{
David Sterbaf10152b2019-08-01 19:07:55 +02005274 const char *type = btrfs_feature_set_name(set);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005275 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005276 u64 disallowed, unsupported;
5277 u64 set_mask = flags & change_mask;
5278 u64 clear_mask = ~flags & change_mask;
5279
5280 unsupported = set_mask & ~supported_flags;
5281 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005282 names = btrfs_printable_features(set, unsupported);
5283 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005284 btrfs_warn(fs_info,
5285 "this kernel does not support the %s feature bit%s",
5286 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005287 kfree(names);
5288 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005289 btrfs_warn(fs_info,
5290 "this kernel does not support %s bits 0x%llx",
5291 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005292 return -EOPNOTSUPP;
5293 }
5294
5295 disallowed = set_mask & ~safe_set;
5296 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005297 names = btrfs_printable_features(set, disallowed);
5298 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005299 btrfs_warn(fs_info,
5300 "can't set the %s feature bit%s while mounted",
5301 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005302 kfree(names);
5303 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005304 btrfs_warn(fs_info,
5305 "can't set %s bits 0x%llx while mounted",
5306 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005307 return -EPERM;
5308 }
5309
5310 disallowed = clear_mask & ~safe_clear;
5311 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005312 names = btrfs_printable_features(set, disallowed);
5313 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005314 btrfs_warn(fs_info,
5315 "can't clear the %s feature bit%s while mounted",
5316 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005317 kfree(names);
5318 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005319 btrfs_warn(fs_info,
5320 "can't clear %s bits 0x%llx while mounted",
5321 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005322 return -EPERM;
5323 }
5324
5325 return 0;
5326}
5327
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005328#define check_feature(fs_info, change_mask, flags, mask_base) \
5329check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005330 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5331 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5332 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5333
5334static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005336 struct inode *inode = file_inode(file);
5337 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5338 struct btrfs_root *root = BTRFS_I(inode)->root;
5339 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005340 struct btrfs_ioctl_feature_flags flags[2];
5341 struct btrfs_trans_handle *trans;
5342 u64 newflags;
5343 int ret;
5344
5345 if (!capable(CAP_SYS_ADMIN))
5346 return -EPERM;
5347
5348 if (copy_from_user(flags, arg, sizeof(flags)))
5349 return -EFAULT;
5350
5351 /* Nothing to do */
5352 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5353 !flags[0].incompat_flags)
5354 return 0;
5355
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005356 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005357 flags[1].compat_flags, COMPAT);
5358 if (ret)
5359 return ret;
5360
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005361 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005362 flags[1].compat_ro_flags, COMPAT_RO);
5363 if (ret)
5364 return ret;
5365
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005366 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005367 flags[1].incompat_flags, INCOMPAT);
5368 if (ret)
5369 return ret;
5370
David Sterba7ab19622016-05-04 11:32:00 +02005371 ret = mnt_want_write_file(file);
5372 if (ret)
5373 return ret;
5374
David Sterba8051aa12014-02-07 14:34:04 +01005375 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005376 if (IS_ERR(trans)) {
5377 ret = PTR_ERR(trans);
5378 goto out_drop_write;
5379 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005380
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005382 newflags = btrfs_super_compat_flags(super_block);
5383 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5384 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5385 btrfs_set_super_compat_flags(super_block, newflags);
5386
5387 newflags = btrfs_super_compat_ro_flags(super_block);
5388 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5389 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5390 btrfs_set_super_compat_ro_flags(super_block, newflags);
5391
5392 newflags = btrfs_super_incompat_flags(super_block);
5393 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5394 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5395 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005396 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005397
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005398 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005399out_drop_write:
5400 mnt_drop_write_file(file);
5401
5402 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005403}
5404
Josef Bacik2351f432017-09-27 10:43:13 -04005405static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5406{
5407 struct btrfs_ioctl_send_args *arg;
5408 int ret;
5409
5410 if (compat) {
5411#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5412 struct btrfs_ioctl_send_args_32 args32;
5413
5414 ret = copy_from_user(&args32, argp, sizeof(args32));
5415 if (ret)
5416 return -EFAULT;
5417 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5418 if (!arg)
5419 return -ENOMEM;
5420 arg->send_fd = args32.send_fd;
5421 arg->clone_sources_count = args32.clone_sources_count;
5422 arg->clone_sources = compat_ptr(args32.clone_sources);
5423 arg->parent_root = args32.parent_root;
5424 arg->flags = args32.flags;
5425 memcpy(arg->reserved, args32.reserved,
5426 sizeof(args32.reserved));
5427#else
5428 return -ENOTTY;
5429#endif
5430 } else {
5431 arg = memdup_user(argp, sizeof(*arg));
5432 if (IS_ERR(arg))
5433 return PTR_ERR(arg);
5434 }
5435 ret = btrfs_ioctl_send(file, arg);
5436 kfree(arg);
5437 return ret;
5438}
5439
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005440long btrfs_ioctl(struct file *file, unsigned int
5441 cmd, unsigned long arg)
5442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005443 struct inode *inode = file_inode(file);
5444 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5445 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005446 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005447
5448 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005449 case FS_IOC_GETFLAGS:
5450 return btrfs_ioctl_getflags(file, argp);
5451 case FS_IOC_SETFLAGS:
5452 return btrfs_ioctl_setflags(file, argp);
5453 case FS_IOC_GETVERSION:
5454 return btrfs_ioctl_getversion(file, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05005455 case FS_IOC_GETFSLABEL:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005456 return btrfs_ioctl_get_fslabel(fs_info, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05005457 case FS_IOC_SETFSLABEL:
5458 return btrfs_ioctl_set_fslabel(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005459 case FITRIM:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005460 return btrfs_ioctl_fitrim(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005461 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005462 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005463 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005464 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005465 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005466 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005467 case BTRFS_IOC_SUBVOL_CREATE_V2:
5468 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005469 case BTRFS_IOC_SNAP_DESTROY:
5470 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005471 case BTRFS_IOC_SUBVOL_GETFLAGS:
5472 return btrfs_ioctl_subvol_getflags(file, argp);
5473 case BTRFS_IOC_SUBVOL_SETFLAGS:
5474 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005475 case BTRFS_IOC_DEFAULT_SUBVOL:
5476 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005477 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005478 return btrfs_ioctl_defrag(file, NULL);
5479 case BTRFS_IOC_DEFRAG_RANGE:
5480 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005481 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005482 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005483 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005484 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005485 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005486 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005487 case BTRFS_IOC_RM_DEV_V2:
5488 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005489 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005490 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005491 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005492 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005493 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005494 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005495 case BTRFS_IOC_TREE_SEARCH:
5496 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005497 case BTRFS_IOC_TREE_SEARCH_V2:
5498 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005499 case BTRFS_IOC_INO_LOOKUP:
5500 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005501 case BTRFS_IOC_INO_PATHS:
5502 return btrfs_ioctl_ino_to_path(root, argp);
5503 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005504 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5505 case BTRFS_IOC_LOGICAL_INO_V2:
5506 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005507 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005508 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005509 case BTRFS_IOC_SYNC: {
5510 int ret;
5511
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005512 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005513 if (ret)
5514 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005515 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005516 /*
5517 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005518 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005519 * processing uncleaned subvols.
5520 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005521 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005522 return ret;
5523 }
Sage Weil46204592010-10-29 15:41:32 -04005524 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005525 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005526 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005527 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005528 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005529 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005530 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005531 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005532 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005533 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005534 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005535 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005536 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005537 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005538 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005539 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005540 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5541 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005542#ifdef CONFIG_64BIT
5543 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5544 return btrfs_ioctl_set_received_subvol_32(file, argp);
5545#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005546 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005547 return _btrfs_ioctl_send(file, argp, false);
5548#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5549 case BTRFS_IOC_SEND_32:
5550 return _btrfs_ioctl_send(file, argp, true);
5551#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005552 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005553 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005554 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005555 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005556 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005557 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005558 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005559 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005560 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005561 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005562 case BTRFS_IOC_QUOTA_RESCAN:
5563 return btrfs_ioctl_quota_rescan(file, argp);
5564 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005565 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005566 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005567 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005568 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005569 return btrfs_ioctl_dev_replace(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005570 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005571 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005572 case BTRFS_IOC_GET_FEATURES:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005573 return btrfs_ioctl_get_features(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005574 case BTRFS_IOC_SET_FEATURES:
5575 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005576 case FS_IOC_FSGETXATTR:
5577 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005578 case FS_IOC_FSSETXATTR:
5579 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005580 case BTRFS_IOC_GET_SUBVOL_INFO:
5581 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005582 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5583 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005584 case BTRFS_IOC_INO_LOOKUP_USER:
5585 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005586 }
5587
5588 return -ENOTTY;
5589}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005590
5591#ifdef CONFIG_COMPAT
5592long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5593{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005594 /*
5595 * These all access 32-bit values anyway so no further
5596 * handling is necessary.
5597 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005598 switch (cmd) {
5599 case FS_IOC32_GETFLAGS:
5600 cmd = FS_IOC_GETFLAGS;
5601 break;
5602 case FS_IOC32_SETFLAGS:
5603 cmd = FS_IOC_SETFLAGS;
5604 break;
5605 case FS_IOC32_GETVERSION:
5606 cmd = FS_IOC_GETVERSION;
5607 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005608 }
5609
5610 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5611}
5612#endif