blob: 56bd3ea7fb67fe7f4c5809aaf9c38405ac8bf464 [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
Mark Fasheh416161d2013-08-06 11:42:51 -070090static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070091 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
92 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070093
Christoph Hellwig6cbff002009-04-17 10:37:41 +020094/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020095static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
96 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097{
David Sterba1905a0f2018-03-26 18:52:15 +020098 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020099 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +0200100 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200101 return flags & ~FS_DIRSYNC_FL;
102 else
103 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
104}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400105
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200106/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200107 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
108 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200109 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200110static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200111{
112 unsigned int iflags = 0;
113
114 if (flags & BTRFS_INODE_SYNC)
115 iflags |= FS_SYNC_FL;
116 if (flags & BTRFS_INODE_IMMUTABLE)
117 iflags |= FS_IMMUTABLE_FL;
118 if (flags & BTRFS_INODE_APPEND)
119 iflags |= FS_APPEND_FL;
120 if (flags & BTRFS_INODE_NODUMP)
121 iflags |= FS_NODUMP_FL;
122 if (flags & BTRFS_INODE_NOATIME)
123 iflags |= FS_NOATIME_FL;
124 if (flags & BTRFS_INODE_DIRSYNC)
125 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000126 if (flags & BTRFS_INODE_NODATACOW)
127 iflags |= FS_NOCOW_FL;
128
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900129 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000130 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900131 else if (flags & BTRFS_INODE_COMPRESS)
132 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200133
134 return iflags;
135}
136
137/*
138 * Update inode->i_flags based on the btrfs internal flags.
139 */
David Sterba7b6a2212018-03-26 18:40:21 +0200140void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200141{
David Sterba5c57b8b2018-04-23 15:45:18 +0200142 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100143 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200144
David Sterba5c57b8b2018-04-23 15:45:18 +0200145 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200147 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200149 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200151 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100152 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200153 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100154 new_fl |= S_DIRSYNC;
155
156 set_mask_bits(&inode->i_flags,
157 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
158 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200159}
160
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200161static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
162{
David Sterba5c57b8b2018-04-23 15:45:18 +0200163 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
164 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200165
166 if (copy_to_user(arg, &flags, sizeof(flags)))
167 return -EFAULT;
168 return 0;
169}
170
David Sterba5ba76ab2018-03-26 18:52:15 +0200171/* Check if @flags are a supported and valid set of FS_*_FL flags */
172static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000173{
174 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
175 FS_NOATIME_FL | FS_NODUMP_FL | \
176 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000177 FS_NOCOMP_FL | FS_COMPR_FL |
178 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000179 return -EOPNOTSUPP;
180
181 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
182 return -EINVAL;
183
Liu Bo75e7cb72011-03-22 10:12:20 +0000184 return 0;
185}
186
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200187static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
188{
Al Viro496ad9a2013-01-23 17:07:38 -0500189 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400190 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200191 struct btrfs_inode *binode = BTRFS_I(inode);
192 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193 struct btrfs_trans_handle *trans;
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700194 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200195 int ret;
Anand Jainff9fef552019-04-20 19:48:53 +0800196 const char *comp = NULL;
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800197 u32 binode_flags = binode->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200198
David Sterbabd60ea02014-01-16 15:50:22 +0100199 if (!inode_owner_or_capable(inode))
200 return -EPERM;
201
Li Zefanb83cc962010-12-20 16:04:08 +0800202 if (btrfs_root_readonly(root))
203 return -EROFS;
204
David Sterba5c57b8b2018-04-23 15:45:18 +0200205 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200206 return -EFAULT;
207
David Sterba5c57b8b2018-04-23 15:45:18 +0200208 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000209 if (ret)
210 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200211
Jan Karae7848682012-06-12 16:20:32 +0200212 ret = mnt_want_write_file(file);
213 if (ret)
214 return ret;
215
Al Viro59551022016-01-22 15:40:57 -0500216 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200217
David Sterba5c57b8b2018-04-23 15:45:18 +0200218 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700219 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
220 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
221 if (ret)
222 goto out_unlock;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200223
David Sterba5c57b8b2018-04-23 15:45:18 +0200224 if (fsflags & FS_SYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800225 binode_flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800227 binode_flags &= ~BTRFS_INODE_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200228 if (fsflags & FS_IMMUTABLE_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800229 binode_flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200230 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800231 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200232 if (fsflags & FS_APPEND_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800233 binode_flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200234 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800235 binode_flags &= ~BTRFS_INODE_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200236 if (fsflags & FS_NODUMP_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800237 binode_flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800239 binode_flags &= ~BTRFS_INODE_NODUMP;
David Sterba5c57b8b2018-04-23 15:45:18 +0200240 if (fsflags & FS_NOATIME_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800241 binode_flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200242 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800243 binode_flags &= ~BTRFS_INODE_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200244 if (fsflags & FS_DIRSYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800245 binode_flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200246 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800247 binode_flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200248 if (fsflags & FS_NOCOW_FL) {
Anand Jain44e51942019-04-20 19:48:57 +0800249 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600250 /*
251 * It's safe to turn csums off here, no extents exist.
252 * Otherwise we want the flag to reflect the real COW
253 * status of the file and will not set it.
254 */
255 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800256 binode_flags |= BTRFS_INODE_NODATACOW |
257 BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600258 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800259 binode_flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600260 }
261 } else {
262 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400263 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600264 */
Anand Jain44e51942019-04-20 19:48:57 +0800265 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600266 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800267 binode_flags &= ~(BTRFS_INODE_NODATACOW |
268 BTRFS_INODE_NODATASUM);
David Sterba7e97b8d2012-09-07 05:56:55 -0600269 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800270 binode_flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600271 }
272 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200273
Liu Bo75e7cb72011-03-22 10:12:20 +0000274 /*
275 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
276 * flag may be changed automatically if compression code won't make
277 * things smaller.
278 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200279 if (fsflags & FS_NOCOMP_FL) {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800280 binode_flags &= ~BTRFS_INODE_COMPRESS;
281 binode_flags |= BTRFS_INODE_NOCOMPRESS;
David Sterba5c57b8b2018-04-23 15:45:18 +0200282 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000283
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700284 if (IS_SWAPFILE(inode)) {
285 ret = -ETXTBSY;
286 goto out_unlock;
287 }
288
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800289 binode_flags |= BTRFS_INODE_COMPRESS;
290 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000291
David Sterba93370502017-10-31 17:32:41 +0100292 comp = btrfs_compress_type2str(fs_info->compress_type);
293 if (!comp || comp[0] == 0)
294 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
Li Zefanebcb9042011-04-15 03:03:17 +0000295 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800296 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000297 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200298
Anand Jainff9fef552019-04-20 19:48:53 +0800299 /*
300 * 1 for inode item
301 * 2 for properties
302 */
303 trans = btrfs_start_transaction(root, 3);
Li Zefanf062abf02011-12-29 13:36:45 +0800304 if (IS_ERR(trans)) {
305 ret = PTR_ERR(trans);
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800306 goto out_unlock;
Li Zefanf062abf02011-12-29 13:36:45 +0800307 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200308
Anand Jainff9fef552019-04-20 19:48:53 +0800309 if (comp) {
310 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
311 strlen(comp), 0);
312 if (ret) {
313 btrfs_abort_transaction(trans, ret);
314 goto out_end_trans;
315 }
Anand Jainff9fef552019-04-20 19:48:53 +0800316 } else {
317 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
318 0, 0);
319 if (ret && ret != -ENODATA) {
320 btrfs_abort_transaction(trans, ret);
321 goto out_end_trans;
322 }
323 }
324
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800325 binode->flags = binode_flags;
David Sterba7b6a2212018-03-26 18:40:21 +0200326 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400327 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700328 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200329 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200330
Anand Jainff9fef552019-04-20 19:48:53 +0800331 out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400332 btrfs_end_transaction(trans);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200333 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500334 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200335 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000336 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200337}
338
David Sterba19f93b32018-03-26 19:42:05 +0200339/*
340 * Translate btrfs internal inode flags to xflags as expected by the
341 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
342 * silently dropped.
343 */
344static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
345{
346 unsigned int xflags = 0;
347
348 if (flags & BTRFS_INODE_APPEND)
349 xflags |= FS_XFLAG_APPEND;
350 if (flags & BTRFS_INODE_IMMUTABLE)
351 xflags |= FS_XFLAG_IMMUTABLE;
352 if (flags & BTRFS_INODE_NOATIME)
353 xflags |= FS_XFLAG_NOATIME;
354 if (flags & BTRFS_INODE_NODUMP)
355 xflags |= FS_XFLAG_NODUMP;
356 if (flags & BTRFS_INODE_SYNC)
357 xflags |= FS_XFLAG_SYNC;
358
359 return xflags;
360}
361
362/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
363static int check_xflags(unsigned int flags)
364{
365 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
366 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
367 return -EOPNOTSUPP;
368 return 0;
369}
370
David Sterbae4202ac2018-03-26 19:51:16 +0200371/*
372 * Set the xflags from the internal inode flags. The remaining items of fsxattr
373 * are zeroed.
374 */
375static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
376{
377 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
378 struct fsxattr fa;
379
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700380 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
David Sterbae4202ac2018-03-26 19:51:16 +0200381 if (copy_to_user(arg, &fa, sizeof(fa)))
382 return -EFAULT;
383
384 return 0;
385}
386
David Sterba025f2122018-03-26 19:51:16 +0200387static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
388{
389 struct inode *inode = file_inode(file);
390 struct btrfs_inode *binode = BTRFS_I(inode);
391 struct btrfs_root *root = binode->root;
392 struct btrfs_trans_handle *trans;
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700393 struct fsxattr fa, old_fa;
David Sterba025f2122018-03-26 19:51:16 +0200394 unsigned old_flags;
395 unsigned old_i_flags;
396 int ret = 0;
397
398 if (!inode_owner_or_capable(inode))
399 return -EPERM;
400
401 if (btrfs_root_readonly(root))
402 return -EROFS;
403
David Sterba025f2122018-03-26 19:51:16 +0200404 if (copy_from_user(&fa, arg, sizeof(fa)))
405 return -EFAULT;
406
407 ret = check_xflags(fa.fsx_xflags);
408 if (ret)
409 return ret;
410
411 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
412 return -EOPNOTSUPP;
413
414 ret = mnt_want_write_file(file);
415 if (ret)
416 return ret;
417
418 inode_lock(inode);
419
420 old_flags = binode->flags;
421 old_i_flags = inode->i_flags;
422
Darrick J. Wong7b0e4922019-07-01 08:25:35 -0700423 simple_fill_fsxattr(&old_fa,
424 btrfs_inode_flags_to_xflags(binode->flags));
425 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
426 if (ret)
David Sterba025f2122018-03-26 19:51:16 +0200427 goto out_unlock;
David Sterba025f2122018-03-26 19:51:16 +0200428
429 if (fa.fsx_xflags & FS_XFLAG_SYNC)
430 binode->flags |= BTRFS_INODE_SYNC;
431 else
432 binode->flags &= ~BTRFS_INODE_SYNC;
433 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
434 binode->flags |= BTRFS_INODE_IMMUTABLE;
435 else
436 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
437 if (fa.fsx_xflags & FS_XFLAG_APPEND)
438 binode->flags |= BTRFS_INODE_APPEND;
439 else
440 binode->flags &= ~BTRFS_INODE_APPEND;
441 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
442 binode->flags |= BTRFS_INODE_NODUMP;
443 else
444 binode->flags &= ~BTRFS_INODE_NODUMP;
445 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
446 binode->flags |= BTRFS_INODE_NOATIME;
447 else
448 binode->flags &= ~BTRFS_INODE_NOATIME;
449
450 /* 1 item for the inode */
451 trans = btrfs_start_transaction(root, 1);
452 if (IS_ERR(trans)) {
453 ret = PTR_ERR(trans);
454 goto out_unlock;
455 }
456
457 btrfs_sync_inode_flags_to_i_flags(inode);
458 inode_inc_iversion(inode);
459 inode->i_ctime = current_time(inode);
460 ret = btrfs_update_inode(trans, root, inode);
461
462 btrfs_end_transaction(trans);
463
464out_unlock:
465 if (ret) {
466 binode->flags = old_flags;
467 inode->i_flags = old_i_flags;
468 }
469
470 inode_unlock(inode);
471 mnt_drop_write_file(file);
472
473 return ret;
474}
475
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200476static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
477{
Al Viro496ad9a2013-01-23 17:07:38 -0500478 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200479
480 return put_user(inode->i_generation, arg);
481}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400482
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -0300483static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
484 void __user *arg)
Li Dongyangf7039b12011-03-24 10:24:28 +0000485{
Li Dongyangf7039b12011-03-24 10:24:28 +0000486 struct btrfs_device *device;
487 struct request_queue *q;
488 struct fstrim_range range;
489 u64 minlen = ULLONG_MAX;
490 u64 num_devices = 0;
491 int ret;
492
493 if (!capable(CAP_SYS_ADMIN))
494 return -EPERM;
495
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000496 /*
497 * If the fs is mounted with nologreplay, which requires it to be
498 * mounted in RO mode as well, we can not allow discard on free space
499 * inside block groups, because log trees refer to extents that are not
500 * pinned in a block group's free space cache (pinning the extents is
501 * precisely the first phase of replaying a log tree).
502 */
503 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
504 return -EROFS;
505
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506 rcu_read_lock();
507 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
508 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000509 if (!device->bdev)
510 continue;
511 q = bdev_get_queue(device->bdev);
512 if (blk_queue_discard(q)) {
513 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100514 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000515 minlen);
516 }
517 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000518 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200519
Li Dongyangf7039b12011-03-24 10:24:28 +0000520 if (!num_devices)
521 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000522 if (copy_from_user(&range, arg, sizeof(range)))
523 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800524
525 /*
526 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
527 * block group is in the logical address space, which can be any
528 * sectorsize aligned bytenr in the range [0, U64_MAX].
529 */
530 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200531 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000532
533 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400534 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000535 if (ret < 0)
536 return ret;
537
538 if (copy_to_user(arg, &range, sizeof(range)))
539 return -EFAULT;
540
541 return 0;
542}
543
David Sterbae1f60a62019-10-01 19:57:39 +0200544int __pure btrfs_is_empty_uuid(u8 *uuid)
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200545{
Chris Mason46e0f662013-11-15 12:14:55 +0100546 int i;
547
548 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
549 if (uuid[i])
550 return 0;
551 }
552 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200553}
554
Miao Xied5c12072013-02-28 10:04:33 +0000555static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400556 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100557 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200558 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000559 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400561 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400562 struct btrfs_trans_handle *trans;
563 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100564 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400565 struct btrfs_inode_item *inode_item;
566 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000567 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400568 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000569 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700570 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900571 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400572 int ret;
573 int err;
574 u64 objectid;
575 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500576 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200577 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400578
David Sterba49a3c4d2016-03-24 17:49:22 +0100579 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
580 if (!root_item)
581 return -ENOMEM;
582
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400583 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400584 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100585 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000586
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800587 /*
588 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400589 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800590 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100591 if (btrfs_qgroup_level(objectid)) {
592 ret = -ENOSPC;
593 goto fail_free;
594 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800595
Miao Xied5c12072013-02-28 10:04:33 +0000596 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400597 /*
Miao Xied5c12072013-02-28 10:04:33 +0000598 * The same as the snapshot creation, please see the comment
599 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400600 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800601 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000602 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100603 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400604
Miao Xied5c12072013-02-28 10:04:33 +0000605 trans = btrfs_start_transaction(root, 0);
606 if (IS_ERR(trans)) {
607 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100608 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100609 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000610 }
611 trans->block_rsv = &block_rsv;
612 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400613
Lu Fengqia93774222018-07-18 14:45:41 +0800614 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200615 if (ret)
616 goto fail;
617
David Sterba4d75f8a2014-06-15 01:54:12 +0200618 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400619 if (IS_ERR(leaf)) {
620 ret = PTR_ERR(leaf);
621 goto fail;
622 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400624 btrfs_mark_buffer_dirty(leaf);
625
David Sterba49a3c4d2016-03-24 17:49:22 +0100626 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800627 btrfs_set_stack_inode_generation(inode_item, 1);
628 btrfs_set_stack_inode_size(inode_item, 3);
629 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400630 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400631 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800632 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400633
David Sterba49a3c4d2016-03-24 17:49:22 +0100634 btrfs_set_root_flags(root_item, 0);
635 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800636 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000637
David Sterba49a3c4d2016-03-24 17:49:22 +0100638 btrfs_set_root_bytenr(root_item, leaf->start);
639 btrfs_set_root_generation(root_item, trans->transid);
640 btrfs_set_root_level(root_item, 0);
641 btrfs_set_root_refs(root_item, 1);
642 btrfs_set_root_used(root_item, leaf->len);
643 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400644
David Sterba49a3c4d2016-03-24 17:49:22 +0100645 btrfs_set_root_generation_v2(root_item,
646 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200647 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100648 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
649 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
650 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
651 root_item->ctime = root_item->otime;
652 btrfs_set_root_ctransid(root_item, trans->transid);
653 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654
Chris Mason925baed2008-06-25 16:01:30 -0400655 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656 free_extent_buffer(leaf);
657 leaf = NULL;
658
David Sterba49a3c4d2016-03-24 17:49:22 +0100659 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400660
661 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400662 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200663 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400664 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100665 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666 if (ret)
667 goto fail;
668
Yan, Zheng76dda932009-09-21 16:00:26 -0400669 key.offset = (u64)-1;
Josef Bacik3619c942020-01-24 09:32:24 -0500670 new_root = btrfs_get_fs_root(fs_info, &key, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100672 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400673 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100674 goto fail;
675 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400676
677 btrfs_record_root_in_trans(trans, new_root);
678
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000679 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Josef Bacik00246522020-01-24 09:33:01 -0500680 btrfs_put_root(new_root);
Mark Fashehce598972011-07-26 11:32:23 -0700681 if (ret) {
682 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400683 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700684 goto fail;
685 }
686
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530687 mutex_lock(&new_root->objectid_mutex);
688 new_root->highest_objectid = new_dirid;
689 mutex_unlock(&new_root->objectid_mutex);
690
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400691 /*
692 * insert the directory item
693 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200694 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100695 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400696 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100697 goto fail;
698 }
Chris Mason3de45862008-11-17 21:02:50 -0500699
Lu Fengqi684572d2018-08-04 21:10:57 +0800700 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500701 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100702 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400703 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400704 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100705 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500706
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200707 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500708 ret = btrfs_update_inode(trans, root, dir);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500709 if (ret) {
710 btrfs_abort_transaction(trans, ret);
711 goto fail;
712 }
Yan Zheng52c26172009-01-05 15:43:43 -0500713
Lu Fengqi6025c192018-08-01 11:32:29 +0800714 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200715 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500716 if (ret) {
717 btrfs_abort_transaction(trans, ret);
718 goto fail;
719 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500720
Lu Fengqicdb345a2018-05-29 15:01:53 +0800721 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400722 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200723 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400724 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200725
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100727 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000728 trans->block_rsv = NULL;
729 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100730 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800731
Sage Weil72fd0322010-10-29 15:41:32 -0400732 if (async_transid) {
733 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400734 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000735 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400736 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400737 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400738 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400739 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400740 if (err && !ret)
741 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500742
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900743 if (!ret) {
744 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800745 if (IS_ERR(inode))
746 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900747 d_instantiate(dentry, inode);
748 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100750
751fail_free:
752 kfree(root_item);
753 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400754}
755
Miao Xiee9662f72013-02-28 10:01:15 +0000756static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100757 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000758 u64 *async_transid, bool readonly,
759 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400761 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000762 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763 struct btrfs_pending_snapshot *pending_snapshot;
764 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000765 int ret;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800766 bool snapshot_force_cow = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400767
Miao Xie27cdeb72014-04-02 19:51:05 +0800768 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400769 return -EINVAL;
770
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700771 if (atomic_read(&root->nr_swapfiles)) {
772 btrfs_warn(fs_info,
773 "cannot snapshot subvolume with active swapfile");
774 return -ETXTBSY;
775 }
776
David Sterba23269bf2017-02-13 11:03:44 +0100777 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100778 if (!pending_snapshot)
779 return -ENOMEM;
780
David Sterbab0c0ea62015-11-10 18:54:00 +0100781 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100782 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100783 pending_snapshot->path = btrfs_alloc_path();
784 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100785 ret = -ENOMEM;
786 goto free_pending;
787 }
788
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800789 /*
790 * Force new buffered writes to reserve space even when NOCOW is
791 * possible. This is to avoid later writeback (running dealloc) to
792 * fallback to COW mode and unexpectedly fail with ENOSPC.
793 */
David Sterbaea14b57f2017-06-22 02:19:11 +0200794 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100795 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600796 /* wait for no snapshot writes */
797 wait_event(root->subv_writers->wait,
798 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800799
Ethan Lien3cd24c62018-11-01 14:49:03 +0800800 ret = btrfs_start_delalloc_snapshot(root);
Miao Xie6a038432013-05-15 07:48:24 +0000801 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100802 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000803
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800804 /*
805 * All previous writes have started writeback in NOCOW mode, so now
806 * we force future writes to fallback to COW mode during snapshot
807 * creation.
808 */
809 atomic_inc(&root->snapshot_force_cow);
810 snapshot_force_cow = true;
811
Chris Mason6374e57a2017-06-23 09:48:21 -0700812 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000813
Miao Xie66d8f3d2012-09-06 04:02:28 -0600814 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
815 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000816 /*
817 * 1 - parent dir inode
818 * 2 - dir entries
819 * 1 - root item
820 * 2 - root ref/backref
821 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200822 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000823 */
824 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200825 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400826 false);
Miao Xied5c12072013-02-28 10:04:33 +0000827 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100828 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000829
Yan, Zhenga22285a2010-05-16 10:48:46 -0400830 pending_snapshot->dentry = dentry;
831 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800832 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000833 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000834 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400835
Miao Xied5c12072013-02-28 10:04:33 +0000836 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400837 if (IS_ERR(trans)) {
838 ret = PTR_ERR(trans);
839 goto fail;
840 }
841
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400842 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400843 list_add(&pending_snapshot->list,
844 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400845 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400846 if (async_transid) {
847 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400848 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000849 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400850 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400851 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400852 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400853 }
Miao Xieaec80302013-03-04 09:44:29 +0000854 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400855 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400856
857 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000859 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400860
Chris Masond3797302014-10-15 13:50:56 -0700861 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
862 if (ret)
863 goto fail;
864
David Howells2b0143b2015-03-17 22:25:59 +0000865 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000866 if (IS_ERR(inode)) {
867 ret = PTR_ERR(inode);
868 goto fail;
869 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900870
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000871 d_instantiate(dentry, inode);
872 ret = 0;
873fail:
Josef Bacik00246522020-01-24 09:33:01 -0500874 btrfs_put_root(pending_snapshot->snap);
David Sterba7775c812017-02-10 19:18:18 +0100875 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100876dec_and_free:
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800877 if (snapshot_force_cow)
878 atomic_dec(&root->snapshot_force_cow);
David Sterbaea14b57f2017-06-22 02:19:11 +0200879 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100880 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100881free_pending:
882 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100883 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100884 kfree(pending_snapshot);
885
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400886 return ret;
887}
888
Sage Weil4260f7c2010-10-29 15:46:43 -0400889/* copy of may_delete in fs/namei.c()
890 * Check whether we can remove a link victim from directory dir, check
891 * whether the type of victim is right.
892 * 1. We can't do it if dir is read-only (done in permission())
893 * 2. We should have write and exec permissions on dir
894 * 3. We can't remove anything from append-only dir
895 * 4. We can't do anything with immutable dir (done in permission())
896 * 5. If the sticky bit on dir is set we should either
897 * a. be owner of dir, or
898 * b. be owner of victim, or
899 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400900 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400901 * links pointing to it.
902 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
903 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
904 * 9. We can't remove a root or mountpoint.
905 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
906 * nfs_async_unlink().
907 */
908
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530909static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400910{
911 int error;
912
David Howells2b0143b2015-03-17 22:25:59 +0000913 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400914 return -ENOENT;
915
David Howells2b0143b2015-03-17 22:25:59 +0000916 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400917 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400918
919 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
920 if (error)
921 return error;
922 if (IS_APPEND(dir))
923 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000924 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
925 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400926 return -EPERM;
927 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000928 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400929 return -ENOTDIR;
930 if (IS_ROOT(victim))
931 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000932 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400933 return -EISDIR;
934 if (IS_DEADDIR(dir))
935 return -ENOENT;
936 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
937 return -EBUSY;
938 return 0;
939}
940
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400941/* copy of may_create in fs/namei.c() */
942static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
943{
David Howells2b0143b2015-03-17 22:25:59 +0000944 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400945 return -EEXIST;
946 if (IS_DEADDIR(dir))
947 return -ENOENT;
948 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
949}
950
951/*
952 * Create a new subvolume below @parent. This is largely modeled after
953 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
954 * inside this filesystem so it's quite a bit simpler.
955 */
Al Viro92872092016-11-20 19:34:31 -0500956static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100957 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400958 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200959 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000960 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400962 struct inode *dir = d_inode(parent->dentry);
963 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400964 struct dentry *dentry;
965 int error;
966
Al Viro00235412016-05-26 00:05:12 -0400967 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
968 if (error == -EINTR)
969 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400970
971 dentry = lookup_one_len(name, parent->dentry, namelen);
972 error = PTR_ERR(dentry);
973 if (IS_ERR(dentry))
974 goto out_unlock;
975
Yan, Zheng76dda932009-09-21 16:00:26 -0400976 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400977 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600978 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400979
Chris Mason9c520572012-12-17 14:26:57 -0500980 /*
981 * even if this name doesn't exist, we may get hash collisions.
982 * check for them now when we can safely fail
983 */
984 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
985 dir->i_ino, name,
986 namelen);
987 if (error)
988 goto out_dput;
989
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400990 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400991
992 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
993 goto out_up_read;
994
Chris Mason3de45862008-11-17 21:02:50 -0500995 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100996 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200997 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500998 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000999 error = create_subvol(dir, dentry, name, namelen,
1000 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001001 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001002 if (!error)
1003 fsnotify_mkdir(dir, dentry);
1004out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001005 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001006out_dput:
1007 dput(dentry);
1008out_unlock:
Al Viro59551022016-01-22 15:40:57 -05001009 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001010 return error;
1011}
1012
Chris Mason4cb53002011-05-24 15:35:30 -04001013/*
1014 * When we're defragging a range, we don't want to kick it off again
1015 * if it is really just waiting for delalloc to send it down.
1016 * If we find a nice big extent or delalloc range for the bytes in the
1017 * file you want to defrag, we return 0 to let you know to skip this
1018 * part of the file
1019 */
David Sterbaaab110a2014-07-29 17:32:10 +02001020static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001021{
1022 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1023 struct extent_map *em = NULL;
1024 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1025 u64 end;
1026
1027 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001029 read_unlock(&em_tree->lock);
1030
1031 if (em) {
1032 end = extent_map_end(em);
1033 free_extent_map(em);
1034 if (end - offset > thresh)
1035 return 0;
1036 }
1037 /* if we already have a nice delalloc here, just stop */
1038 thresh /= 2;
1039 end = count_range_bits(io_tree, &offset, offset + thresh,
1040 thresh, EXTENT_DELALLOC, 1);
1041 if (end >= thresh)
1042 return 0;
1043 return 1;
1044}
1045
1046/*
1047 * helper function to walk through a file and find extents
1048 * newer than a specific transid, and smaller than thresh.
1049 *
1050 * This is used by the defragging code to find new and small
1051 * extents
1052 */
1053static int find_new_extents(struct btrfs_root *root,
1054 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001055 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001056{
1057 struct btrfs_path *path;
1058 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001059 struct extent_buffer *leaf;
1060 struct btrfs_file_extent_item *extent;
1061 int type;
1062 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001063 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001064
1065 path = btrfs_alloc_path();
1066 if (!path)
1067 return -ENOMEM;
1068
David Sterbaa4689d22011-05-31 17:08:14 +00001069 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001070 min_key.type = BTRFS_EXTENT_DATA_KEY;
1071 min_key.offset = *off;
1072
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301073 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001074 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001075 if (ret != 0)
1076 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001077process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001078 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001079 goto none;
1080 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1081 goto none;
1082
1083 leaf = path->nodes[0];
1084 extent = btrfs_item_ptr(leaf, path->slots[0],
1085 struct btrfs_file_extent_item);
1086
1087 type = btrfs_file_extent_type(leaf, extent);
1088 if (type == BTRFS_FILE_EXTENT_REG &&
1089 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1090 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1091 *off = min_key.offset;
1092 btrfs_free_path(path);
1093 return 0;
1094 }
1095
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001096 path->slots[0]++;
1097 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1098 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1099 goto process_slot;
1100 }
1101
Chris Mason4cb53002011-05-24 15:35:30 -04001102 if (min_key.offset == (u64)-1)
1103 goto none;
1104
1105 min_key.offset++;
1106 btrfs_release_path(path);
1107 }
1108none:
1109 btrfs_free_path(path);
1110 return -ENOENT;
1111}
1112
Li Zefan6c282eb2012-06-11 16:03:35 +08001113static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001114{
1115 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001116 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001117 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001118 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001119
1120 /*
1121 * hopefully we have this extent in the tree already, try without
1122 * the full extent lock
1123 */
1124 read_lock(&em_tree->lock);
1125 em = lookup_extent_mapping(em_tree, start, len);
1126 read_unlock(&em_tree->lock);
1127
1128 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001129 struct extent_state *cached = NULL;
1130 u64 end = start + len - 1;
1131
Chris Mason940100a2010-03-10 10:52:59 -05001132 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001133 lock_extent_bits(io_tree, start, end, &cached);
Omar Sandoval39b07b52019-12-02 17:34:23 -08001134 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
David Sterbae43bbe52017-12-12 21:43:52 +01001135 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001136
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001137 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001138 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001139 }
1140
Li Zefan6c282eb2012-06-11 16:03:35 +08001141 return em;
1142}
1143
1144static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1145{
1146 struct extent_map *next;
1147 bool ret = true;
1148
1149 /* this is the last extent */
1150 if (em->start + em->len >= i_size_read(inode))
1151 return false;
1152
1153 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001154 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1155 ret = false;
1156 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001157 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001158 ret = false;
1159
1160 free_extent_map(next);
1161 return ret;
1162}
1163
David Sterbaaab110a2014-07-29 17:32:10 +02001164static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001165 u64 *last_len, u64 *skip, u64 *defrag_end,
1166 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001167{
1168 struct extent_map *em;
1169 int ret = 1;
1170 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001171 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001172
1173 /*
1174 * make sure that once we start defragging an extent, we keep on
1175 * defragging it
1176 */
1177 if (start < *defrag_end)
1178 return 1;
1179
1180 *skip = 0;
1181
1182 em = defrag_lookup_extent(inode, start);
1183 if (!em)
1184 return 0;
1185
Chris Mason940100a2010-03-10 10:52:59 -05001186 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001187 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001188 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001189 goto out;
1190 }
1191
Liu Bo4a3560c2015-08-07 16:48:41 +08001192 if (!*defrag_end)
1193 prev_mergeable = false;
1194
Li Zefan6c282eb2012-06-11 16:03:35 +08001195 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001196 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001197 * we hit a real extent, if it is big or the next extent is not a
1198 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001199 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001200 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001201 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001202 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001203out:
Chris Mason940100a2010-03-10 10:52:59 -05001204 /*
1205 * last_len ends up being a counter of how many bytes we've defragged.
1206 * every time we choose not to defrag an extent, we reset *last_len
1207 * so that the next tiny extent will force a defrag.
1208 *
1209 * The end result of this is that tiny extents before a single big
1210 * extent will force at least part of that big extent to be defragged.
1211 */
1212 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001213 *defrag_end = extent_map_end(em);
1214 } else {
1215 *last_len = 0;
1216 *skip = extent_map_end(em);
1217 *defrag_end = 0;
1218 }
1219
1220 free_extent_map(em);
1221 return ret;
1222}
1223
Chris Mason4cb53002011-05-24 15:35:30 -04001224/*
1225 * it doesn't do much good to defrag one or two pages
1226 * at a time. This pulls in a nice chunk of pages
1227 * to COW and defrag.
1228 *
1229 * It also makes sure the delalloc code has enough
1230 * dirty data to avoid making new small extents as part
1231 * of the defrag
1232 *
1233 * It's a good idea to start RA on this range
1234 * before calling this.
1235 */
1236static int cluster_pages_for_defrag(struct inode *inode,
1237 struct page **pages,
1238 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001239 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001240{
Chris Mason4cb53002011-05-24 15:35:30 -04001241 unsigned long file_end;
1242 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001243 u64 page_start;
1244 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001245 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001246 int ret;
1247 int i;
1248 int i_done;
1249 struct btrfs_ordered_extent *ordered;
1250 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001251 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001252 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001253 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001254
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001255 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001256 if (!isize || start_index > file_end)
1257 return 0;
1258
1259 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001260
Qu Wenruo364ecf32017-02-27 15:10:38 +08001261 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001262 start_index << PAGE_SHIFT,
1263 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001264 if (ret)
1265 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001266 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001267 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001268
1269 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001270 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001271 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001272again:
Josef Bacika94733d2011-07-11 10:47:06 -04001273 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001274 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001275 if (!page)
1276 break;
1277
Miao Xie600a45e2012-02-16 15:01:24 +08001278 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001279 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001280 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001281 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001282 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001283 ordered = btrfs_lookup_ordered_extent(inode,
1284 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001285 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001286 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001287 if (!ordered)
1288 break;
1289
1290 unlock_page(page);
1291 btrfs_start_ordered_extent(inode, ordered, 1);
1292 btrfs_put_ordered_extent(ordered);
1293 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001294 /*
1295 * we unlocked the page above, so we need check if
1296 * it was released or not.
1297 */
1298 if (page->mapping != inode->i_mapping) {
1299 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001300 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001301 goto again;
1302 }
Miao Xie600a45e2012-02-16 15:01:24 +08001303 }
1304
Chris Mason4cb53002011-05-24 15:35:30 -04001305 if (!PageUptodate(page)) {
1306 btrfs_readpage(NULL, page);
1307 lock_page(page);
1308 if (!PageUptodate(page)) {
1309 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001310 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001311 ret = -EIO;
1312 break;
1313 }
1314 }
Miao Xie600a45e2012-02-16 15:01:24 +08001315
Miao Xie600a45e2012-02-16 15:01:24 +08001316 if (page->mapping != inode->i_mapping) {
1317 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001318 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001319 goto again;
1320 }
1321
Chris Mason4cb53002011-05-24 15:35:30 -04001322 pages[i] = page;
1323 i_done++;
1324 }
1325 if (!i_done || ret)
1326 goto out;
1327
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001328 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001329 goto out;
1330
1331 /*
1332 * so now we have a nice long stream of locked
1333 * and up to date pages, lets wait on them
1334 */
1335 for (i = 0; i < i_done; i++)
1336 wait_on_page_writeback(pages[i]);
1337
1338 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001339 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001340
1341 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001342 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001343 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
Omar Sandovale1821632019-08-15 14:04:04 -07001344 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1345 EXTENT_DEFRAG, 0, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001346
Liu Bo1f12bd02012-03-29 09:57:44 -04001347 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001348 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001349 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001350 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001351 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001352 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001353 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001354 }
1355
1356
Liu Bo9e8a4a82012-09-05 19:10:51 -06001357 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001358 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001359
1360 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001361 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001362
1363 for (i = 0; i < i_done; i++) {
1364 clear_page_dirty_for_io(pages[i]);
1365 ClearPageChecked(pages[i]);
1366 set_page_extent_mapped(pages[i]);
1367 set_page_dirty(pages[i]);
1368 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001369 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001370 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08001371 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001372 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001373 return i_done;
1374out:
1375 for (i = 0; i < i_done; i++) {
1376 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001377 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001378 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001379 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001380 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001381 page_cnt << PAGE_SHIFT, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08001382 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001383 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001384 return ret;
1385
1386}
1387
1388int btrfs_defrag_file(struct inode *inode, struct file *file,
1389 struct btrfs_ioctl_defrag_range_args *range,
1390 u64 newer_than, unsigned long max_to_defrag)
1391{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001392 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001393 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001394 struct file_ra_state *ra = NULL;
1395 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001396 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001397 u64 last_len = 0;
1398 u64 skip = 0;
1399 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001400 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001401 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001402 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001403 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001404 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001405 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001406 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001407 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001408 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001409 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001410 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001411 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001412
Liu Bo0abd5b12013-04-16 09:20:28 +00001413 if (isize == 0)
1414 return 0;
1415
1416 if (range->start >= isize)
1417 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001418
David Sterba1e2ef462017-07-17 20:01:59 +02001419 if (do_compress) {
Chengguang Xuce96b7f2019-10-10 15:59:57 +08001420 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Li Zefan1a419d82010-10-25 15:12:50 +08001421 return -EINVAL;
1422 if (range->compress_type)
1423 compress_type = range->compress_type;
1424 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001425
Liu Bo0abd5b12013-04-16 09:20:28 +00001426 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001427 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428
Chris Mason4cb53002011-05-24 15:35:30 -04001429 /*
David Sterba0a52d102017-06-22 03:22:58 +02001430 * If we were not given a file, allocate a readahead context. As
1431 * readahead is just an optimization, defrag will work without it so
1432 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001433 */
1434 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001435 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001436 if (ra)
1437 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001438 } else {
1439 ra = &file->f_ra;
1440 }
1441
David Sterba63e727e2017-06-22 03:13:02 +02001442 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001443 if (!pages) {
1444 ret = -ENOMEM;
1445 goto out_ra;
1446 }
1447
1448 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001449 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001450 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001451 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001452 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001453 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001454 }
1455
Chris Mason4cb53002011-05-24 15:35:30 -04001456 if (newer_than) {
1457 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001458 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001459 if (!ret) {
1460 range->start = newer_off;
1461 /*
1462 * we always align our defrag to help keep
1463 * the extents in the file evenly spaced
1464 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001465 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001466 } else
1467 goto out_ra;
1468 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001469 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001470 }
1471 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301472 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001473
Li Zefan2a0f7f52011-10-10 15:43:34 -04001474 /*
1475 * make writeback starts from i, so the defrag range can be
1476 * written sequentially.
1477 */
1478 if (i < inode->i_mapping->writeback_index)
1479 inode->i_mapping->writeback_index = i;
1480
Chris Masonf7f43cc2011-10-11 11:41:40 -04001481 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001482 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001483 /*
1484 * make sure we stop running if someone unmounts
1485 * the FS
1486 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001487 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001488 break;
1489
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001490 if (btrfs_defrag_cancelled(fs_info)) {
1491 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001492 ret = -EAGAIN;
1493 break;
1494 }
1495
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001496 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001497 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001498 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001499 unsigned long next;
1500 /*
1501 * the should_defrag function tells us how much to skip
1502 * bump our counter by the suggested amount
1503 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001504 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001505 i = max(i + 1, next);
1506 continue;
1507 }
Li Zefan008873e2011-09-02 15:57:07 +08001508
1509 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001510 cluster = (PAGE_ALIGN(defrag_end) >>
1511 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001512 cluster = min(cluster, max_cluster);
1513 } else {
1514 cluster = max_cluster;
1515 }
1516
Li Zefan008873e2011-09-02 15:57:07 +08001517 if (i + cluster > ra_index) {
1518 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001519 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001520 page_cache_sync_readahead(inode->i_mapping, ra,
1521 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301522 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001523 }
Chris Mason940100a2010-03-10 10:52:59 -05001524
Al Viro59551022016-01-22 15:40:57 -05001525 inode_lock(inode);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001526 if (IS_SWAPFILE(inode)) {
1527 ret = -ETXTBSY;
1528 } else {
1529 if (do_compress)
1530 BTRFS_I(inode)->defrag_compress = compress_type;
1531 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1532 }
Liu Boecb8bea2012-03-29 09:57:44 -04001533 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001534 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001535 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001536 }
Chris Mason940100a2010-03-10 10:52:59 -05001537
Chris Mason4cb53002011-05-24 15:35:30 -04001538 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001539 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001540 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001541
1542 if (newer_than) {
1543 if (newer_off == (u64)-1)
1544 break;
1545
Liu Boe1f041e2012-03-29 09:57:45 -04001546 if (ret > 0)
1547 i += ret;
1548
Chris Mason4cb53002011-05-24 15:35:30 -04001549 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001550 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001551
Byongho Leeee221842015-12-15 01:42:10 +09001552 ret = find_new_extents(root, inode, newer_than,
1553 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001554 if (!ret) {
1555 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001556 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001557 } else {
1558 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001559 }
Chris Mason4cb53002011-05-24 15:35:30 -04001560 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001561 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001562 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001563 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001564 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001565 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001566 last_len = 0;
1567 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001568 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001569 }
1570
Filipe Mananadec8ef92014-03-01 10:55:54 +00001571 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001572 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001573 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1574 &BTRFS_I(inode)->runtime_flags))
1575 filemap_flush(inode->i_mapping);
1576 }
Chris Mason1e701a32010-03-11 09:42:04 -05001577
Li Zefan1a419d82010-10-25 15:12:50 +08001578 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001579 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001580 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1581 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001582 }
1583
Diego Calleja60ccf822011-09-01 16:33:57 +02001584 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001585
Chris Mason4cb53002011-05-24 15:35:30 -04001586out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001587 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001588 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001589 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001590 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001591 }
Chris Mason4cb53002011-05-24 15:35:30 -04001592 if (!file)
1593 kfree(ra);
1594 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001595 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001596}
1597
Miao Xie198605a2012-11-26 08:43:45 +00001598static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001599 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001600{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001601 struct inode *inode = file_inode(file);
1602 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001603 u64 new_size;
1604 u64 old_size;
1605 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001606 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001607 struct btrfs_ioctl_vol_args *vol_args;
1608 struct btrfs_trans_handle *trans;
1609 struct btrfs_device *device = NULL;
1610 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001611 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001612 char *devstr = NULL;
1613 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614 int mod = 0;
1615
Chris Masone441d542009-01-05 16:57:23 -05001616 if (!capable(CAP_SYS_ADMIN))
1617 return -EPERM;
1618
Miao Xie198605a2012-11-26 08:43:45 +00001619 ret = mnt_want_write_file(file);
1620 if (ret)
1621 return ret;
1622
David Sterba171938e2017-03-28 14:44:21 +02001623 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001624 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001625 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001626 }
1627
Li Zefandae7b662009-04-08 15:06:54 +08001628 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001629 if (IS_ERR(vol_args)) {
1630 ret = PTR_ERR(vol_args);
1631 goto out;
1632 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001633
1634 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001635
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001636 sizestr = vol_args->name;
1637 devstr = strchr(sizestr, ':');
1638 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001639 sizestr = devstr + 1;
1640 *devstr = '\0';
1641 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001642 ret = kstrtoull(devstr, 10, &devid);
1643 if (ret)
1644 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001645 if (!devid) {
1646 ret = -EINVAL;
1647 goto out_free;
1648 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001649 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001650 }
Miao Xiedba60f32012-12-21 09:19:51 +00001651
Anand Jain09ba3bc2019-01-19 14:48:55 +08001652 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001653 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001654 btrfs_info(fs_info, "resizer unable to find device %llu",
1655 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001656 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001657 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001658 }
Miao Xiedba60f32012-12-21 09:19:51 +00001659
Anand Jainebbede42017-12-04 12:54:52 +08001660 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001661 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001662 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001663 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001664 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001665 goto out_free;
1666 }
1667
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001668 if (!strcmp(sizestr, "max"))
1669 new_size = device->bdev->bd_inode->i_size;
1670 else {
1671 if (sizestr[0] == '-') {
1672 mod = -1;
1673 sizestr++;
1674 } else if (sizestr[0] == '+') {
1675 mod = 1;
1676 sizestr++;
1677 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001678 new_size = memparse(sizestr, &retptr);
1679 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001680 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001681 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001682 }
1683 }
1684
Anand Jain401e29c2017-12-04 12:54:55 +08001685 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001686 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001687 goto out_free;
1688 }
1689
Miao Xie7cc8e582014-09-03 21:35:38 +08001690 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001691
1692 if (mod < 0) {
1693 if (new_size > old_size) {
1694 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001695 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001696 }
1697 new_size = old_size - new_size;
1698 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001699 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001700 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001701 goto out_free;
1702 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001703 new_size = old_size + new_size;
1704 }
1705
Byongho Leeee221842015-12-15 01:42:10 +09001706 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001707 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001708 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001709 }
1710 if (new_size > device->bdev->bd_inode->i_size) {
1711 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001712 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001713 }
1714
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001715 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001716
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001717 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1718 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001719
1720 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001721 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001722 if (IS_ERR(trans)) {
1723 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001724 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001725 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001726 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001727 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001728 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001729 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001730 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001731
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001732out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001733 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001734out:
David Sterba171938e2017-03-28 14:44:21 +02001735 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001736 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001737 return ret;
1738}
1739
Sage Weil72fd0322010-10-29 15:41:32 -04001740static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001741 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001742 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001743 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001744{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001745 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001746 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001747
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001748 if (!S_ISDIR(file_inode(file)->i_mode))
1749 return -ENOTDIR;
1750
Liu Boa874a632012-06-29 03:58:46 -06001751 ret = mnt_want_write_file(file);
1752 if (ret)
1753 goto out;
1754
Sage Weil72fd0322010-10-29 15:41:32 -04001755 namelen = strlen(name);
1756 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001757 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001758 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001759 }
1760
Chris Mason16780ca2012-02-20 22:14:55 -05001761 if (name[0] == '.' &&
1762 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1763 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001764 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001765 }
1766
Chris Mason3de45862008-11-17 21:02:50 -05001767 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001768 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001769 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001770 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001771 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001772 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001773 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001774 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001775 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001776 }
1777
Al Viro496ad9a2013-01-23 17:07:38 -05001778 src_inode = file_inode(src.file);
1779 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001780 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001781 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001782 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001783 } else if (!inode_owner_or_capable(src_inode)) {
1784 /*
1785 * Subvolume creation is not restricted, but snapshots
1786 * are limited to own subvolumes only
1787 */
1788 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001789 } else {
1790 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1791 BTRFS_I(src_inode)->root,
1792 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001793 }
Al Viro2903ff02012-08-28 12:52:22 -04001794 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001795 }
Liu Boa874a632012-06-29 03:58:46 -06001796out_drop_write:
1797 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001798out:
Sage Weil72fd0322010-10-29 15:41:32 -04001799 return ret;
1800}
1801
1802static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001803 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001804{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001805 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001806 int ret;
1807
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001808 if (!S_ISDIR(file_inode(file)->i_mode))
1809 return -ENOTDIR;
1810
Li Zefanfa0d2b92010-12-20 15:53:28 +08001811 vol_args = memdup_user(arg, sizeof(*vol_args));
1812 if (IS_ERR(vol_args))
1813 return PTR_ERR(vol_args);
1814 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001815
Li Zefanfa0d2b92010-12-20 15:53:28 +08001816 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001817 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001818 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001819
Li Zefanfa0d2b92010-12-20 15:53:28 +08001820 kfree(vol_args);
1821 return ret;
1822}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001823
Li Zefanfa0d2b92010-12-20 15:53:28 +08001824static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1825 void __user *arg, int subvol)
1826{
1827 struct btrfs_ioctl_vol_args_v2 *vol_args;
1828 int ret;
1829 u64 transid = 0;
1830 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001831 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001832 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001833
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001834 if (!S_ISDIR(file_inode(file)->i_mode))
1835 return -ENOTDIR;
1836
Li Zefanfa0d2b92010-12-20 15:53:28 +08001837 vol_args = memdup_user(arg, sizeof(*vol_args));
1838 if (IS_ERR(vol_args))
1839 return PTR_ERR(vol_args);
1840 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001841
David Sterba673990d2020-02-21 13:24:37 +01001842 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
Li Zefanb83cc962010-12-20 16:04:08 +08001843 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001844 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001845 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001846
Nikolay Borisovebc87352019-08-26 17:34:24 +03001847 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1848 struct inode *inode = file_inode(file);
1849 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1850
1851 btrfs_warn(fs_info,
1852"SNAP_CREATE_V2 ioctl with CREATE_ASYNC is deprecated and will be removed in kernel 5.7");
1853
Li Zefanfa0d2b92010-12-20 15:53:28 +08001854 ptr = &transid;
Nikolay Borisovebc87352019-08-26 17:34:24 +03001855 }
Li Zefanb83cc962010-12-20 16:04:08 +08001856 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1857 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001858 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001859 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001860 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001861 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001862 }
1863 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1864 if (IS_ERR(inherit)) {
1865 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001866 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001867 }
1868 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001869
1870 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001871 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001872 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001873 if (ret)
1874 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001875
Dan Carpenterc47ca322014-09-04 14:09:15 +03001876 if (ptr && copy_to_user(arg +
1877 offsetof(struct btrfs_ioctl_vol_args_v2,
1878 transid),
1879 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001880 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001881
1882free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001883 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001884free_args:
1885 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001886 return ret;
1887}
1888
Li Zefan0caa1022010-12-20 16:30:25 +08001889static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1890 void __user *arg)
1891{
Al Viro496ad9a2013-01-23 17:07:38 -05001892 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001893 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001894 struct btrfs_root *root = BTRFS_I(inode)->root;
1895 int ret = 0;
1896 u64 flags = 0;
1897
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001898 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001899 return -EINVAL;
1900
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001901 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001902 if (btrfs_root_readonly(root))
1903 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001904 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001905
1906 if (copy_to_user(arg, &flags, sizeof(flags)))
1907 ret = -EFAULT;
1908
1909 return ret;
1910}
1911
1912static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1913 void __user *arg)
1914{
Al Viro496ad9a2013-01-23 17:07:38 -05001915 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001917 struct btrfs_root *root = BTRFS_I(inode)->root;
1918 struct btrfs_trans_handle *trans;
1919 u64 root_flags;
1920 u64 flags;
1921 int ret = 0;
1922
David Sterbabd60ea02014-01-16 15:50:22 +01001923 if (!inode_owner_or_capable(inode))
1924 return -EPERM;
1925
Liu Bob9ca0662012-06-29 03:58:49 -06001926 ret = mnt_want_write_file(file);
1927 if (ret)
1928 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001929
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001930 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001931 ret = -EINVAL;
1932 goto out_drop_write;
1933 }
Li Zefan0caa1022010-12-20 16:30:25 +08001934
Liu Bob9ca0662012-06-29 03:58:49 -06001935 if (copy_from_user(&flags, arg, sizeof(flags))) {
1936 ret = -EFAULT;
1937 goto out_drop_write;
1938 }
Li Zefan0caa1022010-12-20 16:30:25 +08001939
Liu Bob9ca0662012-06-29 03:58:49 -06001940 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1941 ret = -EINVAL;
1942 goto out_drop_write;
1943 }
Li Zefan0caa1022010-12-20 16:30:25 +08001944
Liu Bob9ca0662012-06-29 03:58:49 -06001945 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1946 ret = -EOPNOTSUPP;
1947 goto out_drop_write;
1948 }
Li Zefan0caa1022010-12-20 16:30:25 +08001949
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001950 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001951
1952 /* nothing to do */
1953 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001954 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001955
1956 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001957 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001958 btrfs_set_root_flags(&root->root_item,
1959 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001960 } else {
1961 /*
1962 * Block RO -> RW transition if this subvolume is involved in
1963 * send
1964 */
1965 spin_lock(&root->root_item_lock);
1966 if (root->send_in_progress == 0) {
1967 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001968 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001969 spin_unlock(&root->root_item_lock);
1970 } else {
1971 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001972 btrfs_warn(fs_info,
1973 "Attempt to set subvolume %llu read-write during send",
1974 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001975 ret = -EPERM;
1976 goto out_drop_sem;
1977 }
1978 }
Li Zefan0caa1022010-12-20 16:30:25 +08001979
1980 trans = btrfs_start_transaction(root, 1);
1981 if (IS_ERR(trans)) {
1982 ret = PTR_ERR(trans);
1983 goto out_reset;
1984 }
1985
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001986 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001987 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001988 if (ret < 0) {
1989 btrfs_end_transaction(trans);
1990 goto out_reset;
1991 }
Li Zefan0caa1022010-12-20 16:30:25 +08001992
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001993 ret = btrfs_commit_transaction(trans);
1994
Li Zefan0caa1022010-12-20 16:30:25 +08001995out_reset:
1996 if (ret)
1997 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001998out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001999 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06002000out_drop_write:
2001 mnt_drop_write_file(file);
2002out:
Li Zefan0caa1022010-12-20 16:30:25 +08002003 return ret;
2004}
2005
Chris Masonac8e9812010-02-28 15:39:26 -05002006static noinline int key_in_sk(struct btrfs_key *key,
2007 struct btrfs_ioctl_search_key *sk)
2008{
Chris Masonabc6e132010-03-18 12:10:08 -04002009 struct btrfs_key test;
2010 int ret;
2011
2012 test.objectid = sk->min_objectid;
2013 test.type = sk->min_type;
2014 test.offset = sk->min_offset;
2015
2016 ret = btrfs_comp_cpu_keys(key, &test);
2017 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002018 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002019
2020 test.objectid = sk->max_objectid;
2021 test.type = sk->max_type;
2022 test.offset = sk->max_offset;
2023
2024 ret = btrfs_comp_cpu_keys(key, &test);
2025 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002026 return 0;
2027 return 1;
2028}
2029
Jeff Mahoneydf397562016-06-21 20:18:21 -04002030static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002031 struct btrfs_key *key,
2032 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002033 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002034 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002035 unsigned long *sk_offset,
2036 int *num_found)
2037{
2038 u64 found_transid;
2039 struct extent_buffer *leaf;
2040 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002041 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002042 unsigned long item_off;
2043 unsigned long item_len;
2044 int nritems;
2045 int i;
2046 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002047 int ret = 0;
2048
2049 leaf = path->nodes[0];
2050 slot = path->slots[0];
2051 nritems = btrfs_header_nritems(leaf);
2052
2053 if (btrfs_header_generation(leaf) > sk->max_transid) {
2054 i = nritems;
2055 goto advance_key;
2056 }
2057 found_transid = btrfs_header_generation(leaf);
2058
2059 for (i = slot; i < nritems; i++) {
2060 item_off = btrfs_item_ptr_offset(leaf, i);
2061 item_len = btrfs_item_size_nr(leaf, i);
2062
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002063 btrfs_item_key_to_cpu(leaf, key, i);
2064 if (!key_in_sk(key, sk))
2065 continue;
2066
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002067 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002068 if (*num_found) {
2069 ret = 1;
2070 goto out;
2071 }
Chris Masonac8e9812010-02-28 15:39:26 -05002072
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002073 /*
2074 * return one empty item back for v1, which does not
2075 * handle -EOVERFLOW
2076 */
2077
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002078 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002079 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002080 ret = -EOVERFLOW;
2081 }
Chris Masonac8e9812010-02-28 15:39:26 -05002082
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002083 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002084 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002085 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002086 }
2087
Chris Masonac8e9812010-02-28 15:39:26 -05002088 sh.objectid = key->objectid;
2089 sh.offset = key->offset;
2090 sh.type = key->type;
2091 sh.len = item_len;
2092 sh.transid = found_transid;
2093
2094 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002095 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2096 ret = -EFAULT;
2097 goto out;
2098 }
2099
Chris Masonac8e9812010-02-28 15:39:26 -05002100 *sk_offset += sizeof(sh);
2101
2102 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002103 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002104 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002105 if (read_extent_buffer_to_user(leaf, up,
2106 item_off, item_len)) {
2107 ret = -EFAULT;
2108 goto out;
2109 }
2110
Chris Masonac8e9812010-02-28 15:39:26 -05002111 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002112 }
Hugo Millse2156862011-05-14 17:43:41 +00002113 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002114
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002115 if (ret) /* -EOVERFLOW from above */
2116 goto out;
2117
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002118 if (*num_found >= sk->nr_items) {
2119 ret = 1;
2120 goto out;
2121 }
Chris Masonac8e9812010-02-28 15:39:26 -05002122 }
2123advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002124 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002125 test.objectid = sk->max_objectid;
2126 test.type = sk->max_type;
2127 test.offset = sk->max_offset;
2128 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2129 ret = 1;
2130 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002131 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002132 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002133 key->offset = 0;
2134 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002135 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002136 key->offset = 0;
2137 key->type = 0;
2138 key->objectid++;
2139 } else
2140 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002141out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002142 /*
2143 * 0: all items from this leaf copied, continue with next
2144 * 1: * more items can be copied, but unused buffer is too small
2145 * * all items were found
2146 * Either way, it will stops the loop which iterates to the next
2147 * leaf
2148 * -EOVERFLOW: item was to large for buffer
2149 * -EFAULT: could not copy extent buffer back to userspace
2150 */
Chris Masonac8e9812010-02-28 15:39:26 -05002151 return ret;
2152}
2153
2154static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002155 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002156 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002157 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002158{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002159 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002160 struct btrfs_root *root;
2161 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002162 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002163 int ret;
2164 int num_found = 0;
2165 unsigned long sk_offset = 0;
2166
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002167 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2168 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002169 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002170 }
Gerhard Heift12544442014-01-30 16:23:58 +01002171
Chris Masonac8e9812010-02-28 15:39:26 -05002172 path = btrfs_alloc_path();
2173 if (!path)
2174 return -ENOMEM;
2175
2176 if (sk->tree_id == 0) {
2177 /* search the root of the inode that was passed */
Josef Bacik00246522020-01-24 09:33:01 -05002178 root = btrfs_grab_root(BTRFS_I(inode)->root);
Chris Masonac8e9812010-02-28 15:39:26 -05002179 } else {
2180 key.objectid = sk->tree_id;
2181 key.type = BTRFS_ROOT_ITEM_KEY;
2182 key.offset = (u64)-1;
Josef Bacik3619c942020-01-24 09:32:24 -05002183 root = btrfs_get_fs_root(info, &key, true);
Chris Masonac8e9812010-02-28 15:39:26 -05002184 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002185 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002186 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002187 }
2188 }
2189
2190 key.objectid = sk->min_objectid;
2191 key.type = sk->min_type;
2192 key.offset = sk->min_offset;
2193
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302194 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002195 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002196 if (ret != 0) {
2197 if (ret > 0)
2198 ret = 0;
2199 goto err;
2200 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002201 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002202 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002203 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002204 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002205 break;
2206
2207 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002208 if (ret > 0)
2209 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002210err:
2211 sk->nr_items = num_found;
Josef Bacik00246522020-01-24 09:33:01 -05002212 btrfs_put_root(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002213 btrfs_free_path(path);
2214 return ret;
2215}
2216
2217static noinline int btrfs_ioctl_tree_search(struct file *file,
2218 void __user *argp)
2219{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002220 struct btrfs_ioctl_search_args __user *uargs;
2221 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002222 struct inode *inode;
2223 int ret;
2224 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002225
2226 if (!capable(CAP_SYS_ADMIN))
2227 return -EPERM;
2228
Gerhard Heiftba346b32014-01-30 16:24:02 +01002229 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002230
Gerhard Heiftba346b32014-01-30 16:24:02 +01002231 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2232 return -EFAULT;
2233
2234 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002235
Al Viro496ad9a2013-01-23 17:07:38 -05002236 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002237 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002238
2239 /*
2240 * In the origin implementation an overflow is handled by returning a
2241 * search header with a len of zero, so reset ret.
2242 */
2243 if (ret == -EOVERFLOW)
2244 ret = 0;
2245
Gerhard Heiftba346b32014-01-30 16:24:02 +01002246 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002247 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002248 return ret;
2249}
2250
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002251static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2252 void __user *argp)
2253{
2254 struct btrfs_ioctl_search_args_v2 __user *uarg;
2255 struct btrfs_ioctl_search_args_v2 args;
2256 struct inode *inode;
2257 int ret;
2258 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002259 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002260
2261 if (!capable(CAP_SYS_ADMIN))
2262 return -EPERM;
2263
2264 /* copy search header and buffer size */
2265 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2266 if (copy_from_user(&args, uarg, sizeof(args)))
2267 return -EFAULT;
2268
2269 buf_size = args.buf_size;
2270
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002271 /* limit result size to 16MB */
2272 if (buf_size > buf_limit)
2273 buf_size = buf_limit;
2274
2275 inode = file_inode(file);
2276 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002277 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002278 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2279 ret = -EFAULT;
2280 else if (ret == -EOVERFLOW &&
2281 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2282 ret = -EFAULT;
2283
Yan, Zheng76dda932009-09-21 16:00:26 -04002284 return ret;
2285}
2286
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002287/*
Chris Masonac8e9812010-02-28 15:39:26 -05002288 * Search INODE_REFs to identify path name of 'dirid' directory
2289 * in a 'tree_id' tree. and sets path name to 'name'.
2290 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002291static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2292 u64 tree_id, u64 dirid, char *name)
2293{
2294 struct btrfs_root *root;
2295 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002296 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002297 int ret = -1;
2298 int slot;
2299 int len;
2300 int total_len = 0;
2301 struct btrfs_inode_ref *iref;
2302 struct extent_buffer *l;
2303 struct btrfs_path *path;
2304
2305 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2306 name[0]='\0';
2307 return 0;
2308 }
2309
2310 path = btrfs_alloc_path();
2311 if (!path)
2312 return -ENOMEM;
2313
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002314 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002315
2316 key.objectid = tree_id;
2317 key.type = BTRFS_ROOT_ITEM_KEY;
2318 key.offset = (u64)-1;
Josef Bacik3619c942020-01-24 09:32:24 -05002319 root = btrfs_get_fs_root(info, &key, true);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002320 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002321 ret = PTR_ERR(root);
Josef Bacik88234012020-01-24 09:32:34 -05002322 root = NULL;
2323 goto out;
2324 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002325
2326 key.objectid = dirid;
2327 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002328 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002329
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302330 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2332 if (ret < 0)
2333 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002334 else if (ret > 0) {
2335 ret = btrfs_previous_item(root, path, dirid,
2336 BTRFS_INODE_REF_KEY);
2337 if (ret < 0)
2338 goto out;
2339 else if (ret > 0) {
2340 ret = -ENOENT;
2341 goto out;
2342 }
2343 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002344
2345 l = path->nodes[0];
2346 slot = path->slots[0];
2347 btrfs_item_key_to_cpu(l, &key, slot);
2348
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002349 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2350 len = btrfs_inode_ref_name_len(l, iref);
2351 ptr -= len + 1;
2352 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002353 if (ptr < name) {
2354 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002355 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002356 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002357
2358 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302359 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002360
2361 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2362 break;
2363
David Sterbab3b4aa72011-04-21 01:20:15 +02002364 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002365 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002366 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002367 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002368 }
Li Zefan77906a502011-07-14 03:16:00 +00002369 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302370 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002371 ret = 0;
2372out:
Josef Bacik00246522020-01-24 09:33:01 -05002373 btrfs_put_root(root);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002374 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002375 return ret;
2376}
2377
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002378static int btrfs_search_path_in_tree_user(struct inode *inode,
2379 struct btrfs_ioctl_ino_lookup_user_args *args)
2380{
2381 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2382 struct super_block *sb = inode->i_sb;
2383 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2384 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2385 u64 dirid = args->dirid;
2386 unsigned long item_off;
2387 unsigned long item_len;
2388 struct btrfs_inode_ref *iref;
2389 struct btrfs_root_ref *rref;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002390 struct btrfs_root *root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002391 struct btrfs_path *path;
2392 struct btrfs_key key, key2;
2393 struct extent_buffer *leaf;
2394 struct inode *temp_inode;
2395 char *ptr;
2396 int slot;
2397 int len;
2398 int total_len = 0;
2399 int ret;
2400
2401 path = btrfs_alloc_path();
2402 if (!path)
2403 return -ENOMEM;
2404
2405 /*
2406 * If the bottom subvolume does not exist directly under upper_limit,
2407 * construct the path in from the bottom up.
2408 */
2409 if (dirid != upper_limit.objectid) {
2410 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2411
2412 key.objectid = treeid;
2413 key.type = BTRFS_ROOT_ITEM_KEY;
2414 key.offset = (u64)-1;
Josef Bacik3619c942020-01-24 09:32:24 -05002415 root = btrfs_get_fs_root(fs_info, &key, true);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002416 if (IS_ERR(root)) {
2417 ret = PTR_ERR(root);
2418 goto out;
2419 }
2420
2421 key.objectid = dirid;
2422 key.type = BTRFS_INODE_REF_KEY;
2423 key.offset = (u64)-1;
2424 while (1) {
2425 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2426 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002427 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002428 } else if (ret > 0) {
2429 ret = btrfs_previous_item(root, path, dirid,
2430 BTRFS_INODE_REF_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
2439 leaf = path->nodes[0];
2440 slot = path->slots[0];
2441 btrfs_item_key_to_cpu(leaf, &key, slot);
2442
2443 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2444 len = btrfs_inode_ref_name_len(leaf, iref);
2445 ptr -= len + 1;
2446 total_len += len + 1;
2447 if (ptr < args->path) {
2448 ret = -ENAMETOOLONG;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002449 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002450 }
2451
2452 *(ptr + len) = '/';
2453 read_extent_buffer(leaf, ptr,
2454 (unsigned long)(iref + 1), len);
2455
2456 /* Check the read+exec permission of this directory */
2457 ret = btrfs_previous_item(root, path, dirid,
2458 BTRFS_INODE_ITEM_KEY);
2459 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002460 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002461 } else if (ret > 0) {
2462 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002463 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002464 }
2465
2466 leaf = path->nodes[0];
2467 slot = path->slots[0];
2468 btrfs_item_key_to_cpu(leaf, &key2, slot);
2469 if (key2.objectid != dirid) {
2470 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002471 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002472 }
2473
David Sterba4c66e0d2019-10-03 19:09:35 +02002474 temp_inode = btrfs_iget(sb, &key2, root);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002475 if (IS_ERR(temp_inode)) {
2476 ret = PTR_ERR(temp_inode);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002477 goto out_put;
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002478 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002479 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2480 iput(temp_inode);
2481 if (ret) {
2482 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002483 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002484 }
2485
2486 if (key.offset == upper_limit.objectid)
2487 break;
2488 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2489 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002490 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002491 }
2492
2493 btrfs_release_path(path);
2494 key.objectid = key.offset;
2495 key.offset = (u64)-1;
2496 dirid = key.objectid;
2497 }
2498
2499 memmove(args->path, ptr, total_len);
2500 args->path[total_len] = '\0';
Josef Bacik00246522020-01-24 09:33:01 -05002501 btrfs_put_root(root);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002502 root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002503 btrfs_release_path(path);
2504 }
2505
2506 /* Get the bottom subvolume's name from ROOT_REF */
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002507 key.objectid = treeid;
2508 key.type = BTRFS_ROOT_REF_KEY;
2509 key.offset = args->treeid;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002510 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002511 if (ret < 0) {
2512 goto out;
2513 } else if (ret > 0) {
2514 ret = -ENOENT;
2515 goto out;
2516 }
2517
2518 leaf = path->nodes[0];
2519 slot = path->slots[0];
2520 btrfs_item_key_to_cpu(leaf, &key, slot);
2521
2522 item_off = btrfs_item_ptr_offset(leaf, slot);
2523 item_len = btrfs_item_size_nr(leaf, slot);
2524 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2525 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2526 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2527 ret = -EINVAL;
2528 goto out;
2529 }
2530
2531 /* Copy subvolume's name */
2532 item_off += sizeof(struct btrfs_root_ref);
2533 item_len -= sizeof(struct btrfs_root_ref);
2534 read_extent_buffer(leaf, args->name, item_off, item_len);
2535 args->name[item_len] = 0;
2536
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002537out_put:
Josef Bacik00246522020-01-24 09:33:01 -05002538 btrfs_put_root(root);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002539out:
2540 btrfs_free_path(path);
2541 return ret;
2542}
2543
Chris Masonac8e9812010-02-28 15:39:26 -05002544static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2545 void __user *argp)
2546{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002547 struct btrfs_ioctl_ino_lookup_args *args;
2548 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002549 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002550
Julia Lawall2354d08f2010-10-29 15:14:18 -04002551 args = memdup_user(argp, sizeof(*args));
2552 if (IS_ERR(args))
2553 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002554
Al Viro496ad9a2013-01-23 17:07:38 -05002555 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002556
David Sterba01b810b2015-05-12 19:14:49 +02002557 /*
2558 * Unprivileged query to obtain the containing subvolume root id. The
2559 * path is reset so it's consistent with btrfs_search_path_in_tree.
2560 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002561 if (args->treeid == 0)
2562 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2563
David Sterba01b810b2015-05-12 19:14:49 +02002564 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2565 args->name[0] = 0;
2566 goto out;
2567 }
2568
2569 if (!capable(CAP_SYS_ADMIN)) {
2570 ret = -EPERM;
2571 goto out;
2572 }
2573
Chris Masonac8e9812010-02-28 15:39:26 -05002574 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2575 args->treeid, args->objectid,
2576 args->name);
2577
David Sterba01b810b2015-05-12 19:14:49 +02002578out:
Chris Masonac8e9812010-02-28 15:39:26 -05002579 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2580 ret = -EFAULT;
2581
2582 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002583 return ret;
2584}
2585
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002586/*
2587 * Version of ino_lookup ioctl (unprivileged)
2588 *
2589 * The main differences from ino_lookup ioctl are:
2590 *
2591 * 1. Read + Exec permission will be checked using inode_permission() during
2592 * path construction. -EACCES will be returned in case of failure.
2593 * 2. Path construction will be stopped at the inode number which corresponds
2594 * to the fd with which this ioctl is called. If constructed path does not
2595 * exist under fd's inode, -EACCES will be returned.
2596 * 3. The name of bottom subvolume is also searched and filled.
2597 */
2598static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2599{
2600 struct btrfs_ioctl_ino_lookup_user_args *args;
2601 struct inode *inode;
2602 int ret;
2603
2604 args = memdup_user(argp, sizeof(*args));
2605 if (IS_ERR(args))
2606 return PTR_ERR(args);
2607
2608 inode = file_inode(file);
2609
2610 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2611 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2612 /*
2613 * The subvolume does not exist under fd with which this is
2614 * called
2615 */
2616 kfree(args);
2617 return -EACCES;
2618 }
2619
2620 ret = btrfs_search_path_in_tree_user(inode, args);
2621
2622 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2623 ret = -EFAULT;
2624
2625 kfree(args);
2626 return ret;
2627}
2628
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002629/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2630static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2631{
2632 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2633 struct btrfs_fs_info *fs_info;
2634 struct btrfs_root *root;
2635 struct btrfs_path *path;
2636 struct btrfs_key key;
2637 struct btrfs_root_item *root_item;
2638 struct btrfs_root_ref *rref;
2639 struct extent_buffer *leaf;
2640 unsigned long item_off;
2641 unsigned long item_len;
2642 struct inode *inode;
2643 int slot;
2644 int ret = 0;
2645
2646 path = btrfs_alloc_path();
2647 if (!path)
2648 return -ENOMEM;
2649
2650 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2651 if (!subvol_info) {
2652 btrfs_free_path(path);
2653 return -ENOMEM;
2654 }
2655
2656 inode = file_inode(file);
2657 fs_info = BTRFS_I(inode)->root->fs_info;
2658
2659 /* Get root_item of inode's subvolume */
2660 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2661 key.type = BTRFS_ROOT_ITEM_KEY;
2662 key.offset = (u64)-1;
Josef Bacik3619c942020-01-24 09:32:24 -05002663 root = btrfs_get_fs_root(fs_info, &key, true);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002664 if (IS_ERR(root)) {
2665 ret = PTR_ERR(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002666 goto out_free;
2667 }
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002668 root_item = &root->root_item;
2669
2670 subvol_info->treeid = key.objectid;
2671
2672 subvol_info->generation = btrfs_root_generation(root_item);
2673 subvol_info->flags = btrfs_root_flags(root_item);
2674
2675 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2676 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2677 BTRFS_UUID_SIZE);
2678 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2679 BTRFS_UUID_SIZE);
2680
2681 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2682 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2683 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2684
2685 subvol_info->otransid = btrfs_root_otransid(root_item);
2686 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2687 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2688
2689 subvol_info->stransid = btrfs_root_stransid(root_item);
2690 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2691 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2692
2693 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2694 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2695 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2696
2697 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2698 /* Search root tree for ROOT_BACKREF of this subvolume */
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002699 key.type = BTRFS_ROOT_BACKREF_KEY;
2700 key.offset = 0;
Josef Bacik04734e82020-01-24 09:32:36 -05002701 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002702 if (ret < 0) {
2703 goto out;
2704 } else if (path->slots[0] >=
2705 btrfs_header_nritems(path->nodes[0])) {
Josef Bacik04734e82020-01-24 09:32:36 -05002706 ret = btrfs_next_leaf(fs_info->tree_root, path);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002707 if (ret < 0) {
2708 goto out;
2709 } else if (ret > 0) {
2710 ret = -EUCLEAN;
2711 goto out;
2712 }
2713 }
2714
2715 leaf = path->nodes[0];
2716 slot = path->slots[0];
2717 btrfs_item_key_to_cpu(leaf, &key, slot);
2718 if (key.objectid == subvol_info->treeid &&
2719 key.type == BTRFS_ROOT_BACKREF_KEY) {
2720 subvol_info->parent_id = key.offset;
2721
2722 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2723 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2724
2725 item_off = btrfs_item_ptr_offset(leaf, slot)
2726 + sizeof(struct btrfs_root_ref);
2727 item_len = btrfs_item_size_nr(leaf, slot)
2728 - sizeof(struct btrfs_root_ref);
2729 read_extent_buffer(leaf, subvol_info->name,
2730 item_off, item_len);
2731 } else {
2732 ret = -ENOENT;
2733 goto out;
2734 }
2735 }
2736
2737 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2738 ret = -EFAULT;
2739
2740out:
Josef Bacik00246522020-01-24 09:33:01 -05002741 btrfs_put_root(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002742out_free:
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002743 btrfs_free_path(path);
2744 kzfree(subvol_info);
2745 return ret;
2746}
2747
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002748/*
2749 * Return ROOT_REF information of the subvolume containing this inode
2750 * except the subvolume name.
2751 */
2752static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2753{
2754 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2755 struct btrfs_root_ref *rref;
2756 struct btrfs_root *root;
2757 struct btrfs_path *path;
2758 struct btrfs_key key;
2759 struct extent_buffer *leaf;
2760 struct inode *inode;
2761 u64 objectid;
2762 int slot;
2763 int ret;
2764 u8 found;
2765
2766 path = btrfs_alloc_path();
2767 if (!path)
2768 return -ENOMEM;
2769
2770 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2771 if (IS_ERR(rootrefs)) {
2772 btrfs_free_path(path);
2773 return PTR_ERR(rootrefs);
2774 }
2775
2776 inode = file_inode(file);
2777 root = BTRFS_I(inode)->root->fs_info->tree_root;
2778 objectid = BTRFS_I(inode)->root->root_key.objectid;
2779
2780 key.objectid = objectid;
2781 key.type = BTRFS_ROOT_REF_KEY;
2782 key.offset = rootrefs->min_treeid;
2783 found = 0;
2784
2785 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2786 if (ret < 0) {
2787 goto out;
2788 } else if (path->slots[0] >=
2789 btrfs_header_nritems(path->nodes[0])) {
2790 ret = btrfs_next_leaf(root, path);
2791 if (ret < 0) {
2792 goto out;
2793 } else if (ret > 0) {
2794 ret = -EUCLEAN;
2795 goto out;
2796 }
2797 }
2798 while (1) {
2799 leaf = path->nodes[0];
2800 slot = path->slots[0];
2801
2802 btrfs_item_key_to_cpu(leaf, &key, slot);
2803 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2804 ret = 0;
2805 goto out;
2806 }
2807
2808 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2809 ret = -EOVERFLOW;
2810 goto out;
2811 }
2812
2813 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2814 rootrefs->rootref[found].treeid = key.offset;
2815 rootrefs->rootref[found].dirid =
2816 btrfs_root_ref_dirid(leaf, rref);
2817 found++;
2818
2819 ret = btrfs_next_item(root, path);
2820 if (ret < 0) {
2821 goto out;
2822 } else if (ret > 0) {
2823 ret = -EUCLEAN;
2824 goto out;
2825 }
2826 }
2827
2828out:
2829 if (!ret || ret == -EOVERFLOW) {
2830 rootrefs->num_items = found;
2831 /* update min_treeid for next search */
2832 if (found)
2833 rootrefs->min_treeid =
2834 rootrefs->rootref[found - 1].treeid + 1;
2835 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2836 ret = -EFAULT;
2837 }
2838
2839 kfree(rootrefs);
2840 btrfs_free_path(path);
2841
2842 return ret;
2843}
2844
Yan, Zheng76dda932009-09-21 16:00:26 -04002845static noinline int btrfs_ioctl_snap_destroy(struct file *file,
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002846 void __user *arg,
2847 bool destroy_v2)
Yan, Zheng76dda932009-09-21 16:00:26 -04002848{
Al Viro54563d42013-09-01 15:57:51 -04002849 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002850 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002851 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002852 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002853 struct inode *inode;
2854 struct btrfs_root *root = BTRFS_I(dir)->root;
2855 struct btrfs_root *dest = NULL;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002856 struct btrfs_ioctl_vol_args *vol_args = NULL;
2857 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2858 char *subvol_name, *subvol_name_ptr = NULL;
2859 int subvol_namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002860 int err = 0;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002861 bool destroy_parent = false;
Yan, Zheng76dda932009-09-21 16:00:26 -04002862
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002863 if (destroy_v2) {
2864 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2865 if (IS_ERR(vol_args2))
2866 return PTR_ERR(vol_args2);
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002867
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002868 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2869 err = -EOPNOTSUPP;
2870 goto out;
2871 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002872
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002873 /*
2874 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2875 * name, same as v1 currently does.
2876 */
2877 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2878 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2879 subvol_name = vol_args2->name;
2880
2881 err = mnt_want_write_file(file);
2882 if (err)
2883 goto out;
2884 } else {
2885 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2886 err = -EINVAL;
2887 goto out;
2888 }
2889
2890 err = mnt_want_write_file(file);
2891 if (err)
2892 goto out;
2893
2894 dentry = btrfs_get_dentry(fs_info->sb,
2895 BTRFS_FIRST_FREE_OBJECTID,
2896 vol_args2->subvolid, 0, 0);
2897 if (IS_ERR(dentry)) {
2898 err = PTR_ERR(dentry);
2899 goto out_drop_write;
2900 }
2901
2902 /*
2903 * Change the default parent since the subvolume being
2904 * deleted can be outside of the current mount point.
2905 */
2906 parent = btrfs_get_parent(dentry);
2907
2908 /*
2909 * At this point dentry->d_name can point to '/' if the
2910 * subvolume we want to destroy is outsite of the
2911 * current mount point, so we need to release the
2912 * current dentry and execute the lookup to return a new
2913 * one with ->d_name pointing to the
2914 * <mount point>/subvol_name.
2915 */
2916 dput(dentry);
2917 if (IS_ERR(parent)) {
2918 err = PTR_ERR(parent);
2919 goto out_drop_write;
2920 }
2921 dir = d_inode(parent);
2922
2923 /*
2924 * If v2 was used with SPEC_BY_ID, a new parent was
2925 * allocated since the subvolume can be outside of the
2926 * current mount point. Later on we need to release this
2927 * new parent dentry.
2928 */
2929 destroy_parent = true;
2930
2931 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2932 fs_info, vol_args2->subvolid);
2933 if (IS_ERR(subvol_name_ptr)) {
2934 err = PTR_ERR(subvol_name_ptr);
2935 goto free_parent;
2936 }
2937 /* subvol_name_ptr is already NULL termined */
2938 subvol_name = (char *)kbasename(subvol_name_ptr);
2939 }
2940 } else {
2941 vol_args = memdup_user(arg, sizeof(*vol_args));
2942 if (IS_ERR(vol_args))
2943 return PTR_ERR(vol_args);
2944
2945 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
2946 subvol_name = vol_args->name;
2947
2948 err = mnt_want_write_file(file);
2949 if (err)
2950 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002951 }
2952
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002953 subvol_namelen = strlen(subvol_name);
Yan, Zheng76dda932009-09-21 16:00:26 -04002954
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002955 if (strchr(subvol_name, '/') ||
2956 strncmp(subvol_name, "..", subvol_namelen) == 0) {
2957 err = -EINVAL;
2958 goto free_subvol_name;
2959 }
2960
2961 if (!S_ISDIR(dir->i_mode)) {
2962 err = -ENOTDIR;
2963 goto free_subvol_name;
2964 }
David Sterba521e0542014-04-15 16:41:44 +02002965
Al Viro00235412016-05-26 00:05:12 -04002966 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2967 if (err == -EINTR)
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002968 goto free_subvol_name;
2969 dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -04002970 if (IS_ERR(dentry)) {
2971 err = PTR_ERR(dentry);
2972 goto out_unlock_dir;
2973 }
2974
David Howells2b0143b2015-03-17 22:25:59 +00002975 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002976 err = -ENOENT;
2977 goto out_dput;
2978 }
2979
David Howells2b0143b2015-03-17 22:25:59 +00002980 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002981 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302982 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002983 /*
2984 * Regular user. Only allow this with a special mount
2985 * option, when the user has write+exec access to the
2986 * subvol root, and when rmdir(2) would have been
2987 * allowed.
2988 *
2989 * Note that this is _not_ check that the subvol is
2990 * empty or doesn't contain data that we wouldn't
2991 * otherwise be able to delete.
2992 *
2993 * Users who want to delete empty subvols should try
2994 * rmdir(2).
2995 */
2996 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002998 goto out_dput;
2999
3000 /*
3001 * Do not allow deletion if the parent dir is the same
3002 * as the dir to be deleted. That means the ioctl
3003 * must be called on the dentry referencing the root
3004 * of the subvol, not a random directory contained
3005 * within it.
3006 */
3007 err = -EINVAL;
3008 if (root == dest)
3009 goto out_dput;
3010
3011 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
3012 if (err)
3013 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04003014 }
3015
Miao Xie5c39da52012-10-22 11:39:53 +00003016 /* check if subvolume may be deleted by a user */
3017 err = btrfs_may_delete(dir, dentry, 1);
3018 if (err)
3019 goto out_dput;
3020
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003021 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003022 err = -EINVAL;
3023 goto out_dput;
3024 }
3025
Al Viro59551022016-01-22 15:40:57 -05003026 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003027 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05003028 inode_unlock(inode);
Amir Goldstein46008d92019-05-26 17:34:03 +03003029 if (!err) {
3030 fsnotify_rmdir(dir, dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04003031 d_delete(dentry);
Amir Goldstein46008d92019-05-26 17:34:03 +03003032 }
Liu Bofa6ac872013-02-20 14:10:23 +00003033
Yan, Zheng76dda932009-09-21 16:00:26 -04003034out_dput:
3035 dput(dentry);
3036out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05003037 inode_unlock(dir);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003038free_subvol_name:
3039 kfree(subvol_name_ptr);
3040free_parent:
3041 if (destroy_parent)
3042 dput(parent);
Al Viro00235412016-05-26 00:05:12 -04003043out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003044 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04003045out:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003046 kfree(vol_args2);
Yan, Zheng76dda932009-09-21 16:00:26 -04003047 kfree(vol_args);
3048 return err;
3049}
3050
Chris Mason1e701a32010-03-11 09:42:04 -05003051static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003052{
Al Viro496ad9a2013-01-23 17:07:38 -05003053 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003054 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05003055 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05003056 int ret;
3057
Ilya Dryomov25122d12013-01-20 15:57:57 +02003058 ret = mnt_want_write_file(file);
3059 if (ret)
3060 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08003061
Ilya Dryomov25122d12013-01-20 15:57:57 +02003062 if (btrfs_root_readonly(root)) {
3063 ret = -EROFS;
3064 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003065 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003066
3067 switch (inode->i_mode & S_IFMT) {
3068 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05003069 if (!capable(CAP_SYS_ADMIN)) {
3070 ret = -EPERM;
3071 goto out;
3072 }
Eric Sandeende78b512013-01-31 18:21:12 +00003073 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003074 break;
3075 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02003076 /*
3077 * Note that this does not check the file descriptor for write
3078 * access. This prevents defragmenting executables that are
3079 * running and allows defrag on files open in read-only mode.
3080 */
3081 if (!capable(CAP_SYS_ADMIN) &&
3082 inode_permission(inode, MAY_WRITE)) {
3083 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05003084 goto out;
3085 }
Chris Mason1e701a32010-03-11 09:42:04 -05003086
3087 range = kzalloc(sizeof(*range), GFP_KERNEL);
3088 if (!range) {
3089 ret = -ENOMEM;
3090 goto out;
3091 }
3092
3093 if (argp) {
3094 if (copy_from_user(range, argp,
3095 sizeof(*range))) {
3096 ret = -EFAULT;
3097 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00003098 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05003099 }
3100 /* compression requires us to start the IO */
3101 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3102 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3103 range->extent_thresh = (u32)-1;
3104 }
3105 } else {
3106 /* the rest are all set to zero by kzalloc */
3107 range->len = (u64)-1;
3108 }
Al Viro496ad9a2013-01-23 17:07:38 -05003109 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08003110 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003111 if (ret > 0)
3112 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003113 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003114 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003115 default:
3116 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003117 }
Chris Masone441d542009-01-05 16:57:23 -05003118out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003119 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003120 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003121}
3122
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003123static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003124{
3125 struct btrfs_ioctl_vol_args *vol_args;
3126 int ret;
3127
Chris Masone441d542009-01-05 16:57:23 -05003128 if (!capable(CAP_SYS_ADMIN))
3129 return -EPERM;
3130
David Sterba171938e2017-03-28 14:44:21 +02003131 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003132 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003133
Li Zefandae7b662009-04-08 15:06:54 +08003134 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003135 if (IS_ERR(vol_args)) {
3136 ret = PTR_ERR(vol_args);
3137 goto out;
3138 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003139
Mark Fasheh5516e592008-07-24 12:20:14 -04003140 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003141 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003142
Anand Jain43d20762014-07-01 00:58:56 +08003143 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003144 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003145
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003146 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003147out:
David Sterba171938e2017-03-28 14:44:21 +02003148 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003149 return ret;
3150}
3151
Anand Jain6b526ed2016-02-13 10:01:39 +08003152static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003153{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003154 struct inode *inode = file_inode(file);
3155 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003156 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003157 int ret;
3158
Chris Masone441d542009-01-05 16:57:23 -05003159 if (!capable(CAP_SYS_ADMIN))
3160 return -EPERM;
3161
Miao Xieda249272012-11-26 08:44:50 +00003162 ret = mnt_want_write_file(file);
3163 if (ret)
3164 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003165
Li Zefandae7b662009-04-08 15:06:54 +08003166 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003167 if (IS_ERR(vol_args)) {
3168 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003169 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003170 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003171
David Sterba748449cdb2020-02-21 13:30:14 +01003172 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003173 ret = -EOPNOTSUPP;
3174 goto out;
3175 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003176
David Sterba171938e2017-03-28 14:44:21 +02003177 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003178 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3179 goto out;
3180 }
3181
David Sterba735654e2016-02-15 18:15:21 +01003182 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003183 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003184 } else {
3185 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003186 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003187 }
David Sterba171938e2017-03-28 14:44:21 +02003188 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003189
Anand Jain6b526ed2016-02-13 10:01:39 +08003190 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003191 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003192 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003193 vol_args->devid);
3194 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003195 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003196 vol_args->name);
3197 }
Anand Jain183860f2013-05-17 10:52:45 +00003198out:
3199 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003200err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003201 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003202 return ret;
3203}
3204
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003205static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3206{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003207 struct inode *inode = file_inode(file);
3208 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003209 struct btrfs_ioctl_vol_args *vol_args;
3210 int ret;
3211
3212 if (!capable(CAP_SYS_ADMIN))
3213 return -EPERM;
3214
3215 ret = mnt_want_write_file(file);
3216 if (ret)
3217 return ret;
3218
David Sterba171938e2017-03-28 14:44:21 +02003219 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003220 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003221 goto out_drop_write;
3222 }
3223
3224 vol_args = memdup_user(arg, sizeof(*vol_args));
3225 if (IS_ERR(vol_args)) {
3226 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003227 goto out;
3228 }
3229
David Sterba58d7bbf2016-05-04 14:10:47 +02003230 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003231 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003232
3233 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003234 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003235 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003236out:
David Sterba171938e2017-03-28 14:44:21 +02003237 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003238out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003239 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003240
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003241 return ret;
3242}
3243
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003244static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3245 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003246{
Li Zefan027ed2f2011-06-08 08:27:56 +00003247 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003248 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003249 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003250 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003251
Li Zefan027ed2f2011-06-08 08:27:56 +00003252 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3253 if (!fi_args)
3254 return -ENOMEM;
3255
David Sterbad03262c2017-06-16 00:09:21 +02003256 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003257 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003258
David Sterbad03262c2017-06-16 00:09:21 +02003259 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003260 if (device->devid > fi_args->max_id)
3261 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003262 }
David Sterbad03262c2017-06-16 00:09:21 +02003263 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003264
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003265 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003266 fi_args->nodesize = fs_info->nodesize;
3267 fi_args->sectorsize = fs_info->sectorsize;
3268 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003269
Li Zefan027ed2f2011-06-08 08:27:56 +00003270 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3271 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003272
Li Zefan027ed2f2011-06-08 08:27:56 +00003273 kfree(fi_args);
3274 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003275}
3276
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003277static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3278 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003279{
3280 struct btrfs_ioctl_dev_info_args *di_args;
3281 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003282 int ret = 0;
3283 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003284
Jan Schmidt475f6382011-03-11 15:41:01 +01003285 di_args = memdup_user(arg, sizeof(*di_args));
3286 if (IS_ERR(di_args))
3287 return PTR_ERR(di_args);
3288
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003289 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003290 s_uuid = di_args->uuid;
3291
David Sterbac5593ca2017-06-16 00:09:21 +02003292 rcu_read_lock();
Anand Jaine4319cd2019-01-17 23:32:31 +08003293 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08003294 NULL, true);
Jan Schmidt475f6382011-03-11 15:41:01 +01003295
3296 if (!dev) {
3297 ret = -ENODEV;
3298 goto out;
3299 }
3300
3301 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003302 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3303 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003304 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003305 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003306 strncpy(di_args->path, rcu_str_deref(dev->name),
3307 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003308 di_args->path[sizeof(di_args->path) - 1] = 0;
3309 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003310 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003311 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003312
3313out:
David Sterbac5593ca2017-06-16 00:09:21 +02003314 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003315 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3316 ret = -EFAULT;
3317
3318 kfree(di_args);
3319 return ret;
3320}
3321
Filipe Mananad8b55242019-01-08 11:43:07 +00003322static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3323 struct inode *inode2, u64 loff2, u64 len)
3324{
3325 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3326 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3327}
3328
3329static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3330 struct inode *inode2, u64 loff2, u64 len)
3331{
3332 if (inode1 < inode2) {
3333 swap(inode1, inode2);
3334 swap(loff1, loff2);
3335 } else if (inode1 == inode2 && loff2 < loff1) {
3336 swap(loff1, loff2);
3337 }
3338 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3339 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3340}
3341
Filipe Manana57a50e22018-12-12 18:05:59 +00003342static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
Filipe Manana34a28e32018-12-07 15:25:38 +00003343 struct inode *dst, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003344{
Filipe Manana831d2fa2019-12-16 18:26:56 +00003345 const u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Mark Fasheh416161d2013-08-06 11:42:51 -07003346 int ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003347
Filipe Manana34a28e32018-12-07 15:25:38 +00003348 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003349 * Lock destination range to serialize with concurrent readpages() and
3350 * source range to serialize with relocation.
Filipe Manana34a28e32018-12-07 15:25:38 +00003351 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003352 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Filipe Manana831d2fa2019-12-16 18:26:56 +00003353 ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1);
Filipe Mananad8b55242019-01-08 11:43:07 +00003354 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Timofey Titovets39739092018-05-02 08:15:36 +03003355
3356 return ret;
3357}
3358
Timofey Titovetsb6728762018-05-02 08:15:37 +03003359#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3360
Timofey Titovets39739092018-05-02 08:15:36 +03003361static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3362 struct inode *dst, u64 dst_loff)
3363{
3364 int ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003365 u64 i, tail_len, chunk_count;
Filipe Manana62d54f32019-04-22 16:43:42 +01003366 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3367
3368 spin_lock(&root_dst->root_item_lock);
3369 if (root_dst->send_in_progress) {
3370 btrfs_warn_rl(root_dst->fs_info,
3371"cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3372 root_dst->root_key.objectid,
3373 root_dst->send_in_progress);
3374 spin_unlock(&root_dst->root_item_lock);
3375 return -EAGAIN;
3376 }
3377 root_dst->dedupe_in_progress++;
3378 spin_unlock(&root_dst->root_item_lock);
Timofey Titovets39739092018-05-02 08:15:36 +03003379
Timofey Titovetsb6728762018-05-02 08:15:37 +03003380 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3381 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003382
Timofey Titovetsb6728762018-05-02 08:15:37 +03003383 for (i = 0; i < chunk_count; i++) {
3384 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Filipe Manana34a28e32018-12-07 15:25:38 +00003385 dst, dst_loff);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003386 if (ret)
Filipe Manana62d54f32019-04-22 16:43:42 +01003387 goto out;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003388
3389 loff += BTRFS_MAX_DEDUPE_LEN;
3390 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3391 }
3392
3393 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003394 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
Filipe Manana34a28e32018-12-07 15:25:38 +00003395 dst_loff);
Filipe Manana62d54f32019-04-22 16:43:42 +01003396out:
3397 spin_lock(&root_dst->root_item_lock);
3398 root_dst->dedupe_in_progress--;
3399 spin_unlock(&root_dst->root_item_lock);
Mark Fasheh416161d2013-08-06 11:42:51 -07003400
3401 return ret;
3402}
3403
Filipe Mananaf82a9902014-06-01 01:50:28 +01003404static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3405 struct inode *inode,
3406 u64 endoff,
3407 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003408 const u64 olen,
3409 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003410{
3411 struct btrfs_root *root = BTRFS_I(inode)->root;
3412 int ret;
3413
3414 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003415 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003416 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003417 /*
3418 * We round up to the block size at eof when determining which
3419 * extents to clone above, but shouldn't round up the file size.
3420 */
3421 if (endoff > destoff + olen)
3422 endoff = destoff + olen;
Josef Bacik790a1d42020-01-17 09:02:19 -05003423 if (endoff > inode->i_size) {
3424 i_size_write(inode, endoff);
Josef Bacikd923afe2020-01-17 09:02:23 -05003425 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik790a1d42020-01-17 09:02:19 -05003426 }
Filipe Mananaf82a9902014-06-01 01:50:28 +01003427
3428 ret = btrfs_update_inode(trans, root, inode);
3429 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003430 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003431 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003432 goto out;
3433 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003434 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003435out:
3436 return ret;
3437}
3438
Filipe Manana8039d872015-10-13 15:15:00 +01003439/*
3440 * Make sure we do not end up inserting an inline extent into a file that has
3441 * already other (non-inline) extents. If a file has an inline extent it can
3442 * not have any other extents and the (single) inline extent must start at the
3443 * file offset 0. Failing to respect these rules will lead to file corruption,
3444 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3445 *
3446 * We can have extents that have been already written to disk or we can have
3447 * dirty ranges still in delalloc, in which case the extent maps and items are
3448 * created only when we run delalloc, and the delalloc ranges might fall outside
3449 * the range we are currently locking in the inode's io tree. So we check the
3450 * inode's i_size because of that (i_size updates are done while holding the
3451 * i_mutex, which we are holding here).
3452 * We also check to see if the inode has a size not greater than "datal" but has
3453 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3454 * protected against such concurrent fallocate calls by the i_mutex).
3455 *
3456 * If the file has no extents but a size greater than datal, do not allow the
3457 * copy because we would need turn the inline extent into a non-inline one (even
3458 * with NO_HOLES enabled). If we find our destination inode only has one inline
3459 * extent, just overwrite it with the source inline extent if its size is less
3460 * than the source extent's size, or we could copy the source inline extent's
3461 * data into the destination inode's inline extent if the later is greater then
3462 * the former.
3463 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003464static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003465 struct btrfs_trans_handle *trans,
3466 struct btrfs_path *path,
3467 struct btrfs_key *new_key,
3468 const u64 drop_start,
3469 const u64 datal,
3470 const u64 skip,
3471 const u64 size,
3472 char *inline_data)
3473{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003474 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003475 struct btrfs_root *root = BTRFS_I(dst)->root;
3476 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003477 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003478 int ret;
3479 struct btrfs_key key;
3480
3481 if (new_key->offset > 0)
3482 return -EOPNOTSUPP;
3483
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003484 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003485 key.type = BTRFS_EXTENT_DATA_KEY;
3486 key.offset = 0;
3487 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3488 if (ret < 0) {
3489 return ret;
3490 } else if (ret > 0) {
3491 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3492 ret = btrfs_next_leaf(root, path);
3493 if (ret < 0)
3494 return ret;
3495 else if (ret > 0)
3496 goto copy_inline_extent;
3497 }
3498 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003499 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003500 key.type == BTRFS_EXTENT_DATA_KEY) {
3501 ASSERT(key.offset > 0);
3502 return -EOPNOTSUPP;
3503 }
3504 } else if (i_size_read(dst) <= datal) {
3505 struct btrfs_file_extent_item *ei;
3506 u64 ext_len;
3507
3508 /*
3509 * If the file size is <= datal, make sure there are no other
3510 * extents following (can happen do to an fallocate call with
3511 * the flag FALLOC_FL_KEEP_SIZE).
3512 */
3513 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3514 struct btrfs_file_extent_item);
3515 /*
3516 * If it's an inline extent, it can not have other extents
3517 * following it.
3518 */
3519 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3520 BTRFS_FILE_EXTENT_INLINE)
3521 goto copy_inline_extent;
3522
3523 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3524 if (ext_len > aligned_end)
3525 return -EOPNOTSUPP;
3526
3527 ret = btrfs_next_item(root, path);
3528 if (ret < 0) {
3529 return ret;
3530 } else if (ret == 0) {
3531 btrfs_item_key_to_cpu(path->nodes[0], &key,
3532 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003533 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003534 key.type == BTRFS_EXTENT_DATA_KEY)
3535 return -EOPNOTSUPP;
3536 }
3537 }
3538
3539copy_inline_extent:
3540 /*
3541 * We have no extent items, or we have an extent at offset 0 which may
3542 * or may not be inlined. All these cases are dealt the same way.
3543 */
3544 if (i_size_read(dst) > datal) {
3545 /*
3546 * If the destination inode has an inline extent...
3547 * This would require copying the data from the source inline
3548 * extent into the beginning of the destination's inline extent.
3549 * But this is really complex, both extents can be compressed
3550 * or just one of them, which would require decompressing and
3551 * re-compressing data (which could increase the new compressed
3552 * size, not allowing the compressed data to fit anymore in an
3553 * inline extent).
3554 * So just don't support this case for now (it should be rare,
3555 * we are not really saving space when cloning inline extents).
3556 */
3557 return -EOPNOTSUPP;
3558 }
3559
3560 btrfs_release_path(path);
3561 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3562 if (ret)
3563 return ret;
3564 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3565 if (ret)
3566 return ret;
3567
3568 if (skip) {
3569 const u32 start = btrfs_file_extent_calc_inline_size(0);
3570
3571 memmove(inline_data + start, inline_data + start + skip, datal);
3572 }
3573
3574 write_extent_buffer(path->nodes[0], inline_data,
3575 btrfs_item_ptr_offset(path->nodes[0],
3576 path->slots[0]),
3577 size);
3578 inode_add_bytes(dst, datal);
Filipe Manana690a5db2019-07-05 11:09:50 +01003579 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags);
Filipe Manana8039d872015-10-13 15:15:00 +01003580
3581 return 0;
3582}
3583
Mark Fasheh32b7c682013-08-06 11:42:49 -07003584/**
3585 * btrfs_clone() - clone a range from inode file to another
3586 *
3587 * @src: Inode to clone from
3588 * @inode: Inode to clone to
3589 * @off: Offset within source to start clone from
3590 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003591 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003592 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003593 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003594 */
3595static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003596 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003597 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003598{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003599 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003600 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003601 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003602 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003603 struct btrfs_trans_handle *trans;
3604 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003605 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003606 u32 nritems;
3607 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003608 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003609 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003610 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003611
3612 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003613 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3614 if (!buf)
3615 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003616
3617 path = btrfs_alloc_path();
3618 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003619 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003620 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003621 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003622
David Sterbae4058b52015-11-27 16:31:35 +01003623 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003624 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003625 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003626 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003627 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003628
3629 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003630 u64 next_key_min_offset = key.offset + 1;
Filipe Mananab64119b2019-07-02 15:23:07 +01003631 struct btrfs_file_extent_item *extent;
3632 int type;
3633 u32 size;
3634 struct btrfs_key new_key;
3635 u64 disko = 0, diskl = 0;
3636 u64 datao = 0, datal = 0;
3637 u8 comp;
3638 u64 drop_start;
Filipe Mananadf858e72015-03-31 14:56:46 +01003639
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003640 /*
3641 * note the key will change type as we walk through the
3642 * tree.
3643 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003644 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003645 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3646 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003647 if (ret < 0)
3648 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003649 /*
3650 * First search, if no extent item that starts at offset off was
3651 * found but the previous item is an extent item, it's possible
3652 * it might overlap our target range, therefore process it.
3653 */
3654 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3655 btrfs_item_key_to_cpu(path->nodes[0], &key,
3656 path->slots[0] - 1);
3657 if (key.type == BTRFS_EXTENT_DATA_KEY)
3658 path->slots[0]--;
3659 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003660
Yan Zhengae01a0a2008-08-04 23:23:47 -04003661 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003662process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003663 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003664 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003665 if (ret < 0)
3666 goto out;
3667 if (ret > 0)
3668 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003669 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003670 }
3671 leaf = path->nodes[0];
3672 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003673
Yan Zhengae01a0a2008-08-04 23:23:47 -04003674 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003675 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003676 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003677 break;
3678
Filipe Mananab64119b2019-07-02 15:23:07 +01003679 ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
Zheng Yan31840ae2008-09-23 13:14:14 -04003680
Filipe Mananab64119b2019-07-02 15:23:07 +01003681 extent = btrfs_item_ptr(leaf, slot,
3682 struct btrfs_file_extent_item);
3683 comp = btrfs_file_extent_compression(leaf, extent);
3684 type = btrfs_file_extent_type(leaf, extent);
3685 if (type == BTRFS_FILE_EXTENT_REG ||
3686 type == BTRFS_FILE_EXTENT_PREALLOC) {
3687 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
3688 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
3689 datao = btrfs_file_extent_offset(leaf, extent);
3690 datal = btrfs_file_extent_num_bytes(leaf, extent);
3691 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3692 /* Take upper bound, may be compressed */
3693 datal = btrfs_file_extent_ram_bytes(leaf, extent);
3694 }
3695
3696 /*
3697 * The first search might have left us at an extent item that
3698 * ends before our target range's start, can happen if we have
3699 * holes and NO_HOLES feature enabled.
3700 */
3701 if (key.offset + datal <= off) {
3702 path->slots[0]++;
3703 goto process_slot;
3704 } else if (key.offset >= off + len) {
3705 break;
3706 }
3707 next_key_min_offset = key.offset + datal;
3708 size = btrfs_item_size_nr(leaf, slot);
3709 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot),
3710 size);
3711
3712 btrfs_release_path(path);
3713 path->leave_spinning = 0;
3714
3715 memcpy(&new_key, &key, sizeof(new_key));
3716 new_key.objectid = btrfs_ino(BTRFS_I(inode));
3717 if (off <= key.offset)
3718 new_key.offset = key.offset + destoff - off;
3719 else
3720 new_key.offset = destoff;
3721
3722 /*
3723 * Deal with a hole that doesn't have an extent item that
3724 * represents it (NO_HOLES feature enabled).
3725 * This hole is either in the middle of the cloning range or at
3726 * the beginning (fully overlaps it or partially overlaps it).
3727 */
3728 if (new_key.offset != last_dest_end)
3729 drop_start = last_dest_end;
3730 else
3731 drop_start = new_key.offset;
3732
3733 if (type == BTRFS_FILE_EXTENT_REG ||
3734 type == BTRFS_FILE_EXTENT_PREALLOC) {
3735 struct btrfs_clone_extent_info clone_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003736
Filipe Manana2c463822014-05-31 02:31:05 +01003737 /*
Filipe Mananab64119b2019-07-02 15:23:07 +01003738 * a | --- range to clone ---| b
3739 * | ------------- extent ------------- |
Filipe Manana2c463822014-05-31 02:31:05 +01003740 */
Filipe Mananab64119b2019-07-02 15:23:07 +01003741
3742 /* Subtract range b */
3743 if (key.offset + datal > off + len)
3744 datal = off + len - key.offset;
3745
3746 /* Subtract range a */
3747 if (off > key.offset) {
3748 datao += off - key.offset;
3749 datal -= off - key.offset;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003750 }
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003751
Filipe Mananab64119b2019-07-02 15:23:07 +01003752 clone_info.disk_offset = disko;
3753 clone_info.disk_len = diskl;
3754 clone_info.data_offset = datao;
3755 clone_info.data_len = datal;
3756 clone_info.file_offset = new_key.offset;
3757 clone_info.extent_buf = buf;
3758 clone_info.item_size = size;
3759 ret = btrfs_punch_hole_range(inode, path,
Filipe Manana690a5db2019-07-05 11:09:50 +01003760 drop_start,
3761 new_key.offset + datal - 1,
3762 &clone_info, &trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003763 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003764 goto out;
Filipe Mananab64119b2019-07-02 15:23:07 +01003765 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3766 u64 skip = 0;
3767 u64 trim = 0;
3768
3769 if (off > key.offset) {
3770 skip = off - key.offset;
3771 new_key.offset += skip;
3772 }
3773
3774 if (key.offset + datal > off + len)
3775 trim = key.offset + datal - (off + len);
3776
3777 if (comp && (skip || trim)) {
3778 ret = -EINVAL;
3779 goto out;
3780 }
3781 size -= skip + trim;
3782 datal -= skip + trim;
3783
3784 /*
3785 * If our extent is inline, we know we will drop or
3786 * adjust at most 1 extent item in the destination root.
3787 *
3788 * 1 - adjusting old extent (we may have to split it)
3789 * 1 - add new extent
3790 * 1 - inode update
3791 */
3792 trans = btrfs_start_transaction(root, 3);
3793 if (IS_ERR(trans)) {
3794 ret = PTR_ERR(trans);
3795 goto out;
3796 }
3797
3798 ret = clone_copy_inline_extent(inode, trans, path,
3799 &new_key, drop_start,
3800 datal, skip, size, buf);
3801 if (ret) {
3802 if (ret != -EOPNOTSUPP)
3803 btrfs_abort_transaction(trans, ret);
3804 btrfs_end_transaction(trans);
3805 goto out;
3806 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003807 }
Filipe Mananab64119b2019-07-02 15:23:07 +01003808
3809 btrfs_release_path(path);
3810
3811 last_dest_end = ALIGN(new_key.offset + datal,
3812 fs_info->sectorsize);
3813 ret = clone_finish_inode_update(trans, inode, last_dest_end,
3814 destoff, olen, no_time_update);
3815 if (ret)
3816 goto out;
3817 if (new_key.offset + datal >= destoff + len)
3818 break;
3819
David Sterbab3b4aa72011-04-21 01:20:15 +02003820 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003821 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003822
3823 if (fatal_signal_pending(current)) {
3824 ret = -EINTR;
3825 goto out;
3826 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003827 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003828 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003829
Filipe Mananaf82a9902014-06-01 01:50:28 +01003830 if (last_dest_end < destoff + len) {
3831 /*
Filipe Manana147271e2019-12-05 16:58:41 +00003832 * We have an implicit hole that fully or partially overlaps our
3833 * cloning range at its end. This means that we either have the
3834 * NO_HOLES feature enabled or the implicit hole happened due to
3835 * mixing buffered and direct IO writes against this file.
Filipe Mananaf82a9902014-06-01 01:50:28 +01003836 */
3837 btrfs_release_path(path);
Filipe Manana690a5db2019-07-05 11:09:50 +01003838 path->leave_spinning = 0;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003839
Filipe Manana690a5db2019-07-05 11:09:50 +01003840 ret = btrfs_punch_hole_range(inode, path,
3841 last_dest_end, destoff + len - 1,
Filipe Manana147271e2019-12-05 16:58:41 +00003842 NULL, &trans);
Filipe Manana690a5db2019-07-05 11:09:50 +01003843 if (ret)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003844 goto out;
Filipe Manana690a5db2019-07-05 11:09:50 +01003845
Filipe Mananaf82a9902014-06-01 01:50:28 +01003846 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003847 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003848 }
3849
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003850out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003851 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003852 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003853 return ret;
3854}
3855
Zach Brown3db11b22015-11-10 16:53:32 -05003856static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3857 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003858{
Al Viro54563d42013-09-01 15:57:51 -04003859 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003860 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003861 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003862 int ret;
3863 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003864 u64 bs = fs_info->sb->s_blocksize;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003865
3866 /*
3867 * TODO:
3868 * - split compressed inline extents. annoying: we need to
3869 * decompress into destination's address_space (the file offset
3870 * may change, so source mapping won't do), then recompress (or
3871 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003872 *
3873 * - split destination inode's inline extents. The inline extents can
3874 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003875 */
3876
Filipe Mananaac765f82018-11-05 11:14:17 +00003877 /*
Filipe Manana34a28e32018-12-07 15:25:38 +00003878 * VFS's generic_remap_file_range_prep() protects us from cloning the
3879 * eof block into the middle of a file, which would result in corruption
3880 * if the file size is not blocksize aligned. So we don't need to check
3881 * for that case here.
Filipe Mananaac765f82018-11-05 11:14:17 +00003882 */
Filipe Manana34a28e32018-12-07 15:25:38 +00003883 if (off + len == src->i_size)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003884 len = ALIGN(src->i_size, bs) - off;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003885
3886 if (destoff > inode->i_size) {
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003887 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3888
Mark Fasheh32b7c682013-08-06 11:42:49 -07003889 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3890 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003891 return ret;
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003892 /*
3893 * We may have truncated the last block if the inode's size is
3894 * not sector size aligned, so we need to wait for writeback to
3895 * complete before proceeding further, otherwise we can race
3896 * with cloning and attempt to increment a reference to an
3897 * extent that no longer exists (writeback completed right after
3898 * we found the previous extent covering eof and before we
3899 * attempted to increment its reference count).
3900 */
3901 ret = btrfs_wait_ordered_range(inode, wb_start,
3902 destoff - wb_start);
3903 if (ret)
3904 return ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003905 }
3906
Filipe Mananac125b8b2014-05-23 05:03:34 +01003907 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003908 * Lock destination range to serialize with concurrent readpages() and
3909 * source range to serialize with relocation.
Filipe Mananac125b8b2014-05-23 05:03:34 +01003910 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003911 btrfs_double_extent_lock(src, off, inode, destoff, len);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003912 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Filipe Mananad8b55242019-01-08 11:43:07 +00003913 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003914 /*
3915 * Truncate page cache pages so that future reads will see the cloned
3916 * data immediately and not the previous data.
3917 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303918 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003919 round_down(destoff, PAGE_SIZE),
3920 round_up(destoff + len, PAGE_SIZE) - 1);
Filipe Manana34a28e32018-12-07 15:25:38 +00003921
3922 return ret;
3923}
3924
3925static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3926 struct file *file_out, loff_t pos_out,
3927 loff_t *len, unsigned int remap_flags)
3928{
3929 struct inode *inode_in = file_inode(file_in);
3930 struct inode *inode_out = file_inode(file_out);
3931 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3932 bool same_inode = inode_out == inode_in;
3933 u64 wb_len;
3934 int ret;
3935
3936 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3937 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3938
3939 if (btrfs_root_readonly(root_out))
3940 return -EROFS;
3941
3942 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3943 inode_in->i_sb != inode_out->i_sb)
3944 return -EXDEV;
3945 }
3946
Filipe Manana500710d2018-12-12 18:05:56 +00003947 /* don't make the dst file partly checksummed */
3948 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3949 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003950 return -EINVAL;
Filipe Manana500710d2018-12-12 18:05:56 +00003951 }
3952
Filipe Manana34a28e32018-12-07 15:25:38 +00003953 /*
3954 * Now that the inodes are locked, we need to start writeback ourselves
3955 * and can not rely on the writeback from the VFS's generic helper
3956 * generic_remap_file_range_prep() because:
3957 *
3958 * 1) For compression we must call filemap_fdatawrite_range() range
3959 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3960 * helper only calls it once;
3961 *
3962 * 2) filemap_fdatawrite_range(), called by the generic helper only
3963 * waits for the writeback to complete, i.e. for IO to be done, and
3964 * not for the ordered extents to complete. We need to wait for them
3965 * to complete so that new file extent items are in the fs tree.
3966 */
3967 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3968 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3969 else
3970 wb_len = ALIGN(*len, bs);
3971
3972 /*
3973 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3974 * any in progress could create its ordered extents after we wait for
3975 * existing ordered extents below).
3976 */
3977 inode_dio_wait(inode_in);
3978 if (!same_inode)
3979 inode_dio_wait(inode_out);
3980
Qu Wenruoa94d1d02019-05-08 18:49:58 +08003981 /*
3982 * Workaround to make sure NOCOW buffered write reach disk as NOCOW.
3983 *
3984 * Btrfs' back references do not have a block level granularity, they
3985 * work at the whole extent level.
3986 * NOCOW buffered write without data space reserved may not be able
3987 * to fall back to CoW due to lack of data space, thus could cause
3988 * data loss.
3989 *
3990 * Here we take a shortcut by flushing the whole inode, so that all
3991 * nocow write should reach disk as nocow before we increase the
3992 * reference of the extent. We could do better by only flushing NOCOW
3993 * data, but that needs extra accounting.
3994 *
3995 * Also we don't need to check ASYNC_EXTENT, as async extent will be
3996 * CoWed anyway, not affecting nocow part.
3997 */
3998 ret = filemap_flush(inode_in->i_mapping);
3999 if (ret < 0)
4000 return ret;
4001
Filipe Manana34a28e32018-12-07 15:25:38 +00004002 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
4003 wb_len);
4004 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004005 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00004006 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
4007 wb_len);
4008 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004009 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00004010
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004011 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
Filipe Manana34a28e32018-12-07 15:25:38 +00004012 len, remap_flags);
Zach Brown3db11b22015-11-10 16:53:32 -05004013}
4014
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004015loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4016 struct file *dst_file, loff_t destoff, loff_t len,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004017 unsigned int remap_flags)
Zach Brown3db11b22015-11-10 16:53:32 -05004018{
Filipe Manana34a28e32018-12-07 15:25:38 +00004019 struct inode *src_inode = file_inode(src_file);
4020 struct inode *dst_inode = file_inode(dst_file);
4021 bool same_inode = dst_inode == src_inode;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004022 int ret;
4023
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004024 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4025 return -EINVAL;
4026
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004027 if (same_inode)
4028 inode_lock(src_inode);
4029 else
4030 lock_two_nondirectories(src_inode, dst_inode);
4031
Filipe Manana34a28e32018-12-07 15:25:38 +00004032 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4033 &len, remap_flags);
4034 if (ret < 0 || len == 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004035 goto out_unlock;
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004036
Filipe Manana34a28e32018-12-07 15:25:38 +00004037 if (remap_flags & REMAP_FILE_DEDUP)
4038 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4039 else
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004040 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
Filipe Manana34a28e32018-12-07 15:25:38 +00004041
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004042out_unlock:
Filipe Manana34a28e32018-12-07 15:25:38 +00004043 if (same_inode)
4044 inode_unlock(src_inode);
4045 else
Filipe Manana4ea748e2019-02-26 12:06:09 +00004046 unlock_two_nondirectories(src_inode, dst_inode);
Filipe Manana34a28e32018-12-07 15:25:38 +00004047
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004048 return ret < 0 ? ret : len;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004049}
4050
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004051static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4052{
Al Viro496ad9a2013-01-23 17:07:38 -05004053 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004055 struct btrfs_root *root = BTRFS_I(inode)->root;
4056 struct btrfs_root *new_root;
4057 struct btrfs_dir_item *di;
4058 struct btrfs_trans_handle *trans;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004059 struct btrfs_path *path = NULL;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004060 struct btrfs_key location;
4061 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004062 u64 objectid = 0;
4063 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004064 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004065
4066 if (!capable(CAP_SYS_ADMIN))
4067 return -EPERM;
4068
Miao Xie3c04ce02012-11-26 08:43:07 +00004069 ret = mnt_want_write_file(file);
4070 if (ret)
4071 return ret;
4072
4073 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4074 ret = -EFAULT;
4075 goto out;
4076 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004077
4078 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304079 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004080
4081 location.objectid = objectid;
4082 location.type = BTRFS_ROOT_ITEM_KEY;
4083 location.offset = (u64)-1;
4084
Josef Bacik3619c942020-01-24 09:32:24 -05004085 new_root = btrfs_get_fs_root(fs_info, &location, true);
Miao Xie3c04ce02012-11-26 08:43:07 +00004086 if (IS_ERR(new_root)) {
4087 ret = PTR_ERR(new_root);
4088 goto out;
4089 }
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004090 if (!is_fstree(new_root->root_key.objectid)) {
4091 ret = -ENOENT;
4092 goto out_free;
4093 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004094
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004095 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004096 if (!path) {
4097 ret = -ENOMEM;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004098 goto out_free;
Miao Xie3c04ce02012-11-26 08:43:07 +00004099 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004100 path->leave_spinning = 1;
4101
4102 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004103 if (IS_ERR(trans)) {
Miao Xie3c04ce02012-11-26 08:43:07 +00004104 ret = PTR_ERR(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004105 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004106 }
4107
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004108 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4109 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004110 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004111 if (IS_ERR_OR_NULL(di)) {
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004112 btrfs_release_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004113 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004114 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004115 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004116 ret = -ENOENT;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004117 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004118 }
4119
4120 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4121 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4122 btrfs_mark_buffer_dirty(path->nodes[0]);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004123 btrfs_release_path(path);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004124
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004125 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004126 btrfs_end_transaction(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004127out_free:
Josef Bacik00246522020-01-24 09:33:01 -05004128 btrfs_put_root(new_root);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05004129 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004130out:
4131 mnt_drop_write_file(file);
4132 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004133}
4134
Su Yuec065f5b12018-04-02 17:24:11 +08004135static void get_block_group_info(struct list_head *groups_list,
4136 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004137{
David Sterba32da53862019-10-29 19:20:18 +01004138 struct btrfs_block_group *block_group;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004139
4140 space->total_bytes = 0;
4141 space->used_bytes = 0;
4142 space->flags = 0;
4143 list_for_each_entry(block_group, groups_list, list) {
4144 space->flags = block_group->flags;
David Sterbab3470b52019-10-23 18:48:22 +02004145 space->total_bytes += block_group->length;
David Sterbabf38be62019-10-23 18:48:11 +02004146 space->used_bytes += block_group->used;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004147 }
4148}
4149
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004150static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4151 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004152{
4153 struct btrfs_ioctl_space_args space_args;
4154 struct btrfs_ioctl_space_info space;
4155 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004156 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004157 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004158 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004159 static const u64 types[] = {
4160 BTRFS_BLOCK_GROUP_DATA,
4161 BTRFS_BLOCK_GROUP_SYSTEM,
4162 BTRFS_BLOCK_GROUP_METADATA,
4163 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4164 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004165 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004166 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004167 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004168 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004169 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004170
4171 if (copy_from_user(&space_args,
4172 (struct btrfs_ioctl_space_args __user *)arg,
4173 sizeof(space_args)))
4174 return -EFAULT;
4175
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004176 for (i = 0; i < num_types; i++) {
4177 struct btrfs_space_info *tmp;
4178
4179 info = NULL;
4180 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004181 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004182 list) {
4183 if (tmp->flags == types[i]) {
4184 info = tmp;
4185 break;
4186 }
4187 }
4188 rcu_read_unlock();
4189
4190 if (!info)
4191 continue;
4192
4193 down_read(&info->groups_sem);
4194 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4195 if (!list_empty(&info->block_groups[c]))
4196 slot_count++;
4197 }
4198 up_read(&info->groups_sem);
4199 }
Josef Bacik1406e432010-01-13 18:19:06 +00004200
David Sterba36523e952014-02-07 14:34:12 +01004201 /*
4202 * Global block reserve, exported as a space_info
4203 */
4204 slot_count++;
4205
Chris Mason7fde62b2010-03-16 15:40:10 -04004206 /* space_slots == 0 means they are asking for a count */
4207 if (space_args.space_slots == 0) {
4208 space_args.total_spaces = slot_count;
4209 goto out;
4210 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004211
Dan Rosenberg51788b12011-02-14 16:04:23 -05004212 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004213
Chris Mason7fde62b2010-03-16 15:40:10 -04004214 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004215
Chris Mason7fde62b2010-03-16 15:40:10 -04004216 /* we generally have at most 6 or so space infos, one for each raid
4217 * level. So, a whole page should be more than enough for everyone
4218 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004219 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004220 return -ENOMEM;
4221
4222 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004223 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004224 if (!dest)
4225 return -ENOMEM;
4226 dest_orig = dest;
4227
4228 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004229 for (i = 0; i < num_types; i++) {
4230 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004231
Dan Rosenberg51788b12011-02-14 16:04:23 -05004232 if (!slot_count)
4233 break;
4234
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004235 info = NULL;
4236 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004237 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004238 list) {
4239 if (tmp->flags == types[i]) {
4240 info = tmp;
4241 break;
4242 }
4243 }
4244 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004245
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004246 if (!info)
4247 continue;
4248 down_read(&info->groups_sem);
4249 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4250 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004251 get_block_group_info(&info->block_groups[c],
4252 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004253 memcpy(dest, &space, sizeof(space));
4254 dest++;
4255 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004256 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004257 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004258 if (!slot_count)
4259 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004260 }
4261 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004262 }
Josef Bacik1406e432010-01-13 18:19:06 +00004263
David Sterba36523e952014-02-07 14:34:12 +01004264 /*
4265 * Add global block reserve
4266 */
4267 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004268 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004269
4270 spin_lock(&block_rsv->lock);
4271 space.total_bytes = block_rsv->size;
4272 space.used_bytes = block_rsv->size - block_rsv->reserved;
4273 spin_unlock(&block_rsv->lock);
4274 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4275 memcpy(dest, &space, sizeof(space));
4276 space_args.total_spaces++;
4277 }
4278
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004279 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004280 (arg + sizeof(struct btrfs_ioctl_space_args));
4281
4282 if (copy_to_user(user_dest, dest_orig, alloc_size))
4283 ret = -EFAULT;
4284
4285 kfree(dest_orig);
4286out:
4287 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004288 ret = -EFAULT;
4289
4290 return ret;
4291}
4292
Miao Xie9a8c28b2012-11-26 08:40:43 +00004293static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4294 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004295{
Sage Weil46204592010-10-29 15:41:32 -04004296 struct btrfs_trans_handle *trans;
4297 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004298 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004299
Miao Xied4edf392013-02-20 09:17:06 +00004300 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004301 if (IS_ERR(trans)) {
4302 if (PTR_ERR(trans) != -ENOENT)
4303 return PTR_ERR(trans);
4304
4305 /* No running transaction, don't bother */
4306 transid = root->fs_info->last_trans_committed;
4307 goto out;
4308 }
Sage Weil46204592010-10-29 15:41:32 -04004309 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004310 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004311 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004312 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004313 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004314 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004315out:
Sage Weil46204592010-10-29 15:41:32 -04004316 if (argp)
4317 if (copy_to_user(argp, &transid, sizeof(transid)))
4318 return -EFAULT;
4319 return 0;
4320}
4321
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004322static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004323 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004324{
Sage Weil46204592010-10-29 15:41:32 -04004325 u64 transid;
4326
4327 if (argp) {
4328 if (copy_from_user(&transid, argp, sizeof(transid)))
4329 return -EFAULT;
4330 } else {
4331 transid = 0; /* current trans */
4332 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004333 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004334}
4335
Miao Xieb8e95482012-11-26 08:48:01 +00004336static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004338 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004339 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004340 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004341
4342 if (!capable(CAP_SYS_ADMIN))
4343 return -EPERM;
4344
4345 sa = memdup_user(arg, sizeof(*sa));
4346 if (IS_ERR(sa))
4347 return PTR_ERR(sa);
4348
Miao Xieb8e95482012-11-26 08:48:01 +00004349 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4350 ret = mnt_want_write_file(file);
4351 if (ret)
4352 goto out;
4353 }
4354
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004355 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004356 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4357 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004358
Filipe Manana5afe6ce2020-01-16 11:29:20 +00004359 /*
4360 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
4361 * error. This is important as it allows user space to know how much
4362 * progress scrub has done. For example, if scrub is canceled we get
4363 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
4364 * space. Later user space can inspect the progress from the structure
4365 * btrfs_ioctl_scrub_args and resume scrub from where it left off
4366 * previously (btrfs-progs does this).
4367 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
4368 * then return -EFAULT to signal the structure was not copied or it may
4369 * be corrupt and unreliable due to a partial copy.
4370 */
4371 if (copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004372 ret = -EFAULT;
4373
Miao Xieb8e95482012-11-26 08:48:01 +00004374 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4375 mnt_drop_write_file(file);
4376out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004377 kfree(sa);
4378 return ret;
4379}
4380
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004381static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004382{
4383 if (!capable(CAP_SYS_ADMIN))
4384 return -EPERM;
4385
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004386 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004387}
4388
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004389static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004390 void __user *arg)
4391{
4392 struct btrfs_ioctl_scrub_args *sa;
4393 int ret;
4394
4395 if (!capable(CAP_SYS_ADMIN))
4396 return -EPERM;
4397
4398 sa = memdup_user(arg, sizeof(*sa));
4399 if (IS_ERR(sa))
4400 return PTR_ERR(sa);
4401
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004402 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004403
Filipe Manana4fa99b02018-12-14 19:45:13 +00004404 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004405 ret = -EFAULT;
4406
4407 kfree(sa);
4408 return ret;
4409}
4410
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004411static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004412 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004413{
4414 struct btrfs_ioctl_get_dev_stats *sa;
4415 int ret;
4416
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004417 sa = memdup_user(arg, sizeof(*sa));
4418 if (IS_ERR(sa))
4419 return PTR_ERR(sa);
4420
David Sterbab27f7c02012-06-22 06:30:39 -06004421 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4422 kfree(sa);
4423 return -EPERM;
4424 }
4425
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004426 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004427
Filipe Mananaeee99572018-12-14 19:45:22 +00004428 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004429 ret = -EFAULT;
4430
4431 kfree(sa);
4432 return ret;
4433}
4434
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004435static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4436 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004437{
4438 struct btrfs_ioctl_dev_replace_args *p;
4439 int ret;
4440
4441 if (!capable(CAP_SYS_ADMIN))
4442 return -EPERM;
4443
4444 p = memdup_user(arg, sizeof(*p));
4445 if (IS_ERR(p))
4446 return PTR_ERR(p);
4447
4448 switch (p->cmd) {
4449 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004450 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004451 ret = -EROFS;
4452 goto out;
4453 }
David Sterba171938e2017-03-28 14:44:21 +02004454 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004455 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004456 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004457 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004458 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004459 }
4460 break;
4461 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004462 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004463 ret = 0;
4464 break;
4465 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004466 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004467 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004468 break;
4469 default:
4470 ret = -EINVAL;
4471 break;
4472 }
4473
Filipe Mananad3a53282019-01-08 11:42:09 +00004474 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004475 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004476out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004477 kfree(p);
4478 return ret;
4479}
4480
Jan Schmidtd7728c92011-07-07 16:48:38 +02004481static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4482{
4483 int ret = 0;
4484 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004485 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004486 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004487 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004488 struct inode_fs_paths *ipath = NULL;
4489 struct btrfs_path *path;
4490
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004491 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004492 return -EPERM;
4493
4494 path = btrfs_alloc_path();
4495 if (!path) {
4496 ret = -ENOMEM;
4497 goto out;
4498 }
4499
4500 ipa = memdup_user(arg, sizeof(*ipa));
4501 if (IS_ERR(ipa)) {
4502 ret = PTR_ERR(ipa);
4503 ipa = NULL;
4504 goto out;
4505 }
4506
4507 size = min_t(u32, ipa->size, 4096);
4508 ipath = init_ipath(size, root, path);
4509 if (IS_ERR(ipath)) {
4510 ret = PTR_ERR(ipath);
4511 ipath = NULL;
4512 goto out;
4513 }
4514
4515 ret = paths_from_inode(ipa->inum, ipath);
4516 if (ret < 0)
4517 goto out;
4518
4519 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004520 rel_ptr = ipath->fspath->val[i] -
4521 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004522 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004523 }
4524
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004525 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4526 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004527 if (ret) {
4528 ret = -EFAULT;
4529 goto out;
4530 }
4531
4532out:
4533 btrfs_free_path(path);
4534 free_ipath(ipath);
4535 kfree(ipa);
4536
4537 return ret;
4538}
4539
4540static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4541{
4542 struct btrfs_data_container *inodes = ctx;
4543 const size_t c = 3 * sizeof(u64);
4544
4545 if (inodes->bytes_left >= c) {
4546 inodes->bytes_left -= c;
4547 inodes->val[inodes->elem_cnt] = inum;
4548 inodes->val[inodes->elem_cnt + 1] = offset;
4549 inodes->val[inodes->elem_cnt + 2] = root;
4550 inodes->elem_cnt += 3;
4551 } else {
4552 inodes->bytes_missing += c - inodes->bytes_left;
4553 inodes->bytes_left = 0;
4554 inodes->elem_missed += 3;
4555 }
4556
4557 return 0;
4558}
4559
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004560static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004561 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004562{
4563 int ret = 0;
4564 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004565 struct btrfs_ioctl_logical_ino_args *loi;
4566 struct btrfs_data_container *inodes = NULL;
4567 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004568 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004569
4570 if (!capable(CAP_SYS_ADMIN))
4571 return -EPERM;
4572
4573 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304574 if (IS_ERR(loi))
4575 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004576
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004577 if (version == 1) {
4578 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004579 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004580 } else {
4581 /* All reserved bits must be 0 for now */
4582 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4583 ret = -EINVAL;
4584 goto out_loi;
4585 }
4586 /* Only accept flags we have defined so far */
4587 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4588 ret = -EINVAL;
4589 goto out_loi;
4590 }
4591 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004592 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004593 }
4594
Jan Schmidtd7728c92011-07-07 16:48:38 +02004595 path = btrfs_alloc_path();
4596 if (!path) {
4597 ret = -ENOMEM;
4598 goto out;
4599 }
4600
Jan Schmidtd7728c92011-07-07 16:48:38 +02004601 inodes = init_data_container(size);
4602 if (IS_ERR(inodes)) {
4603 ret = PTR_ERR(inodes);
4604 inodes = NULL;
4605 goto out;
4606 }
4607
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004608 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004609 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004610 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004611 ret = -ENOENT;
4612 if (ret < 0)
4613 goto out;
4614
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004615 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4616 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004617 if (ret)
4618 ret = -EFAULT;
4619
4620out:
4621 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004622 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004623out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004624 kfree(loi);
4625
4626 return ret;
4627}
4628
David Sterba008ef092018-03-21 02:05:27 +01004629void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004630 struct btrfs_ioctl_balance_args *bargs)
4631{
4632 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4633
4634 bargs->flags = bctl->flags;
4635
David Sterba3009a622018-03-21 01:31:04 +01004636 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004637 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4638 if (atomic_read(&fs_info->balance_pause_req))
4639 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004640 if (atomic_read(&fs_info->balance_cancel_req))
4641 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004642
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004643 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4644 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4645 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004646
David Sterba008ef092018-03-21 02:05:27 +01004647 spin_lock(&fs_info->balance_lock);
4648 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4649 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004650}
4651
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004652static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004653{
Al Viro496ad9a2013-01-23 17:07:38 -05004654 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004655 struct btrfs_fs_info *fs_info = root->fs_info;
4656 struct btrfs_ioctl_balance_args *bargs;
4657 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004658 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004659 int ret;
4660
4661 if (!capable(CAP_SYS_ADMIN))
4662 return -EPERM;
4663
Liu Boe54bfa312012-06-29 03:58:48 -06004664 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004665 if (ret)
4666 return ret;
4667
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004668again:
David Sterba171938e2017-03-28 14:44:21 +02004669 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004670 mutex_lock(&fs_info->balance_mutex);
4671 need_unlock = true;
4672 goto locked;
4673 }
4674
4675 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004676 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004677 * (1) some other op is running
4678 * (2) balance is running
4679 * (3) balance is paused -- special case (think resume)
4680 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004681 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004682 if (fs_info->balance_ctl) {
4683 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004684 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004685 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004686 /*
4687 * Lock released to allow other waiters to continue,
4688 * we'll reexamine the status again.
4689 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004690 mutex_lock(&fs_info->balance_mutex);
4691
4692 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004693 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004694 /* this is (3) */
4695 need_unlock = false;
4696 goto locked;
4697 }
4698
4699 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004700 goto again;
4701 } else {
4702 /* this is (2) */
4703 mutex_unlock(&fs_info->balance_mutex);
4704 ret = -EINPROGRESS;
4705 goto out;
4706 }
4707 } else {
4708 /* this is (1) */
4709 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004710 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004711 goto out;
4712 }
4713
4714locked:
David Sterba171938e2017-03-28 14:44:21 +02004715 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004716
4717 if (arg) {
4718 bargs = memdup_user(arg, sizeof(*bargs));
4719 if (IS_ERR(bargs)) {
4720 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004721 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004722 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004723
4724 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4725 if (!fs_info->balance_ctl) {
4726 ret = -ENOTCONN;
4727 goto out_bargs;
4728 }
4729
4730 bctl = fs_info->balance_ctl;
4731 spin_lock(&fs_info->balance_lock);
4732 bctl->flags |= BTRFS_BALANCE_RESUME;
4733 spin_unlock(&fs_info->balance_lock);
4734
4735 goto do_balance;
4736 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004737 } else {
4738 bargs = NULL;
4739 }
4740
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004741 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004742 ret = -EINPROGRESS;
4743 goto out_bargs;
4744 }
4745
David Sterba8d2db782015-11-04 15:38:29 +01004746 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004747 if (!bctl) {
4748 ret = -ENOMEM;
4749 goto out_bargs;
4750 }
4751
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004752 if (arg) {
4753 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4754 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4755 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4756
4757 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004758 } else {
4759 /* balance everything - no filters */
4760 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004761 }
4762
David Sterba8eb93452015-10-12 16:55:54 +02004763 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4764 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004765 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004766 }
4767
Ilya Dryomovde322262012-01-16 22:04:49 +02004768do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004769 /*
David Sterba149196a2018-03-20 20:23:09 +01004770 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4771 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4772 * restriper was paused all the way until unmount, in free_fs_info.
4773 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004774 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004775 need_unlock = false;
4776
David Sterba6fcf6e22018-05-07 17:44:03 +02004777 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004778 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004779
Filipe Mananad00c2d92019-01-08 11:42:01 +00004780 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004781 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4782 ret = -EFAULT;
4783 }
4784
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004785out_bctl:
4786 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004787out_bargs:
4788 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004789out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004790 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004791 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004792 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004793out:
Liu Boe54bfa312012-06-29 03:58:48 -06004794 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004795 return ret;
4796}
4797
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004798static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004799{
4800 if (!capable(CAP_SYS_ADMIN))
4801 return -EPERM;
4802
4803 switch (cmd) {
4804 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004805 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004806 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004807 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004808 }
4809
4810 return -EINVAL;
4811}
4812
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004813static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004814 void __user *arg)
4815{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004816 struct btrfs_ioctl_balance_args *bargs;
4817 int ret = 0;
4818
4819 if (!capable(CAP_SYS_ADMIN))
4820 return -EPERM;
4821
4822 mutex_lock(&fs_info->balance_mutex);
4823 if (!fs_info->balance_ctl) {
4824 ret = -ENOTCONN;
4825 goto out;
4826 }
4827
David Sterba8d2db782015-11-04 15:38:29 +01004828 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004829 if (!bargs) {
4830 ret = -ENOMEM;
4831 goto out;
4832 }
4833
David Sterba008ef092018-03-21 02:05:27 +01004834 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004835
4836 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4837 ret = -EFAULT;
4838
4839 kfree(bargs);
4840out:
4841 mutex_unlock(&fs_info->balance_mutex);
4842 return ret;
4843}
4844
Miao Xie905b0dd2012-11-26 08:50:11 +00004845static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004846{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004847 struct inode *inode = file_inode(file);
4848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004849 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004850 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004851
4852 if (!capable(CAP_SYS_ADMIN))
4853 return -EPERM;
4854
Miao Xie905b0dd2012-11-26 08:50:11 +00004855 ret = mnt_want_write_file(file);
4856 if (ret)
4857 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004858
4859 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004860 if (IS_ERR(sa)) {
4861 ret = PTR_ERR(sa);
4862 goto drop_write;
4863 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004864
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004865 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004866
4867 switch (sa->cmd) {
4868 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004869 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004870 break;
4871 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004872 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004873 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004874 default:
4875 ret = -EINVAL;
4876 break;
4877 }
4878
Arne Jansen5d13a372011-09-14 15:53:51 +02004879 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004880 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004881drop_write:
4882 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004883 return ret;
4884}
4885
Miao Xie905b0dd2012-11-26 08:50:11 +00004886static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004887{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004888 struct inode *inode = file_inode(file);
4889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4890 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004891 struct btrfs_ioctl_qgroup_assign_args *sa;
4892 struct btrfs_trans_handle *trans;
4893 int ret;
4894 int err;
4895
4896 if (!capable(CAP_SYS_ADMIN))
4897 return -EPERM;
4898
Miao Xie905b0dd2012-11-26 08:50:11 +00004899 ret = mnt_want_write_file(file);
4900 if (ret)
4901 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004902
4903 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004904 if (IS_ERR(sa)) {
4905 ret = PTR_ERR(sa);
4906 goto drop_write;
4907 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004908
4909 trans = btrfs_join_transaction(root);
4910 if (IS_ERR(trans)) {
4911 ret = PTR_ERR(trans);
4912 goto out;
4913 }
4914
Arne Jansen5d13a372011-09-14 15:53:51 +02004915 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004916 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004917 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004918 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004919 }
4920
Qu Wenruoe082f562015-02-27 16:24:28 +08004921 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004922 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004923 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004924 btrfs_handle_fs_error(fs_info, err,
4925 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004926 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004927 if (err && !ret)
4928 ret = err;
4929
4930out:
4931 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004932drop_write:
4933 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004934 return ret;
4935}
4936
Miao Xie905b0dd2012-11-26 08:50:11 +00004937static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004938{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004939 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004940 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004941 struct btrfs_ioctl_qgroup_create_args *sa;
4942 struct btrfs_trans_handle *trans;
4943 int ret;
4944 int err;
4945
4946 if (!capable(CAP_SYS_ADMIN))
4947 return -EPERM;
4948
Miao Xie905b0dd2012-11-26 08:50:11 +00004949 ret = mnt_want_write_file(file);
4950 if (ret)
4951 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004952
4953 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004954 if (IS_ERR(sa)) {
4955 ret = PTR_ERR(sa);
4956 goto drop_write;
4957 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004958
Miao Xied86e56c2012-11-15 11:35:41 +00004959 if (!sa->qgroupid) {
4960 ret = -EINVAL;
4961 goto out;
4962 }
4963
Arne Jansen5d13a372011-09-14 15:53:51 +02004964 trans = btrfs_join_transaction(root);
4965 if (IS_ERR(trans)) {
4966 ret = PTR_ERR(trans);
4967 goto out;
4968 }
4969
Arne Jansen5d13a372011-09-14 15:53:51 +02004970 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004971 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004972 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004973 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004974 }
4975
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004976 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004977 if (err && !ret)
4978 ret = err;
4979
4980out:
4981 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004982drop_write:
4983 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004984 return ret;
4985}
4986
Miao Xie905b0dd2012-11-26 08:50:11 +00004987static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004988{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004989 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004990 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004991 struct btrfs_ioctl_qgroup_limit_args *sa;
4992 struct btrfs_trans_handle *trans;
4993 int ret;
4994 int err;
4995 u64 qgroupid;
4996
4997 if (!capable(CAP_SYS_ADMIN))
4998 return -EPERM;
4999
Miao Xie905b0dd2012-11-26 08:50:11 +00005000 ret = mnt_want_write_file(file);
5001 if (ret)
5002 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005003
5004 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005005 if (IS_ERR(sa)) {
5006 ret = PTR_ERR(sa);
5007 goto drop_write;
5008 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005009
5010 trans = btrfs_join_transaction(root);
5011 if (IS_ERR(trans)) {
5012 ret = PTR_ERR(trans);
5013 goto out;
5014 }
5015
5016 qgroupid = sa->qgroupid;
5017 if (!qgroupid) {
5018 /* take the current subvol as qgroup */
5019 qgroupid = root->root_key.objectid;
5020 }
5021
Lu Fengqif0042d52018-07-18 14:45:35 +08005022 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005023
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005024 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005025 if (err && !ret)
5026 ret = err;
5027
5028out:
5029 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005030drop_write:
5031 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005032 return ret;
5033}
5034
Jan Schmidt2f232032013-04-25 16:04:51 +00005035static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5036{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005037 struct inode *inode = file_inode(file);
5038 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005039 struct btrfs_ioctl_quota_rescan_args *qsa;
5040 int ret;
5041
5042 if (!capable(CAP_SYS_ADMIN))
5043 return -EPERM;
5044
5045 ret = mnt_want_write_file(file);
5046 if (ret)
5047 return ret;
5048
5049 qsa = memdup_user(arg, sizeof(*qsa));
5050 if (IS_ERR(qsa)) {
5051 ret = PTR_ERR(qsa);
5052 goto drop_write;
5053 }
5054
5055 if (qsa->flags) {
5056 ret = -EINVAL;
5057 goto out;
5058 }
5059
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005060 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005061
5062out:
5063 kfree(qsa);
5064drop_write:
5065 mnt_drop_write_file(file);
5066 return ret;
5067}
5068
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005069static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
5070 void __user *arg)
Jan Schmidt2f232032013-04-25 16:04:51 +00005071{
Jan Schmidt2f232032013-04-25 16:04:51 +00005072 struct btrfs_ioctl_quota_rescan_args *qsa;
5073 int ret = 0;
5074
5075 if (!capable(CAP_SYS_ADMIN))
5076 return -EPERM;
5077
David Sterba8d2db782015-11-04 15:38:29 +01005078 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005079 if (!qsa)
5080 return -ENOMEM;
5081
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005082 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005083 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005084 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005085 }
5086
5087 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5088 ret = -EFAULT;
5089
5090 kfree(qsa);
5091 return ret;
5092}
5093
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005094static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
5095 void __user *arg)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005096{
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005097 if (!capable(CAP_SYS_ADMIN))
5098 return -EPERM;
5099
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005100 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005101}
5102
Hugo Millsabccd002014-01-30 20:17:00 +00005103static long _btrfs_ioctl_set_received_subvol(struct file *file,
5104 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005105{
Al Viro496ad9a2013-01-23 17:07:38 -05005106 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005107 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005108 struct btrfs_root *root = BTRFS_I(inode)->root;
5109 struct btrfs_root_item *root_item = &root->root_item;
5110 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07005111 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005112 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005113 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005114
David Sterbabd60ea02014-01-16 15:50:22 +01005115 if (!inode_owner_or_capable(inode))
5116 return -EPERM;
5117
Alexander Block8ea05e32012-07-25 17:35:53 +02005118 ret = mnt_want_write_file(file);
5119 if (ret < 0)
5120 return ret;
5121
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005122 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005123
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005124 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005125 ret = -EINVAL;
5126 goto out;
5127 }
5128
5129 if (btrfs_root_readonly(root)) {
5130 ret = -EROFS;
5131 goto out;
5132 }
5133
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005134 /*
5135 * 1 - root item
5136 * 2 - uuid items (received uuid + subvol uuid)
5137 */
5138 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005139 if (IS_ERR(trans)) {
5140 ret = PTR_ERR(trans);
5141 trans = NULL;
5142 goto out;
5143 }
5144
5145 sa->rtransid = trans->transid;
5146 sa->rtime.sec = ct.tv_sec;
5147 sa->rtime.nsec = ct.tv_nsec;
5148
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005149 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5150 BTRFS_UUID_SIZE);
5151 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005152 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005153 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005154 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5155 root->root_key.objectid);
5156 if (ret && ret != -ENOENT) {
5157 btrfs_abort_transaction(trans, ret);
5158 btrfs_end_transaction(trans);
5159 goto out;
5160 }
5161 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005162 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5163 btrfs_set_root_stransid(root_item, sa->stransid);
5164 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005165 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5166 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5167 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5168 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005169
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005170 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005171 &root->root_key, &root->root_item);
5172 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005173 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005174 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005175 }
5176 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005177 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005178 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5179 root->root_key.objectid);
5180 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005181 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005182 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005183 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005184 }
5185 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005186 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005187out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005188 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005189 mnt_drop_write_file(file);
5190 return ret;
5191}
5192
5193#ifdef CONFIG_64BIT
5194static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5195 void __user *arg)
5196{
5197 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5198 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5199 int ret = 0;
5200
5201 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305202 if (IS_ERR(args32))
5203 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005204
David Sterba8d2db782015-11-04 15:38:29 +01005205 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005206 if (!args64) {
5207 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005208 goto out;
5209 }
5210
5211 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5212 args64->stransid = args32->stransid;
5213 args64->rtransid = args32->rtransid;
5214 args64->stime.sec = args32->stime.sec;
5215 args64->stime.nsec = args32->stime.nsec;
5216 args64->rtime.sec = args32->rtime.sec;
5217 args64->rtime.nsec = args32->rtime.nsec;
5218 args64->flags = args32->flags;
5219
5220 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5221 if (ret)
5222 goto out;
5223
5224 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5225 args32->stransid = args64->stransid;
5226 args32->rtransid = args64->rtransid;
5227 args32->stime.sec = args64->stime.sec;
5228 args32->stime.nsec = args64->stime.nsec;
5229 args32->rtime.sec = args64->rtime.sec;
5230 args32->rtime.nsec = args64->rtime.nsec;
5231 args32->flags = args64->flags;
5232
5233 ret = copy_to_user(arg, args32, sizeof(*args32));
5234 if (ret)
5235 ret = -EFAULT;
5236
5237out:
5238 kfree(args32);
5239 kfree(args64);
5240 return ret;
5241}
5242#endif
5243
5244static long btrfs_ioctl_set_received_subvol(struct file *file,
5245 void __user *arg)
5246{
5247 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5248 int ret = 0;
5249
5250 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305251 if (IS_ERR(sa))
5252 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005253
5254 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5255
5256 if (ret)
5257 goto out;
5258
Alexander Block8ea05e32012-07-25 17:35:53 +02005259 ret = copy_to_user(arg, sa, sizeof(*sa));
5260 if (ret)
5261 ret = -EFAULT;
5262
5263out:
5264 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005265 return ret;
5266}
5267
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005268static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
5269 void __user *arg)
jeff.liu867ab662013-01-05 02:48:01 +00005270{
Anand Jaina1b83ac2013-07-19 17:39:32 +08005271 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005272 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005273 char label[BTRFS_LABEL_SIZE];
5274
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005275 spin_lock(&fs_info->super_lock);
5276 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5277 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005278
5279 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005280
5281 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005282 btrfs_warn(fs_info,
5283 "label is too long, return the first %zu bytes",
5284 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005285 }
5286
jeff.liu867ab662013-01-05 02:48:01 +00005287 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005288
5289 return ret ? -EFAULT : 0;
5290}
5291
jeff.liua8bfd4a2013-01-05 02:48:08 +00005292static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005294 struct inode *inode = file_inode(file);
5295 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5296 struct btrfs_root *root = BTRFS_I(inode)->root;
5297 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005298 struct btrfs_trans_handle *trans;
5299 char label[BTRFS_LABEL_SIZE];
5300 int ret;
5301
5302 if (!capable(CAP_SYS_ADMIN))
5303 return -EPERM;
5304
5305 if (copy_from_user(label, arg, sizeof(label)))
5306 return -EFAULT;
5307
5308 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005309 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005310 "unable to set label with more than %d bytes",
5311 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005312 return -EINVAL;
5313 }
5314
5315 ret = mnt_want_write_file(file);
5316 if (ret)
5317 return ret;
5318
jeff.liua8bfd4a2013-01-05 02:48:08 +00005319 trans = btrfs_start_transaction(root, 0);
5320 if (IS_ERR(trans)) {
5321 ret = PTR_ERR(trans);
5322 goto out_unlock;
5323 }
5324
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005325 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005326 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005327 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005328 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005329
5330out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005331 mnt_drop_write_file(file);
5332 return ret;
5333}
5334
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005335#define INIT_FEATURE_FLAGS(suffix) \
5336 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5337 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5338 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5339
David Sterbad5131b62016-02-17 15:26:27 +01005340int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005341{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005342 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005343 INIT_FEATURE_FLAGS(SUPP),
5344 INIT_FEATURE_FLAGS(SAFE_SET),
5345 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5346 };
5347
5348 if (copy_to_user(arg, &features, sizeof(features)))
5349 return -EFAULT;
5350
5351 return 0;
5352}
5353
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005354static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
5355 void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005356{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005357 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005358 struct btrfs_ioctl_feature_flags features;
5359
5360 features.compat_flags = btrfs_super_compat_flags(super_block);
5361 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5362 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5363
5364 if (copy_to_user(arg, &features, sizeof(features)))
5365 return -EFAULT;
5366
5367 return 0;
5368}
5369
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005370static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005371 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005372 u64 change_mask, u64 flags, u64 supported_flags,
5373 u64 safe_set, u64 safe_clear)
5374{
David Sterbaf10152b2019-08-01 19:07:55 +02005375 const char *type = btrfs_feature_set_name(set);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005376 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005377 u64 disallowed, unsupported;
5378 u64 set_mask = flags & change_mask;
5379 u64 clear_mask = ~flags & change_mask;
5380
5381 unsupported = set_mask & ~supported_flags;
5382 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005383 names = btrfs_printable_features(set, unsupported);
5384 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005385 btrfs_warn(fs_info,
5386 "this kernel does not support the %s feature bit%s",
5387 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005388 kfree(names);
5389 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005390 btrfs_warn(fs_info,
5391 "this kernel does not support %s bits 0x%llx",
5392 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005393 return -EOPNOTSUPP;
5394 }
5395
5396 disallowed = set_mask & ~safe_set;
5397 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005398 names = btrfs_printable_features(set, disallowed);
5399 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005400 btrfs_warn(fs_info,
5401 "can't set the %s feature bit%s while mounted",
5402 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005403 kfree(names);
5404 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005405 btrfs_warn(fs_info,
5406 "can't set %s bits 0x%llx while mounted",
5407 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005408 return -EPERM;
5409 }
5410
5411 disallowed = clear_mask & ~safe_clear;
5412 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005413 names = btrfs_printable_features(set, disallowed);
5414 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005415 btrfs_warn(fs_info,
5416 "can't clear the %s feature bit%s while mounted",
5417 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005418 kfree(names);
5419 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005420 btrfs_warn(fs_info,
5421 "can't clear %s bits 0x%llx while mounted",
5422 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005423 return -EPERM;
5424 }
5425
5426 return 0;
5427}
5428
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005429#define check_feature(fs_info, change_mask, flags, mask_base) \
5430check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005431 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5432 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5433 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5434
5435static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005437 struct inode *inode = file_inode(file);
5438 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5439 struct btrfs_root *root = BTRFS_I(inode)->root;
5440 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005441 struct btrfs_ioctl_feature_flags flags[2];
5442 struct btrfs_trans_handle *trans;
5443 u64 newflags;
5444 int ret;
5445
5446 if (!capable(CAP_SYS_ADMIN))
5447 return -EPERM;
5448
5449 if (copy_from_user(flags, arg, sizeof(flags)))
5450 return -EFAULT;
5451
5452 /* Nothing to do */
5453 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5454 !flags[0].incompat_flags)
5455 return 0;
5456
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005457 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005458 flags[1].compat_flags, COMPAT);
5459 if (ret)
5460 return ret;
5461
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005462 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005463 flags[1].compat_ro_flags, COMPAT_RO);
5464 if (ret)
5465 return ret;
5466
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005467 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005468 flags[1].incompat_flags, INCOMPAT);
5469 if (ret)
5470 return ret;
5471
David Sterba7ab19622016-05-04 11:32:00 +02005472 ret = mnt_want_write_file(file);
5473 if (ret)
5474 return ret;
5475
David Sterba8051aa12014-02-07 14:34:04 +01005476 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005477 if (IS_ERR(trans)) {
5478 ret = PTR_ERR(trans);
5479 goto out_drop_write;
5480 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005481
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005482 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005483 newflags = btrfs_super_compat_flags(super_block);
5484 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5485 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5486 btrfs_set_super_compat_flags(super_block, newflags);
5487
5488 newflags = btrfs_super_compat_ro_flags(super_block);
5489 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5490 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5491 btrfs_set_super_compat_ro_flags(super_block, newflags);
5492
5493 newflags = btrfs_super_incompat_flags(super_block);
5494 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5495 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5496 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005497 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005498
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005499 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005500out_drop_write:
5501 mnt_drop_write_file(file);
5502
5503 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005504}
5505
Josef Bacik2351f432017-09-27 10:43:13 -04005506static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5507{
5508 struct btrfs_ioctl_send_args *arg;
5509 int ret;
5510
5511 if (compat) {
5512#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5513 struct btrfs_ioctl_send_args_32 args32;
5514
5515 ret = copy_from_user(&args32, argp, sizeof(args32));
5516 if (ret)
5517 return -EFAULT;
5518 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5519 if (!arg)
5520 return -ENOMEM;
5521 arg->send_fd = args32.send_fd;
5522 arg->clone_sources_count = args32.clone_sources_count;
5523 arg->clone_sources = compat_ptr(args32.clone_sources);
5524 arg->parent_root = args32.parent_root;
5525 arg->flags = args32.flags;
5526 memcpy(arg->reserved, args32.reserved,
5527 sizeof(args32.reserved));
5528#else
5529 return -ENOTTY;
5530#endif
5531 } else {
5532 arg = memdup_user(argp, sizeof(*arg));
5533 if (IS_ERR(arg))
5534 return PTR_ERR(arg);
5535 }
5536 ret = btrfs_ioctl_send(file, arg);
5537 kfree(arg);
5538 return ret;
5539}
5540
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005541long btrfs_ioctl(struct file *file, unsigned int
5542 cmd, unsigned long arg)
5543{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005544 struct inode *inode = file_inode(file);
5545 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5546 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005547 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005548
5549 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005550 case FS_IOC_GETFLAGS:
5551 return btrfs_ioctl_getflags(file, argp);
5552 case FS_IOC_SETFLAGS:
5553 return btrfs_ioctl_setflags(file, argp);
5554 case FS_IOC_GETVERSION:
5555 return btrfs_ioctl_getversion(file, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05005556 case FS_IOC_GETFSLABEL:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005557 return btrfs_ioctl_get_fslabel(fs_info, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05005558 case FS_IOC_SETFSLABEL:
5559 return btrfs_ioctl_set_fslabel(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005560 case FITRIM:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005561 return btrfs_ioctl_fitrim(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005562 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005563 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005564 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005565 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005566 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005567 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005568 case BTRFS_IOC_SUBVOL_CREATE_V2:
5569 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005570 case BTRFS_IOC_SNAP_DESTROY:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03005571 return btrfs_ioctl_snap_destroy(file, argp, false);
5572 case BTRFS_IOC_SNAP_DESTROY_V2:
5573 return btrfs_ioctl_snap_destroy(file, argp, true);
Li Zefan0caa1022010-12-20 16:30:25 +08005574 case BTRFS_IOC_SUBVOL_GETFLAGS:
5575 return btrfs_ioctl_subvol_getflags(file, argp);
5576 case BTRFS_IOC_SUBVOL_SETFLAGS:
5577 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005578 case BTRFS_IOC_DEFAULT_SUBVOL:
5579 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005580 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005581 return btrfs_ioctl_defrag(file, NULL);
5582 case BTRFS_IOC_DEFRAG_RANGE:
5583 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005584 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005585 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005586 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005587 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005588 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005589 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005590 case BTRFS_IOC_RM_DEV_V2:
5591 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005592 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005593 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005594 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005595 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005596 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005597 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005598 case BTRFS_IOC_TREE_SEARCH:
5599 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005600 case BTRFS_IOC_TREE_SEARCH_V2:
5601 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005602 case BTRFS_IOC_INO_LOOKUP:
5603 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005604 case BTRFS_IOC_INO_PATHS:
5605 return btrfs_ioctl_ino_to_path(root, argp);
5606 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005607 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5608 case BTRFS_IOC_LOGICAL_INO_V2:
5609 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005610 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005611 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005612 case BTRFS_IOC_SYNC: {
5613 int ret;
5614
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005615 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005616 if (ret)
5617 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005618 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005619 /*
5620 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005621 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005622 * processing uncleaned subvols.
5623 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005624 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005625 return ret;
5626 }
Sage Weil46204592010-10-29 15:41:32 -04005627 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005628 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005629 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005630 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005631 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005632 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005633 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005634 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005635 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005636 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005637 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005638 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005639 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005640 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005641 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005642 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005643 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5644 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005645#ifdef CONFIG_64BIT
5646 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5647 return btrfs_ioctl_set_received_subvol_32(file, argp);
5648#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005649 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005650 return _btrfs_ioctl_send(file, argp, false);
5651#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5652 case BTRFS_IOC_SEND_32:
5653 return _btrfs_ioctl_send(file, argp, true);
5654#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005655 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005656 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005657 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005658 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005659 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005660 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005661 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005662 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005663 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005664 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005665 case BTRFS_IOC_QUOTA_RESCAN:
5666 return btrfs_ioctl_quota_rescan(file, argp);
5667 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005668 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005669 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005670 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005671 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005672 return btrfs_ioctl_dev_replace(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005673 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005674 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005675 case BTRFS_IOC_GET_FEATURES:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005676 return btrfs_ioctl_get_features(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005677 case BTRFS_IOC_SET_FEATURES:
5678 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005679 case FS_IOC_FSGETXATTR:
5680 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005681 case FS_IOC_FSSETXATTR:
5682 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005683 case BTRFS_IOC_GET_SUBVOL_INFO:
5684 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005685 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5686 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005687 case BTRFS_IOC_INO_LOOKUP_USER:
5688 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005689 }
5690
5691 return -ENOTTY;
5692}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005693
5694#ifdef CONFIG_COMPAT
5695long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5696{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005697 /*
5698 * These all access 32-bit values anyway so no further
5699 * handling is necessary.
5700 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005701 switch (cmd) {
5702 case FS_IOC32_GETFLAGS:
5703 cmd = FS_IOC_GETFLAGS;
5704 break;
5705 case FS_IOC32_SETFLAGS:
5706 cmd = FS_IOC_SETFLAGS;
5707 break;
5708 case FS_IOC32_GETVERSION:
5709 cmd = FS_IOC_GETVERSION;
5710 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005711 }
5712
5713 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5714}
5715#endif