blob: a1ee0b775e652b16f9abe3134e3ae2226ba3ab9c [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);
707 BUG_ON(ret);
708
Lu Fengqi6025c192018-08-01 11:32:29 +0800709 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200710 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500711 BUG_ON(ret);
712
Lu Fengqicdb345a2018-05-29 15:01:53 +0800713 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400714 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200715 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400716 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200717
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400718fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100719 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000720 trans->block_rsv = NULL;
721 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100722 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800723
Sage Weil72fd0322010-10-29 15:41:32 -0400724 if (async_transid) {
725 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400726 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000727 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400728 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400729 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400730 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400731 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400732 if (err && !ret)
733 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500734
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900735 if (!ret) {
736 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800737 if (IS_ERR(inode))
738 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900739 d_instantiate(dentry, inode);
740 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400741 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100742
743fail_free:
744 kfree(root_item);
745 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400746}
747
Miao Xiee9662f72013-02-28 10:01:15 +0000748static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100749 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000750 u64 *async_transid, bool readonly,
751 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400753 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000754 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400755 struct btrfs_pending_snapshot *pending_snapshot;
756 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000757 int ret;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800758 bool snapshot_force_cow = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400759
Miao Xie27cdeb72014-04-02 19:51:05 +0800760 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400761 return -EINVAL;
762
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700763 if (atomic_read(&root->nr_swapfiles)) {
764 btrfs_warn(fs_info,
765 "cannot snapshot subvolume with active swapfile");
766 return -ETXTBSY;
767 }
768
David Sterba23269bf2017-02-13 11:03:44 +0100769 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100770 if (!pending_snapshot)
771 return -ENOMEM;
772
David Sterbab0c0ea62015-11-10 18:54:00 +0100773 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100774 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100775 pending_snapshot->path = btrfs_alloc_path();
776 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100777 ret = -ENOMEM;
778 goto free_pending;
779 }
780
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800781 /*
782 * Force new buffered writes to reserve space even when NOCOW is
783 * possible. This is to avoid later writeback (running dealloc) to
784 * fallback to COW mode and unexpectedly fail with ENOSPC.
785 */
David Sterbaea14b57f2017-06-22 02:19:11 +0200786 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100787 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600788 /* wait for no snapshot writes */
789 wait_event(root->subv_writers->wait,
790 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800791
Ethan Lien3cd24c62018-11-01 14:49:03 +0800792 ret = btrfs_start_delalloc_snapshot(root);
Miao Xie6a038432013-05-15 07:48:24 +0000793 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100794 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000795
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800796 /*
797 * All previous writes have started writeback in NOCOW mode, so now
798 * we force future writes to fallback to COW mode during snapshot
799 * creation.
800 */
801 atomic_inc(&root->snapshot_force_cow);
802 snapshot_force_cow = true;
803
Chris Mason6374e57a2017-06-23 09:48:21 -0700804 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000805
Miao Xie66d8f3d2012-09-06 04:02:28 -0600806 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
807 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000808 /*
809 * 1 - parent dir inode
810 * 2 - dir entries
811 * 1 - root item
812 * 2 - root ref/backref
813 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200814 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000815 */
816 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200817 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400818 false);
Miao Xied5c12072013-02-28 10:04:33 +0000819 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100820 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000821
Yan, Zhenga22285a2010-05-16 10:48:46 -0400822 pending_snapshot->dentry = dentry;
823 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800824 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000825 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000826 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400827
Miao Xied5c12072013-02-28 10:04:33 +0000828 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400829 if (IS_ERR(trans)) {
830 ret = PTR_ERR(trans);
831 goto fail;
832 }
833
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400834 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400835 list_add(&pending_snapshot->list,
836 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400837 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400838 if (async_transid) {
839 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400840 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000841 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400842 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400843 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400844 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400845 }
Miao Xieaec80302013-03-04 09:44:29 +0000846 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400847 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400848
849 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400850 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000851 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400852
Chris Masond3797302014-10-15 13:50:56 -0700853 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
854 if (ret)
855 goto fail;
856
David Howells2b0143b2015-03-17 22:25:59 +0000857 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000858 if (IS_ERR(inode)) {
859 ret = PTR_ERR(inode);
860 goto fail;
861 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900862
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000863 d_instantiate(dentry, inode);
864 ret = 0;
865fail:
David Sterba7775c812017-02-10 19:18:18 +0100866 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100867dec_and_free:
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800868 if (snapshot_force_cow)
869 atomic_dec(&root->snapshot_force_cow);
David Sterbaea14b57f2017-06-22 02:19:11 +0200870 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100871 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100872free_pending:
873 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100874 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100875 kfree(pending_snapshot);
876
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400877 return ret;
878}
879
Sage Weil4260f7c2010-10-29 15:46:43 -0400880/* copy of may_delete in fs/namei.c()
881 * Check whether we can remove a link victim from directory dir, check
882 * whether the type of victim is right.
883 * 1. We can't do it if dir is read-only (done in permission())
884 * 2. We should have write and exec permissions on dir
885 * 3. We can't remove anything from append-only dir
886 * 4. We can't do anything with immutable dir (done in permission())
887 * 5. If the sticky bit on dir is set we should either
888 * a. be owner of dir, or
889 * b. be owner of victim, or
890 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400891 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400892 * links pointing to it.
893 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
894 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
895 * 9. We can't remove a root or mountpoint.
896 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
897 * nfs_async_unlink().
898 */
899
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530900static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400901{
902 int error;
903
David Howells2b0143b2015-03-17 22:25:59 +0000904 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400905 return -ENOENT;
906
David Howells2b0143b2015-03-17 22:25:59 +0000907 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400908 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400909
910 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
911 if (error)
912 return error;
913 if (IS_APPEND(dir))
914 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000915 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
916 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400917 return -EPERM;
918 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000919 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400920 return -ENOTDIR;
921 if (IS_ROOT(victim))
922 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000923 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400924 return -EISDIR;
925 if (IS_DEADDIR(dir))
926 return -ENOENT;
927 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
928 return -EBUSY;
929 return 0;
930}
931
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400932/* copy of may_create in fs/namei.c() */
933static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
934{
David Howells2b0143b2015-03-17 22:25:59 +0000935 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400936 return -EEXIST;
937 if (IS_DEADDIR(dir))
938 return -ENOENT;
939 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
940}
941
942/*
943 * Create a new subvolume below @parent. This is largely modeled after
944 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
945 * inside this filesystem so it's quite a bit simpler.
946 */
Al Viro92872092016-11-20 19:34:31 -0500947static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100948 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400949 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200950 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000951 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400952{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400953 struct inode *dir = d_inode(parent->dentry);
954 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400955 struct dentry *dentry;
956 int error;
957
Al Viro00235412016-05-26 00:05:12 -0400958 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
959 if (error == -EINTR)
960 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400961
962 dentry = lookup_one_len(name, parent->dentry, namelen);
963 error = PTR_ERR(dentry);
964 if (IS_ERR(dentry))
965 goto out_unlock;
966
Yan, Zheng76dda932009-09-21 16:00:26 -0400967 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400968 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600969 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400970
Chris Mason9c520572012-12-17 14:26:57 -0500971 /*
972 * even if this name doesn't exist, we may get hash collisions.
973 * check for them now when we can safely fail
974 */
975 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
976 dir->i_ino, name,
977 namelen);
978 if (error)
979 goto out_dput;
980
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400981 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400982
983 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
984 goto out_up_read;
985
Chris Mason3de45862008-11-17 21:02:50 -0500986 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100987 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200988 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500989 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000990 error = create_subvol(dir, dentry, name, namelen,
991 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500992 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400993 if (!error)
994 fsnotify_mkdir(dir, dentry);
995out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400996 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400997out_dput:
998 dput(dentry);
999out_unlock:
Al Viro59551022016-01-22 15:40:57 -05001000 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001001 return error;
1002}
1003
Chris Mason4cb53002011-05-24 15:35:30 -04001004/*
1005 * When we're defragging a range, we don't want to kick it off again
1006 * if it is really just waiting for delalloc to send it down.
1007 * If we find a nice big extent or delalloc range for the bytes in the
1008 * file you want to defrag, we return 0 to let you know to skip this
1009 * part of the file
1010 */
David Sterbaaab110a2014-07-29 17:32:10 +02001011static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001012{
1013 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1014 struct extent_map *em = NULL;
1015 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1016 u64 end;
1017
1018 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001019 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001020 read_unlock(&em_tree->lock);
1021
1022 if (em) {
1023 end = extent_map_end(em);
1024 free_extent_map(em);
1025 if (end - offset > thresh)
1026 return 0;
1027 }
1028 /* if we already have a nice delalloc here, just stop */
1029 thresh /= 2;
1030 end = count_range_bits(io_tree, &offset, offset + thresh,
1031 thresh, EXTENT_DELALLOC, 1);
1032 if (end >= thresh)
1033 return 0;
1034 return 1;
1035}
1036
1037/*
1038 * helper function to walk through a file and find extents
1039 * newer than a specific transid, and smaller than thresh.
1040 *
1041 * This is used by the defragging code to find new and small
1042 * extents
1043 */
1044static int find_new_extents(struct btrfs_root *root,
1045 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001046 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001047{
1048 struct btrfs_path *path;
1049 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001050 struct extent_buffer *leaf;
1051 struct btrfs_file_extent_item *extent;
1052 int type;
1053 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001054 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001055
1056 path = btrfs_alloc_path();
1057 if (!path)
1058 return -ENOMEM;
1059
David Sterbaa4689d22011-05-31 17:08:14 +00001060 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001061 min_key.type = BTRFS_EXTENT_DATA_KEY;
1062 min_key.offset = *off;
1063
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301064 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001065 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001066 if (ret != 0)
1067 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001068process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001069 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001070 goto none;
1071 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1072 goto none;
1073
1074 leaf = path->nodes[0];
1075 extent = btrfs_item_ptr(leaf, path->slots[0],
1076 struct btrfs_file_extent_item);
1077
1078 type = btrfs_file_extent_type(leaf, extent);
1079 if (type == BTRFS_FILE_EXTENT_REG &&
1080 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1081 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1082 *off = min_key.offset;
1083 btrfs_free_path(path);
1084 return 0;
1085 }
1086
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001087 path->slots[0]++;
1088 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1089 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1090 goto process_slot;
1091 }
1092
Chris Mason4cb53002011-05-24 15:35:30 -04001093 if (min_key.offset == (u64)-1)
1094 goto none;
1095
1096 min_key.offset++;
1097 btrfs_release_path(path);
1098 }
1099none:
1100 btrfs_free_path(path);
1101 return -ENOENT;
1102}
1103
Li Zefan6c282eb2012-06-11 16:03:35 +08001104static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001105{
1106 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001107 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001108 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001109 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001110
1111 /*
1112 * hopefully we have this extent in the tree already, try without
1113 * the full extent lock
1114 */
1115 read_lock(&em_tree->lock);
1116 em = lookup_extent_mapping(em_tree, start, len);
1117 read_unlock(&em_tree->lock);
1118
1119 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001120 struct extent_state *cached = NULL;
1121 u64 end = start + len - 1;
1122
Chris Mason940100a2010-03-10 10:52:59 -05001123 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001124 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001125 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001126 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001127
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001128 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001129 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001130 }
1131
Li Zefan6c282eb2012-06-11 16:03:35 +08001132 return em;
1133}
1134
1135static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1136{
1137 struct extent_map *next;
1138 bool ret = true;
1139
1140 /* this is the last extent */
1141 if (em->start + em->len >= i_size_read(inode))
1142 return false;
1143
1144 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001145 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1146 ret = false;
1147 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001148 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001149 ret = false;
1150
1151 free_extent_map(next);
1152 return ret;
1153}
1154
David Sterbaaab110a2014-07-29 17:32:10 +02001155static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001156 u64 *last_len, u64 *skip, u64 *defrag_end,
1157 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001158{
1159 struct extent_map *em;
1160 int ret = 1;
1161 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001162 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001163
1164 /*
1165 * make sure that once we start defragging an extent, we keep on
1166 * defragging it
1167 */
1168 if (start < *defrag_end)
1169 return 1;
1170
1171 *skip = 0;
1172
1173 em = defrag_lookup_extent(inode, start);
1174 if (!em)
1175 return 0;
1176
Chris Mason940100a2010-03-10 10:52:59 -05001177 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001178 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001179 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001180 goto out;
1181 }
1182
Liu Bo4a3560c2015-08-07 16:48:41 +08001183 if (!*defrag_end)
1184 prev_mergeable = false;
1185
Li Zefan6c282eb2012-06-11 16:03:35 +08001186 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001187 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001188 * we hit a real extent, if it is big or the next extent is not a
1189 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001190 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001191 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001192 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001193 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001194out:
Chris Mason940100a2010-03-10 10:52:59 -05001195 /*
1196 * last_len ends up being a counter of how many bytes we've defragged.
1197 * every time we choose not to defrag an extent, we reset *last_len
1198 * so that the next tiny extent will force a defrag.
1199 *
1200 * The end result of this is that tiny extents before a single big
1201 * extent will force at least part of that big extent to be defragged.
1202 */
1203 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001204 *defrag_end = extent_map_end(em);
1205 } else {
1206 *last_len = 0;
1207 *skip = extent_map_end(em);
1208 *defrag_end = 0;
1209 }
1210
1211 free_extent_map(em);
1212 return ret;
1213}
1214
Chris Mason4cb53002011-05-24 15:35:30 -04001215/*
1216 * it doesn't do much good to defrag one or two pages
1217 * at a time. This pulls in a nice chunk of pages
1218 * to COW and defrag.
1219 *
1220 * It also makes sure the delalloc code has enough
1221 * dirty data to avoid making new small extents as part
1222 * of the defrag
1223 *
1224 * It's a good idea to start RA on this range
1225 * before calling this.
1226 */
1227static int cluster_pages_for_defrag(struct inode *inode,
1228 struct page **pages,
1229 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001230 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001231{
Chris Mason4cb53002011-05-24 15:35:30 -04001232 unsigned long file_end;
1233 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001234 u64 page_start;
1235 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001236 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001237 int ret;
1238 int i;
1239 int i_done;
1240 struct btrfs_ordered_extent *ordered;
1241 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001242 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001243 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001244 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001245
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001246 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001247 if (!isize || start_index > file_end)
1248 return 0;
1249
1250 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001251
Qu Wenruo364ecf32017-02-27 15:10:38 +08001252 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001253 start_index << PAGE_SHIFT,
1254 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001255 if (ret)
1256 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001257 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001258 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001259
1260 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001261 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001262 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001263again:
Josef Bacika94733d2011-07-11 10:47:06 -04001264 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001265 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001266 if (!page)
1267 break;
1268
Miao Xie600a45e2012-02-16 15:01:24 +08001269 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001270 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001271 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001272 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001273 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001274 ordered = btrfs_lookup_ordered_extent(inode,
1275 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001276 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001277 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001278 if (!ordered)
1279 break;
1280
1281 unlock_page(page);
1282 btrfs_start_ordered_extent(inode, ordered, 1);
1283 btrfs_put_ordered_extent(ordered);
1284 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001285 /*
1286 * we unlocked the page above, so we need check if
1287 * it was released or not.
1288 */
1289 if (page->mapping != inode->i_mapping) {
1290 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001291 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001292 goto again;
1293 }
Miao Xie600a45e2012-02-16 15:01:24 +08001294 }
1295
Chris Mason4cb53002011-05-24 15:35:30 -04001296 if (!PageUptodate(page)) {
1297 btrfs_readpage(NULL, page);
1298 lock_page(page);
1299 if (!PageUptodate(page)) {
1300 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001301 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001302 ret = -EIO;
1303 break;
1304 }
1305 }
Miao Xie600a45e2012-02-16 15:01:24 +08001306
Miao Xie600a45e2012-02-16 15:01:24 +08001307 if (page->mapping != inode->i_mapping) {
1308 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001309 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001310 goto again;
1311 }
1312
Chris Mason4cb53002011-05-24 15:35:30 -04001313 pages[i] = page;
1314 i_done++;
1315 }
1316 if (!i_done || ret)
1317 goto out;
1318
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001319 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001320 goto out;
1321
1322 /*
1323 * so now we have a nice long stream of locked
1324 * and up to date pages, lets wait on them
1325 */
1326 for (i = 0; i < i_done; i++)
1327 wait_on_page_writeback(pages[i]);
1328
1329 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001330 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001331
1332 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001333 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001334 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
Omar Sandovale1821632019-08-15 14:04:04 -07001335 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1336 EXTENT_DEFRAG, 0, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001337
Liu Bo1f12bd02012-03-29 09:57:44 -04001338 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001339 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001340 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001341 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001342 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001343 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001344 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001345 }
1346
1347
Liu Bo9e8a4a82012-09-05 19:10:51 -06001348 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001349 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001350
1351 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001352 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001353
1354 for (i = 0; i < i_done; i++) {
1355 clear_page_dirty_for_io(pages[i]);
1356 ClearPageChecked(pages[i]);
1357 set_page_extent_mapped(pages[i]);
1358 set_page_dirty(pages[i]);
1359 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001360 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001361 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08001362 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001363 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001364 return i_done;
1365out:
1366 for (i = 0; i < i_done; i++) {
1367 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001368 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001369 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001370 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001371 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001372 page_cnt << PAGE_SHIFT, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08001373 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001374 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001375 return ret;
1376
1377}
1378
1379int btrfs_defrag_file(struct inode *inode, struct file *file,
1380 struct btrfs_ioctl_defrag_range_args *range,
1381 u64 newer_than, unsigned long max_to_defrag)
1382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001383 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001384 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001385 struct file_ra_state *ra = NULL;
1386 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001387 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001388 u64 last_len = 0;
1389 u64 skip = 0;
1390 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001391 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001392 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001393 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001394 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001395 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001396 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001397 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001398 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001399 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001400 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001401 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001402 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001403
Liu Bo0abd5b12013-04-16 09:20:28 +00001404 if (isize == 0)
1405 return 0;
1406
1407 if (range->start >= isize)
1408 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001409
David Sterba1e2ef462017-07-17 20:01:59 +02001410 if (do_compress) {
Chengguang Xuce96b7f2019-10-10 15:59:57 +08001411 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Li Zefan1a419d82010-10-25 15:12:50 +08001412 return -EINVAL;
1413 if (range->compress_type)
1414 compress_type = range->compress_type;
1415 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001416
Liu Bo0abd5b12013-04-16 09:20:28 +00001417 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001418 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001419
Chris Mason4cb53002011-05-24 15:35:30 -04001420 /*
David Sterba0a52d102017-06-22 03:22:58 +02001421 * If we were not given a file, allocate a readahead context. As
1422 * readahead is just an optimization, defrag will work without it so
1423 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001424 */
1425 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001426 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001427 if (ra)
1428 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001429 } else {
1430 ra = &file->f_ra;
1431 }
1432
David Sterba63e727e2017-06-22 03:13:02 +02001433 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001434 if (!pages) {
1435 ret = -ENOMEM;
1436 goto out_ra;
1437 }
1438
1439 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001440 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001441 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001442 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001443 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001444 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001445 }
1446
Chris Mason4cb53002011-05-24 15:35:30 -04001447 if (newer_than) {
1448 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001449 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001450 if (!ret) {
1451 range->start = newer_off;
1452 /*
1453 * we always align our defrag to help keep
1454 * the extents in the file evenly spaced
1455 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001456 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001457 } else
1458 goto out_ra;
1459 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001460 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001461 }
1462 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301463 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001464
Li Zefan2a0f7f52011-10-10 15:43:34 -04001465 /*
1466 * make writeback starts from i, so the defrag range can be
1467 * written sequentially.
1468 */
1469 if (i < inode->i_mapping->writeback_index)
1470 inode->i_mapping->writeback_index = i;
1471
Chris Masonf7f43cc2011-10-11 11:41:40 -04001472 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001473 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001474 /*
1475 * make sure we stop running if someone unmounts
1476 * the FS
1477 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001478 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001479 break;
1480
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001481 if (btrfs_defrag_cancelled(fs_info)) {
1482 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001483 ret = -EAGAIN;
1484 break;
1485 }
1486
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001487 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001488 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001489 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001490 unsigned long next;
1491 /*
1492 * the should_defrag function tells us how much to skip
1493 * bump our counter by the suggested amount
1494 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001495 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001496 i = max(i + 1, next);
1497 continue;
1498 }
Li Zefan008873e2011-09-02 15:57:07 +08001499
1500 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001501 cluster = (PAGE_ALIGN(defrag_end) >>
1502 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001503 cluster = min(cluster, max_cluster);
1504 } else {
1505 cluster = max_cluster;
1506 }
1507
Li Zefan008873e2011-09-02 15:57:07 +08001508 if (i + cluster > ra_index) {
1509 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001510 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001511 page_cache_sync_readahead(inode->i_mapping, ra,
1512 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301513 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001514 }
Chris Mason940100a2010-03-10 10:52:59 -05001515
Al Viro59551022016-01-22 15:40:57 -05001516 inode_lock(inode);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001517 if (IS_SWAPFILE(inode)) {
1518 ret = -ETXTBSY;
1519 } else {
1520 if (do_compress)
1521 BTRFS_I(inode)->defrag_compress = compress_type;
1522 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1523 }
Liu Boecb8bea2012-03-29 09:57:44 -04001524 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001525 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001526 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001527 }
Chris Mason940100a2010-03-10 10:52:59 -05001528
Chris Mason4cb53002011-05-24 15:35:30 -04001529 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001530 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001531 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001532
1533 if (newer_than) {
1534 if (newer_off == (u64)-1)
1535 break;
1536
Liu Boe1f041e2012-03-29 09:57:45 -04001537 if (ret > 0)
1538 i += ret;
1539
Chris Mason4cb53002011-05-24 15:35:30 -04001540 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001541 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001542
Byongho Leeee221842015-12-15 01:42:10 +09001543 ret = find_new_extents(root, inode, newer_than,
1544 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001545 if (!ret) {
1546 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001547 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001548 } else {
1549 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001550 }
Chris Mason4cb53002011-05-24 15:35:30 -04001551 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001552 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001553 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001554 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001555 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001556 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001557 last_len = 0;
1558 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001559 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001560 }
1561
Filipe Mananadec8ef92014-03-01 10:55:54 +00001562 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001563 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001564 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1565 &BTRFS_I(inode)->runtime_flags))
1566 filemap_flush(inode->i_mapping);
1567 }
Chris Mason1e701a32010-03-11 09:42:04 -05001568
Li Zefan1a419d82010-10-25 15:12:50 +08001569 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001570 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001571 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1572 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001573 }
1574
Diego Calleja60ccf822011-09-01 16:33:57 +02001575 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001576
Chris Mason4cb53002011-05-24 15:35:30 -04001577out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001578 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001579 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001580 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001581 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001582 }
Chris Mason4cb53002011-05-24 15:35:30 -04001583 if (!file)
1584 kfree(ra);
1585 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001586 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001587}
1588
Miao Xie198605a2012-11-26 08:43:45 +00001589static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001590 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001591{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001592 struct inode *inode = file_inode(file);
1593 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001594 u64 new_size;
1595 u64 old_size;
1596 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001597 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001598 struct btrfs_ioctl_vol_args *vol_args;
1599 struct btrfs_trans_handle *trans;
1600 struct btrfs_device *device = NULL;
1601 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001602 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001603 char *devstr = NULL;
1604 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001605 int mod = 0;
1606
Chris Masone441d542009-01-05 16:57:23 -05001607 if (!capable(CAP_SYS_ADMIN))
1608 return -EPERM;
1609
Miao Xie198605a2012-11-26 08:43:45 +00001610 ret = mnt_want_write_file(file);
1611 if (ret)
1612 return ret;
1613
David Sterba171938e2017-03-28 14:44:21 +02001614 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001615 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001616 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001617 }
1618
Li Zefandae7b662009-04-08 15:06:54 +08001619 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001620 if (IS_ERR(vol_args)) {
1621 ret = PTR_ERR(vol_args);
1622 goto out;
1623 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001624
1625 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001627 sizestr = vol_args->name;
1628 devstr = strchr(sizestr, ':');
1629 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001630 sizestr = devstr + 1;
1631 *devstr = '\0';
1632 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001633 ret = kstrtoull(devstr, 10, &devid);
1634 if (ret)
1635 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001636 if (!devid) {
1637 ret = -EINVAL;
1638 goto out_free;
1639 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001640 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001641 }
Miao Xiedba60f32012-12-21 09:19:51 +00001642
Anand Jain09ba3bc2019-01-19 14:48:55 +08001643 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001644 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001645 btrfs_info(fs_info, "resizer unable to find device %llu",
1646 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001647 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001648 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001649 }
Miao Xiedba60f32012-12-21 09:19:51 +00001650
Anand Jainebbede42017-12-04 12:54:52 +08001651 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001652 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001653 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001654 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001655 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001656 goto out_free;
1657 }
1658
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001659 if (!strcmp(sizestr, "max"))
1660 new_size = device->bdev->bd_inode->i_size;
1661 else {
1662 if (sizestr[0] == '-') {
1663 mod = -1;
1664 sizestr++;
1665 } else if (sizestr[0] == '+') {
1666 mod = 1;
1667 sizestr++;
1668 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001669 new_size = memparse(sizestr, &retptr);
1670 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001671 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001672 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001673 }
1674 }
1675
Anand Jain401e29c2017-12-04 12:54:55 +08001676 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001677 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001678 goto out_free;
1679 }
1680
Miao Xie7cc8e582014-09-03 21:35:38 +08001681 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001682
1683 if (mod < 0) {
1684 if (new_size > old_size) {
1685 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001686 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001687 }
1688 new_size = old_size - new_size;
1689 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001690 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001691 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001692 goto out_free;
1693 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001694 new_size = old_size + new_size;
1695 }
1696
Byongho Leeee221842015-12-15 01:42:10 +09001697 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001698 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001699 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001700 }
1701 if (new_size > device->bdev->bd_inode->i_size) {
1702 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001703 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001704 }
1705
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001706 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001707
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001708 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1709 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001710
1711 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001712 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001713 if (IS_ERR(trans)) {
1714 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001715 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001716 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001717 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001718 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001719 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001720 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001721 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001722
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001723out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001724 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001725out:
David Sterba171938e2017-03-28 14:44:21 +02001726 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001727 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001728 return ret;
1729}
1730
Sage Weil72fd0322010-10-29 15:41:32 -04001731static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001732 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001733 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001734 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001735{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001736 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001737 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001738
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001739 if (!S_ISDIR(file_inode(file)->i_mode))
1740 return -ENOTDIR;
1741
Liu Boa874a632012-06-29 03:58:46 -06001742 ret = mnt_want_write_file(file);
1743 if (ret)
1744 goto out;
1745
Sage Weil72fd0322010-10-29 15:41:32 -04001746 namelen = strlen(name);
1747 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001748 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001749 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750 }
1751
Chris Mason16780ca2012-02-20 22:14:55 -05001752 if (name[0] == '.' &&
1753 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1754 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001755 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001756 }
1757
Chris Mason3de45862008-11-17 21:02:50 -05001758 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001759 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001760 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001761 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001762 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001763 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001764 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001765 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001766 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001767 }
1768
Al Viro496ad9a2013-01-23 17:07:38 -05001769 src_inode = file_inode(src.file);
1770 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001771 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001772 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001773 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001774 } else if (!inode_owner_or_capable(src_inode)) {
1775 /*
1776 * Subvolume creation is not restricted, but snapshots
1777 * are limited to own subvolumes only
1778 */
1779 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001780 } else {
1781 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1782 BTRFS_I(src_inode)->root,
1783 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001784 }
Al Viro2903ff02012-08-28 12:52:22 -04001785 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001786 }
Liu Boa874a632012-06-29 03:58:46 -06001787out_drop_write:
1788 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001789out:
Sage Weil72fd0322010-10-29 15:41:32 -04001790 return ret;
1791}
1792
1793static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001794 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001795{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001796 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001797 int ret;
1798
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001799 if (!S_ISDIR(file_inode(file)->i_mode))
1800 return -ENOTDIR;
1801
Li Zefanfa0d2b92010-12-20 15:53:28 +08001802 vol_args = memdup_user(arg, sizeof(*vol_args));
1803 if (IS_ERR(vol_args))
1804 return PTR_ERR(vol_args);
1805 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001806
Li Zefanfa0d2b92010-12-20 15:53:28 +08001807 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001808 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001809 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001810
Li Zefanfa0d2b92010-12-20 15:53:28 +08001811 kfree(vol_args);
1812 return ret;
1813}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001814
Li Zefanfa0d2b92010-12-20 15:53:28 +08001815static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1816 void __user *arg, int subvol)
1817{
1818 struct btrfs_ioctl_vol_args_v2 *vol_args;
1819 int ret;
1820 u64 transid = 0;
1821 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001822 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001823 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001824
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001825 if (!S_ISDIR(file_inode(file)->i_mode))
1826 return -ENOTDIR;
1827
Li Zefanfa0d2b92010-12-20 15:53:28 +08001828 vol_args = memdup_user(arg, sizeof(*vol_args));
1829 if (IS_ERR(vol_args))
1830 return PTR_ERR(vol_args);
1831 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001832
Li Zefanb83cc962010-12-20 16:04:08 +08001833 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001834 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1835 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001836 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001837 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001838 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001839
Nikolay Borisovebc87352019-08-26 17:34:24 +03001840 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1841 struct inode *inode = file_inode(file);
1842 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1843
1844 btrfs_warn(fs_info,
1845"SNAP_CREATE_V2 ioctl with CREATE_ASYNC is deprecated and will be removed in kernel 5.7");
1846
Li Zefanfa0d2b92010-12-20 15:53:28 +08001847 ptr = &transid;
Nikolay Borisovebc87352019-08-26 17:34:24 +03001848 }
Li Zefanb83cc962010-12-20 16:04:08 +08001849 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1850 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001851 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001852 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001853 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001854 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001855 }
1856 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1857 if (IS_ERR(inherit)) {
1858 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001859 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001860 }
1861 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001862
1863 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001864 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001865 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001866 if (ret)
1867 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001868
Dan Carpenterc47ca322014-09-04 14:09:15 +03001869 if (ptr && copy_to_user(arg +
1870 offsetof(struct btrfs_ioctl_vol_args_v2,
1871 transid),
1872 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001873 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001874
1875free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001876 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001877free_args:
1878 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001879 return ret;
1880}
1881
Li Zefan0caa1022010-12-20 16:30:25 +08001882static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1883 void __user *arg)
1884{
Al Viro496ad9a2013-01-23 17:07:38 -05001885 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001886 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001887 struct btrfs_root *root = BTRFS_I(inode)->root;
1888 int ret = 0;
1889 u64 flags = 0;
1890
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001891 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001892 return -EINVAL;
1893
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001894 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001895 if (btrfs_root_readonly(root))
1896 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001897 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001898
1899 if (copy_to_user(arg, &flags, sizeof(flags)))
1900 ret = -EFAULT;
1901
1902 return ret;
1903}
1904
1905static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1906 void __user *arg)
1907{
Al Viro496ad9a2013-01-23 17:07:38 -05001908 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001909 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001910 struct btrfs_root *root = BTRFS_I(inode)->root;
1911 struct btrfs_trans_handle *trans;
1912 u64 root_flags;
1913 u64 flags;
1914 int ret = 0;
1915
David Sterbabd60ea02014-01-16 15:50:22 +01001916 if (!inode_owner_or_capable(inode))
1917 return -EPERM;
1918
Liu Bob9ca0662012-06-29 03:58:49 -06001919 ret = mnt_want_write_file(file);
1920 if (ret)
1921 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001922
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001923 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001924 ret = -EINVAL;
1925 goto out_drop_write;
1926 }
Li Zefan0caa1022010-12-20 16:30:25 +08001927
Liu Bob9ca0662012-06-29 03:58:49 -06001928 if (copy_from_user(&flags, arg, sizeof(flags))) {
1929 ret = -EFAULT;
1930 goto out_drop_write;
1931 }
Li Zefan0caa1022010-12-20 16:30:25 +08001932
Liu Bob9ca0662012-06-29 03:58:49 -06001933 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1934 ret = -EINVAL;
1935 goto out_drop_write;
1936 }
Li Zefan0caa1022010-12-20 16:30:25 +08001937
Liu Bob9ca0662012-06-29 03:58:49 -06001938 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1939 ret = -EOPNOTSUPP;
1940 goto out_drop_write;
1941 }
Li Zefan0caa1022010-12-20 16:30:25 +08001942
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001943 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001944
1945 /* nothing to do */
1946 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001947 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001948
1949 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001950 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001951 btrfs_set_root_flags(&root->root_item,
1952 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001953 } else {
1954 /*
1955 * Block RO -> RW transition if this subvolume is involved in
1956 * send
1957 */
1958 spin_lock(&root->root_item_lock);
1959 if (root->send_in_progress == 0) {
1960 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001961 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001962 spin_unlock(&root->root_item_lock);
1963 } else {
1964 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001965 btrfs_warn(fs_info,
1966 "Attempt to set subvolume %llu read-write during send",
1967 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001968 ret = -EPERM;
1969 goto out_drop_sem;
1970 }
1971 }
Li Zefan0caa1022010-12-20 16:30:25 +08001972
1973 trans = btrfs_start_transaction(root, 1);
1974 if (IS_ERR(trans)) {
1975 ret = PTR_ERR(trans);
1976 goto out_reset;
1977 }
1978
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001979 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001980 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc82017-09-28 10:53:17 +03001981 if (ret < 0) {
1982 btrfs_end_transaction(trans);
1983 goto out_reset;
1984 }
Li Zefan0caa1022010-12-20 16:30:25 +08001985
Nikolay Borisov9417ebc82017-09-28 10:53:17 +03001986 ret = btrfs_commit_transaction(trans);
1987
Li Zefan0caa1022010-12-20 16:30:25 +08001988out_reset:
1989 if (ret)
1990 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001991out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001993out_drop_write:
1994 mnt_drop_write_file(file);
1995out:
Li Zefan0caa1022010-12-20 16:30:25 +08001996 return ret;
1997}
1998
Chris Masonac8e9812010-02-28 15:39:26 -05001999static noinline int key_in_sk(struct btrfs_key *key,
2000 struct btrfs_ioctl_search_key *sk)
2001{
Chris Masonabc6e132010-03-18 12:10:08 -04002002 struct btrfs_key test;
2003 int ret;
2004
2005 test.objectid = sk->min_objectid;
2006 test.type = sk->min_type;
2007 test.offset = sk->min_offset;
2008
2009 ret = btrfs_comp_cpu_keys(key, &test);
2010 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002011 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002012
2013 test.objectid = sk->max_objectid;
2014 test.type = sk->max_type;
2015 test.offset = sk->max_offset;
2016
2017 ret = btrfs_comp_cpu_keys(key, &test);
2018 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002019 return 0;
2020 return 1;
2021}
2022
Jeff Mahoneydf397562016-06-21 20:18:21 -04002023static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002024 struct btrfs_key *key,
2025 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002026 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002027 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002028 unsigned long *sk_offset,
2029 int *num_found)
2030{
2031 u64 found_transid;
2032 struct extent_buffer *leaf;
2033 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002034 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002035 unsigned long item_off;
2036 unsigned long item_len;
2037 int nritems;
2038 int i;
2039 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002040 int ret = 0;
2041
2042 leaf = path->nodes[0];
2043 slot = path->slots[0];
2044 nritems = btrfs_header_nritems(leaf);
2045
2046 if (btrfs_header_generation(leaf) > sk->max_transid) {
2047 i = nritems;
2048 goto advance_key;
2049 }
2050 found_transid = btrfs_header_generation(leaf);
2051
2052 for (i = slot; i < nritems; i++) {
2053 item_off = btrfs_item_ptr_offset(leaf, i);
2054 item_len = btrfs_item_size_nr(leaf, i);
2055
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002056 btrfs_item_key_to_cpu(leaf, key, i);
2057 if (!key_in_sk(key, sk))
2058 continue;
2059
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002060 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002061 if (*num_found) {
2062 ret = 1;
2063 goto out;
2064 }
Chris Masonac8e9812010-02-28 15:39:26 -05002065
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002066 /*
2067 * return one empty item back for v1, which does not
2068 * handle -EOVERFLOW
2069 */
2070
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002071 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002072 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002073 ret = -EOVERFLOW;
2074 }
Chris Masonac8e9812010-02-28 15:39:26 -05002075
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002076 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002077 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002078 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002079 }
2080
Chris Masonac8e9812010-02-28 15:39:26 -05002081 sh.objectid = key->objectid;
2082 sh.offset = key->offset;
2083 sh.type = key->type;
2084 sh.len = item_len;
2085 sh.transid = found_transid;
2086
2087 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002088 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2089 ret = -EFAULT;
2090 goto out;
2091 }
2092
Chris Masonac8e9812010-02-28 15:39:26 -05002093 *sk_offset += sizeof(sh);
2094
2095 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002096 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002097 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002098 if (read_extent_buffer_to_user(leaf, up,
2099 item_off, item_len)) {
2100 ret = -EFAULT;
2101 goto out;
2102 }
2103
Chris Masonac8e9812010-02-28 15:39:26 -05002104 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002105 }
Hugo Millse2156862011-05-14 17:43:41 +00002106 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002107
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002108 if (ret) /* -EOVERFLOW from above */
2109 goto out;
2110
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002111 if (*num_found >= sk->nr_items) {
2112 ret = 1;
2113 goto out;
2114 }
Chris Masonac8e9812010-02-28 15:39:26 -05002115 }
2116advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002117 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002118 test.objectid = sk->max_objectid;
2119 test.type = sk->max_type;
2120 test.offset = sk->max_offset;
2121 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2122 ret = 1;
2123 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002124 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002125 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002126 key->offset = 0;
2127 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002128 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002129 key->offset = 0;
2130 key->type = 0;
2131 key->objectid++;
2132 } else
2133 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002134out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002135 /*
2136 * 0: all items from this leaf copied, continue with next
2137 * 1: * more items can be copied, but unused buffer is too small
2138 * * all items were found
2139 * Either way, it will stops the loop which iterates to the next
2140 * leaf
2141 * -EOVERFLOW: item was to large for buffer
2142 * -EFAULT: could not copy extent buffer back to userspace
2143 */
Chris Masonac8e9812010-02-28 15:39:26 -05002144 return ret;
2145}
2146
2147static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002148 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002149 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002150 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002151{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002152 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002153 struct btrfs_root *root;
2154 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002155 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002156 int ret;
2157 int num_found = 0;
2158 unsigned long sk_offset = 0;
2159
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002160 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2161 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002162 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002163 }
Gerhard Heift12544442014-01-30 16:23:58 +01002164
Chris Masonac8e9812010-02-28 15:39:26 -05002165 path = btrfs_alloc_path();
2166 if (!path)
2167 return -ENOMEM;
2168
2169 if (sk->tree_id == 0) {
2170 /* search the root of the inode that was passed */
2171 root = BTRFS_I(inode)->root;
2172 } else {
2173 key.objectid = sk->tree_id;
2174 key.type = BTRFS_ROOT_ITEM_KEY;
2175 key.offset = (u64)-1;
2176 root = btrfs_read_fs_root_no_name(info, &key);
2177 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002178 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002179 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002180 }
2181 }
2182
2183 key.objectid = sk->min_objectid;
2184 key.type = sk->min_type;
2185 key.offset = sk->min_offset;
2186
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302187 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002188 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002189 if (ret != 0) {
2190 if (ret > 0)
2191 ret = 0;
2192 goto err;
2193 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002194 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002195 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002196 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002197 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002198 break;
2199
2200 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002201 if (ret > 0)
2202 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002203err:
2204 sk->nr_items = num_found;
2205 btrfs_free_path(path);
2206 return ret;
2207}
2208
2209static noinline int btrfs_ioctl_tree_search(struct file *file,
2210 void __user *argp)
2211{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002212 struct btrfs_ioctl_search_args __user *uargs;
2213 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002214 struct inode *inode;
2215 int ret;
2216 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002217
2218 if (!capable(CAP_SYS_ADMIN))
2219 return -EPERM;
2220
Gerhard Heiftba346b32014-01-30 16:24:02 +01002221 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002222
Gerhard Heiftba346b32014-01-30 16:24:02 +01002223 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2224 return -EFAULT;
2225
2226 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002227
Al Viro496ad9a2013-01-23 17:07:38 -05002228 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002229 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002230
2231 /*
2232 * In the origin implementation an overflow is handled by returning a
2233 * search header with a len of zero, so reset ret.
2234 */
2235 if (ret == -EOVERFLOW)
2236 ret = 0;
2237
Gerhard Heiftba346b32014-01-30 16:24:02 +01002238 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002239 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002240 return ret;
2241}
2242
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002243static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2244 void __user *argp)
2245{
2246 struct btrfs_ioctl_search_args_v2 __user *uarg;
2247 struct btrfs_ioctl_search_args_v2 args;
2248 struct inode *inode;
2249 int ret;
2250 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002251 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002252
2253 if (!capable(CAP_SYS_ADMIN))
2254 return -EPERM;
2255
2256 /* copy search header and buffer size */
2257 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2258 if (copy_from_user(&args, uarg, sizeof(args)))
2259 return -EFAULT;
2260
2261 buf_size = args.buf_size;
2262
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002263 /* limit result size to 16MB */
2264 if (buf_size > buf_limit)
2265 buf_size = buf_limit;
2266
2267 inode = file_inode(file);
2268 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002269 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002270 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2271 ret = -EFAULT;
2272 else if (ret == -EOVERFLOW &&
2273 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2274 ret = -EFAULT;
2275
Yan, Zheng76dda932009-09-21 16:00:26 -04002276 return ret;
2277}
2278
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002279/*
Chris Masonac8e9812010-02-28 15:39:26 -05002280 * Search INODE_REFs to identify path name of 'dirid' directory
2281 * in a 'tree_id' tree. and sets path name to 'name'.
2282 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002283static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2284 u64 tree_id, u64 dirid, char *name)
2285{
2286 struct btrfs_root *root;
2287 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002288 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002289 int ret = -1;
2290 int slot;
2291 int len;
2292 int total_len = 0;
2293 struct btrfs_inode_ref *iref;
2294 struct extent_buffer *l;
2295 struct btrfs_path *path;
2296
2297 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2298 name[0]='\0';
2299 return 0;
2300 }
2301
2302 path = btrfs_alloc_path();
2303 if (!path)
2304 return -ENOMEM;
2305
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002306 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002307
2308 key.objectid = tree_id;
2309 key.type = BTRFS_ROOT_ITEM_KEY;
2310 key.offset = (u64)-1;
2311 root = btrfs_read_fs_root_no_name(info, &key);
2312 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002313 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002314 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002315 }
2316
2317 key.objectid = dirid;
2318 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002319 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002320
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302321 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002322 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2323 if (ret < 0)
2324 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002325 else if (ret > 0) {
2326 ret = btrfs_previous_item(root, path, dirid,
2327 BTRFS_INODE_REF_KEY);
2328 if (ret < 0)
2329 goto out;
2330 else if (ret > 0) {
2331 ret = -ENOENT;
2332 goto out;
2333 }
2334 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002335
2336 l = path->nodes[0];
2337 slot = path->slots[0];
2338 btrfs_item_key_to_cpu(l, &key, slot);
2339
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002340 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2341 len = btrfs_inode_ref_name_len(l, iref);
2342 ptr -= len + 1;
2343 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002344 if (ptr < name) {
2345 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002346 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002347 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002348
2349 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302350 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002351
2352 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2353 break;
2354
David Sterbab3b4aa72011-04-21 01:20:15 +02002355 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002356 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002357 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002358 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002359 }
Li Zefan77906a502011-07-14 03:16:00 +00002360 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302361 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002362 ret = 0;
2363out:
2364 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002365 return ret;
2366}
2367
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002368static int btrfs_search_path_in_tree_user(struct inode *inode,
2369 struct btrfs_ioctl_ino_lookup_user_args *args)
2370{
2371 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2372 struct super_block *sb = inode->i_sb;
2373 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2374 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2375 u64 dirid = args->dirid;
2376 unsigned long item_off;
2377 unsigned long item_len;
2378 struct btrfs_inode_ref *iref;
2379 struct btrfs_root_ref *rref;
2380 struct btrfs_root *root;
2381 struct btrfs_path *path;
2382 struct btrfs_key key, key2;
2383 struct extent_buffer *leaf;
2384 struct inode *temp_inode;
2385 char *ptr;
2386 int slot;
2387 int len;
2388 int total_len = 0;
2389 int ret;
2390
2391 path = btrfs_alloc_path();
2392 if (!path)
2393 return -ENOMEM;
2394
2395 /*
2396 * If the bottom subvolume does not exist directly under upper_limit,
2397 * construct the path in from the bottom up.
2398 */
2399 if (dirid != upper_limit.objectid) {
2400 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2401
2402 key.objectid = treeid;
2403 key.type = BTRFS_ROOT_ITEM_KEY;
2404 key.offset = (u64)-1;
2405 root = btrfs_read_fs_root_no_name(fs_info, &key);
2406 if (IS_ERR(root)) {
2407 ret = PTR_ERR(root);
2408 goto out;
2409 }
2410
2411 key.objectid = dirid;
2412 key.type = BTRFS_INODE_REF_KEY;
2413 key.offset = (u64)-1;
2414 while (1) {
2415 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2416 if (ret < 0) {
2417 goto out;
2418 } else if (ret > 0) {
2419 ret = btrfs_previous_item(root, path, dirid,
2420 BTRFS_INODE_REF_KEY);
2421 if (ret < 0) {
2422 goto out;
2423 } else if (ret > 0) {
2424 ret = -ENOENT;
2425 goto out;
2426 }
2427 }
2428
2429 leaf = path->nodes[0];
2430 slot = path->slots[0];
2431 btrfs_item_key_to_cpu(leaf, &key, slot);
2432
2433 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2434 len = btrfs_inode_ref_name_len(leaf, iref);
2435 ptr -= len + 1;
2436 total_len += len + 1;
2437 if (ptr < args->path) {
2438 ret = -ENAMETOOLONG;
2439 goto out;
2440 }
2441
2442 *(ptr + len) = '/';
2443 read_extent_buffer(leaf, ptr,
2444 (unsigned long)(iref + 1), len);
2445
2446 /* Check the read+exec permission of this directory */
2447 ret = btrfs_previous_item(root, path, dirid,
2448 BTRFS_INODE_ITEM_KEY);
2449 if (ret < 0) {
2450 goto out;
2451 } else if (ret > 0) {
2452 ret = -ENOENT;
2453 goto out;
2454 }
2455
2456 leaf = path->nodes[0];
2457 slot = path->slots[0];
2458 btrfs_item_key_to_cpu(leaf, &key2, slot);
2459 if (key2.objectid != dirid) {
2460 ret = -ENOENT;
2461 goto out;
2462 }
2463
David Sterba4c66e0d2019-10-03 19:09:35 +02002464 temp_inode = btrfs_iget(sb, &key2, root);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002465 if (IS_ERR(temp_inode)) {
2466 ret = PTR_ERR(temp_inode);
2467 goto out;
2468 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002469 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2470 iput(temp_inode);
2471 if (ret) {
2472 ret = -EACCES;
2473 goto out;
2474 }
2475
2476 if (key.offset == upper_limit.objectid)
2477 break;
2478 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2479 ret = -EACCES;
2480 goto out;
2481 }
2482
2483 btrfs_release_path(path);
2484 key.objectid = key.offset;
2485 key.offset = (u64)-1;
2486 dirid = key.objectid;
2487 }
2488
2489 memmove(args->path, ptr, total_len);
2490 args->path[total_len] = '\0';
2491 btrfs_release_path(path);
2492 }
2493
2494 /* Get the bottom subvolume's name from ROOT_REF */
2495 root = fs_info->tree_root;
2496 key.objectid = treeid;
2497 key.type = BTRFS_ROOT_REF_KEY;
2498 key.offset = args->treeid;
2499 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2500 if (ret < 0) {
2501 goto out;
2502 } else if (ret > 0) {
2503 ret = -ENOENT;
2504 goto out;
2505 }
2506
2507 leaf = path->nodes[0];
2508 slot = path->slots[0];
2509 btrfs_item_key_to_cpu(leaf, &key, slot);
2510
2511 item_off = btrfs_item_ptr_offset(leaf, slot);
2512 item_len = btrfs_item_size_nr(leaf, slot);
2513 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2514 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2515 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2516 ret = -EINVAL;
2517 goto out;
2518 }
2519
2520 /* Copy subvolume's name */
2521 item_off += sizeof(struct btrfs_root_ref);
2522 item_len -= sizeof(struct btrfs_root_ref);
2523 read_extent_buffer(leaf, args->name, item_off, item_len);
2524 args->name[item_len] = 0;
2525
2526out:
2527 btrfs_free_path(path);
2528 return ret;
2529}
2530
Chris Masonac8e9812010-02-28 15:39:26 -05002531static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2532 void __user *argp)
2533{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002534 struct btrfs_ioctl_ino_lookup_args *args;
2535 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002536 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002537
Julia Lawall2354d08f2010-10-29 15:14:18 -04002538 args = memdup_user(argp, sizeof(*args));
2539 if (IS_ERR(args))
2540 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002541
Al Viro496ad9a2013-01-23 17:07:38 -05002542 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002543
David Sterba01b810b2015-05-12 19:14:49 +02002544 /*
2545 * Unprivileged query to obtain the containing subvolume root id. The
2546 * path is reset so it's consistent with btrfs_search_path_in_tree.
2547 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002548 if (args->treeid == 0)
2549 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2550
David Sterba01b810b2015-05-12 19:14:49 +02002551 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2552 args->name[0] = 0;
2553 goto out;
2554 }
2555
2556 if (!capable(CAP_SYS_ADMIN)) {
2557 ret = -EPERM;
2558 goto out;
2559 }
2560
Chris Masonac8e9812010-02-28 15:39:26 -05002561 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2562 args->treeid, args->objectid,
2563 args->name);
2564
David Sterba01b810b2015-05-12 19:14:49 +02002565out:
Chris Masonac8e9812010-02-28 15:39:26 -05002566 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2567 ret = -EFAULT;
2568
2569 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002570 return ret;
2571}
2572
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002573/*
2574 * Version of ino_lookup ioctl (unprivileged)
2575 *
2576 * The main differences from ino_lookup ioctl are:
2577 *
2578 * 1. Read + Exec permission will be checked using inode_permission() during
2579 * path construction. -EACCES will be returned in case of failure.
2580 * 2. Path construction will be stopped at the inode number which corresponds
2581 * to the fd with which this ioctl is called. If constructed path does not
2582 * exist under fd's inode, -EACCES will be returned.
2583 * 3. The name of bottom subvolume is also searched and filled.
2584 */
2585static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2586{
2587 struct btrfs_ioctl_ino_lookup_user_args *args;
2588 struct inode *inode;
2589 int ret;
2590
2591 args = memdup_user(argp, sizeof(*args));
2592 if (IS_ERR(args))
2593 return PTR_ERR(args);
2594
2595 inode = file_inode(file);
2596
2597 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2598 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2599 /*
2600 * The subvolume does not exist under fd with which this is
2601 * called
2602 */
2603 kfree(args);
2604 return -EACCES;
2605 }
2606
2607 ret = btrfs_search_path_in_tree_user(inode, args);
2608
2609 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2610 ret = -EFAULT;
2611
2612 kfree(args);
2613 return ret;
2614}
2615
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002616/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2617static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2618{
2619 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2620 struct btrfs_fs_info *fs_info;
2621 struct btrfs_root *root;
2622 struct btrfs_path *path;
2623 struct btrfs_key key;
2624 struct btrfs_root_item *root_item;
2625 struct btrfs_root_ref *rref;
2626 struct extent_buffer *leaf;
2627 unsigned long item_off;
2628 unsigned long item_len;
2629 struct inode *inode;
2630 int slot;
2631 int ret = 0;
2632
2633 path = btrfs_alloc_path();
2634 if (!path)
2635 return -ENOMEM;
2636
2637 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2638 if (!subvol_info) {
2639 btrfs_free_path(path);
2640 return -ENOMEM;
2641 }
2642
2643 inode = file_inode(file);
2644 fs_info = BTRFS_I(inode)->root->fs_info;
2645
2646 /* Get root_item of inode's subvolume */
2647 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2648 key.type = BTRFS_ROOT_ITEM_KEY;
2649 key.offset = (u64)-1;
2650 root = btrfs_read_fs_root_no_name(fs_info, &key);
2651 if (IS_ERR(root)) {
2652 ret = PTR_ERR(root);
2653 goto out;
2654 }
2655 root_item = &root->root_item;
2656
2657 subvol_info->treeid = key.objectid;
2658
2659 subvol_info->generation = btrfs_root_generation(root_item);
2660 subvol_info->flags = btrfs_root_flags(root_item);
2661
2662 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2663 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2664 BTRFS_UUID_SIZE);
2665 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2666 BTRFS_UUID_SIZE);
2667
2668 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2669 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2670 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2671
2672 subvol_info->otransid = btrfs_root_otransid(root_item);
2673 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2674 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2675
2676 subvol_info->stransid = btrfs_root_stransid(root_item);
2677 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2678 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2679
2680 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2681 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2682 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2683
2684 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2685 /* Search root tree for ROOT_BACKREF of this subvolume */
2686 root = fs_info->tree_root;
2687
2688 key.type = BTRFS_ROOT_BACKREF_KEY;
2689 key.offset = 0;
2690 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2691 if (ret < 0) {
2692 goto out;
2693 } else if (path->slots[0] >=
2694 btrfs_header_nritems(path->nodes[0])) {
2695 ret = btrfs_next_leaf(root, path);
2696 if (ret < 0) {
2697 goto out;
2698 } else if (ret > 0) {
2699 ret = -EUCLEAN;
2700 goto out;
2701 }
2702 }
2703
2704 leaf = path->nodes[0];
2705 slot = path->slots[0];
2706 btrfs_item_key_to_cpu(leaf, &key, slot);
2707 if (key.objectid == subvol_info->treeid &&
2708 key.type == BTRFS_ROOT_BACKREF_KEY) {
2709 subvol_info->parent_id = key.offset;
2710
2711 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2712 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2713
2714 item_off = btrfs_item_ptr_offset(leaf, slot)
2715 + sizeof(struct btrfs_root_ref);
2716 item_len = btrfs_item_size_nr(leaf, slot)
2717 - sizeof(struct btrfs_root_ref);
2718 read_extent_buffer(leaf, subvol_info->name,
2719 item_off, item_len);
2720 } else {
2721 ret = -ENOENT;
2722 goto out;
2723 }
2724 }
2725
2726 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2727 ret = -EFAULT;
2728
2729out:
2730 btrfs_free_path(path);
2731 kzfree(subvol_info);
2732 return ret;
2733}
2734
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002735/*
2736 * Return ROOT_REF information of the subvolume containing this inode
2737 * except the subvolume name.
2738 */
2739static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2740{
2741 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2742 struct btrfs_root_ref *rref;
2743 struct btrfs_root *root;
2744 struct btrfs_path *path;
2745 struct btrfs_key key;
2746 struct extent_buffer *leaf;
2747 struct inode *inode;
2748 u64 objectid;
2749 int slot;
2750 int ret;
2751 u8 found;
2752
2753 path = btrfs_alloc_path();
2754 if (!path)
2755 return -ENOMEM;
2756
2757 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2758 if (IS_ERR(rootrefs)) {
2759 btrfs_free_path(path);
2760 return PTR_ERR(rootrefs);
2761 }
2762
2763 inode = file_inode(file);
2764 root = BTRFS_I(inode)->root->fs_info->tree_root;
2765 objectid = BTRFS_I(inode)->root->root_key.objectid;
2766
2767 key.objectid = objectid;
2768 key.type = BTRFS_ROOT_REF_KEY;
2769 key.offset = rootrefs->min_treeid;
2770 found = 0;
2771
2772 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2773 if (ret < 0) {
2774 goto out;
2775 } else if (path->slots[0] >=
2776 btrfs_header_nritems(path->nodes[0])) {
2777 ret = btrfs_next_leaf(root, path);
2778 if (ret < 0) {
2779 goto out;
2780 } else if (ret > 0) {
2781 ret = -EUCLEAN;
2782 goto out;
2783 }
2784 }
2785 while (1) {
2786 leaf = path->nodes[0];
2787 slot = path->slots[0];
2788
2789 btrfs_item_key_to_cpu(leaf, &key, slot);
2790 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2791 ret = 0;
2792 goto out;
2793 }
2794
2795 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2796 ret = -EOVERFLOW;
2797 goto out;
2798 }
2799
2800 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2801 rootrefs->rootref[found].treeid = key.offset;
2802 rootrefs->rootref[found].dirid =
2803 btrfs_root_ref_dirid(leaf, rref);
2804 found++;
2805
2806 ret = btrfs_next_item(root, path);
2807 if (ret < 0) {
2808 goto out;
2809 } else if (ret > 0) {
2810 ret = -EUCLEAN;
2811 goto out;
2812 }
2813 }
2814
2815out:
2816 if (!ret || ret == -EOVERFLOW) {
2817 rootrefs->num_items = found;
2818 /* update min_treeid for next search */
2819 if (found)
2820 rootrefs->min_treeid =
2821 rootrefs->rootref[found - 1].treeid + 1;
2822 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2823 ret = -EFAULT;
2824 }
2825
2826 kfree(rootrefs);
2827 btrfs_free_path(path);
2828
2829 return ret;
2830}
2831
Yan, Zheng76dda932009-09-21 16:00:26 -04002832static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2833 void __user *arg)
2834{
Al Viro54563d42013-09-01 15:57:51 -04002835 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002836 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002837 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002838 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002839 struct inode *inode;
2840 struct btrfs_root *root = BTRFS_I(dir)->root;
2841 struct btrfs_root *dest = NULL;
2842 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002843 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002844 int err = 0;
2845
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002846 if (!S_ISDIR(dir->i_mode))
2847 return -ENOTDIR;
2848
Yan, Zheng76dda932009-09-21 16:00:26 -04002849 vol_args = memdup_user(arg, sizeof(*vol_args));
2850 if (IS_ERR(vol_args))
2851 return PTR_ERR(vol_args);
2852
2853 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2854 namelen = strlen(vol_args->name);
2855 if (strchr(vol_args->name, '/') ||
2856 strncmp(vol_args->name, "..", namelen) == 0) {
2857 err = -EINVAL;
2858 goto out;
2859 }
2860
Al Viroa561be72011-11-23 11:57:51 -05002861 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002862 if (err)
2863 goto out;
2864
David Sterba521e0542014-04-15 16:41:44 +02002865
Al Viro00235412016-05-26 00:05:12 -04002866 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2867 if (err == -EINTR)
2868 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002869 dentry = lookup_one_len(vol_args->name, parent, namelen);
2870 if (IS_ERR(dentry)) {
2871 err = PTR_ERR(dentry);
2872 goto out_unlock_dir;
2873 }
2874
David Howells2b0143b2015-03-17 22:25:59 +00002875 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002876 err = -ENOENT;
2877 goto out_dput;
2878 }
2879
David Howells2b0143b2015-03-17 22:25:59 +00002880 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002881 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302882 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002883 /*
2884 * Regular user. Only allow this with a special mount
2885 * option, when the user has write+exec access to the
2886 * subvol root, and when rmdir(2) would have been
2887 * allowed.
2888 *
2889 * Note that this is _not_ check that the subvol is
2890 * empty or doesn't contain data that we wouldn't
2891 * otherwise be able to delete.
2892 *
2893 * Users who want to delete empty subvols should try
2894 * rmdir(2).
2895 */
2896 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002897 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002898 goto out_dput;
2899
2900 /*
2901 * Do not allow deletion if the parent dir is the same
2902 * as the dir to be deleted. That means the ioctl
2903 * must be called on the dentry referencing the root
2904 * of the subvol, not a random directory contained
2905 * within it.
2906 */
2907 err = -EINVAL;
2908 if (root == dest)
2909 goto out_dput;
2910
2911 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2912 if (err)
2913 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002914 }
2915
Miao Xie5c39da52012-10-22 11:39:53 +00002916 /* check if subvolume may be deleted by a user */
2917 err = btrfs_may_delete(dir, dentry, 1);
2918 if (err)
2919 goto out_dput;
2920
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002921 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002922 err = -EINVAL;
2923 goto out_dput;
2924 }
2925
Al Viro59551022016-01-22 15:40:57 -05002926 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002927 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002928 inode_unlock(inode);
Amir Goldstein46008d92019-05-26 17:34:03 +03002929 if (!err) {
2930 fsnotify_rmdir(dir, dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002931 d_delete(dentry);
Amir Goldstein46008d92019-05-26 17:34:03 +03002932 }
Liu Bofa6ac872013-02-20 14:10:23 +00002933
Yan, Zheng76dda932009-09-21 16:00:26 -04002934out_dput:
2935 dput(dentry);
2936out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002937 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002938out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002939 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002940out:
2941 kfree(vol_args);
2942 return err;
2943}
2944
Chris Mason1e701a32010-03-11 09:42:04 -05002945static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002946{
Al Viro496ad9a2013-01-23 17:07:38 -05002947 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002948 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002949 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002950 int ret;
2951
Ilya Dryomov25122d12013-01-20 15:57:57 +02002952 ret = mnt_want_write_file(file);
2953 if (ret)
2954 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002955
Ilya Dryomov25122d12013-01-20 15:57:57 +02002956 if (btrfs_root_readonly(root)) {
2957 ret = -EROFS;
2958 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002959 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002960
2961 switch (inode->i_mode & S_IFMT) {
2962 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002963 if (!capable(CAP_SYS_ADMIN)) {
2964 ret = -EPERM;
2965 goto out;
2966 }
Eric Sandeende78b512013-01-31 18:21:12 +00002967 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002968 break;
2969 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02002970 /*
2971 * Note that this does not check the file descriptor for write
2972 * access. This prevents defragmenting executables that are
2973 * running and allows defrag on files open in read-only mode.
2974 */
2975 if (!capable(CAP_SYS_ADMIN) &&
2976 inode_permission(inode, MAY_WRITE)) {
2977 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05002978 goto out;
2979 }
Chris Mason1e701a32010-03-11 09:42:04 -05002980
2981 range = kzalloc(sizeof(*range), GFP_KERNEL);
2982 if (!range) {
2983 ret = -ENOMEM;
2984 goto out;
2985 }
2986
2987 if (argp) {
2988 if (copy_from_user(range, argp,
2989 sizeof(*range))) {
2990 ret = -EFAULT;
2991 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002992 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002993 }
2994 /* compression requires us to start the IO */
2995 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2996 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2997 range->extent_thresh = (u32)-1;
2998 }
2999 } else {
3000 /* the rest are all set to zero by kzalloc */
3001 range->len = (u64)-1;
3002 }
Al Viro496ad9a2013-01-23 17:07:38 -05003003 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08003004 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003005 if (ret > 0)
3006 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003007 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003008 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003009 default:
3010 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003011 }
Chris Masone441d542009-01-05 16:57:23 -05003012out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003013 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003014 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003015}
3016
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003017static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003018{
3019 struct btrfs_ioctl_vol_args *vol_args;
3020 int ret;
3021
Chris Masone441d542009-01-05 16:57:23 -05003022 if (!capable(CAP_SYS_ADMIN))
3023 return -EPERM;
3024
David Sterba171938e2017-03-28 14:44:21 +02003025 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003026 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003027
Li Zefandae7b662009-04-08 15:06:54 +08003028 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003029 if (IS_ERR(vol_args)) {
3030 ret = PTR_ERR(vol_args);
3031 goto out;
3032 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003033
Mark Fasheh5516e592008-07-24 12:20:14 -04003034 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003035 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003036
Anand Jain43d20762014-07-01 00:58:56 +08003037 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003038 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003039
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003040 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003041out:
David Sterba171938e2017-03-28 14:44:21 +02003042 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003043 return ret;
3044}
3045
Anand Jain6b526ed2016-02-13 10:01:39 +08003046static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003047{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003048 struct inode *inode = file_inode(file);
3049 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003050 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003051 int ret;
3052
Chris Masone441d542009-01-05 16:57:23 -05003053 if (!capable(CAP_SYS_ADMIN))
3054 return -EPERM;
3055
Miao Xieda249272012-11-26 08:44:50 +00003056 ret = mnt_want_write_file(file);
3057 if (ret)
3058 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003059
Li Zefandae7b662009-04-08 15:06:54 +08003060 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003061 if (IS_ERR(vol_args)) {
3062 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003063 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003064 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003065
Anand Jain6b526ed2016-02-13 10:01:39 +08003066 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003067 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3068 ret = -EOPNOTSUPP;
3069 goto out;
3070 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003071
David Sterba171938e2017-03-28 14:44:21 +02003072 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003073 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3074 goto out;
3075 }
3076
David Sterba735654e2016-02-15 18:15:21 +01003077 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003078 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003079 } else {
3080 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003081 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003082 }
David Sterba171938e2017-03-28 14:44:21 +02003083 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003084
Anand Jain6b526ed2016-02-13 10:01:39 +08003085 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003086 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003087 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003088 vol_args->devid);
3089 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003090 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003091 vol_args->name);
3092 }
Anand Jain183860f2013-05-17 10:52:45 +00003093out:
3094 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003095err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003096 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003097 return ret;
3098}
3099
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003100static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3101{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003102 struct inode *inode = file_inode(file);
3103 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003104 struct btrfs_ioctl_vol_args *vol_args;
3105 int ret;
3106
3107 if (!capable(CAP_SYS_ADMIN))
3108 return -EPERM;
3109
3110 ret = mnt_want_write_file(file);
3111 if (ret)
3112 return ret;
3113
David Sterba171938e2017-03-28 14:44:21 +02003114 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003115 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003116 goto out_drop_write;
3117 }
3118
3119 vol_args = memdup_user(arg, sizeof(*vol_args));
3120 if (IS_ERR(vol_args)) {
3121 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003122 goto out;
3123 }
3124
David Sterba58d7bbf2016-05-04 14:10:47 +02003125 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003126 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003127
3128 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003129 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003130 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003131out:
David Sterba171938e2017-03-28 14:44:21 +02003132 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003133out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003134 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003135
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003136 return ret;
3137}
3138
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003139static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3140 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003141{
Li Zefan027ed2f2011-06-08 08:27:56 +00003142 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003143 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003144 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003145 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003146
Li Zefan027ed2f2011-06-08 08:27:56 +00003147 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3148 if (!fi_args)
3149 return -ENOMEM;
3150
David Sterbad03262c2017-06-16 00:09:21 +02003151 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003152 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003153
David Sterbad03262c2017-06-16 00:09:21 +02003154 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003155 if (device->devid > fi_args->max_id)
3156 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003157 }
David Sterbad03262c2017-06-16 00:09:21 +02003158 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003159
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003160 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003161 fi_args->nodesize = fs_info->nodesize;
3162 fi_args->sectorsize = fs_info->sectorsize;
3163 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003164
Li Zefan027ed2f2011-06-08 08:27:56 +00003165 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3166 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003167
Li Zefan027ed2f2011-06-08 08:27:56 +00003168 kfree(fi_args);
3169 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003170}
3171
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003172static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3173 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003174{
3175 struct btrfs_ioctl_dev_info_args *di_args;
3176 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003177 int ret = 0;
3178 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003179
Jan Schmidt475f6382011-03-11 15:41:01 +01003180 di_args = memdup_user(arg, sizeof(*di_args));
3181 if (IS_ERR(di_args))
3182 return PTR_ERR(di_args);
3183
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003184 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003185 s_uuid = di_args->uuid;
3186
David Sterbac5593ca2017-06-16 00:09:21 +02003187 rcu_read_lock();
Anand Jaine4319cd2019-01-17 23:32:31 +08003188 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08003189 NULL, true);
Jan Schmidt475f6382011-03-11 15:41:01 +01003190
3191 if (!dev) {
3192 ret = -ENODEV;
3193 goto out;
3194 }
3195
3196 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003197 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3198 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003199 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003200 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003201 strncpy(di_args->path, rcu_str_deref(dev->name),
3202 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003203 di_args->path[sizeof(di_args->path) - 1] = 0;
3204 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003205 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003206 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003207
3208out:
David Sterbac5593ca2017-06-16 00:09:21 +02003209 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003210 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3211 ret = -EFAULT;
3212
3213 kfree(di_args);
3214 return ret;
3215}
3216
Filipe Mananad8b55242019-01-08 11:43:07 +00003217static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3218 struct inode *inode2, u64 loff2, u64 len)
3219{
3220 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3221 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3222}
3223
3224static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3225 struct inode *inode2, u64 loff2, u64 len)
3226{
3227 if (inode1 < inode2) {
3228 swap(inode1, inode2);
3229 swap(loff1, loff2);
3230 } else if (inode1 == inode2 && loff2 < loff1) {
3231 swap(loff1, loff2);
3232 }
3233 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3234 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3235}
3236
Filipe Manana57a50e22018-12-12 18:05:59 +00003237static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
Filipe Manana34a28e32018-12-07 15:25:38 +00003238 struct inode *dst, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003239{
3240 int ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003241
Filipe Manana34a28e32018-12-07 15:25:38 +00003242 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003243 * Lock destination range to serialize with concurrent readpages() and
3244 * source range to serialize with relocation.
Filipe Manana34a28e32018-12-07 15:25:38 +00003245 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003246 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Filipe Manana57a50e22018-12-12 18:05:59 +00003247 ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
Filipe Mananad8b55242019-01-08 11:43:07 +00003248 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Timofey Titovets39739092018-05-02 08:15:36 +03003249
3250 return ret;
3251}
3252
Timofey Titovetsb6728762018-05-02 08:15:37 +03003253#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3254
Timofey Titovets39739092018-05-02 08:15:36 +03003255static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3256 struct inode *dst, u64 dst_loff)
3257{
3258 int ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003259 u64 i, tail_len, chunk_count;
Filipe Manana62d54f32019-04-22 16:43:42 +01003260 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3261
3262 spin_lock(&root_dst->root_item_lock);
3263 if (root_dst->send_in_progress) {
3264 btrfs_warn_rl(root_dst->fs_info,
3265"cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3266 root_dst->root_key.objectid,
3267 root_dst->send_in_progress);
3268 spin_unlock(&root_dst->root_item_lock);
3269 return -EAGAIN;
3270 }
3271 root_dst->dedupe_in_progress++;
3272 spin_unlock(&root_dst->root_item_lock);
Timofey Titovets39739092018-05-02 08:15:36 +03003273
Timofey Titovetsb6728762018-05-02 08:15:37 +03003274 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3275 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003276
Timofey Titovetsb6728762018-05-02 08:15:37 +03003277 for (i = 0; i < chunk_count; i++) {
3278 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Filipe Manana34a28e32018-12-07 15:25:38 +00003279 dst, dst_loff);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003280 if (ret)
Filipe Manana62d54f32019-04-22 16:43:42 +01003281 goto out;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003282
3283 loff += BTRFS_MAX_DEDUPE_LEN;
3284 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3285 }
3286
3287 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003288 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
Filipe Manana34a28e32018-12-07 15:25:38 +00003289 dst_loff);
Filipe Manana62d54f32019-04-22 16:43:42 +01003290out:
3291 spin_lock(&root_dst->root_item_lock);
3292 root_dst->dedupe_in_progress--;
3293 spin_unlock(&root_dst->root_item_lock);
Mark Fasheh416161d2013-08-06 11:42:51 -07003294
3295 return ret;
3296}
3297
Filipe Mananaf82a9902014-06-01 01:50:28 +01003298static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3299 struct inode *inode,
3300 u64 endoff,
3301 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003302 const u64 olen,
3303 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003304{
3305 struct btrfs_root *root = BTRFS_I(inode)->root;
3306 int ret;
3307
3308 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003309 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003310 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003311 /*
3312 * We round up to the block size at eof when determining which
3313 * extents to clone above, but shouldn't round up the file size.
3314 */
3315 if (endoff > destoff + olen)
3316 endoff = destoff + olen;
3317 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003318 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003319
3320 ret = btrfs_update_inode(trans, root, inode);
3321 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003322 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003323 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003324 goto out;
3325 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003326 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003327out:
3328 return ret;
3329}
3330
Filipe Manana8039d872015-10-13 15:15:00 +01003331/*
3332 * Make sure we do not end up inserting an inline extent into a file that has
3333 * already other (non-inline) extents. If a file has an inline extent it can
3334 * not have any other extents and the (single) inline extent must start at the
3335 * file offset 0. Failing to respect these rules will lead to file corruption,
3336 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3337 *
3338 * We can have extents that have been already written to disk or we can have
3339 * dirty ranges still in delalloc, in which case the extent maps and items are
3340 * created only when we run delalloc, and the delalloc ranges might fall outside
3341 * the range we are currently locking in the inode's io tree. So we check the
3342 * inode's i_size because of that (i_size updates are done while holding the
3343 * i_mutex, which we are holding here).
3344 * We also check to see if the inode has a size not greater than "datal" but has
3345 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3346 * protected against such concurrent fallocate calls by the i_mutex).
3347 *
3348 * If the file has no extents but a size greater than datal, do not allow the
3349 * copy because we would need turn the inline extent into a non-inline one (even
3350 * with NO_HOLES enabled). If we find our destination inode only has one inline
3351 * extent, just overwrite it with the source inline extent if its size is less
3352 * than the source extent's size, or we could copy the source inline extent's
3353 * data into the destination inode's inline extent if the later is greater then
3354 * the former.
3355 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003356static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003357 struct btrfs_trans_handle *trans,
3358 struct btrfs_path *path,
3359 struct btrfs_key *new_key,
3360 const u64 drop_start,
3361 const u64 datal,
3362 const u64 skip,
3363 const u64 size,
3364 char *inline_data)
3365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003366 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003367 struct btrfs_root *root = BTRFS_I(dst)->root;
3368 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003369 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003370 int ret;
3371 struct btrfs_key key;
3372
3373 if (new_key->offset > 0)
3374 return -EOPNOTSUPP;
3375
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003376 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003377 key.type = BTRFS_EXTENT_DATA_KEY;
3378 key.offset = 0;
3379 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3380 if (ret < 0) {
3381 return ret;
3382 } else if (ret > 0) {
3383 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3384 ret = btrfs_next_leaf(root, path);
3385 if (ret < 0)
3386 return ret;
3387 else if (ret > 0)
3388 goto copy_inline_extent;
3389 }
3390 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003391 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003392 key.type == BTRFS_EXTENT_DATA_KEY) {
3393 ASSERT(key.offset > 0);
3394 return -EOPNOTSUPP;
3395 }
3396 } else if (i_size_read(dst) <= datal) {
3397 struct btrfs_file_extent_item *ei;
3398 u64 ext_len;
3399
3400 /*
3401 * If the file size is <= datal, make sure there are no other
3402 * extents following (can happen do to an fallocate call with
3403 * the flag FALLOC_FL_KEEP_SIZE).
3404 */
3405 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3406 struct btrfs_file_extent_item);
3407 /*
3408 * If it's an inline extent, it can not have other extents
3409 * following it.
3410 */
3411 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3412 BTRFS_FILE_EXTENT_INLINE)
3413 goto copy_inline_extent;
3414
3415 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3416 if (ext_len > aligned_end)
3417 return -EOPNOTSUPP;
3418
3419 ret = btrfs_next_item(root, path);
3420 if (ret < 0) {
3421 return ret;
3422 } else if (ret == 0) {
3423 btrfs_item_key_to_cpu(path->nodes[0], &key,
3424 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003425 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003426 key.type == BTRFS_EXTENT_DATA_KEY)
3427 return -EOPNOTSUPP;
3428 }
3429 }
3430
3431copy_inline_extent:
3432 /*
3433 * We have no extent items, or we have an extent at offset 0 which may
3434 * or may not be inlined. All these cases are dealt the same way.
3435 */
3436 if (i_size_read(dst) > datal) {
3437 /*
3438 * If the destination inode has an inline extent...
3439 * This would require copying the data from the source inline
3440 * extent into the beginning of the destination's inline extent.
3441 * But this is really complex, both extents can be compressed
3442 * or just one of them, which would require decompressing and
3443 * re-compressing data (which could increase the new compressed
3444 * size, not allowing the compressed data to fit anymore in an
3445 * inline extent).
3446 * So just don't support this case for now (it should be rare,
3447 * we are not really saving space when cloning inline extents).
3448 */
3449 return -EOPNOTSUPP;
3450 }
3451
3452 btrfs_release_path(path);
3453 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3454 if (ret)
3455 return ret;
3456 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3457 if (ret)
3458 return ret;
3459
3460 if (skip) {
3461 const u32 start = btrfs_file_extent_calc_inline_size(0);
3462
3463 memmove(inline_data + start, inline_data + start + skip, datal);
3464 }
3465
3466 write_extent_buffer(path->nodes[0], inline_data,
3467 btrfs_item_ptr_offset(path->nodes[0],
3468 path->slots[0]),
3469 size);
3470 inode_add_bytes(dst, datal);
Filipe Manana690a5db2019-07-05 11:09:50 +01003471 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags);
Filipe Manana8039d872015-10-13 15:15:00 +01003472
3473 return 0;
3474}
3475
Mark Fasheh32b7c682013-08-06 11:42:49 -07003476/**
3477 * btrfs_clone() - clone a range from inode file to another
3478 *
3479 * @src: Inode to clone from
3480 * @inode: Inode to clone to
3481 * @off: Offset within source to start clone from
3482 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003483 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003484 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003485 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003486 */
3487static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003488 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003489 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003490{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003492 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003493 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003494 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003495 struct btrfs_trans_handle *trans;
3496 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003497 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003498 u32 nritems;
3499 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003500 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003501 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003502 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003503
3504 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003505 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3506 if (!buf)
3507 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003508
3509 path = btrfs_alloc_path();
3510 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003511 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003512 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003513 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003514
David Sterbae4058b52015-11-27 16:31:35 +01003515 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003516 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003517 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003518 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003519 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003520
3521 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003522 u64 next_key_min_offset = key.offset + 1;
Filipe Mananab64119b2019-07-02 15:23:07 +01003523 struct btrfs_file_extent_item *extent;
3524 int type;
3525 u32 size;
3526 struct btrfs_key new_key;
3527 u64 disko = 0, diskl = 0;
3528 u64 datao = 0, datal = 0;
3529 u8 comp;
3530 u64 drop_start;
Filipe Mananadf858e72015-03-31 14:56:46 +01003531
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003532 /*
3533 * note the key will change type as we walk through the
3534 * tree.
3535 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003536 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003537 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3538 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003539 if (ret < 0)
3540 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003541 /*
3542 * First search, if no extent item that starts at offset off was
3543 * found but the previous item is an extent item, it's possible
3544 * it might overlap our target range, therefore process it.
3545 */
3546 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3547 btrfs_item_key_to_cpu(path->nodes[0], &key,
3548 path->slots[0] - 1);
3549 if (key.type == BTRFS_EXTENT_DATA_KEY)
3550 path->slots[0]--;
3551 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003552
Yan Zhengae01a0a2008-08-04 23:23:47 -04003553 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003554process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003555 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003556 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003557 if (ret < 0)
3558 goto out;
3559 if (ret > 0)
3560 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003561 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003562 }
3563 leaf = path->nodes[0];
3564 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003565
Yan Zhengae01a0a2008-08-04 23:23:47 -04003566 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003567 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003568 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003569 break;
3570
Filipe Mananab64119b2019-07-02 15:23:07 +01003571 ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
Zheng Yan31840ae2008-09-23 13:14:14 -04003572
Filipe Mananab64119b2019-07-02 15:23:07 +01003573 extent = btrfs_item_ptr(leaf, slot,
3574 struct btrfs_file_extent_item);
3575 comp = btrfs_file_extent_compression(leaf, extent);
3576 type = btrfs_file_extent_type(leaf, extent);
3577 if (type == BTRFS_FILE_EXTENT_REG ||
3578 type == BTRFS_FILE_EXTENT_PREALLOC) {
3579 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
3580 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
3581 datao = btrfs_file_extent_offset(leaf, extent);
3582 datal = btrfs_file_extent_num_bytes(leaf, extent);
3583 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3584 /* Take upper bound, may be compressed */
3585 datal = btrfs_file_extent_ram_bytes(leaf, extent);
3586 }
3587
3588 /*
3589 * The first search might have left us at an extent item that
3590 * ends before our target range's start, can happen if we have
3591 * holes and NO_HOLES feature enabled.
3592 */
3593 if (key.offset + datal <= off) {
3594 path->slots[0]++;
3595 goto process_slot;
3596 } else if (key.offset >= off + len) {
3597 break;
3598 }
3599 next_key_min_offset = key.offset + datal;
3600 size = btrfs_item_size_nr(leaf, slot);
3601 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot),
3602 size);
3603
3604 btrfs_release_path(path);
3605 path->leave_spinning = 0;
3606
3607 memcpy(&new_key, &key, sizeof(new_key));
3608 new_key.objectid = btrfs_ino(BTRFS_I(inode));
3609 if (off <= key.offset)
3610 new_key.offset = key.offset + destoff - off;
3611 else
3612 new_key.offset = destoff;
3613
3614 /*
3615 * Deal with a hole that doesn't have an extent item that
3616 * represents it (NO_HOLES feature enabled).
3617 * This hole is either in the middle of the cloning range or at
3618 * the beginning (fully overlaps it or partially overlaps it).
3619 */
3620 if (new_key.offset != last_dest_end)
3621 drop_start = last_dest_end;
3622 else
3623 drop_start = new_key.offset;
3624
3625 if (type == BTRFS_FILE_EXTENT_REG ||
3626 type == BTRFS_FILE_EXTENT_PREALLOC) {
3627 struct btrfs_clone_extent_info clone_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003628
Filipe Manana2c463822014-05-31 02:31:05 +01003629 /*
Filipe Mananab64119b2019-07-02 15:23:07 +01003630 * a | --- range to clone ---| b
3631 * | ------------- extent ------------- |
Filipe Manana2c463822014-05-31 02:31:05 +01003632 */
Filipe Mananab64119b2019-07-02 15:23:07 +01003633
3634 /* Subtract range b */
3635 if (key.offset + datal > off + len)
3636 datal = off + len - key.offset;
3637
3638 /* Subtract range a */
3639 if (off > key.offset) {
3640 datao += off - key.offset;
3641 datal -= off - key.offset;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003642 }
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003643
Filipe Mananab64119b2019-07-02 15:23:07 +01003644 clone_info.disk_offset = disko;
3645 clone_info.disk_len = diskl;
3646 clone_info.data_offset = datao;
3647 clone_info.data_len = datal;
3648 clone_info.file_offset = new_key.offset;
3649 clone_info.extent_buf = buf;
3650 clone_info.item_size = size;
3651 ret = btrfs_punch_hole_range(inode, path,
Filipe Manana690a5db2019-07-05 11:09:50 +01003652 drop_start,
3653 new_key.offset + datal - 1,
3654 &clone_info, &trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003655 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003656 goto out;
Filipe Mananab64119b2019-07-02 15:23:07 +01003657 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3658 u64 skip = 0;
3659 u64 trim = 0;
3660
3661 if (off > key.offset) {
3662 skip = off - key.offset;
3663 new_key.offset += skip;
3664 }
3665
3666 if (key.offset + datal > off + len)
3667 trim = key.offset + datal - (off + len);
3668
3669 if (comp && (skip || trim)) {
3670 ret = -EINVAL;
3671 goto out;
3672 }
3673 size -= skip + trim;
3674 datal -= skip + trim;
3675
3676 /*
3677 * If our extent is inline, we know we will drop or
3678 * adjust at most 1 extent item in the destination root.
3679 *
3680 * 1 - adjusting old extent (we may have to split it)
3681 * 1 - add new extent
3682 * 1 - inode update
3683 */
3684 trans = btrfs_start_transaction(root, 3);
3685 if (IS_ERR(trans)) {
3686 ret = PTR_ERR(trans);
3687 goto out;
3688 }
3689
3690 ret = clone_copy_inline_extent(inode, trans, path,
3691 &new_key, drop_start,
3692 datal, skip, size, buf);
3693 if (ret) {
3694 if (ret != -EOPNOTSUPP)
3695 btrfs_abort_transaction(trans, ret);
3696 btrfs_end_transaction(trans);
3697 goto out;
3698 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699 }
Filipe Mananab64119b2019-07-02 15:23:07 +01003700
3701 btrfs_release_path(path);
3702
3703 last_dest_end = ALIGN(new_key.offset + datal,
3704 fs_info->sectorsize);
3705 ret = clone_finish_inode_update(trans, inode, last_dest_end,
3706 destoff, olen, no_time_update);
3707 if (ret)
3708 goto out;
3709 if (new_key.offset + datal >= destoff + len)
3710 break;
3711
David Sterbab3b4aa72011-04-21 01:20:15 +02003712 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003713 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003714
3715 if (fatal_signal_pending(current)) {
3716 ret = -EINTR;
3717 goto out;
3718 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003719 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003720 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003721
Filipe Mananaf82a9902014-06-01 01:50:28 +01003722 if (last_dest_end < destoff + len) {
Filipe Manana690a5db2019-07-05 11:09:50 +01003723 struct btrfs_clone_extent_info clone_info = { 0 };
Filipe Mananaf82a9902014-06-01 01:50:28 +01003724 /*
3725 * We have an implicit hole (NO_HOLES feature is enabled) that
3726 * fully or partially overlaps our cloning range at its end.
3727 */
3728 btrfs_release_path(path);
Filipe Manana690a5db2019-07-05 11:09:50 +01003729 path->leave_spinning = 0;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003730
3731 /*
Filipe Manana690a5db2019-07-05 11:09:50 +01003732 * We are dealing with a hole and our clone_info already has a
3733 * disk_offset of 0, we only need to fill the data length and
3734 * file offset.
Filipe Mananaf82a9902014-06-01 01:50:28 +01003735 */
Filipe Manana690a5db2019-07-05 11:09:50 +01003736 clone_info.data_len = destoff + len - last_dest_end;
3737 clone_info.file_offset = last_dest_end;
3738 ret = btrfs_punch_hole_range(inode, path,
3739 last_dest_end, destoff + len - 1,
3740 &clone_info, &trans);
3741 if (ret)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003742 goto out;
Filipe Manana690a5db2019-07-05 11:09:50 +01003743
Filipe Mananaf82a9902014-06-01 01:50:28 +01003744 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003745 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003746 }
3747
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003748out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003749 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003750 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003751 return ret;
3752}
3753
Zach Brown3db11b22015-11-10 16:53:32 -05003754static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3755 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003756{
Al Viro54563d42013-09-01 15:57:51 -04003757 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003758 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003760 int ret;
3761 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003762 u64 bs = fs_info->sb->s_blocksize;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003763
3764 /*
3765 * TODO:
3766 * - split compressed inline extents. annoying: we need to
3767 * decompress into destination's address_space (the file offset
3768 * may change, so source mapping won't do), then recompress (or
3769 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003770 *
3771 * - split destination inode's inline extents. The inline extents can
3772 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003773 */
3774
Filipe Mananaac765f82018-11-05 11:14:17 +00003775 /*
Filipe Manana34a28e32018-12-07 15:25:38 +00003776 * VFS's generic_remap_file_range_prep() protects us from cloning the
3777 * eof block into the middle of a file, which would result in corruption
3778 * if the file size is not blocksize aligned. So we don't need to check
3779 * for that case here.
Filipe Mananaac765f82018-11-05 11:14:17 +00003780 */
Filipe Manana34a28e32018-12-07 15:25:38 +00003781 if (off + len == src->i_size)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003782 len = ALIGN(src->i_size, bs) - off;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003783
3784 if (destoff > inode->i_size) {
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003785 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3786
Mark Fasheh32b7c682013-08-06 11:42:49 -07003787 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3788 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003789 return ret;
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003790 /*
3791 * We may have truncated the last block if the inode's size is
3792 * not sector size aligned, so we need to wait for writeback to
3793 * complete before proceeding further, otherwise we can race
3794 * with cloning and attempt to increment a reference to an
3795 * extent that no longer exists (writeback completed right after
3796 * we found the previous extent covering eof and before we
3797 * attempted to increment its reference count).
3798 */
3799 ret = btrfs_wait_ordered_range(inode, wb_start,
3800 destoff - wb_start);
3801 if (ret)
3802 return ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003803 }
3804
Filipe Mananac125b8b2014-05-23 05:03:34 +01003805 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003806 * Lock destination range to serialize with concurrent readpages() and
3807 * source range to serialize with relocation.
Filipe Mananac125b8b2014-05-23 05:03:34 +01003808 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003809 btrfs_double_extent_lock(src, off, inode, destoff, len);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003810 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Filipe Mananad8b55242019-01-08 11:43:07 +00003811 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003812 /*
3813 * Truncate page cache pages so that future reads will see the cloned
3814 * data immediately and not the previous data.
3815 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303816 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003817 round_down(destoff, PAGE_SIZE),
3818 round_up(destoff + len, PAGE_SIZE) - 1);
Filipe Manana34a28e32018-12-07 15:25:38 +00003819
3820 return ret;
3821}
3822
3823static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3824 struct file *file_out, loff_t pos_out,
3825 loff_t *len, unsigned int remap_flags)
3826{
3827 struct inode *inode_in = file_inode(file_in);
3828 struct inode *inode_out = file_inode(file_out);
3829 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3830 bool same_inode = inode_out == inode_in;
3831 u64 wb_len;
3832 int ret;
3833
3834 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3835 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3836
3837 if (btrfs_root_readonly(root_out))
3838 return -EROFS;
3839
3840 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3841 inode_in->i_sb != inode_out->i_sb)
3842 return -EXDEV;
3843 }
3844
Filipe Manana500710d2018-12-12 18:05:56 +00003845 /* don't make the dst file partly checksummed */
3846 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3847 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003848 return -EINVAL;
Filipe Manana500710d2018-12-12 18:05:56 +00003849 }
3850
Filipe Manana34a28e32018-12-07 15:25:38 +00003851 /*
3852 * Now that the inodes are locked, we need to start writeback ourselves
3853 * and can not rely on the writeback from the VFS's generic helper
3854 * generic_remap_file_range_prep() because:
3855 *
3856 * 1) For compression we must call filemap_fdatawrite_range() range
3857 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3858 * helper only calls it once;
3859 *
3860 * 2) filemap_fdatawrite_range(), called by the generic helper only
3861 * waits for the writeback to complete, i.e. for IO to be done, and
3862 * not for the ordered extents to complete. We need to wait for them
3863 * to complete so that new file extent items are in the fs tree.
3864 */
3865 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3866 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3867 else
3868 wb_len = ALIGN(*len, bs);
3869
3870 /*
3871 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3872 * any in progress could create its ordered extents after we wait for
3873 * existing ordered extents below).
3874 */
3875 inode_dio_wait(inode_in);
3876 if (!same_inode)
3877 inode_dio_wait(inode_out);
3878
Qu Wenruoa94d1d02019-05-08 18:49:58 +08003879 /*
3880 * Workaround to make sure NOCOW buffered write reach disk as NOCOW.
3881 *
3882 * Btrfs' back references do not have a block level granularity, they
3883 * work at the whole extent level.
3884 * NOCOW buffered write without data space reserved may not be able
3885 * to fall back to CoW due to lack of data space, thus could cause
3886 * data loss.
3887 *
3888 * Here we take a shortcut by flushing the whole inode, so that all
3889 * nocow write should reach disk as nocow before we increase the
3890 * reference of the extent. We could do better by only flushing NOCOW
3891 * data, but that needs extra accounting.
3892 *
3893 * Also we don't need to check ASYNC_EXTENT, as async extent will be
3894 * CoWed anyway, not affecting nocow part.
3895 */
3896 ret = filemap_flush(inode_in->i_mapping);
3897 if (ret < 0)
3898 return ret;
3899
Filipe Manana34a28e32018-12-07 15:25:38 +00003900 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
3901 wb_len);
3902 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003903 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00003904 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
3905 wb_len);
3906 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003907 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00003908
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003909 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
Filipe Manana34a28e32018-12-07 15:25:38 +00003910 len, remap_flags);
Zach Brown3db11b22015-11-10 16:53:32 -05003911}
3912
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003913loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
3914 struct file *dst_file, loff_t destoff, loff_t len,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11003915 unsigned int remap_flags)
Zach Brown3db11b22015-11-10 16:53:32 -05003916{
Filipe Manana34a28e32018-12-07 15:25:38 +00003917 struct inode *src_inode = file_inode(src_file);
3918 struct inode *dst_inode = file_inode(dst_file);
3919 bool same_inode = dst_inode == src_inode;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003920 int ret;
3921
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11003922 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
3923 return -EINVAL;
3924
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003925 if (same_inode)
3926 inode_lock(src_inode);
3927 else
3928 lock_two_nondirectories(src_inode, dst_inode);
3929
Filipe Manana34a28e32018-12-07 15:25:38 +00003930 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
3931 &len, remap_flags);
3932 if (ret < 0 || len == 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003933 goto out_unlock;
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11003934
Filipe Manana34a28e32018-12-07 15:25:38 +00003935 if (remap_flags & REMAP_FILE_DEDUP)
3936 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
3937 else
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003938 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
Filipe Manana34a28e32018-12-07 15:25:38 +00003939
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003940out_unlock:
Filipe Manana34a28e32018-12-07 15:25:38 +00003941 if (same_inode)
3942 inode_unlock(src_inode);
3943 else
Filipe Manana4ea748e2019-02-26 12:06:09 +00003944 unlock_two_nondirectories(src_inode, dst_inode);
Filipe Manana34a28e32018-12-07 15:25:38 +00003945
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11003946 return ret < 0 ? ret : len;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003947}
3948
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003949static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3950{
Al Viro496ad9a2013-01-23 17:07:38 -05003951 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003952 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003953 struct btrfs_root *root = BTRFS_I(inode)->root;
3954 struct btrfs_root *new_root;
3955 struct btrfs_dir_item *di;
3956 struct btrfs_trans_handle *trans;
3957 struct btrfs_path *path;
3958 struct btrfs_key location;
3959 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003960 u64 objectid = 0;
3961 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003962 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003963
3964 if (!capable(CAP_SYS_ADMIN))
3965 return -EPERM;
3966
Miao Xie3c04ce02012-11-26 08:43:07 +00003967 ret = mnt_want_write_file(file);
3968 if (ret)
3969 return ret;
3970
3971 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3972 ret = -EFAULT;
3973 goto out;
3974 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003975
3976 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303977 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003978
3979 location.objectid = objectid;
3980 location.type = BTRFS_ROOT_ITEM_KEY;
3981 location.offset = (u64)-1;
3982
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003983 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003984 if (IS_ERR(new_root)) {
3985 ret = PTR_ERR(new_root);
3986 goto out;
3987 }
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003988 if (!is_fstree(new_root->root_key.objectid)) {
satoru takeuchi6d6d2822017-09-12 22:42:52 +09003989 ret = -ENOENT;
3990 goto out;
3991 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003992
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003993 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003994 if (!path) {
3995 ret = -ENOMEM;
3996 goto out;
3997 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003998 path->leave_spinning = 1;
3999
4000 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004001 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004002 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004003 ret = PTR_ERR(trans);
4004 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004005 }
4006
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004007 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4008 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004009 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004010 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004011 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004012 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004013 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004014 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004015 ret = -ENOENT;
4016 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004017 }
4018
4019 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4020 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4021 btrfs_mark_buffer_dirty(path->nodes[0]);
4022 btrfs_free_path(path);
4023
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004024 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004025 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004026out:
4027 mnt_drop_write_file(file);
4028 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004029}
4030
Su Yuec065f5b12018-04-02 17:24:11 +08004031static void get_block_group_info(struct list_head *groups_list,
4032 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004033{
David Sterba32da53862019-10-29 19:20:18 +01004034 struct btrfs_block_group *block_group;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004035
4036 space->total_bytes = 0;
4037 space->used_bytes = 0;
4038 space->flags = 0;
4039 list_for_each_entry(block_group, groups_list, list) {
4040 space->flags = block_group->flags;
David Sterbab3470b52019-10-23 18:48:22 +02004041 space->total_bytes += block_group->length;
David Sterbabf38be62019-10-23 18:48:11 +02004042 space->used_bytes += block_group->used;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004043 }
4044}
4045
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004046static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4047 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004048{
4049 struct btrfs_ioctl_space_args space_args;
4050 struct btrfs_ioctl_space_info space;
4051 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004052 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004053 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004054 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004055 static const u64 types[] = {
4056 BTRFS_BLOCK_GROUP_DATA,
4057 BTRFS_BLOCK_GROUP_SYSTEM,
4058 BTRFS_BLOCK_GROUP_METADATA,
4059 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4060 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004061 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004062 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004063 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004064 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004065 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004066
4067 if (copy_from_user(&space_args,
4068 (struct btrfs_ioctl_space_args __user *)arg,
4069 sizeof(space_args)))
4070 return -EFAULT;
4071
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004072 for (i = 0; i < num_types; i++) {
4073 struct btrfs_space_info *tmp;
4074
4075 info = NULL;
4076 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004077 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004078 list) {
4079 if (tmp->flags == types[i]) {
4080 info = tmp;
4081 break;
4082 }
4083 }
4084 rcu_read_unlock();
4085
4086 if (!info)
4087 continue;
4088
4089 down_read(&info->groups_sem);
4090 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4091 if (!list_empty(&info->block_groups[c]))
4092 slot_count++;
4093 }
4094 up_read(&info->groups_sem);
4095 }
Josef Bacik1406e432010-01-13 18:19:06 +00004096
David Sterba36523e952014-02-07 14:34:12 +01004097 /*
4098 * Global block reserve, exported as a space_info
4099 */
4100 slot_count++;
4101
Chris Mason7fde62b2010-03-16 15:40:10 -04004102 /* space_slots == 0 means they are asking for a count */
4103 if (space_args.space_slots == 0) {
4104 space_args.total_spaces = slot_count;
4105 goto out;
4106 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004107
Dan Rosenberg51788b12011-02-14 16:04:23 -05004108 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004109
Chris Mason7fde62b2010-03-16 15:40:10 -04004110 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004111
Chris Mason7fde62b2010-03-16 15:40:10 -04004112 /* we generally have at most 6 or so space infos, one for each raid
4113 * level. So, a whole page should be more than enough for everyone
4114 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004115 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004116 return -ENOMEM;
4117
4118 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004119 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004120 if (!dest)
4121 return -ENOMEM;
4122 dest_orig = dest;
4123
4124 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004125 for (i = 0; i < num_types; i++) {
4126 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004127
Dan Rosenberg51788b12011-02-14 16:04:23 -05004128 if (!slot_count)
4129 break;
4130
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004131 info = NULL;
4132 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004133 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004134 list) {
4135 if (tmp->flags == types[i]) {
4136 info = tmp;
4137 break;
4138 }
4139 }
4140 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004141
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004142 if (!info)
4143 continue;
4144 down_read(&info->groups_sem);
4145 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4146 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004147 get_block_group_info(&info->block_groups[c],
4148 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004149 memcpy(dest, &space, sizeof(space));
4150 dest++;
4151 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004152 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004153 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004154 if (!slot_count)
4155 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004156 }
4157 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004158 }
Josef Bacik1406e432010-01-13 18:19:06 +00004159
David Sterba36523e952014-02-07 14:34:12 +01004160 /*
4161 * Add global block reserve
4162 */
4163 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004164 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004165
4166 spin_lock(&block_rsv->lock);
4167 space.total_bytes = block_rsv->size;
4168 space.used_bytes = block_rsv->size - block_rsv->reserved;
4169 spin_unlock(&block_rsv->lock);
4170 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4171 memcpy(dest, &space, sizeof(space));
4172 space_args.total_spaces++;
4173 }
4174
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004175 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004176 (arg + sizeof(struct btrfs_ioctl_space_args));
4177
4178 if (copy_to_user(user_dest, dest_orig, alloc_size))
4179 ret = -EFAULT;
4180
4181 kfree(dest_orig);
4182out:
4183 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004184 ret = -EFAULT;
4185
4186 return ret;
4187}
4188
Miao Xie9a8c28b2012-11-26 08:40:43 +00004189static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4190 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004191{
Sage Weil46204592010-10-29 15:41:32 -04004192 struct btrfs_trans_handle *trans;
4193 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004194 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004195
Miao Xied4edf392013-02-20 09:17:06 +00004196 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004197 if (IS_ERR(trans)) {
4198 if (PTR_ERR(trans) != -ENOENT)
4199 return PTR_ERR(trans);
4200
4201 /* No running transaction, don't bother */
4202 transid = root->fs_info->last_trans_committed;
4203 goto out;
4204 }
Sage Weil46204592010-10-29 15:41:32 -04004205 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004206 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004207 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004208 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004209 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004210 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004211out:
Sage Weil46204592010-10-29 15:41:32 -04004212 if (argp)
4213 if (copy_to_user(argp, &transid, sizeof(transid)))
4214 return -EFAULT;
4215 return 0;
4216}
4217
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004218static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004219 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004220{
Sage Weil46204592010-10-29 15:41:32 -04004221 u64 transid;
4222
4223 if (argp) {
4224 if (copy_from_user(&transid, argp, sizeof(transid)))
4225 return -EFAULT;
4226 } else {
4227 transid = 0; /* current trans */
4228 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004229 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004230}
4231
Miao Xieb8e95482012-11-26 08:48:01 +00004232static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004233{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004234 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004235 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004236 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004237
4238 if (!capable(CAP_SYS_ADMIN))
4239 return -EPERM;
4240
4241 sa = memdup_user(arg, sizeof(*sa));
4242 if (IS_ERR(sa))
4243 return PTR_ERR(sa);
4244
Miao Xieb8e95482012-11-26 08:48:01 +00004245 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4246 ret = mnt_want_write_file(file);
4247 if (ret)
4248 goto out;
4249 }
4250
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004251 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004252 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4253 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004254
Filipe Manana06fe39a2018-12-14 19:50:17 +00004255 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004256 ret = -EFAULT;
4257
Miao Xieb8e95482012-11-26 08:48:01 +00004258 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4259 mnt_drop_write_file(file);
4260out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004261 kfree(sa);
4262 return ret;
4263}
4264
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004265static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004266{
4267 if (!capable(CAP_SYS_ADMIN))
4268 return -EPERM;
4269
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004270 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004271}
4272
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004273static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004274 void __user *arg)
4275{
4276 struct btrfs_ioctl_scrub_args *sa;
4277 int ret;
4278
4279 if (!capable(CAP_SYS_ADMIN))
4280 return -EPERM;
4281
4282 sa = memdup_user(arg, sizeof(*sa));
4283 if (IS_ERR(sa))
4284 return PTR_ERR(sa);
4285
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004286 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004287
Filipe Manana4fa99b02018-12-14 19:45:13 +00004288 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004289 ret = -EFAULT;
4290
4291 kfree(sa);
4292 return ret;
4293}
4294
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004295static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004296 void __user *arg)
Stefan Behrensc11d2c232012-05-25 16:06:09 +02004297{
4298 struct btrfs_ioctl_get_dev_stats *sa;
4299 int ret;
4300
Stefan Behrensc11d2c232012-05-25 16:06:09 +02004301 sa = memdup_user(arg, sizeof(*sa));
4302 if (IS_ERR(sa))
4303 return PTR_ERR(sa);
4304
David Sterbab27f7c02012-06-22 06:30:39 -06004305 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4306 kfree(sa);
4307 return -EPERM;
4308 }
4309
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004310 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c232012-05-25 16:06:09 +02004311
Filipe Mananaeee99572018-12-14 19:45:22 +00004312 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c232012-05-25 16:06:09 +02004313 ret = -EFAULT;
4314
4315 kfree(sa);
4316 return ret;
4317}
4318
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004319static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4320 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004321{
4322 struct btrfs_ioctl_dev_replace_args *p;
4323 int ret;
4324
4325 if (!capable(CAP_SYS_ADMIN))
4326 return -EPERM;
4327
4328 p = memdup_user(arg, sizeof(*p));
4329 if (IS_ERR(p))
4330 return PTR_ERR(p);
4331
4332 switch (p->cmd) {
4333 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004334 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004335 ret = -EROFS;
4336 goto out;
4337 }
David Sterba171938e2017-03-28 14:44:21 +02004338 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004339 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004340 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004341 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004342 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004343 }
4344 break;
4345 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004346 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004347 ret = 0;
4348 break;
4349 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004350 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004351 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004352 break;
4353 default:
4354 ret = -EINVAL;
4355 break;
4356 }
4357
Filipe Mananad3a53282019-01-08 11:42:09 +00004358 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004359 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004360out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004361 kfree(p);
4362 return ret;
4363}
4364
Jan Schmidtd7728c92011-07-07 16:48:38 +02004365static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4366{
4367 int ret = 0;
4368 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004369 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004370 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004371 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004372 struct inode_fs_paths *ipath = NULL;
4373 struct btrfs_path *path;
4374
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004375 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004376 return -EPERM;
4377
4378 path = btrfs_alloc_path();
4379 if (!path) {
4380 ret = -ENOMEM;
4381 goto out;
4382 }
4383
4384 ipa = memdup_user(arg, sizeof(*ipa));
4385 if (IS_ERR(ipa)) {
4386 ret = PTR_ERR(ipa);
4387 ipa = NULL;
4388 goto out;
4389 }
4390
4391 size = min_t(u32, ipa->size, 4096);
4392 ipath = init_ipath(size, root, path);
4393 if (IS_ERR(ipath)) {
4394 ret = PTR_ERR(ipath);
4395 ipath = NULL;
4396 goto out;
4397 }
4398
4399 ret = paths_from_inode(ipa->inum, ipath);
4400 if (ret < 0)
4401 goto out;
4402
4403 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004404 rel_ptr = ipath->fspath->val[i] -
4405 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004406 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004407 }
4408
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004409 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4410 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004411 if (ret) {
4412 ret = -EFAULT;
4413 goto out;
4414 }
4415
4416out:
4417 btrfs_free_path(path);
4418 free_ipath(ipath);
4419 kfree(ipa);
4420
4421 return ret;
4422}
4423
4424static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4425{
4426 struct btrfs_data_container *inodes = ctx;
4427 const size_t c = 3 * sizeof(u64);
4428
4429 if (inodes->bytes_left >= c) {
4430 inodes->bytes_left -= c;
4431 inodes->val[inodes->elem_cnt] = inum;
4432 inodes->val[inodes->elem_cnt + 1] = offset;
4433 inodes->val[inodes->elem_cnt + 2] = root;
4434 inodes->elem_cnt += 3;
4435 } else {
4436 inodes->bytes_missing += c - inodes->bytes_left;
4437 inodes->bytes_left = 0;
4438 inodes->elem_missed += 3;
4439 }
4440
4441 return 0;
4442}
4443
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004444static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004445 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004446{
4447 int ret = 0;
4448 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004449 struct btrfs_ioctl_logical_ino_args *loi;
4450 struct btrfs_data_container *inodes = NULL;
4451 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004452 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004453
4454 if (!capable(CAP_SYS_ADMIN))
4455 return -EPERM;
4456
4457 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304458 if (IS_ERR(loi))
4459 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004460
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004461 if (version == 1) {
4462 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004463 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004464 } else {
4465 /* All reserved bits must be 0 for now */
4466 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4467 ret = -EINVAL;
4468 goto out_loi;
4469 }
4470 /* Only accept flags we have defined so far */
4471 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4472 ret = -EINVAL;
4473 goto out_loi;
4474 }
4475 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004476 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004477 }
4478
Jan Schmidtd7728c92011-07-07 16:48:38 +02004479 path = btrfs_alloc_path();
4480 if (!path) {
4481 ret = -ENOMEM;
4482 goto out;
4483 }
4484
Jan Schmidtd7728c92011-07-07 16:48:38 +02004485 inodes = init_data_container(size);
4486 if (IS_ERR(inodes)) {
4487 ret = PTR_ERR(inodes);
4488 inodes = NULL;
4489 goto out;
4490 }
4491
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004492 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004493 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004494 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004495 ret = -ENOENT;
4496 if (ret < 0)
4497 goto out;
4498
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004499 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4500 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004501 if (ret)
4502 ret = -EFAULT;
4503
4504out:
4505 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004506 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004507out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004508 kfree(loi);
4509
4510 return ret;
4511}
4512
David Sterba008ef092018-03-21 02:05:27 +01004513void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004514 struct btrfs_ioctl_balance_args *bargs)
4515{
4516 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4517
4518 bargs->flags = bctl->flags;
4519
David Sterba3009a622018-03-21 01:31:04 +01004520 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004521 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4522 if (atomic_read(&fs_info->balance_pause_req))
4523 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004524 if (atomic_read(&fs_info->balance_cancel_req))
4525 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004526
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004527 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4528 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4529 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004530
David Sterba008ef092018-03-21 02:05:27 +01004531 spin_lock(&fs_info->balance_lock);
4532 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4533 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004534}
4535
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004536static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004537{
Al Viro496ad9a2013-01-23 17:07:38 -05004538 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004539 struct btrfs_fs_info *fs_info = root->fs_info;
4540 struct btrfs_ioctl_balance_args *bargs;
4541 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004542 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004543 int ret;
4544
4545 if (!capable(CAP_SYS_ADMIN))
4546 return -EPERM;
4547
Liu Boe54bfa312012-06-29 03:58:48 -06004548 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004549 if (ret)
4550 return ret;
4551
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004552again:
David Sterba171938e2017-03-28 14:44:21 +02004553 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004554 mutex_lock(&fs_info->balance_mutex);
4555 need_unlock = true;
4556 goto locked;
4557 }
4558
4559 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004560 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004561 * (1) some other op is running
4562 * (2) balance is running
4563 * (3) balance is paused -- special case (think resume)
4564 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004565 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004566 if (fs_info->balance_ctl) {
4567 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004568 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004569 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004570 /*
4571 * Lock released to allow other waiters to continue,
4572 * we'll reexamine the status again.
4573 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004574 mutex_lock(&fs_info->balance_mutex);
4575
4576 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004577 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004578 /* this is (3) */
4579 need_unlock = false;
4580 goto locked;
4581 }
4582
4583 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004584 goto again;
4585 } else {
4586 /* this is (2) */
4587 mutex_unlock(&fs_info->balance_mutex);
4588 ret = -EINPROGRESS;
4589 goto out;
4590 }
4591 } else {
4592 /* this is (1) */
4593 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004594 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004595 goto out;
4596 }
4597
4598locked:
David Sterba171938e2017-03-28 14:44:21 +02004599 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004600
4601 if (arg) {
4602 bargs = memdup_user(arg, sizeof(*bargs));
4603 if (IS_ERR(bargs)) {
4604 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004605 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004606 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004607
4608 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4609 if (!fs_info->balance_ctl) {
4610 ret = -ENOTCONN;
4611 goto out_bargs;
4612 }
4613
4614 bctl = fs_info->balance_ctl;
4615 spin_lock(&fs_info->balance_lock);
4616 bctl->flags |= BTRFS_BALANCE_RESUME;
4617 spin_unlock(&fs_info->balance_lock);
4618
4619 goto do_balance;
4620 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004621 } else {
4622 bargs = NULL;
4623 }
4624
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004625 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004626 ret = -EINPROGRESS;
4627 goto out_bargs;
4628 }
4629
David Sterba8d2db782015-11-04 15:38:29 +01004630 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004631 if (!bctl) {
4632 ret = -ENOMEM;
4633 goto out_bargs;
4634 }
4635
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004636 if (arg) {
4637 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4638 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4639 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4640
4641 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004642 } else {
4643 /* balance everything - no filters */
4644 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004645 }
4646
David Sterba8eb93452015-10-12 16:55:54 +02004647 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4648 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004649 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004650 }
4651
Ilya Dryomovde322262012-01-16 22:04:49 +02004652do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004653 /*
David Sterba149196a2018-03-20 20:23:09 +01004654 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4655 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4656 * restriper was paused all the way until unmount, in free_fs_info.
4657 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004658 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004659 need_unlock = false;
4660
David Sterba6fcf6e22018-05-07 17:44:03 +02004661 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004662 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004663
Filipe Mananad00c2d92019-01-08 11:42:01 +00004664 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004665 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4666 ret = -EFAULT;
4667 }
4668
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004669out_bctl:
4670 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004671out_bargs:
4672 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004673out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004674 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004675 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004676 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004677out:
Liu Boe54bfa312012-06-29 03:58:48 -06004678 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004679 return ret;
4680}
4681
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004682static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004683{
4684 if (!capable(CAP_SYS_ADMIN))
4685 return -EPERM;
4686
4687 switch (cmd) {
4688 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004689 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004690 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004691 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004692 }
4693
4694 return -EINVAL;
4695}
4696
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004697static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004698 void __user *arg)
4699{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004700 struct btrfs_ioctl_balance_args *bargs;
4701 int ret = 0;
4702
4703 if (!capable(CAP_SYS_ADMIN))
4704 return -EPERM;
4705
4706 mutex_lock(&fs_info->balance_mutex);
4707 if (!fs_info->balance_ctl) {
4708 ret = -ENOTCONN;
4709 goto out;
4710 }
4711
David Sterba8d2db782015-11-04 15:38:29 +01004712 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004713 if (!bargs) {
4714 ret = -ENOMEM;
4715 goto out;
4716 }
4717
David Sterba008ef092018-03-21 02:05:27 +01004718 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004719
4720 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4721 ret = -EFAULT;
4722
4723 kfree(bargs);
4724out:
4725 mutex_unlock(&fs_info->balance_mutex);
4726 return ret;
4727}
4728
Miao Xie905b0dd2012-11-26 08:50:11 +00004729static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004730{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004731 struct inode *inode = file_inode(file);
4732 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004733 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004734 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004735
4736 if (!capable(CAP_SYS_ADMIN))
4737 return -EPERM;
4738
Miao Xie905b0dd2012-11-26 08:50:11 +00004739 ret = mnt_want_write_file(file);
4740 if (ret)
4741 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004742
4743 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004744 if (IS_ERR(sa)) {
4745 ret = PTR_ERR(sa);
4746 goto drop_write;
4747 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004748
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004749 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004750
4751 switch (sa->cmd) {
4752 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004753 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004754 break;
4755 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004756 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004757 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004758 default:
4759 ret = -EINVAL;
4760 break;
4761 }
4762
Arne Jansen5d13a372011-09-14 15:53:51 +02004763 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004764 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004765drop_write:
4766 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004767 return ret;
4768}
4769
Miao Xie905b0dd2012-11-26 08:50:11 +00004770static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004771{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004772 struct inode *inode = file_inode(file);
4773 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4774 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004775 struct btrfs_ioctl_qgroup_assign_args *sa;
4776 struct btrfs_trans_handle *trans;
4777 int ret;
4778 int err;
4779
4780 if (!capable(CAP_SYS_ADMIN))
4781 return -EPERM;
4782
Miao Xie905b0dd2012-11-26 08:50:11 +00004783 ret = mnt_want_write_file(file);
4784 if (ret)
4785 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004786
4787 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004788 if (IS_ERR(sa)) {
4789 ret = PTR_ERR(sa);
4790 goto drop_write;
4791 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004792
4793 trans = btrfs_join_transaction(root);
4794 if (IS_ERR(trans)) {
4795 ret = PTR_ERR(trans);
4796 goto out;
4797 }
4798
Arne Jansen5d13a372011-09-14 15:53:51 +02004799 if (sa->assign) {
Lu Fengqi9f8a6ce62018-07-18 14:45:30 +08004800 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004801 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004802 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004803 }
4804
Qu Wenruoe082f562015-02-27 16:24:28 +08004805 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004806 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004807 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004808 btrfs_handle_fs_error(fs_info, err,
4809 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004810 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004811 if (err && !ret)
4812 ret = err;
4813
4814out:
4815 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004816drop_write:
4817 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004818 return ret;
4819}
4820
Miao Xie905b0dd2012-11-26 08:50:11 +00004821static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004822{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004823 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004824 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004825 struct btrfs_ioctl_qgroup_create_args *sa;
4826 struct btrfs_trans_handle *trans;
4827 int ret;
4828 int err;
4829
4830 if (!capable(CAP_SYS_ADMIN))
4831 return -EPERM;
4832
Miao Xie905b0dd2012-11-26 08:50:11 +00004833 ret = mnt_want_write_file(file);
4834 if (ret)
4835 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004836
4837 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004838 if (IS_ERR(sa)) {
4839 ret = PTR_ERR(sa);
4840 goto drop_write;
4841 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004842
Miao Xied86e56c2012-11-15 11:35:41 +00004843 if (!sa->qgroupid) {
4844 ret = -EINVAL;
4845 goto out;
4846 }
4847
Arne Jansen5d13a372011-09-14 15:53:51 +02004848 trans = btrfs_join_transaction(root);
4849 if (IS_ERR(trans)) {
4850 ret = PTR_ERR(trans);
4851 goto out;
4852 }
4853
Arne Jansen5d13a372011-09-14 15:53:51 +02004854 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004855 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004856 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004857 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004858 }
4859
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004860 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004861 if (err && !ret)
4862 ret = err;
4863
4864out:
4865 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004866drop_write:
4867 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004868 return ret;
4869}
4870
Miao Xie905b0dd2012-11-26 08:50:11 +00004871static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004872{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004873 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004874 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004875 struct btrfs_ioctl_qgroup_limit_args *sa;
4876 struct btrfs_trans_handle *trans;
4877 int ret;
4878 int err;
4879 u64 qgroupid;
4880
4881 if (!capable(CAP_SYS_ADMIN))
4882 return -EPERM;
4883
Miao Xie905b0dd2012-11-26 08:50:11 +00004884 ret = mnt_want_write_file(file);
4885 if (ret)
4886 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004887
4888 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004889 if (IS_ERR(sa)) {
4890 ret = PTR_ERR(sa);
4891 goto drop_write;
4892 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004893
4894 trans = btrfs_join_transaction(root);
4895 if (IS_ERR(trans)) {
4896 ret = PTR_ERR(trans);
4897 goto out;
4898 }
4899
4900 qgroupid = sa->qgroupid;
4901 if (!qgroupid) {
4902 /* take the current subvol as qgroup */
4903 qgroupid = root->root_key.objectid;
4904 }
4905
Lu Fengqif0042d52018-07-18 14:45:35 +08004906 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004907
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004908 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004909 if (err && !ret)
4910 ret = err;
4911
4912out:
4913 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004914drop_write:
4915 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004916 return ret;
4917}
4918
Jan Schmidt2f232032013-04-25 16:04:51 +00004919static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4920{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 struct inode *inode = file_inode(file);
4922 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004923 struct btrfs_ioctl_quota_rescan_args *qsa;
4924 int ret;
4925
4926 if (!capable(CAP_SYS_ADMIN))
4927 return -EPERM;
4928
4929 ret = mnt_want_write_file(file);
4930 if (ret)
4931 return ret;
4932
4933 qsa = memdup_user(arg, sizeof(*qsa));
4934 if (IS_ERR(qsa)) {
4935 ret = PTR_ERR(qsa);
4936 goto drop_write;
4937 }
4938
4939 if (qsa->flags) {
4940 ret = -EINVAL;
4941 goto out;
4942 }
4943
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004944 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004945
4946out:
4947 kfree(qsa);
4948drop_write:
4949 mnt_drop_write_file(file);
4950 return ret;
4951}
4952
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004953static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4954 void __user *arg)
Jan Schmidt2f232032013-04-25 16:04:51 +00004955{
Jan Schmidt2f232032013-04-25 16:04:51 +00004956 struct btrfs_ioctl_quota_rescan_args *qsa;
4957 int ret = 0;
4958
4959 if (!capable(CAP_SYS_ADMIN))
4960 return -EPERM;
4961
David Sterba8d2db782015-11-04 15:38:29 +01004962 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004963 if (!qsa)
4964 return -ENOMEM;
4965
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004966 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00004967 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004969 }
4970
4971 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4972 ret = -EFAULT;
4973
4974 kfree(qsa);
4975 return ret;
4976}
4977
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004978static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4979 void __user *arg)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004980{
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004981 if (!capable(CAP_SYS_ADMIN))
4982 return -EPERM;
4983
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004984 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004985}
4986
Hugo Millsabccd002014-01-30 20:17:00 +00004987static long _btrfs_ioctl_set_received_subvol(struct file *file,
4988 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004989{
Al Viro496ad9a2013-01-23 17:07:38 -05004990 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004991 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004992 struct btrfs_root *root = BTRFS_I(inode)->root;
4993 struct btrfs_root_item *root_item = &root->root_item;
4994 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07004995 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004996 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004997 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004998
David Sterbabd60ea02014-01-16 15:50:22 +01004999 if (!inode_owner_or_capable(inode))
5000 return -EPERM;
5001
Alexander Block8ea05e32012-07-25 17:35:53 +02005002 ret = mnt_want_write_file(file);
5003 if (ret < 0)
5004 return ret;
5005
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005006 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005007
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005008 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005009 ret = -EINVAL;
5010 goto out;
5011 }
5012
5013 if (btrfs_root_readonly(root)) {
5014 ret = -EROFS;
5015 goto out;
5016 }
5017
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005018 /*
5019 * 1 - root item
5020 * 2 - uuid items (received uuid + subvol uuid)
5021 */
5022 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005023 if (IS_ERR(trans)) {
5024 ret = PTR_ERR(trans);
5025 trans = NULL;
5026 goto out;
5027 }
5028
5029 sa->rtransid = trans->transid;
5030 sa->rtime.sec = ct.tv_sec;
5031 sa->rtime.nsec = ct.tv_nsec;
5032
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005033 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5034 BTRFS_UUID_SIZE);
5035 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005036 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005037 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005038 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5039 root->root_key.objectid);
5040 if (ret && ret != -ENOENT) {
5041 btrfs_abort_transaction(trans, ret);
5042 btrfs_end_transaction(trans);
5043 goto out;
5044 }
5045 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005046 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5047 btrfs_set_root_stransid(root_item, sa->stransid);
5048 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005049 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5050 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5051 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5052 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005053
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005054 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005055 &root->root_key, &root->root_item);
5056 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005057 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005058 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005059 }
5060 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005061 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005062 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5063 root->root_key.objectid);
5064 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005065 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005066 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005067 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005068 }
5069 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005070 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005071out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005072 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005073 mnt_drop_write_file(file);
5074 return ret;
5075}
5076
5077#ifdef CONFIG_64BIT
5078static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5079 void __user *arg)
5080{
5081 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5082 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5083 int ret = 0;
5084
5085 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305086 if (IS_ERR(args32))
5087 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005088
David Sterba8d2db782015-11-04 15:38:29 +01005089 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005090 if (!args64) {
5091 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005092 goto out;
5093 }
5094
5095 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5096 args64->stransid = args32->stransid;
5097 args64->rtransid = args32->rtransid;
5098 args64->stime.sec = args32->stime.sec;
5099 args64->stime.nsec = args32->stime.nsec;
5100 args64->rtime.sec = args32->rtime.sec;
5101 args64->rtime.nsec = args32->rtime.nsec;
5102 args64->flags = args32->flags;
5103
5104 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5105 if (ret)
5106 goto out;
5107
5108 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5109 args32->stransid = args64->stransid;
5110 args32->rtransid = args64->rtransid;
5111 args32->stime.sec = args64->stime.sec;
5112 args32->stime.nsec = args64->stime.nsec;
5113 args32->rtime.sec = args64->rtime.sec;
5114 args32->rtime.nsec = args64->rtime.nsec;
5115 args32->flags = args64->flags;
5116
5117 ret = copy_to_user(arg, args32, sizeof(*args32));
5118 if (ret)
5119 ret = -EFAULT;
5120
5121out:
5122 kfree(args32);
5123 kfree(args64);
5124 return ret;
5125}
5126#endif
5127
5128static long btrfs_ioctl_set_received_subvol(struct file *file,
5129 void __user *arg)
5130{
5131 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5132 int ret = 0;
5133
5134 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305135 if (IS_ERR(sa))
5136 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005137
5138 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5139
5140 if (ret)
5141 goto out;
5142
Alexander Block8ea05e32012-07-25 17:35:53 +02005143 ret = copy_to_user(arg, sa, sizeof(*sa));
5144 if (ret)
5145 ret = -EFAULT;
5146
5147out:
5148 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005149 return ret;
5150}
5151
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005152static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
5153 void __user *arg)
jeff.liu867ab662013-01-05 02:48:01 +00005154{
Anand Jaina1b83ac2013-07-19 17:39:32 +08005155 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005156 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005157 char label[BTRFS_LABEL_SIZE];
5158
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005159 spin_lock(&fs_info->super_lock);
5160 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5161 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005162
5163 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005164
5165 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005166 btrfs_warn(fs_info,
5167 "label is too long, return the first %zu bytes",
5168 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005169 }
5170
jeff.liu867ab662013-01-05 02:48:01 +00005171 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005172
5173 return ret ? -EFAULT : 0;
5174}
5175
jeff.liua8bfd4a2013-01-05 02:48:08 +00005176static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5177{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005178 struct inode *inode = file_inode(file);
5179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5180 struct btrfs_root *root = BTRFS_I(inode)->root;
5181 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005182 struct btrfs_trans_handle *trans;
5183 char label[BTRFS_LABEL_SIZE];
5184 int ret;
5185
5186 if (!capable(CAP_SYS_ADMIN))
5187 return -EPERM;
5188
5189 if (copy_from_user(label, arg, sizeof(label)))
5190 return -EFAULT;
5191
5192 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005193 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005194 "unable to set label with more than %d bytes",
5195 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005196 return -EINVAL;
5197 }
5198
5199 ret = mnt_want_write_file(file);
5200 if (ret)
5201 return ret;
5202
jeff.liua8bfd4a2013-01-05 02:48:08 +00005203 trans = btrfs_start_transaction(root, 0);
5204 if (IS_ERR(trans)) {
5205 ret = PTR_ERR(trans);
5206 goto out_unlock;
5207 }
5208
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005209 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005210 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005211 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005212 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005213
5214out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005215 mnt_drop_write_file(file);
5216 return ret;
5217}
5218
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005219#define INIT_FEATURE_FLAGS(suffix) \
5220 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5221 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5222 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5223
David Sterbad5131b62016-02-17 15:26:27 +01005224int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005225{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005226 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005227 INIT_FEATURE_FLAGS(SUPP),
5228 INIT_FEATURE_FLAGS(SAFE_SET),
5229 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5230 };
5231
5232 if (copy_to_user(arg, &features, sizeof(features)))
5233 return -EFAULT;
5234
5235 return 0;
5236}
5237
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005238static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
5239 void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005240{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005241 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005242 struct btrfs_ioctl_feature_flags features;
5243
5244 features.compat_flags = btrfs_super_compat_flags(super_block);
5245 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5246 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5247
5248 if (copy_to_user(arg, &features, sizeof(features)))
5249 return -EFAULT;
5250
5251 return 0;
5252}
5253
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005254static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005255 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005256 u64 change_mask, u64 flags, u64 supported_flags,
5257 u64 safe_set, u64 safe_clear)
5258{
David Sterbaf10152b2019-08-01 19:07:55 +02005259 const char *type = btrfs_feature_set_name(set);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005260 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005261 u64 disallowed, unsupported;
5262 u64 set_mask = flags & change_mask;
5263 u64 clear_mask = ~flags & change_mask;
5264
5265 unsupported = set_mask & ~supported_flags;
5266 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005267 names = btrfs_printable_features(set, unsupported);
5268 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005269 btrfs_warn(fs_info,
5270 "this kernel does not support the %s feature bit%s",
5271 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005272 kfree(names);
5273 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005274 btrfs_warn(fs_info,
5275 "this kernel does not support %s bits 0x%llx",
5276 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005277 return -EOPNOTSUPP;
5278 }
5279
5280 disallowed = set_mask & ~safe_set;
5281 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005282 names = btrfs_printable_features(set, disallowed);
5283 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005284 btrfs_warn(fs_info,
5285 "can't set the %s feature bit%s while mounted",
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 "can't set %s bits 0x%llx while mounted",
5291 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005292 return -EPERM;
5293 }
5294
5295 disallowed = clear_mask & ~safe_clear;
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 clear 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 clear %s bits 0x%llx while mounted",
5306 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005307 return -EPERM;
5308 }
5309
5310 return 0;
5311}
5312
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005313#define check_feature(fs_info, change_mask, flags, mask_base) \
5314check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005315 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5316 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5317 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5318
5319static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5320{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005321 struct inode *inode = file_inode(file);
5322 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5323 struct btrfs_root *root = BTRFS_I(inode)->root;
5324 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005325 struct btrfs_ioctl_feature_flags flags[2];
5326 struct btrfs_trans_handle *trans;
5327 u64 newflags;
5328 int ret;
5329
5330 if (!capable(CAP_SYS_ADMIN))
5331 return -EPERM;
5332
5333 if (copy_from_user(flags, arg, sizeof(flags)))
5334 return -EFAULT;
5335
5336 /* Nothing to do */
5337 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5338 !flags[0].incompat_flags)
5339 return 0;
5340
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005341 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005342 flags[1].compat_flags, COMPAT);
5343 if (ret)
5344 return ret;
5345
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005346 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005347 flags[1].compat_ro_flags, COMPAT_RO);
5348 if (ret)
5349 return ret;
5350
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005351 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005352 flags[1].incompat_flags, INCOMPAT);
5353 if (ret)
5354 return ret;
5355
David Sterba7ab19622016-05-04 11:32:00 +02005356 ret = mnt_want_write_file(file);
5357 if (ret)
5358 return ret;
5359
David Sterba8051aa12014-02-07 14:34:04 +01005360 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005361 if (IS_ERR(trans)) {
5362 ret = PTR_ERR(trans);
5363 goto out_drop_write;
5364 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005365
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005366 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005367 newflags = btrfs_super_compat_flags(super_block);
5368 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5369 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5370 btrfs_set_super_compat_flags(super_block, newflags);
5371
5372 newflags = btrfs_super_compat_ro_flags(super_block);
5373 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5374 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5375 btrfs_set_super_compat_ro_flags(super_block, newflags);
5376
5377 newflags = btrfs_super_incompat_flags(super_block);
5378 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5379 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5380 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005382
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005383 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005384out_drop_write:
5385 mnt_drop_write_file(file);
5386
5387 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005388}
5389
Josef Bacik2351f432017-09-27 10:43:13 -04005390static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5391{
5392 struct btrfs_ioctl_send_args *arg;
5393 int ret;
5394
5395 if (compat) {
5396#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5397 struct btrfs_ioctl_send_args_32 args32;
5398
5399 ret = copy_from_user(&args32, argp, sizeof(args32));
5400 if (ret)
5401 return -EFAULT;
5402 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5403 if (!arg)
5404 return -ENOMEM;
5405 arg->send_fd = args32.send_fd;
5406 arg->clone_sources_count = args32.clone_sources_count;
5407 arg->clone_sources = compat_ptr(args32.clone_sources);
5408 arg->parent_root = args32.parent_root;
5409 arg->flags = args32.flags;
5410 memcpy(arg->reserved, args32.reserved,
5411 sizeof(args32.reserved));
5412#else
5413 return -ENOTTY;
5414#endif
5415 } else {
5416 arg = memdup_user(argp, sizeof(*arg));
5417 if (IS_ERR(arg))
5418 return PTR_ERR(arg);
5419 }
5420 ret = btrfs_ioctl_send(file, arg);
5421 kfree(arg);
5422 return ret;
5423}
5424
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005425long btrfs_ioctl(struct file *file, unsigned int
5426 cmd, unsigned long arg)
5427{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005428 struct inode *inode = file_inode(file);
5429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5430 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005431 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005432
5433 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005434 case FS_IOC_GETFLAGS:
5435 return btrfs_ioctl_getflags(file, argp);
5436 case FS_IOC_SETFLAGS:
5437 return btrfs_ioctl_setflags(file, argp);
5438 case FS_IOC_GETVERSION:
5439 return btrfs_ioctl_getversion(file, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05005440 case FS_IOC_GETFSLABEL:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005441 return btrfs_ioctl_get_fslabel(fs_info, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05005442 case FS_IOC_SETFSLABEL:
5443 return btrfs_ioctl_set_fslabel(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005444 case FITRIM:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005445 return btrfs_ioctl_fitrim(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005446 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005447 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005448 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005449 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005450 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005451 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005452 case BTRFS_IOC_SUBVOL_CREATE_V2:
5453 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005454 case BTRFS_IOC_SNAP_DESTROY:
5455 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005456 case BTRFS_IOC_SUBVOL_GETFLAGS:
5457 return btrfs_ioctl_subvol_getflags(file, argp);
5458 case BTRFS_IOC_SUBVOL_SETFLAGS:
5459 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005460 case BTRFS_IOC_DEFAULT_SUBVOL:
5461 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005462 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005463 return btrfs_ioctl_defrag(file, NULL);
5464 case BTRFS_IOC_DEFRAG_RANGE:
5465 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005466 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005467 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005468 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005469 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005470 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005471 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005472 case BTRFS_IOC_RM_DEV_V2:
5473 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005474 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005475 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005476 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005477 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005478 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005479 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005480 case BTRFS_IOC_TREE_SEARCH:
5481 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005482 case BTRFS_IOC_TREE_SEARCH_V2:
5483 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005484 case BTRFS_IOC_INO_LOOKUP:
5485 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005486 case BTRFS_IOC_INO_PATHS:
5487 return btrfs_ioctl_ino_to_path(root, argp);
5488 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005489 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5490 case BTRFS_IOC_LOGICAL_INO_V2:
5491 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005492 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005493 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005494 case BTRFS_IOC_SYNC: {
5495 int ret;
5496
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005497 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005498 if (ret)
5499 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005500 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005501 /*
5502 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005503 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005504 * processing uncleaned subvols.
5505 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005506 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005507 return ret;
5508 }
Sage Weil46204592010-10-29 15:41:32 -04005509 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005510 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005511 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005512 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005513 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005514 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005515 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005516 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005517 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005518 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005519 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005520 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005521 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005522 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005523 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005524 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005525 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5526 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005527#ifdef CONFIG_64BIT
5528 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5529 return btrfs_ioctl_set_received_subvol_32(file, argp);
5530#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005531 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005532 return _btrfs_ioctl_send(file, argp, false);
5533#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5534 case BTRFS_IOC_SEND_32:
5535 return _btrfs_ioctl_send(file, argp, true);
5536#endif
Stefan Behrensc11d2c232012-05-25 16:06:09 +02005537 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005538 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005539 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005540 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005541 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005542 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005543 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005544 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005545 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005546 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005547 case BTRFS_IOC_QUOTA_RESCAN:
5548 return btrfs_ioctl_quota_rescan(file, argp);
5549 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005550 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005551 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005552 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005553 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005554 return btrfs_ioctl_dev_replace(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005555 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005556 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005557 case BTRFS_IOC_GET_FEATURES:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005558 return btrfs_ioctl_get_features(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005559 case BTRFS_IOC_SET_FEATURES:
5560 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005561 case FS_IOC_FSGETXATTR:
5562 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005563 case FS_IOC_FSSETXATTR:
5564 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005565 case BTRFS_IOC_GET_SUBVOL_INFO:
5566 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005567 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5568 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005569 case BTRFS_IOC_INO_LOOKUP_USER:
5570 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005571 }
5572
5573 return -ENOTTY;
5574}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005575
5576#ifdef CONFIG_COMPAT
5577long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5578{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005579 /*
5580 * These all access 32-bit values anyway so no further
5581 * handling is necessary.
5582 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005583 switch (cmd) {
5584 case FS_IOC32_GETFLAGS:
5585 cmd = FS_IOC_GETFLAGS;
5586 break;
5587 case FS_IOC32_SETFLAGS:
5588 cmd = FS_IOC_SETFLAGS;
5589 break;
5590 case FS_IOC32_GETVERSION:
5591 cmd = FS_IOC_GETVERSION;
5592 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005593 }
5594
5595 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5596}
5597#endif