blob: ab34179d7cbc8b2d9b1d3fde8591fe01f79d4d3c [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"
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -030031#include "export.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include "transaction.h"
33#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include "print-tree.h"
35#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040036#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080037#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020038#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040039#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020040#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010041#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000042#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040043#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070044#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000045#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080046#include "compression.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040047#include "space-info.h"
Josef Bacik86736342019-06-19 15:12:00 -040048#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040049#include "block-group.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040050
Hugo Millsabccd002014-01-30 20:17:00 +000051#ifdef CONFIG_64BIT
52/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
53 * structures are incorrect, as the timespec structure from userspace
54 * is 4 bytes too small. We define these alternatives here to teach
55 * the kernel about the 32-bit struct packing.
56 */
57struct btrfs_ioctl_timespec_32 {
58 __u64 sec;
59 __u32 nsec;
60} __attribute__ ((__packed__));
61
62struct btrfs_ioctl_received_subvol_args_32 {
63 char uuid[BTRFS_UUID_SIZE]; /* in */
64 __u64 stransid; /* in */
65 __u64 rtransid; /* out */
66 struct btrfs_ioctl_timespec_32 stime; /* in */
67 struct btrfs_ioctl_timespec_32 rtime; /* out */
68 __u64 flags; /* in */
69 __u64 reserved[16]; /* in */
70} __attribute__ ((__packed__));
71
72#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
73 struct btrfs_ioctl_received_subvol_args_32)
74#endif
75
Josef Bacik2351f432017-09-27 10:43:13 -040076#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
77struct btrfs_ioctl_send_args_32 {
78 __s64 send_fd; /* in */
79 __u64 clone_sources_count; /* in */
80 compat_uptr_t clone_sources; /* in */
81 __u64 parent_root; /* in */
82 __u64 flags; /* in */
83 __u64 reserved[4]; /* in */
84} __attribute__ ((__packed__));
85
86#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
87 struct btrfs_ioctl_send_args_32)
88#endif
Hugo Millsabccd002014-01-30 20:17:00 +000089
Christoph Hellwig6cbff002009-04-17 10:37:41 +020090/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020091static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
92 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093{
David Sterba1905a0f2018-03-26 18:52:15 +020094 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020096 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097 return flags & ~FS_DIRSYNC_FL;
98 else
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
100}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400101
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
104 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200106static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107{
108 unsigned int iflags = 0;
109
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
124
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900125 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000126 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129
130 return iflags;
131}
132
133/*
134 * Update inode->i_flags based on the btrfs internal flags.
135 */
David Sterba7b6a2212018-03-26 18:40:21 +0200136void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200137{
David Sterba5c57b8b2018-04-23 15:45:18 +0200138 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100139 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140
David Sterba5c57b8b2018-04-23 15:45:18 +0200141 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100142 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200145 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200147 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200149 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_DIRSYNC;
151
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
154 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200155}
156
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
David Sterba5c57b8b2018-04-23 15:45:18 +0200159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
David Sterba5ba76ab2018-03-26 18:52:15 +0200167/* Check if @flags are a supported and valid set of FS_*_FL flags */
168static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000169{
170 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
171 FS_NOATIME_FL | FS_NODUMP_FL | \
172 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000173 FS_NOCOMP_FL | FS_COMPR_FL |
174 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 return -EOPNOTSUPP;
176
177 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
178 return -EINVAL;
179
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return 0;
181}
182
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200183static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
184{
Al Viro496ad9a2013-01-23 17:07:38 -0500185 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400186 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200187 struct btrfs_inode *binode = BTRFS_I(inode);
188 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200189 struct btrfs_trans_handle *trans;
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700190 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191 int ret;
Anand Jainff9fef552019-04-20 19:48:53 +0800192 const char *comp = NULL;
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800193 u32 binode_flags = binode->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194
David Sterbabd60ea02014-01-16 15:50:22 +0100195 if (!inode_owner_or_capable(inode))
196 return -EPERM;
197
Li Zefanb83cc962010-12-20 16:04:08 +0800198 if (btrfs_root_readonly(root))
199 return -EROFS;
200
David Sterba5c57b8b2018-04-23 15:45:18 +0200201 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200202 return -EFAULT;
203
David Sterba5c57b8b2018-04-23 15:45:18 +0200204 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000205 if (ret)
206 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200207
Jan Karae7848682012-06-12 16:20:32 +0200208 ret = mnt_want_write_file(file);
209 if (ret)
210 return ret;
211
Al Viro59551022016-01-22 15:40:57 -0500212 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200213
David Sterba5c57b8b2018-04-23 15:45:18 +0200214 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700215 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
216 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
217 if (ret)
218 goto out_unlock;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200219
David Sterba5c57b8b2018-04-23 15:45:18 +0200220 if (fsflags & FS_SYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800221 binode_flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800223 binode_flags &= ~BTRFS_INODE_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200224 if (fsflags & FS_IMMUTABLE_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800225 binode_flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800227 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200228 if (fsflags & FS_APPEND_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800229 binode_flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200230 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800231 binode_flags &= ~BTRFS_INODE_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200232 if (fsflags & FS_NODUMP_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800233 binode_flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200234 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800235 binode_flags &= ~BTRFS_INODE_NODUMP;
David Sterba5c57b8b2018-04-23 15:45:18 +0200236 if (fsflags & FS_NOATIME_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800237 binode_flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800239 binode_flags &= ~BTRFS_INODE_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200240 if (fsflags & FS_DIRSYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800241 binode_flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200242 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800243 binode_flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200244 if (fsflags & FS_NOCOW_FL) {
Anand Jain44e51942019-04-20 19:48:57 +0800245 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600246 /*
247 * It's safe to turn csums off here, no extents exist.
248 * Otherwise we want the flag to reflect the real COW
249 * status of the file and will not set it.
250 */
251 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800252 binode_flags |= BTRFS_INODE_NODATACOW |
253 BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600254 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800255 binode_flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600256 }
257 } else {
258 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400259 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600260 */
Anand Jain44e51942019-04-20 19:48:57 +0800261 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600262 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800263 binode_flags &= ~(BTRFS_INODE_NODATACOW |
264 BTRFS_INODE_NODATASUM);
David Sterba7e97b8d2012-09-07 05:56:55 -0600265 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800266 binode_flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600267 }
268 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200269
Liu Bo75e7cb72011-03-22 10:12:20 +0000270 /*
271 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
272 * flag may be changed automatically if compression code won't make
273 * things smaller.
274 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200275 if (fsflags & FS_NOCOMP_FL) {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800276 binode_flags &= ~BTRFS_INODE_COMPRESS;
277 binode_flags |= BTRFS_INODE_NOCOMPRESS;
David Sterba5c57b8b2018-04-23 15:45:18 +0200278 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000279
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700280 if (IS_SWAPFILE(inode)) {
281 ret = -ETXTBSY;
282 goto out_unlock;
283 }
284
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800285 binode_flags |= BTRFS_INODE_COMPRESS;
286 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000287
David Sterba93370502017-10-31 17:32:41 +0100288 comp = btrfs_compress_type2str(fs_info->compress_type);
289 if (!comp || comp[0] == 0)
290 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
Li Zefanebcb9042011-04-15 03:03:17 +0000291 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800292 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000293 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200294
Anand Jainff9fef552019-04-20 19:48:53 +0800295 /*
296 * 1 for inode item
297 * 2 for properties
298 */
299 trans = btrfs_start_transaction(root, 3);
Li Zefanf062abf02011-12-29 13:36:45 +0800300 if (IS_ERR(trans)) {
301 ret = PTR_ERR(trans);
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800302 goto out_unlock;
Li Zefanf062abf02011-12-29 13:36:45 +0800303 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200304
Anand Jainff9fef552019-04-20 19:48:53 +0800305 if (comp) {
306 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
307 strlen(comp), 0);
308 if (ret) {
309 btrfs_abort_transaction(trans, ret);
310 goto out_end_trans;
311 }
Anand Jainff9fef552019-04-20 19:48:53 +0800312 } else {
313 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
314 0, 0);
315 if (ret && ret != -ENODATA) {
316 btrfs_abort_transaction(trans, ret);
317 goto out_end_trans;
318 }
319 }
320
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800321 binode->flags = binode_flags;
David Sterba7b6a2212018-03-26 18:40:21 +0200322 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400323 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700324 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200325 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200326
Anand Jainff9fef552019-04-20 19:48:53 +0800327 out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400328 btrfs_end_transaction(trans);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200329 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500330 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200331 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000332 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200333}
334
David Sterba19f93b32018-03-26 19:42:05 +0200335/*
336 * Translate btrfs internal inode flags to xflags as expected by the
337 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
338 * silently dropped.
339 */
340static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
341{
342 unsigned int xflags = 0;
343
344 if (flags & BTRFS_INODE_APPEND)
345 xflags |= FS_XFLAG_APPEND;
346 if (flags & BTRFS_INODE_IMMUTABLE)
347 xflags |= FS_XFLAG_IMMUTABLE;
348 if (flags & BTRFS_INODE_NOATIME)
349 xflags |= FS_XFLAG_NOATIME;
350 if (flags & BTRFS_INODE_NODUMP)
351 xflags |= FS_XFLAG_NODUMP;
352 if (flags & BTRFS_INODE_SYNC)
353 xflags |= FS_XFLAG_SYNC;
354
355 return xflags;
356}
357
358/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
359static int check_xflags(unsigned int flags)
360{
361 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
362 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
363 return -EOPNOTSUPP;
364 return 0;
365}
366
David Sterbae4202ac2018-03-26 19:51:16 +0200367/*
368 * Set the xflags from the internal inode flags. The remaining items of fsxattr
369 * are zeroed.
370 */
371static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
372{
373 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
374 struct fsxattr fa;
375
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700376 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
David Sterbae4202ac2018-03-26 19:51:16 +0200377 if (copy_to_user(arg, &fa, sizeof(fa)))
378 return -EFAULT;
379
380 return 0;
381}
382
David Sterba025f2122018-03-26 19:51:16 +0200383static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
384{
385 struct inode *inode = file_inode(file);
386 struct btrfs_inode *binode = BTRFS_I(inode);
387 struct btrfs_root *root = binode->root;
388 struct btrfs_trans_handle *trans;
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700389 struct fsxattr fa, old_fa;
David Sterba025f2122018-03-26 19:51:16 +0200390 unsigned old_flags;
391 unsigned old_i_flags;
392 int ret = 0;
393
394 if (!inode_owner_or_capable(inode))
395 return -EPERM;
396
397 if (btrfs_root_readonly(root))
398 return -EROFS;
399
David Sterba025f2122018-03-26 19:51:16 +0200400 if (copy_from_user(&fa, arg, sizeof(fa)))
401 return -EFAULT;
402
403 ret = check_xflags(fa.fsx_xflags);
404 if (ret)
405 return ret;
406
407 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
408 return -EOPNOTSUPP;
409
410 ret = mnt_want_write_file(file);
411 if (ret)
412 return ret;
413
414 inode_lock(inode);
415
416 old_flags = binode->flags;
417 old_i_flags = inode->i_flags;
418
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700419 simple_fill_fsxattr(&old_fa,
420 btrfs_inode_flags_to_xflags(binode->flags));
421 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
422 if (ret)
David Sterba025f2122018-03-26 19:51:16 +0200423 goto out_unlock;
David Sterba025f2122018-03-26 19:51:16 +0200424
425 if (fa.fsx_xflags & FS_XFLAG_SYNC)
426 binode->flags |= BTRFS_INODE_SYNC;
427 else
428 binode->flags &= ~BTRFS_INODE_SYNC;
429 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
430 binode->flags |= BTRFS_INODE_IMMUTABLE;
431 else
432 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
433 if (fa.fsx_xflags & FS_XFLAG_APPEND)
434 binode->flags |= BTRFS_INODE_APPEND;
435 else
436 binode->flags &= ~BTRFS_INODE_APPEND;
437 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
438 binode->flags |= BTRFS_INODE_NODUMP;
439 else
440 binode->flags &= ~BTRFS_INODE_NODUMP;
441 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
442 binode->flags |= BTRFS_INODE_NOATIME;
443 else
444 binode->flags &= ~BTRFS_INODE_NOATIME;
445
446 /* 1 item for the inode */
447 trans = btrfs_start_transaction(root, 1);
448 if (IS_ERR(trans)) {
449 ret = PTR_ERR(trans);
450 goto out_unlock;
451 }
452
453 btrfs_sync_inode_flags_to_i_flags(inode);
454 inode_inc_iversion(inode);
455 inode->i_ctime = current_time(inode);
456 ret = btrfs_update_inode(trans, root, inode);
457
458 btrfs_end_transaction(trans);
459
460out_unlock:
461 if (ret) {
462 binode->flags = old_flags;
463 inode->i_flags = old_i_flags;
464 }
465
466 inode_unlock(inode);
467 mnt_drop_write_file(file);
468
469 return ret;
470}
471
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200472static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
473{
Al Viro496ad9a2013-01-23 17:07:38 -0500474 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200475
476 return put_user(inode->i_generation, arg);
477}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400478
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -0300479static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
480 void __user *arg)
Li Dongyangf7039b12011-03-24 10:24:28 +0000481{
Li Dongyangf7039b12011-03-24 10:24:28 +0000482 struct btrfs_device *device;
483 struct request_queue *q;
484 struct fstrim_range range;
485 u64 minlen = ULLONG_MAX;
486 u64 num_devices = 0;
487 int ret;
488
489 if (!capable(CAP_SYS_ADMIN))
490 return -EPERM;
491
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000492 /*
493 * If the fs is mounted with nologreplay, which requires it to be
494 * mounted in RO mode as well, we can not allow discard on free space
495 * inside block groups, because log trees refer to extents that are not
496 * pinned in a block group's free space cache (pinning the extents is
497 * precisely the first phase of replaying a log tree).
498 */
499 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
500 return -EROFS;
501
Xiao Guangrong1f781602011-04-20 10:09:16 +0000502 rcu_read_lock();
503 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
504 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000505 if (!device->bdev)
506 continue;
507 q = bdev_get_queue(device->bdev);
508 if (blk_queue_discard(q)) {
509 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100510 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000511 minlen);
512 }
513 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000514 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200515
Li Dongyangf7039b12011-03-24 10:24:28 +0000516 if (!num_devices)
517 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000518 if (copy_from_user(&range, arg, sizeof(range)))
519 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800520
521 /*
522 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
523 * block group is in the logical address space, which can be any
524 * sectorsize aligned bytenr in the range [0, U64_MAX].
525 */
526 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200527 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000528
529 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400530 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000531 if (ret < 0)
532 return ret;
533
534 if (copy_to_user(arg, &range, sizeof(range)))
535 return -EFAULT;
536
537 return 0;
538}
539
David Sterbae1f60a62019-10-01 19:57:39 +0200540int __pure btrfs_is_empty_uuid(u8 *uuid)
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200541{
Chris Mason46e0f662013-11-15 12:14:55 +0100542 int i;
543
544 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
545 if (uuid[i])
546 return 0;
547 }
548 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200549}
550
Miao Xied5c12072013-02-28 10:04:33 +0000551static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400552 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100553 const char *name, int namelen,
Miao Xie8696c532013-02-07 06:02:44 +0000554 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400556 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557 struct btrfs_trans_handle *trans;
558 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100559 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 struct btrfs_inode_item *inode_item;
561 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000562 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400563 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000564 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700565 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900566 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567 int ret;
568 int err;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800569 dev_t anon_dev = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570 u64 objectid;
571 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500572 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400573
David Sterba49a3c4d2016-03-24 17:49:22 +0100574 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
575 if (!root_item)
576 return -ENOMEM;
577
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400578 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400579 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100580 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000581
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800582 ret = get_anon_bdev(&anon_dev);
583 if (ret < 0)
584 goto fail_free;
585
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));
Andy Shevchenko807fc792020-02-24 17:37:51 +0200646 generate_random_guid(root_item->uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100647 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
648 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
649 root_item->ctime = root_item->otime;
650 btrfs_set_root_ctransid(root_item, trans->transid);
651 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400652
Chris Mason925baed2008-06-25 16:01:30 -0400653 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654 free_extent_buffer(leaf);
655 leaf = NULL;
656
David Sterba49a3c4d2016-03-24 17:49:22 +0100657 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400658
659 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400660 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200661 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400662 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100663 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664 if (ret)
665 goto fail;
666
Yan, Zheng76dda932009-09-21 16:00:26 -0400667 key.offset = (u64)-1;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800668 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100669 if (IS_ERR(new_root)) {
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800670 free_anon_bdev(anon_dev);
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 }
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800675 /* Freeing will be done in btrfs_put_root() of new_root */
676 anon_dev = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -0400677
678 btrfs_record_root_in_trans(trans, new_root);
679
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000680 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Josef Bacik00246522020-01-24 09:33:01 -0500681 btrfs_put_root(new_root);
Mark Fashehce598972011-07-26 11:32:23 -0700682 if (ret) {
683 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400684 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700685 goto fail;
686 }
687
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530688 mutex_lock(&new_root->objectid_mutex);
689 new_root->highest_objectid = new_dirid;
690 mutex_unlock(&new_root->objectid_mutex);
691
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400692 /*
693 * insert the directory item
694 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200695 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100696 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400697 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100698 goto fail;
699 }
Chris Mason3de45862008-11-17 21:02:50 -0500700
Lu Fengqi684572d2018-08-04 21:10:57 +0800701 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500702 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100703 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400704 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400705 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100706 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500707
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200708 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500709 ret = btrfs_update_inode(trans, root, dir);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500710 if (ret) {
711 btrfs_abort_transaction(trans, ret);
712 goto fail;
713 }
Yan Zheng52c26172009-01-05 15:43:43 -0500714
Lu Fengqi6025c192018-08-01 11:32:29 +0800715 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200716 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500717 if (ret) {
718 btrfs_abort_transaction(trans, ret);
719 goto fail;
720 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500721
Lu Fengqicdb345a2018-05-29 15:01:53 +0800722 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400723 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200724 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400725 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200726
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400727fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100728 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000729 trans->block_rsv = NULL;
730 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100731 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800732
Nikolay Borisov9babda92020-03-13 17:23:20 +0200733 err = btrfs_commit_transaction(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400734 if (err && !ret)
735 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500736
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900737 if (!ret) {
738 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800739 if (IS_ERR(inode))
740 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900741 d_instantiate(dentry, inode);
742 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400743 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100744
745fail_free:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800746 if (anon_dev)
747 free_anon_bdev(anon_dev);
David Sterba49a3c4d2016-03-24 17:49:22 +0100748 kfree(root_item);
749 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400750}
751
Miao Xiee9662f72013-02-28 10:01:15 +0000752static int create_snapshot(struct btrfs_root *root, struct inode *dir,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200753 struct dentry *dentry, bool readonly,
Miao Xiee9662f72013-02-28 10:01:15 +0000754 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400755{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400756 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000757 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400758 struct btrfs_pending_snapshot *pending_snapshot;
759 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000760 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400761
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800762 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763 return -EINVAL;
764
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700765 if (atomic_read(&root->nr_swapfiles)) {
766 btrfs_warn(fs_info,
767 "cannot snapshot subvolume with active swapfile");
768 return -ETXTBSY;
769 }
770
David Sterba23269bf2017-02-13 11:03:44 +0100771 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100772 if (!pending_snapshot)
773 return -ENOMEM;
774
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800775 ret = get_anon_bdev(&pending_snapshot->anon_dev);
776 if (ret < 0)
777 goto free_pending;
David Sterbab0c0ea62015-11-10 18:54:00 +0100778 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100779 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100780 pending_snapshot->path = btrfs_alloc_path();
781 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100782 ret = -ENOMEM;
783 goto free_pending;
784 }
785
Miao Xie66d8f3d2012-09-06 04:02:28 -0600786 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
787 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000788 /*
789 * 1 - parent dir inode
790 * 2 - dir entries
791 * 1 - root item
792 * 2 - root ref/backref
793 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200794 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000795 */
796 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200797 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400798 false);
Miao Xied5c12072013-02-28 10:04:33 +0000799 if (ret)
Robbie Koc11fbb62020-05-14 17:19:18 +0800800 goto free_pending;
Miao Xied5c12072013-02-28 10:04:33 +0000801
Yan, Zhenga22285a2010-05-16 10:48:46 -0400802 pending_snapshot->dentry = dentry;
803 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800804 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000805 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000806 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400807
Miao Xied5c12072013-02-28 10:04:33 +0000808 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400809 if (IS_ERR(trans)) {
810 ret = PTR_ERR(trans);
811 goto fail;
812 }
813
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400814 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400815 list_add(&pending_snapshot->list,
816 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400817 spin_unlock(&fs_info->trans_lock);
Nikolay Borisov9babda92020-03-13 17:23:20 +0200818
819 ret = btrfs_commit_transaction(trans);
Miao Xieaec80302013-03-04 09:44:29 +0000820 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400821 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400822
823 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400824 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000825 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400826
Chris Masond3797302014-10-15 13:50:56 -0700827 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
828 if (ret)
829 goto fail;
830
David Howells2b0143b2015-03-17 22:25:59 +0000831 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000832 if (IS_ERR(inode)) {
833 ret = PTR_ERR(inode);
834 goto fail;
835 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900836
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000837 d_instantiate(dentry, inode);
838 ret = 0;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800839 pending_snapshot->anon_dev = 0;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000840fail:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800841 /* Prevent double freeing of anon_dev */
842 if (ret && pending_snapshot->snap)
843 pending_snapshot->snap->anon_dev = 0;
Josef Bacik00246522020-01-24 09:33:01 -0500844 btrfs_put_root(pending_snapshot->snap);
David Sterba7775c812017-02-10 19:18:18 +0100845 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbab0c0ea62015-11-10 18:54:00 +0100846free_pending:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800847 if (pending_snapshot->anon_dev)
848 free_anon_bdev(pending_snapshot->anon_dev);
David Sterbab0c0ea62015-11-10 18:54:00 +0100849 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100850 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100851 kfree(pending_snapshot);
852
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400853 return ret;
854}
855
Sage Weil4260f7c2010-10-29 15:46:43 -0400856/* copy of may_delete in fs/namei.c()
857 * Check whether we can remove a link victim from directory dir, check
858 * whether the type of victim is right.
859 * 1. We can't do it if dir is read-only (done in permission())
860 * 2. We should have write and exec permissions on dir
861 * 3. We can't remove anything from append-only dir
862 * 4. We can't do anything with immutable dir (done in permission())
863 * 5. If the sticky bit on dir is set we should either
864 * a. be owner of dir, or
865 * b. be owner of victim, or
866 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400867 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400868 * links pointing to it.
869 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
870 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
871 * 9. We can't remove a root or mountpoint.
872 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
873 * nfs_async_unlink().
874 */
875
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530876static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400877{
878 int error;
879
David Howells2b0143b2015-03-17 22:25:59 +0000880 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400881 return -ENOENT;
882
David Howells2b0143b2015-03-17 22:25:59 +0000883 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400884 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400885
886 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
887 if (error)
888 return error;
889 if (IS_APPEND(dir))
890 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000891 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
892 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400893 return -EPERM;
894 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000895 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400896 return -ENOTDIR;
897 if (IS_ROOT(victim))
898 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000899 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400900 return -EISDIR;
901 if (IS_DEADDIR(dir))
902 return -ENOENT;
903 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
904 return -EBUSY;
905 return 0;
906}
907
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400908/* copy of may_create in fs/namei.c() */
909static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
910{
David Howells2b0143b2015-03-17 22:25:59 +0000911 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400912 return -EEXIST;
913 if (IS_DEADDIR(dir))
914 return -ENOENT;
915 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
916}
917
918/*
919 * Create a new subvolume below @parent. This is largely modeled after
920 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
921 * inside this filesystem so it's quite a bit simpler.
922 */
Al Viro92872092016-11-20 19:34:31 -0500923static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100924 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400925 struct btrfs_root *snap_src,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200926 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000927 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400929 struct inode *dir = d_inode(parent->dentry);
930 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400931 struct dentry *dentry;
932 int error;
933
Al Viro00235412016-05-26 00:05:12 -0400934 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
935 if (error == -EINTR)
936 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400937
938 dentry = lookup_one_len(name, parent->dentry, namelen);
939 error = PTR_ERR(dentry);
940 if (IS_ERR(dentry))
941 goto out_unlock;
942
Yan, Zheng76dda932009-09-21 16:00:26 -0400943 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400944 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600945 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400946
Chris Mason9c520572012-12-17 14:26:57 -0500947 /*
948 * even if this name doesn't exist, we may get hash collisions.
949 * check for them now when we can safely fail
950 */
951 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
952 dir->i_ino, name,
953 namelen);
954 if (error)
955 goto out_dput;
956
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400957 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400958
959 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
960 goto out_up_read;
961
Nikolay Borisov9babda92020-03-13 17:23:20 +0200962 if (snap_src)
963 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
964 else
965 error = create_subvol(dir, dentry, name, namelen, inherit);
966
Yan, Zheng76dda932009-09-21 16:00:26 -0400967 if (!error)
968 fsnotify_mkdir(dir, dentry);
969out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400970 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400971out_dput:
972 dput(dentry);
973out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500974 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400975 return error;
976}
977
Robbie Koc11fbb62020-05-14 17:19:18 +0800978static noinline int btrfs_mksnapshot(const struct path *parent,
979 const char *name, int namelen,
980 struct btrfs_root *root,
981 bool readonly,
982 struct btrfs_qgroup_inherit *inherit)
983{
984 int ret;
985 bool snapshot_force_cow = false;
986
987 /*
988 * Force new buffered writes to reserve space even when NOCOW is
989 * possible. This is to avoid later writeback (running dealloc) to
990 * fallback to COW mode and unexpectedly fail with ENOSPC.
991 */
992 btrfs_drew_read_lock(&root->snapshot_lock);
993
994 ret = btrfs_start_delalloc_snapshot(root);
995 if (ret)
996 goto out;
997
998 /*
999 * All previous writes have started writeback in NOCOW mode, so now
1000 * we force future writes to fallback to COW mode during snapshot
1001 * creation.
1002 */
1003 atomic_inc(&root->snapshot_force_cow);
1004 snapshot_force_cow = true;
1005
1006 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
1007
1008 ret = btrfs_mksubvol(parent, name, namelen,
1009 root, readonly, inherit);
1010out:
1011 if (snapshot_force_cow)
1012 atomic_dec(&root->snapshot_force_cow);
1013 btrfs_drew_read_unlock(&root->snapshot_lock);
1014 return ret;
1015}
1016
Chris Mason4cb53002011-05-24 15:35:30 -04001017/*
1018 * When we're defragging a range, we don't want to kick it off again
1019 * if it is really just waiting for delalloc to send it down.
1020 * If we find a nice big extent or delalloc range for the bytes in the
1021 * file you want to defrag, we return 0 to let you know to skip this
1022 * part of the file
1023 */
David Sterbaaab110a2014-07-29 17:32:10 +02001024static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001025{
1026 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1027 struct extent_map *em = NULL;
1028 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1029 u64 end;
1030
1031 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001032 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001033 read_unlock(&em_tree->lock);
1034
1035 if (em) {
1036 end = extent_map_end(em);
1037 free_extent_map(em);
1038 if (end - offset > thresh)
1039 return 0;
1040 }
1041 /* if we already have a nice delalloc here, just stop */
1042 thresh /= 2;
1043 end = count_range_bits(io_tree, &offset, offset + thresh,
1044 thresh, EXTENT_DELALLOC, 1);
1045 if (end >= thresh)
1046 return 0;
1047 return 1;
1048}
1049
1050/*
1051 * helper function to walk through a file and find extents
1052 * newer than a specific transid, and smaller than thresh.
1053 *
1054 * This is used by the defragging code to find new and small
1055 * extents
1056 */
1057static int find_new_extents(struct btrfs_root *root,
1058 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001059 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001060{
1061 struct btrfs_path *path;
1062 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001063 struct extent_buffer *leaf;
1064 struct btrfs_file_extent_item *extent;
1065 int type;
1066 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001067 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001068
1069 path = btrfs_alloc_path();
1070 if (!path)
1071 return -ENOMEM;
1072
David Sterbaa4689d22011-05-31 17:08:14 +00001073 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001074 min_key.type = BTRFS_EXTENT_DATA_KEY;
1075 min_key.offset = *off;
1076
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301077 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001078 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001079 if (ret != 0)
1080 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001081process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001082 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001083 goto none;
1084 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1085 goto none;
1086
1087 leaf = path->nodes[0];
1088 extent = btrfs_item_ptr(leaf, path->slots[0],
1089 struct btrfs_file_extent_item);
1090
1091 type = btrfs_file_extent_type(leaf, extent);
1092 if (type == BTRFS_FILE_EXTENT_REG &&
1093 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1094 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1095 *off = min_key.offset;
1096 btrfs_free_path(path);
1097 return 0;
1098 }
1099
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001100 path->slots[0]++;
1101 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1102 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1103 goto process_slot;
1104 }
1105
Chris Mason4cb53002011-05-24 15:35:30 -04001106 if (min_key.offset == (u64)-1)
1107 goto none;
1108
1109 min_key.offset++;
1110 btrfs_release_path(path);
1111 }
1112none:
1113 btrfs_free_path(path);
1114 return -ENOENT;
1115}
1116
Li Zefan6c282eb2012-06-11 16:03:35 +08001117static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001118{
1119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001120 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001121 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001122 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001123
1124 /*
1125 * hopefully we have this extent in the tree already, try without
1126 * the full extent lock
1127 */
1128 read_lock(&em_tree->lock);
1129 em = lookup_extent_mapping(em_tree, start, len);
1130 read_unlock(&em_tree->lock);
1131
1132 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001133 struct extent_state *cached = NULL;
1134 u64 end = start + len - 1;
1135
Chris Mason940100a2010-03-10 10:52:59 -05001136 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001137 lock_extent_bits(io_tree, start, end, &cached);
Omar Sandoval39b07b52019-12-02 17:34:23 -08001138 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
David Sterbae43bbe52017-12-12 21:43:52 +01001139 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001140
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001141 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001142 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001143 }
1144
Li Zefan6c282eb2012-06-11 16:03:35 +08001145 return em;
1146}
1147
1148static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1149{
1150 struct extent_map *next;
1151 bool ret = true;
1152
1153 /* this is the last extent */
1154 if (em->start + em->len >= i_size_read(inode))
1155 return false;
1156
1157 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001158 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1159 ret = false;
1160 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001161 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001162 ret = false;
1163
1164 free_extent_map(next);
1165 return ret;
1166}
1167
David Sterbaaab110a2014-07-29 17:32:10 +02001168static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001169 u64 *last_len, u64 *skip, u64 *defrag_end,
1170 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001171{
1172 struct extent_map *em;
1173 int ret = 1;
1174 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001175 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001176
1177 /*
1178 * make sure that once we start defragging an extent, we keep on
1179 * defragging it
1180 */
1181 if (start < *defrag_end)
1182 return 1;
1183
1184 *skip = 0;
1185
1186 em = defrag_lookup_extent(inode, start);
1187 if (!em)
1188 return 0;
1189
Chris Mason940100a2010-03-10 10:52:59 -05001190 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001191 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001192 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001193 goto out;
1194 }
1195
Liu Bo4a3560c2015-08-07 16:48:41 +08001196 if (!*defrag_end)
1197 prev_mergeable = false;
1198
Li Zefan6c282eb2012-06-11 16:03:35 +08001199 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001200 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001201 * we hit a real extent, if it is big or the next extent is not a
1202 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001203 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001204 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001205 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001206 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001207out:
Chris Mason940100a2010-03-10 10:52:59 -05001208 /*
1209 * last_len ends up being a counter of how many bytes we've defragged.
1210 * every time we choose not to defrag an extent, we reset *last_len
1211 * so that the next tiny extent will force a defrag.
1212 *
1213 * The end result of this is that tiny extents before a single big
1214 * extent will force at least part of that big extent to be defragged.
1215 */
1216 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001217 *defrag_end = extent_map_end(em);
1218 } else {
1219 *last_len = 0;
1220 *skip = extent_map_end(em);
1221 *defrag_end = 0;
1222 }
1223
1224 free_extent_map(em);
1225 return ret;
1226}
1227
Chris Mason4cb53002011-05-24 15:35:30 -04001228/*
1229 * it doesn't do much good to defrag one or two pages
1230 * at a time. This pulls in a nice chunk of pages
1231 * to COW and defrag.
1232 *
1233 * It also makes sure the delalloc code has enough
1234 * dirty data to avoid making new small extents as part
1235 * of the defrag
1236 *
1237 * It's a good idea to start RA on this range
1238 * before calling this.
1239 */
1240static int cluster_pages_for_defrag(struct inode *inode,
1241 struct page **pages,
1242 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001243 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001244{
Chris Mason4cb53002011-05-24 15:35:30 -04001245 unsigned long file_end;
1246 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001247 u64 page_start;
1248 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001249 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001250 int ret;
1251 int i;
1252 int i_done;
1253 struct btrfs_ordered_extent *ordered;
1254 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001255 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001256 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001257 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001258
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001259 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001260 if (!isize || start_index > file_end)
1261 return 0;
1262
1263 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001264
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03001265 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001266 start_index << PAGE_SHIFT,
1267 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001268 if (ret)
1269 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001270 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001271 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001272
1273 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001274 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001275 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001276again:
Josef Bacika94733d2011-07-11 10:47:06 -04001277 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001278 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001279 if (!page)
1280 break;
1281
Miao Xie600a45e2012-02-16 15:01:24 +08001282 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001283 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001284 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001285 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001286 &cached_state);
Nikolay Borisovc3504372020-06-03 08:55:03 +03001287 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode),
Miao Xie600a45e2012-02-16 15:01:24 +08001288 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001289 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001290 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001291 if (!ordered)
1292 break;
1293
1294 unlock_page(page);
1295 btrfs_start_ordered_extent(inode, ordered, 1);
1296 btrfs_put_ordered_extent(ordered);
1297 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001298 /*
1299 * we unlocked the page above, so we need check if
1300 * it was released or not.
1301 */
1302 if (page->mapping != inode->i_mapping) {
1303 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001304 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001305 goto again;
1306 }
Miao Xie600a45e2012-02-16 15:01:24 +08001307 }
1308
Chris Mason4cb53002011-05-24 15:35:30 -04001309 if (!PageUptodate(page)) {
1310 btrfs_readpage(NULL, page);
1311 lock_page(page);
1312 if (!PageUptodate(page)) {
1313 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001314 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001315 ret = -EIO;
1316 break;
1317 }
1318 }
Miao Xie600a45e2012-02-16 15:01:24 +08001319
Miao Xie600a45e2012-02-16 15:01:24 +08001320 if (page->mapping != inode->i_mapping) {
1321 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001322 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001323 goto again;
1324 }
1325
Chris Mason4cb53002011-05-24 15:35:30 -04001326 pages[i] = page;
1327 i_done++;
1328 }
1329 if (!i_done || ret)
1330 goto out;
1331
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001332 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001333 goto out;
1334
1335 /*
1336 * so now we have a nice long stream of locked
1337 * and up to date pages, lets wait on them
1338 */
1339 for (i = 0; i < i_done; i++)
1340 wait_on_page_writeback(pages[i]);
1341
1342 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001343 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001344
1345 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001346 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001347 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
Omar Sandovale1821632019-08-15 14:04:04 -07001348 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1349 EXTENT_DEFRAG, 0, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001350
Liu Bo1f12bd02012-03-29 09:57:44 -04001351 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001352 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001353 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001354 spin_unlock(&BTRFS_I(inode)->lock);
Nikolay Borisov86d52922020-06-03 08:55:40 +03001355 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001356 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001357 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001358 }
1359
1360
Liu Bo9e8a4a82012-09-05 19:10:51 -06001361 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001362 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001363
1364 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001365 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001366
1367 for (i = 0; i < i_done; i++) {
1368 clear_page_dirty_for_io(pages[i]);
1369 ClearPageChecked(pages[i]);
1370 set_page_extent_mapped(pages[i]);
1371 set_page_dirty(pages[i]);
1372 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001373 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001374 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08001375 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001376 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001377 return i_done;
1378out:
1379 for (i = 0; i < i_done; i++) {
1380 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001381 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001382 }
Nikolay Borisov86d52922020-06-03 08:55:40 +03001383 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001384 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001385 page_cnt << PAGE_SHIFT, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08001386 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001387 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001388 return ret;
1389
1390}
1391
1392int btrfs_defrag_file(struct inode *inode, struct file *file,
1393 struct btrfs_ioctl_defrag_range_args *range,
1394 u64 newer_than, unsigned long max_to_defrag)
1395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001396 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001397 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001398 struct file_ra_state *ra = NULL;
1399 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001400 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001401 u64 last_len = 0;
1402 u64 skip = 0;
1403 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001404 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001405 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001406 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001407 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001408 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001409 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001410 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001411 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001412 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001413 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001414 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001415 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001416
Liu Bo0abd5b12013-04-16 09:20:28 +00001417 if (isize == 0)
1418 return 0;
1419
1420 if (range->start >= isize)
1421 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001422
David Sterba1e2ef462017-07-17 20:01:59 +02001423 if (do_compress) {
Chengguang Xuce96b7f2019-10-10 15:59:57 +08001424 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Li Zefan1a419d82010-10-25 15:12:50 +08001425 return -EINVAL;
1426 if (range->compress_type)
1427 compress_type = range->compress_type;
1428 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001429
Liu Bo0abd5b12013-04-16 09:20:28 +00001430 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001431 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001432
Chris Mason4cb53002011-05-24 15:35:30 -04001433 /*
David Sterba0a52d102017-06-22 03:22:58 +02001434 * If we were not given a file, allocate a readahead context. As
1435 * readahead is just an optimization, defrag will work without it so
1436 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001437 */
1438 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001439 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001440 if (ra)
1441 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001442 } else {
1443 ra = &file->f_ra;
1444 }
1445
David Sterba63e727e2017-06-22 03:13:02 +02001446 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001447 if (!pages) {
1448 ret = -ENOMEM;
1449 goto out_ra;
1450 }
1451
1452 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001453 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001454 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001455 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001456 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001457 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001458 }
1459
Chris Mason4cb53002011-05-24 15:35:30 -04001460 if (newer_than) {
1461 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001462 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001463 if (!ret) {
1464 range->start = newer_off;
1465 /*
1466 * we always align our defrag to help keep
1467 * the extents in the file evenly spaced
1468 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001469 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001470 } else
1471 goto out_ra;
1472 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001473 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001474 }
1475 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301476 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001477
Li Zefan2a0f7f52011-10-10 15:43:34 -04001478 /*
1479 * make writeback starts from i, so the defrag range can be
1480 * written sequentially.
1481 */
1482 if (i < inode->i_mapping->writeback_index)
1483 inode->i_mapping->writeback_index = i;
1484
Chris Masonf7f43cc2011-10-11 11:41:40 -04001485 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001486 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001487 /*
1488 * make sure we stop running if someone unmounts
1489 * the FS
1490 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001491 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001492 break;
1493
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001494 if (btrfs_defrag_cancelled(fs_info)) {
1495 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001496 ret = -EAGAIN;
1497 break;
1498 }
1499
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001500 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001501 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001502 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001503 unsigned long next;
1504 /*
1505 * the should_defrag function tells us how much to skip
1506 * bump our counter by the suggested amount
1507 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001508 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001509 i = max(i + 1, next);
1510 continue;
1511 }
Li Zefan008873e2011-09-02 15:57:07 +08001512
1513 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001514 cluster = (PAGE_ALIGN(defrag_end) >>
1515 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001516 cluster = min(cluster, max_cluster);
1517 } else {
1518 cluster = max_cluster;
1519 }
1520
Li Zefan008873e2011-09-02 15:57:07 +08001521 if (i + cluster > ra_index) {
1522 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001523 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001524 page_cache_sync_readahead(inode->i_mapping, ra,
1525 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301526 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001527 }
Chris Mason940100a2010-03-10 10:52:59 -05001528
Al Viro59551022016-01-22 15:40:57 -05001529 inode_lock(inode);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001530 if (IS_SWAPFILE(inode)) {
1531 ret = -ETXTBSY;
1532 } else {
1533 if (do_compress)
1534 BTRFS_I(inode)->defrag_compress = compress_type;
1535 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1536 }
Liu Boecb8bea2012-03-29 09:57:44 -04001537 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001538 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001539 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001540 }
Chris Mason940100a2010-03-10 10:52:59 -05001541
Chris Mason4cb53002011-05-24 15:35:30 -04001542 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001543 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001544 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001545
1546 if (newer_than) {
1547 if (newer_off == (u64)-1)
1548 break;
1549
Liu Boe1f041e2012-03-29 09:57:45 -04001550 if (ret > 0)
1551 i += ret;
1552
Chris Mason4cb53002011-05-24 15:35:30 -04001553 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001554 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001555
Byongho Leeee221842015-12-15 01:42:10 +09001556 ret = find_new_extents(root, inode, newer_than,
1557 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001558 if (!ret) {
1559 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001560 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001561 } else {
1562 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001563 }
Chris Mason4cb53002011-05-24 15:35:30 -04001564 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001565 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001566 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001567 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001568 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001569 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001570 last_len = 0;
1571 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573 }
1574
Filipe Mananadec8ef92014-03-01 10:55:54 +00001575 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001576 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001577 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1578 &BTRFS_I(inode)->runtime_flags))
1579 filemap_flush(inode->i_mapping);
1580 }
Chris Mason1e701a32010-03-11 09:42:04 -05001581
Li Zefan1a419d82010-10-25 15:12:50 +08001582 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001583 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001584 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1585 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001586 }
1587
Diego Calleja60ccf822011-09-01 16:33:57 +02001588 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001589
Chris Mason4cb53002011-05-24 15:35:30 -04001590out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001591 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001592 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001593 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001594 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001595 }
Chris Mason4cb53002011-05-24 15:35:30 -04001596 if (!file)
1597 kfree(ra);
1598 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001599 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001600}
1601
Miao Xie198605a2012-11-26 08:43:45 +00001602static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001603 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001604{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001605 struct inode *inode = file_inode(file);
1606 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001607 u64 new_size;
1608 u64 old_size;
1609 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001610 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001611 struct btrfs_ioctl_vol_args *vol_args;
1612 struct btrfs_trans_handle *trans;
1613 struct btrfs_device *device = NULL;
1614 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001615 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616 char *devstr = NULL;
1617 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001618 int mod = 0;
1619
Chris Masone441d542009-01-05 16:57:23 -05001620 if (!capable(CAP_SYS_ADMIN))
1621 return -EPERM;
1622
Miao Xie198605a2012-11-26 08:43:45 +00001623 ret = mnt_want_write_file(file);
1624 if (ret)
1625 return ret;
1626
David Sterba171938e2017-03-28 14:44:21 +02001627 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001628 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001629 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001630 }
1631
Li Zefandae7b662009-04-08 15:06:54 +08001632 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001633 if (IS_ERR(vol_args)) {
1634 ret = PTR_ERR(vol_args);
1635 goto out;
1636 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001637
1638 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001639
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001640 sizestr = vol_args->name;
1641 devstr = strchr(sizestr, ':');
1642 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001643 sizestr = devstr + 1;
1644 *devstr = '\0';
1645 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001646 ret = kstrtoull(devstr, 10, &devid);
1647 if (ret)
1648 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001649 if (!devid) {
1650 ret = -EINVAL;
1651 goto out_free;
1652 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001653 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001654 }
Miao Xiedba60f32012-12-21 09:19:51 +00001655
Anand Jain09ba3bc2019-01-19 14:48:55 +08001656 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001657 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001658 btrfs_info(fs_info, "resizer unable to find device %llu",
1659 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001660 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001661 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001662 }
Miao Xiedba60f32012-12-21 09:19:51 +00001663
Anand Jainebbede42017-12-04 12:54:52 +08001664 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001665 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001666 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001667 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001668 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001669 goto out_free;
1670 }
1671
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001672 if (!strcmp(sizestr, "max"))
1673 new_size = device->bdev->bd_inode->i_size;
1674 else {
1675 if (sizestr[0] == '-') {
1676 mod = -1;
1677 sizestr++;
1678 } else if (sizestr[0] == '+') {
1679 mod = 1;
1680 sizestr++;
1681 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001682 new_size = memparse(sizestr, &retptr);
1683 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001684 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001685 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001686 }
1687 }
1688
Anand Jain401e29c2017-12-04 12:54:55 +08001689 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001690 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001691 goto out_free;
1692 }
1693
Miao Xie7cc8e582014-09-03 21:35:38 +08001694 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001695
1696 if (mod < 0) {
1697 if (new_size > old_size) {
1698 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001699 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001700 }
1701 new_size = old_size - new_size;
1702 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001703 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001704 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001705 goto out_free;
1706 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001707 new_size = old_size + new_size;
1708 }
1709
Byongho Leeee221842015-12-15 01:42:10 +09001710 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001711 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001712 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001713 }
1714 if (new_size > device->bdev->bd_inode->i_size) {
1715 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001716 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001717 }
1718
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001719 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001720
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001721 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001722 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001723 if (IS_ERR(trans)) {
1724 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001725 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001726 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001727 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001728 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001729 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001730 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001731 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001732
Marcos Paulo de Souzafaf8f7b2020-02-11 10:55:26 -03001733 if (ret == 0 && new_size != old_size)
1734 btrfs_info_in_rcu(fs_info,
1735 "resize device %s (devid %llu) from %llu to %llu",
1736 rcu_str_deref(device->name), device->devid,
1737 old_size, new_size);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001738out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001739 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001740out:
David Sterba171938e2017-03-28 14:44:21 +02001741 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001742 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001743 return ret;
1744}
1745
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001746static noinline int __btrfs_ioctl_snap_create(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001747 const char *name, unsigned long fd, int subvol,
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001748 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001749 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001751 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001752 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001753
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001754 if (!S_ISDIR(file_inode(file)->i_mode))
1755 return -ENOTDIR;
1756
Liu Boa874a632012-06-29 03:58:46 -06001757 ret = mnt_want_write_file(file);
1758 if (ret)
1759 goto out;
1760
Sage Weil72fd0322010-10-29 15:41:32 -04001761 namelen = strlen(name);
1762 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001763 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001764 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001765 }
1766
Chris Mason16780ca2012-02-20 22:14:55 -05001767 if (name[0] == '.' &&
1768 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1769 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001770 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001771 }
1772
Chris Mason3de45862008-11-17 21:02:50 -05001773 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001774 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001775 NULL, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001776 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001777 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001778 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001779 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001780 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001781 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001782 }
1783
Al Viro496ad9a2013-01-23 17:07:38 -05001784 src_inode = file_inode(src.file);
1785 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001786 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001787 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001788 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001789 } else if (!inode_owner_or_capable(src_inode)) {
1790 /*
1791 * Subvolume creation is not restricted, but snapshots
1792 * are limited to own subvolumes only
1793 */
1794 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001795 } else {
Robbie Koc11fbb62020-05-14 17:19:18 +08001796 ret = btrfs_mksnapshot(&file->f_path, name, namelen,
Al Viroecd18812012-08-26 21:20:24 -04001797 BTRFS_I(src_inode)->root,
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001798 readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001799 }
Al Viro2903ff02012-08-28 12:52:22 -04001800 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001801 }
Liu Boa874a632012-06-29 03:58:46 -06001802out_drop_write:
1803 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001804out:
Sage Weil72fd0322010-10-29 15:41:32 -04001805 return ret;
1806}
1807
1808static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001809 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001810{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001811 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001812 int ret;
1813
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001814 if (!S_ISDIR(file_inode(file)->i_mode))
1815 return -ENOTDIR;
1816
Li Zefanfa0d2b92010-12-20 15:53:28 +08001817 vol_args = memdup_user(arg, sizeof(*vol_args));
1818 if (IS_ERR(vol_args))
1819 return PTR_ERR(vol_args);
1820 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001821
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001822 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1823 subvol, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001824
Li Zefanfa0d2b92010-12-20 15:53:28 +08001825 kfree(vol_args);
1826 return ret;
1827}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001828
Li Zefanfa0d2b92010-12-20 15:53:28 +08001829static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1830 void __user *arg, int subvol)
1831{
1832 struct btrfs_ioctl_vol_args_v2 *vol_args;
1833 int ret;
Li Zefanb83cc962010-12-20 16:04:08 +08001834 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001835 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001836
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001837 if (!S_ISDIR(file_inode(file)->i_mode))
1838 return -ENOTDIR;
1839
Li Zefanfa0d2b92010-12-20 15:53:28 +08001840 vol_args = memdup_user(arg, sizeof(*vol_args));
1841 if (IS_ERR(vol_args))
1842 return PTR_ERR(vol_args);
1843 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001844
David Sterba673990d2020-02-21 13:24:37 +01001845 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
Li Zefanb83cc962010-12-20 16:04:08 +08001846 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001847 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001848 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001849
Li Zefanb83cc962010-12-20 16:04:08 +08001850 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1851 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001852 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001853 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001854 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001855 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001856 }
1857 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1858 if (IS_ERR(inherit)) {
1859 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001860 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001861 }
1862 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001863
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001864 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1865 subvol, readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001866 if (ret)
1867 goto free_inherit;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001868free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001869 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001870free_args:
1871 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001872 return ret;
1873}
1874
Li Zefan0caa1022010-12-20 16:30:25 +08001875static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1876 void __user *arg)
1877{
Al Viro496ad9a2013-01-23 17:07:38 -05001878 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001879 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001880 struct btrfs_root *root = BTRFS_I(inode)->root;
1881 int ret = 0;
1882 u64 flags = 0;
1883
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001884 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001885 return -EINVAL;
1886
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001887 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001888 if (btrfs_root_readonly(root))
1889 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001891
1892 if (copy_to_user(arg, &flags, sizeof(flags)))
1893 ret = -EFAULT;
1894
1895 return ret;
1896}
1897
1898static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1899 void __user *arg)
1900{
Al Viro496ad9a2013-01-23 17:07:38 -05001901 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001902 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001903 struct btrfs_root *root = BTRFS_I(inode)->root;
1904 struct btrfs_trans_handle *trans;
1905 u64 root_flags;
1906 u64 flags;
1907 int ret = 0;
1908
David Sterbabd60ea02014-01-16 15:50:22 +01001909 if (!inode_owner_or_capable(inode))
1910 return -EPERM;
1911
Liu Bob9ca0662012-06-29 03:58:49 -06001912 ret = mnt_want_write_file(file);
1913 if (ret)
1914 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001915
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001916 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001917 ret = -EINVAL;
1918 goto out_drop_write;
1919 }
Li Zefan0caa1022010-12-20 16:30:25 +08001920
Liu Bob9ca0662012-06-29 03:58:49 -06001921 if (copy_from_user(&flags, arg, sizeof(flags))) {
1922 ret = -EFAULT;
1923 goto out_drop_write;
1924 }
Li Zefan0caa1022010-12-20 16:30:25 +08001925
Liu Bob9ca0662012-06-29 03:58:49 -06001926 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1927 ret = -EOPNOTSUPP;
1928 goto out_drop_write;
1929 }
Li Zefan0caa1022010-12-20 16:30:25 +08001930
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001931 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001932
1933 /* nothing to do */
1934 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001935 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001936
1937 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001938 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001939 btrfs_set_root_flags(&root->root_item,
1940 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001941 } else {
1942 /*
1943 * Block RO -> RW transition if this subvolume is involved in
1944 * send
1945 */
1946 spin_lock(&root->root_item_lock);
1947 if (root->send_in_progress == 0) {
1948 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001949 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001950 spin_unlock(&root->root_item_lock);
1951 } else {
1952 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001953 btrfs_warn(fs_info,
1954 "Attempt to set subvolume %llu read-write during send",
1955 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001956 ret = -EPERM;
1957 goto out_drop_sem;
1958 }
1959 }
Li Zefan0caa1022010-12-20 16:30:25 +08001960
1961 trans = btrfs_start_transaction(root, 1);
1962 if (IS_ERR(trans)) {
1963 ret = PTR_ERR(trans);
1964 goto out_reset;
1965 }
1966
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001967 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001968 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001969 if (ret < 0) {
1970 btrfs_end_transaction(trans);
1971 goto out_reset;
1972 }
Li Zefan0caa1022010-12-20 16:30:25 +08001973
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001974 ret = btrfs_commit_transaction(trans);
1975
Li Zefan0caa1022010-12-20 16:30:25 +08001976out_reset:
1977 if (ret)
1978 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001979out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001980 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001981out_drop_write:
1982 mnt_drop_write_file(file);
1983out:
Li Zefan0caa1022010-12-20 16:30:25 +08001984 return ret;
1985}
1986
Chris Masonac8e9812010-02-28 15:39:26 -05001987static noinline int key_in_sk(struct btrfs_key *key,
1988 struct btrfs_ioctl_search_key *sk)
1989{
Chris Masonabc6e132010-03-18 12:10:08 -04001990 struct btrfs_key test;
1991 int ret;
1992
1993 test.objectid = sk->min_objectid;
1994 test.type = sk->min_type;
1995 test.offset = sk->min_offset;
1996
1997 ret = btrfs_comp_cpu_keys(key, &test);
1998 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001999 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002000
2001 test.objectid = sk->max_objectid;
2002 test.type = sk->max_type;
2003 test.offset = sk->max_offset;
2004
2005 ret = btrfs_comp_cpu_keys(key, &test);
2006 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002007 return 0;
2008 return 1;
2009}
2010
Jeff Mahoneydf397562016-06-21 20:18:21 -04002011static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002012 struct btrfs_key *key,
2013 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002014 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002015 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002016 unsigned long *sk_offset,
2017 int *num_found)
2018{
2019 u64 found_transid;
2020 struct extent_buffer *leaf;
2021 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002022 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002023 unsigned long item_off;
2024 unsigned long item_len;
2025 int nritems;
2026 int i;
2027 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002028 int ret = 0;
2029
2030 leaf = path->nodes[0];
2031 slot = path->slots[0];
2032 nritems = btrfs_header_nritems(leaf);
2033
2034 if (btrfs_header_generation(leaf) > sk->max_transid) {
2035 i = nritems;
2036 goto advance_key;
2037 }
2038 found_transid = btrfs_header_generation(leaf);
2039
2040 for (i = slot; i < nritems; i++) {
2041 item_off = btrfs_item_ptr_offset(leaf, i);
2042 item_len = btrfs_item_size_nr(leaf, i);
2043
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002044 btrfs_item_key_to_cpu(leaf, key, i);
2045 if (!key_in_sk(key, sk))
2046 continue;
2047
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002048 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002049 if (*num_found) {
2050 ret = 1;
2051 goto out;
2052 }
Chris Masonac8e9812010-02-28 15:39:26 -05002053
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002054 /*
2055 * return one empty item back for v1, which does not
2056 * handle -EOVERFLOW
2057 */
2058
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002059 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002060 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002061 ret = -EOVERFLOW;
2062 }
Chris Masonac8e9812010-02-28 15:39:26 -05002063
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002064 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002065 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002066 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002067 }
2068
Chris Masonac8e9812010-02-28 15:39:26 -05002069 sh.objectid = key->objectid;
2070 sh.offset = key->offset;
2071 sh.type = key->type;
2072 sh.len = item_len;
2073 sh.transid = found_transid;
2074
2075 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002076 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2077 ret = -EFAULT;
2078 goto out;
2079 }
2080
Chris Masonac8e9812010-02-28 15:39:26 -05002081 *sk_offset += sizeof(sh);
2082
2083 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002084 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002085 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002086 if (read_extent_buffer_to_user(leaf, up,
2087 item_off, item_len)) {
2088 ret = -EFAULT;
2089 goto out;
2090 }
2091
Chris Masonac8e9812010-02-28 15:39:26 -05002092 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002093 }
Hugo Millse2156862011-05-14 17:43:41 +00002094 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002095
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002096 if (ret) /* -EOVERFLOW from above */
2097 goto out;
2098
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002099 if (*num_found >= sk->nr_items) {
2100 ret = 1;
2101 goto out;
2102 }
Chris Masonac8e9812010-02-28 15:39:26 -05002103 }
2104advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002105 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002106 test.objectid = sk->max_objectid;
2107 test.type = sk->max_type;
2108 test.offset = sk->max_offset;
2109 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2110 ret = 1;
2111 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002112 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002113 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002114 key->offset = 0;
2115 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002116 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002117 key->offset = 0;
2118 key->type = 0;
2119 key->objectid++;
2120 } else
2121 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002122out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002123 /*
2124 * 0: all items from this leaf copied, continue with next
2125 * 1: * more items can be copied, but unused buffer is too small
2126 * * all items were found
2127 * Either way, it will stops the loop which iterates to the next
2128 * leaf
2129 * -EOVERFLOW: item was to large for buffer
2130 * -EFAULT: could not copy extent buffer back to userspace
2131 */
Chris Masonac8e9812010-02-28 15:39:26 -05002132 return ret;
2133}
2134
2135static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002136 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002137 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002138 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002139{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002140 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002141 struct btrfs_root *root;
2142 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002143 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002144 int ret;
2145 int num_found = 0;
2146 unsigned long sk_offset = 0;
2147
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002148 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2149 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002150 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002151 }
Gerhard Heift12544442014-01-30 16:23:58 +01002152
Chris Masonac8e9812010-02-28 15:39:26 -05002153 path = btrfs_alloc_path();
2154 if (!path)
2155 return -ENOMEM;
2156
2157 if (sk->tree_id == 0) {
2158 /* search the root of the inode that was passed */
Josef Bacik00246522020-01-24 09:33:01 -05002159 root = btrfs_grab_root(BTRFS_I(inode)->root);
Chris Masonac8e9812010-02-28 15:39:26 -05002160 } else {
David Sterba56e93572020-05-15 19:35:55 +02002161 root = btrfs_get_fs_root(info, sk->tree_id, true);
Chris Masonac8e9812010-02-28 15:39:26 -05002162 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002163 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002164 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002165 }
2166 }
2167
2168 key.objectid = sk->min_objectid;
2169 key.type = sk->min_type;
2170 key.offset = sk->min_offset;
2171
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302172 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002173 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002174 if (ret != 0) {
2175 if (ret > 0)
2176 ret = 0;
2177 goto err;
2178 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002179 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002180 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002181 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002182 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002183 break;
2184
2185 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002186 if (ret > 0)
2187 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002188err:
2189 sk->nr_items = num_found;
Josef Bacik00246522020-01-24 09:33:01 -05002190 btrfs_put_root(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002191 btrfs_free_path(path);
2192 return ret;
2193}
2194
2195static noinline int btrfs_ioctl_tree_search(struct file *file,
2196 void __user *argp)
2197{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002198 struct btrfs_ioctl_search_args __user *uargs;
2199 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002200 struct inode *inode;
2201 int ret;
2202 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002203
2204 if (!capable(CAP_SYS_ADMIN))
2205 return -EPERM;
2206
Gerhard Heiftba346b32014-01-30 16:24:02 +01002207 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002208
Gerhard Heiftba346b32014-01-30 16:24:02 +01002209 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2210 return -EFAULT;
2211
2212 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002213
Al Viro496ad9a2013-01-23 17:07:38 -05002214 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002215 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002216
2217 /*
2218 * In the origin implementation an overflow is handled by returning a
2219 * search header with a len of zero, so reset ret.
2220 */
2221 if (ret == -EOVERFLOW)
2222 ret = 0;
2223
Gerhard Heiftba346b32014-01-30 16:24:02 +01002224 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002225 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002226 return ret;
2227}
2228
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002229static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2230 void __user *argp)
2231{
2232 struct btrfs_ioctl_search_args_v2 __user *uarg;
2233 struct btrfs_ioctl_search_args_v2 args;
2234 struct inode *inode;
2235 int ret;
2236 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002237 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002238
2239 if (!capable(CAP_SYS_ADMIN))
2240 return -EPERM;
2241
2242 /* copy search header and buffer size */
2243 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2244 if (copy_from_user(&args, uarg, sizeof(args)))
2245 return -EFAULT;
2246
2247 buf_size = args.buf_size;
2248
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002249 /* limit result size to 16MB */
2250 if (buf_size > buf_limit)
2251 buf_size = buf_limit;
2252
2253 inode = file_inode(file);
2254 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002255 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002256 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2257 ret = -EFAULT;
2258 else if (ret == -EOVERFLOW &&
2259 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2260 ret = -EFAULT;
2261
Yan, Zheng76dda932009-09-21 16:00:26 -04002262 return ret;
2263}
2264
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002265/*
Chris Masonac8e9812010-02-28 15:39:26 -05002266 * Search INODE_REFs to identify path name of 'dirid' directory
2267 * in a 'tree_id' tree. and sets path name to 'name'.
2268 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002269static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2270 u64 tree_id, u64 dirid, char *name)
2271{
2272 struct btrfs_root *root;
2273 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002274 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002275 int ret = -1;
2276 int slot;
2277 int len;
2278 int total_len = 0;
2279 struct btrfs_inode_ref *iref;
2280 struct extent_buffer *l;
2281 struct btrfs_path *path;
2282
2283 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2284 name[0]='\0';
2285 return 0;
2286 }
2287
2288 path = btrfs_alloc_path();
2289 if (!path)
2290 return -ENOMEM;
2291
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002292 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002293
David Sterba56e93572020-05-15 19:35:55 +02002294 root = btrfs_get_fs_root(info, tree_id, true);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002295 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002296 ret = PTR_ERR(root);
Josef Bacik88234012020-01-24 09:32:34 -05002297 root = NULL;
2298 goto out;
2299 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002300
2301 key.objectid = dirid;
2302 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002303 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002304
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302305 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2307 if (ret < 0)
2308 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002309 else if (ret > 0) {
2310 ret = btrfs_previous_item(root, path, dirid,
2311 BTRFS_INODE_REF_KEY);
2312 if (ret < 0)
2313 goto out;
2314 else if (ret > 0) {
2315 ret = -ENOENT;
2316 goto out;
2317 }
2318 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002319
2320 l = path->nodes[0];
2321 slot = path->slots[0];
2322 btrfs_item_key_to_cpu(l, &key, slot);
2323
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002324 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2325 len = btrfs_inode_ref_name_len(l, iref);
2326 ptr -= len + 1;
2327 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002328 if (ptr < name) {
2329 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002330 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002331 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002332
2333 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302334 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002335
2336 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2337 break;
2338
David Sterbab3b4aa72011-04-21 01:20:15 +02002339 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002340 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002341 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002342 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002343 }
Li Zefan77906a502011-07-14 03:16:00 +00002344 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302345 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002346 ret = 0;
2347out:
Josef Bacik00246522020-01-24 09:33:01 -05002348 btrfs_put_root(root);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002349 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002350 return ret;
2351}
2352
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002353static int btrfs_search_path_in_tree_user(struct inode *inode,
2354 struct btrfs_ioctl_ino_lookup_user_args *args)
2355{
2356 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2357 struct super_block *sb = inode->i_sb;
2358 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2359 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2360 u64 dirid = args->dirid;
2361 unsigned long item_off;
2362 unsigned long item_len;
2363 struct btrfs_inode_ref *iref;
2364 struct btrfs_root_ref *rref;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002365 struct btrfs_root *root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002366 struct btrfs_path *path;
2367 struct btrfs_key key, key2;
2368 struct extent_buffer *leaf;
2369 struct inode *temp_inode;
2370 char *ptr;
2371 int slot;
2372 int len;
2373 int total_len = 0;
2374 int ret;
2375
2376 path = btrfs_alloc_path();
2377 if (!path)
2378 return -ENOMEM;
2379
2380 /*
2381 * If the bottom subvolume does not exist directly under upper_limit,
2382 * construct the path in from the bottom up.
2383 */
2384 if (dirid != upper_limit.objectid) {
2385 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2386
David Sterba56e93572020-05-15 19:35:55 +02002387 root = btrfs_get_fs_root(fs_info, treeid, true);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002388 if (IS_ERR(root)) {
2389 ret = PTR_ERR(root);
2390 goto out;
2391 }
2392
2393 key.objectid = dirid;
2394 key.type = BTRFS_INODE_REF_KEY;
2395 key.offset = (u64)-1;
2396 while (1) {
2397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2398 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002399 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002400 } else if (ret > 0) {
2401 ret = btrfs_previous_item(root, path, dirid,
2402 BTRFS_INODE_REF_KEY);
2403 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002404 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002405 } else if (ret > 0) {
2406 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002407 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002408 }
2409 }
2410
2411 leaf = path->nodes[0];
2412 slot = path->slots[0];
2413 btrfs_item_key_to_cpu(leaf, &key, slot);
2414
2415 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2416 len = btrfs_inode_ref_name_len(leaf, iref);
2417 ptr -= len + 1;
2418 total_len += len + 1;
2419 if (ptr < args->path) {
2420 ret = -ENAMETOOLONG;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002421 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002422 }
2423
2424 *(ptr + len) = '/';
2425 read_extent_buffer(leaf, ptr,
2426 (unsigned long)(iref + 1), len);
2427
2428 /* Check the read+exec permission of this directory */
2429 ret = btrfs_previous_item(root, path, dirid,
2430 BTRFS_INODE_ITEM_KEY);
2431 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002432 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002433 } else if (ret > 0) {
2434 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002435 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002436 }
2437
2438 leaf = path->nodes[0];
2439 slot = path->slots[0];
2440 btrfs_item_key_to_cpu(leaf, &key2, slot);
2441 if (key2.objectid != dirid) {
2442 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002443 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002444 }
2445
David Sterba0202e832020-05-15 19:35:59 +02002446 temp_inode = btrfs_iget(sb, key2.objectid, root);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002447 if (IS_ERR(temp_inode)) {
2448 ret = PTR_ERR(temp_inode);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002449 goto out_put;
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002450 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002451 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2452 iput(temp_inode);
2453 if (ret) {
2454 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002455 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002456 }
2457
2458 if (key.offset == upper_limit.objectid)
2459 break;
2460 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2461 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002462 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002463 }
2464
2465 btrfs_release_path(path);
2466 key.objectid = key.offset;
2467 key.offset = (u64)-1;
2468 dirid = key.objectid;
2469 }
2470
2471 memmove(args->path, ptr, total_len);
2472 args->path[total_len] = '\0';
Josef Bacik00246522020-01-24 09:33:01 -05002473 btrfs_put_root(root);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002474 root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002475 btrfs_release_path(path);
2476 }
2477
2478 /* Get the bottom subvolume's name from ROOT_REF */
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002479 key.objectid = treeid;
2480 key.type = BTRFS_ROOT_REF_KEY;
2481 key.offset = args->treeid;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002482 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002483 if (ret < 0) {
2484 goto out;
2485 } else if (ret > 0) {
2486 ret = -ENOENT;
2487 goto out;
2488 }
2489
2490 leaf = path->nodes[0];
2491 slot = path->slots[0];
2492 btrfs_item_key_to_cpu(leaf, &key, slot);
2493
2494 item_off = btrfs_item_ptr_offset(leaf, slot);
2495 item_len = btrfs_item_size_nr(leaf, slot);
2496 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2497 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2498 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2499 ret = -EINVAL;
2500 goto out;
2501 }
2502
2503 /* Copy subvolume's name */
2504 item_off += sizeof(struct btrfs_root_ref);
2505 item_len -= sizeof(struct btrfs_root_ref);
2506 read_extent_buffer(leaf, args->name, item_off, item_len);
2507 args->name[item_len] = 0;
2508
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002509out_put:
Josef Bacik00246522020-01-24 09:33:01 -05002510 btrfs_put_root(root);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002511out:
2512 btrfs_free_path(path);
2513 return ret;
2514}
2515
Chris Masonac8e9812010-02-28 15:39:26 -05002516static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2517 void __user *argp)
2518{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002519 struct btrfs_ioctl_ino_lookup_args *args;
2520 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002521 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002522
Julia Lawall2354d08f2010-10-29 15:14:18 -04002523 args = memdup_user(argp, sizeof(*args));
2524 if (IS_ERR(args))
2525 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002526
Al Viro496ad9a2013-01-23 17:07:38 -05002527 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002528
David Sterba01b810b2015-05-12 19:14:49 +02002529 /*
2530 * Unprivileged query to obtain the containing subvolume root id. The
2531 * path is reset so it's consistent with btrfs_search_path_in_tree.
2532 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002533 if (args->treeid == 0)
2534 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2535
David Sterba01b810b2015-05-12 19:14:49 +02002536 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2537 args->name[0] = 0;
2538 goto out;
2539 }
2540
2541 if (!capable(CAP_SYS_ADMIN)) {
2542 ret = -EPERM;
2543 goto out;
2544 }
2545
Chris Masonac8e9812010-02-28 15:39:26 -05002546 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2547 args->treeid, args->objectid,
2548 args->name);
2549
David Sterba01b810b2015-05-12 19:14:49 +02002550out:
Chris Masonac8e9812010-02-28 15:39:26 -05002551 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2552 ret = -EFAULT;
2553
2554 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002555 return ret;
2556}
2557
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002558/*
2559 * Version of ino_lookup ioctl (unprivileged)
2560 *
2561 * The main differences from ino_lookup ioctl are:
2562 *
2563 * 1. Read + Exec permission will be checked using inode_permission() during
2564 * path construction. -EACCES will be returned in case of failure.
2565 * 2. Path construction will be stopped at the inode number which corresponds
2566 * to the fd with which this ioctl is called. If constructed path does not
2567 * exist under fd's inode, -EACCES will be returned.
2568 * 3. The name of bottom subvolume is also searched and filled.
2569 */
2570static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2571{
2572 struct btrfs_ioctl_ino_lookup_user_args *args;
2573 struct inode *inode;
2574 int ret;
2575
2576 args = memdup_user(argp, sizeof(*args));
2577 if (IS_ERR(args))
2578 return PTR_ERR(args);
2579
2580 inode = file_inode(file);
2581
2582 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2583 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2584 /*
2585 * The subvolume does not exist under fd with which this is
2586 * called
2587 */
2588 kfree(args);
2589 return -EACCES;
2590 }
2591
2592 ret = btrfs_search_path_in_tree_user(inode, args);
2593
2594 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2595 ret = -EFAULT;
2596
2597 kfree(args);
2598 return ret;
2599}
2600
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002601/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2602static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2603{
2604 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2605 struct btrfs_fs_info *fs_info;
2606 struct btrfs_root *root;
2607 struct btrfs_path *path;
2608 struct btrfs_key key;
2609 struct btrfs_root_item *root_item;
2610 struct btrfs_root_ref *rref;
2611 struct extent_buffer *leaf;
2612 unsigned long item_off;
2613 unsigned long item_len;
2614 struct inode *inode;
2615 int slot;
2616 int ret = 0;
2617
2618 path = btrfs_alloc_path();
2619 if (!path)
2620 return -ENOMEM;
2621
2622 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2623 if (!subvol_info) {
2624 btrfs_free_path(path);
2625 return -ENOMEM;
2626 }
2627
2628 inode = file_inode(file);
2629 fs_info = BTRFS_I(inode)->root->fs_info;
2630
2631 /* Get root_item of inode's subvolume */
2632 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
David Sterba56e93572020-05-15 19:35:55 +02002633 root = btrfs_get_fs_root(fs_info, key.objectid, true);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002634 if (IS_ERR(root)) {
2635 ret = PTR_ERR(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002636 goto out_free;
2637 }
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002638 root_item = &root->root_item;
2639
2640 subvol_info->treeid = key.objectid;
2641
2642 subvol_info->generation = btrfs_root_generation(root_item);
2643 subvol_info->flags = btrfs_root_flags(root_item);
2644
2645 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2646 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2647 BTRFS_UUID_SIZE);
2648 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2649 BTRFS_UUID_SIZE);
2650
2651 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2652 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2653 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2654
2655 subvol_info->otransid = btrfs_root_otransid(root_item);
2656 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2657 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2658
2659 subvol_info->stransid = btrfs_root_stransid(root_item);
2660 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2661 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2662
2663 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2664 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2665 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2666
2667 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2668 /* Search root tree for ROOT_BACKREF of this subvolume */
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002669 key.type = BTRFS_ROOT_BACKREF_KEY;
2670 key.offset = 0;
Josef Bacik04734e82020-01-24 09:32:36 -05002671 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002672 if (ret < 0) {
2673 goto out;
2674 } else if (path->slots[0] >=
2675 btrfs_header_nritems(path->nodes[0])) {
Josef Bacik04734e82020-01-24 09:32:36 -05002676 ret = btrfs_next_leaf(fs_info->tree_root, path);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002677 if (ret < 0) {
2678 goto out;
2679 } else if (ret > 0) {
2680 ret = -EUCLEAN;
2681 goto out;
2682 }
2683 }
2684
2685 leaf = path->nodes[0];
2686 slot = path->slots[0];
2687 btrfs_item_key_to_cpu(leaf, &key, slot);
2688 if (key.objectid == subvol_info->treeid &&
2689 key.type == BTRFS_ROOT_BACKREF_KEY) {
2690 subvol_info->parent_id = key.offset;
2691
2692 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2693 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2694
2695 item_off = btrfs_item_ptr_offset(leaf, slot)
2696 + sizeof(struct btrfs_root_ref);
2697 item_len = btrfs_item_size_nr(leaf, slot)
2698 - sizeof(struct btrfs_root_ref);
2699 read_extent_buffer(leaf, subvol_info->name,
2700 item_off, item_len);
2701 } else {
2702 ret = -ENOENT;
2703 goto out;
2704 }
2705 }
2706
2707 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2708 ret = -EFAULT;
2709
2710out:
Josef Bacik00246522020-01-24 09:33:01 -05002711 btrfs_put_root(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002712out_free:
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002713 btrfs_free_path(path);
Waiman Longb091f7f2020-06-16 11:31:59 -04002714 kfree(subvol_info);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002715 return ret;
2716}
2717
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002718/*
2719 * Return ROOT_REF information of the subvolume containing this inode
2720 * except the subvolume name.
2721 */
2722static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2723{
2724 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2725 struct btrfs_root_ref *rref;
2726 struct btrfs_root *root;
2727 struct btrfs_path *path;
2728 struct btrfs_key key;
2729 struct extent_buffer *leaf;
2730 struct inode *inode;
2731 u64 objectid;
2732 int slot;
2733 int ret;
2734 u8 found;
2735
2736 path = btrfs_alloc_path();
2737 if (!path)
2738 return -ENOMEM;
2739
2740 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2741 if (IS_ERR(rootrefs)) {
2742 btrfs_free_path(path);
2743 return PTR_ERR(rootrefs);
2744 }
2745
2746 inode = file_inode(file);
2747 root = BTRFS_I(inode)->root->fs_info->tree_root;
2748 objectid = BTRFS_I(inode)->root->root_key.objectid;
2749
2750 key.objectid = objectid;
2751 key.type = BTRFS_ROOT_REF_KEY;
2752 key.offset = rootrefs->min_treeid;
2753 found = 0;
2754
2755 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2756 if (ret < 0) {
2757 goto out;
2758 } else if (path->slots[0] >=
2759 btrfs_header_nritems(path->nodes[0])) {
2760 ret = btrfs_next_leaf(root, path);
2761 if (ret < 0) {
2762 goto out;
2763 } else if (ret > 0) {
2764 ret = -EUCLEAN;
2765 goto out;
2766 }
2767 }
2768 while (1) {
2769 leaf = path->nodes[0];
2770 slot = path->slots[0];
2771
2772 btrfs_item_key_to_cpu(leaf, &key, slot);
2773 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2774 ret = 0;
2775 goto out;
2776 }
2777
2778 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2779 ret = -EOVERFLOW;
2780 goto out;
2781 }
2782
2783 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2784 rootrefs->rootref[found].treeid = key.offset;
2785 rootrefs->rootref[found].dirid =
2786 btrfs_root_ref_dirid(leaf, rref);
2787 found++;
2788
2789 ret = btrfs_next_item(root, path);
2790 if (ret < 0) {
2791 goto out;
2792 } else if (ret > 0) {
2793 ret = -EUCLEAN;
2794 goto out;
2795 }
2796 }
2797
2798out:
2799 if (!ret || ret == -EOVERFLOW) {
2800 rootrefs->num_items = found;
2801 /* update min_treeid for next search */
2802 if (found)
2803 rootrefs->min_treeid =
2804 rootrefs->rootref[found - 1].treeid + 1;
2805 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2806 ret = -EFAULT;
2807 }
2808
2809 kfree(rootrefs);
2810 btrfs_free_path(path);
2811
2812 return ret;
2813}
2814
Yan, Zheng76dda932009-09-21 16:00:26 -04002815static noinline int btrfs_ioctl_snap_destroy(struct file *file,
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002816 void __user *arg,
2817 bool destroy_v2)
Yan, Zheng76dda932009-09-21 16:00:26 -04002818{
Al Viro54563d42013-09-01 15:57:51 -04002819 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002820 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002821 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002822 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002823 struct inode *inode;
2824 struct btrfs_root *root = BTRFS_I(dir)->root;
2825 struct btrfs_root *dest = NULL;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002826 struct btrfs_ioctl_vol_args *vol_args = NULL;
2827 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2828 char *subvol_name, *subvol_name_ptr = NULL;
2829 int subvol_namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002830 int err = 0;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002831 bool destroy_parent = false;
Yan, Zheng76dda932009-09-21 16:00:26 -04002832
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002833 if (destroy_v2) {
2834 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2835 if (IS_ERR(vol_args2))
2836 return PTR_ERR(vol_args2);
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002837
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002838 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2839 err = -EOPNOTSUPP;
2840 goto out;
2841 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002842
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002843 /*
2844 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2845 * name, same as v1 currently does.
2846 */
2847 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2848 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2849 subvol_name = vol_args2->name;
2850
2851 err = mnt_want_write_file(file);
2852 if (err)
2853 goto out;
2854 } else {
2855 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2856 err = -EINVAL;
2857 goto out;
2858 }
2859
2860 err = mnt_want_write_file(file);
2861 if (err)
2862 goto out;
2863
2864 dentry = btrfs_get_dentry(fs_info->sb,
2865 BTRFS_FIRST_FREE_OBJECTID,
2866 vol_args2->subvolid, 0, 0);
2867 if (IS_ERR(dentry)) {
2868 err = PTR_ERR(dentry);
2869 goto out_drop_write;
2870 }
2871
2872 /*
2873 * Change the default parent since the subvolume being
2874 * deleted can be outside of the current mount point.
2875 */
2876 parent = btrfs_get_parent(dentry);
2877
2878 /*
2879 * At this point dentry->d_name can point to '/' if the
2880 * subvolume we want to destroy is outsite of the
2881 * current mount point, so we need to release the
2882 * current dentry and execute the lookup to return a new
2883 * one with ->d_name pointing to the
2884 * <mount point>/subvol_name.
2885 */
2886 dput(dentry);
2887 if (IS_ERR(parent)) {
2888 err = PTR_ERR(parent);
2889 goto out_drop_write;
2890 }
2891 dir = d_inode(parent);
2892
2893 /*
2894 * If v2 was used with SPEC_BY_ID, a new parent was
2895 * allocated since the subvolume can be outside of the
2896 * current mount point. Later on we need to release this
2897 * new parent dentry.
2898 */
2899 destroy_parent = true;
2900
2901 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2902 fs_info, vol_args2->subvolid);
2903 if (IS_ERR(subvol_name_ptr)) {
2904 err = PTR_ERR(subvol_name_ptr);
2905 goto free_parent;
2906 }
2907 /* subvol_name_ptr is already NULL termined */
2908 subvol_name = (char *)kbasename(subvol_name_ptr);
2909 }
2910 } else {
2911 vol_args = memdup_user(arg, sizeof(*vol_args));
2912 if (IS_ERR(vol_args))
2913 return PTR_ERR(vol_args);
2914
2915 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
2916 subvol_name = vol_args->name;
2917
2918 err = mnt_want_write_file(file);
2919 if (err)
2920 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002921 }
2922
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002923 subvol_namelen = strlen(subvol_name);
Yan, Zheng76dda932009-09-21 16:00:26 -04002924
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002925 if (strchr(subvol_name, '/') ||
2926 strncmp(subvol_name, "..", subvol_namelen) == 0) {
2927 err = -EINVAL;
2928 goto free_subvol_name;
2929 }
2930
2931 if (!S_ISDIR(dir->i_mode)) {
2932 err = -ENOTDIR;
2933 goto free_subvol_name;
2934 }
David Sterba521e0542014-04-15 16:41:44 +02002935
Al Viro00235412016-05-26 00:05:12 -04002936 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2937 if (err == -EINTR)
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002938 goto free_subvol_name;
2939 dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -04002940 if (IS_ERR(dentry)) {
2941 err = PTR_ERR(dentry);
2942 goto out_unlock_dir;
2943 }
2944
David Howells2b0143b2015-03-17 22:25:59 +00002945 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002946 err = -ENOENT;
2947 goto out_dput;
2948 }
2949
David Howells2b0143b2015-03-17 22:25:59 +00002950 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002951 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302952 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002953 /*
2954 * Regular user. Only allow this with a special mount
2955 * option, when the user has write+exec access to the
2956 * subvol root, and when rmdir(2) would have been
2957 * allowed.
2958 *
2959 * Note that this is _not_ check that the subvol is
2960 * empty or doesn't contain data that we wouldn't
2961 * otherwise be able to delete.
2962 *
2963 * Users who want to delete empty subvols should try
2964 * rmdir(2).
2965 */
2966 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002967 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002968 goto out_dput;
2969
2970 /*
2971 * Do not allow deletion if the parent dir is the same
2972 * as the dir to be deleted. That means the ioctl
2973 * must be called on the dentry referencing the root
2974 * of the subvol, not a random directory contained
2975 * within it.
2976 */
2977 err = -EINVAL;
2978 if (root == dest)
2979 goto out_dput;
2980
2981 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2982 if (err)
2983 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002984 }
2985
Miao Xie5c39da52012-10-22 11:39:53 +00002986 /* check if subvolume may be deleted by a user */
2987 err = btrfs_may_delete(dir, dentry, 1);
2988 if (err)
2989 goto out_dput;
2990
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002991 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002992 err = -EINVAL;
2993 goto out_dput;
2994 }
2995
Al Viro59551022016-01-22 15:40:57 -05002996 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002997 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002998 inode_unlock(inode);
Amir Goldstein46008d92019-05-26 17:34:03 +03002999 if (!err) {
3000 fsnotify_rmdir(dir, dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04003001 d_delete(dentry);
Amir Goldstein46008d92019-05-26 17:34:03 +03003002 }
Liu Bofa6ac872013-02-20 14:10:23 +00003003
Yan, Zheng76dda932009-09-21 16:00:26 -04003004out_dput:
3005 dput(dentry);
3006out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05003007 inode_unlock(dir);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003008free_subvol_name:
3009 kfree(subvol_name_ptr);
3010free_parent:
3011 if (destroy_parent)
3012 dput(parent);
Al Viro00235412016-05-26 00:05:12 -04003013out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003014 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04003015out:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003016 kfree(vol_args2);
Yan, Zheng76dda932009-09-21 16:00:26 -04003017 kfree(vol_args);
3018 return err;
3019}
3020
Chris Mason1e701a32010-03-11 09:42:04 -05003021static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003022{
Al Viro496ad9a2013-01-23 17:07:38 -05003023 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003024 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05003025 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05003026 int ret;
3027
Ilya Dryomov25122d12013-01-20 15:57:57 +02003028 ret = mnt_want_write_file(file);
3029 if (ret)
3030 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08003031
Ilya Dryomov25122d12013-01-20 15:57:57 +02003032 if (btrfs_root_readonly(root)) {
3033 ret = -EROFS;
3034 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003035 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003036
3037 switch (inode->i_mode & S_IFMT) {
3038 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05003039 if (!capable(CAP_SYS_ADMIN)) {
3040 ret = -EPERM;
3041 goto out;
3042 }
Eric Sandeende78b512013-01-31 18:21:12 +00003043 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003044 break;
3045 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02003046 /*
3047 * Note that this does not check the file descriptor for write
3048 * access. This prevents defragmenting executables that are
3049 * running and allows defrag on files open in read-only mode.
3050 */
3051 if (!capable(CAP_SYS_ADMIN) &&
3052 inode_permission(inode, MAY_WRITE)) {
3053 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05003054 goto out;
3055 }
Chris Mason1e701a32010-03-11 09:42:04 -05003056
3057 range = kzalloc(sizeof(*range), GFP_KERNEL);
3058 if (!range) {
3059 ret = -ENOMEM;
3060 goto out;
3061 }
3062
3063 if (argp) {
3064 if (copy_from_user(range, argp,
3065 sizeof(*range))) {
3066 ret = -EFAULT;
3067 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00003068 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05003069 }
3070 /* compression requires us to start the IO */
3071 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3072 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3073 range->extent_thresh = (u32)-1;
3074 }
3075 } else {
3076 /* the rest are all set to zero by kzalloc */
3077 range->len = (u64)-1;
3078 }
Al Viro496ad9a2013-01-23 17:07:38 -05003079 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08003080 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003081 if (ret > 0)
3082 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003083 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003084 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003085 default:
3086 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003087 }
Chris Masone441d542009-01-05 16:57:23 -05003088out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003089 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003090 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003091}
3092
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003093static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003094{
3095 struct btrfs_ioctl_vol_args *vol_args;
3096 int ret;
3097
Chris Masone441d542009-01-05 16:57:23 -05003098 if (!capable(CAP_SYS_ADMIN))
3099 return -EPERM;
3100
David Sterba171938e2017-03-28 14:44:21 +02003101 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003102 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003103
Li Zefandae7b662009-04-08 15:06:54 +08003104 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003105 if (IS_ERR(vol_args)) {
3106 ret = PTR_ERR(vol_args);
3107 goto out;
3108 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003109
Mark Fasheh5516e592008-07-24 12:20:14 -04003110 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003111 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003112
Anand Jain43d20762014-07-01 00:58:56 +08003113 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003114 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003115
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003116 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003117out:
David Sterba171938e2017-03-28 14:44:21 +02003118 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003119 return ret;
3120}
3121
Anand Jain6b526ed2016-02-13 10:01:39 +08003122static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003123{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003124 struct inode *inode = file_inode(file);
3125 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003126 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003127 int ret;
3128
Chris Masone441d542009-01-05 16:57:23 -05003129 if (!capable(CAP_SYS_ADMIN))
3130 return -EPERM;
3131
Miao Xieda249272012-11-26 08:44:50 +00003132 ret = mnt_want_write_file(file);
3133 if (ret)
3134 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003135
Li Zefandae7b662009-04-08 15:06:54 +08003136 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003137 if (IS_ERR(vol_args)) {
3138 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003139 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003140 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003141
David Sterba748449cdb2020-02-21 13:30:14 +01003142 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003143 ret = -EOPNOTSUPP;
3144 goto out;
3145 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003146
David Sterba171938e2017-03-28 14:44:21 +02003147 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003148 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3149 goto out;
3150 }
3151
David Sterba735654e2016-02-15 18:15:21 +01003152 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003153 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003154 } else {
3155 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003156 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003157 }
David Sterba171938e2017-03-28 14:44:21 +02003158 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003159
Anand Jain6b526ed2016-02-13 10:01:39 +08003160 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003161 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003162 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003163 vol_args->devid);
3164 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003165 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003166 vol_args->name);
3167 }
Anand Jain183860f2013-05-17 10:52:45 +00003168out:
3169 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003170err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003171 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003172 return ret;
3173}
3174
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003175static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3176{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003177 struct inode *inode = file_inode(file);
3178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003179 struct btrfs_ioctl_vol_args *vol_args;
3180 int ret;
3181
3182 if (!capable(CAP_SYS_ADMIN))
3183 return -EPERM;
3184
3185 ret = mnt_want_write_file(file);
3186 if (ret)
3187 return ret;
3188
David Sterba171938e2017-03-28 14:44:21 +02003189 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003190 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003191 goto out_drop_write;
3192 }
3193
3194 vol_args = memdup_user(arg, sizeof(*vol_args));
3195 if (IS_ERR(vol_args)) {
3196 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003197 goto out;
3198 }
3199
David Sterba58d7bbf2016-05-04 14:10:47 +02003200 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003201 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003202
3203 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003204 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003205 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003206out:
David Sterba171938e2017-03-28 14:44:21 +02003207 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003208out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003209 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003210
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003211 return ret;
3212}
3213
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003214static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3215 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003216{
Li Zefan027ed2f2011-06-08 08:27:56 +00003217 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003218 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003219 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003220 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003221
Li Zefan027ed2f2011-06-08 08:27:56 +00003222 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3223 if (!fi_args)
3224 return -ENOMEM;
3225
David Sterbad03262c2017-06-16 00:09:21 +02003226 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003227 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003228
David Sterbad03262c2017-06-16 00:09:21 +02003229 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003230 if (device->devid > fi_args->max_id)
3231 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003232 }
David Sterbad03262c2017-06-16 00:09:21 +02003233 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003234
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003235 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003236 fi_args->nodesize = fs_info->nodesize;
3237 fi_args->sectorsize = fs_info->sectorsize;
3238 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003239
Li Zefan027ed2f2011-06-08 08:27:56 +00003240 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3241 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003242
Li Zefan027ed2f2011-06-08 08:27:56 +00003243 kfree(fi_args);
3244 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003245}
3246
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003247static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3248 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003249{
3250 struct btrfs_ioctl_dev_info_args *di_args;
3251 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003252 int ret = 0;
3253 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003254
Jan Schmidt475f6382011-03-11 15:41:01 +01003255 di_args = memdup_user(arg, sizeof(*di_args));
3256 if (IS_ERR(di_args))
3257 return PTR_ERR(di_args);
3258
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003259 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003260 s_uuid = di_args->uuid;
3261
David Sterbac5593ca2017-06-16 00:09:21 +02003262 rcu_read_lock();
Anand Jaine4319cd2019-01-17 23:32:31 +08003263 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08003264 NULL, true);
Jan Schmidt475f6382011-03-11 15:41:01 +01003265
3266 if (!dev) {
3267 ret = -ENODEV;
3268 goto out;
3269 }
3270
3271 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003272 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3273 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003274 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003275 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003276 strncpy(di_args->path, rcu_str_deref(dev->name),
3277 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003278 di_args->path[sizeof(di_args->path) - 1] = 0;
3279 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003280 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003281 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003282
3283out:
David Sterbac5593ca2017-06-16 00:09:21 +02003284 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003285 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3286 ret = -EFAULT;
3287
3288 kfree(di_args);
3289 return ret;
3290}
3291
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003292static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3293{
Al Viro496ad9a2013-01-23 17:07:38 -05003294 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003295 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003296 struct btrfs_root *root = BTRFS_I(inode)->root;
3297 struct btrfs_root *new_root;
3298 struct btrfs_dir_item *di;
3299 struct btrfs_trans_handle *trans;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003300 struct btrfs_path *path = NULL;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003301 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003302 u64 objectid = 0;
3303 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003304 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003305
3306 if (!capable(CAP_SYS_ADMIN))
3307 return -EPERM;
3308
Miao Xie3c04ce02012-11-26 08:43:07 +00003309 ret = mnt_want_write_file(file);
3310 if (ret)
3311 return ret;
3312
3313 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3314 ret = -EFAULT;
3315 goto out;
3316 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003317
3318 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303319 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003320
David Sterba56e93572020-05-15 19:35:55 +02003321 new_root = btrfs_get_fs_root(fs_info, objectid, true);
Miao Xie3c04ce02012-11-26 08:43:07 +00003322 if (IS_ERR(new_root)) {
3323 ret = PTR_ERR(new_root);
3324 goto out;
3325 }
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003326 if (!is_fstree(new_root->root_key.objectid)) {
3327 ret = -ENOENT;
3328 goto out_free;
3329 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003330
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003331 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003332 if (!path) {
3333 ret = -ENOMEM;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003334 goto out_free;
Miao Xie3c04ce02012-11-26 08:43:07 +00003335 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003336 path->leave_spinning = 1;
3337
3338 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003339 if (IS_ERR(trans)) {
Miao Xie3c04ce02012-11-26 08:43:07 +00003340 ret = PTR_ERR(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003341 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003342 }
3343
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003344 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3345 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003346 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003347 if (IS_ERR_OR_NULL(di)) {
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003348 btrfs_release_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003349 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003350 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003351 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003352 ret = -ENOENT;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003353 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003354 }
3355
3356 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3357 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3358 btrfs_mark_buffer_dirty(path->nodes[0]);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003359 btrfs_release_path(path);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003360
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003361 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003362 btrfs_end_transaction(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003363out_free:
Josef Bacik00246522020-01-24 09:33:01 -05003364 btrfs_put_root(new_root);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003365 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003366out:
3367 mnt_drop_write_file(file);
3368 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003369}
3370
Su Yuec065f5b12018-04-02 17:24:11 +08003371static void get_block_group_info(struct list_head *groups_list,
3372 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003373{
David Sterba32da53862019-10-29 19:20:18 +01003374 struct btrfs_block_group *block_group;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003375
3376 space->total_bytes = 0;
3377 space->used_bytes = 0;
3378 space->flags = 0;
3379 list_for_each_entry(block_group, groups_list, list) {
3380 space->flags = block_group->flags;
David Sterbab3470b52019-10-23 18:48:22 +02003381 space->total_bytes += block_group->length;
David Sterbabf38be62019-10-23 18:48:11 +02003382 space->used_bytes += block_group->used;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003383 }
3384}
3385
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003386static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3387 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003388{
3389 struct btrfs_ioctl_space_args space_args;
3390 struct btrfs_ioctl_space_info space;
3391 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003392 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003393 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003394 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01003395 static const u64 types[] = {
3396 BTRFS_BLOCK_GROUP_DATA,
3397 BTRFS_BLOCK_GROUP_SYSTEM,
3398 BTRFS_BLOCK_GROUP_METADATA,
3399 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3400 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003401 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003402 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003403 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003404 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003405 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003406
3407 if (copy_from_user(&space_args,
3408 (struct btrfs_ioctl_space_args __user *)arg,
3409 sizeof(space_args)))
3410 return -EFAULT;
3411
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003412 for (i = 0; i < num_types; i++) {
3413 struct btrfs_space_info *tmp;
3414
3415 info = NULL;
3416 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003417 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003418 list) {
3419 if (tmp->flags == types[i]) {
3420 info = tmp;
3421 break;
3422 }
3423 }
3424 rcu_read_unlock();
3425
3426 if (!info)
3427 continue;
3428
3429 down_read(&info->groups_sem);
3430 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3431 if (!list_empty(&info->block_groups[c]))
3432 slot_count++;
3433 }
3434 up_read(&info->groups_sem);
3435 }
Josef Bacik1406e432010-01-13 18:19:06 +00003436
David Sterba36523e952014-02-07 14:34:12 +01003437 /*
3438 * Global block reserve, exported as a space_info
3439 */
3440 slot_count++;
3441
Chris Mason7fde62b2010-03-16 15:40:10 -04003442 /* space_slots == 0 means they are asking for a count */
3443 if (space_args.space_slots == 0) {
3444 space_args.total_spaces = slot_count;
3445 goto out;
3446 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003447
Dan Rosenberg51788b12011-02-14 16:04:23 -05003448 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003449
Chris Mason7fde62b2010-03-16 15:40:10 -04003450 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003451
Chris Mason7fde62b2010-03-16 15:40:10 -04003452 /* we generally have at most 6 or so space infos, one for each raid
3453 * level. So, a whole page should be more than enough for everyone
3454 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003455 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04003456 return -ENOMEM;
3457
3458 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01003459 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04003460 if (!dest)
3461 return -ENOMEM;
3462 dest_orig = dest;
3463
3464 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003465 for (i = 0; i < num_types; i++) {
3466 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003467
Dan Rosenberg51788b12011-02-14 16:04:23 -05003468 if (!slot_count)
3469 break;
3470
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003471 info = NULL;
3472 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003473 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003474 list) {
3475 if (tmp->flags == types[i]) {
3476 info = tmp;
3477 break;
3478 }
3479 }
3480 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003481
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003482 if (!info)
3483 continue;
3484 down_read(&info->groups_sem);
3485 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3486 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08003487 get_block_group_info(&info->block_groups[c],
3488 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003489 memcpy(dest, &space, sizeof(space));
3490 dest++;
3491 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003492 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003493 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003494 if (!slot_count)
3495 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003496 }
3497 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003498 }
Josef Bacik1406e432010-01-13 18:19:06 +00003499
David Sterba36523e952014-02-07 14:34:12 +01003500 /*
3501 * Add global block reserve
3502 */
3503 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003504 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01003505
3506 spin_lock(&block_rsv->lock);
3507 space.total_bytes = block_rsv->size;
3508 space.used_bytes = block_rsv->size - block_rsv->reserved;
3509 spin_unlock(&block_rsv->lock);
3510 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3511 memcpy(dest, &space, sizeof(space));
3512 space_args.total_spaces++;
3513 }
3514
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003515 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003516 (arg + sizeof(struct btrfs_ioctl_space_args));
3517
3518 if (copy_to_user(user_dest, dest_orig, alloc_size))
3519 ret = -EFAULT;
3520
3521 kfree(dest_orig);
3522out:
3523 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003524 ret = -EFAULT;
3525
3526 return ret;
3527}
3528
Miao Xie9a8c28b2012-11-26 08:40:43 +00003529static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3530 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003531{
Sage Weil46204592010-10-29 15:41:32 -04003532 struct btrfs_trans_handle *trans;
3533 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003534 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003535
Miao Xied4edf392013-02-20 09:17:06 +00003536 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003537 if (IS_ERR(trans)) {
3538 if (PTR_ERR(trans) != -ENOENT)
3539 return PTR_ERR(trans);
3540
3541 /* No running transaction, don't bother */
3542 transid = root->fs_info->last_trans_committed;
3543 goto out;
3544 }
Sage Weil46204592010-10-29 15:41:32 -04003545 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003546 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003547 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003548 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003549 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003550 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003551out:
Sage Weil46204592010-10-29 15:41:32 -04003552 if (argp)
3553 if (copy_to_user(argp, &transid, sizeof(transid)))
3554 return -EFAULT;
3555 return 0;
3556}
3557
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003558static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00003559 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003560{
Sage Weil46204592010-10-29 15:41:32 -04003561 u64 transid;
3562
3563 if (argp) {
3564 if (copy_from_user(&transid, argp, sizeof(transid)))
3565 return -EFAULT;
3566 } else {
3567 transid = 0; /* current trans */
3568 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003569 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04003570}
3571
Miao Xieb8e95482012-11-26 08:48:01 +00003572static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003573{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003574 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01003575 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003576 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003577
3578 if (!capable(CAP_SYS_ADMIN))
3579 return -EPERM;
3580
3581 sa = memdup_user(arg, sizeof(*sa));
3582 if (IS_ERR(sa))
3583 return PTR_ERR(sa);
3584
Miao Xieb8e95482012-11-26 08:48:01 +00003585 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3586 ret = mnt_want_write_file(file);
3587 if (ret)
3588 goto out;
3589 }
3590
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003591 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003592 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3593 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003594
Filipe Manana5afe6ce2020-01-16 11:29:20 +00003595 /*
3596 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3597 * error. This is important as it allows user space to know how much
3598 * progress scrub has done. For example, if scrub is canceled we get
3599 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3600 * space. Later user space can inspect the progress from the structure
3601 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3602 * previously (btrfs-progs does this).
3603 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3604 * then return -EFAULT to signal the structure was not copied or it may
3605 * be corrupt and unreliable due to a partial copy.
3606 */
3607 if (copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003608 ret = -EFAULT;
3609
Miao Xieb8e95482012-11-26 08:48:01 +00003610 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3611 mnt_drop_write_file(file);
3612out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003613 kfree(sa);
3614 return ret;
3615}
3616
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003617static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01003618{
3619 if (!capable(CAP_SYS_ADMIN))
3620 return -EPERM;
3621
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003622 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003623}
3624
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003625static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01003626 void __user *arg)
3627{
3628 struct btrfs_ioctl_scrub_args *sa;
3629 int ret;
3630
3631 if (!capable(CAP_SYS_ADMIN))
3632 return -EPERM;
3633
3634 sa = memdup_user(arg, sizeof(*sa));
3635 if (IS_ERR(sa))
3636 return PTR_ERR(sa);
3637
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003638 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01003639
Filipe Manana4fa99b02018-12-14 19:45:13 +00003640 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003641 ret = -EFAULT;
3642
3643 kfree(sa);
3644 return ret;
3645}
3646
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003647static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06003648 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003649{
3650 struct btrfs_ioctl_get_dev_stats *sa;
3651 int ret;
3652
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003653 sa = memdup_user(arg, sizeof(*sa));
3654 if (IS_ERR(sa))
3655 return PTR_ERR(sa);
3656
David Sterbab27f7c02012-06-22 06:30:39 -06003657 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3658 kfree(sa);
3659 return -EPERM;
3660 }
3661
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003662 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003663
Filipe Mananaeee99572018-12-14 19:45:22 +00003664 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003665 ret = -EFAULT;
3666
3667 kfree(sa);
3668 return ret;
3669}
3670
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003671static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3672 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003673{
3674 struct btrfs_ioctl_dev_replace_args *p;
3675 int ret;
3676
3677 if (!capable(CAP_SYS_ADMIN))
3678 return -EPERM;
3679
3680 p = memdup_user(arg, sizeof(*p));
3681 if (IS_ERR(p))
3682 return PTR_ERR(p);
3683
3684 switch (p->cmd) {
3685 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01003686 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003687 ret = -EROFS;
3688 goto out;
3689 }
David Sterba171938e2017-03-28 14:44:21 +02003690 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003691 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003692 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003693 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02003694 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003695 }
3696 break;
3697 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003698 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003699 ret = 0;
3700 break;
3701 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08003702 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08003703 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003704 break;
3705 default:
3706 ret = -EINVAL;
3707 break;
3708 }
3709
Filipe Mananad3a53282019-01-08 11:42:09 +00003710 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003711 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003712out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003713 kfree(p);
3714 return ret;
3715}
3716
Jan Schmidtd7728c92011-07-07 16:48:38 +02003717static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3718{
3719 int ret = 0;
3720 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003721 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003722 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003723 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003724 struct inode_fs_paths *ipath = NULL;
3725 struct btrfs_path *path;
3726
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003727 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003728 return -EPERM;
3729
3730 path = btrfs_alloc_path();
3731 if (!path) {
3732 ret = -ENOMEM;
3733 goto out;
3734 }
3735
3736 ipa = memdup_user(arg, sizeof(*ipa));
3737 if (IS_ERR(ipa)) {
3738 ret = PTR_ERR(ipa);
3739 ipa = NULL;
3740 goto out;
3741 }
3742
3743 size = min_t(u32, ipa->size, 4096);
3744 ipath = init_ipath(size, root, path);
3745 if (IS_ERR(ipath)) {
3746 ret = PTR_ERR(ipath);
3747 ipath = NULL;
3748 goto out;
3749 }
3750
3751 ret = paths_from_inode(ipa->inum, ipath);
3752 if (ret < 0)
3753 goto out;
3754
3755 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003756 rel_ptr = ipath->fspath->val[i] -
3757 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003758 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003759 }
3760
Omar Sandoval718dc5f2017-08-22 23:46:05 -07003761 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3762 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003763 if (ret) {
3764 ret = -EFAULT;
3765 goto out;
3766 }
3767
3768out:
3769 btrfs_free_path(path);
3770 free_ipath(ipath);
3771 kfree(ipa);
3772
3773 return ret;
3774}
3775
3776static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3777{
3778 struct btrfs_data_container *inodes = ctx;
3779 const size_t c = 3 * sizeof(u64);
3780
3781 if (inodes->bytes_left >= c) {
3782 inodes->bytes_left -= c;
3783 inodes->val[inodes->elem_cnt] = inum;
3784 inodes->val[inodes->elem_cnt + 1] = offset;
3785 inodes->val[inodes->elem_cnt + 2] = root;
3786 inodes->elem_cnt += 3;
3787 } else {
3788 inodes->bytes_missing += c - inodes->bytes_left;
3789 inodes->bytes_left = 0;
3790 inodes->elem_missed += 3;
3791 }
3792
3793 return 0;
3794}
3795
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003796static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003797 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003798{
3799 int ret = 0;
3800 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003801 struct btrfs_ioctl_logical_ino_args *loi;
3802 struct btrfs_data_container *inodes = NULL;
3803 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003804 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003805
3806 if (!capable(CAP_SYS_ADMIN))
3807 return -EPERM;
3808
3809 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05303810 if (IS_ERR(loi))
3811 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003812
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003813 if (version == 1) {
3814 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04003815 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003816 } else {
3817 /* All reserved bits must be 0 for now */
3818 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3819 ret = -EINVAL;
3820 goto out_loi;
3821 }
3822 /* Only accept flags we have defined so far */
3823 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3824 ret = -EINVAL;
3825 goto out_loi;
3826 }
3827 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04003828 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003829 }
3830
Jan Schmidtd7728c92011-07-07 16:48:38 +02003831 path = btrfs_alloc_path();
3832 if (!path) {
3833 ret = -ENOMEM;
3834 goto out;
3835 }
3836
Jan Schmidtd7728c92011-07-07 16:48:38 +02003837 inodes = init_data_container(size);
3838 if (IS_ERR(inodes)) {
3839 ret = PTR_ERR(inodes);
3840 inodes = NULL;
3841 goto out;
3842 }
3843
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003844 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003845 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06003846 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003847 ret = -ENOENT;
3848 if (ret < 0)
3849 goto out;
3850
Omar Sandoval718dc5f2017-08-22 23:46:05 -07003851 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3852 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003853 if (ret)
3854 ret = -EFAULT;
3855
3856out:
3857 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02003858 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003859out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02003860 kfree(loi);
3861
3862 return ret;
3863}
3864
David Sterba008ef092018-03-21 02:05:27 +01003865void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003866 struct btrfs_ioctl_balance_args *bargs)
3867{
3868 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3869
3870 bargs->flags = bctl->flags;
3871
David Sterba3009a622018-03-21 01:31:04 +01003872 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003873 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3874 if (atomic_read(&fs_info->balance_pause_req))
3875 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003876 if (atomic_read(&fs_info->balance_cancel_req))
3877 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003878
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003879 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3880 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3881 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003882
David Sterba008ef092018-03-21 02:05:27 +01003883 spin_lock(&fs_info->balance_lock);
3884 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3885 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003886}
3887
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003888static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003889{
Al Viro496ad9a2013-01-23 17:07:38 -05003890 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003891 struct btrfs_fs_info *fs_info = root->fs_info;
3892 struct btrfs_ioctl_balance_args *bargs;
3893 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003894 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003895 int ret;
3896
3897 if (!capable(CAP_SYS_ADMIN))
3898 return -EPERM;
3899
Liu Boe54bfa312012-06-29 03:58:48 -06003900 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003901 if (ret)
3902 return ret;
3903
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003904again:
David Sterba171938e2017-03-28 14:44:21 +02003905 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003906 mutex_lock(&fs_info->balance_mutex);
3907 need_unlock = true;
3908 goto locked;
3909 }
3910
3911 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003912 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003913 * (1) some other op is running
3914 * (2) balance is running
3915 * (3) balance is paused -- special case (think resume)
3916 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003917 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003918 if (fs_info->balance_ctl) {
3919 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01003920 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003921 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01003922 /*
3923 * Lock released to allow other waiters to continue,
3924 * we'll reexamine the status again.
3925 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003926 mutex_lock(&fs_info->balance_mutex);
3927
3928 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01003929 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003930 /* this is (3) */
3931 need_unlock = false;
3932 goto locked;
3933 }
3934
3935 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003936 goto again;
3937 } else {
3938 /* this is (2) */
3939 mutex_unlock(&fs_info->balance_mutex);
3940 ret = -EINPROGRESS;
3941 goto out;
3942 }
3943 } else {
3944 /* this is (1) */
3945 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08003946 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003947 goto out;
3948 }
3949
3950locked:
David Sterba171938e2017-03-28 14:44:21 +02003951 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003952
3953 if (arg) {
3954 bargs = memdup_user(arg, sizeof(*bargs));
3955 if (IS_ERR(bargs)) {
3956 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003957 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003958 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003959
3960 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3961 if (!fs_info->balance_ctl) {
3962 ret = -ENOTCONN;
3963 goto out_bargs;
3964 }
3965
3966 bctl = fs_info->balance_ctl;
3967 spin_lock(&fs_info->balance_lock);
3968 bctl->flags |= BTRFS_BALANCE_RESUME;
3969 spin_unlock(&fs_info->balance_lock);
3970
3971 goto do_balance;
3972 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003973 } else {
3974 bargs = NULL;
3975 }
3976
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003977 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003978 ret = -EINPROGRESS;
3979 goto out_bargs;
3980 }
3981
David Sterba8d2db782015-11-04 15:38:29 +01003982 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003983 if (!bctl) {
3984 ret = -ENOMEM;
3985 goto out_bargs;
3986 }
3987
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003988 if (arg) {
3989 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3990 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3991 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3992
3993 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003994 } else {
3995 /* balance everything - no filters */
3996 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003997 }
3998
David Sterba8eb93452015-10-12 16:55:54 +02003999 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4000 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004001 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004002 }
4003
Ilya Dryomovde322262012-01-16 22:04:49 +02004004do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004005 /*
David Sterba149196a2018-03-20 20:23:09 +01004006 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4007 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4008 * restriper was paused all the way until unmount, in free_fs_info.
4009 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004010 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004011 need_unlock = false;
4012
David Sterba6fcf6e22018-05-07 17:44:03 +02004013 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004014 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004015
Filipe Mananad00c2d92019-01-08 11:42:01 +00004016 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004017 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4018 ret = -EFAULT;
4019 }
4020
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004021out_bctl:
4022 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004023out_bargs:
4024 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004025out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004026 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004027 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004028 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004029out:
Liu Boe54bfa312012-06-29 03:58:48 -06004030 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004031 return ret;
4032}
4033
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004034static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004035{
4036 if (!capable(CAP_SYS_ADMIN))
4037 return -EPERM;
4038
4039 switch (cmd) {
4040 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004041 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004042 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004043 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004044 }
4045
4046 return -EINVAL;
4047}
4048
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004049static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004050 void __user *arg)
4051{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004052 struct btrfs_ioctl_balance_args *bargs;
4053 int ret = 0;
4054
4055 if (!capable(CAP_SYS_ADMIN))
4056 return -EPERM;
4057
4058 mutex_lock(&fs_info->balance_mutex);
4059 if (!fs_info->balance_ctl) {
4060 ret = -ENOTCONN;
4061 goto out;
4062 }
4063
David Sterba8d2db782015-11-04 15:38:29 +01004064 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004065 if (!bargs) {
4066 ret = -ENOMEM;
4067 goto out;
4068 }
4069
David Sterba008ef092018-03-21 02:05:27 +01004070 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004071
4072 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4073 ret = -EFAULT;
4074
4075 kfree(bargs);
4076out:
4077 mutex_unlock(&fs_info->balance_mutex);
4078 return ret;
4079}
4080
Miao Xie905b0dd2012-11-26 08:50:11 +00004081static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004082{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004083 struct inode *inode = file_inode(file);
4084 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004085 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004086 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004087
4088 if (!capable(CAP_SYS_ADMIN))
4089 return -EPERM;
4090
Miao Xie905b0dd2012-11-26 08:50:11 +00004091 ret = mnt_want_write_file(file);
4092 if (ret)
4093 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004094
4095 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004096 if (IS_ERR(sa)) {
4097 ret = PTR_ERR(sa);
4098 goto drop_write;
4099 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004100
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004101 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004102
4103 switch (sa->cmd) {
4104 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004105 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004106 break;
4107 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004108 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004109 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004110 default:
4111 ret = -EINVAL;
4112 break;
4113 }
4114
Arne Jansen5d13a372011-09-14 15:53:51 +02004115 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004116 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004117drop_write:
4118 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004119 return ret;
4120}
4121
Miao Xie905b0dd2012-11-26 08:50:11 +00004122static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004123{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004124 struct inode *inode = file_inode(file);
4125 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4126 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004127 struct btrfs_ioctl_qgroup_assign_args *sa;
4128 struct btrfs_trans_handle *trans;
4129 int ret;
4130 int err;
4131
4132 if (!capable(CAP_SYS_ADMIN))
4133 return -EPERM;
4134
Miao Xie905b0dd2012-11-26 08:50:11 +00004135 ret = mnt_want_write_file(file);
4136 if (ret)
4137 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004138
4139 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004140 if (IS_ERR(sa)) {
4141 ret = PTR_ERR(sa);
4142 goto drop_write;
4143 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004144
4145 trans = btrfs_join_transaction(root);
4146 if (IS_ERR(trans)) {
4147 ret = PTR_ERR(trans);
4148 goto out;
4149 }
4150
Arne Jansen5d13a372011-09-14 15:53:51 +02004151 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004152 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004153 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004154 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004155 }
4156
Qu Wenruoe082f562015-02-27 16:24:28 +08004157 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004158 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004159 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004160 btrfs_handle_fs_error(fs_info, err,
4161 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004162 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004163 if (err && !ret)
4164 ret = err;
4165
4166out:
4167 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004168drop_write:
4169 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004170 return ret;
4171}
4172
Miao Xie905b0dd2012-11-26 08:50:11 +00004173static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004174{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004175 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004176 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004177 struct btrfs_ioctl_qgroup_create_args *sa;
4178 struct btrfs_trans_handle *trans;
4179 int ret;
4180 int err;
4181
4182 if (!capable(CAP_SYS_ADMIN))
4183 return -EPERM;
4184
Miao Xie905b0dd2012-11-26 08:50:11 +00004185 ret = mnt_want_write_file(file);
4186 if (ret)
4187 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004188
4189 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004190 if (IS_ERR(sa)) {
4191 ret = PTR_ERR(sa);
4192 goto drop_write;
4193 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004194
Miao Xied86e56c2012-11-15 11:35:41 +00004195 if (!sa->qgroupid) {
4196 ret = -EINVAL;
4197 goto out;
4198 }
4199
Arne Jansen5d13a372011-09-14 15:53:51 +02004200 trans = btrfs_join_transaction(root);
4201 if (IS_ERR(trans)) {
4202 ret = PTR_ERR(trans);
4203 goto out;
4204 }
4205
Arne Jansen5d13a372011-09-14 15:53:51 +02004206 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004207 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004208 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004209 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004210 }
4211
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004212 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004213 if (err && !ret)
4214 ret = err;
4215
4216out:
4217 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004218drop_write:
4219 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004220 return ret;
4221}
4222
Miao Xie905b0dd2012-11-26 08:50:11 +00004223static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004225 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004226 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004227 struct btrfs_ioctl_qgroup_limit_args *sa;
4228 struct btrfs_trans_handle *trans;
4229 int ret;
4230 int err;
4231 u64 qgroupid;
4232
4233 if (!capable(CAP_SYS_ADMIN))
4234 return -EPERM;
4235
Miao Xie905b0dd2012-11-26 08:50:11 +00004236 ret = mnt_want_write_file(file);
4237 if (ret)
4238 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004239
4240 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004241 if (IS_ERR(sa)) {
4242 ret = PTR_ERR(sa);
4243 goto drop_write;
4244 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004245
4246 trans = btrfs_join_transaction(root);
4247 if (IS_ERR(trans)) {
4248 ret = PTR_ERR(trans);
4249 goto out;
4250 }
4251
4252 qgroupid = sa->qgroupid;
4253 if (!qgroupid) {
4254 /* take the current subvol as qgroup */
4255 qgroupid = root->root_key.objectid;
4256 }
4257
Lu Fengqif0042d52018-07-18 14:45:35 +08004258 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004259
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004260 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004261 if (err && !ret)
4262 ret = err;
4263
4264out:
4265 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004266drop_write:
4267 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004268 return ret;
4269}
4270
Jan Schmidt2f232032013-04-25 16:04:51 +00004271static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4272{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004273 struct inode *inode = file_inode(file);
4274 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004275 struct btrfs_ioctl_quota_rescan_args *qsa;
4276 int ret;
4277
4278 if (!capable(CAP_SYS_ADMIN))
4279 return -EPERM;
4280
4281 ret = mnt_want_write_file(file);
4282 if (ret)
4283 return ret;
4284
4285 qsa = memdup_user(arg, sizeof(*qsa));
4286 if (IS_ERR(qsa)) {
4287 ret = PTR_ERR(qsa);
4288 goto drop_write;
4289 }
4290
4291 if (qsa->flags) {
4292 ret = -EINVAL;
4293 goto out;
4294 }
4295
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004296 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004297
4298out:
4299 kfree(qsa);
4300drop_write:
4301 mnt_drop_write_file(file);
4302 return ret;
4303}
4304
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004305static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4306 void __user *arg)
Jan Schmidt2f232032013-04-25 16:04:51 +00004307{
Jan Schmidt2f232032013-04-25 16:04:51 +00004308 struct btrfs_ioctl_quota_rescan_args *qsa;
4309 int ret = 0;
4310
4311 if (!capable(CAP_SYS_ADMIN))
4312 return -EPERM;
4313
David Sterba8d2db782015-11-04 15:38:29 +01004314 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004315 if (!qsa)
4316 return -ENOMEM;
4317
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004318 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00004319 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004320 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004321 }
4322
4323 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4324 ret = -EFAULT;
4325
4326 kfree(qsa);
4327 return ret;
4328}
4329
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004330static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4331 void __user *arg)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004332{
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004333 if (!capable(CAP_SYS_ADMIN))
4334 return -EPERM;
4335
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004336 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004337}
4338
Hugo Millsabccd002014-01-30 20:17:00 +00004339static long _btrfs_ioctl_set_received_subvol(struct file *file,
4340 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004341{
Al Viro496ad9a2013-01-23 17:07:38 -05004342 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004343 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004344 struct btrfs_root *root = BTRFS_I(inode)->root;
4345 struct btrfs_root_item *root_item = &root->root_item;
4346 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07004347 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004348 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004349 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004350
David Sterbabd60ea02014-01-16 15:50:22 +01004351 if (!inode_owner_or_capable(inode))
4352 return -EPERM;
4353
Alexander Block8ea05e32012-07-25 17:35:53 +02004354 ret = mnt_want_write_file(file);
4355 if (ret < 0)
4356 return ret;
4357
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004358 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004359
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004360 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004361 ret = -EINVAL;
4362 goto out;
4363 }
4364
4365 if (btrfs_root_readonly(root)) {
4366 ret = -EROFS;
4367 goto out;
4368 }
4369
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004370 /*
4371 * 1 - root item
4372 * 2 - uuid items (received uuid + subvol uuid)
4373 */
4374 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004375 if (IS_ERR(trans)) {
4376 ret = PTR_ERR(trans);
4377 trans = NULL;
4378 goto out;
4379 }
4380
4381 sa->rtransid = trans->transid;
4382 sa->rtime.sec = ct.tv_sec;
4383 sa->rtime.nsec = ct.tv_nsec;
4384
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004385 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4386 BTRFS_UUID_SIZE);
4387 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004388 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004389 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004390 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4391 root->root_key.objectid);
4392 if (ret && ret != -ENOENT) {
4393 btrfs_abort_transaction(trans, ret);
4394 btrfs_end_transaction(trans);
4395 goto out;
4396 }
4397 }
Alexander Block8ea05e32012-07-25 17:35:53 +02004398 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4399 btrfs_set_root_stransid(root_item, sa->stransid);
4400 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004401 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4402 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4403 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4404 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004405
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004406 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02004407 &root->root_key, &root->root_item);
4408 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004409 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004410 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004411 }
4412 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004413 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004414 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4415 root->root_key.objectid);
4416 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004417 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03004418 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004419 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004420 }
4421 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004422 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00004423out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004424 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00004425 mnt_drop_write_file(file);
4426 return ret;
4427}
4428
4429#ifdef CONFIG_64BIT
4430static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4431 void __user *arg)
4432{
4433 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4434 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4435 int ret = 0;
4436
4437 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304438 if (IS_ERR(args32))
4439 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00004440
David Sterba8d2db782015-11-04 15:38:29 +01004441 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03004442 if (!args64) {
4443 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00004444 goto out;
4445 }
4446
4447 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4448 args64->stransid = args32->stransid;
4449 args64->rtransid = args32->rtransid;
4450 args64->stime.sec = args32->stime.sec;
4451 args64->stime.nsec = args32->stime.nsec;
4452 args64->rtime.sec = args32->rtime.sec;
4453 args64->rtime.nsec = args32->rtime.nsec;
4454 args64->flags = args32->flags;
4455
4456 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4457 if (ret)
4458 goto out;
4459
4460 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4461 args32->stransid = args64->stransid;
4462 args32->rtransid = args64->rtransid;
4463 args32->stime.sec = args64->stime.sec;
4464 args32->stime.nsec = args64->stime.nsec;
4465 args32->rtime.sec = args64->rtime.sec;
4466 args32->rtime.nsec = args64->rtime.nsec;
4467 args32->flags = args64->flags;
4468
4469 ret = copy_to_user(arg, args32, sizeof(*args32));
4470 if (ret)
4471 ret = -EFAULT;
4472
4473out:
4474 kfree(args32);
4475 kfree(args64);
4476 return ret;
4477}
4478#endif
4479
4480static long btrfs_ioctl_set_received_subvol(struct file *file,
4481 void __user *arg)
4482{
4483 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4484 int ret = 0;
4485
4486 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304487 if (IS_ERR(sa))
4488 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004489
4490 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4491
4492 if (ret)
4493 goto out;
4494
Alexander Block8ea05e32012-07-25 17:35:53 +02004495 ret = copy_to_user(arg, sa, sizeof(*sa));
4496 if (ret)
4497 ret = -EFAULT;
4498
4499out:
4500 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02004501 return ret;
4502}
4503
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004504static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4505 void __user *arg)
jeff.liu867ab662013-01-05 02:48:01 +00004506{
Anand Jaina1b83ac2013-07-19 17:39:32 +08004507 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004508 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004509 char label[BTRFS_LABEL_SIZE];
4510
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004511 spin_lock(&fs_info->super_lock);
4512 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4513 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004514
4515 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004516
4517 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004518 btrfs_warn(fs_info,
4519 "label is too long, return the first %zu bytes",
4520 --len);
jeff.liu867ab662013-01-05 02:48:01 +00004521 }
4522
jeff.liu867ab662013-01-05 02:48:01 +00004523 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004524
4525 return ret ? -EFAULT : 0;
4526}
4527
jeff.liua8bfd4a2013-01-05 02:48:08 +00004528static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4529{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004530 struct inode *inode = file_inode(file);
4531 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4532 struct btrfs_root *root = BTRFS_I(inode)->root;
4533 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004534 struct btrfs_trans_handle *trans;
4535 char label[BTRFS_LABEL_SIZE];
4536 int ret;
4537
4538 if (!capable(CAP_SYS_ADMIN))
4539 return -EPERM;
4540
4541 if (copy_from_user(label, arg, sizeof(label)))
4542 return -EFAULT;
4543
4544 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004545 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004546 "unable to set label with more than %d bytes",
4547 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004548 return -EINVAL;
4549 }
4550
4551 ret = mnt_want_write_file(file);
4552 if (ret)
4553 return ret;
4554
jeff.liua8bfd4a2013-01-05 02:48:08 +00004555 trans = btrfs_start_transaction(root, 0);
4556 if (IS_ERR(trans)) {
4557 ret = PTR_ERR(trans);
4558 goto out_unlock;
4559 }
4560
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004561 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004562 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004563 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004564 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004565
4566out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004567 mnt_drop_write_file(file);
4568 return ret;
4569}
4570
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004571#define INIT_FEATURE_FLAGS(suffix) \
4572 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4573 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4574 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4575
David Sterbad5131b62016-02-17 15:26:27 +01004576int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004577{
David Sterba4d4ab6d2015-11-19 11:42:31 +01004578 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004579 INIT_FEATURE_FLAGS(SUPP),
4580 INIT_FEATURE_FLAGS(SAFE_SET),
4581 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4582 };
4583
4584 if (copy_to_user(arg, &features, sizeof(features)))
4585 return -EFAULT;
4586
4587 return 0;
4588}
4589
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004590static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4591 void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004592{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004593 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004594 struct btrfs_ioctl_feature_flags features;
4595
4596 features.compat_flags = btrfs_super_compat_flags(super_block);
4597 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4598 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4599
4600 if (copy_to_user(arg, &features, sizeof(features)))
4601 return -EFAULT;
4602
4603 return 0;
4604}
4605
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004606static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004607 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004608 u64 change_mask, u64 flags, u64 supported_flags,
4609 u64 safe_set, u64 safe_clear)
4610{
David Sterbaf10152b2019-08-01 19:07:55 +02004611 const char *type = btrfs_feature_set_name(set);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004612 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004613 u64 disallowed, unsupported;
4614 u64 set_mask = flags & change_mask;
4615 u64 clear_mask = ~flags & change_mask;
4616
4617 unsupported = set_mask & ~supported_flags;
4618 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004619 names = btrfs_printable_features(set, unsupported);
4620 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004621 btrfs_warn(fs_info,
4622 "this kernel does not support the %s feature bit%s",
4623 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004624 kfree(names);
4625 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004626 btrfs_warn(fs_info,
4627 "this kernel does not support %s bits 0x%llx",
4628 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004629 return -EOPNOTSUPP;
4630 }
4631
4632 disallowed = set_mask & ~safe_set;
4633 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004634 names = btrfs_printable_features(set, disallowed);
4635 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004636 btrfs_warn(fs_info,
4637 "can't set the %s feature bit%s while mounted",
4638 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004639 kfree(names);
4640 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004641 btrfs_warn(fs_info,
4642 "can't set %s bits 0x%llx while mounted",
4643 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004644 return -EPERM;
4645 }
4646
4647 disallowed = clear_mask & ~safe_clear;
4648 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004649 names = btrfs_printable_features(set, disallowed);
4650 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 btrfs_warn(fs_info,
4652 "can't clear the %s feature bit%s while mounted",
4653 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004654 kfree(names);
4655 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004656 btrfs_warn(fs_info,
4657 "can't clear %s bits 0x%llx while mounted",
4658 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004659 return -EPERM;
4660 }
4661
4662 return 0;
4663}
4664
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004665#define check_feature(fs_info, change_mask, flags, mask_base) \
4666check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004667 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4668 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4669 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4670
4671static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4672{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004673 struct inode *inode = file_inode(file);
4674 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4675 struct btrfs_root *root = BTRFS_I(inode)->root;
4676 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004677 struct btrfs_ioctl_feature_flags flags[2];
4678 struct btrfs_trans_handle *trans;
4679 u64 newflags;
4680 int ret;
4681
4682 if (!capable(CAP_SYS_ADMIN))
4683 return -EPERM;
4684
4685 if (copy_from_user(flags, arg, sizeof(flags)))
4686 return -EFAULT;
4687
4688 /* Nothing to do */
4689 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4690 !flags[0].incompat_flags)
4691 return 0;
4692
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004693 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004694 flags[1].compat_flags, COMPAT);
4695 if (ret)
4696 return ret;
4697
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004698 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004699 flags[1].compat_ro_flags, COMPAT_RO);
4700 if (ret)
4701 return ret;
4702
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004703 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004704 flags[1].incompat_flags, INCOMPAT);
4705 if (ret)
4706 return ret;
4707
David Sterba7ab19622016-05-04 11:32:00 +02004708 ret = mnt_want_write_file(file);
4709 if (ret)
4710 return ret;
4711
David Sterba8051aa12014-02-07 14:34:04 +01004712 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02004713 if (IS_ERR(trans)) {
4714 ret = PTR_ERR(trans);
4715 goto out_drop_write;
4716 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004717
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004718 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004719 newflags = btrfs_super_compat_flags(super_block);
4720 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4721 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4722 btrfs_set_super_compat_flags(super_block, newflags);
4723
4724 newflags = btrfs_super_compat_ro_flags(super_block);
4725 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4726 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4727 btrfs_set_super_compat_ro_flags(super_block, newflags);
4728
4729 newflags = btrfs_super_incompat_flags(super_block);
4730 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4731 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4732 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004733 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004734
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004735 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02004736out_drop_write:
4737 mnt_drop_write_file(file);
4738
4739 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004740}
4741
Josef Bacik2351f432017-09-27 10:43:13 -04004742static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4743{
4744 struct btrfs_ioctl_send_args *arg;
4745 int ret;
4746
4747 if (compat) {
4748#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4749 struct btrfs_ioctl_send_args_32 args32;
4750
4751 ret = copy_from_user(&args32, argp, sizeof(args32));
4752 if (ret)
4753 return -EFAULT;
4754 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4755 if (!arg)
4756 return -ENOMEM;
4757 arg->send_fd = args32.send_fd;
4758 arg->clone_sources_count = args32.clone_sources_count;
4759 arg->clone_sources = compat_ptr(args32.clone_sources);
4760 arg->parent_root = args32.parent_root;
4761 arg->flags = args32.flags;
4762 memcpy(arg->reserved, args32.reserved,
4763 sizeof(args32.reserved));
4764#else
4765 return -ENOTTY;
4766#endif
4767 } else {
4768 arg = memdup_user(argp, sizeof(*arg));
4769 if (IS_ERR(arg))
4770 return PTR_ERR(arg);
4771 }
4772 ret = btrfs_ioctl_send(file, arg);
4773 kfree(arg);
4774 return ret;
4775}
4776
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004777long btrfs_ioctl(struct file *file, unsigned int
4778 cmd, unsigned long arg)
4779{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 struct inode *inode = file_inode(file);
4781 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4782 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004783 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004784
4785 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004786 case FS_IOC_GETFLAGS:
4787 return btrfs_ioctl_getflags(file, argp);
4788 case FS_IOC_SETFLAGS:
4789 return btrfs_ioctl_setflags(file, argp);
4790 case FS_IOC_GETVERSION:
4791 return btrfs_ioctl_getversion(file, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05004792 case FS_IOC_GETFSLABEL:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004793 return btrfs_ioctl_get_fslabel(fs_info, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05004794 case FS_IOC_SETFSLABEL:
4795 return btrfs_ioctl_set_fslabel(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004796 case FITRIM:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004797 return btrfs_ioctl_fitrim(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004798 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004799 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004800 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004801 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004802 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004803 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004804 case BTRFS_IOC_SUBVOL_CREATE_V2:
4805 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004806 case BTRFS_IOC_SNAP_DESTROY:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03004807 return btrfs_ioctl_snap_destroy(file, argp, false);
4808 case BTRFS_IOC_SNAP_DESTROY_V2:
4809 return btrfs_ioctl_snap_destroy(file, argp, true);
Li Zefan0caa1022010-12-20 16:30:25 +08004810 case BTRFS_IOC_SUBVOL_GETFLAGS:
4811 return btrfs_ioctl_subvol_getflags(file, argp);
4812 case BTRFS_IOC_SUBVOL_SETFLAGS:
4813 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004814 case BTRFS_IOC_DEFAULT_SUBVOL:
4815 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004816 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004817 return btrfs_ioctl_defrag(file, NULL);
4818 case BTRFS_IOC_DEFRAG_RANGE:
4819 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004820 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004821 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004822 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004823 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004824 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004825 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08004826 case BTRFS_IOC_RM_DEV_V2:
4827 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004828 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004829 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004830 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004831 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004832 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004833 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05004834 case BTRFS_IOC_TREE_SEARCH:
4835 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01004836 case BTRFS_IOC_TREE_SEARCH_V2:
4837 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05004838 case BTRFS_IOC_INO_LOOKUP:
4839 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004840 case BTRFS_IOC_INO_PATHS:
4841 return btrfs_ioctl_ino_to_path(root, argp);
4842 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004843 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4844 case BTRFS_IOC_LOGICAL_INO_V2:
4845 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00004846 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004847 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004848 case BTRFS_IOC_SYNC: {
4849 int ret;
4850
Nikolay Borisov82b3e532018-04-23 10:54:13 +03004851 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004852 if (ret)
4853 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004854 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02004855 /*
4856 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004857 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02004858 * processing uncleaned subvols.
4859 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004860 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004861 return ret;
4862 }
Sage Weil46204592010-10-29 15:41:32 -04004863 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004864 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004865 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004866 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004867 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004868 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004869 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004870 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004871 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004872 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004873 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004874 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004875 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004876 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004877 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004878 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004879 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4880 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00004881#ifdef CONFIG_64BIT
4882 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4883 return btrfs_ioctl_set_received_subvol_32(file, argp);
4884#endif
Alexander Block31db9f72012-07-25 23:19:24 +02004885 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04004886 return _btrfs_ioctl_send(file, argp, false);
4887#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4888 case BTRFS_IOC_SEND_32:
4889 return _btrfs_ioctl_send(file, argp, true);
4890#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004891 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004892 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004893 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004894 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004895 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004896 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004897 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004898 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004899 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004900 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004901 case BTRFS_IOC_QUOTA_RESCAN:
4902 return btrfs_ioctl_quota_rescan(file, argp);
4903 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004904 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004905 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004906 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004907 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004908 return btrfs_ioctl_dev_replace(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004909 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01004910 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004911 case BTRFS_IOC_GET_FEATURES:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004912 return btrfs_ioctl_get_features(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004913 case BTRFS_IOC_SET_FEATURES:
4914 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02004915 case FS_IOC_FSGETXATTR:
4916 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02004917 case FS_IOC_FSSETXATTR:
4918 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09004919 case BTRFS_IOC_GET_SUBVOL_INFO:
4920 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09004921 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
4922 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09004923 case BTRFS_IOC_INO_LOOKUP_USER:
4924 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004925 }
4926
4927 return -ENOTTY;
4928}
Luke Dashjr4c63c242015-10-29 08:22:21 +00004929
4930#ifdef CONFIG_COMPAT
4931long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4932{
Jeff Mahoney2a362242017-02-06 19:39:09 -05004933 /*
4934 * These all access 32-bit values anyway so no further
4935 * handling is necessary.
4936 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00004937 switch (cmd) {
4938 case FS_IOC32_GETFLAGS:
4939 cmd = FS_IOC_GETFLAGS;
4940 break;
4941 case FS_IOC32_SETFLAGS:
4942 cmd = FS_IOC_SETFLAGS;
4943 break;
4944 case FS_IOC32_GETVERSION:
4945 cmd = FS_IOC_GETVERSION;
4946 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00004947 }
4948
4949 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
4950}
4951#endif