blob: 7755b503b348e4c5140119edca034d6d2b427a60 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008#include <linux/file.h>
9#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040010#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040011#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040014#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040015#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040016#include <linux/mount.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040017#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040018#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/compat.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020022#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000024#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020025#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000026#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070027#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050028#include <linux/iversion.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040029#include "ctree.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040033#include "print-tree.h"
34#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080036#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020037#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040038#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020039#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010040#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000041#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040042#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070043#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000044#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080045#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046
Hugo Millsabccd002014-01-30 20:17:00 +000047#ifdef CONFIG_64BIT
48/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
49 * structures are incorrect, as the timespec structure from userspace
50 * is 4 bytes too small. We define these alternatives here to teach
51 * the kernel about the 32-bit struct packing.
52 */
53struct btrfs_ioctl_timespec_32 {
54 __u64 sec;
55 __u32 nsec;
56} __attribute__ ((__packed__));
57
58struct btrfs_ioctl_received_subvol_args_32 {
59 char uuid[BTRFS_UUID_SIZE]; /* in */
60 __u64 stransid; /* in */
61 __u64 rtransid; /* out */
62 struct btrfs_ioctl_timespec_32 stime; /* in */
63 struct btrfs_ioctl_timespec_32 rtime; /* out */
64 __u64 flags; /* in */
65 __u64 reserved[16]; /* in */
66} __attribute__ ((__packed__));
67
68#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
69 struct btrfs_ioctl_received_subvol_args_32)
70#endif
71
Josef Bacik2351f432017-09-27 10:43:13 -040072#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
73struct btrfs_ioctl_send_args_32 {
74 __s64 send_fd; /* in */
75 __u64 clone_sources_count; /* in */
76 compat_uptr_t clone_sources; /* in */
77 __u64 parent_root; /* in */
78 __u64 flags; /* in */
79 __u64 reserved[4]; /* in */
80} __attribute__ ((__packed__));
81
82#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
83 struct btrfs_ioctl_send_args_32)
84#endif
Hugo Millsabccd002014-01-30 20:17:00 +000085
Mark Fasheh416161d2013-08-06 11:42:51 -070086static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070087 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
88 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070089
Christoph Hellwig6cbff002009-04-17 10:37:41 +020090/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020091static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
92 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093{
David Sterba1905a0f2018-03-26 18:52:15 +020094 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020096 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097 return flags & ~FS_DIRSYNC_FL;
98 else
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
100}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400101
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
104 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200106static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107{
108 unsigned int iflags = 0;
109
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
124
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900125 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000126 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129
130 return iflags;
131}
132
133/*
134 * Update inode->i_flags based on the btrfs internal flags.
135 */
David Sterba7b6a2212018-03-26 18:40:21 +0200136void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200137{
David Sterba5c57b8b2018-04-23 15:45:18 +0200138 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100139 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140
David Sterba5c57b8b2018-04-23 15:45:18 +0200141 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100142 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200145 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200147 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200149 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_DIRSYNC;
151
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
154 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200155}
156
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
David Sterba5c57b8b2018-04-23 15:45:18 +0200159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
David Sterba5ba76ab2018-03-26 18:52:15 +0200167/* Check if @flags are a supported and valid set of FS_*_FL flags */
168static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000169{
170 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
171 FS_NOATIME_FL | FS_NODUMP_FL | \
172 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000173 FS_NOCOMP_FL | FS_COMPR_FL |
174 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 return -EOPNOTSUPP;
176
177 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
178 return -EINVAL;
179
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return 0;
181}
182
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200183static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
184{
Al Viro496ad9a2013-01-23 17:07:38 -0500185 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400186 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200187 struct btrfs_inode *binode = BTRFS_I(inode);
188 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200189 struct btrfs_trans_handle *trans;
David Sterba5c57b8b2018-04-23 15:45:18 +0200190 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191 int ret;
David Sterba5c57b8b2018-04-23 15:45:18 +0200192 u64 old_flags;
193 unsigned int old_i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600194 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200195
David Sterbabd60ea02014-01-16 15:50:22 +0100196 if (!inode_owner_or_capable(inode))
197 return -EPERM;
198
Li Zefanb83cc962010-12-20 16:04:08 +0800199 if (btrfs_root_readonly(root))
200 return -EROFS;
201
David Sterba5c57b8b2018-04-23 15:45:18 +0200202 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203 return -EFAULT;
204
David Sterba5c57b8b2018-04-23 15:45:18 +0200205 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000206 if (ret)
207 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200208
Jan Karae7848682012-06-12 16:20:32 +0200209 ret = mnt_want_write_file(file);
210 if (ret)
211 return ret;
212
Al Viro59551022016-01-22 15:40:57 -0500213 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200214
David Sterba5c57b8b2018-04-23 15:45:18 +0200215 old_flags = binode->flags;
216 old_i_flags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600217 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800218
David Sterba5c57b8b2018-04-23 15:45:18 +0200219 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
220 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
221 if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222 if (!capable(CAP_LINUX_IMMUTABLE)) {
223 ret = -EPERM;
224 goto out_unlock;
225 }
226 }
227
David Sterba5c57b8b2018-04-23 15:45:18 +0200228 if (fsflags & FS_SYNC_FL)
229 binode->flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200230 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200231 binode->flags &= ~BTRFS_INODE_SYNC;
232 if (fsflags & FS_IMMUTABLE_FL)
233 binode->flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200234 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200235 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
236 if (fsflags & FS_APPEND_FL)
237 binode->flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200239 binode->flags &= ~BTRFS_INODE_APPEND;
240 if (fsflags & FS_NODUMP_FL)
241 binode->flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200242 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200243 binode->flags &= ~BTRFS_INODE_NODUMP;
244 if (fsflags & FS_NOATIME_FL)
245 binode->flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200246 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200247 binode->flags &= ~BTRFS_INODE_NOATIME;
248 if (fsflags & FS_DIRSYNC_FL)
249 binode->flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200250 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200251 binode->flags &= ~BTRFS_INODE_DIRSYNC;
252 if (fsflags & FS_NOCOW_FL) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600253 if (S_ISREG(mode)) {
254 /*
255 * It's safe to turn csums off here, no extents exist.
256 * Otherwise we want the flag to reflect the real COW
257 * status of the file and will not set it.
258 */
259 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200260 binode->flags |= BTRFS_INODE_NODATACOW
261 | BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600262 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200263 binode->flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600264 }
265 } else {
266 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400267 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600268 */
269 if (S_ISREG(mode)) {
270 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200271 binode->flags &= ~(BTRFS_INODE_NODATACOW
David Sterba7e97b8d2012-09-07 05:56:55 -0600272 | BTRFS_INODE_NODATASUM);
273 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200274 binode->flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600275 }
276 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200277
Liu Bo75e7cb72011-03-22 10:12:20 +0000278 /*
279 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
280 * flag may be changed automatically if compression code won't make
281 * things smaller.
282 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200283 if (fsflags & FS_NOCOMP_FL) {
284 binode->flags &= ~BTRFS_INODE_COMPRESS;
285 binode->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000286
Anand Jain262c96a2019-03-01 12:34:50 +0800287 ret = btrfs_set_prop_trans(inode, "btrfs.compression", NULL,
288 0, 0);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000289 if (ret && ret != -ENODATA)
290 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200291 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000292 const char *comp;
293
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700294 if (IS_SWAPFILE(inode)) {
295 ret = -ETXTBSY;
296 goto out_unlock;
297 }
298
David Sterba5c57b8b2018-04-23 15:45:18 +0200299 binode->flags |= BTRFS_INODE_COMPRESS;
300 binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000301
David Sterba93370502017-10-31 17:32:41 +0100302 comp = btrfs_compress_type2str(fs_info->compress_type);
303 if (!comp || comp[0] == 0)
304 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
305
Anand Jain262c96a2019-03-01 12:34:50 +0800306 ret = btrfs_set_prop_trans(inode, "btrfs.compression", comp,
307 strlen(comp), 0);
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000308 if (ret)
309 goto out_drop;
310
Li Zefanebcb9042011-04-15 03:03:17 +0000311 } else {
Anand Jain262c96a2019-03-01 12:34:50 +0800312 ret = btrfs_set_prop_trans(inode, "btrfs.compression", NULL,
313 0, 0);
Filipe Manana78a017a2014-09-11 11:44:49 +0100314 if (ret && ret != -ENODATA)
315 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200316 binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000317 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200318
Li Zefan4da6f1a2011-12-29 13:39:50 +0800319 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800320 if (IS_ERR(trans)) {
321 ret = PTR_ERR(trans);
322 goto out_drop;
323 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200324
David Sterba7b6a2212018-03-26 18:40:21 +0200325 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400326 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700327 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200328 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200329
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400330 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800331 out_drop:
332 if (ret) {
David Sterba5c57b8b2018-04-23 15:45:18 +0200333 binode->flags = old_flags;
334 inode->i_flags = old_i_flags;
Li Zefanf062abf02011-12-29 13:36:45 +0800335 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200336
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200337 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500338 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200339 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000340 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200341}
342
David Sterba19f93b32018-03-26 19:42:05 +0200343/*
344 * Translate btrfs internal inode flags to xflags as expected by the
345 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
346 * silently dropped.
347 */
348static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
349{
350 unsigned int xflags = 0;
351
352 if (flags & BTRFS_INODE_APPEND)
353 xflags |= FS_XFLAG_APPEND;
354 if (flags & BTRFS_INODE_IMMUTABLE)
355 xflags |= FS_XFLAG_IMMUTABLE;
356 if (flags & BTRFS_INODE_NOATIME)
357 xflags |= FS_XFLAG_NOATIME;
358 if (flags & BTRFS_INODE_NODUMP)
359 xflags |= FS_XFLAG_NODUMP;
360 if (flags & BTRFS_INODE_SYNC)
361 xflags |= FS_XFLAG_SYNC;
362
363 return xflags;
364}
365
366/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
367static int check_xflags(unsigned int flags)
368{
369 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
370 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
371 return -EOPNOTSUPP;
372 return 0;
373}
374
David Sterbae4202ac2018-03-26 19:51:16 +0200375/*
376 * Set the xflags from the internal inode flags. The remaining items of fsxattr
377 * are zeroed.
378 */
379static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
380{
381 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
382 struct fsxattr fa;
383
384 memset(&fa, 0, sizeof(fa));
385 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
386
387 if (copy_to_user(arg, &fa, sizeof(fa)))
388 return -EFAULT;
389
390 return 0;
391}
392
David Sterba025f2122018-03-26 19:51:16 +0200393static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
394{
395 struct inode *inode = file_inode(file);
396 struct btrfs_inode *binode = BTRFS_I(inode);
397 struct btrfs_root *root = binode->root;
398 struct btrfs_trans_handle *trans;
399 struct fsxattr fa;
400 unsigned old_flags;
401 unsigned old_i_flags;
402 int ret = 0;
403
404 if (!inode_owner_or_capable(inode))
405 return -EPERM;
406
407 if (btrfs_root_readonly(root))
408 return -EROFS;
409
410 memset(&fa, 0, sizeof(fa));
411 if (copy_from_user(&fa, arg, sizeof(fa)))
412 return -EFAULT;
413
414 ret = check_xflags(fa.fsx_xflags);
415 if (ret)
416 return ret;
417
418 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
419 return -EOPNOTSUPP;
420
421 ret = mnt_want_write_file(file);
422 if (ret)
423 return ret;
424
425 inode_lock(inode);
426
427 old_flags = binode->flags;
428 old_i_flags = inode->i_flags;
429
430 /* We need the capabilities to change append-only or immutable inode */
431 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
432 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
433 !capable(CAP_LINUX_IMMUTABLE)) {
434 ret = -EPERM;
435 goto out_unlock;
436 }
437
438 if (fa.fsx_xflags & FS_XFLAG_SYNC)
439 binode->flags |= BTRFS_INODE_SYNC;
440 else
441 binode->flags &= ~BTRFS_INODE_SYNC;
442 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
443 binode->flags |= BTRFS_INODE_IMMUTABLE;
444 else
445 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
446 if (fa.fsx_xflags & FS_XFLAG_APPEND)
447 binode->flags |= BTRFS_INODE_APPEND;
448 else
449 binode->flags &= ~BTRFS_INODE_APPEND;
450 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
451 binode->flags |= BTRFS_INODE_NODUMP;
452 else
453 binode->flags &= ~BTRFS_INODE_NODUMP;
454 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
455 binode->flags |= BTRFS_INODE_NOATIME;
456 else
457 binode->flags &= ~BTRFS_INODE_NOATIME;
458
459 /* 1 item for the inode */
460 trans = btrfs_start_transaction(root, 1);
461 if (IS_ERR(trans)) {
462 ret = PTR_ERR(trans);
463 goto out_unlock;
464 }
465
466 btrfs_sync_inode_flags_to_i_flags(inode);
467 inode_inc_iversion(inode);
468 inode->i_ctime = current_time(inode);
469 ret = btrfs_update_inode(trans, root, inode);
470
471 btrfs_end_transaction(trans);
472
473out_unlock:
474 if (ret) {
475 binode->flags = old_flags;
476 inode->i_flags = old_i_flags;
477 }
478
479 inode_unlock(inode);
480 mnt_drop_write_file(file);
481
482 return ret;
483}
484
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200485static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
486{
Al Viro496ad9a2013-01-23 17:07:38 -0500487 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200488
489 return put_user(inode->i_generation, arg);
490}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491
Li Dongyangf7039b12011-03-24 10:24:28 +0000492static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
493{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400494 struct inode *inode = file_inode(file);
495 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000496 struct btrfs_device *device;
497 struct request_queue *q;
498 struct fstrim_range range;
499 u64 minlen = ULLONG_MAX;
500 u64 num_devices = 0;
501 int ret;
502
503 if (!capable(CAP_SYS_ADMIN))
504 return -EPERM;
505
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000506 /*
507 * If the fs is mounted with nologreplay, which requires it to be
508 * mounted in RO mode as well, we can not allow discard on free space
509 * inside block groups, because log trees refer to extents that are not
510 * pinned in a block group's free space cache (pinning the extents is
511 * precisely the first phase of replaying a log tree).
512 */
513 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
514 return -EROFS;
515
Xiao Guangrong1f781602011-04-20 10:09:16 +0000516 rcu_read_lock();
517 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
518 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000519 if (!device->bdev)
520 continue;
521 q = bdev_get_queue(device->bdev);
522 if (blk_queue_discard(q)) {
523 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100524 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000525 minlen);
526 }
527 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000528 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200529
Li Dongyangf7039b12011-03-24 10:24:28 +0000530 if (!num_devices)
531 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000532 if (copy_from_user(&range, arg, sizeof(range)))
533 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800534
535 /*
536 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
537 * block group is in the logical address space, which can be any
538 * sectorsize aligned bytenr in the range [0, U64_MAX].
539 */
540 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200541 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000542
543 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400544 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000545 if (ret < 0)
546 return ret;
547
548 if (copy_to_user(arg, &range, sizeof(range)))
549 return -EFAULT;
550
551 return 0;
552}
553
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200554int btrfs_is_empty_uuid(u8 *uuid)
555{
Chris Mason46e0f662013-11-15 12:14:55 +0100556 int i;
557
558 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
559 if (uuid[i])
560 return 0;
561 }
562 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200563}
564
Miao Xied5c12072013-02-28 10:04:33 +0000565static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400566 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100567 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200568 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000569 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400571 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400572 struct btrfs_trans_handle *trans;
573 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100574 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400575 struct btrfs_inode_item *inode_item;
576 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000577 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400578 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000579 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700580 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900581 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400582 int ret;
583 int err;
584 u64 objectid;
585 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500586 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200587 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400588
David Sterba49a3c4d2016-03-24 17:49:22 +0100589 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
590 if (!root_item)
591 return -ENOMEM;
592
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400593 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400594 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100595 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000596
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800597 /*
598 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400599 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800600 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100601 if (btrfs_qgroup_level(objectid)) {
602 ret = -ENOSPC;
603 goto fail_free;
604 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800605
Miao Xied5c12072013-02-28 10:04:33 +0000606 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400607 /*
Miao Xied5c12072013-02-28 10:04:33 +0000608 * The same as the snapshot creation, please see the comment
609 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400610 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800611 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000612 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100613 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400614
Miao Xied5c12072013-02-28 10:04:33 +0000615 trans = btrfs_start_transaction(root, 0);
616 if (IS_ERR(trans)) {
617 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100618 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100619 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000620 }
621 trans->block_rsv = &block_rsv;
622 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623
Lu Fengqia93774222018-07-18 14:45:41 +0800624 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200625 if (ret)
626 goto fail;
627
David Sterba4d75f8a2014-06-15 01:54:12 +0200628 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400629 if (IS_ERR(leaf)) {
630 ret = PTR_ERR(leaf);
631 goto fail;
632 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400633
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634 btrfs_mark_buffer_dirty(leaf);
635
David Sterba49a3c4d2016-03-24 17:49:22 +0100636 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800637 btrfs_set_stack_inode_generation(inode_item, 1);
638 btrfs_set_stack_inode_size(inode_item, 3);
639 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400640 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400641 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800642 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400643
David Sterba49a3c4d2016-03-24 17:49:22 +0100644 btrfs_set_root_flags(root_item, 0);
645 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800646 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000647
David Sterba49a3c4d2016-03-24 17:49:22 +0100648 btrfs_set_root_bytenr(root_item, leaf->start);
649 btrfs_set_root_generation(root_item, trans->transid);
650 btrfs_set_root_level(root_item, 0);
651 btrfs_set_root_refs(root_item, 1);
652 btrfs_set_root_used(root_item, leaf->len);
653 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654
David Sterba49a3c4d2016-03-24 17:49:22 +0100655 btrfs_set_root_generation_v2(root_item,
656 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200657 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100658 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
659 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
660 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
661 root_item->ctime = root_item->otime;
662 btrfs_set_root_ctransid(root_item, trans->transid);
663 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664
Chris Mason925baed2008-06-25 16:01:30 -0400665 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666 free_extent_buffer(leaf);
667 leaf = NULL;
668
David Sterba49a3c4d2016-03-24 17:49:22 +0100669 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400670
671 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200673 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400674 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100675 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400676 if (ret)
677 goto fail;
678
Yan, Zheng76dda932009-09-21 16:00:26 -0400679 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400680 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100681 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400683 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100684 goto fail;
685 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400686
687 btrfs_record_root_in_trans(trans, new_root);
688
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000689 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700690 if (ret) {
691 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400692 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700693 goto fail;
694 }
695
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530696 mutex_lock(&new_root->objectid_mutex);
697 new_root->highest_objectid = new_dirid;
698 mutex_unlock(&new_root->objectid_mutex);
699
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400700 /*
701 * insert the directory item
702 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200703 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100704 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400705 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100706 goto fail;
707 }
Chris Mason3de45862008-11-17 21:02:50 -0500708
Lu Fengqi684572d2018-08-04 21:10:57 +0800709 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500710 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100711 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400712 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400713 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100714 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500715
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200716 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500717 ret = btrfs_update_inode(trans, root, dir);
718 BUG_ON(ret);
719
Lu Fengqi6025c192018-08-01 11:32:29 +0800720 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200721 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500722 BUG_ON(ret);
723
Lu Fengqicdb345a2018-05-29 15:01:53 +0800724 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400725 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200726 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400727 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200728
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400729fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100730 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000731 trans->block_rsv = NULL;
732 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100733 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800734
Sage Weil72fd0322010-10-29 15:41:32 -0400735 if (async_transid) {
736 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400737 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000738 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400739 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400740 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400741 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400742 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400743 if (err && !ret)
744 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500745
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900746 if (!ret) {
747 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800748 if (IS_ERR(inode))
749 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900750 d_instantiate(dentry, inode);
751 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100753
754fail_free:
755 kfree(root_item);
756 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400757}
758
Miao Xiee9662f72013-02-28 10:01:15 +0000759static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100760 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000761 u64 *async_transid, bool readonly,
762 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400764 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000765 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400766 struct btrfs_pending_snapshot *pending_snapshot;
767 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000768 int ret;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800769 bool snapshot_force_cow = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400770
Miao Xie27cdeb72014-04-02 19:51:05 +0800771 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400772 return -EINVAL;
773
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700774 if (atomic_read(&root->nr_swapfiles)) {
775 btrfs_warn(fs_info,
776 "cannot snapshot subvolume with active swapfile");
777 return -ETXTBSY;
778 }
779
David Sterba23269bf2017-02-13 11:03:44 +0100780 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100781 if (!pending_snapshot)
782 return -ENOMEM;
783
David Sterbab0c0ea62015-11-10 18:54:00 +0100784 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100785 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100786 pending_snapshot->path = btrfs_alloc_path();
787 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100788 ret = -ENOMEM;
789 goto free_pending;
790 }
791
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800792 /*
793 * Force new buffered writes to reserve space even when NOCOW is
794 * possible. This is to avoid later writeback (running dealloc) to
795 * fallback to COW mode and unexpectedly fail with ENOSPC.
796 */
David Sterbaea14b57f2017-06-22 02:19:11 +0200797 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100798 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600799 /* wait for no snapshot writes */
800 wait_event(root->subv_writers->wait,
801 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800802
Ethan Lien3cd24c62018-11-01 14:49:03 +0800803 ret = btrfs_start_delalloc_snapshot(root);
Miao Xie6a038432013-05-15 07:48:24 +0000804 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100805 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000806
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800807 /*
808 * All previous writes have started writeback in NOCOW mode, so now
809 * we force future writes to fallback to COW mode during snapshot
810 * creation.
811 */
812 atomic_inc(&root->snapshot_force_cow);
813 snapshot_force_cow = true;
814
Chris Mason6374e57a2017-06-23 09:48:21 -0700815 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000816
Miao Xie66d8f3d2012-09-06 04:02:28 -0600817 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
818 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000819 /*
820 * 1 - parent dir inode
821 * 2 - dir entries
822 * 1 - root item
823 * 2 - root ref/backref
824 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200825 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000826 */
827 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200828 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400829 false);
Miao Xied5c12072013-02-28 10:04:33 +0000830 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100831 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000832
Yan, Zhenga22285a2010-05-16 10:48:46 -0400833 pending_snapshot->dentry = dentry;
834 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800835 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000836 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000837 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400838
Miao Xied5c12072013-02-28 10:04:33 +0000839 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400840 if (IS_ERR(trans)) {
841 ret = PTR_ERR(trans);
842 goto fail;
843 }
844
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400845 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400846 list_add(&pending_snapshot->list,
847 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400848 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400849 if (async_transid) {
850 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400851 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000852 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400853 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400854 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400855 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400856 }
Miao Xieaec80302013-03-04 09:44:29 +0000857 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400858 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400859
860 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400861 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000862 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400863
Chris Masond3797302014-10-15 13:50:56 -0700864 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
865 if (ret)
866 goto fail;
867
David Howells2b0143b2015-03-17 22:25:59 +0000868 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000869 if (IS_ERR(inode)) {
870 ret = PTR_ERR(inode);
871 goto fail;
872 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900873
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000874 d_instantiate(dentry, inode);
875 ret = 0;
876fail:
David Sterba7775c812017-02-10 19:18:18 +0100877 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100878dec_and_free:
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800879 if (snapshot_force_cow)
880 atomic_dec(&root->snapshot_force_cow);
David Sterbaea14b57f2017-06-22 02:19:11 +0200881 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100882 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100883free_pending:
884 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100885 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100886 kfree(pending_snapshot);
887
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400888 return ret;
889}
890
Sage Weil4260f7c2010-10-29 15:46:43 -0400891/* copy of may_delete in fs/namei.c()
892 * Check whether we can remove a link victim from directory dir, check
893 * whether the type of victim is right.
894 * 1. We can't do it if dir is read-only (done in permission())
895 * 2. We should have write and exec permissions on dir
896 * 3. We can't remove anything from append-only dir
897 * 4. We can't do anything with immutable dir (done in permission())
898 * 5. If the sticky bit on dir is set we should either
899 * a. be owner of dir, or
900 * b. be owner of victim, or
901 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400902 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400903 * links pointing to it.
904 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
905 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
906 * 9. We can't remove a root or mountpoint.
907 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
908 * nfs_async_unlink().
909 */
910
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530911static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400912{
913 int error;
914
David Howells2b0143b2015-03-17 22:25:59 +0000915 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400916 return -ENOENT;
917
David Howells2b0143b2015-03-17 22:25:59 +0000918 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400919 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400920
921 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
922 if (error)
923 return error;
924 if (IS_APPEND(dir))
925 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000926 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
927 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400928 return -EPERM;
929 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000930 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400931 return -ENOTDIR;
932 if (IS_ROOT(victim))
933 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000934 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400935 return -EISDIR;
936 if (IS_DEADDIR(dir))
937 return -ENOENT;
938 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
939 return -EBUSY;
940 return 0;
941}
942
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400943/* copy of may_create in fs/namei.c() */
944static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
945{
David Howells2b0143b2015-03-17 22:25:59 +0000946 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400947 return -EEXIST;
948 if (IS_DEADDIR(dir))
949 return -ENOENT;
950 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
951}
952
953/*
954 * Create a new subvolume below @parent. This is largely modeled after
955 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
956 * inside this filesystem so it's quite a bit simpler.
957 */
Al Viro92872092016-11-20 19:34:31 -0500958static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100959 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400960 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200961 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000962 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400963{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400964 struct inode *dir = d_inode(parent->dentry);
965 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400966 struct dentry *dentry;
967 int error;
968
Al Viro00235412016-05-26 00:05:12 -0400969 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
970 if (error == -EINTR)
971 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400972
973 dentry = lookup_one_len(name, parent->dentry, namelen);
974 error = PTR_ERR(dentry);
975 if (IS_ERR(dentry))
976 goto out_unlock;
977
Yan, Zheng76dda932009-09-21 16:00:26 -0400978 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400979 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600980 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400981
Chris Mason9c520572012-12-17 14:26:57 -0500982 /*
983 * even if this name doesn't exist, we may get hash collisions.
984 * check for them now when we can safely fail
985 */
986 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
987 dir->i_ino, name,
988 namelen);
989 if (error)
990 goto out_dput;
991
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400992 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400993
994 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
995 goto out_up_read;
996
Chris Mason3de45862008-11-17 21:02:50 -0500997 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100998 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200999 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001000 } else {
Miao Xied5c12072013-02-28 10:04:33 +00001001 error = create_subvol(dir, dentry, name, namelen,
1002 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001003 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001004 if (!error)
1005 fsnotify_mkdir(dir, dentry);
1006out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001007 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001008out_dput:
1009 dput(dentry);
1010out_unlock:
Al Viro59551022016-01-22 15:40:57 -05001011 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001012 return error;
1013}
1014
Chris Mason4cb53002011-05-24 15:35:30 -04001015/*
1016 * When we're defragging a range, we don't want to kick it off again
1017 * if it is really just waiting for delalloc to send it down.
1018 * If we find a nice big extent or delalloc range for the bytes in the
1019 * file you want to defrag, we return 0 to let you know to skip this
1020 * part of the file
1021 */
David Sterbaaab110a2014-07-29 17:32:10 +02001022static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001023{
1024 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1025 struct extent_map *em = NULL;
1026 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1027 u64 end;
1028
1029 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001030 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001031 read_unlock(&em_tree->lock);
1032
1033 if (em) {
1034 end = extent_map_end(em);
1035 free_extent_map(em);
1036 if (end - offset > thresh)
1037 return 0;
1038 }
1039 /* if we already have a nice delalloc here, just stop */
1040 thresh /= 2;
1041 end = count_range_bits(io_tree, &offset, offset + thresh,
1042 thresh, EXTENT_DELALLOC, 1);
1043 if (end >= thresh)
1044 return 0;
1045 return 1;
1046}
1047
1048/*
1049 * helper function to walk through a file and find extents
1050 * newer than a specific transid, and smaller than thresh.
1051 *
1052 * This is used by the defragging code to find new and small
1053 * extents
1054 */
1055static int find_new_extents(struct btrfs_root *root,
1056 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001057 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001058{
1059 struct btrfs_path *path;
1060 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001061 struct extent_buffer *leaf;
1062 struct btrfs_file_extent_item *extent;
1063 int type;
1064 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001065 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001066
1067 path = btrfs_alloc_path();
1068 if (!path)
1069 return -ENOMEM;
1070
David Sterbaa4689d22011-05-31 17:08:14 +00001071 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001072 min_key.type = BTRFS_EXTENT_DATA_KEY;
1073 min_key.offset = *off;
1074
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301075 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001076 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001077 if (ret != 0)
1078 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001079process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001080 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001081 goto none;
1082 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1083 goto none;
1084
1085 leaf = path->nodes[0];
1086 extent = btrfs_item_ptr(leaf, path->slots[0],
1087 struct btrfs_file_extent_item);
1088
1089 type = btrfs_file_extent_type(leaf, extent);
1090 if (type == BTRFS_FILE_EXTENT_REG &&
1091 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1092 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1093 *off = min_key.offset;
1094 btrfs_free_path(path);
1095 return 0;
1096 }
1097
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001098 path->slots[0]++;
1099 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1100 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1101 goto process_slot;
1102 }
1103
Chris Mason4cb53002011-05-24 15:35:30 -04001104 if (min_key.offset == (u64)-1)
1105 goto none;
1106
1107 min_key.offset++;
1108 btrfs_release_path(path);
1109 }
1110none:
1111 btrfs_free_path(path);
1112 return -ENOENT;
1113}
1114
Li Zefan6c282eb2012-06-11 16:03:35 +08001115static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001116{
1117 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001118 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001119 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001120 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001121
1122 /*
1123 * hopefully we have this extent in the tree already, try without
1124 * the full extent lock
1125 */
1126 read_lock(&em_tree->lock);
1127 em = lookup_extent_mapping(em_tree, start, len);
1128 read_unlock(&em_tree->lock);
1129
1130 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001131 struct extent_state *cached = NULL;
1132 u64 end = start + len - 1;
1133
Chris Mason940100a2010-03-10 10:52:59 -05001134 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001135 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001136 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001137 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001138
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001139 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001140 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001141 }
1142
Li Zefan6c282eb2012-06-11 16:03:35 +08001143 return em;
1144}
1145
1146static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1147{
1148 struct extent_map *next;
1149 bool ret = true;
1150
1151 /* this is the last extent */
1152 if (em->start + em->len >= i_size_read(inode))
1153 return false;
1154
1155 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001156 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1157 ret = false;
1158 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001159 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001160 ret = false;
1161
1162 free_extent_map(next);
1163 return ret;
1164}
1165
David Sterbaaab110a2014-07-29 17:32:10 +02001166static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001167 u64 *last_len, u64 *skip, u64 *defrag_end,
1168 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001169{
1170 struct extent_map *em;
1171 int ret = 1;
1172 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001173 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001174
1175 /*
1176 * make sure that once we start defragging an extent, we keep on
1177 * defragging it
1178 */
1179 if (start < *defrag_end)
1180 return 1;
1181
1182 *skip = 0;
1183
1184 em = defrag_lookup_extent(inode, start);
1185 if (!em)
1186 return 0;
1187
Chris Mason940100a2010-03-10 10:52:59 -05001188 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001189 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001190 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001191 goto out;
1192 }
1193
Liu Bo4a3560c2015-08-07 16:48:41 +08001194 if (!*defrag_end)
1195 prev_mergeable = false;
1196
Li Zefan6c282eb2012-06-11 16:03:35 +08001197 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001198 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001199 * we hit a real extent, if it is big or the next extent is not a
1200 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001201 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001202 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001203 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001204 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001205out:
Chris Mason940100a2010-03-10 10:52:59 -05001206 /*
1207 * last_len ends up being a counter of how many bytes we've defragged.
1208 * every time we choose not to defrag an extent, we reset *last_len
1209 * so that the next tiny extent will force a defrag.
1210 *
1211 * The end result of this is that tiny extents before a single big
1212 * extent will force at least part of that big extent to be defragged.
1213 */
1214 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001215 *defrag_end = extent_map_end(em);
1216 } else {
1217 *last_len = 0;
1218 *skip = extent_map_end(em);
1219 *defrag_end = 0;
1220 }
1221
1222 free_extent_map(em);
1223 return ret;
1224}
1225
Chris Mason4cb53002011-05-24 15:35:30 -04001226/*
1227 * it doesn't do much good to defrag one or two pages
1228 * at a time. This pulls in a nice chunk of pages
1229 * to COW and defrag.
1230 *
1231 * It also makes sure the delalloc code has enough
1232 * dirty data to avoid making new small extents as part
1233 * of the defrag
1234 *
1235 * It's a good idea to start RA on this range
1236 * before calling this.
1237 */
1238static int cluster_pages_for_defrag(struct inode *inode,
1239 struct page **pages,
1240 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001241 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001242{
Chris Mason4cb53002011-05-24 15:35:30 -04001243 unsigned long file_end;
1244 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001245 u64 page_start;
1246 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001247 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001248 int ret;
1249 int i;
1250 int i_done;
1251 struct btrfs_ordered_extent *ordered;
1252 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001253 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001254 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001255 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001256
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001257 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001258 if (!isize || start_index > file_end)
1259 return 0;
1260
1261 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001262
Qu Wenruo364ecf32017-02-27 15:10:38 +08001263 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001264 start_index << PAGE_SHIFT,
1265 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001266 if (ret)
1267 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001268 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001269 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001270
1271 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001272 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001273 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001274again:
Josef Bacika94733d2011-07-11 10:47:06 -04001275 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001276 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001277 if (!page)
1278 break;
1279
Miao Xie600a45e2012-02-16 15:01:24 +08001280 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001281 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001282 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001283 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001284 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001285 ordered = btrfs_lookup_ordered_extent(inode,
1286 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001287 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001288 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001289 if (!ordered)
1290 break;
1291
1292 unlock_page(page);
1293 btrfs_start_ordered_extent(inode, ordered, 1);
1294 btrfs_put_ordered_extent(ordered);
1295 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001296 /*
1297 * we unlocked the page above, so we need check if
1298 * it was released or not.
1299 */
1300 if (page->mapping != inode->i_mapping) {
1301 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001302 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001303 goto again;
1304 }
Miao Xie600a45e2012-02-16 15:01:24 +08001305 }
1306
Chris Mason4cb53002011-05-24 15:35:30 -04001307 if (!PageUptodate(page)) {
1308 btrfs_readpage(NULL, page);
1309 lock_page(page);
1310 if (!PageUptodate(page)) {
1311 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001312 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001313 ret = -EIO;
1314 break;
1315 }
1316 }
Miao Xie600a45e2012-02-16 15:01:24 +08001317
Miao Xie600a45e2012-02-16 15:01:24 +08001318 if (page->mapping != inode->i_mapping) {
1319 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001320 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001321 goto again;
1322 }
1323
Chris Mason4cb53002011-05-24 15:35:30 -04001324 pages[i] = page;
1325 i_done++;
1326 }
1327 if (!i_done || ret)
1328 goto out;
1329
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001330 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001331 goto out;
1332
1333 /*
1334 * so now we have a nice long stream of locked
1335 * and up to date pages, lets wait on them
1336 */
1337 for (i = 0; i < i_done; i++)
1338 wait_on_page_writeback(pages[i]);
1339
1340 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001341 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001342
1343 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001344 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001345 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1346 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001347 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001348 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001349
Liu Bo1f12bd02012-03-29 09:57:44 -04001350 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001351 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001352 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001353 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001354 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001355 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001356 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001357 }
1358
1359
Liu Bo9e8a4a82012-09-05 19:10:51 -06001360 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001361 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001362
1363 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001364 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001365
1366 for (i = 0; i < i_done; i++) {
1367 clear_page_dirty_for_io(pages[i]);
1368 ClearPageChecked(pages[i]);
1369 set_page_extent_mapped(pages[i]);
1370 set_page_dirty(pages[i]);
1371 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001372 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001373 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001374 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1375 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001376 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001377 return i_done;
1378out:
1379 for (i = 0; i < i_done; i++) {
1380 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001381 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001382 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001383 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001384 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001385 page_cnt << PAGE_SHIFT, true);
1386 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1387 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001388 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001389 return ret;
1390
1391}
1392
1393int btrfs_defrag_file(struct inode *inode, struct file *file,
1394 struct btrfs_ioctl_defrag_range_args *range,
1395 u64 newer_than, unsigned long max_to_defrag)
1396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001397 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001398 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001399 struct file_ra_state *ra = NULL;
1400 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001401 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001402 u64 last_len = 0;
1403 u64 skip = 0;
1404 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001405 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001406 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001407 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001408 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001409 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001410 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001411 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001412 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001413 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001414 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001415 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001416 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001417
Liu Bo0abd5b12013-04-16 09:20:28 +00001418 if (isize == 0)
1419 return 0;
1420
1421 if (range->start >= isize)
1422 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001423
David Sterba1e2ef462017-07-17 20:01:59 +02001424 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001425 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1426 return -EINVAL;
1427 if (range->compress_type)
1428 compress_type = range->compress_type;
1429 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001430
Liu Bo0abd5b12013-04-16 09:20:28 +00001431 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001432 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001433
Chris Mason4cb53002011-05-24 15:35:30 -04001434 /*
David Sterba0a52d102017-06-22 03:22:58 +02001435 * If we were not given a file, allocate a readahead context. As
1436 * readahead is just an optimization, defrag will work without it so
1437 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001438 */
1439 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001440 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001441 if (ra)
1442 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001443 } else {
1444 ra = &file->f_ra;
1445 }
1446
David Sterba63e727e2017-06-22 03:13:02 +02001447 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001448 if (!pages) {
1449 ret = -ENOMEM;
1450 goto out_ra;
1451 }
1452
1453 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001454 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001455 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001456 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001457 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001458 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001459 }
1460
Chris Mason4cb53002011-05-24 15:35:30 -04001461 if (newer_than) {
1462 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001463 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001464 if (!ret) {
1465 range->start = newer_off;
1466 /*
1467 * we always align our defrag to help keep
1468 * the extents in the file evenly spaced
1469 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001470 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001471 } else
1472 goto out_ra;
1473 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001474 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001475 }
1476 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301477 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001478
Li Zefan2a0f7f52011-10-10 15:43:34 -04001479 /*
1480 * make writeback starts from i, so the defrag range can be
1481 * written sequentially.
1482 */
1483 if (i < inode->i_mapping->writeback_index)
1484 inode->i_mapping->writeback_index = i;
1485
Chris Masonf7f43cc2011-10-11 11:41:40 -04001486 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001487 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001488 /*
1489 * make sure we stop running if someone unmounts
1490 * the FS
1491 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001492 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001493 break;
1494
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001495 if (btrfs_defrag_cancelled(fs_info)) {
1496 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001497 ret = -EAGAIN;
1498 break;
1499 }
1500
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001501 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001502 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001503 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001504 unsigned long next;
1505 /*
1506 * the should_defrag function tells us how much to skip
1507 * bump our counter by the suggested amount
1508 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001509 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001510 i = max(i + 1, next);
1511 continue;
1512 }
Li Zefan008873e2011-09-02 15:57:07 +08001513
1514 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001515 cluster = (PAGE_ALIGN(defrag_end) >>
1516 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001517 cluster = min(cluster, max_cluster);
1518 } else {
1519 cluster = max_cluster;
1520 }
1521
Li Zefan008873e2011-09-02 15:57:07 +08001522 if (i + cluster > ra_index) {
1523 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001524 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001525 page_cache_sync_readahead(inode->i_mapping, ra,
1526 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301527 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001528 }
Chris Mason940100a2010-03-10 10:52:59 -05001529
Al Viro59551022016-01-22 15:40:57 -05001530 inode_lock(inode);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001531 if (IS_SWAPFILE(inode)) {
1532 ret = -ETXTBSY;
1533 } else {
1534 if (do_compress)
1535 BTRFS_I(inode)->defrag_compress = compress_type;
1536 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1537 }
Liu Boecb8bea2012-03-29 09:57:44 -04001538 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001539 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001540 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001541 }
Chris Mason940100a2010-03-10 10:52:59 -05001542
Chris Mason4cb53002011-05-24 15:35:30 -04001543 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001544 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001545 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001546
1547 if (newer_than) {
1548 if (newer_off == (u64)-1)
1549 break;
1550
Liu Boe1f041e2012-03-29 09:57:45 -04001551 if (ret > 0)
1552 i += ret;
1553
Chris Mason4cb53002011-05-24 15:35:30 -04001554 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001555 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001556
Byongho Leeee221842015-12-15 01:42:10 +09001557 ret = find_new_extents(root, inode, newer_than,
1558 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001559 if (!ret) {
1560 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001561 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001562 } else {
1563 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001564 }
Chris Mason4cb53002011-05-24 15:35:30 -04001565 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001566 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001567 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001568 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001569 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001570 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001571 last_len = 0;
1572 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001574 }
1575
Filipe Mananadec8ef92014-03-01 10:55:54 +00001576 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001577 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001578 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1579 &BTRFS_I(inode)->runtime_flags))
1580 filemap_flush(inode->i_mapping);
1581 }
Chris Mason1e701a32010-03-11 09:42:04 -05001582
Li Zefan1a419d82010-10-25 15:12:50 +08001583 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001584 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001585 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1586 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001587 }
1588
Diego Calleja60ccf822011-09-01 16:33:57 +02001589 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001590
Chris Mason4cb53002011-05-24 15:35:30 -04001591out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001592 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001593 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001594 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001595 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001596 }
Chris Mason4cb53002011-05-24 15:35:30 -04001597 if (!file)
1598 kfree(ra);
1599 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001600 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001601}
1602
Miao Xie198605a2012-11-26 08:43:45 +00001603static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001604 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001605{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001606 struct inode *inode = file_inode(file);
1607 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001608 u64 new_size;
1609 u64 old_size;
1610 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001611 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001612 struct btrfs_ioctl_vol_args *vol_args;
1613 struct btrfs_trans_handle *trans;
1614 struct btrfs_device *device = NULL;
1615 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001616 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001617 char *devstr = NULL;
1618 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001619 int mod = 0;
1620
Chris Masone441d542009-01-05 16:57:23 -05001621 if (!capable(CAP_SYS_ADMIN))
1622 return -EPERM;
1623
Miao Xie198605a2012-11-26 08:43:45 +00001624 ret = mnt_want_write_file(file);
1625 if (ret)
1626 return ret;
1627
David Sterba171938e2017-03-28 14:44:21 +02001628 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001629 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001630 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001631 }
1632
Li Zefandae7b662009-04-08 15:06:54 +08001633 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001634 if (IS_ERR(vol_args)) {
1635 ret = PTR_ERR(vol_args);
1636 goto out;
1637 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001638
1639 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001640
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001641 sizestr = vol_args->name;
1642 devstr = strchr(sizestr, ':');
1643 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001644 sizestr = devstr + 1;
1645 *devstr = '\0';
1646 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001647 ret = kstrtoull(devstr, 10, &devid);
1648 if (ret)
1649 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001650 if (!devid) {
1651 ret = -EINVAL;
1652 goto out_free;
1653 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001654 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001655 }
Miao Xiedba60f32012-12-21 09:19:51 +00001656
Anand Jain09ba3bc2019-01-19 14:48:55 +08001657 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001658 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001659 btrfs_info(fs_info, "resizer unable to find device %llu",
1660 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001661 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001662 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001663 }
Miao Xiedba60f32012-12-21 09:19:51 +00001664
Anand Jainebbede42017-12-04 12:54:52 +08001665 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001666 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001667 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001668 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001669 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001670 goto out_free;
1671 }
1672
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001673 if (!strcmp(sizestr, "max"))
1674 new_size = device->bdev->bd_inode->i_size;
1675 else {
1676 if (sizestr[0] == '-') {
1677 mod = -1;
1678 sizestr++;
1679 } else if (sizestr[0] == '+') {
1680 mod = 1;
1681 sizestr++;
1682 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001683 new_size = memparse(sizestr, &retptr);
1684 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001685 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001686 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001687 }
1688 }
1689
Anand Jain401e29c2017-12-04 12:54:55 +08001690 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001691 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001692 goto out_free;
1693 }
1694
Miao Xie7cc8e582014-09-03 21:35:38 +08001695 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001696
1697 if (mod < 0) {
1698 if (new_size > old_size) {
1699 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001700 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001701 }
1702 new_size = old_size - new_size;
1703 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001704 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001705 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001706 goto out_free;
1707 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001708 new_size = old_size + new_size;
1709 }
1710
Byongho Leeee221842015-12-15 01:42:10 +09001711 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001712 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001713 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001714 }
1715 if (new_size > device->bdev->bd_inode->i_size) {
1716 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001717 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001718 }
1719
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001720 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001721
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1723 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001724
1725 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001726 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001727 if (IS_ERR(trans)) {
1728 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001729 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001730 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001731 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001732 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001733 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001734 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001735 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001736
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001737out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001738 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001739out:
David Sterba171938e2017-03-28 14:44:21 +02001740 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001741 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001742 return ret;
1743}
1744
Sage Weil72fd0322010-10-29 15:41:32 -04001745static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001746 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001747 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001748 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001749{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001751 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001752
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001753 if (!S_ISDIR(file_inode(file)->i_mode))
1754 return -ENOTDIR;
1755
Liu Boa874a632012-06-29 03:58:46 -06001756 ret = mnt_want_write_file(file);
1757 if (ret)
1758 goto out;
1759
Sage Weil72fd0322010-10-29 15:41:32 -04001760 namelen = strlen(name);
1761 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001762 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001763 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001764 }
1765
Chris Mason16780ca2012-02-20 22:14:55 -05001766 if (name[0] == '.' &&
1767 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1768 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001769 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001770 }
1771
Chris Mason3de45862008-11-17 21:02:50 -05001772 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001773 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001774 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001775 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001776 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001777 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001778 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001779 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001780 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001781 }
1782
Al Viro496ad9a2013-01-23 17:07:38 -05001783 src_inode = file_inode(src.file);
1784 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001785 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001786 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001787 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001788 } else if (!inode_owner_or_capable(src_inode)) {
1789 /*
1790 * Subvolume creation is not restricted, but snapshots
1791 * are limited to own subvolumes only
1792 */
1793 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001794 } else {
1795 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1796 BTRFS_I(src_inode)->root,
1797 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001798 }
Al Viro2903ff02012-08-28 12:52:22 -04001799 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001800 }
Liu Boa874a632012-06-29 03:58:46 -06001801out_drop_write:
1802 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001803out:
Sage Weil72fd0322010-10-29 15:41:32 -04001804 return ret;
1805}
1806
1807static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001808 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001809{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001810 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001811 int ret;
1812
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001813 if (!S_ISDIR(file_inode(file)->i_mode))
1814 return -ENOTDIR;
1815
Li Zefanfa0d2b92010-12-20 15:53:28 +08001816 vol_args = memdup_user(arg, sizeof(*vol_args));
1817 if (IS_ERR(vol_args))
1818 return PTR_ERR(vol_args);
1819 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001820
Li Zefanfa0d2b92010-12-20 15:53:28 +08001821 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001822 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001823 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001824
Li Zefanfa0d2b92010-12-20 15:53:28 +08001825 kfree(vol_args);
1826 return ret;
1827}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001828
Li Zefanfa0d2b92010-12-20 15:53:28 +08001829static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1830 void __user *arg, int subvol)
1831{
1832 struct btrfs_ioctl_vol_args_v2 *vol_args;
1833 int ret;
1834 u64 transid = 0;
1835 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001836 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001837 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001838
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001839 if (!S_ISDIR(file_inode(file)->i_mode))
1840 return -ENOTDIR;
1841
Li Zefanfa0d2b92010-12-20 15:53:28 +08001842 vol_args = memdup_user(arg, sizeof(*vol_args));
1843 if (IS_ERR(vol_args))
1844 return PTR_ERR(vol_args);
1845 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001846
Li Zefanb83cc962010-12-20 16:04:08 +08001847 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001848 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1849 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001850 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001851 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001852 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001853
1854 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1855 ptr = &transid;
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 */
2178 root = BTRFS_I(inode)->root;
2179 } else {
2180 key.objectid = sk->tree_id;
2181 key.type = BTRFS_ROOT_ITEM_KEY;
2182 key.offset = (u64)-1;
2183 root = btrfs_read_fs_root_no_name(info, &key);
2184 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;
2212 btrfs_free_path(path);
2213 return ret;
2214}
2215
2216static noinline int btrfs_ioctl_tree_search(struct file *file,
2217 void __user *argp)
2218{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002219 struct btrfs_ioctl_search_args __user *uargs;
2220 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002221 struct inode *inode;
2222 int ret;
2223 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002224
2225 if (!capable(CAP_SYS_ADMIN))
2226 return -EPERM;
2227
Gerhard Heiftba346b32014-01-30 16:24:02 +01002228 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002229
Gerhard Heiftba346b32014-01-30 16:24:02 +01002230 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2231 return -EFAULT;
2232
2233 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002234
Al Viro496ad9a2013-01-23 17:07:38 -05002235 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002236 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002237
2238 /*
2239 * In the origin implementation an overflow is handled by returning a
2240 * search header with a len of zero, so reset ret.
2241 */
2242 if (ret == -EOVERFLOW)
2243 ret = 0;
2244
Gerhard Heiftba346b32014-01-30 16:24:02 +01002245 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002246 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002247 return ret;
2248}
2249
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002250static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2251 void __user *argp)
2252{
2253 struct btrfs_ioctl_search_args_v2 __user *uarg;
2254 struct btrfs_ioctl_search_args_v2 args;
2255 struct inode *inode;
2256 int ret;
2257 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002258 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002259
2260 if (!capable(CAP_SYS_ADMIN))
2261 return -EPERM;
2262
2263 /* copy search header and buffer size */
2264 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2265 if (copy_from_user(&args, uarg, sizeof(args)))
2266 return -EFAULT;
2267
2268 buf_size = args.buf_size;
2269
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002270 /* limit result size to 16MB */
2271 if (buf_size > buf_limit)
2272 buf_size = buf_limit;
2273
2274 inode = file_inode(file);
2275 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002276 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002277 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2278 ret = -EFAULT;
2279 else if (ret == -EOVERFLOW &&
2280 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2281 ret = -EFAULT;
2282
Yan, Zheng76dda932009-09-21 16:00:26 -04002283 return ret;
2284}
2285
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002286/*
Chris Masonac8e9812010-02-28 15:39:26 -05002287 * Search INODE_REFs to identify path name of 'dirid' directory
2288 * in a 'tree_id' tree. and sets path name to 'name'.
2289 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002290static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2291 u64 tree_id, u64 dirid, char *name)
2292{
2293 struct btrfs_root *root;
2294 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002295 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002296 int ret = -1;
2297 int slot;
2298 int len;
2299 int total_len = 0;
2300 struct btrfs_inode_ref *iref;
2301 struct extent_buffer *l;
2302 struct btrfs_path *path;
2303
2304 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2305 name[0]='\0';
2306 return 0;
2307 }
2308
2309 path = btrfs_alloc_path();
2310 if (!path)
2311 return -ENOMEM;
2312
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002313 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002314
2315 key.objectid = tree_id;
2316 key.type = BTRFS_ROOT_ITEM_KEY;
2317 key.offset = (u64)-1;
2318 root = btrfs_read_fs_root_no_name(info, &key);
2319 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002320 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002321 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002322 }
2323
2324 key.objectid = dirid;
2325 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002326 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002327
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302328 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002329 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2330 if (ret < 0)
2331 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002332 else if (ret > 0) {
2333 ret = btrfs_previous_item(root, path, dirid,
2334 BTRFS_INODE_REF_KEY);
2335 if (ret < 0)
2336 goto out;
2337 else if (ret > 0) {
2338 ret = -ENOENT;
2339 goto out;
2340 }
2341 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002342
2343 l = path->nodes[0];
2344 slot = path->slots[0];
2345 btrfs_item_key_to_cpu(l, &key, slot);
2346
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002347 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2348 len = btrfs_inode_ref_name_len(l, iref);
2349 ptr -= len + 1;
2350 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002351 if (ptr < name) {
2352 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002353 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002354 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002355
2356 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302357 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002358
2359 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2360 break;
2361
David Sterbab3b4aa72011-04-21 01:20:15 +02002362 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002363 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002364 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002365 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002366 }
Li Zefan77906a502011-07-14 03:16:00 +00002367 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302368 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002369 ret = 0;
2370out:
2371 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002372 return ret;
2373}
2374
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002375static int btrfs_search_path_in_tree_user(struct inode *inode,
2376 struct btrfs_ioctl_ino_lookup_user_args *args)
2377{
2378 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2379 struct super_block *sb = inode->i_sb;
2380 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2381 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2382 u64 dirid = args->dirid;
2383 unsigned long item_off;
2384 unsigned long item_len;
2385 struct btrfs_inode_ref *iref;
2386 struct btrfs_root_ref *rref;
2387 struct btrfs_root *root;
2388 struct btrfs_path *path;
2389 struct btrfs_key key, key2;
2390 struct extent_buffer *leaf;
2391 struct inode *temp_inode;
2392 char *ptr;
2393 int slot;
2394 int len;
2395 int total_len = 0;
2396 int ret;
2397
2398 path = btrfs_alloc_path();
2399 if (!path)
2400 return -ENOMEM;
2401
2402 /*
2403 * If the bottom subvolume does not exist directly under upper_limit,
2404 * construct the path in from the bottom up.
2405 */
2406 if (dirid != upper_limit.objectid) {
2407 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2408
2409 key.objectid = treeid;
2410 key.type = BTRFS_ROOT_ITEM_KEY;
2411 key.offset = (u64)-1;
2412 root = btrfs_read_fs_root_no_name(fs_info, &key);
2413 if (IS_ERR(root)) {
2414 ret = PTR_ERR(root);
2415 goto out;
2416 }
2417
2418 key.objectid = dirid;
2419 key.type = BTRFS_INODE_REF_KEY;
2420 key.offset = (u64)-1;
2421 while (1) {
2422 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2423 if (ret < 0) {
2424 goto out;
2425 } else if (ret > 0) {
2426 ret = btrfs_previous_item(root, path, dirid,
2427 BTRFS_INODE_REF_KEY);
2428 if (ret < 0) {
2429 goto out;
2430 } else if (ret > 0) {
2431 ret = -ENOENT;
2432 goto out;
2433 }
2434 }
2435
2436 leaf = path->nodes[0];
2437 slot = path->slots[0];
2438 btrfs_item_key_to_cpu(leaf, &key, slot);
2439
2440 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2441 len = btrfs_inode_ref_name_len(leaf, iref);
2442 ptr -= len + 1;
2443 total_len += len + 1;
2444 if (ptr < args->path) {
2445 ret = -ENAMETOOLONG;
2446 goto out;
2447 }
2448
2449 *(ptr + len) = '/';
2450 read_extent_buffer(leaf, ptr,
2451 (unsigned long)(iref + 1), len);
2452
2453 /* Check the read+exec permission of this directory */
2454 ret = btrfs_previous_item(root, path, dirid,
2455 BTRFS_INODE_ITEM_KEY);
2456 if (ret < 0) {
2457 goto out;
2458 } else if (ret > 0) {
2459 ret = -ENOENT;
2460 goto out;
2461 }
2462
2463 leaf = path->nodes[0];
2464 slot = path->slots[0];
2465 btrfs_item_key_to_cpu(leaf, &key2, slot);
2466 if (key2.objectid != dirid) {
2467 ret = -ENOENT;
2468 goto out;
2469 }
2470
2471 temp_inode = btrfs_iget(sb, &key2, root, NULL);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002472 if (IS_ERR(temp_inode)) {
2473 ret = PTR_ERR(temp_inode);
2474 goto out;
2475 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002476 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2477 iput(temp_inode);
2478 if (ret) {
2479 ret = -EACCES;
2480 goto out;
2481 }
2482
2483 if (key.offset == upper_limit.objectid)
2484 break;
2485 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2486 ret = -EACCES;
2487 goto out;
2488 }
2489
2490 btrfs_release_path(path);
2491 key.objectid = key.offset;
2492 key.offset = (u64)-1;
2493 dirid = key.objectid;
2494 }
2495
2496 memmove(args->path, ptr, total_len);
2497 args->path[total_len] = '\0';
2498 btrfs_release_path(path);
2499 }
2500
2501 /* Get the bottom subvolume's name from ROOT_REF */
2502 root = fs_info->tree_root;
2503 key.objectid = treeid;
2504 key.type = BTRFS_ROOT_REF_KEY;
2505 key.offset = args->treeid;
2506 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2507 if (ret < 0) {
2508 goto out;
2509 } else if (ret > 0) {
2510 ret = -ENOENT;
2511 goto out;
2512 }
2513
2514 leaf = path->nodes[0];
2515 slot = path->slots[0];
2516 btrfs_item_key_to_cpu(leaf, &key, slot);
2517
2518 item_off = btrfs_item_ptr_offset(leaf, slot);
2519 item_len = btrfs_item_size_nr(leaf, slot);
2520 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2521 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2522 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2523 ret = -EINVAL;
2524 goto out;
2525 }
2526
2527 /* Copy subvolume's name */
2528 item_off += sizeof(struct btrfs_root_ref);
2529 item_len -= sizeof(struct btrfs_root_ref);
2530 read_extent_buffer(leaf, args->name, item_off, item_len);
2531 args->name[item_len] = 0;
2532
2533out:
2534 btrfs_free_path(path);
2535 return ret;
2536}
2537
Chris Masonac8e9812010-02-28 15:39:26 -05002538static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2539 void __user *argp)
2540{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002541 struct btrfs_ioctl_ino_lookup_args *args;
2542 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002543 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002544
Julia Lawall2354d08f2010-10-29 15:14:18 -04002545 args = memdup_user(argp, sizeof(*args));
2546 if (IS_ERR(args))
2547 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002548
Al Viro496ad9a2013-01-23 17:07:38 -05002549 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002550
David Sterba01b810b2015-05-12 19:14:49 +02002551 /*
2552 * Unprivileged query to obtain the containing subvolume root id. The
2553 * path is reset so it's consistent with btrfs_search_path_in_tree.
2554 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002555 if (args->treeid == 0)
2556 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2557
David Sterba01b810b2015-05-12 19:14:49 +02002558 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2559 args->name[0] = 0;
2560 goto out;
2561 }
2562
2563 if (!capable(CAP_SYS_ADMIN)) {
2564 ret = -EPERM;
2565 goto out;
2566 }
2567
Chris Masonac8e9812010-02-28 15:39:26 -05002568 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2569 args->treeid, args->objectid,
2570 args->name);
2571
David Sterba01b810b2015-05-12 19:14:49 +02002572out:
Chris Masonac8e9812010-02-28 15:39:26 -05002573 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2574 ret = -EFAULT;
2575
2576 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002577 return ret;
2578}
2579
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002580/*
2581 * Version of ino_lookup ioctl (unprivileged)
2582 *
2583 * The main differences from ino_lookup ioctl are:
2584 *
2585 * 1. Read + Exec permission will be checked using inode_permission() during
2586 * path construction. -EACCES will be returned in case of failure.
2587 * 2. Path construction will be stopped at the inode number which corresponds
2588 * to the fd with which this ioctl is called. If constructed path does not
2589 * exist under fd's inode, -EACCES will be returned.
2590 * 3. The name of bottom subvolume is also searched and filled.
2591 */
2592static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2593{
2594 struct btrfs_ioctl_ino_lookup_user_args *args;
2595 struct inode *inode;
2596 int ret;
2597
2598 args = memdup_user(argp, sizeof(*args));
2599 if (IS_ERR(args))
2600 return PTR_ERR(args);
2601
2602 inode = file_inode(file);
2603
2604 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2605 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2606 /*
2607 * The subvolume does not exist under fd with which this is
2608 * called
2609 */
2610 kfree(args);
2611 return -EACCES;
2612 }
2613
2614 ret = btrfs_search_path_in_tree_user(inode, args);
2615
2616 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2617 ret = -EFAULT;
2618
2619 kfree(args);
2620 return ret;
2621}
2622
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002623/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2624static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2625{
2626 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2627 struct btrfs_fs_info *fs_info;
2628 struct btrfs_root *root;
2629 struct btrfs_path *path;
2630 struct btrfs_key key;
2631 struct btrfs_root_item *root_item;
2632 struct btrfs_root_ref *rref;
2633 struct extent_buffer *leaf;
2634 unsigned long item_off;
2635 unsigned long item_len;
2636 struct inode *inode;
2637 int slot;
2638 int ret = 0;
2639
2640 path = btrfs_alloc_path();
2641 if (!path)
2642 return -ENOMEM;
2643
2644 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2645 if (!subvol_info) {
2646 btrfs_free_path(path);
2647 return -ENOMEM;
2648 }
2649
2650 inode = file_inode(file);
2651 fs_info = BTRFS_I(inode)->root->fs_info;
2652
2653 /* Get root_item of inode's subvolume */
2654 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2655 key.type = BTRFS_ROOT_ITEM_KEY;
2656 key.offset = (u64)-1;
2657 root = btrfs_read_fs_root_no_name(fs_info, &key);
2658 if (IS_ERR(root)) {
2659 ret = PTR_ERR(root);
2660 goto out;
2661 }
2662 root_item = &root->root_item;
2663
2664 subvol_info->treeid = key.objectid;
2665
2666 subvol_info->generation = btrfs_root_generation(root_item);
2667 subvol_info->flags = btrfs_root_flags(root_item);
2668
2669 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2670 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2671 BTRFS_UUID_SIZE);
2672 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2673 BTRFS_UUID_SIZE);
2674
2675 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2676 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2677 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2678
2679 subvol_info->otransid = btrfs_root_otransid(root_item);
2680 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2681 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2682
2683 subvol_info->stransid = btrfs_root_stransid(root_item);
2684 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2685 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2686
2687 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2688 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2689 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2690
2691 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2692 /* Search root tree for ROOT_BACKREF of this subvolume */
2693 root = fs_info->tree_root;
2694
2695 key.type = BTRFS_ROOT_BACKREF_KEY;
2696 key.offset = 0;
2697 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2698 if (ret < 0) {
2699 goto out;
2700 } else if (path->slots[0] >=
2701 btrfs_header_nritems(path->nodes[0])) {
2702 ret = btrfs_next_leaf(root, path);
2703 if (ret < 0) {
2704 goto out;
2705 } else if (ret > 0) {
2706 ret = -EUCLEAN;
2707 goto out;
2708 }
2709 }
2710
2711 leaf = path->nodes[0];
2712 slot = path->slots[0];
2713 btrfs_item_key_to_cpu(leaf, &key, slot);
2714 if (key.objectid == subvol_info->treeid &&
2715 key.type == BTRFS_ROOT_BACKREF_KEY) {
2716 subvol_info->parent_id = key.offset;
2717
2718 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2719 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2720
2721 item_off = btrfs_item_ptr_offset(leaf, slot)
2722 + sizeof(struct btrfs_root_ref);
2723 item_len = btrfs_item_size_nr(leaf, slot)
2724 - sizeof(struct btrfs_root_ref);
2725 read_extent_buffer(leaf, subvol_info->name,
2726 item_off, item_len);
2727 } else {
2728 ret = -ENOENT;
2729 goto out;
2730 }
2731 }
2732
2733 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2734 ret = -EFAULT;
2735
2736out:
2737 btrfs_free_path(path);
2738 kzfree(subvol_info);
2739 return ret;
2740}
2741
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002742/*
2743 * Return ROOT_REF information of the subvolume containing this inode
2744 * except the subvolume name.
2745 */
2746static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2747{
2748 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2749 struct btrfs_root_ref *rref;
2750 struct btrfs_root *root;
2751 struct btrfs_path *path;
2752 struct btrfs_key key;
2753 struct extent_buffer *leaf;
2754 struct inode *inode;
2755 u64 objectid;
2756 int slot;
2757 int ret;
2758 u8 found;
2759
2760 path = btrfs_alloc_path();
2761 if (!path)
2762 return -ENOMEM;
2763
2764 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2765 if (IS_ERR(rootrefs)) {
2766 btrfs_free_path(path);
2767 return PTR_ERR(rootrefs);
2768 }
2769
2770 inode = file_inode(file);
2771 root = BTRFS_I(inode)->root->fs_info->tree_root;
2772 objectid = BTRFS_I(inode)->root->root_key.objectid;
2773
2774 key.objectid = objectid;
2775 key.type = BTRFS_ROOT_REF_KEY;
2776 key.offset = rootrefs->min_treeid;
2777 found = 0;
2778
2779 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2780 if (ret < 0) {
2781 goto out;
2782 } else if (path->slots[0] >=
2783 btrfs_header_nritems(path->nodes[0])) {
2784 ret = btrfs_next_leaf(root, path);
2785 if (ret < 0) {
2786 goto out;
2787 } else if (ret > 0) {
2788 ret = -EUCLEAN;
2789 goto out;
2790 }
2791 }
2792 while (1) {
2793 leaf = path->nodes[0];
2794 slot = path->slots[0];
2795
2796 btrfs_item_key_to_cpu(leaf, &key, slot);
2797 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2798 ret = 0;
2799 goto out;
2800 }
2801
2802 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2803 ret = -EOVERFLOW;
2804 goto out;
2805 }
2806
2807 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2808 rootrefs->rootref[found].treeid = key.offset;
2809 rootrefs->rootref[found].dirid =
2810 btrfs_root_ref_dirid(leaf, rref);
2811 found++;
2812
2813 ret = btrfs_next_item(root, path);
2814 if (ret < 0) {
2815 goto out;
2816 } else if (ret > 0) {
2817 ret = -EUCLEAN;
2818 goto out;
2819 }
2820 }
2821
2822out:
2823 if (!ret || ret == -EOVERFLOW) {
2824 rootrefs->num_items = found;
2825 /* update min_treeid for next search */
2826 if (found)
2827 rootrefs->min_treeid =
2828 rootrefs->rootref[found - 1].treeid + 1;
2829 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2830 ret = -EFAULT;
2831 }
2832
2833 kfree(rootrefs);
2834 btrfs_free_path(path);
2835
2836 return ret;
2837}
2838
Yan, Zheng76dda932009-09-21 16:00:26 -04002839static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2840 void __user *arg)
2841{
Al Viro54563d42013-09-01 15:57:51 -04002842 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002843 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002844 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002845 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002846 struct inode *inode;
2847 struct btrfs_root *root = BTRFS_I(dir)->root;
2848 struct btrfs_root *dest = NULL;
2849 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002850 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002851 int err = 0;
2852
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002853 if (!S_ISDIR(dir->i_mode))
2854 return -ENOTDIR;
2855
Yan, Zheng76dda932009-09-21 16:00:26 -04002856 vol_args = memdup_user(arg, sizeof(*vol_args));
2857 if (IS_ERR(vol_args))
2858 return PTR_ERR(vol_args);
2859
2860 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2861 namelen = strlen(vol_args->name);
2862 if (strchr(vol_args->name, '/') ||
2863 strncmp(vol_args->name, "..", namelen) == 0) {
2864 err = -EINVAL;
2865 goto out;
2866 }
2867
Al Viroa561be72011-11-23 11:57:51 -05002868 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002869 if (err)
2870 goto out;
2871
David Sterba521e0542014-04-15 16:41:44 +02002872
Al Viro00235412016-05-26 00:05:12 -04002873 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2874 if (err == -EINTR)
2875 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002876 dentry = lookup_one_len(vol_args->name, parent, namelen);
2877 if (IS_ERR(dentry)) {
2878 err = PTR_ERR(dentry);
2879 goto out_unlock_dir;
2880 }
2881
David Howells2b0143b2015-03-17 22:25:59 +00002882 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002883 err = -ENOENT;
2884 goto out_dput;
2885 }
2886
David Howells2b0143b2015-03-17 22:25:59 +00002887 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002888 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302889 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002890 /*
2891 * Regular user. Only allow this with a special mount
2892 * option, when the user has write+exec access to the
2893 * subvol root, and when rmdir(2) would have been
2894 * allowed.
2895 *
2896 * Note that this is _not_ check that the subvol is
2897 * empty or doesn't contain data that we wouldn't
2898 * otherwise be able to delete.
2899 *
2900 * Users who want to delete empty subvols should try
2901 * rmdir(2).
2902 */
2903 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002904 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002905 goto out_dput;
2906
2907 /*
2908 * Do not allow deletion if the parent dir is the same
2909 * as the dir to be deleted. That means the ioctl
2910 * must be called on the dentry referencing the root
2911 * of the subvol, not a random directory contained
2912 * within it.
2913 */
2914 err = -EINVAL;
2915 if (root == dest)
2916 goto out_dput;
2917
2918 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2919 if (err)
2920 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002921 }
2922
Miao Xie5c39da52012-10-22 11:39:53 +00002923 /* check if subvolume may be deleted by a user */
2924 err = btrfs_may_delete(dir, dentry, 1);
2925 if (err)
2926 goto out_dput;
2927
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002928 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002929 err = -EINVAL;
2930 goto out_dput;
2931 }
2932
Al Viro59551022016-01-22 15:40:57 -05002933 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002934 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002935 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002936 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002937 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002938
Yan, Zheng76dda932009-09-21 16:00:26 -04002939out_dput:
2940 dput(dentry);
2941out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002942 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002943out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002944 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002945out:
2946 kfree(vol_args);
2947 return err;
2948}
2949
Chris Mason1e701a32010-03-11 09:42:04 -05002950static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002951{
Al Viro496ad9a2013-01-23 17:07:38 -05002952 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002953 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002954 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002955 int ret;
2956
Ilya Dryomov25122d12013-01-20 15:57:57 +02002957 ret = mnt_want_write_file(file);
2958 if (ret)
2959 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002960
Ilya Dryomov25122d12013-01-20 15:57:57 +02002961 if (btrfs_root_readonly(root)) {
2962 ret = -EROFS;
2963 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002964 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002965
2966 switch (inode->i_mode & S_IFMT) {
2967 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002968 if (!capable(CAP_SYS_ADMIN)) {
2969 ret = -EPERM;
2970 goto out;
2971 }
Eric Sandeende78b512013-01-31 18:21:12 +00002972 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002973 break;
2974 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02002975 /*
2976 * Note that this does not check the file descriptor for write
2977 * access. This prevents defragmenting executables that are
2978 * running and allows defrag on files open in read-only mode.
2979 */
2980 if (!capable(CAP_SYS_ADMIN) &&
2981 inode_permission(inode, MAY_WRITE)) {
2982 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05002983 goto out;
2984 }
Chris Mason1e701a32010-03-11 09:42:04 -05002985
2986 range = kzalloc(sizeof(*range), GFP_KERNEL);
2987 if (!range) {
2988 ret = -ENOMEM;
2989 goto out;
2990 }
2991
2992 if (argp) {
2993 if (copy_from_user(range, argp,
2994 sizeof(*range))) {
2995 ret = -EFAULT;
2996 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002997 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002998 }
2999 /* compression requires us to start the IO */
3000 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3001 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3002 range->extent_thresh = (u32)-1;
3003 }
3004 } else {
3005 /* the rest are all set to zero by kzalloc */
3006 range->len = (u64)-1;
3007 }
Al Viro496ad9a2013-01-23 17:07:38 -05003008 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08003009 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003010 if (ret > 0)
3011 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003012 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003013 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003014 default:
3015 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003016 }
Chris Masone441d542009-01-05 16:57:23 -05003017out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003018 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003019 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003020}
3021
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003022static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003023{
3024 struct btrfs_ioctl_vol_args *vol_args;
3025 int ret;
3026
Chris Masone441d542009-01-05 16:57:23 -05003027 if (!capable(CAP_SYS_ADMIN))
3028 return -EPERM;
3029
David Sterba171938e2017-03-28 14:44:21 +02003030 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003031 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003032
Li Zefandae7b662009-04-08 15:06:54 +08003033 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003034 if (IS_ERR(vol_args)) {
3035 ret = PTR_ERR(vol_args);
3036 goto out;
3037 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003038
Mark Fasheh5516e592008-07-24 12:20:14 -04003039 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003040 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003041
Anand Jain43d20762014-07-01 00:58:56 +08003042 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003043 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003044
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003045 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003046out:
David Sterba171938e2017-03-28 14:44:21 +02003047 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003048 return ret;
3049}
3050
Anand Jain6b526ed2016-02-13 10:01:39 +08003051static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003052{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003053 struct inode *inode = file_inode(file);
3054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003055 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003056 int ret;
3057
Chris Masone441d542009-01-05 16:57:23 -05003058 if (!capable(CAP_SYS_ADMIN))
3059 return -EPERM;
3060
Miao Xieda249272012-11-26 08:44:50 +00003061 ret = mnt_want_write_file(file);
3062 if (ret)
3063 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003064
Li Zefandae7b662009-04-08 15:06:54 +08003065 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003066 if (IS_ERR(vol_args)) {
3067 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003068 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003069 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003070
Anand Jain6b526ed2016-02-13 10:01:39 +08003071 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003072 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3073 ret = -EOPNOTSUPP;
3074 goto out;
3075 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003076
David Sterba171938e2017-03-28 14:44:21 +02003077 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003078 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3079 goto out;
3080 }
3081
David Sterba735654e2016-02-15 18:15:21 +01003082 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003083 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003084 } else {
3085 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003086 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003087 }
David Sterba171938e2017-03-28 14:44:21 +02003088 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003089
Anand Jain6b526ed2016-02-13 10:01:39 +08003090 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003091 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003092 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003093 vol_args->devid);
3094 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003095 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003096 vol_args->name);
3097 }
Anand Jain183860f2013-05-17 10:52:45 +00003098out:
3099 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003100err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003101 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003102 return ret;
3103}
3104
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003105static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3106{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003107 struct inode *inode = file_inode(file);
3108 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003109 struct btrfs_ioctl_vol_args *vol_args;
3110 int ret;
3111
3112 if (!capable(CAP_SYS_ADMIN))
3113 return -EPERM;
3114
3115 ret = mnt_want_write_file(file);
3116 if (ret)
3117 return ret;
3118
David Sterba171938e2017-03-28 14:44:21 +02003119 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003120 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003121 goto out_drop_write;
3122 }
3123
3124 vol_args = memdup_user(arg, sizeof(*vol_args));
3125 if (IS_ERR(vol_args)) {
3126 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003127 goto out;
3128 }
3129
David Sterba58d7bbf2016-05-04 14:10:47 +02003130 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003131 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003132
3133 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003134 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003135 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003136out:
David Sterba171938e2017-03-28 14:44:21 +02003137 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003138out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003139 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003140
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003141 return ret;
3142}
3143
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003144static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3145 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003146{
Li Zefan027ed2f2011-06-08 08:27:56 +00003147 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003148 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003149 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003150 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003151
Li Zefan027ed2f2011-06-08 08:27:56 +00003152 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3153 if (!fi_args)
3154 return -ENOMEM;
3155
David Sterbad03262c2017-06-16 00:09:21 +02003156 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003157 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003158
David Sterbad03262c2017-06-16 00:09:21 +02003159 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003160 if (device->devid > fi_args->max_id)
3161 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003162 }
David Sterbad03262c2017-06-16 00:09:21 +02003163 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003164
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003165 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003166 fi_args->nodesize = fs_info->nodesize;
3167 fi_args->sectorsize = fs_info->sectorsize;
3168 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003169
Li Zefan027ed2f2011-06-08 08:27:56 +00003170 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3171 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003172
Li Zefan027ed2f2011-06-08 08:27:56 +00003173 kfree(fi_args);
3174 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003175}
3176
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003177static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3178 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003179{
3180 struct btrfs_ioctl_dev_info_args *di_args;
3181 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003182 int ret = 0;
3183 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003184
Jan Schmidt475f6382011-03-11 15:41:01 +01003185 di_args = memdup_user(arg, sizeof(*di_args));
3186 if (IS_ERR(di_args))
3187 return PTR_ERR(di_args);
3188
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003189 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003190 s_uuid = di_args->uuid;
3191
David Sterbac5593ca2017-06-16 00:09:21 +02003192 rcu_read_lock();
Anand Jaine4319cd2019-01-17 23:32:31 +08003193 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08003194 NULL, true);
Jan Schmidt475f6382011-03-11 15:41:01 +01003195
3196 if (!dev) {
3197 ret = -ENODEV;
3198 goto out;
3199 }
3200
3201 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003202 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3203 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003204 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003205 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003206 strncpy(di_args->path, rcu_str_deref(dev->name),
3207 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003208 di_args->path[sizeof(di_args->path) - 1] = 0;
3209 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003210 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003211 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003212
3213out:
David Sterbac5593ca2017-06-16 00:09:21 +02003214 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003215 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3216 ret = -EFAULT;
3217
3218 kfree(di_args);
3219 return ret;
3220}
3221
Filipe Mananad8b55242019-01-08 11:43:07 +00003222static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3223 struct inode *inode2, u64 loff2, u64 len)
3224{
3225 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3226 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3227}
3228
3229static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3230 struct inode *inode2, u64 loff2, u64 len)
3231{
3232 if (inode1 < inode2) {
3233 swap(inode1, inode2);
3234 swap(loff1, loff2);
3235 } else if (inode1 == inode2 && loff2 < loff1) {
3236 swap(loff1, loff2);
3237 }
3238 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3239 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3240}
3241
Filipe Manana57a50e22018-12-12 18:05:59 +00003242static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
Filipe Manana34a28e32018-12-07 15:25:38 +00003243 struct inode *dst, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003244{
3245 int ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003246
Filipe Manana34a28e32018-12-07 15:25:38 +00003247 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003248 * Lock destination range to serialize with concurrent readpages() and
3249 * source range to serialize with relocation.
Filipe Manana34a28e32018-12-07 15:25:38 +00003250 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003251 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Filipe Manana57a50e22018-12-12 18:05:59 +00003252 ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
Filipe Mananad8b55242019-01-08 11:43:07 +00003253 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Timofey Titovets39739092018-05-02 08:15:36 +03003254
3255 return ret;
3256}
3257
Timofey Titovetsb6728762018-05-02 08:15:37 +03003258#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3259
Timofey Titovets39739092018-05-02 08:15:36 +03003260static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3261 struct inode *dst, u64 dst_loff)
3262{
3263 int ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003264 u64 i, tail_len, chunk_count;
Filipe Manana62d54f32019-04-22 16:43:42 +01003265 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3266
3267 spin_lock(&root_dst->root_item_lock);
3268 if (root_dst->send_in_progress) {
3269 btrfs_warn_rl(root_dst->fs_info,
3270"cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3271 root_dst->root_key.objectid,
3272 root_dst->send_in_progress);
3273 spin_unlock(&root_dst->root_item_lock);
3274 return -EAGAIN;
3275 }
3276 root_dst->dedupe_in_progress++;
3277 spin_unlock(&root_dst->root_item_lock);
Timofey Titovets39739092018-05-02 08:15:36 +03003278
Timofey Titovetsb6728762018-05-02 08:15:37 +03003279 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3280 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003281
Timofey Titovetsb6728762018-05-02 08:15:37 +03003282 for (i = 0; i < chunk_count; i++) {
3283 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Filipe Manana34a28e32018-12-07 15:25:38 +00003284 dst, dst_loff);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003285 if (ret)
Filipe Manana62d54f32019-04-22 16:43:42 +01003286 goto out;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003287
3288 loff += BTRFS_MAX_DEDUPE_LEN;
3289 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3290 }
3291
3292 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003293 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
Filipe Manana34a28e32018-12-07 15:25:38 +00003294 dst_loff);
Filipe Manana62d54f32019-04-22 16:43:42 +01003295out:
3296 spin_lock(&root_dst->root_item_lock);
3297 root_dst->dedupe_in_progress--;
3298 spin_unlock(&root_dst->root_item_lock);
Mark Fasheh416161d2013-08-06 11:42:51 -07003299
3300 return ret;
3301}
3302
Filipe Mananaf82a9902014-06-01 01:50:28 +01003303static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3304 struct inode *inode,
3305 u64 endoff,
3306 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003307 const u64 olen,
3308 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003309{
3310 struct btrfs_root *root = BTRFS_I(inode)->root;
3311 int ret;
3312
3313 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003314 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003315 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003316 /*
3317 * We round up to the block size at eof when determining which
3318 * extents to clone above, but shouldn't round up the file size.
3319 */
3320 if (endoff > destoff + olen)
3321 endoff = destoff + olen;
3322 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003323 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003324
3325 ret = btrfs_update_inode(trans, root, inode);
3326 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003327 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003328 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003329 goto out;
3330 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003331 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003332out:
3333 return ret;
3334}
3335
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003336static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003337 const struct btrfs_trans_handle *trans,
3338 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003339 const u64 hole_offset,
3340 const u64 hole_len)
3341{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003342 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003343 struct extent_map *em;
3344 int ret;
3345
3346 em = alloc_extent_map();
3347 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003348 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003349 return;
3350 }
3351
Filipe Manana14f59792014-06-29 21:45:40 +01003352 if (path) {
3353 struct btrfs_file_extent_item *fi;
3354
3355 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3356 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003357 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003358 em->generation = -1;
3359 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3360 BTRFS_FILE_EXTENT_INLINE)
3361 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003362 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003363 } else {
3364 em->start = hole_offset;
3365 em->len = hole_len;
3366 em->ram_bytes = em->len;
3367 em->orig_start = hole_offset;
3368 em->block_start = EXTENT_MAP_HOLE;
3369 em->block_len = 0;
3370 em->orig_block_len = 0;
3371 em->compress_type = BTRFS_COMPRESS_NONE;
3372 em->generation = trans->transid;
3373 }
3374
3375 while (1) {
3376 write_lock(&em_tree->lock);
3377 ret = add_extent_mapping(em_tree, em, 1);
3378 write_unlock(&em_tree->lock);
3379 if (ret != -EEXIST) {
3380 free_extent_map(em);
3381 break;
3382 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003383 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003384 em->start + em->len - 1, 0);
3385 }
3386
David Sterbaee39b432014-09-30 01:33:33 +02003387 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003388 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003389}
3390
Filipe Manana8039d872015-10-13 15:15:00 +01003391/*
3392 * Make sure we do not end up inserting an inline extent into a file that has
3393 * already other (non-inline) extents. If a file has an inline extent it can
3394 * not have any other extents and the (single) inline extent must start at the
3395 * file offset 0. Failing to respect these rules will lead to file corruption,
3396 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3397 *
3398 * We can have extents that have been already written to disk or we can have
3399 * dirty ranges still in delalloc, in which case the extent maps and items are
3400 * created only when we run delalloc, and the delalloc ranges might fall outside
3401 * the range we are currently locking in the inode's io tree. So we check the
3402 * inode's i_size because of that (i_size updates are done while holding the
3403 * i_mutex, which we are holding here).
3404 * We also check to see if the inode has a size not greater than "datal" but has
3405 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3406 * protected against such concurrent fallocate calls by the i_mutex).
3407 *
3408 * If the file has no extents but a size greater than datal, do not allow the
3409 * copy because we would need turn the inline extent into a non-inline one (even
3410 * with NO_HOLES enabled). If we find our destination inode only has one inline
3411 * extent, just overwrite it with the source inline extent if its size is less
3412 * than the source extent's size, or we could copy the source inline extent's
3413 * data into the destination inode's inline extent if the later is greater then
3414 * the former.
3415 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003416static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003417 struct btrfs_trans_handle *trans,
3418 struct btrfs_path *path,
3419 struct btrfs_key *new_key,
3420 const u64 drop_start,
3421 const u64 datal,
3422 const u64 skip,
3423 const u64 size,
3424 char *inline_data)
3425{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003426 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003427 struct btrfs_root *root = BTRFS_I(dst)->root;
3428 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003429 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003430 int ret;
3431 struct btrfs_key key;
3432
3433 if (new_key->offset > 0)
3434 return -EOPNOTSUPP;
3435
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003436 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003437 key.type = BTRFS_EXTENT_DATA_KEY;
3438 key.offset = 0;
3439 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3440 if (ret < 0) {
3441 return ret;
3442 } else if (ret > 0) {
3443 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3444 ret = btrfs_next_leaf(root, path);
3445 if (ret < 0)
3446 return ret;
3447 else if (ret > 0)
3448 goto copy_inline_extent;
3449 }
3450 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003451 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003452 key.type == BTRFS_EXTENT_DATA_KEY) {
3453 ASSERT(key.offset > 0);
3454 return -EOPNOTSUPP;
3455 }
3456 } else if (i_size_read(dst) <= datal) {
3457 struct btrfs_file_extent_item *ei;
3458 u64 ext_len;
3459
3460 /*
3461 * If the file size is <= datal, make sure there are no other
3462 * extents following (can happen do to an fallocate call with
3463 * the flag FALLOC_FL_KEEP_SIZE).
3464 */
3465 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3466 struct btrfs_file_extent_item);
3467 /*
3468 * If it's an inline extent, it can not have other extents
3469 * following it.
3470 */
3471 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3472 BTRFS_FILE_EXTENT_INLINE)
3473 goto copy_inline_extent;
3474
3475 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3476 if (ext_len > aligned_end)
3477 return -EOPNOTSUPP;
3478
3479 ret = btrfs_next_item(root, path);
3480 if (ret < 0) {
3481 return ret;
3482 } else if (ret == 0) {
3483 btrfs_item_key_to_cpu(path->nodes[0], &key,
3484 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003485 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003486 key.type == BTRFS_EXTENT_DATA_KEY)
3487 return -EOPNOTSUPP;
3488 }
3489 }
3490
3491copy_inline_extent:
3492 /*
3493 * We have no extent items, or we have an extent at offset 0 which may
3494 * or may not be inlined. All these cases are dealt the same way.
3495 */
3496 if (i_size_read(dst) > datal) {
3497 /*
3498 * If the destination inode has an inline extent...
3499 * This would require copying the data from the source inline
3500 * extent into the beginning of the destination's inline extent.
3501 * But this is really complex, both extents can be compressed
3502 * or just one of them, which would require decompressing and
3503 * re-compressing data (which could increase the new compressed
3504 * size, not allowing the compressed data to fit anymore in an
3505 * inline extent).
3506 * So just don't support this case for now (it should be rare,
3507 * we are not really saving space when cloning inline extents).
3508 */
3509 return -EOPNOTSUPP;
3510 }
3511
3512 btrfs_release_path(path);
3513 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3514 if (ret)
3515 return ret;
3516 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3517 if (ret)
3518 return ret;
3519
3520 if (skip) {
3521 const u32 start = btrfs_file_extent_calc_inline_size(0);
3522
3523 memmove(inline_data + start, inline_data + start + skip, datal);
3524 }
3525
3526 write_extent_buffer(path->nodes[0], inline_data,
3527 btrfs_item_ptr_offset(path->nodes[0],
3528 path->slots[0]),
3529 size);
3530 inode_add_bytes(dst, datal);
3531
3532 return 0;
3533}
3534
Mark Fasheh32b7c682013-08-06 11:42:49 -07003535/**
3536 * btrfs_clone() - clone a range from inode file to another
3537 *
3538 * @src: Inode to clone from
3539 * @inode: Inode to clone to
3540 * @off: Offset within source to start clone from
3541 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003542 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003543 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003544 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003545 */
3546static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003547 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003548 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003549{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003550 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003551 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003552 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003553 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003554 struct btrfs_trans_handle *trans;
3555 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003556 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003557 u32 nritems;
3558 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003559 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003560 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003561 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003562
3563 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003564 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3565 if (!buf)
3566 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003567
3568 path = btrfs_alloc_path();
3569 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003570 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003571 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003572 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003573
David Sterbae4058b52015-11-27 16:31:35 +01003574 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003575 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003576 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003577 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003578 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003579
3580 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003581 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003582
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003583 /*
3584 * note the key will change type as we walk through the
3585 * tree.
3586 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003587 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003588 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3589 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003590 if (ret < 0)
3591 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003592 /*
3593 * First search, if no extent item that starts at offset off was
3594 * found but the previous item is an extent item, it's possible
3595 * it might overlap our target range, therefore process it.
3596 */
3597 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3598 btrfs_item_key_to_cpu(path->nodes[0], &key,
3599 path->slots[0] - 1);
3600 if (key.type == BTRFS_EXTENT_DATA_KEY)
3601 path->slots[0]--;
3602 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003603
Yan Zhengae01a0a2008-08-04 23:23:47 -04003604 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003605process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003606 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003607 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003608 if (ret < 0)
3609 goto out;
3610 if (ret > 0)
3611 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003612 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003613 }
3614 leaf = path->nodes[0];
3615 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003616
Yan Zhengae01a0a2008-08-04 23:23:47 -04003617 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003618 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003619 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003620 break;
3621
David Sterba962a2982014-06-04 18:41:45 +02003622 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003623 struct btrfs_file_extent_item *extent;
3624 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003625 u32 size;
3626 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003627 u64 disko = 0, diskl = 0;
3628 u64 datao = 0, datal = 0;
3629 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003630 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003631
Sage Weilc5c9cd42008-11-12 14:32:25 -05003632 extent = btrfs_item_ptr(leaf, slot,
3633 struct btrfs_file_extent_item);
3634 comp = btrfs_file_extent_compression(leaf, extent);
3635 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003636 if (type == BTRFS_FILE_EXTENT_REG ||
3637 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003638 disko = btrfs_file_extent_disk_bytenr(leaf,
3639 extent);
3640 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3641 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003642 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003643 datal = btrfs_file_extent_num_bytes(leaf,
3644 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003645 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3646 /* take upper bound, may be compressed */
3647 datal = btrfs_file_extent_ram_bytes(leaf,
3648 extent);
3649 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003650
Filipe Manana2c463822014-05-31 02:31:05 +01003651 /*
3652 * The first search might have left us at an extent
3653 * item that ends before our target range's start, can
3654 * happen if we have holes and NO_HOLES feature enabled.
3655 */
3656 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003657 path->slots[0]++;
3658 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003659 } else if (key.offset >= off + len) {
3660 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003661 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003662 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003663 size = btrfs_item_size_nr(leaf, slot);
3664 read_extent_buffer(leaf, buf,
3665 btrfs_item_ptr_offset(leaf, slot),
3666 size);
3667
3668 btrfs_release_path(path);
3669 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003670
Zheng Yan31840ae2008-09-23 13:14:14 -04003671 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003672 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003673 if (off <= key.offset)
3674 new_key.offset = key.offset + destoff - off;
3675 else
3676 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003677
Sage Weilb6f34092011-09-20 14:48:51 -04003678 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003679 * Deal with a hole that doesn't have an extent item
3680 * that represents it (NO_HOLES feature enabled).
3681 * This hole is either in the middle of the cloning
3682 * range or at the beginning (fully overlaps it or
3683 * partially overlaps it).
3684 */
3685 if (new_key.offset != last_dest_end)
3686 drop_start = last_dest_end;
3687 else
3688 drop_start = new_key.offset;
3689
3690 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003691 * 1 - adjusting old extent (we may have to split it)
3692 * 1 - add new extent
3693 * 1 - inode update
3694 */
3695 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696 if (IS_ERR(trans)) {
3697 ret = PTR_ERR(trans);
3698 goto out;
3699 }
3700
Chris Masonc8a894d2009-06-27 21:07:03 -04003701 if (type == BTRFS_FILE_EXTENT_REG ||
3702 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003703 /*
3704 * a | --- range to clone ---| b
3705 * | ------------- extent ------------- |
3706 */
3707
Antonio Ospite93915582014-06-04 14:03:48 +02003708 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003709 if (key.offset + datal > off + len)
3710 datal = off + len - key.offset;
3711
Antonio Ospite93915582014-06-04 14:03:48 +02003712 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003713 if (off > key.offset) {
3714 datao += off - key.offset;
3715 datal -= off - key.offset;
3716 }
3717
Josef Bacik5dc562c2012-08-17 13:14:17 -04003718 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003719 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003720 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003721 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003722 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003723 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003724 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003725 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003726 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003727 goto out;
3728 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003729
Sage Weilc5c9cd42008-11-12 14:32:25 -05003730 ret = btrfs_insert_empty_item(trans, root, path,
3731 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003732 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003733 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003734 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003735 goto out;
3736 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003737
3738 leaf = path->nodes[0];
3739 slot = path->slots[0];
3740 write_extent_buffer(leaf, buf,
3741 btrfs_item_ptr_offset(leaf, slot),
3742 size);
3743
3744 extent = btrfs_item_ptr(leaf, slot,
3745 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003746
Sage Weilc5c9cd42008-11-12 14:32:25 -05003747 /* disko == 0 means it's a hole */
3748 if (!disko)
3749 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003750
3751 btrfs_set_file_extent_offset(leaf, extent,
3752 datao);
3753 btrfs_set_file_extent_num_bytes(leaf, extent,
3754 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003755
Sage Weilc5c9cd42008-11-12 14:32:25 -05003756 if (disko) {
Qu Wenruo82fa1132019-04-04 14:45:35 +08003757 struct btrfs_ref ref = { 0 };
Sage Weilc5c9cd42008-11-12 14:32:25 -05003758 inode_add_bytes(inode, datal);
Qu Wenruo82fa1132019-04-04 14:45:35 +08003759 btrfs_init_generic_ref(&ref,
3760 BTRFS_ADD_DELAYED_REF, disko,
3761 diskl, 0);
3762 btrfs_init_data_ref(&ref,
3763 root->root_key.objectid,
3764 btrfs_ino(BTRFS_I(inode)),
3765 new_key.offset - datao);
3766 ret = btrfs_inc_extent_ref(trans, &ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003767 if (ret) {
3768 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003769 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003770 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003771 goto out;
3772
3773 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003774 }
3775 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3776 u64 skip = 0;
3777 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003778
Sage Weilc5c9cd42008-11-12 14:32:25 -05003779 if (off > key.offset) {
3780 skip = off - key.offset;
3781 new_key.offset += skip;
3782 }
Chris Masond3977122009-01-05 21:25:51 -05003783
Liu Boaa42ffd2012-09-18 03:52:23 -06003784 if (key.offset + datal > off + len)
3785 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003786
Sage Weilc5c9cd42008-11-12 14:32:25 -05003787 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003788 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003789 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003790 goto out;
3791 }
3792 size -= skip + trim;
3793 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003794
David Sterba4a0ab9d2017-02-10 20:18:49 +01003795 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003796 trans, path,
3797 &new_key,
3798 drop_start,
3799 datal,
3800 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003801 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003802 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003803 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003804 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003805 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003806 goto out;
3807 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003808 leaf = path->nodes[0];
3809 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003810 }
3811
Filipe Manana7ffbb592014-06-09 03:48:05 +01003812 /* If we have an implicit hole (NO_HOLES feature). */
3813 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003814 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003815 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003816 new_key.offset - drop_start);
3817
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003818 clone_update_extent_map(BTRFS_I(inode), trans,
3819 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003820
Sage Weilc5c9cd42008-11-12 14:32:25 -05003821 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003822 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003823
Filipe Manana62e23902014-08-08 02:47:06 +01003824 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003825 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003826 ret = clone_finish_inode_update(trans, inode,
3827 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003828 destoff, olen,
3829 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003830 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003831 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003832 if (new_key.offset + datal >= destoff + len)
3833 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003834 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003835 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003836 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003837
3838 if (fatal_signal_pending(current)) {
3839 ret = -EINTR;
3840 goto out;
3841 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003842 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003843 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003844
Filipe Mananaf82a9902014-06-01 01:50:28 +01003845 if (last_dest_end < destoff + len) {
3846 /*
3847 * We have an implicit hole (NO_HOLES feature is enabled) that
3848 * fully or partially overlaps our cloning range at its end.
3849 */
3850 btrfs_release_path(path);
3851
3852 /*
3853 * 1 - remove extent(s)
3854 * 1 - inode update
3855 */
3856 trans = btrfs_start_transaction(root, 2);
3857 if (IS_ERR(trans)) {
3858 ret = PTR_ERR(trans);
3859 goto out;
3860 }
3861 ret = btrfs_drop_extents(trans, root, inode,
3862 last_dest_end, destoff + len, 1);
3863 if (ret) {
3864 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003865 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003866 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003867 goto out;
3868 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003869 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3870 last_dest_end,
3871 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003872 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003873 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003874 }
3875
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003876out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003877 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003878 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003879 return ret;
3880}
3881
Zach Brown3db11b22015-11-10 16:53:32 -05003882static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3883 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003884{
Al Viro54563d42013-09-01 15:57:51 -04003885 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003886 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003887 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003888 int ret;
3889 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003890 u64 bs = fs_info->sb->s_blocksize;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003891
3892 /*
3893 * TODO:
3894 * - split compressed inline extents. annoying: we need to
3895 * decompress into destination's address_space (the file offset
3896 * may change, so source mapping won't do), then recompress (or
3897 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003898 *
3899 * - split destination inode's inline extents. The inline extents can
3900 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003901 */
3902
Filipe Mananaac765f82018-11-05 11:14:17 +00003903 /*
Filipe Manana34a28e32018-12-07 15:25:38 +00003904 * VFS's generic_remap_file_range_prep() protects us from cloning the
3905 * eof block into the middle of a file, which would result in corruption
3906 * if the file size is not blocksize aligned. So we don't need to check
3907 * for that case here.
Filipe Mananaac765f82018-11-05 11:14:17 +00003908 */
Filipe Manana34a28e32018-12-07 15:25:38 +00003909 if (off + len == src->i_size)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003910 len = ALIGN(src->i_size, bs) - off;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003911
3912 if (destoff > inode->i_size) {
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003913 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3914
Mark Fasheh32b7c682013-08-06 11:42:49 -07003915 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3916 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003917 return ret;
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003918 /*
3919 * We may have truncated the last block if the inode's size is
3920 * not sector size aligned, so we need to wait for writeback to
3921 * complete before proceeding further, otherwise we can race
3922 * with cloning and attempt to increment a reference to an
3923 * extent that no longer exists (writeback completed right after
3924 * we found the previous extent covering eof and before we
3925 * attempted to increment its reference count).
3926 */
3927 ret = btrfs_wait_ordered_range(inode, wb_start,
3928 destoff - wb_start);
3929 if (ret)
3930 return ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003931 }
3932
Filipe Mananac125b8b2014-05-23 05:03:34 +01003933 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003934 * Lock destination range to serialize with concurrent readpages() and
3935 * source range to serialize with relocation.
Filipe Mananac125b8b2014-05-23 05:03:34 +01003936 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003937 btrfs_double_extent_lock(src, off, inode, destoff, len);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003938 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Filipe Mananad8b55242019-01-08 11:43:07 +00003939 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003940 /*
3941 * Truncate page cache pages so that future reads will see the cloned
3942 * data immediately and not the previous data.
3943 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303944 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003945 round_down(destoff, PAGE_SIZE),
3946 round_up(destoff + len, PAGE_SIZE) - 1);
Filipe Manana34a28e32018-12-07 15:25:38 +00003947
3948 return ret;
3949}
3950
3951static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3952 struct file *file_out, loff_t pos_out,
3953 loff_t *len, unsigned int remap_flags)
3954{
3955 struct inode *inode_in = file_inode(file_in);
3956 struct inode *inode_out = file_inode(file_out);
3957 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3958 bool same_inode = inode_out == inode_in;
3959 u64 wb_len;
3960 int ret;
3961
3962 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3963 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3964
3965 if (btrfs_root_readonly(root_out))
3966 return -EROFS;
3967
3968 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3969 inode_in->i_sb != inode_out->i_sb)
3970 return -EXDEV;
3971 }
3972
Filipe Manana500710d2018-12-12 18:05:56 +00003973 /* don't make the dst file partly checksummed */
3974 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3975 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003976 return -EINVAL;
Filipe Manana500710d2018-12-12 18:05:56 +00003977 }
3978
Filipe Manana34a28e32018-12-07 15:25:38 +00003979 /*
3980 * Now that the inodes are locked, we need to start writeback ourselves
3981 * and can not rely on the writeback from the VFS's generic helper
3982 * generic_remap_file_range_prep() because:
3983 *
3984 * 1) For compression we must call filemap_fdatawrite_range() range
3985 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3986 * helper only calls it once;
3987 *
3988 * 2) filemap_fdatawrite_range(), called by the generic helper only
3989 * waits for the writeback to complete, i.e. for IO to be done, and
3990 * not for the ordered extents to complete. We need to wait for them
3991 * to complete so that new file extent items are in the fs tree.
3992 */
3993 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3994 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3995 else
3996 wb_len = ALIGN(*len, bs);
3997
3998 /*
3999 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
4000 * any in progress could create its ordered extents after we wait for
4001 * existing ordered extents below).
4002 */
4003 inode_dio_wait(inode_in);
4004 if (!same_inode)
4005 inode_dio_wait(inode_out);
4006
4007 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
4008 wb_len);
4009 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004010 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00004011 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
4012 wb_len);
4013 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004014 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00004015
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004016 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
Filipe Manana34a28e32018-12-07 15:25:38 +00004017 len, remap_flags);
Zach Brown3db11b22015-11-10 16:53:32 -05004018}
4019
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004020loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4021 struct file *dst_file, loff_t destoff, loff_t len,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004022 unsigned int remap_flags)
Zach Brown3db11b22015-11-10 16:53:32 -05004023{
Filipe Manana34a28e32018-12-07 15:25:38 +00004024 struct inode *src_inode = file_inode(src_file);
4025 struct inode *dst_inode = file_inode(dst_file);
4026 bool same_inode = dst_inode == src_inode;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004027 int ret;
4028
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004029 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4030 return -EINVAL;
4031
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004032 if (same_inode)
4033 inode_lock(src_inode);
4034 else
4035 lock_two_nondirectories(src_inode, dst_inode);
4036
Filipe Manana34a28e32018-12-07 15:25:38 +00004037 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4038 &len, remap_flags);
4039 if (ret < 0 || len == 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004040 goto out_unlock;
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004041
Filipe Manana34a28e32018-12-07 15:25:38 +00004042 if (remap_flags & REMAP_FILE_DEDUP)
4043 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4044 else
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004045 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
Filipe Manana34a28e32018-12-07 15:25:38 +00004046
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004047out_unlock:
Filipe Manana34a28e32018-12-07 15:25:38 +00004048 if (same_inode)
4049 inode_unlock(src_inode);
4050 else
Filipe Manana4ea748e2019-02-26 12:06:09 +00004051 unlock_two_nondirectories(src_inode, dst_inode);
Filipe Manana34a28e32018-12-07 15:25:38 +00004052
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004053 return ret < 0 ? ret : len;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004054}
4055
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004056static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4057{
Al Viro496ad9a2013-01-23 17:07:38 -05004058 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004059 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004060 struct btrfs_root *root = BTRFS_I(inode)->root;
4061 struct btrfs_root *new_root;
4062 struct btrfs_dir_item *di;
4063 struct btrfs_trans_handle *trans;
4064 struct btrfs_path *path;
4065 struct btrfs_key location;
4066 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004067 u64 objectid = 0;
4068 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004069 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004070
4071 if (!capable(CAP_SYS_ADMIN))
4072 return -EPERM;
4073
Miao Xie3c04ce02012-11-26 08:43:07 +00004074 ret = mnt_want_write_file(file);
4075 if (ret)
4076 return ret;
4077
4078 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4079 ret = -EFAULT;
4080 goto out;
4081 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004082
4083 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304084 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004085
4086 location.objectid = objectid;
4087 location.type = BTRFS_ROOT_ITEM_KEY;
4088 location.offset = (u64)-1;
4089
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004090 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004091 if (IS_ERR(new_root)) {
4092 ret = PTR_ERR(new_root);
4093 goto out;
4094 }
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09004095 if (!is_fstree(new_root->root_key.objectid)) {
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004096 ret = -ENOENT;
4097 goto out;
4098 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004099
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004100 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004101 if (!path) {
4102 ret = -ENOMEM;
4103 goto out;
4104 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004105 path->leave_spinning = 1;
4106
4107 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004108 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004109 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004110 ret = PTR_ERR(trans);
4111 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004112 }
4113
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004114 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4115 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004116 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004117 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004118 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004119 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004120 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004121 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004122 ret = -ENOENT;
4123 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004124 }
4125
4126 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4127 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4128 btrfs_mark_buffer_dirty(path->nodes[0]);
4129 btrfs_free_path(path);
4130
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004131 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004132 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004133out:
4134 mnt_drop_write_file(file);
4135 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004136}
4137
Su Yuec065f5b12018-04-02 17:24:11 +08004138static void get_block_group_info(struct list_head *groups_list,
4139 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004140{
4141 struct btrfs_block_group_cache *block_group;
4142
4143 space->total_bytes = 0;
4144 space->used_bytes = 0;
4145 space->flags = 0;
4146 list_for_each_entry(block_group, groups_list, list) {
4147 space->flags = block_group->flags;
4148 space->total_bytes += block_group->key.offset;
4149 space->used_bytes +=
4150 btrfs_block_group_used(&block_group->item);
4151 }
4152}
4153
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004154static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4155 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004156{
4157 struct btrfs_ioctl_space_args space_args;
4158 struct btrfs_ioctl_space_info space;
4159 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004160 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004161 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004162 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004163 static const u64 types[] = {
4164 BTRFS_BLOCK_GROUP_DATA,
4165 BTRFS_BLOCK_GROUP_SYSTEM,
4166 BTRFS_BLOCK_GROUP_METADATA,
4167 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4168 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004169 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004170 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004171 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004172 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004173 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004174
4175 if (copy_from_user(&space_args,
4176 (struct btrfs_ioctl_space_args __user *)arg,
4177 sizeof(space_args)))
4178 return -EFAULT;
4179
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004180 for (i = 0; i < num_types; i++) {
4181 struct btrfs_space_info *tmp;
4182
4183 info = NULL;
4184 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004185 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004186 list) {
4187 if (tmp->flags == types[i]) {
4188 info = tmp;
4189 break;
4190 }
4191 }
4192 rcu_read_unlock();
4193
4194 if (!info)
4195 continue;
4196
4197 down_read(&info->groups_sem);
4198 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4199 if (!list_empty(&info->block_groups[c]))
4200 slot_count++;
4201 }
4202 up_read(&info->groups_sem);
4203 }
Josef Bacik1406e432010-01-13 18:19:06 +00004204
David Sterba36523e952014-02-07 14:34:12 +01004205 /*
4206 * Global block reserve, exported as a space_info
4207 */
4208 slot_count++;
4209
Chris Mason7fde62b2010-03-16 15:40:10 -04004210 /* space_slots == 0 means they are asking for a count */
4211 if (space_args.space_slots == 0) {
4212 space_args.total_spaces = slot_count;
4213 goto out;
4214 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004215
Dan Rosenberg51788b12011-02-14 16:04:23 -05004216 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004217
Chris Mason7fde62b2010-03-16 15:40:10 -04004218 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004219
Chris Mason7fde62b2010-03-16 15:40:10 -04004220 /* we generally have at most 6 or so space infos, one for each raid
4221 * level. So, a whole page should be more than enough for everyone
4222 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004223 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004224 return -ENOMEM;
4225
4226 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004227 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004228 if (!dest)
4229 return -ENOMEM;
4230 dest_orig = dest;
4231
4232 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004233 for (i = 0; i < num_types; i++) {
4234 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004235
Dan Rosenberg51788b12011-02-14 16:04:23 -05004236 if (!slot_count)
4237 break;
4238
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004239 info = NULL;
4240 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004241 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004242 list) {
4243 if (tmp->flags == types[i]) {
4244 info = tmp;
4245 break;
4246 }
4247 }
4248 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004249
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004250 if (!info)
4251 continue;
4252 down_read(&info->groups_sem);
4253 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4254 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004255 get_block_group_info(&info->block_groups[c],
4256 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004257 memcpy(dest, &space, sizeof(space));
4258 dest++;
4259 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004260 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004261 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004262 if (!slot_count)
4263 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004264 }
4265 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004266 }
Josef Bacik1406e432010-01-13 18:19:06 +00004267
David Sterba36523e952014-02-07 14:34:12 +01004268 /*
4269 * Add global block reserve
4270 */
4271 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004272 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004273
4274 spin_lock(&block_rsv->lock);
4275 space.total_bytes = block_rsv->size;
4276 space.used_bytes = block_rsv->size - block_rsv->reserved;
4277 spin_unlock(&block_rsv->lock);
4278 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4279 memcpy(dest, &space, sizeof(space));
4280 space_args.total_spaces++;
4281 }
4282
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004283 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004284 (arg + sizeof(struct btrfs_ioctl_space_args));
4285
4286 if (copy_to_user(user_dest, dest_orig, alloc_size))
4287 ret = -EFAULT;
4288
4289 kfree(dest_orig);
4290out:
4291 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004292 ret = -EFAULT;
4293
4294 return ret;
4295}
4296
Miao Xie9a8c28b2012-11-26 08:40:43 +00004297static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4298 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004299{
Sage Weil46204592010-10-29 15:41:32 -04004300 struct btrfs_trans_handle *trans;
4301 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004302 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004303
Miao Xied4edf392013-02-20 09:17:06 +00004304 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004305 if (IS_ERR(trans)) {
4306 if (PTR_ERR(trans) != -ENOENT)
4307 return PTR_ERR(trans);
4308
4309 /* No running transaction, don't bother */
4310 transid = root->fs_info->last_trans_committed;
4311 goto out;
4312 }
Sage Weil46204592010-10-29 15:41:32 -04004313 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004314 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004315 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004316 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004317 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004318 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004319out:
Sage Weil46204592010-10-29 15:41:32 -04004320 if (argp)
4321 if (copy_to_user(argp, &transid, sizeof(transid)))
4322 return -EFAULT;
4323 return 0;
4324}
4325
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004326static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004327 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004328{
Sage Weil46204592010-10-29 15:41:32 -04004329 u64 transid;
4330
4331 if (argp) {
4332 if (copy_from_user(&transid, argp, sizeof(transid)))
4333 return -EFAULT;
4334 } else {
4335 transid = 0; /* current trans */
4336 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004337 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004338}
4339
Miao Xieb8e95482012-11-26 08:48:01 +00004340static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004341{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004342 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004343 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004344 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004345
4346 if (!capable(CAP_SYS_ADMIN))
4347 return -EPERM;
4348
4349 sa = memdup_user(arg, sizeof(*sa));
4350 if (IS_ERR(sa))
4351 return PTR_ERR(sa);
4352
Miao Xieb8e95482012-11-26 08:48:01 +00004353 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4354 ret = mnt_want_write_file(file);
4355 if (ret)
4356 goto out;
4357 }
4358
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004359 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004360 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4361 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004362
Filipe Manana06fe39a2018-12-14 19:50:17 +00004363 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004364 ret = -EFAULT;
4365
Miao Xieb8e95482012-11-26 08:48:01 +00004366 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4367 mnt_drop_write_file(file);
4368out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004369 kfree(sa);
4370 return ret;
4371}
4372
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004373static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004374{
4375 if (!capable(CAP_SYS_ADMIN))
4376 return -EPERM;
4377
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004378 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004379}
4380
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004381static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004382 void __user *arg)
4383{
4384 struct btrfs_ioctl_scrub_args *sa;
4385 int ret;
4386
4387 if (!capable(CAP_SYS_ADMIN))
4388 return -EPERM;
4389
4390 sa = memdup_user(arg, sizeof(*sa));
4391 if (IS_ERR(sa))
4392 return PTR_ERR(sa);
4393
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004394 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004395
Filipe Manana4fa99b02018-12-14 19:45:13 +00004396 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004397 ret = -EFAULT;
4398
4399 kfree(sa);
4400 return ret;
4401}
4402
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004403static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004404 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004405{
4406 struct btrfs_ioctl_get_dev_stats *sa;
4407 int ret;
4408
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004409 sa = memdup_user(arg, sizeof(*sa));
4410 if (IS_ERR(sa))
4411 return PTR_ERR(sa);
4412
David Sterbab27f7c02012-06-22 06:30:39 -06004413 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4414 kfree(sa);
4415 return -EPERM;
4416 }
4417
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004418 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004419
Filipe Mananaeee99572018-12-14 19:45:22 +00004420 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004421 ret = -EFAULT;
4422
4423 kfree(sa);
4424 return ret;
4425}
4426
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004427static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4428 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004429{
4430 struct btrfs_ioctl_dev_replace_args *p;
4431 int ret;
4432
4433 if (!capable(CAP_SYS_ADMIN))
4434 return -EPERM;
4435
4436 p = memdup_user(arg, sizeof(*p));
4437 if (IS_ERR(p))
4438 return PTR_ERR(p);
4439
4440 switch (p->cmd) {
4441 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004442 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004443 ret = -EROFS;
4444 goto out;
4445 }
David Sterba171938e2017-03-28 14:44:21 +02004446 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004447 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004448 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004449 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004450 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004451 }
4452 break;
4453 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004454 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004455 ret = 0;
4456 break;
4457 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004458 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004459 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004460 break;
4461 default:
4462 ret = -EINVAL;
4463 break;
4464 }
4465
Filipe Mananad3a53282019-01-08 11:42:09 +00004466 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004467 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004468out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004469 kfree(p);
4470 return ret;
4471}
4472
Jan Schmidtd7728c92011-07-07 16:48:38 +02004473static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4474{
4475 int ret = 0;
4476 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004477 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004478 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004479 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004480 struct inode_fs_paths *ipath = NULL;
4481 struct btrfs_path *path;
4482
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004483 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004484 return -EPERM;
4485
4486 path = btrfs_alloc_path();
4487 if (!path) {
4488 ret = -ENOMEM;
4489 goto out;
4490 }
4491
4492 ipa = memdup_user(arg, sizeof(*ipa));
4493 if (IS_ERR(ipa)) {
4494 ret = PTR_ERR(ipa);
4495 ipa = NULL;
4496 goto out;
4497 }
4498
4499 size = min_t(u32, ipa->size, 4096);
4500 ipath = init_ipath(size, root, path);
4501 if (IS_ERR(ipath)) {
4502 ret = PTR_ERR(ipath);
4503 ipath = NULL;
4504 goto out;
4505 }
4506
4507 ret = paths_from_inode(ipa->inum, ipath);
4508 if (ret < 0)
4509 goto out;
4510
4511 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004512 rel_ptr = ipath->fspath->val[i] -
4513 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004514 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004515 }
4516
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004517 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4518 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004519 if (ret) {
4520 ret = -EFAULT;
4521 goto out;
4522 }
4523
4524out:
4525 btrfs_free_path(path);
4526 free_ipath(ipath);
4527 kfree(ipa);
4528
4529 return ret;
4530}
4531
4532static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4533{
4534 struct btrfs_data_container *inodes = ctx;
4535 const size_t c = 3 * sizeof(u64);
4536
4537 if (inodes->bytes_left >= c) {
4538 inodes->bytes_left -= c;
4539 inodes->val[inodes->elem_cnt] = inum;
4540 inodes->val[inodes->elem_cnt + 1] = offset;
4541 inodes->val[inodes->elem_cnt + 2] = root;
4542 inodes->elem_cnt += 3;
4543 } else {
4544 inodes->bytes_missing += c - inodes->bytes_left;
4545 inodes->bytes_left = 0;
4546 inodes->elem_missed += 3;
4547 }
4548
4549 return 0;
4550}
4551
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004552static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004553 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004554{
4555 int ret = 0;
4556 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004557 struct btrfs_ioctl_logical_ino_args *loi;
4558 struct btrfs_data_container *inodes = NULL;
4559 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004560 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004561
4562 if (!capable(CAP_SYS_ADMIN))
4563 return -EPERM;
4564
4565 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304566 if (IS_ERR(loi))
4567 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004568
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004569 if (version == 1) {
4570 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004571 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004572 } else {
4573 /* All reserved bits must be 0 for now */
4574 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4575 ret = -EINVAL;
4576 goto out_loi;
4577 }
4578 /* Only accept flags we have defined so far */
4579 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4580 ret = -EINVAL;
4581 goto out_loi;
4582 }
4583 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004584 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004585 }
4586
Jan Schmidtd7728c92011-07-07 16:48:38 +02004587 path = btrfs_alloc_path();
4588 if (!path) {
4589 ret = -ENOMEM;
4590 goto out;
4591 }
4592
Jan Schmidtd7728c92011-07-07 16:48:38 +02004593 inodes = init_data_container(size);
4594 if (IS_ERR(inodes)) {
4595 ret = PTR_ERR(inodes);
4596 inodes = NULL;
4597 goto out;
4598 }
4599
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004600 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004601 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004602 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004603 ret = -ENOENT;
4604 if (ret < 0)
4605 goto out;
4606
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004607 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4608 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004609 if (ret)
4610 ret = -EFAULT;
4611
4612out:
4613 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004614 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004615out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004616 kfree(loi);
4617
4618 return ret;
4619}
4620
David Sterba008ef092018-03-21 02:05:27 +01004621void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004622 struct btrfs_ioctl_balance_args *bargs)
4623{
4624 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4625
4626 bargs->flags = bctl->flags;
4627
David Sterba3009a622018-03-21 01:31:04 +01004628 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004629 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4630 if (atomic_read(&fs_info->balance_pause_req))
4631 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004632 if (atomic_read(&fs_info->balance_cancel_req))
4633 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004634
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004635 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4636 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4637 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004638
David Sterba008ef092018-03-21 02:05:27 +01004639 spin_lock(&fs_info->balance_lock);
4640 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4641 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004642}
4643
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004644static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004645{
Al Viro496ad9a2013-01-23 17:07:38 -05004646 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004647 struct btrfs_fs_info *fs_info = root->fs_info;
4648 struct btrfs_ioctl_balance_args *bargs;
4649 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004650 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004651 int ret;
4652
4653 if (!capable(CAP_SYS_ADMIN))
4654 return -EPERM;
4655
Liu Boe54bfa312012-06-29 03:58:48 -06004656 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004657 if (ret)
4658 return ret;
4659
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004660again:
David Sterba171938e2017-03-28 14:44:21 +02004661 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004662 mutex_lock(&fs_info->balance_mutex);
4663 need_unlock = true;
4664 goto locked;
4665 }
4666
4667 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004668 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004669 * (1) some other op is running
4670 * (2) balance is running
4671 * (3) balance is paused -- special case (think resume)
4672 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004673 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004674 if (fs_info->balance_ctl) {
4675 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004676 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004677 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004678 /*
4679 * Lock released to allow other waiters to continue,
4680 * we'll reexamine the status again.
4681 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004682 mutex_lock(&fs_info->balance_mutex);
4683
4684 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004685 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004686 /* this is (3) */
4687 need_unlock = false;
4688 goto locked;
4689 }
4690
4691 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004692 goto again;
4693 } else {
4694 /* this is (2) */
4695 mutex_unlock(&fs_info->balance_mutex);
4696 ret = -EINPROGRESS;
4697 goto out;
4698 }
4699 } else {
4700 /* this is (1) */
4701 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004702 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004703 goto out;
4704 }
4705
4706locked:
David Sterba171938e2017-03-28 14:44:21 +02004707 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004708
4709 if (arg) {
4710 bargs = memdup_user(arg, sizeof(*bargs));
4711 if (IS_ERR(bargs)) {
4712 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004713 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004714 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004715
4716 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4717 if (!fs_info->balance_ctl) {
4718 ret = -ENOTCONN;
4719 goto out_bargs;
4720 }
4721
4722 bctl = fs_info->balance_ctl;
4723 spin_lock(&fs_info->balance_lock);
4724 bctl->flags |= BTRFS_BALANCE_RESUME;
4725 spin_unlock(&fs_info->balance_lock);
4726
4727 goto do_balance;
4728 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004729 } else {
4730 bargs = NULL;
4731 }
4732
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004733 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004734 ret = -EINPROGRESS;
4735 goto out_bargs;
4736 }
4737
David Sterba8d2db782015-11-04 15:38:29 +01004738 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004739 if (!bctl) {
4740 ret = -ENOMEM;
4741 goto out_bargs;
4742 }
4743
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004744 if (arg) {
4745 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4746 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4747 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4748
4749 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004750 } else {
4751 /* balance everything - no filters */
4752 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004753 }
4754
David Sterba8eb93452015-10-12 16:55:54 +02004755 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4756 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004757 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004758 }
4759
Ilya Dryomovde322262012-01-16 22:04:49 +02004760do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004761 /*
David Sterba149196a2018-03-20 20:23:09 +01004762 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4763 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4764 * restriper was paused all the way until unmount, in free_fs_info.
4765 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004766 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004767 need_unlock = false;
4768
David Sterba6fcf6e22018-05-07 17:44:03 +02004769 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004770 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004771
Filipe Mananad00c2d92019-01-08 11:42:01 +00004772 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004773 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4774 ret = -EFAULT;
4775 }
4776
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004777out_bctl:
4778 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004779out_bargs:
4780 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004781out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004782 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004783 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004784 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004785out:
Liu Boe54bfa312012-06-29 03:58:48 -06004786 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004787 return ret;
4788}
4789
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004790static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004791{
4792 if (!capable(CAP_SYS_ADMIN))
4793 return -EPERM;
4794
4795 switch (cmd) {
4796 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004797 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004798 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004799 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004800 }
4801
4802 return -EINVAL;
4803}
4804
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004805static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004806 void __user *arg)
4807{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004808 struct btrfs_ioctl_balance_args *bargs;
4809 int ret = 0;
4810
4811 if (!capable(CAP_SYS_ADMIN))
4812 return -EPERM;
4813
4814 mutex_lock(&fs_info->balance_mutex);
4815 if (!fs_info->balance_ctl) {
4816 ret = -ENOTCONN;
4817 goto out;
4818 }
4819
David Sterba8d2db782015-11-04 15:38:29 +01004820 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004821 if (!bargs) {
4822 ret = -ENOMEM;
4823 goto out;
4824 }
4825
David Sterba008ef092018-03-21 02:05:27 +01004826 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004827
4828 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4829 ret = -EFAULT;
4830
4831 kfree(bargs);
4832out:
4833 mutex_unlock(&fs_info->balance_mutex);
4834 return ret;
4835}
4836
Miao Xie905b0dd2012-11-26 08:50:11 +00004837static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004838{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004839 struct inode *inode = file_inode(file);
4840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004841 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004842 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004843
4844 if (!capable(CAP_SYS_ADMIN))
4845 return -EPERM;
4846
Miao Xie905b0dd2012-11-26 08:50:11 +00004847 ret = mnt_want_write_file(file);
4848 if (ret)
4849 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004850
4851 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004852 if (IS_ERR(sa)) {
4853 ret = PTR_ERR(sa);
4854 goto drop_write;
4855 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004856
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004858
4859 switch (sa->cmd) {
4860 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004861 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004862 break;
4863 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004864 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004865 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004866 default:
4867 ret = -EINVAL;
4868 break;
4869 }
4870
Arne Jansen5d13a372011-09-14 15:53:51 +02004871 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004872 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004873drop_write:
4874 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004875 return ret;
4876}
4877
Miao Xie905b0dd2012-11-26 08:50:11 +00004878static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004879{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004880 struct inode *inode = file_inode(file);
4881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4882 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004883 struct btrfs_ioctl_qgroup_assign_args *sa;
4884 struct btrfs_trans_handle *trans;
4885 int ret;
4886 int err;
4887
4888 if (!capable(CAP_SYS_ADMIN))
4889 return -EPERM;
4890
Miao Xie905b0dd2012-11-26 08:50:11 +00004891 ret = mnt_want_write_file(file);
4892 if (ret)
4893 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004894
4895 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004896 if (IS_ERR(sa)) {
4897 ret = PTR_ERR(sa);
4898 goto drop_write;
4899 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004900
4901 trans = btrfs_join_transaction(root);
4902 if (IS_ERR(trans)) {
4903 ret = PTR_ERR(trans);
4904 goto out;
4905 }
4906
Arne Jansen5d13a372011-09-14 15:53:51 +02004907 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004908 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004909 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004910 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004911 }
4912
Qu Wenruoe082f562015-02-27 16:24:28 +08004913 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004914 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004915 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004916 btrfs_handle_fs_error(fs_info, err,
4917 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004918 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004919 if (err && !ret)
4920 ret = err;
4921
4922out:
4923 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004924drop_write:
4925 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004926 return ret;
4927}
4928
Miao Xie905b0dd2012-11-26 08:50:11 +00004929static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004930{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004931 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004932 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004933 struct btrfs_ioctl_qgroup_create_args *sa;
4934 struct btrfs_trans_handle *trans;
4935 int ret;
4936 int err;
4937
4938 if (!capable(CAP_SYS_ADMIN))
4939 return -EPERM;
4940
Miao Xie905b0dd2012-11-26 08:50:11 +00004941 ret = mnt_want_write_file(file);
4942 if (ret)
4943 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004944
4945 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004946 if (IS_ERR(sa)) {
4947 ret = PTR_ERR(sa);
4948 goto drop_write;
4949 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004950
Miao Xied86e56c2012-11-15 11:35:41 +00004951 if (!sa->qgroupid) {
4952 ret = -EINVAL;
4953 goto out;
4954 }
4955
Arne Jansen5d13a372011-09-14 15:53:51 +02004956 trans = btrfs_join_transaction(root);
4957 if (IS_ERR(trans)) {
4958 ret = PTR_ERR(trans);
4959 goto out;
4960 }
4961
Arne Jansen5d13a372011-09-14 15:53:51 +02004962 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004963 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004964 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004965 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004966 }
4967
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004968 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004969 if (err && !ret)
4970 ret = err;
4971
4972out:
4973 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004974drop_write:
4975 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004976 return ret;
4977}
4978
Miao Xie905b0dd2012-11-26 08:50:11 +00004979static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004980{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004981 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004982 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004983 struct btrfs_ioctl_qgroup_limit_args *sa;
4984 struct btrfs_trans_handle *trans;
4985 int ret;
4986 int err;
4987 u64 qgroupid;
4988
4989 if (!capable(CAP_SYS_ADMIN))
4990 return -EPERM;
4991
Miao Xie905b0dd2012-11-26 08:50:11 +00004992 ret = mnt_want_write_file(file);
4993 if (ret)
4994 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004995
4996 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004997 if (IS_ERR(sa)) {
4998 ret = PTR_ERR(sa);
4999 goto drop_write;
5000 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005001
5002 trans = btrfs_join_transaction(root);
5003 if (IS_ERR(trans)) {
5004 ret = PTR_ERR(trans);
5005 goto out;
5006 }
5007
5008 qgroupid = sa->qgroupid;
5009 if (!qgroupid) {
5010 /* take the current subvol as qgroup */
5011 qgroupid = root->root_key.objectid;
5012 }
5013
Lu Fengqif0042d52018-07-18 14:45:35 +08005014 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005015
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005016 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005017 if (err && !ret)
5018 ret = err;
5019
5020out:
5021 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005022drop_write:
5023 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005024 return ret;
5025}
5026
Jan Schmidt2f232032013-04-25 16:04:51 +00005027static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5028{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005029 struct inode *inode = file_inode(file);
5030 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005031 struct btrfs_ioctl_quota_rescan_args *qsa;
5032 int ret;
5033
5034 if (!capable(CAP_SYS_ADMIN))
5035 return -EPERM;
5036
5037 ret = mnt_want_write_file(file);
5038 if (ret)
5039 return ret;
5040
5041 qsa = memdup_user(arg, sizeof(*qsa));
5042 if (IS_ERR(qsa)) {
5043 ret = PTR_ERR(qsa);
5044 goto drop_write;
5045 }
5046
5047 if (qsa->flags) {
5048 ret = -EINVAL;
5049 goto out;
5050 }
5051
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005052 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005053
5054out:
5055 kfree(qsa);
5056drop_write:
5057 mnt_drop_write_file(file);
5058 return ret;
5059}
5060
5061static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5062{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005063 struct inode *inode = file_inode(file);
5064 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005065 struct btrfs_ioctl_quota_rescan_args *qsa;
5066 int ret = 0;
5067
5068 if (!capable(CAP_SYS_ADMIN))
5069 return -EPERM;
5070
David Sterba8d2db782015-11-04 15:38:29 +01005071 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005072 if (!qsa)
5073 return -ENOMEM;
5074
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005075 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005076 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005077 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005078 }
5079
5080 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5081 ret = -EFAULT;
5082
5083 kfree(qsa);
5084 return ret;
5085}
5086
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005087static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5088{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005089 struct inode *inode = file_inode(file);
5090 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005091
5092 if (!capable(CAP_SYS_ADMIN))
5093 return -EPERM;
5094
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005095 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005096}
5097
Hugo Millsabccd002014-01-30 20:17:00 +00005098static long _btrfs_ioctl_set_received_subvol(struct file *file,
5099 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005100{
Al Viro496ad9a2013-01-23 17:07:38 -05005101 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005102 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005103 struct btrfs_root *root = BTRFS_I(inode)->root;
5104 struct btrfs_root_item *root_item = &root->root_item;
5105 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07005106 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005107 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005108 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005109
David Sterbabd60ea02014-01-16 15:50:22 +01005110 if (!inode_owner_or_capable(inode))
5111 return -EPERM;
5112
Alexander Block8ea05e32012-07-25 17:35:53 +02005113 ret = mnt_want_write_file(file);
5114 if (ret < 0)
5115 return ret;
5116
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005117 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005118
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005119 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005120 ret = -EINVAL;
5121 goto out;
5122 }
5123
5124 if (btrfs_root_readonly(root)) {
5125 ret = -EROFS;
5126 goto out;
5127 }
5128
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005129 /*
5130 * 1 - root item
5131 * 2 - uuid items (received uuid + subvol uuid)
5132 */
5133 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005134 if (IS_ERR(trans)) {
5135 ret = PTR_ERR(trans);
5136 trans = NULL;
5137 goto out;
5138 }
5139
5140 sa->rtransid = trans->transid;
5141 sa->rtime.sec = ct.tv_sec;
5142 sa->rtime.nsec = ct.tv_nsec;
5143
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005144 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5145 BTRFS_UUID_SIZE);
5146 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005147 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005148 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005149 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5150 root->root_key.objectid);
5151 if (ret && ret != -ENOENT) {
5152 btrfs_abort_transaction(trans, ret);
5153 btrfs_end_transaction(trans);
5154 goto out;
5155 }
5156 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005157 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5158 btrfs_set_root_stransid(root_item, sa->stransid);
5159 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005160 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5161 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5162 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5163 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005164
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005165 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005166 &root->root_key, &root->root_item);
5167 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005168 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005169 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005170 }
5171 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005172 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005173 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5174 root->root_key.objectid);
5175 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005176 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005177 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005178 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005179 }
5180 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005181 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005182out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005183 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005184 mnt_drop_write_file(file);
5185 return ret;
5186}
5187
5188#ifdef CONFIG_64BIT
5189static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5190 void __user *arg)
5191{
5192 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5193 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5194 int ret = 0;
5195
5196 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305197 if (IS_ERR(args32))
5198 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005199
David Sterba8d2db782015-11-04 15:38:29 +01005200 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005201 if (!args64) {
5202 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005203 goto out;
5204 }
5205
5206 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5207 args64->stransid = args32->stransid;
5208 args64->rtransid = args32->rtransid;
5209 args64->stime.sec = args32->stime.sec;
5210 args64->stime.nsec = args32->stime.nsec;
5211 args64->rtime.sec = args32->rtime.sec;
5212 args64->rtime.nsec = args32->rtime.nsec;
5213 args64->flags = args32->flags;
5214
5215 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5216 if (ret)
5217 goto out;
5218
5219 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5220 args32->stransid = args64->stransid;
5221 args32->rtransid = args64->rtransid;
5222 args32->stime.sec = args64->stime.sec;
5223 args32->stime.nsec = args64->stime.nsec;
5224 args32->rtime.sec = args64->rtime.sec;
5225 args32->rtime.nsec = args64->rtime.nsec;
5226 args32->flags = args64->flags;
5227
5228 ret = copy_to_user(arg, args32, sizeof(*args32));
5229 if (ret)
5230 ret = -EFAULT;
5231
5232out:
5233 kfree(args32);
5234 kfree(args64);
5235 return ret;
5236}
5237#endif
5238
5239static long btrfs_ioctl_set_received_subvol(struct file *file,
5240 void __user *arg)
5241{
5242 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5243 int ret = 0;
5244
5245 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305246 if (IS_ERR(sa))
5247 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005248
5249 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5250
5251 if (ret)
5252 goto out;
5253
Alexander Block8ea05e32012-07-25 17:35:53 +02005254 ret = copy_to_user(arg, sa, sizeof(*sa));
5255 if (ret)
5256 ret = -EFAULT;
5257
5258out:
5259 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005260 return ret;
5261}
5262
jeff.liu867ab662013-01-05 02:48:01 +00005263static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005265 struct inode *inode = file_inode(file);
5266 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005267 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005268 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005269 char label[BTRFS_LABEL_SIZE];
5270
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005271 spin_lock(&fs_info->super_lock);
5272 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5273 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005274
5275 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005276
5277 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005278 btrfs_warn(fs_info,
5279 "label is too long, return the first %zu bytes",
5280 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005281 }
5282
jeff.liu867ab662013-01-05 02:48:01 +00005283 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005284
5285 return ret ? -EFAULT : 0;
5286}
5287
jeff.liua8bfd4a2013-01-05 02:48:08 +00005288static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5289{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005290 struct inode *inode = file_inode(file);
5291 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5292 struct btrfs_root *root = BTRFS_I(inode)->root;
5293 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005294 struct btrfs_trans_handle *trans;
5295 char label[BTRFS_LABEL_SIZE];
5296 int ret;
5297
5298 if (!capable(CAP_SYS_ADMIN))
5299 return -EPERM;
5300
5301 if (copy_from_user(label, arg, sizeof(label)))
5302 return -EFAULT;
5303
5304 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005305 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005306 "unable to set label with more than %d bytes",
5307 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005308 return -EINVAL;
5309 }
5310
5311 ret = mnt_want_write_file(file);
5312 if (ret)
5313 return ret;
5314
jeff.liua8bfd4a2013-01-05 02:48:08 +00005315 trans = btrfs_start_transaction(root, 0);
5316 if (IS_ERR(trans)) {
5317 ret = PTR_ERR(trans);
5318 goto out_unlock;
5319 }
5320
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005321 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005322 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005323 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005324 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005325
5326out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005327 mnt_drop_write_file(file);
5328 return ret;
5329}
5330
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005331#define INIT_FEATURE_FLAGS(suffix) \
5332 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5333 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5334 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5335
David Sterbad5131b62016-02-17 15:26:27 +01005336int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005337{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005338 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005339 INIT_FEATURE_FLAGS(SUPP),
5340 INIT_FEATURE_FLAGS(SAFE_SET),
5341 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5342 };
5343
5344 if (copy_to_user(arg, &features, sizeof(features)))
5345 return -EFAULT;
5346
5347 return 0;
5348}
5349
5350static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5351{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005352 struct inode *inode = file_inode(file);
5353 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5354 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005355 struct btrfs_ioctl_feature_flags features;
5356
5357 features.compat_flags = btrfs_super_compat_flags(super_block);
5358 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5359 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5360
5361 if (copy_to_user(arg, &features, sizeof(features)))
5362 return -EFAULT;
5363
5364 return 0;
5365}
5366
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005367static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005368 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005369 u64 change_mask, u64 flags, u64 supported_flags,
5370 u64 safe_set, u64 safe_clear)
5371{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005372 const char *type = btrfs_feature_set_names[set];
5373 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005374 u64 disallowed, unsupported;
5375 u64 set_mask = flags & change_mask;
5376 u64 clear_mask = ~flags & change_mask;
5377
5378 unsupported = set_mask & ~supported_flags;
5379 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005380 names = btrfs_printable_features(set, unsupported);
5381 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005382 btrfs_warn(fs_info,
5383 "this kernel does not support the %s feature bit%s",
5384 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005385 kfree(names);
5386 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005387 btrfs_warn(fs_info,
5388 "this kernel does not support %s bits 0x%llx",
5389 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005390 return -EOPNOTSUPP;
5391 }
5392
5393 disallowed = set_mask & ~safe_set;
5394 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005395 names = btrfs_printable_features(set, disallowed);
5396 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005397 btrfs_warn(fs_info,
5398 "can't set the %s feature bit%s while mounted",
5399 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005400 kfree(names);
5401 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005402 btrfs_warn(fs_info,
5403 "can't set %s bits 0x%llx while mounted",
5404 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005405 return -EPERM;
5406 }
5407
5408 disallowed = clear_mask & ~safe_clear;
5409 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005410 names = btrfs_printable_features(set, disallowed);
5411 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005412 btrfs_warn(fs_info,
5413 "can't clear the %s feature bit%s while mounted",
5414 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005415 kfree(names);
5416 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005417 btrfs_warn(fs_info,
5418 "can't clear %s bits 0x%llx while mounted",
5419 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005420 return -EPERM;
5421 }
5422
5423 return 0;
5424}
5425
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005426#define check_feature(fs_info, change_mask, flags, mask_base) \
5427check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005428 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5429 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5430 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5431
5432static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5433{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005434 struct inode *inode = file_inode(file);
5435 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5436 struct btrfs_root *root = BTRFS_I(inode)->root;
5437 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005438 struct btrfs_ioctl_feature_flags flags[2];
5439 struct btrfs_trans_handle *trans;
5440 u64 newflags;
5441 int ret;
5442
5443 if (!capable(CAP_SYS_ADMIN))
5444 return -EPERM;
5445
5446 if (copy_from_user(flags, arg, sizeof(flags)))
5447 return -EFAULT;
5448
5449 /* Nothing to do */
5450 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5451 !flags[0].incompat_flags)
5452 return 0;
5453
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005454 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005455 flags[1].compat_flags, COMPAT);
5456 if (ret)
5457 return ret;
5458
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005459 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005460 flags[1].compat_ro_flags, COMPAT_RO);
5461 if (ret)
5462 return ret;
5463
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005464 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005465 flags[1].incompat_flags, INCOMPAT);
5466 if (ret)
5467 return ret;
5468
David Sterba7ab19622016-05-04 11:32:00 +02005469 ret = mnt_want_write_file(file);
5470 if (ret)
5471 return ret;
5472
David Sterba8051aa12014-02-07 14:34:04 +01005473 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005474 if (IS_ERR(trans)) {
5475 ret = PTR_ERR(trans);
5476 goto out_drop_write;
5477 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005478
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005479 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005480 newflags = btrfs_super_compat_flags(super_block);
5481 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5482 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5483 btrfs_set_super_compat_flags(super_block, newflags);
5484
5485 newflags = btrfs_super_compat_ro_flags(super_block);
5486 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5487 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5488 btrfs_set_super_compat_ro_flags(super_block, newflags);
5489
5490 newflags = btrfs_super_incompat_flags(super_block);
5491 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5492 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5493 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005494 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005495
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005496 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005497out_drop_write:
5498 mnt_drop_write_file(file);
5499
5500 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005501}
5502
Josef Bacik2351f432017-09-27 10:43:13 -04005503static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5504{
5505 struct btrfs_ioctl_send_args *arg;
5506 int ret;
5507
5508 if (compat) {
5509#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5510 struct btrfs_ioctl_send_args_32 args32;
5511
5512 ret = copy_from_user(&args32, argp, sizeof(args32));
5513 if (ret)
5514 return -EFAULT;
5515 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5516 if (!arg)
5517 return -ENOMEM;
5518 arg->send_fd = args32.send_fd;
5519 arg->clone_sources_count = args32.clone_sources_count;
5520 arg->clone_sources = compat_ptr(args32.clone_sources);
5521 arg->parent_root = args32.parent_root;
5522 arg->flags = args32.flags;
5523 memcpy(arg->reserved, args32.reserved,
5524 sizeof(args32.reserved));
5525#else
5526 return -ENOTTY;
5527#endif
5528 } else {
5529 arg = memdup_user(argp, sizeof(*arg));
5530 if (IS_ERR(arg))
5531 return PTR_ERR(arg);
5532 }
5533 ret = btrfs_ioctl_send(file, arg);
5534 kfree(arg);
5535 return ret;
5536}
5537
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005538long btrfs_ioctl(struct file *file, unsigned int
5539 cmd, unsigned long arg)
5540{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005541 struct inode *inode = file_inode(file);
5542 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5543 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005544 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005545
5546 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005547 case FS_IOC_GETFLAGS:
5548 return btrfs_ioctl_getflags(file, argp);
5549 case FS_IOC_SETFLAGS:
5550 return btrfs_ioctl_setflags(file, argp);
5551 case FS_IOC_GETVERSION:
5552 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005553 case FITRIM:
5554 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005555 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005556 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005557 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005558 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005559 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005560 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005561 case BTRFS_IOC_SUBVOL_CREATE_V2:
5562 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005563 case BTRFS_IOC_SNAP_DESTROY:
5564 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005565 case BTRFS_IOC_SUBVOL_GETFLAGS:
5566 return btrfs_ioctl_subvol_getflags(file, argp);
5567 case BTRFS_IOC_SUBVOL_SETFLAGS:
5568 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005569 case BTRFS_IOC_DEFAULT_SUBVOL:
5570 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005571 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005572 return btrfs_ioctl_defrag(file, NULL);
5573 case BTRFS_IOC_DEFRAG_RANGE:
5574 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005575 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005576 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005577 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005578 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005579 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005580 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005581 case BTRFS_IOC_RM_DEV_V2:
5582 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005583 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005584 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005585 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005586 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005587 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005588 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005589 case BTRFS_IOC_TREE_SEARCH:
5590 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005591 case BTRFS_IOC_TREE_SEARCH_V2:
5592 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005593 case BTRFS_IOC_INO_LOOKUP:
5594 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005595 case BTRFS_IOC_INO_PATHS:
5596 return btrfs_ioctl_ino_to_path(root, argp);
5597 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005598 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5599 case BTRFS_IOC_LOGICAL_INO_V2:
5600 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005601 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005602 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005603 case BTRFS_IOC_SYNC: {
5604 int ret;
5605
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005606 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005607 if (ret)
5608 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005609 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005610 /*
5611 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005612 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005613 * processing uncleaned subvols.
5614 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005615 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005616 return ret;
5617 }
Sage Weil46204592010-10-29 15:41:32 -04005618 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005619 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005620 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005621 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005622 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005623 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005624 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005625 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005626 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005627 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005628 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005629 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005630 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005631 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005632 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005633 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005634 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5635 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005636#ifdef CONFIG_64BIT
5637 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5638 return btrfs_ioctl_set_received_subvol_32(file, argp);
5639#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005640 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005641 return _btrfs_ioctl_send(file, argp, false);
5642#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5643 case BTRFS_IOC_SEND_32:
5644 return _btrfs_ioctl_send(file, argp, true);
5645#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005646 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005647 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005648 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005649 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005650 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005651 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005652 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005653 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005654 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005655 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005656 case BTRFS_IOC_QUOTA_RESCAN:
5657 return btrfs_ioctl_quota_rescan(file, argp);
5658 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5659 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005660 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5661 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005662 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005663 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005664 case BTRFS_IOC_GET_FSLABEL:
5665 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005666 case BTRFS_IOC_SET_FSLABEL:
5667 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005668 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005669 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005670 case BTRFS_IOC_GET_FEATURES:
5671 return btrfs_ioctl_get_features(file, argp);
5672 case BTRFS_IOC_SET_FEATURES:
5673 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005674 case FS_IOC_FSGETXATTR:
5675 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005676 case FS_IOC_FSSETXATTR:
5677 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005678 case BTRFS_IOC_GET_SUBVOL_INFO:
5679 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005680 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5681 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005682 case BTRFS_IOC_INO_LOOKUP_USER:
5683 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005684 }
5685
5686 return -ENOTTY;
5687}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005688
5689#ifdef CONFIG_COMPAT
5690long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5691{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005692 /*
5693 * These all access 32-bit values anyway so no further
5694 * handling is necessary.
5695 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005696 switch (cmd) {
5697 case FS_IOC32_GETFLAGS:
5698 cmd = FS_IOC_GETFLAGS;
5699 break;
5700 case FS_IOC32_SETFLAGS:
5701 cmd = FS_IOC_SETFLAGS;
5702 break;
5703 case FS_IOC32_GETVERSION:
5704 cmd = FS_IOC_GETVERSION;
5705 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005706 }
5707
5708 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5709}
5710#endif