blob: cd4e693406a0e62bda2171d7d6800c8b4ef65ab0 [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
287 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
288 if (ret && ret != -ENODATA)
289 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200290 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000291 const char *comp;
292
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700293 if (IS_SWAPFILE(inode)) {
294 ret = -ETXTBSY;
295 goto out_unlock;
296 }
297
David Sterba5c57b8b2018-04-23 15:45:18 +0200298 binode->flags |= BTRFS_INODE_COMPRESS;
299 binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000300
David Sterba93370502017-10-31 17:32:41 +0100301 comp = btrfs_compress_type2str(fs_info->compress_type);
302 if (!comp || comp[0] == 0)
303 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
304
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000305 ret = btrfs_set_prop(inode, "btrfs.compression",
306 comp, strlen(comp), 0);
307 if (ret)
308 goto out_drop;
309
Li Zefanebcb9042011-04-15 03:03:17 +0000310 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100311 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
312 if (ret && ret != -ENODATA)
313 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200314 binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000315 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200316
Li Zefan4da6f1a2011-12-29 13:39:50 +0800317 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800318 if (IS_ERR(trans)) {
319 ret = PTR_ERR(trans);
320 goto out_drop;
321 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322
David Sterba7b6a2212018-03-26 18:40:21 +0200323 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400324 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700325 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200326 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200327
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400328 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800329 out_drop:
330 if (ret) {
David Sterba5c57b8b2018-04-23 15:45:18 +0200331 binode->flags = old_flags;
332 inode->i_flags = old_i_flags;
Li Zefanf062abf02011-12-29 13:36:45 +0800333 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200334
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500336 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200337 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000338 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200339}
340
David Sterba19f93b32018-03-26 19:42:05 +0200341/*
342 * Translate btrfs internal inode flags to xflags as expected by the
343 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
344 * silently dropped.
345 */
346static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
347{
348 unsigned int xflags = 0;
349
350 if (flags & BTRFS_INODE_APPEND)
351 xflags |= FS_XFLAG_APPEND;
352 if (flags & BTRFS_INODE_IMMUTABLE)
353 xflags |= FS_XFLAG_IMMUTABLE;
354 if (flags & BTRFS_INODE_NOATIME)
355 xflags |= FS_XFLAG_NOATIME;
356 if (flags & BTRFS_INODE_NODUMP)
357 xflags |= FS_XFLAG_NODUMP;
358 if (flags & BTRFS_INODE_SYNC)
359 xflags |= FS_XFLAG_SYNC;
360
361 return xflags;
362}
363
364/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
365static int check_xflags(unsigned int flags)
366{
367 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
368 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
369 return -EOPNOTSUPP;
370 return 0;
371}
372
David Sterbae4202ac2018-03-26 19:51:16 +0200373/*
374 * Set the xflags from the internal inode flags. The remaining items of fsxattr
375 * are zeroed.
376 */
377static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
378{
379 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
380 struct fsxattr fa;
381
382 memset(&fa, 0, sizeof(fa));
383 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
384
385 if (copy_to_user(arg, &fa, sizeof(fa)))
386 return -EFAULT;
387
388 return 0;
389}
390
David Sterba025f2122018-03-26 19:51:16 +0200391static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
392{
393 struct inode *inode = file_inode(file);
394 struct btrfs_inode *binode = BTRFS_I(inode);
395 struct btrfs_root *root = binode->root;
396 struct btrfs_trans_handle *trans;
397 struct fsxattr fa;
398 unsigned old_flags;
399 unsigned old_i_flags;
400 int ret = 0;
401
402 if (!inode_owner_or_capable(inode))
403 return -EPERM;
404
405 if (btrfs_root_readonly(root))
406 return -EROFS;
407
408 memset(&fa, 0, sizeof(fa));
409 if (copy_from_user(&fa, arg, sizeof(fa)))
410 return -EFAULT;
411
412 ret = check_xflags(fa.fsx_xflags);
413 if (ret)
414 return ret;
415
416 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
417 return -EOPNOTSUPP;
418
419 ret = mnt_want_write_file(file);
420 if (ret)
421 return ret;
422
423 inode_lock(inode);
424
425 old_flags = binode->flags;
426 old_i_flags = inode->i_flags;
427
428 /* We need the capabilities to change append-only or immutable inode */
429 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
430 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
431 !capable(CAP_LINUX_IMMUTABLE)) {
432 ret = -EPERM;
433 goto out_unlock;
434 }
435
436 if (fa.fsx_xflags & FS_XFLAG_SYNC)
437 binode->flags |= BTRFS_INODE_SYNC;
438 else
439 binode->flags &= ~BTRFS_INODE_SYNC;
440 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
441 binode->flags |= BTRFS_INODE_IMMUTABLE;
442 else
443 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
444 if (fa.fsx_xflags & FS_XFLAG_APPEND)
445 binode->flags |= BTRFS_INODE_APPEND;
446 else
447 binode->flags &= ~BTRFS_INODE_APPEND;
448 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
449 binode->flags |= BTRFS_INODE_NODUMP;
450 else
451 binode->flags &= ~BTRFS_INODE_NODUMP;
452 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
453 binode->flags |= BTRFS_INODE_NOATIME;
454 else
455 binode->flags &= ~BTRFS_INODE_NOATIME;
456
457 /* 1 item for the inode */
458 trans = btrfs_start_transaction(root, 1);
459 if (IS_ERR(trans)) {
460 ret = PTR_ERR(trans);
461 goto out_unlock;
462 }
463
464 btrfs_sync_inode_flags_to_i_flags(inode);
465 inode_inc_iversion(inode);
466 inode->i_ctime = current_time(inode);
467 ret = btrfs_update_inode(trans, root, inode);
468
469 btrfs_end_transaction(trans);
470
471out_unlock:
472 if (ret) {
473 binode->flags = old_flags;
474 inode->i_flags = old_i_flags;
475 }
476
477 inode_unlock(inode);
478 mnt_drop_write_file(file);
479
480 return ret;
481}
482
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200483static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
484{
Al Viro496ad9a2013-01-23 17:07:38 -0500485 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200486
487 return put_user(inode->i_generation, arg);
488}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489
Li Dongyangf7039b12011-03-24 10:24:28 +0000490static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
491{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400492 struct inode *inode = file_inode(file);
493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000494 struct btrfs_device *device;
495 struct request_queue *q;
496 struct fstrim_range range;
497 u64 minlen = ULLONG_MAX;
498 u64 num_devices = 0;
499 int ret;
500
501 if (!capable(CAP_SYS_ADMIN))
502 return -EPERM;
503
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000504 /*
505 * If the fs is mounted with nologreplay, which requires it to be
506 * mounted in RO mode as well, we can not allow discard on free space
507 * inside block groups, because log trees refer to extents that are not
508 * pinned in a block group's free space cache (pinning the extents is
509 * precisely the first phase of replaying a log tree).
510 */
511 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
512 return -EROFS;
513
Xiao Guangrong1f781602011-04-20 10:09:16 +0000514 rcu_read_lock();
515 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
516 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000517 if (!device->bdev)
518 continue;
519 q = bdev_get_queue(device->bdev);
520 if (blk_queue_discard(q)) {
521 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100522 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000523 minlen);
524 }
525 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000526 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200527
Li Dongyangf7039b12011-03-24 10:24:28 +0000528 if (!num_devices)
529 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000530 if (copy_from_user(&range, arg, sizeof(range)))
531 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800532
533 /*
534 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
535 * block group is in the logical address space, which can be any
536 * sectorsize aligned bytenr in the range [0, U64_MAX].
537 */
538 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200539 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000540
541 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400542 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000543 if (ret < 0)
544 return ret;
545
546 if (copy_to_user(arg, &range, sizeof(range)))
547 return -EFAULT;
548
549 return 0;
550}
551
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200552int btrfs_is_empty_uuid(u8 *uuid)
553{
Chris Mason46e0f662013-11-15 12:14:55 +0100554 int i;
555
556 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
557 if (uuid[i])
558 return 0;
559 }
560 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200561}
562
Miao Xied5c12072013-02-28 10:04:33 +0000563static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400564 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100565 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200566 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000567 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400568{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400569 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570 struct btrfs_trans_handle *trans;
571 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100572 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400573 struct btrfs_inode_item *inode_item;
574 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000575 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400576 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000577 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700578 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900579 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400580 int ret;
581 int err;
582 u64 objectid;
583 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500584 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200585 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400586
David Sterba49a3c4d2016-03-24 17:49:22 +0100587 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
588 if (!root_item)
589 return -ENOMEM;
590
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400591 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400592 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100593 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000594
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800595 /*
596 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400597 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800598 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100599 if (btrfs_qgroup_level(objectid)) {
600 ret = -ENOSPC;
601 goto fail_free;
602 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800603
Miao Xied5c12072013-02-28 10:04:33 +0000604 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400605 /*
Miao Xied5c12072013-02-28 10:04:33 +0000606 * The same as the snapshot creation, please see the comment
607 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400608 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800609 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000610 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100611 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400612
Miao Xied5c12072013-02-28 10:04:33 +0000613 trans = btrfs_start_transaction(root, 0);
614 if (IS_ERR(trans)) {
615 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100616 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100617 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000618 }
619 trans->block_rsv = &block_rsv;
620 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400621
Lu Fengqia93774222018-07-18 14:45:41 +0800622 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200623 if (ret)
624 goto fail;
625
David Sterba4d75f8a2014-06-15 01:54:12 +0200626 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400627 if (IS_ERR(leaf)) {
628 ret = PTR_ERR(leaf);
629 goto fail;
630 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400631
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400632 btrfs_mark_buffer_dirty(leaf);
633
David Sterba49a3c4d2016-03-24 17:49:22 +0100634 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800635 btrfs_set_stack_inode_generation(inode_item, 1);
636 btrfs_set_stack_inode_size(inode_item, 3);
637 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400638 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400639 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800640 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400641
David Sterba49a3c4d2016-03-24 17:49:22 +0100642 btrfs_set_root_flags(root_item, 0);
643 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800644 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000645
David Sterba49a3c4d2016-03-24 17:49:22 +0100646 btrfs_set_root_bytenr(root_item, leaf->start);
647 btrfs_set_root_generation(root_item, trans->transid);
648 btrfs_set_root_level(root_item, 0);
649 btrfs_set_root_refs(root_item, 1);
650 btrfs_set_root_used(root_item, leaf->len);
651 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400652
David Sterba49a3c4d2016-03-24 17:49:22 +0100653 btrfs_set_root_generation_v2(root_item,
654 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200655 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100656 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
657 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
658 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
659 root_item->ctime = root_item->otime;
660 btrfs_set_root_ctransid(root_item, trans->transid);
661 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400662
Chris Mason925baed2008-06-25 16:01:30 -0400663 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664 free_extent_buffer(leaf);
665 leaf = NULL;
666
David Sterba49a3c4d2016-03-24 17:49:22 +0100667 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400668
669 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400670 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200671 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400672 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100673 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400674 if (ret)
675 goto fail;
676
Yan, Zheng76dda932009-09-21 16:00:26 -0400677 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400678 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100680 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400681 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 goto fail;
683 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400684
685 btrfs_record_root_in_trans(trans, new_root);
686
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000687 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700688 if (ret) {
689 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400690 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700691 goto fail;
692 }
693
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530694 mutex_lock(&new_root->objectid_mutex);
695 new_root->highest_objectid = new_dirid;
696 mutex_unlock(&new_root->objectid_mutex);
697
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400698 /*
699 * insert the directory item
700 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200701 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100702 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400703 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100704 goto fail;
705 }
Chris Mason3de45862008-11-17 21:02:50 -0500706
Lu Fengqi684572d2018-08-04 21:10:57 +0800707 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500708 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100709 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400710 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400711 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100712 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500713
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200714 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500715 ret = btrfs_update_inode(trans, root, dir);
716 BUG_ON(ret);
717
Lu Fengqi6025c192018-08-01 11:32:29 +0800718 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200719 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500720 BUG_ON(ret);
721
Lu Fengqicdb345a2018-05-29 15:01:53 +0800722 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400723 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200724 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400725 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200726
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400727fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100728 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000729 trans->block_rsv = NULL;
730 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100731 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800732
Sage Weil72fd0322010-10-29 15:41:32 -0400733 if (async_transid) {
734 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400735 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000736 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400737 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400738 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400739 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400740 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400741 if (err && !ret)
742 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500743
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900744 if (!ret) {
745 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800746 if (IS_ERR(inode))
747 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900748 d_instantiate(dentry, inode);
749 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400750 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100751
752fail_free:
753 kfree(root_item);
754 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400755}
756
Miao Xiee9662f72013-02-28 10:01:15 +0000757static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100758 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000759 u64 *async_transid, bool readonly,
760 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400761{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400762 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000763 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400764 struct btrfs_pending_snapshot *pending_snapshot;
765 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000766 int ret;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800767 bool snapshot_force_cow = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400768
Miao Xie27cdeb72014-04-02 19:51:05 +0800769 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400770 return -EINVAL;
771
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700772 if (atomic_read(&root->nr_swapfiles)) {
773 btrfs_warn(fs_info,
774 "cannot snapshot subvolume with active swapfile");
775 return -ETXTBSY;
776 }
777
David Sterba23269bf2017-02-13 11:03:44 +0100778 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100779 if (!pending_snapshot)
780 return -ENOMEM;
781
David Sterbab0c0ea62015-11-10 18:54:00 +0100782 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100783 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100784 pending_snapshot->path = btrfs_alloc_path();
785 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100786 ret = -ENOMEM;
787 goto free_pending;
788 }
789
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800790 /*
791 * Force new buffered writes to reserve space even when NOCOW is
792 * possible. This is to avoid later writeback (running dealloc) to
793 * fallback to COW mode and unexpectedly fail with ENOSPC.
794 */
David Sterbaea14b57f2017-06-22 02:19:11 +0200795 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100796 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600797 /* wait for no snapshot writes */
798 wait_event(root->subv_writers->wait,
799 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800800
Ethan Lien3cd24c62018-11-01 14:49:03 +0800801 ret = btrfs_start_delalloc_snapshot(root);
Miao Xie6a038432013-05-15 07:48:24 +0000802 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100803 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000804
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800805 /*
806 * All previous writes have started writeback in NOCOW mode, so now
807 * we force future writes to fallback to COW mode during snapshot
808 * creation.
809 */
810 atomic_inc(&root->snapshot_force_cow);
811 snapshot_force_cow = true;
812
Chris Mason6374e57a2017-06-23 09:48:21 -0700813 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000814
Miao Xie66d8f3d2012-09-06 04:02:28 -0600815 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
816 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000817 /*
818 * 1 - parent dir inode
819 * 2 - dir entries
820 * 1 - root item
821 * 2 - root ref/backref
822 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200823 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000824 */
825 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200826 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400827 false);
Miao Xied5c12072013-02-28 10:04:33 +0000828 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100829 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000830
Yan, Zhenga22285a2010-05-16 10:48:46 -0400831 pending_snapshot->dentry = dentry;
832 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800833 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000834 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000835 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400836
Miao Xied5c12072013-02-28 10:04:33 +0000837 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400838 if (IS_ERR(trans)) {
839 ret = PTR_ERR(trans);
840 goto fail;
841 }
842
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400843 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400844 list_add(&pending_snapshot->list,
845 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400846 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400847 if (async_transid) {
848 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400849 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000850 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400851 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400852 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400853 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400854 }
Miao Xieaec80302013-03-04 09:44:29 +0000855 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400856 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400857
858 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400859 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000860 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400861
Chris Masond3797302014-10-15 13:50:56 -0700862 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
863 if (ret)
864 goto fail;
865
David Howells2b0143b2015-03-17 22:25:59 +0000866 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000867 if (IS_ERR(inode)) {
868 ret = PTR_ERR(inode);
869 goto fail;
870 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900871
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000872 d_instantiate(dentry, inode);
873 ret = 0;
874fail:
David Sterba7775c812017-02-10 19:18:18 +0100875 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100876dec_and_free:
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800877 if (snapshot_force_cow)
878 atomic_dec(&root->snapshot_force_cow);
David Sterbaea14b57f2017-06-22 02:19:11 +0200879 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100880 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100881free_pending:
882 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100883 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100884 kfree(pending_snapshot);
885
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400886 return ret;
887}
888
Sage Weil4260f7c2010-10-29 15:46:43 -0400889/* copy of may_delete in fs/namei.c()
890 * Check whether we can remove a link victim from directory dir, check
891 * whether the type of victim is right.
892 * 1. We can't do it if dir is read-only (done in permission())
893 * 2. We should have write and exec permissions on dir
894 * 3. We can't remove anything from append-only dir
895 * 4. We can't do anything with immutable dir (done in permission())
896 * 5. If the sticky bit on dir is set we should either
897 * a. be owner of dir, or
898 * b. be owner of victim, or
899 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400900 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400901 * links pointing to it.
902 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
903 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
904 * 9. We can't remove a root or mountpoint.
905 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
906 * nfs_async_unlink().
907 */
908
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530909static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400910{
911 int error;
912
David Howells2b0143b2015-03-17 22:25:59 +0000913 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400914 return -ENOENT;
915
David Howells2b0143b2015-03-17 22:25:59 +0000916 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400917 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400918
919 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
920 if (error)
921 return error;
922 if (IS_APPEND(dir))
923 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000924 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
925 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400926 return -EPERM;
927 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000928 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400929 return -ENOTDIR;
930 if (IS_ROOT(victim))
931 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000932 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400933 return -EISDIR;
934 if (IS_DEADDIR(dir))
935 return -ENOENT;
936 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
937 return -EBUSY;
938 return 0;
939}
940
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400941/* copy of may_create in fs/namei.c() */
942static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
943{
David Howells2b0143b2015-03-17 22:25:59 +0000944 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400945 return -EEXIST;
946 if (IS_DEADDIR(dir))
947 return -ENOENT;
948 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
949}
950
951/*
952 * Create a new subvolume below @parent. This is largely modeled after
953 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
954 * inside this filesystem so it's quite a bit simpler.
955 */
Al Viro92872092016-11-20 19:34:31 -0500956static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100957 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400958 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200959 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000960 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400962 struct inode *dir = d_inode(parent->dentry);
963 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400964 struct dentry *dentry;
965 int error;
966
Al Viro00235412016-05-26 00:05:12 -0400967 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
968 if (error == -EINTR)
969 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400970
971 dentry = lookup_one_len(name, parent->dentry, namelen);
972 error = PTR_ERR(dentry);
973 if (IS_ERR(dentry))
974 goto out_unlock;
975
Yan, Zheng76dda932009-09-21 16:00:26 -0400976 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400977 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600978 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400979
Chris Mason9c520572012-12-17 14:26:57 -0500980 /*
981 * even if this name doesn't exist, we may get hash collisions.
982 * check for them now when we can safely fail
983 */
984 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
985 dir->i_ino, name,
986 namelen);
987 if (error)
988 goto out_dput;
989
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400990 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400991
992 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
993 goto out_up_read;
994
Chris Mason3de45862008-11-17 21:02:50 -0500995 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100996 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200997 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500998 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000999 error = create_subvol(dir, dentry, name, namelen,
1000 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001001 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001002 if (!error)
1003 fsnotify_mkdir(dir, dentry);
1004out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001005 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001006out_dput:
1007 dput(dentry);
1008out_unlock:
Al Viro59551022016-01-22 15:40:57 -05001009 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001010 return error;
1011}
1012
Chris Mason4cb53002011-05-24 15:35:30 -04001013/*
1014 * When we're defragging a range, we don't want to kick it off again
1015 * if it is really just waiting for delalloc to send it down.
1016 * If we find a nice big extent or delalloc range for the bytes in the
1017 * file you want to defrag, we return 0 to let you know to skip this
1018 * part of the file
1019 */
David Sterbaaab110a2014-07-29 17:32:10 +02001020static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001021{
1022 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1023 struct extent_map *em = NULL;
1024 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1025 u64 end;
1026
1027 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001029 read_unlock(&em_tree->lock);
1030
1031 if (em) {
1032 end = extent_map_end(em);
1033 free_extent_map(em);
1034 if (end - offset > thresh)
1035 return 0;
1036 }
1037 /* if we already have a nice delalloc here, just stop */
1038 thresh /= 2;
1039 end = count_range_bits(io_tree, &offset, offset + thresh,
1040 thresh, EXTENT_DELALLOC, 1);
1041 if (end >= thresh)
1042 return 0;
1043 return 1;
1044}
1045
1046/*
1047 * helper function to walk through a file and find extents
1048 * newer than a specific transid, and smaller than thresh.
1049 *
1050 * This is used by the defragging code to find new and small
1051 * extents
1052 */
1053static int find_new_extents(struct btrfs_root *root,
1054 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001055 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001056{
1057 struct btrfs_path *path;
1058 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001059 struct extent_buffer *leaf;
1060 struct btrfs_file_extent_item *extent;
1061 int type;
1062 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001063 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001064
1065 path = btrfs_alloc_path();
1066 if (!path)
1067 return -ENOMEM;
1068
David Sterbaa4689d22011-05-31 17:08:14 +00001069 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001070 min_key.type = BTRFS_EXTENT_DATA_KEY;
1071 min_key.offset = *off;
1072
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301073 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001074 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001075 if (ret != 0)
1076 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001077process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001078 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001079 goto none;
1080 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1081 goto none;
1082
1083 leaf = path->nodes[0];
1084 extent = btrfs_item_ptr(leaf, path->slots[0],
1085 struct btrfs_file_extent_item);
1086
1087 type = btrfs_file_extent_type(leaf, extent);
1088 if (type == BTRFS_FILE_EXTENT_REG &&
1089 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1090 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1091 *off = min_key.offset;
1092 btrfs_free_path(path);
1093 return 0;
1094 }
1095
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001096 path->slots[0]++;
1097 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1098 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1099 goto process_slot;
1100 }
1101
Chris Mason4cb53002011-05-24 15:35:30 -04001102 if (min_key.offset == (u64)-1)
1103 goto none;
1104
1105 min_key.offset++;
1106 btrfs_release_path(path);
1107 }
1108none:
1109 btrfs_free_path(path);
1110 return -ENOENT;
1111}
1112
Li Zefan6c282eb2012-06-11 16:03:35 +08001113static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001114{
1115 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001116 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001117 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001118 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001119
1120 /*
1121 * hopefully we have this extent in the tree already, try without
1122 * the full extent lock
1123 */
1124 read_lock(&em_tree->lock);
1125 em = lookup_extent_mapping(em_tree, start, len);
1126 read_unlock(&em_tree->lock);
1127
1128 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001129 struct extent_state *cached = NULL;
1130 u64 end = start + len - 1;
1131
Chris Mason940100a2010-03-10 10:52:59 -05001132 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001133 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001134 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001135 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001136
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001137 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001138 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001139 }
1140
Li Zefan6c282eb2012-06-11 16:03:35 +08001141 return em;
1142}
1143
1144static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1145{
1146 struct extent_map *next;
1147 bool ret = true;
1148
1149 /* this is the last extent */
1150 if (em->start + em->len >= i_size_read(inode))
1151 return false;
1152
1153 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001154 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1155 ret = false;
1156 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001157 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001158 ret = false;
1159
1160 free_extent_map(next);
1161 return ret;
1162}
1163
David Sterbaaab110a2014-07-29 17:32:10 +02001164static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001165 u64 *last_len, u64 *skip, u64 *defrag_end,
1166 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001167{
1168 struct extent_map *em;
1169 int ret = 1;
1170 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001171 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001172
1173 /*
1174 * make sure that once we start defragging an extent, we keep on
1175 * defragging it
1176 */
1177 if (start < *defrag_end)
1178 return 1;
1179
1180 *skip = 0;
1181
1182 em = defrag_lookup_extent(inode, start);
1183 if (!em)
1184 return 0;
1185
Chris Mason940100a2010-03-10 10:52:59 -05001186 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001187 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001188 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001189 goto out;
1190 }
1191
Liu Bo4a3560c2015-08-07 16:48:41 +08001192 if (!*defrag_end)
1193 prev_mergeable = false;
1194
Li Zefan6c282eb2012-06-11 16:03:35 +08001195 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001196 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001197 * we hit a real extent, if it is big or the next extent is not a
1198 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001199 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001200 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001201 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001202 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001203out:
Chris Mason940100a2010-03-10 10:52:59 -05001204 /*
1205 * last_len ends up being a counter of how many bytes we've defragged.
1206 * every time we choose not to defrag an extent, we reset *last_len
1207 * so that the next tiny extent will force a defrag.
1208 *
1209 * The end result of this is that tiny extents before a single big
1210 * extent will force at least part of that big extent to be defragged.
1211 */
1212 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001213 *defrag_end = extent_map_end(em);
1214 } else {
1215 *last_len = 0;
1216 *skip = extent_map_end(em);
1217 *defrag_end = 0;
1218 }
1219
1220 free_extent_map(em);
1221 return ret;
1222}
1223
Chris Mason4cb53002011-05-24 15:35:30 -04001224/*
1225 * it doesn't do much good to defrag one or two pages
1226 * at a time. This pulls in a nice chunk of pages
1227 * to COW and defrag.
1228 *
1229 * It also makes sure the delalloc code has enough
1230 * dirty data to avoid making new small extents as part
1231 * of the defrag
1232 *
1233 * It's a good idea to start RA on this range
1234 * before calling this.
1235 */
1236static int cluster_pages_for_defrag(struct inode *inode,
1237 struct page **pages,
1238 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001239 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001240{
Chris Mason4cb53002011-05-24 15:35:30 -04001241 unsigned long file_end;
1242 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001243 u64 page_start;
1244 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001245 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001246 int ret;
1247 int i;
1248 int i_done;
1249 struct btrfs_ordered_extent *ordered;
1250 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001251 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001252 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001253 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001254
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001255 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001256 if (!isize || start_index > file_end)
1257 return 0;
1258
1259 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001260
Qu Wenruo364ecf32017-02-27 15:10:38 +08001261 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001262 start_index << PAGE_SHIFT,
1263 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001264 if (ret)
1265 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001266 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001267 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001268
1269 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001270 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001271 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001272again:
Josef Bacika94733d2011-07-11 10:47:06 -04001273 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001274 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001275 if (!page)
1276 break;
1277
Miao Xie600a45e2012-02-16 15:01:24 +08001278 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001279 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001280 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001281 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001282 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001283 ordered = btrfs_lookup_ordered_extent(inode,
1284 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001285 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001286 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001287 if (!ordered)
1288 break;
1289
1290 unlock_page(page);
1291 btrfs_start_ordered_extent(inode, ordered, 1);
1292 btrfs_put_ordered_extent(ordered);
1293 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001294 /*
1295 * we unlocked the page above, so we need check if
1296 * it was released or not.
1297 */
1298 if (page->mapping != inode->i_mapping) {
1299 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001300 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001301 goto again;
1302 }
Miao Xie600a45e2012-02-16 15:01:24 +08001303 }
1304
Chris Mason4cb53002011-05-24 15:35:30 -04001305 if (!PageUptodate(page)) {
1306 btrfs_readpage(NULL, page);
1307 lock_page(page);
1308 if (!PageUptodate(page)) {
1309 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001310 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001311 ret = -EIO;
1312 break;
1313 }
1314 }
Miao Xie600a45e2012-02-16 15:01:24 +08001315
Miao Xie600a45e2012-02-16 15:01:24 +08001316 if (page->mapping != inode->i_mapping) {
1317 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001318 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001319 goto again;
1320 }
1321
Chris Mason4cb53002011-05-24 15:35:30 -04001322 pages[i] = page;
1323 i_done++;
1324 }
1325 if (!i_done || ret)
1326 goto out;
1327
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001328 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001329 goto out;
1330
1331 /*
1332 * so now we have a nice long stream of locked
1333 * and up to date pages, lets wait on them
1334 */
1335 for (i = 0; i < i_done; i++)
1336 wait_on_page_writeback(pages[i]);
1337
1338 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001339 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001340
1341 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001342 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001343 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1344 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001345 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001346 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001347
Liu Bo1f12bd02012-03-29 09:57:44 -04001348 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001349 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001350 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001351 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001352 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001353 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001354 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001355 }
1356
1357
Liu Bo9e8a4a82012-09-05 19:10:51 -06001358 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001359 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001360
1361 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001362 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001363
1364 for (i = 0; i < i_done; i++) {
1365 clear_page_dirty_for_io(pages[i]);
1366 ClearPageChecked(pages[i]);
1367 set_page_extent_mapped(pages[i]);
1368 set_page_dirty(pages[i]);
1369 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001370 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001371 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001372 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1373 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001374 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001375 return i_done;
1376out:
1377 for (i = 0; i < i_done; i++) {
1378 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001379 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001380 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001381 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001382 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001383 page_cnt << PAGE_SHIFT, true);
1384 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1385 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001386 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001387 return ret;
1388
1389}
1390
1391int btrfs_defrag_file(struct inode *inode, struct file *file,
1392 struct btrfs_ioctl_defrag_range_args *range,
1393 u64 newer_than, unsigned long max_to_defrag)
1394{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001395 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001396 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001397 struct file_ra_state *ra = NULL;
1398 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001399 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001400 u64 last_len = 0;
1401 u64 skip = 0;
1402 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001403 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001404 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001405 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001406 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001407 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001408 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001409 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001410 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001411 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001412 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001413 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001414 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001415
Liu Bo0abd5b12013-04-16 09:20:28 +00001416 if (isize == 0)
1417 return 0;
1418
1419 if (range->start >= isize)
1420 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001421
David Sterba1e2ef462017-07-17 20:01:59 +02001422 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001423 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1424 return -EINVAL;
1425 if (range->compress_type)
1426 compress_type = range->compress_type;
1427 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428
Liu Bo0abd5b12013-04-16 09:20:28 +00001429 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001430 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431
Chris Mason4cb53002011-05-24 15:35:30 -04001432 /*
David Sterba0a52d102017-06-22 03:22:58 +02001433 * If we were not given a file, allocate a readahead context. As
1434 * readahead is just an optimization, defrag will work without it so
1435 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001436 */
1437 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001438 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001439 if (ra)
1440 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001441 } else {
1442 ra = &file->f_ra;
1443 }
1444
David Sterba63e727e2017-06-22 03:13:02 +02001445 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001446 if (!pages) {
1447 ret = -ENOMEM;
1448 goto out_ra;
1449 }
1450
1451 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001452 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001453 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001454 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001455 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001456 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001457 }
1458
Chris Mason4cb53002011-05-24 15:35:30 -04001459 if (newer_than) {
1460 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001461 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001462 if (!ret) {
1463 range->start = newer_off;
1464 /*
1465 * we always align our defrag to help keep
1466 * the extents in the file evenly spaced
1467 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001468 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001469 } else
1470 goto out_ra;
1471 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001472 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001473 }
1474 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301475 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001476
Li Zefan2a0f7f52011-10-10 15:43:34 -04001477 /*
1478 * make writeback starts from i, so the defrag range can be
1479 * written sequentially.
1480 */
1481 if (i < inode->i_mapping->writeback_index)
1482 inode->i_mapping->writeback_index = i;
1483
Chris Masonf7f43cc2011-10-11 11:41:40 -04001484 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001485 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001486 /*
1487 * make sure we stop running if someone unmounts
1488 * the FS
1489 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001490 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001491 break;
1492
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001493 if (btrfs_defrag_cancelled(fs_info)) {
1494 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001495 ret = -EAGAIN;
1496 break;
1497 }
1498
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001499 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001500 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001501 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001502 unsigned long next;
1503 /*
1504 * the should_defrag function tells us how much to skip
1505 * bump our counter by the suggested amount
1506 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001507 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001508 i = max(i + 1, next);
1509 continue;
1510 }
Li Zefan008873e2011-09-02 15:57:07 +08001511
1512 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001513 cluster = (PAGE_ALIGN(defrag_end) >>
1514 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001515 cluster = min(cluster, max_cluster);
1516 } else {
1517 cluster = max_cluster;
1518 }
1519
Li Zefan008873e2011-09-02 15:57:07 +08001520 if (i + cluster > ra_index) {
1521 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001522 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001523 page_cache_sync_readahead(inode->i_mapping, ra,
1524 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301525 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001526 }
Chris Mason940100a2010-03-10 10:52:59 -05001527
Al Viro59551022016-01-22 15:40:57 -05001528 inode_lock(inode);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001529 if (IS_SWAPFILE(inode)) {
1530 ret = -ETXTBSY;
1531 } else {
1532 if (do_compress)
1533 BTRFS_I(inode)->defrag_compress = compress_type;
1534 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1535 }
Liu Boecb8bea2012-03-29 09:57:44 -04001536 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001537 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001538 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001539 }
Chris Mason940100a2010-03-10 10:52:59 -05001540
Chris Mason4cb53002011-05-24 15:35:30 -04001541 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001542 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001543 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001544
1545 if (newer_than) {
1546 if (newer_off == (u64)-1)
1547 break;
1548
Liu Boe1f041e2012-03-29 09:57:45 -04001549 if (ret > 0)
1550 i += ret;
1551
Chris Mason4cb53002011-05-24 15:35:30 -04001552 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001553 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001554
Byongho Leeee221842015-12-15 01:42:10 +09001555 ret = find_new_extents(root, inode, newer_than,
1556 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001557 if (!ret) {
1558 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001559 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001560 } else {
1561 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001562 }
Chris Mason4cb53002011-05-24 15:35:30 -04001563 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001564 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001565 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001566 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001567 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001568 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001569 last_len = 0;
1570 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001571 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572 }
1573
Filipe Mananadec8ef92014-03-01 10:55:54 +00001574 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001575 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001576 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1577 &BTRFS_I(inode)->runtime_flags))
1578 filemap_flush(inode->i_mapping);
1579 }
Chris Mason1e701a32010-03-11 09:42:04 -05001580
Li Zefan1a419d82010-10-25 15:12:50 +08001581 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001582 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001583 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1584 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001585 }
1586
Diego Calleja60ccf822011-09-01 16:33:57 +02001587 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001588
Chris Mason4cb53002011-05-24 15:35:30 -04001589out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001590 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001591 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001592 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001593 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001594 }
Chris Mason4cb53002011-05-24 15:35:30 -04001595 if (!file)
1596 kfree(ra);
1597 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001598 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001599}
1600
Miao Xie198605a2012-11-26 08:43:45 +00001601static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001602 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001603{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001604 struct inode *inode = file_inode(file);
1605 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001606 u64 new_size;
1607 u64 old_size;
1608 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001609 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001610 struct btrfs_ioctl_vol_args *vol_args;
1611 struct btrfs_trans_handle *trans;
1612 struct btrfs_device *device = NULL;
1613 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001614 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001615 char *devstr = NULL;
1616 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001617 int mod = 0;
1618
Chris Masone441d542009-01-05 16:57:23 -05001619 if (!capable(CAP_SYS_ADMIN))
1620 return -EPERM;
1621
Miao Xie198605a2012-11-26 08:43:45 +00001622 ret = mnt_want_write_file(file);
1623 if (ret)
1624 return ret;
1625
David Sterba171938e2017-03-28 14:44:21 +02001626 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001627 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001628 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001629 }
1630
Li Zefandae7b662009-04-08 15:06:54 +08001631 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001632 if (IS_ERR(vol_args)) {
1633 ret = PTR_ERR(vol_args);
1634 goto out;
1635 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001636
1637 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001638
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001639 sizestr = vol_args->name;
1640 devstr = strchr(sizestr, ':');
1641 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001642 sizestr = devstr + 1;
1643 *devstr = '\0';
1644 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001645 ret = kstrtoull(devstr, 10, &devid);
1646 if (ret)
1647 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001648 if (!devid) {
1649 ret = -EINVAL;
1650 goto out_free;
1651 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001652 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001653 }
Miao Xiedba60f32012-12-21 09:19:51 +00001654
Anand Jain09ba3bc2019-01-19 14:48:55 +08001655 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001656 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001657 btrfs_info(fs_info, "resizer unable to find device %llu",
1658 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001659 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001660 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001661 }
Miao Xiedba60f32012-12-21 09:19:51 +00001662
Anand Jainebbede42017-12-04 12:54:52 +08001663 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001664 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001665 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001666 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001667 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001668 goto out_free;
1669 }
1670
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001671 if (!strcmp(sizestr, "max"))
1672 new_size = device->bdev->bd_inode->i_size;
1673 else {
1674 if (sizestr[0] == '-') {
1675 mod = -1;
1676 sizestr++;
1677 } else if (sizestr[0] == '+') {
1678 mod = 1;
1679 sizestr++;
1680 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001681 new_size = memparse(sizestr, &retptr);
1682 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001683 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001684 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001685 }
1686 }
1687
Anand Jain401e29c2017-12-04 12:54:55 +08001688 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001689 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001690 goto out_free;
1691 }
1692
Miao Xie7cc8e582014-09-03 21:35:38 +08001693 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001694
1695 if (mod < 0) {
1696 if (new_size > old_size) {
1697 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001698 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001699 }
1700 new_size = old_size - new_size;
1701 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001702 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001703 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001704 goto out_free;
1705 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 new_size = old_size + new_size;
1707 }
1708
Byongho Leeee221842015-12-15 01:42:10 +09001709 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001710 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001711 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001712 }
1713 if (new_size > device->bdev->bd_inode->i_size) {
1714 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001715 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001716 }
1717
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001718 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001719
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001720 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1721 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001722
1723 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001724 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001725 if (IS_ERR(trans)) {
1726 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001727 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001728 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001729 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001730 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001731 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001732 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001733 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001734
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001735out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001736 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001737out:
David Sterba171938e2017-03-28 14:44:21 +02001738 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001739 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001740 return ret;
1741}
1742
Sage Weil72fd0322010-10-29 15:41:32 -04001743static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001744 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001745 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001746 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001747{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001748 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001749 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001751 if (!S_ISDIR(file_inode(file)->i_mode))
1752 return -ENOTDIR;
1753
Liu Boa874a632012-06-29 03:58:46 -06001754 ret = mnt_want_write_file(file);
1755 if (ret)
1756 goto out;
1757
Sage Weil72fd0322010-10-29 15:41:32 -04001758 namelen = strlen(name);
1759 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001760 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001761 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001762 }
1763
Chris Mason16780ca2012-02-20 22:14:55 -05001764 if (name[0] == '.' &&
1765 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1766 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001767 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001768 }
1769
Chris Mason3de45862008-11-17 21:02:50 -05001770 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001771 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001772 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001773 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001774 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001775 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001776 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001777 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001778 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001779 }
1780
Al Viro496ad9a2013-01-23 17:07:38 -05001781 src_inode = file_inode(src.file);
1782 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001783 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001784 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001785 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001786 } else if (!inode_owner_or_capable(src_inode)) {
1787 /*
1788 * Subvolume creation is not restricted, but snapshots
1789 * are limited to own subvolumes only
1790 */
1791 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001792 } else {
1793 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1794 BTRFS_I(src_inode)->root,
1795 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001796 }
Al Viro2903ff02012-08-28 12:52:22 -04001797 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001798 }
Liu Boa874a632012-06-29 03:58:46 -06001799out_drop_write:
1800 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001801out:
Sage Weil72fd0322010-10-29 15:41:32 -04001802 return ret;
1803}
1804
1805static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001806 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001807{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001808 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001809 int ret;
1810
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001811 if (!S_ISDIR(file_inode(file)->i_mode))
1812 return -ENOTDIR;
1813
Li Zefanfa0d2b92010-12-20 15:53:28 +08001814 vol_args = memdup_user(arg, sizeof(*vol_args));
1815 if (IS_ERR(vol_args))
1816 return PTR_ERR(vol_args);
1817 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001818
Li Zefanfa0d2b92010-12-20 15:53:28 +08001819 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001820 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001821 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001822
Li Zefanfa0d2b92010-12-20 15:53:28 +08001823 kfree(vol_args);
1824 return ret;
1825}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001826
Li Zefanfa0d2b92010-12-20 15:53:28 +08001827static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1828 void __user *arg, int subvol)
1829{
1830 struct btrfs_ioctl_vol_args_v2 *vol_args;
1831 int ret;
1832 u64 transid = 0;
1833 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001834 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001835 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001836
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001837 if (!S_ISDIR(file_inode(file)->i_mode))
1838 return -ENOTDIR;
1839
Li Zefanfa0d2b92010-12-20 15:53:28 +08001840 vol_args = memdup_user(arg, sizeof(*vol_args));
1841 if (IS_ERR(vol_args))
1842 return PTR_ERR(vol_args);
1843 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001844
Li Zefanb83cc962010-12-20 16:04:08 +08001845 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001846 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1847 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001848 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001849 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001850 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001851
1852 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1853 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001854 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1855 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001856 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001857 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001858 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001859 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001860 }
1861 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1862 if (IS_ERR(inherit)) {
1863 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001864 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001865 }
1866 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001867
1868 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001869 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001870 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001871 if (ret)
1872 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001873
Dan Carpenterc47ca322014-09-04 14:09:15 +03001874 if (ptr && copy_to_user(arg +
1875 offsetof(struct btrfs_ioctl_vol_args_v2,
1876 transid),
1877 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001878 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001879
1880free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001881 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001882free_args:
1883 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001884 return ret;
1885}
1886
Li Zefan0caa1022010-12-20 16:30:25 +08001887static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1888 void __user *arg)
1889{
Al Viro496ad9a2013-01-23 17:07:38 -05001890 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001892 struct btrfs_root *root = BTRFS_I(inode)->root;
1893 int ret = 0;
1894 u64 flags = 0;
1895
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001896 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001897 return -EINVAL;
1898
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001899 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001900 if (btrfs_root_readonly(root))
1901 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001902 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001903
1904 if (copy_to_user(arg, &flags, sizeof(flags)))
1905 ret = -EFAULT;
1906
1907 return ret;
1908}
1909
1910static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1911 void __user *arg)
1912{
Al Viro496ad9a2013-01-23 17:07:38 -05001913 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001914 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001915 struct btrfs_root *root = BTRFS_I(inode)->root;
1916 struct btrfs_trans_handle *trans;
1917 u64 root_flags;
1918 u64 flags;
1919 int ret = 0;
1920
David Sterbabd60ea02014-01-16 15:50:22 +01001921 if (!inode_owner_or_capable(inode))
1922 return -EPERM;
1923
Liu Bob9ca0662012-06-29 03:58:49 -06001924 ret = mnt_want_write_file(file);
1925 if (ret)
1926 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001927
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001928 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001929 ret = -EINVAL;
1930 goto out_drop_write;
1931 }
Li Zefan0caa1022010-12-20 16:30:25 +08001932
Liu Bob9ca0662012-06-29 03:58:49 -06001933 if (copy_from_user(&flags, arg, sizeof(flags))) {
1934 ret = -EFAULT;
1935 goto out_drop_write;
1936 }
Li Zefan0caa1022010-12-20 16:30:25 +08001937
Liu Bob9ca0662012-06-29 03:58:49 -06001938 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1939 ret = -EINVAL;
1940 goto out_drop_write;
1941 }
Li Zefan0caa1022010-12-20 16:30:25 +08001942
Liu Bob9ca0662012-06-29 03:58:49 -06001943 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1944 ret = -EOPNOTSUPP;
1945 goto out_drop_write;
1946 }
Li Zefan0caa1022010-12-20 16:30:25 +08001947
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001948 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001949
1950 /* nothing to do */
1951 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001952 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001953
1954 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001955 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001956 btrfs_set_root_flags(&root->root_item,
1957 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001958 } else {
1959 /*
1960 * Block RO -> RW transition if this subvolume is involved in
1961 * send
1962 */
1963 spin_lock(&root->root_item_lock);
1964 if (root->send_in_progress == 0) {
1965 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001966 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001967 spin_unlock(&root->root_item_lock);
1968 } else {
1969 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001970 btrfs_warn(fs_info,
1971 "Attempt to set subvolume %llu read-write during send",
1972 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001973 ret = -EPERM;
1974 goto out_drop_sem;
1975 }
1976 }
Li Zefan0caa1022010-12-20 16:30:25 +08001977
1978 trans = btrfs_start_transaction(root, 1);
1979 if (IS_ERR(trans)) {
1980 ret = PTR_ERR(trans);
1981 goto out_reset;
1982 }
1983
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001984 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001985 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001986 if (ret < 0) {
1987 btrfs_end_transaction(trans);
1988 goto out_reset;
1989 }
Li Zefan0caa1022010-12-20 16:30:25 +08001990
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001991 ret = btrfs_commit_transaction(trans);
1992
Li Zefan0caa1022010-12-20 16:30:25 +08001993out_reset:
1994 if (ret)
1995 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001996out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001997 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001998out_drop_write:
1999 mnt_drop_write_file(file);
2000out:
Li Zefan0caa1022010-12-20 16:30:25 +08002001 return ret;
2002}
2003
Chris Masonac8e9812010-02-28 15:39:26 -05002004static noinline int key_in_sk(struct btrfs_key *key,
2005 struct btrfs_ioctl_search_key *sk)
2006{
Chris Masonabc6e132010-03-18 12:10:08 -04002007 struct btrfs_key test;
2008 int ret;
2009
2010 test.objectid = sk->min_objectid;
2011 test.type = sk->min_type;
2012 test.offset = sk->min_offset;
2013
2014 ret = btrfs_comp_cpu_keys(key, &test);
2015 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002016 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002017
2018 test.objectid = sk->max_objectid;
2019 test.type = sk->max_type;
2020 test.offset = sk->max_offset;
2021
2022 ret = btrfs_comp_cpu_keys(key, &test);
2023 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002024 return 0;
2025 return 1;
2026}
2027
Jeff Mahoneydf397562016-06-21 20:18:21 -04002028static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002029 struct btrfs_key *key,
2030 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002031 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002032 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002033 unsigned long *sk_offset,
2034 int *num_found)
2035{
2036 u64 found_transid;
2037 struct extent_buffer *leaf;
2038 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002039 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002040 unsigned long item_off;
2041 unsigned long item_len;
2042 int nritems;
2043 int i;
2044 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002045 int ret = 0;
2046
2047 leaf = path->nodes[0];
2048 slot = path->slots[0];
2049 nritems = btrfs_header_nritems(leaf);
2050
2051 if (btrfs_header_generation(leaf) > sk->max_transid) {
2052 i = nritems;
2053 goto advance_key;
2054 }
2055 found_transid = btrfs_header_generation(leaf);
2056
2057 for (i = slot; i < nritems; i++) {
2058 item_off = btrfs_item_ptr_offset(leaf, i);
2059 item_len = btrfs_item_size_nr(leaf, i);
2060
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002061 btrfs_item_key_to_cpu(leaf, key, i);
2062 if (!key_in_sk(key, sk))
2063 continue;
2064
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002065 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002066 if (*num_found) {
2067 ret = 1;
2068 goto out;
2069 }
Chris Masonac8e9812010-02-28 15:39:26 -05002070
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002071 /*
2072 * return one empty item back for v1, which does not
2073 * handle -EOVERFLOW
2074 */
2075
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002076 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002077 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002078 ret = -EOVERFLOW;
2079 }
Chris Masonac8e9812010-02-28 15:39:26 -05002080
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002081 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002082 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002083 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002084 }
2085
Chris Masonac8e9812010-02-28 15:39:26 -05002086 sh.objectid = key->objectid;
2087 sh.offset = key->offset;
2088 sh.type = key->type;
2089 sh.len = item_len;
2090 sh.transid = found_transid;
2091
2092 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002093 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2094 ret = -EFAULT;
2095 goto out;
2096 }
2097
Chris Masonac8e9812010-02-28 15:39:26 -05002098 *sk_offset += sizeof(sh);
2099
2100 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002101 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002102 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002103 if (read_extent_buffer_to_user(leaf, up,
2104 item_off, item_len)) {
2105 ret = -EFAULT;
2106 goto out;
2107 }
2108
Chris Masonac8e9812010-02-28 15:39:26 -05002109 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002110 }
Hugo Millse2156862011-05-14 17:43:41 +00002111 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002112
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002113 if (ret) /* -EOVERFLOW from above */
2114 goto out;
2115
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002116 if (*num_found >= sk->nr_items) {
2117 ret = 1;
2118 goto out;
2119 }
Chris Masonac8e9812010-02-28 15:39:26 -05002120 }
2121advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002122 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002123 test.objectid = sk->max_objectid;
2124 test.type = sk->max_type;
2125 test.offset = sk->max_offset;
2126 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2127 ret = 1;
2128 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002129 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002130 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002131 key->offset = 0;
2132 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002133 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002134 key->offset = 0;
2135 key->type = 0;
2136 key->objectid++;
2137 } else
2138 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002139out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002140 /*
2141 * 0: all items from this leaf copied, continue with next
2142 * 1: * more items can be copied, but unused buffer is too small
2143 * * all items were found
2144 * Either way, it will stops the loop which iterates to the next
2145 * leaf
2146 * -EOVERFLOW: item was to large for buffer
2147 * -EFAULT: could not copy extent buffer back to userspace
2148 */
Chris Masonac8e9812010-02-28 15:39:26 -05002149 return ret;
2150}
2151
2152static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002153 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002154 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002155 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002156{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002157 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002158 struct btrfs_root *root;
2159 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002160 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002161 int ret;
2162 int num_found = 0;
2163 unsigned long sk_offset = 0;
2164
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002165 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2166 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002167 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002168 }
Gerhard Heift12544442014-01-30 16:23:58 +01002169
Chris Masonac8e9812010-02-28 15:39:26 -05002170 path = btrfs_alloc_path();
2171 if (!path)
2172 return -ENOMEM;
2173
2174 if (sk->tree_id == 0) {
2175 /* search the root of the inode that was passed */
2176 root = BTRFS_I(inode)->root;
2177 } else {
2178 key.objectid = sk->tree_id;
2179 key.type = BTRFS_ROOT_ITEM_KEY;
2180 key.offset = (u64)-1;
2181 root = btrfs_read_fs_root_no_name(info, &key);
2182 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002183 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002184 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002185 }
2186 }
2187
2188 key.objectid = sk->min_objectid;
2189 key.type = sk->min_type;
2190 key.offset = sk->min_offset;
2191
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302192 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002193 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002194 if (ret != 0) {
2195 if (ret > 0)
2196 ret = 0;
2197 goto err;
2198 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002199 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002200 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002201 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002202 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002203 break;
2204
2205 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002206 if (ret > 0)
2207 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002208err:
2209 sk->nr_items = num_found;
2210 btrfs_free_path(path);
2211 return ret;
2212}
2213
2214static noinline int btrfs_ioctl_tree_search(struct file *file,
2215 void __user *argp)
2216{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002217 struct btrfs_ioctl_search_args __user *uargs;
2218 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002219 struct inode *inode;
2220 int ret;
2221 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002222
2223 if (!capable(CAP_SYS_ADMIN))
2224 return -EPERM;
2225
Gerhard Heiftba346b32014-01-30 16:24:02 +01002226 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002227
Gerhard Heiftba346b32014-01-30 16:24:02 +01002228 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2229 return -EFAULT;
2230
2231 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002232
Al Viro496ad9a2013-01-23 17:07:38 -05002233 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002234 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002235
2236 /*
2237 * In the origin implementation an overflow is handled by returning a
2238 * search header with a len of zero, so reset ret.
2239 */
2240 if (ret == -EOVERFLOW)
2241 ret = 0;
2242
Gerhard Heiftba346b32014-01-30 16:24:02 +01002243 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002244 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002245 return ret;
2246}
2247
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002248static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2249 void __user *argp)
2250{
2251 struct btrfs_ioctl_search_args_v2 __user *uarg;
2252 struct btrfs_ioctl_search_args_v2 args;
2253 struct inode *inode;
2254 int ret;
2255 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002256 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002257
2258 if (!capable(CAP_SYS_ADMIN))
2259 return -EPERM;
2260
2261 /* copy search header and buffer size */
2262 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2263 if (copy_from_user(&args, uarg, sizeof(args)))
2264 return -EFAULT;
2265
2266 buf_size = args.buf_size;
2267
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002268 /* limit result size to 16MB */
2269 if (buf_size > buf_limit)
2270 buf_size = buf_limit;
2271
2272 inode = file_inode(file);
2273 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002274 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002275 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2276 ret = -EFAULT;
2277 else if (ret == -EOVERFLOW &&
2278 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2279 ret = -EFAULT;
2280
Yan, Zheng76dda932009-09-21 16:00:26 -04002281 return ret;
2282}
2283
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002284/*
Chris Masonac8e9812010-02-28 15:39:26 -05002285 * Search INODE_REFs to identify path name of 'dirid' directory
2286 * in a 'tree_id' tree. and sets path name to 'name'.
2287 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002288static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2289 u64 tree_id, u64 dirid, char *name)
2290{
2291 struct btrfs_root *root;
2292 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002293 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002294 int ret = -1;
2295 int slot;
2296 int len;
2297 int total_len = 0;
2298 struct btrfs_inode_ref *iref;
2299 struct extent_buffer *l;
2300 struct btrfs_path *path;
2301
2302 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2303 name[0]='\0';
2304 return 0;
2305 }
2306
2307 path = btrfs_alloc_path();
2308 if (!path)
2309 return -ENOMEM;
2310
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002311 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002312
2313 key.objectid = tree_id;
2314 key.type = BTRFS_ROOT_ITEM_KEY;
2315 key.offset = (u64)-1;
2316 root = btrfs_read_fs_root_no_name(info, &key);
2317 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002318 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002319 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002320 }
2321
2322 key.objectid = dirid;
2323 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002324 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002325
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302326 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002327 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2328 if (ret < 0)
2329 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002330 else if (ret > 0) {
2331 ret = btrfs_previous_item(root, path, dirid,
2332 BTRFS_INODE_REF_KEY);
2333 if (ret < 0)
2334 goto out;
2335 else if (ret > 0) {
2336 ret = -ENOENT;
2337 goto out;
2338 }
2339 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002340
2341 l = path->nodes[0];
2342 slot = path->slots[0];
2343 btrfs_item_key_to_cpu(l, &key, slot);
2344
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002345 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2346 len = btrfs_inode_ref_name_len(l, iref);
2347 ptr -= len + 1;
2348 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002349 if (ptr < name) {
2350 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002351 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002352 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002353
2354 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302355 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002356
2357 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2358 break;
2359
David Sterbab3b4aa72011-04-21 01:20:15 +02002360 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002361 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002362 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002363 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002364 }
Li Zefan77906a502011-07-14 03:16:00 +00002365 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302366 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002367 ret = 0;
2368out:
2369 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002370 return ret;
2371}
2372
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002373static int btrfs_search_path_in_tree_user(struct inode *inode,
2374 struct btrfs_ioctl_ino_lookup_user_args *args)
2375{
2376 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2377 struct super_block *sb = inode->i_sb;
2378 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2379 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2380 u64 dirid = args->dirid;
2381 unsigned long item_off;
2382 unsigned long item_len;
2383 struct btrfs_inode_ref *iref;
2384 struct btrfs_root_ref *rref;
2385 struct btrfs_root *root;
2386 struct btrfs_path *path;
2387 struct btrfs_key key, key2;
2388 struct extent_buffer *leaf;
2389 struct inode *temp_inode;
2390 char *ptr;
2391 int slot;
2392 int len;
2393 int total_len = 0;
2394 int ret;
2395
2396 path = btrfs_alloc_path();
2397 if (!path)
2398 return -ENOMEM;
2399
2400 /*
2401 * If the bottom subvolume does not exist directly under upper_limit,
2402 * construct the path in from the bottom up.
2403 */
2404 if (dirid != upper_limit.objectid) {
2405 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2406
2407 key.objectid = treeid;
2408 key.type = BTRFS_ROOT_ITEM_KEY;
2409 key.offset = (u64)-1;
2410 root = btrfs_read_fs_root_no_name(fs_info, &key);
2411 if (IS_ERR(root)) {
2412 ret = PTR_ERR(root);
2413 goto out;
2414 }
2415
2416 key.objectid = dirid;
2417 key.type = BTRFS_INODE_REF_KEY;
2418 key.offset = (u64)-1;
2419 while (1) {
2420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2421 if (ret < 0) {
2422 goto out;
2423 } else if (ret > 0) {
2424 ret = btrfs_previous_item(root, path, dirid,
2425 BTRFS_INODE_REF_KEY);
2426 if (ret < 0) {
2427 goto out;
2428 } else if (ret > 0) {
2429 ret = -ENOENT;
2430 goto out;
2431 }
2432 }
2433
2434 leaf = path->nodes[0];
2435 slot = path->slots[0];
2436 btrfs_item_key_to_cpu(leaf, &key, slot);
2437
2438 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2439 len = btrfs_inode_ref_name_len(leaf, iref);
2440 ptr -= len + 1;
2441 total_len += len + 1;
2442 if (ptr < args->path) {
2443 ret = -ENAMETOOLONG;
2444 goto out;
2445 }
2446
2447 *(ptr + len) = '/';
2448 read_extent_buffer(leaf, ptr,
2449 (unsigned long)(iref + 1), len);
2450
2451 /* Check the read+exec permission of this directory */
2452 ret = btrfs_previous_item(root, path, dirid,
2453 BTRFS_INODE_ITEM_KEY);
2454 if (ret < 0) {
2455 goto out;
2456 } else if (ret > 0) {
2457 ret = -ENOENT;
2458 goto out;
2459 }
2460
2461 leaf = path->nodes[0];
2462 slot = path->slots[0];
2463 btrfs_item_key_to_cpu(leaf, &key2, slot);
2464 if (key2.objectid != dirid) {
2465 ret = -ENOENT;
2466 goto out;
2467 }
2468
2469 temp_inode = btrfs_iget(sb, &key2, root, NULL);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002470 if (IS_ERR(temp_inode)) {
2471 ret = PTR_ERR(temp_inode);
2472 goto out;
2473 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002474 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2475 iput(temp_inode);
2476 if (ret) {
2477 ret = -EACCES;
2478 goto out;
2479 }
2480
2481 if (key.offset == upper_limit.objectid)
2482 break;
2483 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2484 ret = -EACCES;
2485 goto out;
2486 }
2487
2488 btrfs_release_path(path);
2489 key.objectid = key.offset;
2490 key.offset = (u64)-1;
2491 dirid = key.objectid;
2492 }
2493
2494 memmove(args->path, ptr, total_len);
2495 args->path[total_len] = '\0';
2496 btrfs_release_path(path);
2497 }
2498
2499 /* Get the bottom subvolume's name from ROOT_REF */
2500 root = fs_info->tree_root;
2501 key.objectid = treeid;
2502 key.type = BTRFS_ROOT_REF_KEY;
2503 key.offset = args->treeid;
2504 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2505 if (ret < 0) {
2506 goto out;
2507 } else if (ret > 0) {
2508 ret = -ENOENT;
2509 goto out;
2510 }
2511
2512 leaf = path->nodes[0];
2513 slot = path->slots[0];
2514 btrfs_item_key_to_cpu(leaf, &key, slot);
2515
2516 item_off = btrfs_item_ptr_offset(leaf, slot);
2517 item_len = btrfs_item_size_nr(leaf, slot);
2518 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2519 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2520 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2521 ret = -EINVAL;
2522 goto out;
2523 }
2524
2525 /* Copy subvolume's name */
2526 item_off += sizeof(struct btrfs_root_ref);
2527 item_len -= sizeof(struct btrfs_root_ref);
2528 read_extent_buffer(leaf, args->name, item_off, item_len);
2529 args->name[item_len] = 0;
2530
2531out:
2532 btrfs_free_path(path);
2533 return ret;
2534}
2535
Chris Masonac8e9812010-02-28 15:39:26 -05002536static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2537 void __user *argp)
2538{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002539 struct btrfs_ioctl_ino_lookup_args *args;
2540 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002541 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002542
Julia Lawall2354d08f2010-10-29 15:14:18 -04002543 args = memdup_user(argp, sizeof(*args));
2544 if (IS_ERR(args))
2545 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002546
Al Viro496ad9a2013-01-23 17:07:38 -05002547 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002548
David Sterba01b810b2015-05-12 19:14:49 +02002549 /*
2550 * Unprivileged query to obtain the containing subvolume root id. The
2551 * path is reset so it's consistent with btrfs_search_path_in_tree.
2552 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002553 if (args->treeid == 0)
2554 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2555
David Sterba01b810b2015-05-12 19:14:49 +02002556 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2557 args->name[0] = 0;
2558 goto out;
2559 }
2560
2561 if (!capable(CAP_SYS_ADMIN)) {
2562 ret = -EPERM;
2563 goto out;
2564 }
2565
Chris Masonac8e9812010-02-28 15:39:26 -05002566 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2567 args->treeid, args->objectid,
2568 args->name);
2569
David Sterba01b810b2015-05-12 19:14:49 +02002570out:
Chris Masonac8e9812010-02-28 15:39:26 -05002571 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2572 ret = -EFAULT;
2573
2574 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002575 return ret;
2576}
2577
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002578/*
2579 * Version of ino_lookup ioctl (unprivileged)
2580 *
2581 * The main differences from ino_lookup ioctl are:
2582 *
2583 * 1. Read + Exec permission will be checked using inode_permission() during
2584 * path construction. -EACCES will be returned in case of failure.
2585 * 2. Path construction will be stopped at the inode number which corresponds
2586 * to the fd with which this ioctl is called. If constructed path does not
2587 * exist under fd's inode, -EACCES will be returned.
2588 * 3. The name of bottom subvolume is also searched and filled.
2589 */
2590static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2591{
2592 struct btrfs_ioctl_ino_lookup_user_args *args;
2593 struct inode *inode;
2594 int ret;
2595
2596 args = memdup_user(argp, sizeof(*args));
2597 if (IS_ERR(args))
2598 return PTR_ERR(args);
2599
2600 inode = file_inode(file);
2601
2602 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2603 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2604 /*
2605 * The subvolume does not exist under fd with which this is
2606 * called
2607 */
2608 kfree(args);
2609 return -EACCES;
2610 }
2611
2612 ret = btrfs_search_path_in_tree_user(inode, args);
2613
2614 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2615 ret = -EFAULT;
2616
2617 kfree(args);
2618 return ret;
2619}
2620
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002621/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2622static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2623{
2624 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2625 struct btrfs_fs_info *fs_info;
2626 struct btrfs_root *root;
2627 struct btrfs_path *path;
2628 struct btrfs_key key;
2629 struct btrfs_root_item *root_item;
2630 struct btrfs_root_ref *rref;
2631 struct extent_buffer *leaf;
2632 unsigned long item_off;
2633 unsigned long item_len;
2634 struct inode *inode;
2635 int slot;
2636 int ret = 0;
2637
2638 path = btrfs_alloc_path();
2639 if (!path)
2640 return -ENOMEM;
2641
2642 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2643 if (!subvol_info) {
2644 btrfs_free_path(path);
2645 return -ENOMEM;
2646 }
2647
2648 inode = file_inode(file);
2649 fs_info = BTRFS_I(inode)->root->fs_info;
2650
2651 /* Get root_item of inode's subvolume */
2652 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2653 key.type = BTRFS_ROOT_ITEM_KEY;
2654 key.offset = (u64)-1;
2655 root = btrfs_read_fs_root_no_name(fs_info, &key);
2656 if (IS_ERR(root)) {
2657 ret = PTR_ERR(root);
2658 goto out;
2659 }
2660 root_item = &root->root_item;
2661
2662 subvol_info->treeid = key.objectid;
2663
2664 subvol_info->generation = btrfs_root_generation(root_item);
2665 subvol_info->flags = btrfs_root_flags(root_item);
2666
2667 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2668 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2669 BTRFS_UUID_SIZE);
2670 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2671 BTRFS_UUID_SIZE);
2672
2673 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2674 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2675 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2676
2677 subvol_info->otransid = btrfs_root_otransid(root_item);
2678 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2679 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2680
2681 subvol_info->stransid = btrfs_root_stransid(root_item);
2682 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2683 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2684
2685 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2686 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2687 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2688
2689 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2690 /* Search root tree for ROOT_BACKREF of this subvolume */
2691 root = fs_info->tree_root;
2692
2693 key.type = BTRFS_ROOT_BACKREF_KEY;
2694 key.offset = 0;
2695 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2696 if (ret < 0) {
2697 goto out;
2698 } else if (path->slots[0] >=
2699 btrfs_header_nritems(path->nodes[0])) {
2700 ret = btrfs_next_leaf(root, path);
2701 if (ret < 0) {
2702 goto out;
2703 } else if (ret > 0) {
2704 ret = -EUCLEAN;
2705 goto out;
2706 }
2707 }
2708
2709 leaf = path->nodes[0];
2710 slot = path->slots[0];
2711 btrfs_item_key_to_cpu(leaf, &key, slot);
2712 if (key.objectid == subvol_info->treeid &&
2713 key.type == BTRFS_ROOT_BACKREF_KEY) {
2714 subvol_info->parent_id = key.offset;
2715
2716 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2717 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2718
2719 item_off = btrfs_item_ptr_offset(leaf, slot)
2720 + sizeof(struct btrfs_root_ref);
2721 item_len = btrfs_item_size_nr(leaf, slot)
2722 - sizeof(struct btrfs_root_ref);
2723 read_extent_buffer(leaf, subvol_info->name,
2724 item_off, item_len);
2725 } else {
2726 ret = -ENOENT;
2727 goto out;
2728 }
2729 }
2730
2731 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2732 ret = -EFAULT;
2733
2734out:
2735 btrfs_free_path(path);
2736 kzfree(subvol_info);
2737 return ret;
2738}
2739
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002740/*
2741 * Return ROOT_REF information of the subvolume containing this inode
2742 * except the subvolume name.
2743 */
2744static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2745{
2746 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2747 struct btrfs_root_ref *rref;
2748 struct btrfs_root *root;
2749 struct btrfs_path *path;
2750 struct btrfs_key key;
2751 struct extent_buffer *leaf;
2752 struct inode *inode;
2753 u64 objectid;
2754 int slot;
2755 int ret;
2756 u8 found;
2757
2758 path = btrfs_alloc_path();
2759 if (!path)
2760 return -ENOMEM;
2761
2762 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2763 if (IS_ERR(rootrefs)) {
2764 btrfs_free_path(path);
2765 return PTR_ERR(rootrefs);
2766 }
2767
2768 inode = file_inode(file);
2769 root = BTRFS_I(inode)->root->fs_info->tree_root;
2770 objectid = BTRFS_I(inode)->root->root_key.objectid;
2771
2772 key.objectid = objectid;
2773 key.type = BTRFS_ROOT_REF_KEY;
2774 key.offset = rootrefs->min_treeid;
2775 found = 0;
2776
2777 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2778 if (ret < 0) {
2779 goto out;
2780 } else if (path->slots[0] >=
2781 btrfs_header_nritems(path->nodes[0])) {
2782 ret = btrfs_next_leaf(root, path);
2783 if (ret < 0) {
2784 goto out;
2785 } else if (ret > 0) {
2786 ret = -EUCLEAN;
2787 goto out;
2788 }
2789 }
2790 while (1) {
2791 leaf = path->nodes[0];
2792 slot = path->slots[0];
2793
2794 btrfs_item_key_to_cpu(leaf, &key, slot);
2795 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2796 ret = 0;
2797 goto out;
2798 }
2799
2800 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2801 ret = -EOVERFLOW;
2802 goto out;
2803 }
2804
2805 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2806 rootrefs->rootref[found].treeid = key.offset;
2807 rootrefs->rootref[found].dirid =
2808 btrfs_root_ref_dirid(leaf, rref);
2809 found++;
2810
2811 ret = btrfs_next_item(root, path);
2812 if (ret < 0) {
2813 goto out;
2814 } else if (ret > 0) {
2815 ret = -EUCLEAN;
2816 goto out;
2817 }
2818 }
2819
2820out:
2821 if (!ret || ret == -EOVERFLOW) {
2822 rootrefs->num_items = found;
2823 /* update min_treeid for next search */
2824 if (found)
2825 rootrefs->min_treeid =
2826 rootrefs->rootref[found - 1].treeid + 1;
2827 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2828 ret = -EFAULT;
2829 }
2830
2831 kfree(rootrefs);
2832 btrfs_free_path(path);
2833
2834 return ret;
2835}
2836
Yan, Zheng76dda932009-09-21 16:00:26 -04002837static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2838 void __user *arg)
2839{
Al Viro54563d42013-09-01 15:57:51 -04002840 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002841 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002842 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002843 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002844 struct inode *inode;
2845 struct btrfs_root *root = BTRFS_I(dir)->root;
2846 struct btrfs_root *dest = NULL;
2847 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002848 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002849 int err = 0;
2850
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002851 if (!S_ISDIR(dir->i_mode))
2852 return -ENOTDIR;
2853
Yan, Zheng76dda932009-09-21 16:00:26 -04002854 vol_args = memdup_user(arg, sizeof(*vol_args));
2855 if (IS_ERR(vol_args))
2856 return PTR_ERR(vol_args);
2857
2858 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2859 namelen = strlen(vol_args->name);
2860 if (strchr(vol_args->name, '/') ||
2861 strncmp(vol_args->name, "..", namelen) == 0) {
2862 err = -EINVAL;
2863 goto out;
2864 }
2865
Al Viroa561be72011-11-23 11:57:51 -05002866 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002867 if (err)
2868 goto out;
2869
David Sterba521e0542014-04-15 16:41:44 +02002870
Al Viro00235412016-05-26 00:05:12 -04002871 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2872 if (err == -EINTR)
2873 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002874 dentry = lookup_one_len(vol_args->name, parent, namelen);
2875 if (IS_ERR(dentry)) {
2876 err = PTR_ERR(dentry);
2877 goto out_unlock_dir;
2878 }
2879
David Howells2b0143b2015-03-17 22:25:59 +00002880 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002881 err = -ENOENT;
2882 goto out_dput;
2883 }
2884
David Howells2b0143b2015-03-17 22:25:59 +00002885 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002886 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302887 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002888 /*
2889 * Regular user. Only allow this with a special mount
2890 * option, when the user has write+exec access to the
2891 * subvol root, and when rmdir(2) would have been
2892 * allowed.
2893 *
2894 * Note that this is _not_ check that the subvol is
2895 * empty or doesn't contain data that we wouldn't
2896 * otherwise be able to delete.
2897 *
2898 * Users who want to delete empty subvols should try
2899 * rmdir(2).
2900 */
2901 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002902 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002903 goto out_dput;
2904
2905 /*
2906 * Do not allow deletion if the parent dir is the same
2907 * as the dir to be deleted. That means the ioctl
2908 * must be called on the dentry referencing the root
2909 * of the subvol, not a random directory contained
2910 * within it.
2911 */
2912 err = -EINVAL;
2913 if (root == dest)
2914 goto out_dput;
2915
2916 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2917 if (err)
2918 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002919 }
2920
Miao Xie5c39da52012-10-22 11:39:53 +00002921 /* check if subvolume may be deleted by a user */
2922 err = btrfs_may_delete(dir, dentry, 1);
2923 if (err)
2924 goto out_dput;
2925
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002926 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002927 err = -EINVAL;
2928 goto out_dput;
2929 }
2930
Al Viro59551022016-01-22 15:40:57 -05002931 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002932 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002933 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002934 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002935 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002936
Yan, Zheng76dda932009-09-21 16:00:26 -04002937out_dput:
2938 dput(dentry);
2939out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002940 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002941out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002942 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002943out:
2944 kfree(vol_args);
2945 return err;
2946}
2947
Chris Mason1e701a32010-03-11 09:42:04 -05002948static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002949{
Al Viro496ad9a2013-01-23 17:07:38 -05002950 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002951 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002952 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002953 int ret;
2954
Ilya Dryomov25122d12013-01-20 15:57:57 +02002955 ret = mnt_want_write_file(file);
2956 if (ret)
2957 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002958
Ilya Dryomov25122d12013-01-20 15:57:57 +02002959 if (btrfs_root_readonly(root)) {
2960 ret = -EROFS;
2961 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002962 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002963
2964 switch (inode->i_mode & S_IFMT) {
2965 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002966 if (!capable(CAP_SYS_ADMIN)) {
2967 ret = -EPERM;
2968 goto out;
2969 }
Eric Sandeende78b512013-01-31 18:21:12 +00002970 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002971 break;
2972 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02002973 /*
2974 * Note that this does not check the file descriptor for write
2975 * access. This prevents defragmenting executables that are
2976 * running and allows defrag on files open in read-only mode.
2977 */
2978 if (!capable(CAP_SYS_ADMIN) &&
2979 inode_permission(inode, MAY_WRITE)) {
2980 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05002981 goto out;
2982 }
Chris Mason1e701a32010-03-11 09:42:04 -05002983
2984 range = kzalloc(sizeof(*range), GFP_KERNEL);
2985 if (!range) {
2986 ret = -ENOMEM;
2987 goto out;
2988 }
2989
2990 if (argp) {
2991 if (copy_from_user(range, argp,
2992 sizeof(*range))) {
2993 ret = -EFAULT;
2994 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002995 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002996 }
2997 /* compression requires us to start the IO */
2998 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2999 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3000 range->extent_thresh = (u32)-1;
3001 }
3002 } else {
3003 /* the rest are all set to zero by kzalloc */
3004 range->len = (u64)-1;
3005 }
Al Viro496ad9a2013-01-23 17:07:38 -05003006 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08003007 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003008 if (ret > 0)
3009 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003010 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003011 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003012 default:
3013 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003014 }
Chris Masone441d542009-01-05 16:57:23 -05003015out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003016 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003017 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003018}
3019
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003020static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003021{
3022 struct btrfs_ioctl_vol_args *vol_args;
3023 int ret;
3024
Chris Masone441d542009-01-05 16:57:23 -05003025 if (!capable(CAP_SYS_ADMIN))
3026 return -EPERM;
3027
David Sterba171938e2017-03-28 14:44:21 +02003028 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003029 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003030
Li Zefandae7b662009-04-08 15:06:54 +08003031 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003032 if (IS_ERR(vol_args)) {
3033 ret = PTR_ERR(vol_args);
3034 goto out;
3035 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003036
Mark Fasheh5516e592008-07-24 12:20:14 -04003037 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003038 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003039
Anand Jain43d20762014-07-01 00:58:56 +08003040 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003041 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003042
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003043 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003044out:
David Sterba171938e2017-03-28 14:44:21 +02003045 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003046 return ret;
3047}
3048
Anand Jain6b526ed2016-02-13 10:01:39 +08003049static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003050{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003051 struct inode *inode = file_inode(file);
3052 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003053 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003054 int ret;
3055
Chris Masone441d542009-01-05 16:57:23 -05003056 if (!capable(CAP_SYS_ADMIN))
3057 return -EPERM;
3058
Miao Xieda249272012-11-26 08:44:50 +00003059 ret = mnt_want_write_file(file);
3060 if (ret)
3061 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003062
Li Zefandae7b662009-04-08 15:06:54 +08003063 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003064 if (IS_ERR(vol_args)) {
3065 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003066 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003067 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003068
Anand Jain6b526ed2016-02-13 10:01:39 +08003069 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003070 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3071 ret = -EOPNOTSUPP;
3072 goto out;
3073 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003074
David Sterba171938e2017-03-28 14:44:21 +02003075 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003076 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3077 goto out;
3078 }
3079
David Sterba735654e2016-02-15 18:15:21 +01003080 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003081 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003082 } else {
3083 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003084 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003085 }
David Sterba171938e2017-03-28 14:44:21 +02003086 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003087
Anand Jain6b526ed2016-02-13 10:01:39 +08003088 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003089 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003090 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003091 vol_args->devid);
3092 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003093 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003094 vol_args->name);
3095 }
Anand Jain183860f2013-05-17 10:52:45 +00003096out:
3097 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003098err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003099 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003100 return ret;
3101}
3102
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003103static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3104{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003105 struct inode *inode = file_inode(file);
3106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003107 struct btrfs_ioctl_vol_args *vol_args;
3108 int ret;
3109
3110 if (!capable(CAP_SYS_ADMIN))
3111 return -EPERM;
3112
3113 ret = mnt_want_write_file(file);
3114 if (ret)
3115 return ret;
3116
David Sterba171938e2017-03-28 14:44:21 +02003117 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003118 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003119 goto out_drop_write;
3120 }
3121
3122 vol_args = memdup_user(arg, sizeof(*vol_args));
3123 if (IS_ERR(vol_args)) {
3124 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003125 goto out;
3126 }
3127
David Sterba58d7bbf2016-05-04 14:10:47 +02003128 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003129 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003130
3131 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003132 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003133 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003134out:
David Sterba171938e2017-03-28 14:44:21 +02003135 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003136out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003137 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003138
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003139 return ret;
3140}
3141
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003142static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3143 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003144{
Li Zefan027ed2f2011-06-08 08:27:56 +00003145 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003146 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003147 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003148 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003149
Li Zefan027ed2f2011-06-08 08:27:56 +00003150 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3151 if (!fi_args)
3152 return -ENOMEM;
3153
David Sterbad03262c2017-06-16 00:09:21 +02003154 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003155 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003156
David Sterbad03262c2017-06-16 00:09:21 +02003157 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003158 if (device->devid > fi_args->max_id)
3159 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003160 }
David Sterbad03262c2017-06-16 00:09:21 +02003161 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003162
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003163 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003164 fi_args->nodesize = fs_info->nodesize;
3165 fi_args->sectorsize = fs_info->sectorsize;
3166 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003167
Li Zefan027ed2f2011-06-08 08:27:56 +00003168 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3169 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003170
Li Zefan027ed2f2011-06-08 08:27:56 +00003171 kfree(fi_args);
3172 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003173}
3174
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003175static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3176 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003177{
3178 struct btrfs_ioctl_dev_info_args *di_args;
3179 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003180 int ret = 0;
3181 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003182
Jan Schmidt475f6382011-03-11 15:41:01 +01003183 di_args = memdup_user(arg, sizeof(*di_args));
3184 if (IS_ERR(di_args))
3185 return PTR_ERR(di_args);
3186
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003187 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003188 s_uuid = di_args->uuid;
3189
David Sterbac5593ca2017-06-16 00:09:21 +02003190 rcu_read_lock();
Anand Jaine4319cd2019-01-17 23:32:31 +08003191 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08003192 NULL, true);
Jan Schmidt475f6382011-03-11 15:41:01 +01003193
3194 if (!dev) {
3195 ret = -ENODEV;
3196 goto out;
3197 }
3198
3199 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003200 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3201 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003202 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003203 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003204 strncpy(di_args->path, rcu_str_deref(dev->name),
3205 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003206 di_args->path[sizeof(di_args->path) - 1] = 0;
3207 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003208 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003209 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003210
3211out:
David Sterbac5593ca2017-06-16 00:09:21 +02003212 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003213 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3214 ret = -EFAULT;
3215
3216 kfree(di_args);
3217 return ret;
3218}
3219
Filipe Mananad8b55242019-01-08 11:43:07 +00003220static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3221 struct inode *inode2, u64 loff2, u64 len)
3222{
3223 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3224 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3225}
3226
3227static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3228 struct inode *inode2, u64 loff2, u64 len)
3229{
3230 if (inode1 < inode2) {
3231 swap(inode1, inode2);
3232 swap(loff1, loff2);
3233 } else if (inode1 == inode2 && loff2 < loff1) {
3234 swap(loff1, loff2);
3235 }
3236 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3237 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3238}
3239
Filipe Manana57a50e22018-12-12 18:05:59 +00003240static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
Filipe Manana34a28e32018-12-07 15:25:38 +00003241 struct inode *dst, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003242{
3243 int ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003244
Filipe Manana34a28e32018-12-07 15:25:38 +00003245 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003246 * Lock destination range to serialize with concurrent readpages() and
3247 * source range to serialize with relocation.
Filipe Manana34a28e32018-12-07 15:25:38 +00003248 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003249 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Filipe Manana57a50e22018-12-12 18:05:59 +00003250 ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
Filipe Mananad8b55242019-01-08 11:43:07 +00003251 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Timofey Titovets39739092018-05-02 08:15:36 +03003252
3253 return ret;
3254}
3255
Timofey Titovetsb6728762018-05-02 08:15:37 +03003256#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3257
Timofey Titovets39739092018-05-02 08:15:36 +03003258static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3259 struct inode *dst, u64 dst_loff)
3260{
3261 int ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003262 u64 i, tail_len, chunk_count;
Timofey Titovets39739092018-05-02 08:15:36 +03003263
Timofey Titovetsb6728762018-05-02 08:15:37 +03003264 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3265 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003266
Timofey Titovetsb6728762018-05-02 08:15:37 +03003267 for (i = 0; i < chunk_count; i++) {
3268 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Filipe Manana34a28e32018-12-07 15:25:38 +00003269 dst, dst_loff);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003270 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003271 return ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003272
3273 loff += BTRFS_MAX_DEDUPE_LEN;
3274 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3275 }
3276
3277 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003278 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
Filipe Manana34a28e32018-12-07 15:25:38 +00003279 dst_loff);
Mark Fasheh416161d2013-08-06 11:42:51 -07003280
3281 return ret;
3282}
3283
Filipe Mananaf82a9902014-06-01 01:50:28 +01003284static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3285 struct inode *inode,
3286 u64 endoff,
3287 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003288 const u64 olen,
3289 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003290{
3291 struct btrfs_root *root = BTRFS_I(inode)->root;
3292 int ret;
3293
3294 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003295 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003296 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003297 /*
3298 * We round up to the block size at eof when determining which
3299 * extents to clone above, but shouldn't round up the file size.
3300 */
3301 if (endoff > destoff + olen)
3302 endoff = destoff + olen;
3303 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003304 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003305
3306 ret = btrfs_update_inode(trans, root, inode);
3307 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003308 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003309 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003310 goto out;
3311 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003312 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003313out:
3314 return ret;
3315}
3316
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003317static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003318 const struct btrfs_trans_handle *trans,
3319 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003320 const u64 hole_offset,
3321 const u64 hole_len)
3322{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003323 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003324 struct extent_map *em;
3325 int ret;
3326
3327 em = alloc_extent_map();
3328 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003329 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003330 return;
3331 }
3332
Filipe Manana14f59792014-06-29 21:45:40 +01003333 if (path) {
3334 struct btrfs_file_extent_item *fi;
3335
3336 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3337 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003338 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003339 em->generation = -1;
3340 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3341 BTRFS_FILE_EXTENT_INLINE)
3342 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003343 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003344 } else {
3345 em->start = hole_offset;
3346 em->len = hole_len;
3347 em->ram_bytes = em->len;
3348 em->orig_start = hole_offset;
3349 em->block_start = EXTENT_MAP_HOLE;
3350 em->block_len = 0;
3351 em->orig_block_len = 0;
3352 em->compress_type = BTRFS_COMPRESS_NONE;
3353 em->generation = trans->transid;
3354 }
3355
3356 while (1) {
3357 write_lock(&em_tree->lock);
3358 ret = add_extent_mapping(em_tree, em, 1);
3359 write_unlock(&em_tree->lock);
3360 if (ret != -EEXIST) {
3361 free_extent_map(em);
3362 break;
3363 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003364 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003365 em->start + em->len - 1, 0);
3366 }
3367
David Sterbaee39b432014-09-30 01:33:33 +02003368 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003369 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003370}
3371
Filipe Manana8039d872015-10-13 15:15:00 +01003372/*
3373 * Make sure we do not end up inserting an inline extent into a file that has
3374 * already other (non-inline) extents. If a file has an inline extent it can
3375 * not have any other extents and the (single) inline extent must start at the
3376 * file offset 0. Failing to respect these rules will lead to file corruption,
3377 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3378 *
3379 * We can have extents that have been already written to disk or we can have
3380 * dirty ranges still in delalloc, in which case the extent maps and items are
3381 * created only when we run delalloc, and the delalloc ranges might fall outside
3382 * the range we are currently locking in the inode's io tree. So we check the
3383 * inode's i_size because of that (i_size updates are done while holding the
3384 * i_mutex, which we are holding here).
3385 * We also check to see if the inode has a size not greater than "datal" but has
3386 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3387 * protected against such concurrent fallocate calls by the i_mutex).
3388 *
3389 * If the file has no extents but a size greater than datal, do not allow the
3390 * copy because we would need turn the inline extent into a non-inline one (even
3391 * with NO_HOLES enabled). If we find our destination inode only has one inline
3392 * extent, just overwrite it with the source inline extent if its size is less
3393 * than the source extent's size, or we could copy the source inline extent's
3394 * data into the destination inode's inline extent if the later is greater then
3395 * the former.
3396 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003397static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003398 struct btrfs_trans_handle *trans,
3399 struct btrfs_path *path,
3400 struct btrfs_key *new_key,
3401 const u64 drop_start,
3402 const u64 datal,
3403 const u64 skip,
3404 const u64 size,
3405 char *inline_data)
3406{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003407 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003408 struct btrfs_root *root = BTRFS_I(dst)->root;
3409 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003410 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003411 int ret;
3412 struct btrfs_key key;
3413
3414 if (new_key->offset > 0)
3415 return -EOPNOTSUPP;
3416
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003417 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003418 key.type = BTRFS_EXTENT_DATA_KEY;
3419 key.offset = 0;
3420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3421 if (ret < 0) {
3422 return ret;
3423 } else if (ret > 0) {
3424 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3425 ret = btrfs_next_leaf(root, path);
3426 if (ret < 0)
3427 return ret;
3428 else if (ret > 0)
3429 goto copy_inline_extent;
3430 }
3431 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003432 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003433 key.type == BTRFS_EXTENT_DATA_KEY) {
3434 ASSERT(key.offset > 0);
3435 return -EOPNOTSUPP;
3436 }
3437 } else if (i_size_read(dst) <= datal) {
3438 struct btrfs_file_extent_item *ei;
3439 u64 ext_len;
3440
3441 /*
3442 * If the file size is <= datal, make sure there are no other
3443 * extents following (can happen do to an fallocate call with
3444 * the flag FALLOC_FL_KEEP_SIZE).
3445 */
3446 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3447 struct btrfs_file_extent_item);
3448 /*
3449 * If it's an inline extent, it can not have other extents
3450 * following it.
3451 */
3452 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3453 BTRFS_FILE_EXTENT_INLINE)
3454 goto copy_inline_extent;
3455
3456 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3457 if (ext_len > aligned_end)
3458 return -EOPNOTSUPP;
3459
3460 ret = btrfs_next_item(root, path);
3461 if (ret < 0) {
3462 return ret;
3463 } else if (ret == 0) {
3464 btrfs_item_key_to_cpu(path->nodes[0], &key,
3465 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003466 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003467 key.type == BTRFS_EXTENT_DATA_KEY)
3468 return -EOPNOTSUPP;
3469 }
3470 }
3471
3472copy_inline_extent:
3473 /*
3474 * We have no extent items, or we have an extent at offset 0 which may
3475 * or may not be inlined. All these cases are dealt the same way.
3476 */
3477 if (i_size_read(dst) > datal) {
3478 /*
3479 * If the destination inode has an inline extent...
3480 * This would require copying the data from the source inline
3481 * extent into the beginning of the destination's inline extent.
3482 * But this is really complex, both extents can be compressed
3483 * or just one of them, which would require decompressing and
3484 * re-compressing data (which could increase the new compressed
3485 * size, not allowing the compressed data to fit anymore in an
3486 * inline extent).
3487 * So just don't support this case for now (it should be rare,
3488 * we are not really saving space when cloning inline extents).
3489 */
3490 return -EOPNOTSUPP;
3491 }
3492
3493 btrfs_release_path(path);
3494 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3495 if (ret)
3496 return ret;
3497 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3498 if (ret)
3499 return ret;
3500
3501 if (skip) {
3502 const u32 start = btrfs_file_extent_calc_inline_size(0);
3503
3504 memmove(inline_data + start, inline_data + start + skip, datal);
3505 }
3506
3507 write_extent_buffer(path->nodes[0], inline_data,
3508 btrfs_item_ptr_offset(path->nodes[0],
3509 path->slots[0]),
3510 size);
3511 inode_add_bytes(dst, datal);
3512
3513 return 0;
3514}
3515
Mark Fasheh32b7c682013-08-06 11:42:49 -07003516/**
3517 * btrfs_clone() - clone a range from inode file to another
3518 *
3519 * @src: Inode to clone from
3520 * @inode: Inode to clone to
3521 * @off: Offset within source to start clone from
3522 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003523 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003524 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003525 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003526 */
3527static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003528 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003529 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003530{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003531 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003532 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003533 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003534 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003535 struct btrfs_trans_handle *trans;
3536 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003537 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003538 u32 nritems;
3539 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003540 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003541 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003542 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003543
3544 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003545 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3546 if (!buf)
3547 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003548
3549 path = btrfs_alloc_path();
3550 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003551 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003552 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003553 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003554
David Sterbae4058b52015-11-27 16:31:35 +01003555 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003556 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003557 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003558 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003559 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003560
3561 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003562 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003563
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003564 /*
3565 * note the key will change type as we walk through the
3566 * tree.
3567 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003568 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003569 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3570 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003571 if (ret < 0)
3572 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003573 /*
3574 * First search, if no extent item that starts at offset off was
3575 * found but the previous item is an extent item, it's possible
3576 * it might overlap our target range, therefore process it.
3577 */
3578 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3579 btrfs_item_key_to_cpu(path->nodes[0], &key,
3580 path->slots[0] - 1);
3581 if (key.type == BTRFS_EXTENT_DATA_KEY)
3582 path->slots[0]--;
3583 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003584
Yan Zhengae01a0a2008-08-04 23:23:47 -04003585 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003586process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003587 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003588 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003589 if (ret < 0)
3590 goto out;
3591 if (ret > 0)
3592 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003593 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003594 }
3595 leaf = path->nodes[0];
3596 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003597
Yan Zhengae01a0a2008-08-04 23:23:47 -04003598 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003599 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003600 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003601 break;
3602
David Sterba962a2982014-06-04 18:41:45 +02003603 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003604 struct btrfs_file_extent_item *extent;
3605 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003606 u32 size;
3607 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003608 u64 disko = 0, diskl = 0;
3609 u64 datao = 0, datal = 0;
3610 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003611 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003612
Sage Weilc5c9cd42008-11-12 14:32:25 -05003613 extent = btrfs_item_ptr(leaf, slot,
3614 struct btrfs_file_extent_item);
3615 comp = btrfs_file_extent_compression(leaf, extent);
3616 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003617 if (type == BTRFS_FILE_EXTENT_REG ||
3618 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003619 disko = btrfs_file_extent_disk_bytenr(leaf,
3620 extent);
3621 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3622 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003623 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003624 datal = btrfs_file_extent_num_bytes(leaf,
3625 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003626 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3627 /* take upper bound, may be compressed */
3628 datal = btrfs_file_extent_ram_bytes(leaf,
3629 extent);
3630 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003631
Filipe Manana2c463822014-05-31 02:31:05 +01003632 /*
3633 * The first search might have left us at an extent
3634 * item that ends before our target range's start, can
3635 * happen if we have holes and NO_HOLES feature enabled.
3636 */
3637 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003638 path->slots[0]++;
3639 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003640 } else if (key.offset >= off + len) {
3641 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003642 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003643 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003644 size = btrfs_item_size_nr(leaf, slot);
3645 read_extent_buffer(leaf, buf,
3646 btrfs_item_ptr_offset(leaf, slot),
3647 size);
3648
3649 btrfs_release_path(path);
3650 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003651
Zheng Yan31840ae2008-09-23 13:14:14 -04003652 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003653 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003654 if (off <= key.offset)
3655 new_key.offset = key.offset + destoff - off;
3656 else
3657 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003658
Sage Weilb6f34092011-09-20 14:48:51 -04003659 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003660 * Deal with a hole that doesn't have an extent item
3661 * that represents it (NO_HOLES feature enabled).
3662 * This hole is either in the middle of the cloning
3663 * range or at the beginning (fully overlaps it or
3664 * partially overlaps it).
3665 */
3666 if (new_key.offset != last_dest_end)
3667 drop_start = last_dest_end;
3668 else
3669 drop_start = new_key.offset;
3670
3671 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003672 * 1 - adjusting old extent (we may have to split it)
3673 * 1 - add new extent
3674 * 1 - inode update
3675 */
3676 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003677 if (IS_ERR(trans)) {
3678 ret = PTR_ERR(trans);
3679 goto out;
3680 }
3681
Chris Masonc8a894d2009-06-27 21:07:03 -04003682 if (type == BTRFS_FILE_EXTENT_REG ||
3683 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003684 /*
3685 * a | --- range to clone ---| b
3686 * | ------------- extent ------------- |
3687 */
3688
Antonio Ospite93915582014-06-04 14:03:48 +02003689 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003690 if (key.offset + datal > off + len)
3691 datal = off + len - key.offset;
3692
Antonio Ospite93915582014-06-04 14:03:48 +02003693 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003694 if (off > key.offset) {
3695 datao += off - key.offset;
3696 datal -= off - key.offset;
3697 }
3698
Josef Bacik5dc562c2012-08-17 13:14:17 -04003699 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003700 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003701 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003702 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003703 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003704 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003705 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003706 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003707 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003708 goto out;
3709 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003710
Sage Weilc5c9cd42008-11-12 14:32:25 -05003711 ret = btrfs_insert_empty_item(trans, root, path,
3712 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003713 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003714 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003715 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003716 goto out;
3717 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003718
3719 leaf = path->nodes[0];
3720 slot = path->slots[0];
3721 write_extent_buffer(leaf, buf,
3722 btrfs_item_ptr_offset(leaf, slot),
3723 size);
3724
3725 extent = btrfs_item_ptr(leaf, slot,
3726 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003727
Sage Weilc5c9cd42008-11-12 14:32:25 -05003728 /* disko == 0 means it's a hole */
3729 if (!disko)
3730 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003731
3732 btrfs_set_file_extent_offset(leaf, extent,
3733 datao);
3734 btrfs_set_file_extent_num_bytes(leaf, extent,
3735 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003736
Sage Weilc5c9cd42008-11-12 14:32:25 -05003737 if (disko) {
3738 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003739 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003740 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003741 disko, diskl, 0,
3742 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003743 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003744 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003745 if (ret) {
3746 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003747 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003748 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003749 goto out;
3750
3751 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003752 }
3753 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3754 u64 skip = 0;
3755 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003756
Sage Weilc5c9cd42008-11-12 14:32:25 -05003757 if (off > key.offset) {
3758 skip = off - key.offset;
3759 new_key.offset += skip;
3760 }
Chris Masond3977122009-01-05 21:25:51 -05003761
Liu Boaa42ffd2012-09-18 03:52:23 -06003762 if (key.offset + datal > off + len)
3763 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003764
Sage Weilc5c9cd42008-11-12 14:32:25 -05003765 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003766 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003767 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003768 goto out;
3769 }
3770 size -= skip + trim;
3771 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003772
David Sterba4a0ab9d2017-02-10 20:18:49 +01003773 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003774 trans, path,
3775 &new_key,
3776 drop_start,
3777 datal,
3778 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003779 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003780 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003781 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003782 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003783 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003784 goto out;
3785 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003786 leaf = path->nodes[0];
3787 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003788 }
3789
Filipe Manana7ffbb592014-06-09 03:48:05 +01003790 /* If we have an implicit hole (NO_HOLES feature). */
3791 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003792 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003793 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003794 new_key.offset - drop_start);
3795
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003796 clone_update_extent_map(BTRFS_I(inode), trans,
3797 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003798
Sage Weilc5c9cd42008-11-12 14:32:25 -05003799 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003800 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003801
Filipe Manana62e23902014-08-08 02:47:06 +01003802 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003803 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003804 ret = clone_finish_inode_update(trans, inode,
3805 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003806 destoff, olen,
3807 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003808 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003809 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003810 if (new_key.offset + datal >= destoff + len)
3811 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003812 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003813 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003814 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003815
3816 if (fatal_signal_pending(current)) {
3817 ret = -EINTR;
3818 goto out;
3819 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003820 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003821 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003822
Filipe Mananaf82a9902014-06-01 01:50:28 +01003823 if (last_dest_end < destoff + len) {
3824 /*
3825 * We have an implicit hole (NO_HOLES feature is enabled) that
3826 * fully or partially overlaps our cloning range at its end.
3827 */
3828 btrfs_release_path(path);
3829
3830 /*
3831 * 1 - remove extent(s)
3832 * 1 - inode update
3833 */
3834 trans = btrfs_start_transaction(root, 2);
3835 if (IS_ERR(trans)) {
3836 ret = PTR_ERR(trans);
3837 goto out;
3838 }
3839 ret = btrfs_drop_extents(trans, root, inode,
3840 last_dest_end, destoff + len, 1);
3841 if (ret) {
3842 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003843 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003844 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003845 goto out;
3846 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003847 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3848 last_dest_end,
3849 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003850 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003851 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003852 }
3853
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003854out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003855 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003856 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003857 return ret;
3858}
3859
Zach Brown3db11b22015-11-10 16:53:32 -05003860static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3861 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003862{
Al Viro54563d42013-09-01 15:57:51 -04003863 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003864 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003865 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003866 int ret;
3867 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003868 u64 bs = fs_info->sb->s_blocksize;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003869
3870 /*
3871 * TODO:
3872 * - split compressed inline extents. annoying: we need to
3873 * decompress into destination's address_space (the file offset
3874 * may change, so source mapping won't do), then recompress (or
3875 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003876 *
3877 * - split destination inode's inline extents. The inline extents can
3878 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003879 */
3880
Filipe Mananaac765f82018-11-05 11:14:17 +00003881 /*
Filipe Manana34a28e32018-12-07 15:25:38 +00003882 * VFS's generic_remap_file_range_prep() protects us from cloning the
3883 * eof block into the middle of a file, which would result in corruption
3884 * if the file size is not blocksize aligned. So we don't need to check
3885 * for that case here.
Filipe Mananaac765f82018-11-05 11:14:17 +00003886 */
Filipe Manana34a28e32018-12-07 15:25:38 +00003887 if (off + len == src->i_size)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003888 len = ALIGN(src->i_size, bs) - off;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003889
3890 if (destoff > inode->i_size) {
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003891 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3892
Mark Fasheh32b7c682013-08-06 11:42:49 -07003893 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3894 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003895 return ret;
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003896 /*
3897 * We may have truncated the last block if the inode's size is
3898 * not sector size aligned, so we need to wait for writeback to
3899 * complete before proceeding further, otherwise we can race
3900 * with cloning and attempt to increment a reference to an
3901 * extent that no longer exists (writeback completed right after
3902 * we found the previous extent covering eof and before we
3903 * attempted to increment its reference count).
3904 */
3905 ret = btrfs_wait_ordered_range(inode, wb_start,
3906 destoff - wb_start);
3907 if (ret)
3908 return ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003909 }
3910
Filipe Mananac125b8b2014-05-23 05:03:34 +01003911 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003912 * Lock destination range to serialize with concurrent readpages() and
3913 * source range to serialize with relocation.
Filipe Mananac125b8b2014-05-23 05:03:34 +01003914 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003915 btrfs_double_extent_lock(src, off, inode, destoff, len);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003916 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Filipe Mananad8b55242019-01-08 11:43:07 +00003917 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003918 /*
3919 * Truncate page cache pages so that future reads will see the cloned
3920 * data immediately and not the previous data.
3921 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303922 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003923 round_down(destoff, PAGE_SIZE),
3924 round_up(destoff + len, PAGE_SIZE) - 1);
Filipe Manana34a28e32018-12-07 15:25:38 +00003925
3926 return ret;
3927}
3928
3929static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3930 struct file *file_out, loff_t pos_out,
3931 loff_t *len, unsigned int remap_flags)
3932{
3933 struct inode *inode_in = file_inode(file_in);
3934 struct inode *inode_out = file_inode(file_out);
3935 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3936 bool same_inode = inode_out == inode_in;
3937 u64 wb_len;
3938 int ret;
3939
3940 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3941 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3942
3943 if (btrfs_root_readonly(root_out))
3944 return -EROFS;
3945
3946 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3947 inode_in->i_sb != inode_out->i_sb)
3948 return -EXDEV;
3949 }
3950
3951 if (same_inode)
3952 inode_lock(inode_in);
Mark Fasheh293a8482015-06-30 14:42:06 -07003953 else
Filipe Manana4ea748e2019-02-26 12:06:09 +00003954 lock_two_nondirectories(inode_in, inode_out);
Filipe Manana34a28e32018-12-07 15:25:38 +00003955
Filipe Manana500710d2018-12-12 18:05:56 +00003956 /* don't make the dst file partly checksummed */
3957 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3958 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
3959 ret = -EINVAL;
3960 goto out_unlock;
3961 }
3962
Filipe Manana34a28e32018-12-07 15:25:38 +00003963 /*
3964 * Now that the inodes are locked, we need to start writeback ourselves
3965 * and can not rely on the writeback from the VFS's generic helper
3966 * generic_remap_file_range_prep() because:
3967 *
3968 * 1) For compression we must call filemap_fdatawrite_range() range
3969 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3970 * helper only calls it once;
3971 *
3972 * 2) filemap_fdatawrite_range(), called by the generic helper only
3973 * waits for the writeback to complete, i.e. for IO to be done, and
3974 * not for the ordered extents to complete. We need to wait for them
3975 * to complete so that new file extent items are in the fs tree.
3976 */
3977 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3978 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3979 else
3980 wb_len = ALIGN(*len, bs);
3981
3982 /*
3983 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3984 * any in progress could create its ordered extents after we wait for
3985 * existing ordered extents below).
3986 */
3987 inode_dio_wait(inode_in);
3988 if (!same_inode)
3989 inode_dio_wait(inode_out);
3990
3991 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
3992 wb_len);
3993 if (ret < 0)
3994 goto out_unlock;
3995 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
3996 wb_len);
3997 if (ret < 0)
3998 goto out_unlock;
3999
4000 ret = generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
4001 len, remap_flags);
4002 if (ret < 0 || *len == 0)
4003 goto out_unlock;
4004
4005 return 0;
4006
4007 out_unlock:
4008 if (same_inode)
4009 inode_unlock(inode_in);
4010 else
Filipe Manana4ea748e2019-02-26 12:06:09 +00004011 unlock_two_nondirectories(inode_in, inode_out);
Filipe Manana34a28e32018-12-07 15:25:38 +00004012
Zach Brown3db11b22015-11-10 16:53:32 -05004013 return ret;
4014}
4015
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004016loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4017 struct file *dst_file, loff_t destoff, loff_t len,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004018 unsigned int remap_flags)
Zach Brown3db11b22015-11-10 16:53:32 -05004019{
Filipe Manana34a28e32018-12-07 15:25:38 +00004020 struct inode *src_inode = file_inode(src_file);
4021 struct inode *dst_inode = file_inode(dst_file);
4022 bool same_inode = dst_inode == src_inode;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004023 int ret;
4024
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004025 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4026 return -EINVAL;
4027
Filipe Manana34a28e32018-12-07 15:25:38 +00004028 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4029 &len, remap_flags);
4030 if (ret < 0 || len == 0)
4031 return ret;
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004032
Filipe Manana34a28e32018-12-07 15:25:38 +00004033 if (remap_flags & REMAP_FILE_DEDUP)
4034 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4035 else
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004036 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
Filipe Manana34a28e32018-12-07 15:25:38 +00004037
4038 if (same_inode)
4039 inode_unlock(src_inode);
4040 else
Filipe Manana4ea748e2019-02-26 12:06:09 +00004041 unlock_two_nondirectories(src_inode, dst_inode);
Filipe Manana34a28e32018-12-07 15:25:38 +00004042
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004043 return ret < 0 ? ret : len;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004044}
4045
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004046static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4047{
Al Viro496ad9a2013-01-23 17:07:38 -05004048 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004049 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004050 struct btrfs_root *root = BTRFS_I(inode)->root;
4051 struct btrfs_root *new_root;
4052 struct btrfs_dir_item *di;
4053 struct btrfs_trans_handle *trans;
4054 struct btrfs_path *path;
4055 struct btrfs_key location;
4056 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004057 u64 objectid = 0;
4058 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004059 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004060
4061 if (!capable(CAP_SYS_ADMIN))
4062 return -EPERM;
4063
Miao Xie3c04ce02012-11-26 08:43:07 +00004064 ret = mnt_want_write_file(file);
4065 if (ret)
4066 return ret;
4067
4068 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4069 ret = -EFAULT;
4070 goto out;
4071 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004072
4073 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304074 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004075
4076 location.objectid = objectid;
4077 location.type = BTRFS_ROOT_ITEM_KEY;
4078 location.offset = (u64)-1;
4079
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004080 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004081 if (IS_ERR(new_root)) {
4082 ret = PTR_ERR(new_root);
4083 goto out;
4084 }
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09004085 if (!is_fstree(new_root->root_key.objectid)) {
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004086 ret = -ENOENT;
4087 goto out;
4088 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004089
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004090 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004091 if (!path) {
4092 ret = -ENOMEM;
4093 goto out;
4094 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004095 path->leave_spinning = 1;
4096
4097 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004098 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004099 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004100 ret = PTR_ERR(trans);
4101 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004102 }
4103
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004104 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4105 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004106 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004107 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004108 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004109 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004110 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004111 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004112 ret = -ENOENT;
4113 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004114 }
4115
4116 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4117 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4118 btrfs_mark_buffer_dirty(path->nodes[0]);
4119 btrfs_free_path(path);
4120
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004121 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004122 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004123out:
4124 mnt_drop_write_file(file);
4125 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004126}
4127
Su Yuec065f5b12018-04-02 17:24:11 +08004128static void get_block_group_info(struct list_head *groups_list,
4129 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004130{
4131 struct btrfs_block_group_cache *block_group;
4132
4133 space->total_bytes = 0;
4134 space->used_bytes = 0;
4135 space->flags = 0;
4136 list_for_each_entry(block_group, groups_list, list) {
4137 space->flags = block_group->flags;
4138 space->total_bytes += block_group->key.offset;
4139 space->used_bytes +=
4140 btrfs_block_group_used(&block_group->item);
4141 }
4142}
4143
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004144static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4145 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004146{
4147 struct btrfs_ioctl_space_args space_args;
4148 struct btrfs_ioctl_space_info space;
4149 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004150 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004151 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004152 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004153 static const u64 types[] = {
4154 BTRFS_BLOCK_GROUP_DATA,
4155 BTRFS_BLOCK_GROUP_SYSTEM,
4156 BTRFS_BLOCK_GROUP_METADATA,
4157 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4158 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004159 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004160 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004161 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004162 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004163 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004164
4165 if (copy_from_user(&space_args,
4166 (struct btrfs_ioctl_space_args __user *)arg,
4167 sizeof(space_args)))
4168 return -EFAULT;
4169
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004170 for (i = 0; i < num_types; i++) {
4171 struct btrfs_space_info *tmp;
4172
4173 info = NULL;
4174 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004175 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004176 list) {
4177 if (tmp->flags == types[i]) {
4178 info = tmp;
4179 break;
4180 }
4181 }
4182 rcu_read_unlock();
4183
4184 if (!info)
4185 continue;
4186
4187 down_read(&info->groups_sem);
4188 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4189 if (!list_empty(&info->block_groups[c]))
4190 slot_count++;
4191 }
4192 up_read(&info->groups_sem);
4193 }
Josef Bacik1406e432010-01-13 18:19:06 +00004194
David Sterba36523e952014-02-07 14:34:12 +01004195 /*
4196 * Global block reserve, exported as a space_info
4197 */
4198 slot_count++;
4199
Chris Mason7fde62b2010-03-16 15:40:10 -04004200 /* space_slots == 0 means they are asking for a count */
4201 if (space_args.space_slots == 0) {
4202 space_args.total_spaces = slot_count;
4203 goto out;
4204 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004205
Dan Rosenberg51788b12011-02-14 16:04:23 -05004206 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004207
Chris Mason7fde62b2010-03-16 15:40:10 -04004208 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004209
Chris Mason7fde62b2010-03-16 15:40:10 -04004210 /* we generally have at most 6 or so space infos, one for each raid
4211 * level. So, a whole page should be more than enough for everyone
4212 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004213 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004214 return -ENOMEM;
4215
4216 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004217 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004218 if (!dest)
4219 return -ENOMEM;
4220 dest_orig = dest;
4221
4222 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004223 for (i = 0; i < num_types; i++) {
4224 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004225
Dan Rosenberg51788b12011-02-14 16:04:23 -05004226 if (!slot_count)
4227 break;
4228
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004229 info = NULL;
4230 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004231 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004232 list) {
4233 if (tmp->flags == types[i]) {
4234 info = tmp;
4235 break;
4236 }
4237 }
4238 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004239
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004240 if (!info)
4241 continue;
4242 down_read(&info->groups_sem);
4243 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4244 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004245 get_block_group_info(&info->block_groups[c],
4246 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004247 memcpy(dest, &space, sizeof(space));
4248 dest++;
4249 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004250 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004251 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004252 if (!slot_count)
4253 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004254 }
4255 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004256 }
Josef Bacik1406e432010-01-13 18:19:06 +00004257
David Sterba36523e952014-02-07 14:34:12 +01004258 /*
4259 * Add global block reserve
4260 */
4261 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004262 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004263
4264 spin_lock(&block_rsv->lock);
4265 space.total_bytes = block_rsv->size;
4266 space.used_bytes = block_rsv->size - block_rsv->reserved;
4267 spin_unlock(&block_rsv->lock);
4268 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4269 memcpy(dest, &space, sizeof(space));
4270 space_args.total_spaces++;
4271 }
4272
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004273 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004274 (arg + sizeof(struct btrfs_ioctl_space_args));
4275
4276 if (copy_to_user(user_dest, dest_orig, alloc_size))
4277 ret = -EFAULT;
4278
4279 kfree(dest_orig);
4280out:
4281 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004282 ret = -EFAULT;
4283
4284 return ret;
4285}
4286
Miao Xie9a8c28b2012-11-26 08:40:43 +00004287static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4288 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004289{
Sage Weil46204592010-10-29 15:41:32 -04004290 struct btrfs_trans_handle *trans;
4291 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004292 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004293
Miao Xied4edf392013-02-20 09:17:06 +00004294 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004295 if (IS_ERR(trans)) {
4296 if (PTR_ERR(trans) != -ENOENT)
4297 return PTR_ERR(trans);
4298
4299 /* No running transaction, don't bother */
4300 transid = root->fs_info->last_trans_committed;
4301 goto out;
4302 }
Sage Weil46204592010-10-29 15:41:32 -04004303 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004304 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004305 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004306 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004307 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004308 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004309out:
Sage Weil46204592010-10-29 15:41:32 -04004310 if (argp)
4311 if (copy_to_user(argp, &transid, sizeof(transid)))
4312 return -EFAULT;
4313 return 0;
4314}
4315
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004316static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004317 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004318{
Sage Weil46204592010-10-29 15:41:32 -04004319 u64 transid;
4320
4321 if (argp) {
4322 if (copy_from_user(&transid, argp, sizeof(transid)))
4323 return -EFAULT;
4324 } else {
4325 transid = 0; /* current trans */
4326 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004327 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004328}
4329
Miao Xieb8e95482012-11-26 08:48:01 +00004330static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004331{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004332 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004333 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004334 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004335
4336 if (!capable(CAP_SYS_ADMIN))
4337 return -EPERM;
4338
4339 sa = memdup_user(arg, sizeof(*sa));
4340 if (IS_ERR(sa))
4341 return PTR_ERR(sa);
4342
Miao Xieb8e95482012-11-26 08:48:01 +00004343 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4344 ret = mnt_want_write_file(file);
4345 if (ret)
4346 goto out;
4347 }
4348
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004349 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004350 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4351 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004352
Filipe Manana06fe39a2018-12-14 19:50:17 +00004353 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004354 ret = -EFAULT;
4355
Miao Xieb8e95482012-11-26 08:48:01 +00004356 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4357 mnt_drop_write_file(file);
4358out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004359 kfree(sa);
4360 return ret;
4361}
4362
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004363static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004364{
4365 if (!capable(CAP_SYS_ADMIN))
4366 return -EPERM;
4367
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004368 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004369}
4370
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004371static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004372 void __user *arg)
4373{
4374 struct btrfs_ioctl_scrub_args *sa;
4375 int ret;
4376
4377 if (!capable(CAP_SYS_ADMIN))
4378 return -EPERM;
4379
4380 sa = memdup_user(arg, sizeof(*sa));
4381 if (IS_ERR(sa))
4382 return PTR_ERR(sa);
4383
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004384 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004385
Filipe Manana4fa99b02018-12-14 19:45:13 +00004386 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004387 ret = -EFAULT;
4388
4389 kfree(sa);
4390 return ret;
4391}
4392
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004393static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004394 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004395{
4396 struct btrfs_ioctl_get_dev_stats *sa;
4397 int ret;
4398
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004399 sa = memdup_user(arg, sizeof(*sa));
4400 if (IS_ERR(sa))
4401 return PTR_ERR(sa);
4402
David Sterbab27f7c02012-06-22 06:30:39 -06004403 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4404 kfree(sa);
4405 return -EPERM;
4406 }
4407
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004408 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004409
Filipe Mananaeee99572018-12-14 19:45:22 +00004410 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004411 ret = -EFAULT;
4412
4413 kfree(sa);
4414 return ret;
4415}
4416
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004417static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4418 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004419{
4420 struct btrfs_ioctl_dev_replace_args *p;
4421 int ret;
4422
4423 if (!capable(CAP_SYS_ADMIN))
4424 return -EPERM;
4425
4426 p = memdup_user(arg, sizeof(*p));
4427 if (IS_ERR(p))
4428 return PTR_ERR(p);
4429
4430 switch (p->cmd) {
4431 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004432 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004433 ret = -EROFS;
4434 goto out;
4435 }
David Sterba171938e2017-03-28 14:44:21 +02004436 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004437 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004438 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004439 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004440 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004441 }
4442 break;
4443 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004444 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004445 ret = 0;
4446 break;
4447 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004448 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004449 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004450 break;
4451 default:
4452 ret = -EINVAL;
4453 break;
4454 }
4455
Filipe Mananad3a53282019-01-08 11:42:09 +00004456 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004457 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004458out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004459 kfree(p);
4460 return ret;
4461}
4462
Jan Schmidtd7728c92011-07-07 16:48:38 +02004463static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4464{
4465 int ret = 0;
4466 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004467 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004468 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004469 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004470 struct inode_fs_paths *ipath = NULL;
4471 struct btrfs_path *path;
4472
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004473 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004474 return -EPERM;
4475
4476 path = btrfs_alloc_path();
4477 if (!path) {
4478 ret = -ENOMEM;
4479 goto out;
4480 }
4481
4482 ipa = memdup_user(arg, sizeof(*ipa));
4483 if (IS_ERR(ipa)) {
4484 ret = PTR_ERR(ipa);
4485 ipa = NULL;
4486 goto out;
4487 }
4488
4489 size = min_t(u32, ipa->size, 4096);
4490 ipath = init_ipath(size, root, path);
4491 if (IS_ERR(ipath)) {
4492 ret = PTR_ERR(ipath);
4493 ipath = NULL;
4494 goto out;
4495 }
4496
4497 ret = paths_from_inode(ipa->inum, ipath);
4498 if (ret < 0)
4499 goto out;
4500
4501 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004502 rel_ptr = ipath->fspath->val[i] -
4503 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004504 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004505 }
4506
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004507 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4508 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004509 if (ret) {
4510 ret = -EFAULT;
4511 goto out;
4512 }
4513
4514out:
4515 btrfs_free_path(path);
4516 free_ipath(ipath);
4517 kfree(ipa);
4518
4519 return ret;
4520}
4521
4522static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4523{
4524 struct btrfs_data_container *inodes = ctx;
4525 const size_t c = 3 * sizeof(u64);
4526
4527 if (inodes->bytes_left >= c) {
4528 inodes->bytes_left -= c;
4529 inodes->val[inodes->elem_cnt] = inum;
4530 inodes->val[inodes->elem_cnt + 1] = offset;
4531 inodes->val[inodes->elem_cnt + 2] = root;
4532 inodes->elem_cnt += 3;
4533 } else {
4534 inodes->bytes_missing += c - inodes->bytes_left;
4535 inodes->bytes_left = 0;
4536 inodes->elem_missed += 3;
4537 }
4538
4539 return 0;
4540}
4541
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004542static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004543 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004544{
4545 int ret = 0;
4546 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004547 struct btrfs_ioctl_logical_ino_args *loi;
4548 struct btrfs_data_container *inodes = NULL;
4549 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004550 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004551
4552 if (!capable(CAP_SYS_ADMIN))
4553 return -EPERM;
4554
4555 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304556 if (IS_ERR(loi))
4557 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004558
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004559 if (version == 1) {
4560 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004561 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004562 } else {
4563 /* All reserved bits must be 0 for now */
4564 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4565 ret = -EINVAL;
4566 goto out_loi;
4567 }
4568 /* Only accept flags we have defined so far */
4569 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4570 ret = -EINVAL;
4571 goto out_loi;
4572 }
4573 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004574 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004575 }
4576
Jan Schmidtd7728c92011-07-07 16:48:38 +02004577 path = btrfs_alloc_path();
4578 if (!path) {
4579 ret = -ENOMEM;
4580 goto out;
4581 }
4582
Jan Schmidtd7728c92011-07-07 16:48:38 +02004583 inodes = init_data_container(size);
4584 if (IS_ERR(inodes)) {
4585 ret = PTR_ERR(inodes);
4586 inodes = NULL;
4587 goto out;
4588 }
4589
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004590 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004591 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004592 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004593 ret = -ENOENT;
4594 if (ret < 0)
4595 goto out;
4596
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004597 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4598 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004599 if (ret)
4600 ret = -EFAULT;
4601
4602out:
4603 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004604 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004605out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004606 kfree(loi);
4607
4608 return ret;
4609}
4610
David Sterba008ef092018-03-21 02:05:27 +01004611void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004612 struct btrfs_ioctl_balance_args *bargs)
4613{
4614 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4615
4616 bargs->flags = bctl->flags;
4617
David Sterba3009a622018-03-21 01:31:04 +01004618 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004619 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4620 if (atomic_read(&fs_info->balance_pause_req))
4621 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004622 if (atomic_read(&fs_info->balance_cancel_req))
4623 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004624
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004625 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4626 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4627 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004628
David Sterba008ef092018-03-21 02:05:27 +01004629 spin_lock(&fs_info->balance_lock);
4630 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4631 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004632}
4633
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004634static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004635{
Al Viro496ad9a2013-01-23 17:07:38 -05004636 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004637 struct btrfs_fs_info *fs_info = root->fs_info;
4638 struct btrfs_ioctl_balance_args *bargs;
4639 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004640 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004641 int ret;
4642
4643 if (!capable(CAP_SYS_ADMIN))
4644 return -EPERM;
4645
Liu Boe54bfa312012-06-29 03:58:48 -06004646 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004647 if (ret)
4648 return ret;
4649
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004650again:
David Sterba171938e2017-03-28 14:44:21 +02004651 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004652 mutex_lock(&fs_info->balance_mutex);
4653 need_unlock = true;
4654 goto locked;
4655 }
4656
4657 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004658 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004659 * (1) some other op is running
4660 * (2) balance is running
4661 * (3) balance is paused -- special case (think resume)
4662 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004663 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004664 if (fs_info->balance_ctl) {
4665 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004666 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004667 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004668 /*
4669 * Lock released to allow other waiters to continue,
4670 * we'll reexamine the status again.
4671 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004672 mutex_lock(&fs_info->balance_mutex);
4673
4674 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004675 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004676 /* this is (3) */
4677 need_unlock = false;
4678 goto locked;
4679 }
4680
4681 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004682 goto again;
4683 } else {
4684 /* this is (2) */
4685 mutex_unlock(&fs_info->balance_mutex);
4686 ret = -EINPROGRESS;
4687 goto out;
4688 }
4689 } else {
4690 /* this is (1) */
4691 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004692 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004693 goto out;
4694 }
4695
4696locked:
David Sterba171938e2017-03-28 14:44:21 +02004697 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004698
4699 if (arg) {
4700 bargs = memdup_user(arg, sizeof(*bargs));
4701 if (IS_ERR(bargs)) {
4702 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004703 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004704 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004705
4706 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4707 if (!fs_info->balance_ctl) {
4708 ret = -ENOTCONN;
4709 goto out_bargs;
4710 }
4711
4712 bctl = fs_info->balance_ctl;
4713 spin_lock(&fs_info->balance_lock);
4714 bctl->flags |= BTRFS_BALANCE_RESUME;
4715 spin_unlock(&fs_info->balance_lock);
4716
4717 goto do_balance;
4718 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004719 } else {
4720 bargs = NULL;
4721 }
4722
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004723 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004724 ret = -EINPROGRESS;
4725 goto out_bargs;
4726 }
4727
David Sterba8d2db782015-11-04 15:38:29 +01004728 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004729 if (!bctl) {
4730 ret = -ENOMEM;
4731 goto out_bargs;
4732 }
4733
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004734 if (arg) {
4735 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4736 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4737 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4738
4739 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004740 } else {
4741 /* balance everything - no filters */
4742 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004743 }
4744
David Sterba8eb93452015-10-12 16:55:54 +02004745 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4746 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004747 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004748 }
4749
Ilya Dryomovde322262012-01-16 22:04:49 +02004750do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004751 /*
David Sterba149196a2018-03-20 20:23:09 +01004752 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4753 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4754 * restriper was paused all the way until unmount, in free_fs_info.
4755 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004756 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004757 need_unlock = false;
4758
David Sterba6fcf6e22018-05-07 17:44:03 +02004759 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004760 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004761
Filipe Mananad00c2d92019-01-08 11:42:01 +00004762 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004763 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4764 ret = -EFAULT;
4765 }
4766
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004767out_bctl:
4768 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004769out_bargs:
4770 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004771out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004772 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004773 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004774 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004775out:
Liu Boe54bfa312012-06-29 03:58:48 -06004776 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004777 return ret;
4778}
4779
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004780static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004781{
4782 if (!capable(CAP_SYS_ADMIN))
4783 return -EPERM;
4784
4785 switch (cmd) {
4786 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004787 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004788 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004789 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004790 }
4791
4792 return -EINVAL;
4793}
4794
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004795static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004796 void __user *arg)
4797{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004798 struct btrfs_ioctl_balance_args *bargs;
4799 int ret = 0;
4800
4801 if (!capable(CAP_SYS_ADMIN))
4802 return -EPERM;
4803
4804 mutex_lock(&fs_info->balance_mutex);
4805 if (!fs_info->balance_ctl) {
4806 ret = -ENOTCONN;
4807 goto out;
4808 }
4809
David Sterba8d2db782015-11-04 15:38:29 +01004810 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004811 if (!bargs) {
4812 ret = -ENOMEM;
4813 goto out;
4814 }
4815
David Sterba008ef092018-03-21 02:05:27 +01004816 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004817
4818 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4819 ret = -EFAULT;
4820
4821 kfree(bargs);
4822out:
4823 mutex_unlock(&fs_info->balance_mutex);
4824 return ret;
4825}
4826
Miao Xie905b0dd2012-11-26 08:50:11 +00004827static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004828{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004829 struct inode *inode = file_inode(file);
4830 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004831 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004832 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004833
4834 if (!capable(CAP_SYS_ADMIN))
4835 return -EPERM;
4836
Miao Xie905b0dd2012-11-26 08:50:11 +00004837 ret = mnt_want_write_file(file);
4838 if (ret)
4839 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004840
4841 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004842 if (IS_ERR(sa)) {
4843 ret = PTR_ERR(sa);
4844 goto drop_write;
4845 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004846
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004847 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004848
4849 switch (sa->cmd) {
4850 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004851 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004852 break;
4853 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004854 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004855 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004856 default:
4857 ret = -EINVAL;
4858 break;
4859 }
4860
Arne Jansen5d13a372011-09-14 15:53:51 +02004861 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004862 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004863drop_write:
4864 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004865 return ret;
4866}
4867
Miao Xie905b0dd2012-11-26 08:50:11 +00004868static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004869{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004870 struct inode *inode = file_inode(file);
4871 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4872 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004873 struct btrfs_ioctl_qgroup_assign_args *sa;
4874 struct btrfs_trans_handle *trans;
4875 int ret;
4876 int err;
4877
4878 if (!capable(CAP_SYS_ADMIN))
4879 return -EPERM;
4880
Miao Xie905b0dd2012-11-26 08:50:11 +00004881 ret = mnt_want_write_file(file);
4882 if (ret)
4883 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004884
4885 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004886 if (IS_ERR(sa)) {
4887 ret = PTR_ERR(sa);
4888 goto drop_write;
4889 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004890
4891 trans = btrfs_join_transaction(root);
4892 if (IS_ERR(trans)) {
4893 ret = PTR_ERR(trans);
4894 goto out;
4895 }
4896
Arne Jansen5d13a372011-09-14 15:53:51 +02004897 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004898 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004899 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004900 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004901 }
4902
Qu Wenruoe082f562015-02-27 16:24:28 +08004903 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004904 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004905 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004906 btrfs_handle_fs_error(fs_info, err,
4907 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004908 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004909 if (err && !ret)
4910 ret = err;
4911
4912out:
4913 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004914drop_write:
4915 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004916 return ret;
4917}
4918
Miao Xie905b0dd2012-11-26 08:50:11 +00004919static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004920{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004922 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004923 struct btrfs_ioctl_qgroup_create_args *sa;
4924 struct btrfs_trans_handle *trans;
4925 int ret;
4926 int err;
4927
4928 if (!capable(CAP_SYS_ADMIN))
4929 return -EPERM;
4930
Miao Xie905b0dd2012-11-26 08:50:11 +00004931 ret = mnt_want_write_file(file);
4932 if (ret)
4933 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004934
4935 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004936 if (IS_ERR(sa)) {
4937 ret = PTR_ERR(sa);
4938 goto drop_write;
4939 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004940
Miao Xied86e56c2012-11-15 11:35:41 +00004941 if (!sa->qgroupid) {
4942 ret = -EINVAL;
4943 goto out;
4944 }
4945
Arne Jansen5d13a372011-09-14 15:53:51 +02004946 trans = btrfs_join_transaction(root);
4947 if (IS_ERR(trans)) {
4948 ret = PTR_ERR(trans);
4949 goto out;
4950 }
4951
Arne Jansen5d13a372011-09-14 15:53:51 +02004952 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004953 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004954 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004955 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004956 }
4957
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004958 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004959 if (err && !ret)
4960 ret = err;
4961
4962out:
4963 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004964drop_write:
4965 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004966 return ret;
4967}
4968
Miao Xie905b0dd2012-11-26 08:50:11 +00004969static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004970{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004971 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004972 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004973 struct btrfs_ioctl_qgroup_limit_args *sa;
4974 struct btrfs_trans_handle *trans;
4975 int ret;
4976 int err;
4977 u64 qgroupid;
4978
4979 if (!capable(CAP_SYS_ADMIN))
4980 return -EPERM;
4981
Miao Xie905b0dd2012-11-26 08:50:11 +00004982 ret = mnt_want_write_file(file);
4983 if (ret)
4984 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004985
4986 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004987 if (IS_ERR(sa)) {
4988 ret = PTR_ERR(sa);
4989 goto drop_write;
4990 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004991
4992 trans = btrfs_join_transaction(root);
4993 if (IS_ERR(trans)) {
4994 ret = PTR_ERR(trans);
4995 goto out;
4996 }
4997
4998 qgroupid = sa->qgroupid;
4999 if (!qgroupid) {
5000 /* take the current subvol as qgroup */
5001 qgroupid = root->root_key.objectid;
5002 }
5003
Lu Fengqif0042d52018-07-18 14:45:35 +08005004 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005005
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005006 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005007 if (err && !ret)
5008 ret = err;
5009
5010out:
5011 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005012drop_write:
5013 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005014 return ret;
5015}
5016
Jan Schmidt2f232032013-04-25 16:04:51 +00005017static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5018{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005019 struct inode *inode = file_inode(file);
5020 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005021 struct btrfs_ioctl_quota_rescan_args *qsa;
5022 int ret;
5023
5024 if (!capable(CAP_SYS_ADMIN))
5025 return -EPERM;
5026
5027 ret = mnt_want_write_file(file);
5028 if (ret)
5029 return ret;
5030
5031 qsa = memdup_user(arg, sizeof(*qsa));
5032 if (IS_ERR(qsa)) {
5033 ret = PTR_ERR(qsa);
5034 goto drop_write;
5035 }
5036
5037 if (qsa->flags) {
5038 ret = -EINVAL;
5039 goto out;
5040 }
5041
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005042 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005043
5044out:
5045 kfree(qsa);
5046drop_write:
5047 mnt_drop_write_file(file);
5048 return ret;
5049}
5050
5051static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5052{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005053 struct inode *inode = file_inode(file);
5054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005055 struct btrfs_ioctl_quota_rescan_args *qsa;
5056 int ret = 0;
5057
5058 if (!capable(CAP_SYS_ADMIN))
5059 return -EPERM;
5060
David Sterba8d2db782015-11-04 15:38:29 +01005061 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005062 if (!qsa)
5063 return -ENOMEM;
5064
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005065 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005066 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005067 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005068 }
5069
5070 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5071 ret = -EFAULT;
5072
5073 kfree(qsa);
5074 return ret;
5075}
5076
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005077static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5078{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005079 struct inode *inode = file_inode(file);
5080 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005081
5082 if (!capable(CAP_SYS_ADMIN))
5083 return -EPERM;
5084
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005085 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005086}
5087
Hugo Millsabccd002014-01-30 20:17:00 +00005088static long _btrfs_ioctl_set_received_subvol(struct file *file,
5089 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005090{
Al Viro496ad9a2013-01-23 17:07:38 -05005091 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005092 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005093 struct btrfs_root *root = BTRFS_I(inode)->root;
5094 struct btrfs_root_item *root_item = &root->root_item;
5095 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07005096 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005097 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005098 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005099
David Sterbabd60ea02014-01-16 15:50:22 +01005100 if (!inode_owner_or_capable(inode))
5101 return -EPERM;
5102
Alexander Block8ea05e32012-07-25 17:35:53 +02005103 ret = mnt_want_write_file(file);
5104 if (ret < 0)
5105 return ret;
5106
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005107 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005108
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005109 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005110 ret = -EINVAL;
5111 goto out;
5112 }
5113
5114 if (btrfs_root_readonly(root)) {
5115 ret = -EROFS;
5116 goto out;
5117 }
5118
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005119 /*
5120 * 1 - root item
5121 * 2 - uuid items (received uuid + subvol uuid)
5122 */
5123 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005124 if (IS_ERR(trans)) {
5125 ret = PTR_ERR(trans);
5126 trans = NULL;
5127 goto out;
5128 }
5129
5130 sa->rtransid = trans->transid;
5131 sa->rtime.sec = ct.tv_sec;
5132 sa->rtime.nsec = ct.tv_nsec;
5133
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005134 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5135 BTRFS_UUID_SIZE);
5136 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005137 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005138 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005139 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5140 root->root_key.objectid);
5141 if (ret && ret != -ENOENT) {
5142 btrfs_abort_transaction(trans, ret);
5143 btrfs_end_transaction(trans);
5144 goto out;
5145 }
5146 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005147 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5148 btrfs_set_root_stransid(root_item, sa->stransid);
5149 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005150 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5151 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5152 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5153 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005154
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005155 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005156 &root->root_key, &root->root_item);
5157 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005158 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005159 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005160 }
5161 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005162 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005163 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5164 root->root_key.objectid);
5165 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005166 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005167 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005168 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005169 }
5170 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005171 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005172out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005173 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005174 mnt_drop_write_file(file);
5175 return ret;
5176}
5177
5178#ifdef CONFIG_64BIT
5179static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5180 void __user *arg)
5181{
5182 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5183 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5184 int ret = 0;
5185
5186 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305187 if (IS_ERR(args32))
5188 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005189
David Sterba8d2db782015-11-04 15:38:29 +01005190 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005191 if (!args64) {
5192 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005193 goto out;
5194 }
5195
5196 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5197 args64->stransid = args32->stransid;
5198 args64->rtransid = args32->rtransid;
5199 args64->stime.sec = args32->stime.sec;
5200 args64->stime.nsec = args32->stime.nsec;
5201 args64->rtime.sec = args32->rtime.sec;
5202 args64->rtime.nsec = args32->rtime.nsec;
5203 args64->flags = args32->flags;
5204
5205 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5206 if (ret)
5207 goto out;
5208
5209 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5210 args32->stransid = args64->stransid;
5211 args32->rtransid = args64->rtransid;
5212 args32->stime.sec = args64->stime.sec;
5213 args32->stime.nsec = args64->stime.nsec;
5214 args32->rtime.sec = args64->rtime.sec;
5215 args32->rtime.nsec = args64->rtime.nsec;
5216 args32->flags = args64->flags;
5217
5218 ret = copy_to_user(arg, args32, sizeof(*args32));
5219 if (ret)
5220 ret = -EFAULT;
5221
5222out:
5223 kfree(args32);
5224 kfree(args64);
5225 return ret;
5226}
5227#endif
5228
5229static long btrfs_ioctl_set_received_subvol(struct file *file,
5230 void __user *arg)
5231{
5232 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5233 int ret = 0;
5234
5235 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305236 if (IS_ERR(sa))
5237 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005238
5239 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5240
5241 if (ret)
5242 goto out;
5243
Alexander Block8ea05e32012-07-25 17:35:53 +02005244 ret = copy_to_user(arg, sa, sizeof(*sa));
5245 if (ret)
5246 ret = -EFAULT;
5247
5248out:
5249 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005250 return ret;
5251}
5252
jeff.liu867ab662013-01-05 02:48:01 +00005253static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5254{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 struct inode *inode = file_inode(file);
5256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005257 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005258 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005259 char label[BTRFS_LABEL_SIZE];
5260
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005261 spin_lock(&fs_info->super_lock);
5262 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5263 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005264
5265 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005266
5267 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005268 btrfs_warn(fs_info,
5269 "label is too long, return the first %zu bytes",
5270 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005271 }
5272
jeff.liu867ab662013-01-05 02:48:01 +00005273 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005274
5275 return ret ? -EFAULT : 0;
5276}
5277
jeff.liua8bfd4a2013-01-05 02:48:08 +00005278static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5279{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 struct inode *inode = file_inode(file);
5281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5282 struct btrfs_root *root = BTRFS_I(inode)->root;
5283 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005284 struct btrfs_trans_handle *trans;
5285 char label[BTRFS_LABEL_SIZE];
5286 int ret;
5287
5288 if (!capable(CAP_SYS_ADMIN))
5289 return -EPERM;
5290
5291 if (copy_from_user(label, arg, sizeof(label)))
5292 return -EFAULT;
5293
5294 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005295 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005296 "unable to set label with more than %d bytes",
5297 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005298 return -EINVAL;
5299 }
5300
5301 ret = mnt_want_write_file(file);
5302 if (ret)
5303 return ret;
5304
jeff.liua8bfd4a2013-01-05 02:48:08 +00005305 trans = btrfs_start_transaction(root, 0);
5306 if (IS_ERR(trans)) {
5307 ret = PTR_ERR(trans);
5308 goto out_unlock;
5309 }
5310
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005311 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005312 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005313 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005314 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005315
5316out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005317 mnt_drop_write_file(file);
5318 return ret;
5319}
5320
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005321#define INIT_FEATURE_FLAGS(suffix) \
5322 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5323 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5324 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5325
David Sterbad5131b62016-02-17 15:26:27 +01005326int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005327{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005328 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005329 INIT_FEATURE_FLAGS(SUPP),
5330 INIT_FEATURE_FLAGS(SAFE_SET),
5331 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5332 };
5333
5334 if (copy_to_user(arg, &features, sizeof(features)))
5335 return -EFAULT;
5336
5337 return 0;
5338}
5339
5340static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5341{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005342 struct inode *inode = file_inode(file);
5343 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5344 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005345 struct btrfs_ioctl_feature_flags features;
5346
5347 features.compat_flags = btrfs_super_compat_flags(super_block);
5348 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5349 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5350
5351 if (copy_to_user(arg, &features, sizeof(features)))
5352 return -EFAULT;
5353
5354 return 0;
5355}
5356
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005357static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005358 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005359 u64 change_mask, u64 flags, u64 supported_flags,
5360 u64 safe_set, u64 safe_clear)
5361{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005362 const char *type = btrfs_feature_set_names[set];
5363 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005364 u64 disallowed, unsupported;
5365 u64 set_mask = flags & change_mask;
5366 u64 clear_mask = ~flags & change_mask;
5367
5368 unsupported = set_mask & ~supported_flags;
5369 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005370 names = btrfs_printable_features(set, unsupported);
5371 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005372 btrfs_warn(fs_info,
5373 "this kernel does not support the %s feature bit%s",
5374 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005375 kfree(names);
5376 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005377 btrfs_warn(fs_info,
5378 "this kernel does not support %s bits 0x%llx",
5379 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005380 return -EOPNOTSUPP;
5381 }
5382
5383 disallowed = set_mask & ~safe_set;
5384 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005385 names = btrfs_printable_features(set, disallowed);
5386 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005387 btrfs_warn(fs_info,
5388 "can't set the %s feature bit%s while mounted",
5389 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005390 kfree(names);
5391 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005392 btrfs_warn(fs_info,
5393 "can't set %s bits 0x%llx while mounted",
5394 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005395 return -EPERM;
5396 }
5397
5398 disallowed = clear_mask & ~safe_clear;
5399 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005400 names = btrfs_printable_features(set, disallowed);
5401 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005402 btrfs_warn(fs_info,
5403 "can't clear the %s feature bit%s while mounted",
5404 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005405 kfree(names);
5406 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005407 btrfs_warn(fs_info,
5408 "can't clear %s bits 0x%llx while mounted",
5409 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005410 return -EPERM;
5411 }
5412
5413 return 0;
5414}
5415
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005416#define check_feature(fs_info, change_mask, flags, mask_base) \
5417check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005418 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5419 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5420 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5421
5422static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005424 struct inode *inode = file_inode(file);
5425 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5426 struct btrfs_root *root = BTRFS_I(inode)->root;
5427 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005428 struct btrfs_ioctl_feature_flags flags[2];
5429 struct btrfs_trans_handle *trans;
5430 u64 newflags;
5431 int ret;
5432
5433 if (!capable(CAP_SYS_ADMIN))
5434 return -EPERM;
5435
5436 if (copy_from_user(flags, arg, sizeof(flags)))
5437 return -EFAULT;
5438
5439 /* Nothing to do */
5440 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5441 !flags[0].incompat_flags)
5442 return 0;
5443
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005444 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005445 flags[1].compat_flags, COMPAT);
5446 if (ret)
5447 return ret;
5448
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005449 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005450 flags[1].compat_ro_flags, COMPAT_RO);
5451 if (ret)
5452 return ret;
5453
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005454 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005455 flags[1].incompat_flags, INCOMPAT);
5456 if (ret)
5457 return ret;
5458
David Sterba7ab19622016-05-04 11:32:00 +02005459 ret = mnt_want_write_file(file);
5460 if (ret)
5461 return ret;
5462
David Sterba8051aa12014-02-07 14:34:04 +01005463 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005464 if (IS_ERR(trans)) {
5465 ret = PTR_ERR(trans);
5466 goto out_drop_write;
5467 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005468
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005469 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005470 newflags = btrfs_super_compat_flags(super_block);
5471 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5472 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5473 btrfs_set_super_compat_flags(super_block, newflags);
5474
5475 newflags = btrfs_super_compat_ro_flags(super_block);
5476 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5477 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5478 btrfs_set_super_compat_ro_flags(super_block, newflags);
5479
5480 newflags = btrfs_super_incompat_flags(super_block);
5481 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5482 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5483 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005484 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005485
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005486 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005487out_drop_write:
5488 mnt_drop_write_file(file);
5489
5490 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005491}
5492
Josef Bacik2351f432017-09-27 10:43:13 -04005493static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5494{
5495 struct btrfs_ioctl_send_args *arg;
5496 int ret;
5497
5498 if (compat) {
5499#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5500 struct btrfs_ioctl_send_args_32 args32;
5501
5502 ret = copy_from_user(&args32, argp, sizeof(args32));
5503 if (ret)
5504 return -EFAULT;
5505 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5506 if (!arg)
5507 return -ENOMEM;
5508 arg->send_fd = args32.send_fd;
5509 arg->clone_sources_count = args32.clone_sources_count;
5510 arg->clone_sources = compat_ptr(args32.clone_sources);
5511 arg->parent_root = args32.parent_root;
5512 arg->flags = args32.flags;
5513 memcpy(arg->reserved, args32.reserved,
5514 sizeof(args32.reserved));
5515#else
5516 return -ENOTTY;
5517#endif
5518 } else {
5519 arg = memdup_user(argp, sizeof(*arg));
5520 if (IS_ERR(arg))
5521 return PTR_ERR(arg);
5522 }
5523 ret = btrfs_ioctl_send(file, arg);
5524 kfree(arg);
5525 return ret;
5526}
5527
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005528long btrfs_ioctl(struct file *file, unsigned int
5529 cmd, unsigned long arg)
5530{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005531 struct inode *inode = file_inode(file);
5532 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5533 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005534 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005535
5536 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005537 case FS_IOC_GETFLAGS:
5538 return btrfs_ioctl_getflags(file, argp);
5539 case FS_IOC_SETFLAGS:
5540 return btrfs_ioctl_setflags(file, argp);
5541 case FS_IOC_GETVERSION:
5542 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005543 case FITRIM:
5544 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005545 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005546 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005547 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005548 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005549 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005550 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005551 case BTRFS_IOC_SUBVOL_CREATE_V2:
5552 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005553 case BTRFS_IOC_SNAP_DESTROY:
5554 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005555 case BTRFS_IOC_SUBVOL_GETFLAGS:
5556 return btrfs_ioctl_subvol_getflags(file, argp);
5557 case BTRFS_IOC_SUBVOL_SETFLAGS:
5558 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005559 case BTRFS_IOC_DEFAULT_SUBVOL:
5560 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005561 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005562 return btrfs_ioctl_defrag(file, NULL);
5563 case BTRFS_IOC_DEFRAG_RANGE:
5564 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005565 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005566 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005567 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005568 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005569 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005570 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005571 case BTRFS_IOC_RM_DEV_V2:
5572 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005573 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005574 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005575 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005576 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005577 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005578 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005579 case BTRFS_IOC_TREE_SEARCH:
5580 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005581 case BTRFS_IOC_TREE_SEARCH_V2:
5582 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005583 case BTRFS_IOC_INO_LOOKUP:
5584 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005585 case BTRFS_IOC_INO_PATHS:
5586 return btrfs_ioctl_ino_to_path(root, argp);
5587 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005588 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5589 case BTRFS_IOC_LOGICAL_INO_V2:
5590 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005591 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005592 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005593 case BTRFS_IOC_SYNC: {
5594 int ret;
5595
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005596 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005597 if (ret)
5598 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005599 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005600 /*
5601 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005602 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005603 * processing uncleaned subvols.
5604 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005605 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005606 return ret;
5607 }
Sage Weil46204592010-10-29 15:41:32 -04005608 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005609 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005610 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005611 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005612 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005613 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005614 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005615 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005616 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005617 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005618 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005619 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005620 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005621 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005622 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005623 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005624 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5625 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005626#ifdef CONFIG_64BIT
5627 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5628 return btrfs_ioctl_set_received_subvol_32(file, argp);
5629#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005630 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005631 return _btrfs_ioctl_send(file, argp, false);
5632#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5633 case BTRFS_IOC_SEND_32:
5634 return _btrfs_ioctl_send(file, argp, true);
5635#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005636 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005637 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005638 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005639 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005640 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005641 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005642 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005643 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005644 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005645 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005646 case BTRFS_IOC_QUOTA_RESCAN:
5647 return btrfs_ioctl_quota_rescan(file, argp);
5648 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5649 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005650 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5651 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005652 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005653 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005654 case BTRFS_IOC_GET_FSLABEL:
5655 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005656 case BTRFS_IOC_SET_FSLABEL:
5657 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005658 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005659 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005660 case BTRFS_IOC_GET_FEATURES:
5661 return btrfs_ioctl_get_features(file, argp);
5662 case BTRFS_IOC_SET_FEATURES:
5663 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005664 case FS_IOC_FSGETXATTR:
5665 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005666 case FS_IOC_FSSETXATTR:
5667 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005668 case BTRFS_IOC_GET_SUBVOL_INFO:
5669 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005670 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5671 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005672 case BTRFS_IOC_INO_LOOKUP_USER:
5673 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005674 }
5675
5676 return -ENOTTY;
5677}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005678
5679#ifdef CONFIG_COMPAT
5680long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5681{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005682 /*
5683 * These all access 32-bit values anyway so no further
5684 * handling is necessary.
5685 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005686 switch (cmd) {
5687 case FS_IOC32_GETFLAGS:
5688 cmd = FS_IOC_GETFLAGS;
5689 break;
5690 case FS_IOC32_SETFLAGS:
5691 cmd = FS_IOC_SETFLAGS;
5692 break;
5693 case FS_IOC32_GETVERSION:
5694 cmd = FS_IOC_GETVERSION;
5695 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005696 }
5697
5698 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5699}
5700#endif