blob: d60b6caf09e857ef7c39e8ce337171d04cca1d72 [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
David Sterba5c57b8b2018-04-23 15:45:18 +0200293 binode->flags |= BTRFS_INODE_COMPRESS;
294 binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000295
David Sterba93370502017-10-31 17:32:41 +0100296 comp = btrfs_compress_type2str(fs_info->compress_type);
297 if (!comp || comp[0] == 0)
298 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
299
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000300 ret = btrfs_set_prop(inode, "btrfs.compression",
301 comp, strlen(comp), 0);
302 if (ret)
303 goto out_drop;
304
Li Zefanebcb9042011-04-15 03:03:17 +0000305 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100306 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
307 if (ret && ret != -ENODATA)
308 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200309 binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000310 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200311
Li Zefan4da6f1a2011-12-29 13:39:50 +0800312 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800313 if (IS_ERR(trans)) {
314 ret = PTR_ERR(trans);
315 goto out_drop;
316 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200317
David Sterba7b6a2212018-03-26 18:40:21 +0200318 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400319 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700320 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200321 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400323 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800324 out_drop:
325 if (ret) {
David Sterba5c57b8b2018-04-23 15:45:18 +0200326 binode->flags = old_flags;
327 inode->i_flags = old_i_flags;
Li Zefanf062abf02011-12-29 13:36:45 +0800328 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200329
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200330 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500331 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200332 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000333 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200334}
335
David Sterba19f93b32018-03-26 19:42:05 +0200336/*
337 * Translate btrfs internal inode flags to xflags as expected by the
338 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
339 * silently dropped.
340 */
341static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
342{
343 unsigned int xflags = 0;
344
345 if (flags & BTRFS_INODE_APPEND)
346 xflags |= FS_XFLAG_APPEND;
347 if (flags & BTRFS_INODE_IMMUTABLE)
348 xflags |= FS_XFLAG_IMMUTABLE;
349 if (flags & BTRFS_INODE_NOATIME)
350 xflags |= FS_XFLAG_NOATIME;
351 if (flags & BTRFS_INODE_NODUMP)
352 xflags |= FS_XFLAG_NODUMP;
353 if (flags & BTRFS_INODE_SYNC)
354 xflags |= FS_XFLAG_SYNC;
355
356 return xflags;
357}
358
359/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
360static int check_xflags(unsigned int flags)
361{
362 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
363 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
364 return -EOPNOTSUPP;
365 return 0;
366}
367
David Sterbae4202ac2018-03-26 19:51:16 +0200368/*
369 * Set the xflags from the internal inode flags. The remaining items of fsxattr
370 * are zeroed.
371 */
372static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
373{
374 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
375 struct fsxattr fa;
376
377 memset(&fa, 0, sizeof(fa));
378 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
379
380 if (copy_to_user(arg, &fa, sizeof(fa)))
381 return -EFAULT;
382
383 return 0;
384}
385
David Sterba025f2122018-03-26 19:51:16 +0200386static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
387{
388 struct inode *inode = file_inode(file);
389 struct btrfs_inode *binode = BTRFS_I(inode);
390 struct btrfs_root *root = binode->root;
391 struct btrfs_trans_handle *trans;
392 struct fsxattr fa;
393 unsigned old_flags;
394 unsigned old_i_flags;
395 int ret = 0;
396
397 if (!inode_owner_or_capable(inode))
398 return -EPERM;
399
400 if (btrfs_root_readonly(root))
401 return -EROFS;
402
403 memset(&fa, 0, sizeof(fa));
404 if (copy_from_user(&fa, arg, sizeof(fa)))
405 return -EFAULT;
406
407 ret = check_xflags(fa.fsx_xflags);
408 if (ret)
409 return ret;
410
411 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
412 return -EOPNOTSUPP;
413
414 ret = mnt_want_write_file(file);
415 if (ret)
416 return ret;
417
418 inode_lock(inode);
419
420 old_flags = binode->flags;
421 old_i_flags = inode->i_flags;
422
423 /* We need the capabilities to change append-only or immutable inode */
424 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
425 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
426 !capable(CAP_LINUX_IMMUTABLE)) {
427 ret = -EPERM;
428 goto out_unlock;
429 }
430
431 if (fa.fsx_xflags & FS_XFLAG_SYNC)
432 binode->flags |= BTRFS_INODE_SYNC;
433 else
434 binode->flags &= ~BTRFS_INODE_SYNC;
435 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
436 binode->flags |= BTRFS_INODE_IMMUTABLE;
437 else
438 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
439 if (fa.fsx_xflags & FS_XFLAG_APPEND)
440 binode->flags |= BTRFS_INODE_APPEND;
441 else
442 binode->flags &= ~BTRFS_INODE_APPEND;
443 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
444 binode->flags |= BTRFS_INODE_NODUMP;
445 else
446 binode->flags &= ~BTRFS_INODE_NODUMP;
447 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
448 binode->flags |= BTRFS_INODE_NOATIME;
449 else
450 binode->flags &= ~BTRFS_INODE_NOATIME;
451
452 /* 1 item for the inode */
453 trans = btrfs_start_transaction(root, 1);
454 if (IS_ERR(trans)) {
455 ret = PTR_ERR(trans);
456 goto out_unlock;
457 }
458
459 btrfs_sync_inode_flags_to_i_flags(inode);
460 inode_inc_iversion(inode);
461 inode->i_ctime = current_time(inode);
462 ret = btrfs_update_inode(trans, root, inode);
463
464 btrfs_end_transaction(trans);
465
466out_unlock:
467 if (ret) {
468 binode->flags = old_flags;
469 inode->i_flags = old_i_flags;
470 }
471
472 inode_unlock(inode);
473 mnt_drop_write_file(file);
474
475 return ret;
476}
477
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200478static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
479{
Al Viro496ad9a2013-01-23 17:07:38 -0500480 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200481
482 return put_user(inode->i_generation, arg);
483}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400484
Li Dongyangf7039b12011-03-24 10:24:28 +0000485static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400487 struct inode *inode = file_inode(file);
488 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000489 struct btrfs_device *device;
490 struct request_queue *q;
491 struct fstrim_range range;
492 u64 minlen = ULLONG_MAX;
493 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500494 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000495 int ret;
496
497 if (!capable(CAP_SYS_ADMIN))
498 return -EPERM;
499
Xiao Guangrong1f781602011-04-20 10:09:16 +0000500 rcu_read_lock();
501 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
502 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000503 if (!device->bdev)
504 continue;
505 q = bdev_get_queue(device->bdev);
506 if (blk_queue_discard(q)) {
507 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100508 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000509 minlen);
510 }
511 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000512 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200513
Li Dongyangf7039b12011-03-24 10:24:28 +0000514 if (!num_devices)
515 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000516 if (copy_from_user(&range, arg, sizeof(range)))
517 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000518 if (range.start > total_bytes ||
519 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200520 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000521
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200522 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000523 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400524 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000525 if (ret < 0)
526 return ret;
527
528 if (copy_to_user(arg, &range, sizeof(range)))
529 return -EFAULT;
530
531 return 0;
532}
533
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200534int btrfs_is_empty_uuid(u8 *uuid)
535{
Chris Mason46e0f662013-11-15 12:14:55 +0100536 int i;
537
538 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
539 if (uuid[i])
540 return 0;
541 }
542 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200543}
544
Miao Xied5c12072013-02-28 10:04:33 +0000545static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400546 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100547 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200548 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000549 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400550{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400551 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400552 struct btrfs_trans_handle *trans;
553 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100554 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555 struct btrfs_inode_item *inode_item;
556 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000557 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400558 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000559 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700560 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900561 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400562 int ret;
563 int err;
564 u64 objectid;
565 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500566 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200567 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400568
David Sterba49a3c4d2016-03-24 17:49:22 +0100569 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
570 if (!root_item)
571 return -ENOMEM;
572
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400573 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400574 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100575 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000576
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800577 /*
578 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400579 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800580 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100581 if (btrfs_qgroup_level(objectid)) {
582 ret = -ENOSPC;
583 goto fail_free;
584 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800585
Miao Xied5c12072013-02-28 10:04:33 +0000586 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400587 /*
Miao Xied5c12072013-02-28 10:04:33 +0000588 * The same as the snapshot creation, please see the comment
589 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400590 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800591 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000592 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100593 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594
Miao Xied5c12072013-02-28 10:04:33 +0000595 trans = btrfs_start_transaction(root, 0);
596 if (IS_ERR(trans)) {
597 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100598 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100599 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000600 }
601 trans->block_rsv = &block_rsv;
602 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400603
Lu Fengqia93774222018-07-18 14:45:41 +0800604 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200605 if (ret)
606 goto fail;
607
David Sterba4d75f8a2014-06-15 01:54:12 +0200608 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400609 if (IS_ERR(leaf)) {
610 ret = PTR_ERR(leaf);
611 goto fail;
612 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400613
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400614 btrfs_mark_buffer_dirty(leaf);
615
David Sterba49a3c4d2016-03-24 17:49:22 +0100616 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800617 btrfs_set_stack_inode_generation(inode_item, 1);
618 btrfs_set_stack_inode_size(inode_item, 3);
619 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400620 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400621 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800622 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623
David Sterba49a3c4d2016-03-24 17:49:22 +0100624 btrfs_set_root_flags(root_item, 0);
625 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800626 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000627
David Sterba49a3c4d2016-03-24 17:49:22 +0100628 btrfs_set_root_bytenr(root_item, leaf->start);
629 btrfs_set_root_generation(root_item, trans->transid);
630 btrfs_set_root_level(root_item, 0);
631 btrfs_set_root_refs(root_item, 1);
632 btrfs_set_root_used(root_item, leaf->len);
633 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634
David Sterba49a3c4d2016-03-24 17:49:22 +0100635 btrfs_set_root_generation_v2(root_item,
636 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200637 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100638 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
639 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
640 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
641 root_item->ctime = root_item->otime;
642 btrfs_set_root_ctransid(root_item, trans->transid);
643 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400644
Chris Mason925baed2008-06-25 16:01:30 -0400645 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400646 free_extent_buffer(leaf);
647 leaf = NULL;
648
David Sterba49a3c4d2016-03-24 17:49:22 +0100649 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400650
651 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400652 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200653 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400654 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100655 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656 if (ret)
657 goto fail;
658
Yan, Zheng76dda932009-09-21 16:00:26 -0400659 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400660 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100661 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100662 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400663 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100664 goto fail;
665 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400666
667 btrfs_record_root_in_trans(trans, new_root);
668
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000669 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700670 if (ret) {
671 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400672 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700673 goto fail;
674 }
675
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530676 mutex_lock(&new_root->objectid_mutex);
677 new_root->highest_objectid = new_dirid;
678 mutex_unlock(&new_root->objectid_mutex);
679
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400680 /*
681 * insert the directory item
682 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200683 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100684 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400685 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 goto fail;
687 }
Chris Mason3de45862008-11-17 21:02:50 -0500688
689 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200690 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500691 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100692 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400693 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400694 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100695 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500696
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200697 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500698 ret = btrfs_update_inode(trans, root, dir);
699 BUG_ON(ret);
700
Lu Fengqi6025c192018-08-01 11:32:29 +0800701 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200702 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500703 BUG_ON(ret);
704
Lu Fengqicdb345a2018-05-29 15:01:53 +0800705 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400706 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200707 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400708 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200709
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400710fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100711 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000712 trans->block_rsv = NULL;
713 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100714 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800715
Sage Weil72fd0322010-10-29 15:41:32 -0400716 if (async_transid) {
717 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400718 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000719 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400720 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400721 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400722 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400723 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400724 if (err && !ret)
725 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500726
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900727 if (!ret) {
728 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800729 if (IS_ERR(inode))
730 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900731 d_instantiate(dentry, inode);
732 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400733 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100734
735fail_free:
736 kfree(root_item);
737 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738}
739
Miao Xiee9662f72013-02-28 10:01:15 +0000740static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100741 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000742 u64 *async_transid, bool readonly,
743 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400744{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400745 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000746 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400747 struct btrfs_pending_snapshot *pending_snapshot;
748 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000749 int ret;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800750 bool snapshot_force_cow = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400751
Miao Xie27cdeb72014-04-02 19:51:05 +0800752 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400753 return -EINVAL;
754
David Sterba23269bf2017-02-13 11:03:44 +0100755 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100756 if (!pending_snapshot)
757 return -ENOMEM;
758
David Sterbab0c0ea62015-11-10 18:54:00 +0100759 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100760 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100761 pending_snapshot->path = btrfs_alloc_path();
762 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100763 ret = -ENOMEM;
764 goto free_pending;
765 }
766
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800767 /*
768 * Force new buffered writes to reserve space even when NOCOW is
769 * possible. This is to avoid later writeback (running dealloc) to
770 * fallback to COW mode and unexpectedly fail with ENOSPC.
771 */
David Sterbaea14b57f2017-06-22 02:19:11 +0200772 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100773 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600774 /* wait for no snapshot writes */
775 wait_event(root->subv_writers->wait,
776 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800777
Nikolay Borisov76f32e22018-04-23 10:54:14 +0300778 ret = btrfs_start_delalloc_inodes(root);
Miao Xie6a038432013-05-15 07:48:24 +0000779 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100780 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000781
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800782 /*
783 * All previous writes have started writeback in NOCOW mode, so now
784 * we force future writes to fallback to COW mode during snapshot
785 * creation.
786 */
787 atomic_inc(&root->snapshot_force_cow);
788 snapshot_force_cow = true;
789
Chris Mason6374e57a2017-06-23 09:48:21 -0700790 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000791
Miao Xie66d8f3d2012-09-06 04:02:28 -0600792 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
793 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000794 /*
795 * 1 - parent dir inode
796 * 2 - dir entries
797 * 1 - root item
798 * 2 - root ref/backref
799 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200800 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000801 */
802 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200803 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400804 false);
Miao Xied5c12072013-02-28 10:04:33 +0000805 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100806 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000807
Yan, Zhenga22285a2010-05-16 10:48:46 -0400808 pending_snapshot->dentry = dentry;
809 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800810 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000811 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000812 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400813
Miao Xied5c12072013-02-28 10:04:33 +0000814 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400815 if (IS_ERR(trans)) {
816 ret = PTR_ERR(trans);
817 goto fail;
818 }
819
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400820 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400821 list_add(&pending_snapshot->list,
822 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400823 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400824 if (async_transid) {
825 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400826 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000827 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400828 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400829 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400830 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400831 }
Miao Xieaec80302013-03-04 09:44:29 +0000832 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400833 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400834
835 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400836 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000837 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400838
Chris Masond3797302014-10-15 13:50:56 -0700839 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
840 if (ret)
841 goto fail;
842
David Howells2b0143b2015-03-17 22:25:59 +0000843 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000844 if (IS_ERR(inode)) {
845 ret = PTR_ERR(inode);
846 goto fail;
847 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900848
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000849 d_instantiate(dentry, inode);
850 ret = 0;
851fail:
David Sterba7775c812017-02-10 19:18:18 +0100852 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100853dec_and_free:
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800854 if (snapshot_force_cow)
855 atomic_dec(&root->snapshot_force_cow);
David Sterbaea14b57f2017-06-22 02:19:11 +0200856 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100857 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100858free_pending:
859 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100860 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100861 kfree(pending_snapshot);
862
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400863 return ret;
864}
865
Sage Weil4260f7c2010-10-29 15:46:43 -0400866/* copy of may_delete in fs/namei.c()
867 * Check whether we can remove a link victim from directory dir, check
868 * whether the type of victim is right.
869 * 1. We can't do it if dir is read-only (done in permission())
870 * 2. We should have write and exec permissions on dir
871 * 3. We can't remove anything from append-only dir
872 * 4. We can't do anything with immutable dir (done in permission())
873 * 5. If the sticky bit on dir is set we should either
874 * a. be owner of dir, or
875 * b. be owner of victim, or
876 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400877 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400878 * links pointing to it.
879 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
880 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
881 * 9. We can't remove a root or mountpoint.
882 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
883 * nfs_async_unlink().
884 */
885
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530886static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400887{
888 int error;
889
David Howells2b0143b2015-03-17 22:25:59 +0000890 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400891 return -ENOENT;
892
David Howells2b0143b2015-03-17 22:25:59 +0000893 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400894 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400895
896 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
897 if (error)
898 return error;
899 if (IS_APPEND(dir))
900 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000901 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
902 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400903 return -EPERM;
904 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000905 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400906 return -ENOTDIR;
907 if (IS_ROOT(victim))
908 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000909 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400910 return -EISDIR;
911 if (IS_DEADDIR(dir))
912 return -ENOENT;
913 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
914 return -EBUSY;
915 return 0;
916}
917
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400918/* copy of may_create in fs/namei.c() */
919static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
920{
David Howells2b0143b2015-03-17 22:25:59 +0000921 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400922 return -EEXIST;
923 if (IS_DEADDIR(dir))
924 return -ENOENT;
925 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
926}
927
928/*
929 * Create a new subvolume below @parent. This is largely modeled after
930 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
931 * inside this filesystem so it's quite a bit simpler.
932 */
Al Viro92872092016-11-20 19:34:31 -0500933static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100934 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400935 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200936 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000937 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400938{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400939 struct inode *dir = d_inode(parent->dentry);
940 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400941 struct dentry *dentry;
942 int error;
943
Al Viro00235412016-05-26 00:05:12 -0400944 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
945 if (error == -EINTR)
946 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400947
948 dentry = lookup_one_len(name, parent->dentry, namelen);
949 error = PTR_ERR(dentry);
950 if (IS_ERR(dentry))
951 goto out_unlock;
952
Yan, Zheng76dda932009-09-21 16:00:26 -0400953 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400954 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600955 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400956
Chris Mason9c520572012-12-17 14:26:57 -0500957 /*
958 * even if this name doesn't exist, we may get hash collisions.
959 * check for them now when we can safely fail
960 */
961 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
962 dir->i_ino, name,
963 namelen);
964 if (error)
965 goto out_dput;
966
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400967 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400968
969 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
970 goto out_up_read;
971
Chris Mason3de45862008-11-17 21:02:50 -0500972 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100973 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200974 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500975 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000976 error = create_subvol(dir, dentry, name, namelen,
977 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500978 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400979 if (!error)
980 fsnotify_mkdir(dir, dentry);
981out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400982 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400983out_dput:
984 dput(dentry);
985out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500986 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400987 return error;
988}
989
Chris Mason4cb53002011-05-24 15:35:30 -0400990/*
991 * When we're defragging a range, we don't want to kick it off again
992 * if it is really just waiting for delalloc to send it down.
993 * If we find a nice big extent or delalloc range for the bytes in the
994 * file you want to defrag, we return 0 to let you know to skip this
995 * part of the file
996 */
David Sterbaaab110a2014-07-29 17:32:10 +0200997static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400998{
999 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1000 struct extent_map *em = NULL;
1001 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1002 u64 end;
1003
1004 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001005 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001006 read_unlock(&em_tree->lock);
1007
1008 if (em) {
1009 end = extent_map_end(em);
1010 free_extent_map(em);
1011 if (end - offset > thresh)
1012 return 0;
1013 }
1014 /* if we already have a nice delalloc here, just stop */
1015 thresh /= 2;
1016 end = count_range_bits(io_tree, &offset, offset + thresh,
1017 thresh, EXTENT_DELALLOC, 1);
1018 if (end >= thresh)
1019 return 0;
1020 return 1;
1021}
1022
1023/*
1024 * helper function to walk through a file and find extents
1025 * newer than a specific transid, and smaller than thresh.
1026 *
1027 * This is used by the defragging code to find new and small
1028 * extents
1029 */
1030static int find_new_extents(struct btrfs_root *root,
1031 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001032 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001033{
1034 struct btrfs_path *path;
1035 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001036 struct extent_buffer *leaf;
1037 struct btrfs_file_extent_item *extent;
1038 int type;
1039 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001040 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001041
1042 path = btrfs_alloc_path();
1043 if (!path)
1044 return -ENOMEM;
1045
David Sterbaa4689d22011-05-31 17:08:14 +00001046 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001047 min_key.type = BTRFS_EXTENT_DATA_KEY;
1048 min_key.offset = *off;
1049
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301050 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001051 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001052 if (ret != 0)
1053 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001054process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001055 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001056 goto none;
1057 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1058 goto none;
1059
1060 leaf = path->nodes[0];
1061 extent = btrfs_item_ptr(leaf, path->slots[0],
1062 struct btrfs_file_extent_item);
1063
1064 type = btrfs_file_extent_type(leaf, extent);
1065 if (type == BTRFS_FILE_EXTENT_REG &&
1066 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1067 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1068 *off = min_key.offset;
1069 btrfs_free_path(path);
1070 return 0;
1071 }
1072
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001073 path->slots[0]++;
1074 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1075 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1076 goto process_slot;
1077 }
1078
Chris Mason4cb53002011-05-24 15:35:30 -04001079 if (min_key.offset == (u64)-1)
1080 goto none;
1081
1082 min_key.offset++;
1083 btrfs_release_path(path);
1084 }
1085none:
1086 btrfs_free_path(path);
1087 return -ENOENT;
1088}
1089
Li Zefan6c282eb2012-06-11 16:03:35 +08001090static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001091{
1092 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001093 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001094 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001095 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001096
1097 /*
1098 * hopefully we have this extent in the tree already, try without
1099 * the full extent lock
1100 */
1101 read_lock(&em_tree->lock);
1102 em = lookup_extent_mapping(em_tree, start, len);
1103 read_unlock(&em_tree->lock);
1104
1105 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001106 struct extent_state *cached = NULL;
1107 u64 end = start + len - 1;
1108
Chris Mason940100a2010-03-10 10:52:59 -05001109 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001110 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001111 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001112 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001113
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001114 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001115 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001116 }
1117
Li Zefan6c282eb2012-06-11 16:03:35 +08001118 return em;
1119}
1120
1121static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1122{
1123 struct extent_map *next;
1124 bool ret = true;
1125
1126 /* this is the last extent */
1127 if (em->start + em->len >= i_size_read(inode))
1128 return false;
1129
1130 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001131 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1132 ret = false;
1133 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001134 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001135 ret = false;
1136
1137 free_extent_map(next);
1138 return ret;
1139}
1140
David Sterbaaab110a2014-07-29 17:32:10 +02001141static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001142 u64 *last_len, u64 *skip, u64 *defrag_end,
1143 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001144{
1145 struct extent_map *em;
1146 int ret = 1;
1147 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001148 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001149
1150 /*
1151 * make sure that once we start defragging an extent, we keep on
1152 * defragging it
1153 */
1154 if (start < *defrag_end)
1155 return 1;
1156
1157 *skip = 0;
1158
1159 em = defrag_lookup_extent(inode, start);
1160 if (!em)
1161 return 0;
1162
Chris Mason940100a2010-03-10 10:52:59 -05001163 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001164 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001165 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001166 goto out;
1167 }
1168
Liu Bo4a3560c2015-08-07 16:48:41 +08001169 if (!*defrag_end)
1170 prev_mergeable = false;
1171
Li Zefan6c282eb2012-06-11 16:03:35 +08001172 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001173 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001174 * we hit a real extent, if it is big or the next extent is not a
1175 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001176 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001177 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001178 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001179 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001180out:
Chris Mason940100a2010-03-10 10:52:59 -05001181 /*
1182 * last_len ends up being a counter of how many bytes we've defragged.
1183 * every time we choose not to defrag an extent, we reset *last_len
1184 * so that the next tiny extent will force a defrag.
1185 *
1186 * The end result of this is that tiny extents before a single big
1187 * extent will force at least part of that big extent to be defragged.
1188 */
1189 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001190 *defrag_end = extent_map_end(em);
1191 } else {
1192 *last_len = 0;
1193 *skip = extent_map_end(em);
1194 *defrag_end = 0;
1195 }
1196
1197 free_extent_map(em);
1198 return ret;
1199}
1200
Chris Mason4cb53002011-05-24 15:35:30 -04001201/*
1202 * it doesn't do much good to defrag one or two pages
1203 * at a time. This pulls in a nice chunk of pages
1204 * to COW and defrag.
1205 *
1206 * It also makes sure the delalloc code has enough
1207 * dirty data to avoid making new small extents as part
1208 * of the defrag
1209 *
1210 * It's a good idea to start RA on this range
1211 * before calling this.
1212 */
1213static int cluster_pages_for_defrag(struct inode *inode,
1214 struct page **pages,
1215 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001216 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001217{
Chris Mason4cb53002011-05-24 15:35:30 -04001218 unsigned long file_end;
1219 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001220 u64 page_start;
1221 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001222 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001223 int ret;
1224 int i;
1225 int i_done;
1226 struct btrfs_ordered_extent *ordered;
1227 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001228 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001229 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001230 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001231
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001232 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001233 if (!isize || start_index > file_end)
1234 return 0;
1235
1236 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001237
Qu Wenruo364ecf32017-02-27 15:10:38 +08001238 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001239 start_index << PAGE_SHIFT,
1240 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001241 if (ret)
1242 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001243 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001244 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001245
1246 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001247 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001248 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001249again:
Josef Bacika94733d2011-07-11 10:47:06 -04001250 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001251 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001252 if (!page)
1253 break;
1254
Miao Xie600a45e2012-02-16 15:01:24 +08001255 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001256 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001257 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001258 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001259 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001260 ordered = btrfs_lookup_ordered_extent(inode,
1261 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001262 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001263 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001264 if (!ordered)
1265 break;
1266
1267 unlock_page(page);
1268 btrfs_start_ordered_extent(inode, ordered, 1);
1269 btrfs_put_ordered_extent(ordered);
1270 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001271 /*
1272 * we unlocked the page above, so we need check if
1273 * it was released or not.
1274 */
1275 if (page->mapping != inode->i_mapping) {
1276 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001277 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001278 goto again;
1279 }
Miao Xie600a45e2012-02-16 15:01:24 +08001280 }
1281
Chris Mason4cb53002011-05-24 15:35:30 -04001282 if (!PageUptodate(page)) {
1283 btrfs_readpage(NULL, page);
1284 lock_page(page);
1285 if (!PageUptodate(page)) {
1286 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001287 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001288 ret = -EIO;
1289 break;
1290 }
1291 }
Miao Xie600a45e2012-02-16 15:01:24 +08001292
Miao Xie600a45e2012-02-16 15:01:24 +08001293 if (page->mapping != inode->i_mapping) {
1294 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001295 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001296 goto again;
1297 }
1298
Chris Mason4cb53002011-05-24 15:35:30 -04001299 pages[i] = page;
1300 i_done++;
1301 }
1302 if (!i_done || ret)
1303 goto out;
1304
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001305 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001306 goto out;
1307
1308 /*
1309 * so now we have a nice long stream of locked
1310 * and up to date pages, lets wait on them
1311 */
1312 for (i = 0; i < i_done; i++)
1313 wait_on_page_writeback(pages[i]);
1314
1315 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001316 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001317
1318 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001319 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001320 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1321 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001322 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001323 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001324
Liu Bo1f12bd02012-03-29 09:57:44 -04001325 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001326 spin_lock(&BTRFS_I(inode)->lock);
1327 BTRFS_I(inode)->outstanding_extents++;
1328 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001329 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001330 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001331 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001332 }
1333
1334
Liu Bo9e8a4a82012-09-05 19:10:51 -06001335 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001336 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001337
1338 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001339 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001340
1341 for (i = 0; i < i_done; i++) {
1342 clear_page_dirty_for_io(pages[i]);
1343 ClearPageChecked(pages[i]);
1344 set_page_extent_mapped(pages[i]);
1345 set_page_dirty(pages[i]);
1346 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001347 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001348 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001349 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1350 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001351 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001352 return i_done;
1353out:
1354 for (i = 0; i < i_done; i++) {
1355 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001356 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001357 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001358 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001359 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001360 page_cnt << PAGE_SHIFT, true);
1361 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1362 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001363 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001364 return ret;
1365
1366}
1367
1368int btrfs_defrag_file(struct inode *inode, struct file *file,
1369 struct btrfs_ioctl_defrag_range_args *range,
1370 u64 newer_than, unsigned long max_to_defrag)
1371{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001372 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001373 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001374 struct file_ra_state *ra = NULL;
1375 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001376 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001377 u64 last_len = 0;
1378 u64 skip = 0;
1379 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001380 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001381 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001382 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001383 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001384 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001385 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001386 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001387 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001388 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001389 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001390 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001391 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001392
Liu Bo0abd5b12013-04-16 09:20:28 +00001393 if (isize == 0)
1394 return 0;
1395
1396 if (range->start >= isize)
1397 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001398
David Sterba1e2ef462017-07-17 20:01:59 +02001399 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001400 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1401 return -EINVAL;
1402 if (range->compress_type)
1403 compress_type = range->compress_type;
1404 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001405
Liu Bo0abd5b12013-04-16 09:20:28 +00001406 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001407 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001408
Chris Mason4cb53002011-05-24 15:35:30 -04001409 /*
David Sterba0a52d102017-06-22 03:22:58 +02001410 * If we were not given a file, allocate a readahead context. As
1411 * readahead is just an optimization, defrag will work without it so
1412 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001413 */
1414 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001415 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001416 if (ra)
1417 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001418 } else {
1419 ra = &file->f_ra;
1420 }
1421
David Sterba63e727e2017-06-22 03:13:02 +02001422 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001423 if (!pages) {
1424 ret = -ENOMEM;
1425 goto out_ra;
1426 }
1427
1428 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001429 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001430 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001431 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001432 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001433 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001434 }
1435
Chris Mason4cb53002011-05-24 15:35:30 -04001436 if (newer_than) {
1437 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001438 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001439 if (!ret) {
1440 range->start = newer_off;
1441 /*
1442 * we always align our defrag to help keep
1443 * the extents in the file evenly spaced
1444 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001445 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001446 } else
1447 goto out_ra;
1448 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001449 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001450 }
1451 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301452 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001453
Li Zefan2a0f7f52011-10-10 15:43:34 -04001454 /*
1455 * make writeback starts from i, so the defrag range can be
1456 * written sequentially.
1457 */
1458 if (i < inode->i_mapping->writeback_index)
1459 inode->i_mapping->writeback_index = i;
1460
Chris Masonf7f43cc2011-10-11 11:41:40 -04001461 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001462 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001463 /*
1464 * make sure we stop running if someone unmounts
1465 * the FS
1466 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001467 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001468 break;
1469
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001470 if (btrfs_defrag_cancelled(fs_info)) {
1471 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001472 ret = -EAGAIN;
1473 break;
1474 }
1475
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001476 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001477 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001478 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001479 unsigned long next;
1480 /*
1481 * the should_defrag function tells us how much to skip
1482 * bump our counter by the suggested amount
1483 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001484 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001485 i = max(i + 1, next);
1486 continue;
1487 }
Li Zefan008873e2011-09-02 15:57:07 +08001488
1489 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001490 cluster = (PAGE_ALIGN(defrag_end) >>
1491 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001492 cluster = min(cluster, max_cluster);
1493 } else {
1494 cluster = max_cluster;
1495 }
1496
Li Zefan008873e2011-09-02 15:57:07 +08001497 if (i + cluster > ra_index) {
1498 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001499 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001500 page_cache_sync_readahead(inode->i_mapping, ra,
1501 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301502 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001503 }
Chris Mason940100a2010-03-10 10:52:59 -05001504
Al Viro59551022016-01-22 15:40:57 -05001505 inode_lock(inode);
David Sterba1e2ef462017-07-17 20:01:59 +02001506 if (do_compress)
David Sterbaeec63c62017-07-17 19:41:31 +02001507 BTRFS_I(inode)->defrag_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001508 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001509 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001510 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001511 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001512 }
Chris Mason940100a2010-03-10 10:52:59 -05001513
Chris Mason4cb53002011-05-24 15:35:30 -04001514 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001515 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001516 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001517
1518 if (newer_than) {
1519 if (newer_off == (u64)-1)
1520 break;
1521
Liu Boe1f041e2012-03-29 09:57:45 -04001522 if (ret > 0)
1523 i += ret;
1524
Chris Mason4cb53002011-05-24 15:35:30 -04001525 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001526 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001527
Byongho Leeee221842015-12-15 01:42:10 +09001528 ret = find_new_extents(root, inode, newer_than,
1529 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001530 if (!ret) {
1531 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001532 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001533 } else {
1534 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001535 }
Chris Mason4cb53002011-05-24 15:35:30 -04001536 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001537 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001538 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001539 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001540 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001541 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001542 last_len = 0;
1543 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001544 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001545 }
1546
Filipe Mananadec8ef92014-03-01 10:55:54 +00001547 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001548 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001549 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1550 &BTRFS_I(inode)->runtime_flags))
1551 filemap_flush(inode->i_mapping);
1552 }
Chris Mason1e701a32010-03-11 09:42:04 -05001553
Li Zefan1a419d82010-10-25 15:12:50 +08001554 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001555 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001556 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1557 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001558 }
1559
Diego Calleja60ccf822011-09-01 16:33:57 +02001560 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001561
Chris Mason4cb53002011-05-24 15:35:30 -04001562out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001563 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001564 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001565 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001566 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001567 }
Chris Mason4cb53002011-05-24 15:35:30 -04001568 if (!file)
1569 kfree(ra);
1570 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001571 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572}
1573
Miao Xie198605a2012-11-26 08:43:45 +00001574static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001575 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001576{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001577 struct inode *inode = file_inode(file);
1578 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001579 u64 new_size;
1580 u64 old_size;
1581 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001582 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001583 struct btrfs_ioctl_vol_args *vol_args;
1584 struct btrfs_trans_handle *trans;
1585 struct btrfs_device *device = NULL;
1586 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001587 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001588 char *devstr = NULL;
1589 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001590 int mod = 0;
1591
Chris Masone441d542009-01-05 16:57:23 -05001592 if (!capable(CAP_SYS_ADMIN))
1593 return -EPERM;
1594
Miao Xie198605a2012-11-26 08:43:45 +00001595 ret = mnt_want_write_file(file);
1596 if (ret)
1597 return ret;
1598
David Sterba171938e2017-03-28 14:44:21 +02001599 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001600 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001601 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001602 }
1603
Li Zefandae7b662009-04-08 15:06:54 +08001604 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001605 if (IS_ERR(vol_args)) {
1606 ret = PTR_ERR(vol_args);
1607 goto out;
1608 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001609
1610 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001611
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001612 sizestr = vol_args->name;
1613 devstr = strchr(sizestr, ':');
1614 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001615 sizestr = devstr + 1;
1616 *devstr = '\0';
1617 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001618 ret = kstrtoull(devstr, 10, &devid);
1619 if (ret)
1620 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001621 if (!devid) {
1622 ret = -EINVAL;
1623 goto out_free;
1624 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001625 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626 }
Miao Xiedba60f32012-12-21 09:19:51 +00001627
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001628 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001629 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001630 btrfs_info(fs_info, "resizer unable to find device %llu",
1631 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001632 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001633 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001634 }
Miao Xiedba60f32012-12-21 09:19:51 +00001635
Anand Jainebbede42017-12-04 12:54:52 +08001636 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001637 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001638 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001639 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001640 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001641 goto out_free;
1642 }
1643
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001644 if (!strcmp(sizestr, "max"))
1645 new_size = device->bdev->bd_inode->i_size;
1646 else {
1647 if (sizestr[0] == '-') {
1648 mod = -1;
1649 sizestr++;
1650 } else if (sizestr[0] == '+') {
1651 mod = 1;
1652 sizestr++;
1653 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001654 new_size = memparse(sizestr, &retptr);
1655 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001656 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001657 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001658 }
1659 }
1660
Anand Jain401e29c2017-12-04 12:54:55 +08001661 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001662 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001663 goto out_free;
1664 }
1665
Miao Xie7cc8e582014-09-03 21:35:38 +08001666 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001667
1668 if (mod < 0) {
1669 if (new_size > old_size) {
1670 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001671 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001672 }
1673 new_size = old_size - new_size;
1674 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001675 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001676 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001677 goto out_free;
1678 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001679 new_size = old_size + new_size;
1680 }
1681
Byongho Leeee221842015-12-15 01:42:10 +09001682 if (new_size < SZ_256M) {
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 if (new_size > device->bdev->bd_inode->i_size) {
1687 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001688 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001689 }
1690
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001691 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001692
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001693 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1694 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001695
1696 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001697 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001698 if (IS_ERR(trans)) {
1699 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001700 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001701 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001702 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001703 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001704 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001705 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001706 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001707
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001708out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001709 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001710out:
David Sterba171938e2017-03-28 14:44:21 +02001711 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001712 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001713 return ret;
1714}
1715
Sage Weil72fd0322010-10-29 15:41:32 -04001716static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001717 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001718 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001719 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001720{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001721 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001722 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001723
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001724 if (!S_ISDIR(file_inode(file)->i_mode))
1725 return -ENOTDIR;
1726
Liu Boa874a632012-06-29 03:58:46 -06001727 ret = mnt_want_write_file(file);
1728 if (ret)
1729 goto out;
1730
Sage Weil72fd0322010-10-29 15:41:32 -04001731 namelen = strlen(name);
1732 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001733 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001734 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001735 }
1736
Chris Mason16780ca2012-02-20 22:14:55 -05001737 if (name[0] == '.' &&
1738 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1739 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001740 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001741 }
1742
Chris Mason3de45862008-11-17 21:02:50 -05001743 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001744 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001745 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001746 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001747 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001748 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001749 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001750 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001751 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001752 }
1753
Al Viro496ad9a2013-01-23 17:07:38 -05001754 src_inode = file_inode(src.file);
1755 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001756 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001757 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001758 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001759 } else if (!inode_owner_or_capable(src_inode)) {
1760 /*
1761 * Subvolume creation is not restricted, but snapshots
1762 * are limited to own subvolumes only
1763 */
1764 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001765 } else {
1766 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1767 BTRFS_I(src_inode)->root,
1768 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001769 }
Al Viro2903ff02012-08-28 12:52:22 -04001770 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001771 }
Liu Boa874a632012-06-29 03:58:46 -06001772out_drop_write:
1773 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001774out:
Sage Weil72fd0322010-10-29 15:41:32 -04001775 return ret;
1776}
1777
1778static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001779 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001780{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001781 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001782 int ret;
1783
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001784 if (!S_ISDIR(file_inode(file)->i_mode))
1785 return -ENOTDIR;
1786
Li Zefanfa0d2b92010-12-20 15:53:28 +08001787 vol_args = memdup_user(arg, sizeof(*vol_args));
1788 if (IS_ERR(vol_args))
1789 return PTR_ERR(vol_args);
1790 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001791
Li Zefanfa0d2b92010-12-20 15:53:28 +08001792 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001793 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001794 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001795
Li Zefanfa0d2b92010-12-20 15:53:28 +08001796 kfree(vol_args);
1797 return ret;
1798}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001799
Li Zefanfa0d2b92010-12-20 15:53:28 +08001800static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1801 void __user *arg, int subvol)
1802{
1803 struct btrfs_ioctl_vol_args_v2 *vol_args;
1804 int ret;
1805 u64 transid = 0;
1806 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001807 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001808 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001809
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001810 if (!S_ISDIR(file_inode(file)->i_mode))
1811 return -ENOTDIR;
1812
Li Zefanfa0d2b92010-12-20 15:53:28 +08001813 vol_args = memdup_user(arg, sizeof(*vol_args));
1814 if (IS_ERR(vol_args))
1815 return PTR_ERR(vol_args);
1816 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001817
Li Zefanb83cc962010-12-20 16:04:08 +08001818 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001819 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1820 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001821 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001822 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001823 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001824
1825 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1826 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001827 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1828 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001829 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001830 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001831 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001832 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001833 }
1834 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1835 if (IS_ERR(inherit)) {
1836 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001837 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001838 }
1839 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001840
1841 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001842 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001843 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001844 if (ret)
1845 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001846
Dan Carpenterc47ca322014-09-04 14:09:15 +03001847 if (ptr && copy_to_user(arg +
1848 offsetof(struct btrfs_ioctl_vol_args_v2,
1849 transid),
1850 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001851 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001852
1853free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001854 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001855free_args:
1856 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001857 return ret;
1858}
1859
Li Zefan0caa1022010-12-20 16:30:25 +08001860static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1861 void __user *arg)
1862{
Al Viro496ad9a2013-01-23 17:07:38 -05001863 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001864 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001865 struct btrfs_root *root = BTRFS_I(inode)->root;
1866 int ret = 0;
1867 u64 flags = 0;
1868
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001869 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001870 return -EINVAL;
1871
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001872 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001873 if (btrfs_root_readonly(root))
1874 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001875 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001876
1877 if (copy_to_user(arg, &flags, sizeof(flags)))
1878 ret = -EFAULT;
1879
1880 return ret;
1881}
1882
1883static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1884 void __user *arg)
1885{
Al Viro496ad9a2013-01-23 17:07:38 -05001886 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001887 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001888 struct btrfs_root *root = BTRFS_I(inode)->root;
1889 struct btrfs_trans_handle *trans;
1890 u64 root_flags;
1891 u64 flags;
1892 int ret = 0;
1893
David Sterbabd60ea02014-01-16 15:50:22 +01001894 if (!inode_owner_or_capable(inode))
1895 return -EPERM;
1896
Liu Bob9ca0662012-06-29 03:58:49 -06001897 ret = mnt_want_write_file(file);
1898 if (ret)
1899 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001900
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001901 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001902 ret = -EINVAL;
1903 goto out_drop_write;
1904 }
Li Zefan0caa1022010-12-20 16:30:25 +08001905
Liu Bob9ca0662012-06-29 03:58:49 -06001906 if (copy_from_user(&flags, arg, sizeof(flags))) {
1907 ret = -EFAULT;
1908 goto out_drop_write;
1909 }
Li Zefan0caa1022010-12-20 16:30:25 +08001910
Liu Bob9ca0662012-06-29 03:58:49 -06001911 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1912 ret = -EINVAL;
1913 goto out_drop_write;
1914 }
Li Zefan0caa1022010-12-20 16:30:25 +08001915
Liu Bob9ca0662012-06-29 03:58:49 -06001916 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1917 ret = -EOPNOTSUPP;
1918 goto out_drop_write;
1919 }
Li Zefan0caa1022010-12-20 16:30:25 +08001920
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001921 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001922
1923 /* nothing to do */
1924 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001925 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001926
1927 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001928 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001929 btrfs_set_root_flags(&root->root_item,
1930 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001931 } else {
1932 /*
1933 * Block RO -> RW transition if this subvolume is involved in
1934 * send
1935 */
1936 spin_lock(&root->root_item_lock);
1937 if (root->send_in_progress == 0) {
1938 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001939 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001940 spin_unlock(&root->root_item_lock);
1941 } else {
1942 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001943 btrfs_warn(fs_info,
1944 "Attempt to set subvolume %llu read-write during send",
1945 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001946 ret = -EPERM;
1947 goto out_drop_sem;
1948 }
1949 }
Li Zefan0caa1022010-12-20 16:30:25 +08001950
1951 trans = btrfs_start_transaction(root, 1);
1952 if (IS_ERR(trans)) {
1953 ret = PTR_ERR(trans);
1954 goto out_reset;
1955 }
1956
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001957 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001958 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001959 if (ret < 0) {
1960 btrfs_end_transaction(trans);
1961 goto out_reset;
1962 }
Li Zefan0caa1022010-12-20 16:30:25 +08001963
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001964 ret = btrfs_commit_transaction(trans);
1965
Li Zefan0caa1022010-12-20 16:30:25 +08001966out_reset:
1967 if (ret)
1968 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001969out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001970 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001971out_drop_write:
1972 mnt_drop_write_file(file);
1973out:
Li Zefan0caa1022010-12-20 16:30:25 +08001974 return ret;
1975}
1976
Chris Masonac8e9812010-02-28 15:39:26 -05001977static noinline int key_in_sk(struct btrfs_key *key,
1978 struct btrfs_ioctl_search_key *sk)
1979{
Chris Masonabc6e132010-03-18 12:10:08 -04001980 struct btrfs_key test;
1981 int ret;
1982
1983 test.objectid = sk->min_objectid;
1984 test.type = sk->min_type;
1985 test.offset = sk->min_offset;
1986
1987 ret = btrfs_comp_cpu_keys(key, &test);
1988 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001989 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001990
1991 test.objectid = sk->max_objectid;
1992 test.type = sk->max_type;
1993 test.offset = sk->max_offset;
1994
1995 ret = btrfs_comp_cpu_keys(key, &test);
1996 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001997 return 0;
1998 return 1;
1999}
2000
Jeff Mahoneydf397562016-06-21 20:18:21 -04002001static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002002 struct btrfs_key *key,
2003 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002004 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002005 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002006 unsigned long *sk_offset,
2007 int *num_found)
2008{
2009 u64 found_transid;
2010 struct extent_buffer *leaf;
2011 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002012 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002013 unsigned long item_off;
2014 unsigned long item_len;
2015 int nritems;
2016 int i;
2017 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002018 int ret = 0;
2019
2020 leaf = path->nodes[0];
2021 slot = path->slots[0];
2022 nritems = btrfs_header_nritems(leaf);
2023
2024 if (btrfs_header_generation(leaf) > sk->max_transid) {
2025 i = nritems;
2026 goto advance_key;
2027 }
2028 found_transid = btrfs_header_generation(leaf);
2029
2030 for (i = slot; i < nritems; i++) {
2031 item_off = btrfs_item_ptr_offset(leaf, i);
2032 item_len = btrfs_item_size_nr(leaf, i);
2033
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002034 btrfs_item_key_to_cpu(leaf, key, i);
2035 if (!key_in_sk(key, sk))
2036 continue;
2037
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002038 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002039 if (*num_found) {
2040 ret = 1;
2041 goto out;
2042 }
Chris Masonac8e9812010-02-28 15:39:26 -05002043
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002044 /*
2045 * return one empty item back for v1, which does not
2046 * handle -EOVERFLOW
2047 */
2048
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002049 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002050 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002051 ret = -EOVERFLOW;
2052 }
Chris Masonac8e9812010-02-28 15:39:26 -05002053
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002054 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002055 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002056 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002057 }
2058
Chris Masonac8e9812010-02-28 15:39:26 -05002059 sh.objectid = key->objectid;
2060 sh.offset = key->offset;
2061 sh.type = key->type;
2062 sh.len = item_len;
2063 sh.transid = found_transid;
2064
2065 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002066 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2067 ret = -EFAULT;
2068 goto out;
2069 }
2070
Chris Masonac8e9812010-02-28 15:39:26 -05002071 *sk_offset += sizeof(sh);
2072
2073 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002074 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002075 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002076 if (read_extent_buffer_to_user(leaf, up,
2077 item_off, item_len)) {
2078 ret = -EFAULT;
2079 goto out;
2080 }
2081
Chris Masonac8e9812010-02-28 15:39:26 -05002082 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002083 }
Hugo Millse2156862011-05-14 17:43:41 +00002084 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002085
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002086 if (ret) /* -EOVERFLOW from above */
2087 goto out;
2088
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002089 if (*num_found >= sk->nr_items) {
2090 ret = 1;
2091 goto out;
2092 }
Chris Masonac8e9812010-02-28 15:39:26 -05002093 }
2094advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002095 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002096 test.objectid = sk->max_objectid;
2097 test.type = sk->max_type;
2098 test.offset = sk->max_offset;
2099 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2100 ret = 1;
2101 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002102 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002103 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002104 key->offset = 0;
2105 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002106 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002107 key->offset = 0;
2108 key->type = 0;
2109 key->objectid++;
2110 } else
2111 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002112out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002113 /*
2114 * 0: all items from this leaf copied, continue with next
2115 * 1: * more items can be copied, but unused buffer is too small
2116 * * all items were found
2117 * Either way, it will stops the loop which iterates to the next
2118 * leaf
2119 * -EOVERFLOW: item was to large for buffer
2120 * -EFAULT: could not copy extent buffer back to userspace
2121 */
Chris Masonac8e9812010-02-28 15:39:26 -05002122 return ret;
2123}
2124
2125static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002126 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002127 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002128 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002129{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002130 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002131 struct btrfs_root *root;
2132 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002133 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002134 int ret;
2135 int num_found = 0;
2136 unsigned long sk_offset = 0;
2137
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002138 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2139 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002140 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002141 }
Gerhard Heift12544442014-01-30 16:23:58 +01002142
Chris Masonac8e9812010-02-28 15:39:26 -05002143 path = btrfs_alloc_path();
2144 if (!path)
2145 return -ENOMEM;
2146
2147 if (sk->tree_id == 0) {
2148 /* search the root of the inode that was passed */
2149 root = BTRFS_I(inode)->root;
2150 } else {
2151 key.objectid = sk->tree_id;
2152 key.type = BTRFS_ROOT_ITEM_KEY;
2153 key.offset = (u64)-1;
2154 root = btrfs_read_fs_root_no_name(info, &key);
2155 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002156 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002157 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002158 }
2159 }
2160
2161 key.objectid = sk->min_objectid;
2162 key.type = sk->min_type;
2163 key.offset = sk->min_offset;
2164
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302165 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002166 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002167 if (ret != 0) {
2168 if (ret > 0)
2169 ret = 0;
2170 goto err;
2171 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002172 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002173 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002174 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002175 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002176 break;
2177
2178 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002179 if (ret > 0)
2180 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002181err:
2182 sk->nr_items = num_found;
2183 btrfs_free_path(path);
2184 return ret;
2185}
2186
2187static noinline int btrfs_ioctl_tree_search(struct file *file,
2188 void __user *argp)
2189{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002190 struct btrfs_ioctl_search_args __user *uargs;
2191 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002192 struct inode *inode;
2193 int ret;
2194 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002195
2196 if (!capable(CAP_SYS_ADMIN))
2197 return -EPERM;
2198
Gerhard Heiftba346b32014-01-30 16:24:02 +01002199 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002200
Gerhard Heiftba346b32014-01-30 16:24:02 +01002201 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2202 return -EFAULT;
2203
2204 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002205
Al Viro496ad9a2013-01-23 17:07:38 -05002206 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002207 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002208
2209 /*
2210 * In the origin implementation an overflow is handled by returning a
2211 * search header with a len of zero, so reset ret.
2212 */
2213 if (ret == -EOVERFLOW)
2214 ret = 0;
2215
Gerhard Heiftba346b32014-01-30 16:24:02 +01002216 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002217 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002218 return ret;
2219}
2220
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002221static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2222 void __user *argp)
2223{
2224 struct btrfs_ioctl_search_args_v2 __user *uarg;
2225 struct btrfs_ioctl_search_args_v2 args;
2226 struct inode *inode;
2227 int ret;
2228 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002229 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002230
2231 if (!capable(CAP_SYS_ADMIN))
2232 return -EPERM;
2233
2234 /* copy search header and buffer size */
2235 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2236 if (copy_from_user(&args, uarg, sizeof(args)))
2237 return -EFAULT;
2238
2239 buf_size = args.buf_size;
2240
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002241 /* limit result size to 16MB */
2242 if (buf_size > buf_limit)
2243 buf_size = buf_limit;
2244
2245 inode = file_inode(file);
2246 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002247 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002248 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2249 ret = -EFAULT;
2250 else if (ret == -EOVERFLOW &&
2251 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2252 ret = -EFAULT;
2253
Yan, Zheng76dda932009-09-21 16:00:26 -04002254 return ret;
2255}
2256
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002257/*
Chris Masonac8e9812010-02-28 15:39:26 -05002258 * Search INODE_REFs to identify path name of 'dirid' directory
2259 * in a 'tree_id' tree. and sets path name to 'name'.
2260 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002261static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2262 u64 tree_id, u64 dirid, char *name)
2263{
2264 struct btrfs_root *root;
2265 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002266 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002267 int ret = -1;
2268 int slot;
2269 int len;
2270 int total_len = 0;
2271 struct btrfs_inode_ref *iref;
2272 struct extent_buffer *l;
2273 struct btrfs_path *path;
2274
2275 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2276 name[0]='\0';
2277 return 0;
2278 }
2279
2280 path = btrfs_alloc_path();
2281 if (!path)
2282 return -ENOMEM;
2283
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002284 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002285
2286 key.objectid = tree_id;
2287 key.type = BTRFS_ROOT_ITEM_KEY;
2288 key.offset = (u64)-1;
2289 root = btrfs_read_fs_root_no_name(info, &key);
2290 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002291 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002292 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002293 }
2294
2295 key.objectid = dirid;
2296 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002297 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002298
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302299 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002300 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2301 if (ret < 0)
2302 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002303 else if (ret > 0) {
2304 ret = btrfs_previous_item(root, path, dirid,
2305 BTRFS_INODE_REF_KEY);
2306 if (ret < 0)
2307 goto out;
2308 else if (ret > 0) {
2309 ret = -ENOENT;
2310 goto out;
2311 }
2312 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002313
2314 l = path->nodes[0];
2315 slot = path->slots[0];
2316 btrfs_item_key_to_cpu(l, &key, slot);
2317
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002318 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2319 len = btrfs_inode_ref_name_len(l, iref);
2320 ptr -= len + 1;
2321 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002322 if (ptr < name) {
2323 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002324 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002325 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002326
2327 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302328 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002329
2330 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2331 break;
2332
David Sterbab3b4aa72011-04-21 01:20:15 +02002333 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002334 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002335 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002336 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002337 }
Li Zefan77906a502011-07-14 03:16:00 +00002338 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302339 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002340 ret = 0;
2341out:
2342 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002343 return ret;
2344}
2345
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002346static int btrfs_search_path_in_tree_user(struct inode *inode,
2347 struct btrfs_ioctl_ino_lookup_user_args *args)
2348{
2349 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2350 struct super_block *sb = inode->i_sb;
2351 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2352 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2353 u64 dirid = args->dirid;
2354 unsigned long item_off;
2355 unsigned long item_len;
2356 struct btrfs_inode_ref *iref;
2357 struct btrfs_root_ref *rref;
2358 struct btrfs_root *root;
2359 struct btrfs_path *path;
2360 struct btrfs_key key, key2;
2361 struct extent_buffer *leaf;
2362 struct inode *temp_inode;
2363 char *ptr;
2364 int slot;
2365 int len;
2366 int total_len = 0;
2367 int ret;
2368
2369 path = btrfs_alloc_path();
2370 if (!path)
2371 return -ENOMEM;
2372
2373 /*
2374 * If the bottom subvolume does not exist directly under upper_limit,
2375 * construct the path in from the bottom up.
2376 */
2377 if (dirid != upper_limit.objectid) {
2378 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2379
2380 key.objectid = treeid;
2381 key.type = BTRFS_ROOT_ITEM_KEY;
2382 key.offset = (u64)-1;
2383 root = btrfs_read_fs_root_no_name(fs_info, &key);
2384 if (IS_ERR(root)) {
2385 ret = PTR_ERR(root);
2386 goto out;
2387 }
2388
2389 key.objectid = dirid;
2390 key.type = BTRFS_INODE_REF_KEY;
2391 key.offset = (u64)-1;
2392 while (1) {
2393 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2394 if (ret < 0) {
2395 goto out;
2396 } else if (ret > 0) {
2397 ret = btrfs_previous_item(root, path, dirid,
2398 BTRFS_INODE_REF_KEY);
2399 if (ret < 0) {
2400 goto out;
2401 } else if (ret > 0) {
2402 ret = -ENOENT;
2403 goto out;
2404 }
2405 }
2406
2407 leaf = path->nodes[0];
2408 slot = path->slots[0];
2409 btrfs_item_key_to_cpu(leaf, &key, slot);
2410
2411 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2412 len = btrfs_inode_ref_name_len(leaf, iref);
2413 ptr -= len + 1;
2414 total_len += len + 1;
2415 if (ptr < args->path) {
2416 ret = -ENAMETOOLONG;
2417 goto out;
2418 }
2419
2420 *(ptr + len) = '/';
2421 read_extent_buffer(leaf, ptr,
2422 (unsigned long)(iref + 1), len);
2423
2424 /* Check the read+exec permission of this directory */
2425 ret = btrfs_previous_item(root, path, dirid,
2426 BTRFS_INODE_ITEM_KEY);
2427 if (ret < 0) {
2428 goto out;
2429 } else if (ret > 0) {
2430 ret = -ENOENT;
2431 goto out;
2432 }
2433
2434 leaf = path->nodes[0];
2435 slot = path->slots[0];
2436 btrfs_item_key_to_cpu(leaf, &key2, slot);
2437 if (key2.objectid != dirid) {
2438 ret = -ENOENT;
2439 goto out;
2440 }
2441
2442 temp_inode = btrfs_iget(sb, &key2, root, NULL);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002443 if (IS_ERR(temp_inode)) {
2444 ret = PTR_ERR(temp_inode);
2445 goto out;
2446 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002447 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2448 iput(temp_inode);
2449 if (ret) {
2450 ret = -EACCES;
2451 goto out;
2452 }
2453
2454 if (key.offset == upper_limit.objectid)
2455 break;
2456 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2457 ret = -EACCES;
2458 goto out;
2459 }
2460
2461 btrfs_release_path(path);
2462 key.objectid = key.offset;
2463 key.offset = (u64)-1;
2464 dirid = key.objectid;
2465 }
2466
2467 memmove(args->path, ptr, total_len);
2468 args->path[total_len] = '\0';
2469 btrfs_release_path(path);
2470 }
2471
2472 /* Get the bottom subvolume's name from ROOT_REF */
2473 root = fs_info->tree_root;
2474 key.objectid = treeid;
2475 key.type = BTRFS_ROOT_REF_KEY;
2476 key.offset = args->treeid;
2477 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2478 if (ret < 0) {
2479 goto out;
2480 } else if (ret > 0) {
2481 ret = -ENOENT;
2482 goto out;
2483 }
2484
2485 leaf = path->nodes[0];
2486 slot = path->slots[0];
2487 btrfs_item_key_to_cpu(leaf, &key, slot);
2488
2489 item_off = btrfs_item_ptr_offset(leaf, slot);
2490 item_len = btrfs_item_size_nr(leaf, slot);
2491 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2492 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2493 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2494 ret = -EINVAL;
2495 goto out;
2496 }
2497
2498 /* Copy subvolume's name */
2499 item_off += sizeof(struct btrfs_root_ref);
2500 item_len -= sizeof(struct btrfs_root_ref);
2501 read_extent_buffer(leaf, args->name, item_off, item_len);
2502 args->name[item_len] = 0;
2503
2504out:
2505 btrfs_free_path(path);
2506 return ret;
2507}
2508
Chris Masonac8e9812010-02-28 15:39:26 -05002509static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2510 void __user *argp)
2511{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002512 struct btrfs_ioctl_ino_lookup_args *args;
2513 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002514 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002515
Julia Lawall2354d08f2010-10-29 15:14:18 -04002516 args = memdup_user(argp, sizeof(*args));
2517 if (IS_ERR(args))
2518 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002519
Al Viro496ad9a2013-01-23 17:07:38 -05002520 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002521
David Sterba01b810b2015-05-12 19:14:49 +02002522 /*
2523 * Unprivileged query to obtain the containing subvolume root id. The
2524 * path is reset so it's consistent with btrfs_search_path_in_tree.
2525 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002526 if (args->treeid == 0)
2527 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2528
David Sterba01b810b2015-05-12 19:14:49 +02002529 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2530 args->name[0] = 0;
2531 goto out;
2532 }
2533
2534 if (!capable(CAP_SYS_ADMIN)) {
2535 ret = -EPERM;
2536 goto out;
2537 }
2538
Chris Masonac8e9812010-02-28 15:39:26 -05002539 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2540 args->treeid, args->objectid,
2541 args->name);
2542
David Sterba01b810b2015-05-12 19:14:49 +02002543out:
Chris Masonac8e9812010-02-28 15:39:26 -05002544 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2545 ret = -EFAULT;
2546
2547 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002548 return ret;
2549}
2550
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002551/*
2552 * Version of ino_lookup ioctl (unprivileged)
2553 *
2554 * The main differences from ino_lookup ioctl are:
2555 *
2556 * 1. Read + Exec permission will be checked using inode_permission() during
2557 * path construction. -EACCES will be returned in case of failure.
2558 * 2. Path construction will be stopped at the inode number which corresponds
2559 * to the fd with which this ioctl is called. If constructed path does not
2560 * exist under fd's inode, -EACCES will be returned.
2561 * 3. The name of bottom subvolume is also searched and filled.
2562 */
2563static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2564{
2565 struct btrfs_ioctl_ino_lookup_user_args *args;
2566 struct inode *inode;
2567 int ret;
2568
2569 args = memdup_user(argp, sizeof(*args));
2570 if (IS_ERR(args))
2571 return PTR_ERR(args);
2572
2573 inode = file_inode(file);
2574
2575 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2576 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2577 /*
2578 * The subvolume does not exist under fd with which this is
2579 * called
2580 */
2581 kfree(args);
2582 return -EACCES;
2583 }
2584
2585 ret = btrfs_search_path_in_tree_user(inode, args);
2586
2587 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2588 ret = -EFAULT;
2589
2590 kfree(args);
2591 return ret;
2592}
2593
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002594/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2595static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2596{
2597 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2598 struct btrfs_fs_info *fs_info;
2599 struct btrfs_root *root;
2600 struct btrfs_path *path;
2601 struct btrfs_key key;
2602 struct btrfs_root_item *root_item;
2603 struct btrfs_root_ref *rref;
2604 struct extent_buffer *leaf;
2605 unsigned long item_off;
2606 unsigned long item_len;
2607 struct inode *inode;
2608 int slot;
2609 int ret = 0;
2610
2611 path = btrfs_alloc_path();
2612 if (!path)
2613 return -ENOMEM;
2614
2615 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2616 if (!subvol_info) {
2617 btrfs_free_path(path);
2618 return -ENOMEM;
2619 }
2620
2621 inode = file_inode(file);
2622 fs_info = BTRFS_I(inode)->root->fs_info;
2623
2624 /* Get root_item of inode's subvolume */
2625 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2626 key.type = BTRFS_ROOT_ITEM_KEY;
2627 key.offset = (u64)-1;
2628 root = btrfs_read_fs_root_no_name(fs_info, &key);
2629 if (IS_ERR(root)) {
2630 ret = PTR_ERR(root);
2631 goto out;
2632 }
2633 root_item = &root->root_item;
2634
2635 subvol_info->treeid = key.objectid;
2636
2637 subvol_info->generation = btrfs_root_generation(root_item);
2638 subvol_info->flags = btrfs_root_flags(root_item);
2639
2640 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2641 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2642 BTRFS_UUID_SIZE);
2643 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2644 BTRFS_UUID_SIZE);
2645
2646 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2647 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2648 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2649
2650 subvol_info->otransid = btrfs_root_otransid(root_item);
2651 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2652 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2653
2654 subvol_info->stransid = btrfs_root_stransid(root_item);
2655 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2656 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2657
2658 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2659 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2660 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2661
2662 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2663 /* Search root tree for ROOT_BACKREF of this subvolume */
2664 root = fs_info->tree_root;
2665
2666 key.type = BTRFS_ROOT_BACKREF_KEY;
2667 key.offset = 0;
2668 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2669 if (ret < 0) {
2670 goto out;
2671 } else if (path->slots[0] >=
2672 btrfs_header_nritems(path->nodes[0])) {
2673 ret = btrfs_next_leaf(root, path);
2674 if (ret < 0) {
2675 goto out;
2676 } else if (ret > 0) {
2677 ret = -EUCLEAN;
2678 goto out;
2679 }
2680 }
2681
2682 leaf = path->nodes[0];
2683 slot = path->slots[0];
2684 btrfs_item_key_to_cpu(leaf, &key, slot);
2685 if (key.objectid == subvol_info->treeid &&
2686 key.type == BTRFS_ROOT_BACKREF_KEY) {
2687 subvol_info->parent_id = key.offset;
2688
2689 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2690 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2691
2692 item_off = btrfs_item_ptr_offset(leaf, slot)
2693 + sizeof(struct btrfs_root_ref);
2694 item_len = btrfs_item_size_nr(leaf, slot)
2695 - sizeof(struct btrfs_root_ref);
2696 read_extent_buffer(leaf, subvol_info->name,
2697 item_off, item_len);
2698 } else {
2699 ret = -ENOENT;
2700 goto out;
2701 }
2702 }
2703
2704 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2705 ret = -EFAULT;
2706
2707out:
2708 btrfs_free_path(path);
2709 kzfree(subvol_info);
2710 return ret;
2711}
2712
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002713/*
2714 * Return ROOT_REF information of the subvolume containing this inode
2715 * except the subvolume name.
2716 */
2717static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2718{
2719 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2720 struct btrfs_root_ref *rref;
2721 struct btrfs_root *root;
2722 struct btrfs_path *path;
2723 struct btrfs_key key;
2724 struct extent_buffer *leaf;
2725 struct inode *inode;
2726 u64 objectid;
2727 int slot;
2728 int ret;
2729 u8 found;
2730
2731 path = btrfs_alloc_path();
2732 if (!path)
2733 return -ENOMEM;
2734
2735 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2736 if (IS_ERR(rootrefs)) {
2737 btrfs_free_path(path);
2738 return PTR_ERR(rootrefs);
2739 }
2740
2741 inode = file_inode(file);
2742 root = BTRFS_I(inode)->root->fs_info->tree_root;
2743 objectid = BTRFS_I(inode)->root->root_key.objectid;
2744
2745 key.objectid = objectid;
2746 key.type = BTRFS_ROOT_REF_KEY;
2747 key.offset = rootrefs->min_treeid;
2748 found = 0;
2749
2750 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2751 if (ret < 0) {
2752 goto out;
2753 } else if (path->slots[0] >=
2754 btrfs_header_nritems(path->nodes[0])) {
2755 ret = btrfs_next_leaf(root, path);
2756 if (ret < 0) {
2757 goto out;
2758 } else if (ret > 0) {
2759 ret = -EUCLEAN;
2760 goto out;
2761 }
2762 }
2763 while (1) {
2764 leaf = path->nodes[0];
2765 slot = path->slots[0];
2766
2767 btrfs_item_key_to_cpu(leaf, &key, slot);
2768 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2769 ret = 0;
2770 goto out;
2771 }
2772
2773 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2774 ret = -EOVERFLOW;
2775 goto out;
2776 }
2777
2778 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2779 rootrefs->rootref[found].treeid = key.offset;
2780 rootrefs->rootref[found].dirid =
2781 btrfs_root_ref_dirid(leaf, rref);
2782 found++;
2783
2784 ret = btrfs_next_item(root, path);
2785 if (ret < 0) {
2786 goto out;
2787 } else if (ret > 0) {
2788 ret = -EUCLEAN;
2789 goto out;
2790 }
2791 }
2792
2793out:
2794 if (!ret || ret == -EOVERFLOW) {
2795 rootrefs->num_items = found;
2796 /* update min_treeid for next search */
2797 if (found)
2798 rootrefs->min_treeid =
2799 rootrefs->rootref[found - 1].treeid + 1;
2800 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2801 ret = -EFAULT;
2802 }
2803
2804 kfree(rootrefs);
2805 btrfs_free_path(path);
2806
2807 return ret;
2808}
2809
Yan, Zheng76dda932009-09-21 16:00:26 -04002810static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2811 void __user *arg)
2812{
Al Viro54563d42013-09-01 15:57:51 -04002813 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002814 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002815 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002816 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002817 struct inode *inode;
2818 struct btrfs_root *root = BTRFS_I(dir)->root;
2819 struct btrfs_root *dest = NULL;
2820 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002821 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002822 int err = 0;
2823
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002824 if (!S_ISDIR(dir->i_mode))
2825 return -ENOTDIR;
2826
Yan, Zheng76dda932009-09-21 16:00:26 -04002827 vol_args = memdup_user(arg, sizeof(*vol_args));
2828 if (IS_ERR(vol_args))
2829 return PTR_ERR(vol_args);
2830
2831 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2832 namelen = strlen(vol_args->name);
2833 if (strchr(vol_args->name, '/') ||
2834 strncmp(vol_args->name, "..", namelen) == 0) {
2835 err = -EINVAL;
2836 goto out;
2837 }
2838
Al Viroa561be72011-11-23 11:57:51 -05002839 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002840 if (err)
2841 goto out;
2842
David Sterba521e0542014-04-15 16:41:44 +02002843
Al Viro00235412016-05-26 00:05:12 -04002844 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2845 if (err == -EINTR)
2846 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002847 dentry = lookup_one_len(vol_args->name, parent, namelen);
2848 if (IS_ERR(dentry)) {
2849 err = PTR_ERR(dentry);
2850 goto out_unlock_dir;
2851 }
2852
David Howells2b0143b2015-03-17 22:25:59 +00002853 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002854 err = -ENOENT;
2855 goto out_dput;
2856 }
2857
David Howells2b0143b2015-03-17 22:25:59 +00002858 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002859 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302860 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002861 /*
2862 * Regular user. Only allow this with a special mount
2863 * option, when the user has write+exec access to the
2864 * subvol root, and when rmdir(2) would have been
2865 * allowed.
2866 *
2867 * Note that this is _not_ check that the subvol is
2868 * empty or doesn't contain data that we wouldn't
2869 * otherwise be able to delete.
2870 *
2871 * Users who want to delete empty subvols should try
2872 * rmdir(2).
2873 */
2874 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002876 goto out_dput;
2877
2878 /*
2879 * Do not allow deletion if the parent dir is the same
2880 * as the dir to be deleted. That means the ioctl
2881 * must be called on the dentry referencing the root
2882 * of the subvol, not a random directory contained
2883 * within it.
2884 */
2885 err = -EINVAL;
2886 if (root == dest)
2887 goto out_dput;
2888
2889 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2890 if (err)
2891 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002892 }
2893
Miao Xie5c39da52012-10-22 11:39:53 +00002894 /* check if subvolume may be deleted by a user */
2895 err = btrfs_may_delete(dir, dentry, 1);
2896 if (err)
2897 goto out_dput;
2898
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002899 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002900 err = -EINVAL;
2901 goto out_dput;
2902 }
2903
Al Viro59551022016-01-22 15:40:57 -05002904 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002905 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002906 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002907 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002908 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002909
Yan, Zheng76dda932009-09-21 16:00:26 -04002910out_dput:
2911 dput(dentry);
2912out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002913 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002914out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002915 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002916out:
2917 kfree(vol_args);
2918 return err;
2919}
2920
Chris Mason1e701a32010-03-11 09:42:04 -05002921static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002922{
Al Viro496ad9a2013-01-23 17:07:38 -05002923 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002924 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002925 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002926 int ret;
2927
Ilya Dryomov25122d12013-01-20 15:57:57 +02002928 ret = mnt_want_write_file(file);
2929 if (ret)
2930 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002931
Ilya Dryomov25122d12013-01-20 15:57:57 +02002932 if (btrfs_root_readonly(root)) {
2933 ret = -EROFS;
2934 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002935 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002936
2937 switch (inode->i_mode & S_IFMT) {
2938 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002939 if (!capable(CAP_SYS_ADMIN)) {
2940 ret = -EPERM;
2941 goto out;
2942 }
Eric Sandeende78b512013-01-31 18:21:12 +00002943 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002944 break;
2945 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02002946 /*
2947 * Note that this does not check the file descriptor for write
2948 * access. This prevents defragmenting executables that are
2949 * running and allows defrag on files open in read-only mode.
2950 */
2951 if (!capable(CAP_SYS_ADMIN) &&
2952 inode_permission(inode, MAY_WRITE)) {
2953 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05002954 goto out;
2955 }
Chris Mason1e701a32010-03-11 09:42:04 -05002956
2957 range = kzalloc(sizeof(*range), GFP_KERNEL);
2958 if (!range) {
2959 ret = -ENOMEM;
2960 goto out;
2961 }
2962
2963 if (argp) {
2964 if (copy_from_user(range, argp,
2965 sizeof(*range))) {
2966 ret = -EFAULT;
2967 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002968 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002969 }
2970 /* compression requires us to start the IO */
2971 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2972 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2973 range->extent_thresh = (u32)-1;
2974 }
2975 } else {
2976 /* the rest are all set to zero by kzalloc */
2977 range->len = (u64)-1;
2978 }
Al Viro496ad9a2013-01-23 17:07:38 -05002979 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002980 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002981 if (ret > 0)
2982 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002983 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002984 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002985 default:
2986 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002987 }
Chris Masone441d542009-01-05 16:57:23 -05002988out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002989 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002990 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002991}
2992
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002993static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002994{
2995 struct btrfs_ioctl_vol_args *vol_args;
2996 int ret;
2997
Chris Masone441d542009-01-05 16:57:23 -05002998 if (!capable(CAP_SYS_ADMIN))
2999 return -EPERM;
3000
David Sterba171938e2017-03-28 14:44:21 +02003001 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003002 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003003
Li Zefandae7b662009-04-08 15:06:54 +08003004 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003005 if (IS_ERR(vol_args)) {
3006 ret = PTR_ERR(vol_args);
3007 goto out;
3008 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003009
Mark Fasheh5516e592008-07-24 12:20:14 -04003010 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003011 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003012
Anand Jain43d20762014-07-01 00:58:56 +08003013 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003014 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003015
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003016 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003017out:
David Sterba171938e2017-03-28 14:44:21 +02003018 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003019 return ret;
3020}
3021
Anand Jain6b526ed2016-02-13 10:01:39 +08003022static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003023{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003024 struct inode *inode = file_inode(file);
3025 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003026 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003027 int ret;
3028
Chris Masone441d542009-01-05 16:57:23 -05003029 if (!capable(CAP_SYS_ADMIN))
3030 return -EPERM;
3031
Miao Xieda249272012-11-26 08:44:50 +00003032 ret = mnt_want_write_file(file);
3033 if (ret)
3034 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003035
Li Zefandae7b662009-04-08 15:06:54 +08003036 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003037 if (IS_ERR(vol_args)) {
3038 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003039 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003040 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003041
Anand Jain6b526ed2016-02-13 10:01:39 +08003042 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003043 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3044 ret = -EOPNOTSUPP;
3045 goto out;
3046 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003047
David Sterba171938e2017-03-28 14:44:21 +02003048 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003049 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3050 goto out;
3051 }
3052
David Sterba735654e2016-02-15 18:15:21 +01003053 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003054 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003055 } else {
3056 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003057 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003058 }
David Sterba171938e2017-03-28 14:44:21 +02003059 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003060
Anand Jain6b526ed2016-02-13 10:01:39 +08003061 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003062 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003063 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003064 vol_args->devid);
3065 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003066 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003067 vol_args->name);
3068 }
Anand Jain183860f2013-05-17 10:52:45 +00003069out:
3070 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003071err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003072 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003073 return ret;
3074}
3075
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003076static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3077{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003078 struct inode *inode = file_inode(file);
3079 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003080 struct btrfs_ioctl_vol_args *vol_args;
3081 int ret;
3082
3083 if (!capable(CAP_SYS_ADMIN))
3084 return -EPERM;
3085
3086 ret = mnt_want_write_file(file);
3087 if (ret)
3088 return ret;
3089
David Sterba171938e2017-03-28 14:44:21 +02003090 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003091 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003092 goto out_drop_write;
3093 }
3094
3095 vol_args = memdup_user(arg, sizeof(*vol_args));
3096 if (IS_ERR(vol_args)) {
3097 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003098 goto out;
3099 }
3100
David Sterba58d7bbf2016-05-04 14:10:47 +02003101 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003102 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003103
3104 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003105 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003106 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003107out:
David Sterba171938e2017-03-28 14:44:21 +02003108 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003109out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003110 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003111
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003112 return ret;
3113}
3114
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003115static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3116 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003117{
Li Zefan027ed2f2011-06-08 08:27:56 +00003118 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003119 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003120 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003121 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003122
Li Zefan027ed2f2011-06-08 08:27:56 +00003123 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3124 if (!fi_args)
3125 return -ENOMEM;
3126
David Sterbad03262c2017-06-16 00:09:21 +02003127 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003128 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003129
David Sterbad03262c2017-06-16 00:09:21 +02003130 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003131 if (device->devid > fi_args->max_id)
3132 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003133 }
David Sterbad03262c2017-06-16 00:09:21 +02003134 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003135
David Sterbad03262c2017-06-16 00:09:21 +02003136 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003137 fi_args->nodesize = fs_info->nodesize;
3138 fi_args->sectorsize = fs_info->sectorsize;
3139 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003140
Li Zefan027ed2f2011-06-08 08:27:56 +00003141 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3142 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003143
Li Zefan027ed2f2011-06-08 08:27:56 +00003144 kfree(fi_args);
3145 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003146}
3147
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003148static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3149 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003150{
3151 struct btrfs_ioctl_dev_info_args *di_args;
3152 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003153 int ret = 0;
3154 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003155
Jan Schmidt475f6382011-03-11 15:41:01 +01003156 di_args = memdup_user(arg, sizeof(*di_args));
3157 if (IS_ERR(di_args))
3158 return PTR_ERR(di_args);
3159
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003160 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003161 s_uuid = di_args->uuid;
3162
David Sterbac5593ca2017-06-16 00:09:21 +02003163 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003164 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01003165
3166 if (!dev) {
3167 ret = -ENODEV;
3168 goto out;
3169 }
3170
3171 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003172 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3173 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003174 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003175 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003176 strncpy(di_args->path, rcu_str_deref(dev->name),
3177 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003178 di_args->path[sizeof(di_args->path) - 1] = 0;
3179 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003180 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003181 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003182
3183out:
David Sterbac5593ca2017-06-16 00:09:21 +02003184 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003185 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3186 ret = -EFAULT;
3187
3188 kfree(di_args);
3189 return ret;
3190}
3191
Mark Fashehf4414602015-06-30 14:42:05 -07003192static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07003193{
3194 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07003195
Mark Fasheh416161d2013-08-06 11:42:51 -07003196 page = grab_cache_page(inode->i_mapping, index);
3197 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00003198 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07003199
3200 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00003201 int ret;
3202
3203 ret = btrfs_readpage(NULL, page);
3204 if (ret)
3205 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07003206 lock_page(page);
3207 if (!PageUptodate(page)) {
3208 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003209 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00003210 return ERR_PTR(-EIO);
3211 }
3212 if (page->mapping != inode->i_mapping) {
3213 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003214 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00003215 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07003216 }
3217 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003218
3219 return page;
3220}
3221
Mark Fashehf4414602015-06-30 14:42:05 -07003222static int gather_extent_pages(struct inode *inode, struct page **pages,
3223 int num_pages, u64 off)
3224{
3225 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003226 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003227
3228 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00003229again:
Mark Fashehf4414602015-06-30 14:42:05 -07003230 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00003231 if (IS_ERR(pages[i])) {
3232 int err = PTR_ERR(pages[i]);
3233
3234 if (err == -EAGAIN)
3235 goto again;
3236 pages[i] = NULL;
3237 return err;
3238 }
Mark Fashehf4414602015-06-30 14:42:05 -07003239 }
3240 return 0;
3241}
3242
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003243static int lock_extent_range(struct inode *inode, u64 off, u64 len,
3244 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003245{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003246 /*
3247 * Do any pending delalloc/csum calculations on inode, one way or
3248 * another, and lock file content.
3249 * The locking order is:
3250 *
3251 * 1) pages
3252 * 2) range in the inode's io tree
3253 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003254 while (1) {
3255 struct btrfs_ordered_extent *ordered;
3256 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
3257 ordered = btrfs_lookup_first_ordered_extent(inode,
3258 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003259 if ((!ordered ||
3260 ordered->file_offset + ordered->len <= off ||
3261 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003262 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003263 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
3264 if (ordered)
3265 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003266 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003267 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003268 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
3269 if (ordered)
3270 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003271 if (!retry_range_locking)
3272 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003273 btrfs_wait_ordered_range(inode, off, len);
3274 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003275 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003276}
3277
Mark Fashehf4414602015-06-30 14:42:05 -07003278static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07003279{
Al Viro59551022016-01-22 15:40:57 -05003280 inode_unlock(inode1);
3281 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07003282}
3283
Mark Fashehf4414602015-06-30 14:42:05 -07003284static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
3285{
3286 if (inode1 < inode2)
3287 swap(inode1, inode2);
3288
Al Viro59551022016-01-22 15:40:57 -05003289 inode_lock_nested(inode1, I_MUTEX_PARENT);
3290 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07003291}
3292
3293static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3294 struct inode *inode2, u64 loff2, u64 len)
3295{
3296 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3297 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3298}
3299
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003300static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3301 struct inode *inode2, u64 loff2, u64 len,
3302 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07003303{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003304 int ret;
3305
Mark Fasheh416161d2013-08-06 11:42:51 -07003306 if (inode1 < inode2) {
3307 swap(inode1, inode2);
3308 swap(loff1, loff2);
3309 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003310 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
3311 if (ret)
3312 return ret;
3313 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
3314 if (ret)
3315 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
3316 loff1 + len - 1);
3317 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003318}
3319
3320struct cmp_pages {
3321 int num_pages;
3322 struct page **src_pages;
3323 struct page **dst_pages;
3324};
3325
3326static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3327{
3328 int i;
3329 struct page *pg;
3330
3331 for (i = 0; i < cmp->num_pages; i++) {
3332 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003333 if (pg) {
3334 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003335 put_page(pg);
Naohiro Aota97b19172018-07-13 23:07:20 +09003336 cmp->src_pages[i] = NULL;
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003337 }
Mark Fashehf4414602015-06-30 14:42:05 -07003338 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003339 if (pg) {
3340 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003341 put_page(pg);
Naohiro Aota97b19172018-07-13 23:07:20 +09003342 cmp->dst_pages[i] = NULL;
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003343 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003344 }
Mark Fashehf4414602015-06-30 14:42:05 -07003345}
3346
3347static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3348 struct inode *dst, u64 dst_loff,
3349 u64 len, struct cmp_pages *cmp)
3350{
3351 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003352 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003353
Mark Fashehf4414602015-06-30 14:42:05 -07003354 cmp->num_pages = num_pages;
Mark Fashehf4414602015-06-30 14:42:05 -07003355
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003356 ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003357 if (ret)
3358 goto out;
3359
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003360 ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003361
3362out:
3363 if (ret)
3364 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09003365 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003366}
3367
David Sterba1a287cf2017-02-10 20:15:10 +01003368static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003369{
3370 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003371 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003372 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003373 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003374 void *addr, *dst_addr;
3375
Mark Fashehf4414602015-06-30 14:42:05 -07003376 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003377 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003378 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003379 cmp_len = len;
3380
Mark Fashehf4414602015-06-30 14:42:05 -07003381 BUG_ON(i >= cmp->num_pages);
3382
3383 src_page = cmp->src_pages[i];
3384 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003385 ASSERT(PageLocked(src_page));
3386 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003387
Mark Fasheh416161d2013-08-06 11:42:51 -07003388 addr = kmap_atomic(src_page);
3389 dst_addr = kmap_atomic(dst_page);
3390
3391 flush_dcache_page(src_page);
3392 flush_dcache_page(dst_page);
3393
3394 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003395 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003396
3397 kunmap_atomic(addr);
3398 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003399
3400 if (ret)
3401 break;
3402
Mark Fasheh416161d2013-08-06 11:42:51 -07003403 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003404 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003405 }
3406
3407 return ret;
3408}
3409
Mark Fashehe1d227a2015-06-08 15:05:25 -07003410static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3411 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003412{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003413 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003414 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3415
Mark Fashehe1d227a2015-06-08 15:05:25 -07003416 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003417 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003418
3419 /* if we extend to eof, continue to block boundary */
3420 if (off + len == inode->i_size)
3421 *plen = len = ALIGN(inode->i_size, bs) - off;
3422
Mark Fasheh416161d2013-08-06 11:42:51 -07003423 /* Check that we are block aligned - btrfs_clone() requires this */
3424 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3425 return -EINVAL;
3426
3427 return 0;
3428}
3429
Timofey Titovets39739092018-05-02 08:15:36 +03003430static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003431 struct inode *dst, u64 dst_loff,
3432 struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003433{
3434 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003435 u64 len = olen;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003436 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003437 u64 same_lock_start = 0;
3438 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003439
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003440 ret = extent_same_check_offsets(src, loff, &len, olen);
3441 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003442 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003443
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003444 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3445 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003446 return ret;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003447
3448 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003449 /*
3450 * Single inode case wants the same checks, except we
3451 * don't want our length pushed out past i_size as
3452 * comparing that data range makes no sense.
3453 *
3454 * extent_same_check_offsets() will do this for an
3455 * unaligned length at i_size, so catch it here and
3456 * reject the request.
3457 *
3458 * This effectively means we require aligned extents
3459 * for the single-inode case, whereas the other cases
3460 * allow an unaligned length so long as it ends at
3461 * i_size.
3462 */
Timofey Titovets39739092018-05-02 08:15:36 +03003463 if (len != olen)
3464 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003465
3466 /* Check for overlapping ranges */
Timofey Titovets39739092018-05-02 08:15:36 +03003467 if (dst_loff + len > loff && dst_loff < loff + len)
3468 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003469
3470 same_lock_start = min_t(u64, loff, dst_loff);
3471 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Filipe Mananade02b9f2018-08-17 09:38:59 +01003472 } else {
3473 /*
3474 * If the source and destination inodes are different, the
3475 * source's range end offset matches the source's i_size, that
3476 * i_size is not a multiple of the sector size, and the
3477 * destination range does not go past the destination's i_size,
3478 * we must round down the length to the nearest sector size
3479 * multiple. If we don't do this adjustment we end replacing
3480 * with zeroes the bytes in the range that starts at the
3481 * deduplication range's end offset and ends at the next sector
3482 * size multiple.
3483 */
3484 if (loff + olen == i_size_read(src) &&
3485 dst_loff + len < i_size_read(dst)) {
3486 const u64 sz = BTRFS_I(src)->root->fs_info->sectorsize;
3487
3488 len = round_down(i_size_read(src), sz) - loff;
3489 olen = len;
3490 }
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003491 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003492
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003493again:
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003494 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp);
Mark Fashehf4414602015-06-30 14:42:05 -07003495 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003496 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003497
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003498 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003499 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3500 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003501 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003502 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3503 false);
3504 /*
3505 * If one of the inodes has dirty pages in the respective range or
3506 * ordered extents, we need to flush dellaloc and wait for all ordered
3507 * extents in the range. We must unlock the pages and the ranges in the
3508 * io trees to avoid deadlocks when flushing delalloc (requires locking
3509 * pages) and when waiting for ordered extents to complete (they require
3510 * range locking).
3511 */
3512 if (ret == -EAGAIN) {
3513 /*
3514 * Ranges in the io trees already unlocked. Now unlock all
3515 * pages before waiting for all IO to complete.
3516 */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003517 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003518 if (same_inode) {
3519 btrfs_wait_ordered_range(src, same_lock_start,
3520 same_lock_len);
3521 } else {
3522 btrfs_wait_ordered_range(src, loff, len);
3523 btrfs_wait_ordered_range(dst, dst_loff, len);
3524 }
3525 goto again;
3526 }
3527 ASSERT(ret == 0);
3528 if (WARN_ON(ret)) {
3529 /* ranges in the io trees already unlocked */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003530 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003531 return ret;
3532 }
Mark Fashehf4414602015-06-30 14:42:05 -07003533
Mark Fasheh207910d2015-06-30 14:42:04 -07003534 /* pass original length for comparison so we stay within i_size */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003535 ret = btrfs_cmp_data(olen, cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003536 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003537 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003538
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003539 if (same_inode)
3540 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3541 same_lock_start + same_lock_len - 1);
3542 else
3543 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003544
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003545 btrfs_cmp_data_free(cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003546
3547 return ret;
3548}
3549
Timofey Titovetsb6728762018-05-02 08:15:37 +03003550#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3551
Timofey Titovets39739092018-05-02 08:15:36 +03003552static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3553 struct inode *dst, u64 dst_loff)
3554{
3555 int ret;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003556 struct cmp_pages cmp;
3557 int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
Timofey Titovets39739092018-05-02 08:15:36 +03003558 bool same_inode = (src == dst);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003559 u64 i, tail_len, chunk_count;
Timofey Titovets39739092018-05-02 08:15:36 +03003560
3561 if (olen == 0)
3562 return 0;
3563
3564 if (same_inode)
3565 inode_lock(src);
3566 else
3567 btrfs_double_inode_lock(src, dst);
3568
3569 /* don't make the dst file partly checksummed */
3570 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3571 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3572 ret = -EINVAL;
3573 goto out_unlock;
3574 }
3575
Timofey Titovetsb6728762018-05-02 08:15:37 +03003576 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3577 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003578 if (chunk_count == 0)
3579 num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
3580
3581 /*
3582 * If deduping ranges in the same inode, locking rules make it
3583 * mandatory to always lock pages in ascending order to avoid deadlocks
3584 * with concurrent tasks (such as starting writeback/delalloc).
3585 */
3586 if (same_inode && dst_loff < loff)
3587 swap(loff, dst_loff);
3588
3589 /*
3590 * We must gather up all the pages before we initiate our extent
3591 * locking. We use an array for the page pointers. Size of the array is
3592 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
3593 */
David Sterbabf5091c2018-05-11 17:57:54 +02003594 cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3595 GFP_KERNEL | __GFP_ZERO);
3596 cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3597 GFP_KERNEL | __GFP_ZERO);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003598 if (!cmp.src_pages || !cmp.dst_pages) {
David Sterbabf5091c2018-05-11 17:57:54 +02003599 ret = -ENOMEM;
3600 goto out_free;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003601 }
Timofey Titovetsb6728762018-05-02 08:15:37 +03003602
3603 for (i = 0; i < chunk_count; i++) {
3604 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003605 dst, dst_loff, &cmp);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003606 if (ret)
Lu Fengqi22883dd2018-06-19 14:54:38 +08003607 goto out_free;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003608
3609 loff += BTRFS_MAX_DEDUPE_LEN;
3610 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3611 }
3612
3613 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003614 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3615 dst_loff, &cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003616
Lu Fengqi22883dd2018-06-19 14:54:38 +08003617out_free:
3618 kvfree(cmp.src_pages);
3619 kvfree(cmp.dst_pages);
3620
Mark Fasheh416161d2013-08-06 11:42:51 -07003621out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003622 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003623 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003624 else
3625 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003626
3627 return ret;
3628}
3629
Miklos Szeredi87eb5eb2018-07-06 23:57:03 +02003630int btrfs_dedupe_file_range(struct file *src_file, loff_t src_loff,
3631 struct file *dst_file, loff_t dst_loff,
3632 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003633{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003634 struct inode *src = file_inode(src_file);
3635 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003636 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Mark Fasheh416161d2013-08-06 11:42:51 -07003637
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003638 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003639 /*
3640 * Btrfs does not support blocksize < page_size. As a
3641 * result, btrfs_cmp_data() won't correctly handle
3642 * this situation without an update.
3643 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003644 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003645 }
3646
Miklos Szeredi87eb5eb2018-07-06 23:57:03 +02003647 return btrfs_extent_same(src, src_loff, olen, dst, dst_loff);
Mark Fasheh416161d2013-08-06 11:42:51 -07003648}
3649
Filipe Mananaf82a9902014-06-01 01:50:28 +01003650static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3651 struct inode *inode,
3652 u64 endoff,
3653 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003654 const u64 olen,
3655 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003656{
3657 struct btrfs_root *root = BTRFS_I(inode)->root;
3658 int ret;
3659
3660 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003661 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003662 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003663 /*
3664 * We round up to the block size at eof when determining which
3665 * extents to clone above, but shouldn't round up the file size.
3666 */
3667 if (endoff > destoff + olen)
3668 endoff = destoff + olen;
3669 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003670 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003671
3672 ret = btrfs_update_inode(trans, root, inode);
3673 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003674 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003675 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003676 goto out;
3677 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003678 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003679out:
3680 return ret;
3681}
3682
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003683static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003684 const struct btrfs_trans_handle *trans,
3685 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003686 const u64 hole_offset,
3687 const u64 hole_len)
3688{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003689 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003690 struct extent_map *em;
3691 int ret;
3692
3693 em = alloc_extent_map();
3694 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003695 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003696 return;
3697 }
3698
Filipe Manana14f59792014-06-29 21:45:40 +01003699 if (path) {
3700 struct btrfs_file_extent_item *fi;
3701
3702 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3703 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003704 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003705 em->generation = -1;
3706 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3707 BTRFS_FILE_EXTENT_INLINE)
3708 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003709 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003710 } else {
3711 em->start = hole_offset;
3712 em->len = hole_len;
3713 em->ram_bytes = em->len;
3714 em->orig_start = hole_offset;
3715 em->block_start = EXTENT_MAP_HOLE;
3716 em->block_len = 0;
3717 em->orig_block_len = 0;
3718 em->compress_type = BTRFS_COMPRESS_NONE;
3719 em->generation = trans->transid;
3720 }
3721
3722 while (1) {
3723 write_lock(&em_tree->lock);
3724 ret = add_extent_mapping(em_tree, em, 1);
3725 write_unlock(&em_tree->lock);
3726 if (ret != -EEXIST) {
3727 free_extent_map(em);
3728 break;
3729 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003730 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003731 em->start + em->len - 1, 0);
3732 }
3733
David Sterbaee39b432014-09-30 01:33:33 +02003734 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003735 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003736}
3737
Filipe Manana8039d872015-10-13 15:15:00 +01003738/*
3739 * Make sure we do not end up inserting an inline extent into a file that has
3740 * already other (non-inline) extents. If a file has an inline extent it can
3741 * not have any other extents and the (single) inline extent must start at the
3742 * file offset 0. Failing to respect these rules will lead to file corruption,
3743 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3744 *
3745 * We can have extents that have been already written to disk or we can have
3746 * dirty ranges still in delalloc, in which case the extent maps and items are
3747 * created only when we run delalloc, and the delalloc ranges might fall outside
3748 * the range we are currently locking in the inode's io tree. So we check the
3749 * inode's i_size because of that (i_size updates are done while holding the
3750 * i_mutex, which we are holding here).
3751 * We also check to see if the inode has a size not greater than "datal" but has
3752 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3753 * protected against such concurrent fallocate calls by the i_mutex).
3754 *
3755 * If the file has no extents but a size greater than datal, do not allow the
3756 * copy because we would need turn the inline extent into a non-inline one (even
3757 * with NO_HOLES enabled). If we find our destination inode only has one inline
3758 * extent, just overwrite it with the source inline extent if its size is less
3759 * than the source extent's size, or we could copy the source inline extent's
3760 * data into the destination inode's inline extent if the later is greater then
3761 * the former.
3762 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003763static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003764 struct btrfs_trans_handle *trans,
3765 struct btrfs_path *path,
3766 struct btrfs_key *new_key,
3767 const u64 drop_start,
3768 const u64 datal,
3769 const u64 skip,
3770 const u64 size,
3771 char *inline_data)
3772{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003773 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003774 struct btrfs_root *root = BTRFS_I(dst)->root;
3775 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003776 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003777 int ret;
3778 struct btrfs_key key;
3779
3780 if (new_key->offset > 0)
3781 return -EOPNOTSUPP;
3782
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003783 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003784 key.type = BTRFS_EXTENT_DATA_KEY;
3785 key.offset = 0;
3786 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3787 if (ret < 0) {
3788 return ret;
3789 } else if (ret > 0) {
3790 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3791 ret = btrfs_next_leaf(root, path);
3792 if (ret < 0)
3793 return ret;
3794 else if (ret > 0)
3795 goto copy_inline_extent;
3796 }
3797 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003798 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003799 key.type == BTRFS_EXTENT_DATA_KEY) {
3800 ASSERT(key.offset > 0);
3801 return -EOPNOTSUPP;
3802 }
3803 } else if (i_size_read(dst) <= datal) {
3804 struct btrfs_file_extent_item *ei;
3805 u64 ext_len;
3806
3807 /*
3808 * If the file size is <= datal, make sure there are no other
3809 * extents following (can happen do to an fallocate call with
3810 * the flag FALLOC_FL_KEEP_SIZE).
3811 */
3812 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3813 struct btrfs_file_extent_item);
3814 /*
3815 * If it's an inline extent, it can not have other extents
3816 * following it.
3817 */
3818 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3819 BTRFS_FILE_EXTENT_INLINE)
3820 goto copy_inline_extent;
3821
3822 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3823 if (ext_len > aligned_end)
3824 return -EOPNOTSUPP;
3825
3826 ret = btrfs_next_item(root, path);
3827 if (ret < 0) {
3828 return ret;
3829 } else if (ret == 0) {
3830 btrfs_item_key_to_cpu(path->nodes[0], &key,
3831 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003832 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003833 key.type == BTRFS_EXTENT_DATA_KEY)
3834 return -EOPNOTSUPP;
3835 }
3836 }
3837
3838copy_inline_extent:
3839 /*
3840 * We have no extent items, or we have an extent at offset 0 which may
3841 * or may not be inlined. All these cases are dealt the same way.
3842 */
3843 if (i_size_read(dst) > datal) {
3844 /*
3845 * If the destination inode has an inline extent...
3846 * This would require copying the data from the source inline
3847 * extent into the beginning of the destination's inline extent.
3848 * But this is really complex, both extents can be compressed
3849 * or just one of them, which would require decompressing and
3850 * re-compressing data (which could increase the new compressed
3851 * size, not allowing the compressed data to fit anymore in an
3852 * inline extent).
3853 * So just don't support this case for now (it should be rare,
3854 * we are not really saving space when cloning inline extents).
3855 */
3856 return -EOPNOTSUPP;
3857 }
3858
3859 btrfs_release_path(path);
3860 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3861 if (ret)
3862 return ret;
3863 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3864 if (ret)
3865 return ret;
3866
3867 if (skip) {
3868 const u32 start = btrfs_file_extent_calc_inline_size(0);
3869
3870 memmove(inline_data + start, inline_data + start + skip, datal);
3871 }
3872
3873 write_extent_buffer(path->nodes[0], inline_data,
3874 btrfs_item_ptr_offset(path->nodes[0],
3875 path->slots[0]),
3876 size);
3877 inode_add_bytes(dst, datal);
3878
3879 return 0;
3880}
3881
Mark Fasheh32b7c682013-08-06 11:42:49 -07003882/**
3883 * btrfs_clone() - clone a range from inode file to another
3884 *
3885 * @src: Inode to clone from
3886 * @inode: Inode to clone to
3887 * @off: Offset within source to start clone from
3888 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003889 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003890 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003891 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003892 */
3893static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003894 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003895 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003896{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003897 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003898 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003899 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003900 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003901 struct btrfs_trans_handle *trans;
3902 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003903 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003904 u32 nritems;
3905 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003906 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003907 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003908 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003909
3910 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003911 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3912 if (!buf)
3913 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003914
3915 path = btrfs_alloc_path();
3916 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003917 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003918 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003919 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003920
David Sterbae4058b52015-11-27 16:31:35 +01003921 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003922 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003923 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003924 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003925 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003926
3927 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003928 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003929
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003930 /*
3931 * note the key will change type as we walk through the
3932 * tree.
3933 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003934 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003935 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3936 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003937 if (ret < 0)
3938 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003939 /*
3940 * First search, if no extent item that starts at offset off was
3941 * found but the previous item is an extent item, it's possible
3942 * it might overlap our target range, therefore process it.
3943 */
3944 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3945 btrfs_item_key_to_cpu(path->nodes[0], &key,
3946 path->slots[0] - 1);
3947 if (key.type == BTRFS_EXTENT_DATA_KEY)
3948 path->slots[0]--;
3949 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003950
Yan Zhengae01a0a2008-08-04 23:23:47 -04003951 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003952process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003953 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003954 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003955 if (ret < 0)
3956 goto out;
3957 if (ret > 0)
3958 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003959 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003960 }
3961 leaf = path->nodes[0];
3962 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003963
Yan Zhengae01a0a2008-08-04 23:23:47 -04003964 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003965 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003966 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003967 break;
3968
David Sterba962a2982014-06-04 18:41:45 +02003969 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003970 struct btrfs_file_extent_item *extent;
3971 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003972 u32 size;
3973 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003974 u64 disko = 0, diskl = 0;
3975 u64 datao = 0, datal = 0;
3976 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003977 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003978
Sage Weilc5c9cd42008-11-12 14:32:25 -05003979 extent = btrfs_item_ptr(leaf, slot,
3980 struct btrfs_file_extent_item);
3981 comp = btrfs_file_extent_compression(leaf, extent);
3982 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003983 if (type == BTRFS_FILE_EXTENT_REG ||
3984 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003985 disko = btrfs_file_extent_disk_bytenr(leaf,
3986 extent);
3987 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3988 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003989 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003990 datal = btrfs_file_extent_num_bytes(leaf,
3991 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003992 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3993 /* take upper bound, may be compressed */
3994 datal = btrfs_file_extent_ram_bytes(leaf,
3995 extent);
3996 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003997
Filipe Manana2c463822014-05-31 02:31:05 +01003998 /*
3999 * The first search might have left us at an extent
4000 * item that ends before our target range's start, can
4001 * happen if we have holes and NO_HOLES feature enabled.
4002 */
4003 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00004004 path->slots[0]++;
4005 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01004006 } else if (key.offset >= off + len) {
4007 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00004008 }
Filipe Mananadf858e72015-03-31 14:56:46 +01004009 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00004010 size = btrfs_item_size_nr(leaf, slot);
4011 read_extent_buffer(leaf, buf,
4012 btrfs_item_ptr_offset(leaf, slot),
4013 size);
4014
4015 btrfs_release_path(path);
4016 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004017
Zheng Yan31840ae2008-09-23 13:14:14 -04004018 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004019 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08004020 if (off <= key.offset)
4021 new_key.offset = key.offset + destoff - off;
4022 else
4023 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004024
Sage Weilb6f34092011-09-20 14:48:51 -04004025 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01004026 * Deal with a hole that doesn't have an extent item
4027 * that represents it (NO_HOLES feature enabled).
4028 * This hole is either in the middle of the cloning
4029 * range or at the beginning (fully overlaps it or
4030 * partially overlaps it).
4031 */
4032 if (new_key.offset != last_dest_end)
4033 drop_start = last_dest_end;
4034 else
4035 drop_start = new_key.offset;
4036
4037 /*
Sage Weilb6f34092011-09-20 14:48:51 -04004038 * 1 - adjusting old extent (we may have to split it)
4039 * 1 - add new extent
4040 * 1 - inode update
4041 */
4042 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004043 if (IS_ERR(trans)) {
4044 ret = PTR_ERR(trans);
4045 goto out;
4046 }
4047
Chris Masonc8a894d2009-06-27 21:07:03 -04004048 if (type == BTRFS_FILE_EXTENT_REG ||
4049 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04004050 /*
4051 * a | --- range to clone ---| b
4052 * | ------------- extent ------------- |
4053 */
4054
Antonio Ospite93915582014-06-04 14:03:48 +02004055 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04004056 if (key.offset + datal > off + len)
4057 datal = off + len - key.offset;
4058
Antonio Ospite93915582014-06-04 14:03:48 +02004059 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004060 if (off > key.offset) {
4061 datao += off - key.offset;
4062 datal -= off - key.offset;
4063 }
4064
Josef Bacik5dc562c2012-08-17 13:14:17 -04004065 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01004066 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04004067 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04004068 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004069 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02004070 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08004071 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04004072 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004073 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004074 goto out;
4075 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004076
Sage Weilc5c9cd42008-11-12 14:32:25 -05004077 ret = btrfs_insert_empty_item(trans, root, path,
4078 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004079 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004080 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004081 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004082 goto out;
4083 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05004084
4085 leaf = path->nodes[0];
4086 slot = path->slots[0];
4087 write_extent_buffer(leaf, buf,
4088 btrfs_item_ptr_offset(leaf, slot),
4089 size);
4090
4091 extent = btrfs_item_ptr(leaf, slot,
4092 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004093
Sage Weilc5c9cd42008-11-12 14:32:25 -05004094 /* disko == 0 means it's a hole */
4095 if (!disko)
4096 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004097
4098 btrfs_set_file_extent_offset(leaf, extent,
4099 datao);
4100 btrfs_set_file_extent_num_bytes(leaf, extent,
4101 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07004102
Sage Weilc5c9cd42008-11-12 14:32:25 -05004103 if (disko) {
4104 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004105 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004106 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004107 disko, diskl, 0,
4108 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004109 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004110 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 if (ret) {
4112 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004114 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004115 goto out;
4116
4117 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05004118 }
4119 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
4120 u64 skip = 0;
4121 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01004122
Sage Weilc5c9cd42008-11-12 14:32:25 -05004123 if (off > key.offset) {
4124 skip = off - key.offset;
4125 new_key.offset += skip;
4126 }
Chris Masond3977122009-01-05 21:25:51 -05004127
Liu Boaa42ffd2012-09-18 03:52:23 -06004128 if (key.offset + datal > off + len)
4129 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05004130
Sage Weilc5c9cd42008-11-12 14:32:25 -05004131 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05004132 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004133 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004134 goto out;
4135 }
4136 size -= skip + trim;
4137 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004138
David Sterba4a0ab9d2017-02-10 20:18:49 +01004139 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01004140 trans, path,
4141 &new_key,
4142 drop_start,
4143 datal,
4144 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004145 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02004146 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07004147 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01004148 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004149 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004150 goto out;
4151 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05004152 leaf = path->nodes[0];
4153 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05004154 }
4155
Filipe Manana7ffbb592014-06-09 03:48:05 +01004156 /* If we have an implicit hole (NO_HOLES feature). */
4157 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02004158 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01004159 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01004160 new_key.offset - drop_start);
4161
Nikolay Borisova2f392e2017-02-20 13:51:04 +02004162 clone_update_extent_map(BTRFS_I(inode), trans,
4163 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01004164
Sage Weilc5c9cd42008-11-12 14:32:25 -05004165 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02004166 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004167
Filipe Manana62e23902014-08-08 02:47:06 +01004168 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004169 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004170 ret = clone_finish_inode_update(trans, inode,
4171 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07004172 destoff, olen,
4173 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004174 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004175 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01004176 if (new_key.offset + datal >= destoff + len)
4177 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004178 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004179 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01004180 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08004181
4182 if (fatal_signal_pending(current)) {
4183 ret = -EINTR;
4184 goto out;
4185 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004186 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004187 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004188
Filipe Mananaf82a9902014-06-01 01:50:28 +01004189 if (last_dest_end < destoff + len) {
4190 /*
4191 * We have an implicit hole (NO_HOLES feature is enabled) that
4192 * fully or partially overlaps our cloning range at its end.
4193 */
4194 btrfs_release_path(path);
4195
4196 /*
4197 * 1 - remove extent(s)
4198 * 1 - inode update
4199 */
4200 trans = btrfs_start_transaction(root, 2);
4201 if (IS_ERR(trans)) {
4202 ret = PTR_ERR(trans);
4203 goto out;
4204 }
4205 ret = btrfs_drop_extents(trans, root, inode,
4206 last_dest_end, destoff + len, 1);
4207 if (ret) {
4208 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04004209 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004210 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004211 goto out;
4212 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02004213 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
4214 last_dest_end,
4215 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004216 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07004217 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01004218 }
4219
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004220out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07004221 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02004222 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004223 return ret;
4224}
4225
Zach Brown3db11b22015-11-10 16:53:32 -05004226static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
4227 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07004228{
Al Viro54563d42013-09-01 15:57:51 -04004229 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05004230 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004232 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004233 int ret;
4234 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004235 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05004236 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004237
4238 /*
4239 * TODO:
4240 * - split compressed inline extents. annoying: we need to
4241 * decompress into destination's address_space (the file offset
4242 * may change, so source mapping won't do), then recompress (or
4243 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08004244 *
4245 * - split destination inode's inline extents. The inline extents can
4246 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07004247 */
4248
Mark Fasheh32b7c682013-08-06 11:42:49 -07004249 if (btrfs_root_readonly(root))
4250 return -EROFS;
4251
Zach Brown3db11b22015-11-10 16:53:32 -05004252 if (file_src->f_path.mnt != file->f_path.mnt ||
4253 src->i_sb != inode->i_sb)
4254 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004255
Mark Fasheh32b7c682013-08-06 11:42:49 -07004256 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05004257 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004258
4259 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07004260 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004261 } else {
Al Viro59551022016-01-22 15:40:57 -05004262 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004263 }
4264
Omar Sandovalb5c40d52018-05-22 15:02:12 -07004265 /* don't make the dst file partly checksummed */
4266 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
4267 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
4268 ret = -EINVAL;
4269 goto out_unlock;
4270 }
4271
Mark Fasheh32b7c682013-08-06 11:42:49 -07004272 /* determine range to clone */
4273 ret = -EINVAL;
4274 if (off + len > src->i_size || off + len < off)
4275 goto out_unlock;
4276 if (len == 0)
4277 olen = len = src->i_size - off;
4278 /* if we extend to eof, continue to block boundary */
4279 if (off + len == src->i_size)
4280 len = ALIGN(src->i_size, bs) - off;
4281
Filipe Mananaccccf3d62015-03-30 18:23:59 +01004282 if (len == 0) {
4283 ret = 0;
4284 goto out_unlock;
4285 }
4286
Mark Fasheh32b7c682013-08-06 11:42:49 -07004287 /* verify the end result is block aligned */
4288 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
4289 !IS_ALIGNED(destoff, bs))
4290 goto out_unlock;
4291
4292 /* verify if ranges are overlapped within the same file */
4293 if (same_inode) {
4294 if (destoff + len > off && destoff < off + len)
4295 goto out_unlock;
4296 }
4297
4298 if (destoff > inode->i_size) {
4299 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
4300 if (ret)
4301 goto out_unlock;
4302 }
4303
Filipe Mananac125b8b2014-05-23 05:03:34 +01004304 /*
4305 * Lock the target range too. Right after we replace the file extent
4306 * items in the fs tree (which now point to the cloned data), we might
4307 * have a worker replace them with extent items relative to a write
4308 * operation that was issued before this clone operation (i.e. confront
4309 * with inode.c:btrfs_finish_ordered_io).
4310 */
4311 if (same_inode) {
4312 u64 lock_start = min_t(u64, off, destoff);
4313 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004314
Filipe Mananae0bd70c2016-01-27 10:20:58 +00004315 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01004316 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00004317 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
4318 true);
4319 }
4320 ASSERT(ret == 0);
4321 if (WARN_ON(ret)) {
4322 /* ranges in the io trees already unlocked */
4323 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01004324 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07004325
Mark Fasheh1c919a52015-06-30 14:42:08 -07004326 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004327
Filipe Mananac125b8b2014-05-23 05:03:34 +01004328 if (same_inode) {
4329 u64 lock_start = min_t(u64, off, destoff);
4330 u64 lock_end = max_t(u64, off, destoff) + len - 1;
4331
4332 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
4333 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07004334 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01004335 }
4336 /*
4337 * Truncate page cache pages so that future reads will see the cloned
4338 * data immediately and not the previous data.
4339 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05304340 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004341 round_down(destoff, PAGE_SIZE),
4342 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004343out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07004344 if (!same_inode)
4345 btrfs_double_inode_unlock(src, inode);
4346 else
Al Viro59551022016-01-22 15:40:57 -05004347 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05004348 return ret;
4349}
4350
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004351int btrfs_clone_file_range(struct file *src_file, loff_t off,
4352 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05004353{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004354 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004355}
4356
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004357static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4358{
Al Viro496ad9a2013-01-23 17:07:38 -05004359 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004360 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004361 struct btrfs_root *root = BTRFS_I(inode)->root;
4362 struct btrfs_root *new_root;
4363 struct btrfs_dir_item *di;
4364 struct btrfs_trans_handle *trans;
4365 struct btrfs_path *path;
4366 struct btrfs_key location;
4367 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004368 u64 objectid = 0;
4369 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004370 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004371
4372 if (!capable(CAP_SYS_ADMIN))
4373 return -EPERM;
4374
Miao Xie3c04ce02012-11-26 08:43:07 +00004375 ret = mnt_want_write_file(file);
4376 if (ret)
4377 return ret;
4378
4379 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4380 ret = -EFAULT;
4381 goto out;
4382 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004383
4384 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304385 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004386
4387 location.objectid = objectid;
4388 location.type = BTRFS_ROOT_ITEM_KEY;
4389 location.offset = (u64)-1;
4390
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004391 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004392 if (IS_ERR(new_root)) {
4393 ret = PTR_ERR(new_root);
4394 goto out;
4395 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004396 if (!is_fstree(new_root->objectid)) {
4397 ret = -ENOENT;
4398 goto out;
4399 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004400
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004401 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004402 if (!path) {
4403 ret = -ENOMEM;
4404 goto out;
4405 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004406 path->leave_spinning = 1;
4407
4408 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004409 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004410 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004411 ret = PTR_ERR(trans);
4412 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004413 }
4414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004415 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4416 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004417 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004418 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004419 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004420 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004421 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004422 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004423 ret = -ENOENT;
4424 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004425 }
4426
4427 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4428 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4429 btrfs_mark_buffer_dirty(path->nodes[0]);
4430 btrfs_free_path(path);
4431
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004432 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004433 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004434out:
4435 mnt_drop_write_file(file);
4436 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004437}
4438
Su Yuec065f5b12018-04-02 17:24:11 +08004439static void get_block_group_info(struct list_head *groups_list,
4440 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004441{
4442 struct btrfs_block_group_cache *block_group;
4443
4444 space->total_bytes = 0;
4445 space->used_bytes = 0;
4446 space->flags = 0;
4447 list_for_each_entry(block_group, groups_list, list) {
4448 space->flags = block_group->flags;
4449 space->total_bytes += block_group->key.offset;
4450 space->used_bytes +=
4451 btrfs_block_group_used(&block_group->item);
4452 }
4453}
4454
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004455static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4456 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004457{
4458 struct btrfs_ioctl_space_args space_args;
4459 struct btrfs_ioctl_space_info space;
4460 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004461 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004462 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004463 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004464 static const u64 types[] = {
4465 BTRFS_BLOCK_GROUP_DATA,
4466 BTRFS_BLOCK_GROUP_SYSTEM,
4467 BTRFS_BLOCK_GROUP_METADATA,
4468 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4469 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004470 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004471 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004472 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004473 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004474 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004475
4476 if (copy_from_user(&space_args,
4477 (struct btrfs_ioctl_space_args __user *)arg,
4478 sizeof(space_args)))
4479 return -EFAULT;
4480
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004481 for (i = 0; i < num_types; i++) {
4482 struct btrfs_space_info *tmp;
4483
4484 info = NULL;
4485 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004486 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004487 list) {
4488 if (tmp->flags == types[i]) {
4489 info = tmp;
4490 break;
4491 }
4492 }
4493 rcu_read_unlock();
4494
4495 if (!info)
4496 continue;
4497
4498 down_read(&info->groups_sem);
4499 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4500 if (!list_empty(&info->block_groups[c]))
4501 slot_count++;
4502 }
4503 up_read(&info->groups_sem);
4504 }
Josef Bacik1406e432010-01-13 18:19:06 +00004505
David Sterba36523e952014-02-07 14:34:12 +01004506 /*
4507 * Global block reserve, exported as a space_info
4508 */
4509 slot_count++;
4510
Chris Mason7fde62b2010-03-16 15:40:10 -04004511 /* space_slots == 0 means they are asking for a count */
4512 if (space_args.space_slots == 0) {
4513 space_args.total_spaces = slot_count;
4514 goto out;
4515 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004516
Dan Rosenberg51788b12011-02-14 16:04:23 -05004517 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004518
Chris Mason7fde62b2010-03-16 15:40:10 -04004519 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004520
Chris Mason7fde62b2010-03-16 15:40:10 -04004521 /* we generally have at most 6 or so space infos, one for each raid
4522 * level. So, a whole page should be more than enough for everyone
4523 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004524 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004525 return -ENOMEM;
4526
4527 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004528 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004529 if (!dest)
4530 return -ENOMEM;
4531 dest_orig = dest;
4532
4533 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004534 for (i = 0; i < num_types; i++) {
4535 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004536
Dan Rosenberg51788b12011-02-14 16:04:23 -05004537 if (!slot_count)
4538 break;
4539
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004540 info = NULL;
4541 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004542 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004543 list) {
4544 if (tmp->flags == types[i]) {
4545 info = tmp;
4546 break;
4547 }
4548 }
4549 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004550
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004551 if (!info)
4552 continue;
4553 down_read(&info->groups_sem);
4554 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4555 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004556 get_block_group_info(&info->block_groups[c],
4557 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004558 memcpy(dest, &space, sizeof(space));
4559 dest++;
4560 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004561 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004562 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004563 if (!slot_count)
4564 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004565 }
4566 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004567 }
Josef Bacik1406e432010-01-13 18:19:06 +00004568
David Sterba36523e952014-02-07 14:34:12 +01004569 /*
4570 * Add global block reserve
4571 */
4572 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004573 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004574
4575 spin_lock(&block_rsv->lock);
4576 space.total_bytes = block_rsv->size;
4577 space.used_bytes = block_rsv->size - block_rsv->reserved;
4578 spin_unlock(&block_rsv->lock);
4579 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4580 memcpy(dest, &space, sizeof(space));
4581 space_args.total_spaces++;
4582 }
4583
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004584 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004585 (arg + sizeof(struct btrfs_ioctl_space_args));
4586
4587 if (copy_to_user(user_dest, dest_orig, alloc_size))
4588 ret = -EFAULT;
4589
4590 kfree(dest_orig);
4591out:
4592 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004593 ret = -EFAULT;
4594
4595 return ret;
4596}
4597
Miao Xie9a8c28b2012-11-26 08:40:43 +00004598static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4599 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004600{
Sage Weil46204592010-10-29 15:41:32 -04004601 struct btrfs_trans_handle *trans;
4602 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004603 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004604
Miao Xied4edf392013-02-20 09:17:06 +00004605 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004606 if (IS_ERR(trans)) {
4607 if (PTR_ERR(trans) != -ENOENT)
4608 return PTR_ERR(trans);
4609
4610 /* No running transaction, don't bother */
4611 transid = root->fs_info->last_trans_committed;
4612 goto out;
4613 }
Sage Weil46204592010-10-29 15:41:32 -04004614 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004615 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004616 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004617 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004618 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004619 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004620out:
Sage Weil46204592010-10-29 15:41:32 -04004621 if (argp)
4622 if (copy_to_user(argp, &transid, sizeof(transid)))
4623 return -EFAULT;
4624 return 0;
4625}
4626
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004627static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004628 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004629{
Sage Weil46204592010-10-29 15:41:32 -04004630 u64 transid;
4631
4632 if (argp) {
4633 if (copy_from_user(&transid, argp, sizeof(transid)))
4634 return -EFAULT;
4635 } else {
4636 transid = 0; /* current trans */
4637 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004638 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004639}
4640
Miao Xieb8e95482012-11-26 08:48:01 +00004641static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004642{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004643 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004644 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004645 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004646
4647 if (!capable(CAP_SYS_ADMIN))
4648 return -EPERM;
4649
4650 sa = memdup_user(arg, sizeof(*sa));
4651 if (IS_ERR(sa))
4652 return PTR_ERR(sa);
4653
Miao Xieb8e95482012-11-26 08:48:01 +00004654 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4655 ret = mnt_want_write_file(file);
4656 if (ret)
4657 goto out;
4658 }
4659
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004660 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004661 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4662 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004663
4664 if (copy_to_user(arg, sa, sizeof(*sa)))
4665 ret = -EFAULT;
4666
Miao Xieb8e95482012-11-26 08:48:01 +00004667 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4668 mnt_drop_write_file(file);
4669out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004670 kfree(sa);
4671 return ret;
4672}
4673
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004674static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004675{
4676 if (!capable(CAP_SYS_ADMIN))
4677 return -EPERM;
4678
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004679 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004680}
4681
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004682static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004683 void __user *arg)
4684{
4685 struct btrfs_ioctl_scrub_args *sa;
4686 int ret;
4687
4688 if (!capable(CAP_SYS_ADMIN))
4689 return -EPERM;
4690
4691 sa = memdup_user(arg, sizeof(*sa));
4692 if (IS_ERR(sa))
4693 return PTR_ERR(sa);
4694
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004695 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004696
4697 if (copy_to_user(arg, sa, sizeof(*sa)))
4698 ret = -EFAULT;
4699
4700 kfree(sa);
4701 return ret;
4702}
4703
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004704static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004705 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004706{
4707 struct btrfs_ioctl_get_dev_stats *sa;
4708 int ret;
4709
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004710 sa = memdup_user(arg, sizeof(*sa));
4711 if (IS_ERR(sa))
4712 return PTR_ERR(sa);
4713
David Sterbab27f7c02012-06-22 06:30:39 -06004714 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4715 kfree(sa);
4716 return -EPERM;
4717 }
4718
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004719 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004720
4721 if (copy_to_user(arg, sa, sizeof(*sa)))
4722 ret = -EFAULT;
4723
4724 kfree(sa);
4725 return ret;
4726}
4727
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004728static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4729 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004730{
4731 struct btrfs_ioctl_dev_replace_args *p;
4732 int ret;
4733
4734 if (!capable(CAP_SYS_ADMIN))
4735 return -EPERM;
4736
4737 p = memdup_user(arg, sizeof(*p));
4738 if (IS_ERR(p))
4739 return PTR_ERR(p);
4740
4741 switch (p->cmd) {
4742 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004743 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004744 ret = -EROFS;
4745 goto out;
4746 }
David Sterba171938e2017-03-28 14:44:21 +02004747 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004748 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004749 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004750 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004751 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004752 }
4753 break;
4754 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004755 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004756 ret = 0;
4757 break;
4758 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004759 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004760 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004761 break;
4762 default:
4763 ret = -EINVAL;
4764 break;
4765 }
4766
4767 if (copy_to_user(arg, p, sizeof(*p)))
4768 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004769out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004770 kfree(p);
4771 return ret;
4772}
4773
Jan Schmidtd7728c92011-07-07 16:48:38 +02004774static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4775{
4776 int ret = 0;
4777 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004778 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004779 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004780 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004781 struct inode_fs_paths *ipath = NULL;
4782 struct btrfs_path *path;
4783
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004784 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004785 return -EPERM;
4786
4787 path = btrfs_alloc_path();
4788 if (!path) {
4789 ret = -ENOMEM;
4790 goto out;
4791 }
4792
4793 ipa = memdup_user(arg, sizeof(*ipa));
4794 if (IS_ERR(ipa)) {
4795 ret = PTR_ERR(ipa);
4796 ipa = NULL;
4797 goto out;
4798 }
4799
4800 size = min_t(u32, ipa->size, 4096);
4801 ipath = init_ipath(size, root, path);
4802 if (IS_ERR(ipath)) {
4803 ret = PTR_ERR(ipath);
4804 ipath = NULL;
4805 goto out;
4806 }
4807
4808 ret = paths_from_inode(ipa->inum, ipath);
4809 if (ret < 0)
4810 goto out;
4811
4812 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004813 rel_ptr = ipath->fspath->val[i] -
4814 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004815 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004816 }
4817
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004818 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4819 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004820 if (ret) {
4821 ret = -EFAULT;
4822 goto out;
4823 }
4824
4825out:
4826 btrfs_free_path(path);
4827 free_ipath(ipath);
4828 kfree(ipa);
4829
4830 return ret;
4831}
4832
4833static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4834{
4835 struct btrfs_data_container *inodes = ctx;
4836 const size_t c = 3 * sizeof(u64);
4837
4838 if (inodes->bytes_left >= c) {
4839 inodes->bytes_left -= c;
4840 inodes->val[inodes->elem_cnt] = inum;
4841 inodes->val[inodes->elem_cnt + 1] = offset;
4842 inodes->val[inodes->elem_cnt + 2] = root;
4843 inodes->elem_cnt += 3;
4844 } else {
4845 inodes->bytes_missing += c - inodes->bytes_left;
4846 inodes->bytes_left = 0;
4847 inodes->elem_missed += 3;
4848 }
4849
4850 return 0;
4851}
4852
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004853static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004854 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004855{
4856 int ret = 0;
4857 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004858 struct btrfs_ioctl_logical_ino_args *loi;
4859 struct btrfs_data_container *inodes = NULL;
4860 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004861 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004862
4863 if (!capable(CAP_SYS_ADMIN))
4864 return -EPERM;
4865
4866 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304867 if (IS_ERR(loi))
4868 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004869
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004870 if (version == 1) {
4871 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004872 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004873 } else {
4874 /* All reserved bits must be 0 for now */
4875 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4876 ret = -EINVAL;
4877 goto out_loi;
4878 }
4879 /* Only accept flags we have defined so far */
4880 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4881 ret = -EINVAL;
4882 goto out_loi;
4883 }
4884 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004885 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004886 }
4887
Jan Schmidtd7728c92011-07-07 16:48:38 +02004888 path = btrfs_alloc_path();
4889 if (!path) {
4890 ret = -ENOMEM;
4891 goto out;
4892 }
4893
Jan Schmidtd7728c92011-07-07 16:48:38 +02004894 inodes = init_data_container(size);
4895 if (IS_ERR(inodes)) {
4896 ret = PTR_ERR(inodes);
4897 inodes = NULL;
4898 goto out;
4899 }
4900
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004901 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004902 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004903 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004904 ret = -ENOENT;
4905 if (ret < 0)
4906 goto out;
4907
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004908 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4909 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004910 if (ret)
4911 ret = -EFAULT;
4912
4913out:
4914 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004915 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004916out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004917 kfree(loi);
4918
4919 return ret;
4920}
4921
David Sterba008ef092018-03-21 02:05:27 +01004922void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004923 struct btrfs_ioctl_balance_args *bargs)
4924{
4925 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4926
4927 bargs->flags = bctl->flags;
4928
David Sterba3009a622018-03-21 01:31:04 +01004929 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004930 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4931 if (atomic_read(&fs_info->balance_pause_req))
4932 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004933 if (atomic_read(&fs_info->balance_cancel_req))
4934 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004935
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004936 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4937 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4938 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004939
David Sterba008ef092018-03-21 02:05:27 +01004940 spin_lock(&fs_info->balance_lock);
4941 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4942 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004943}
4944
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004945static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004946{
Al Viro496ad9a2013-01-23 17:07:38 -05004947 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004948 struct btrfs_fs_info *fs_info = root->fs_info;
4949 struct btrfs_ioctl_balance_args *bargs;
4950 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004951 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004952 int ret;
4953
4954 if (!capable(CAP_SYS_ADMIN))
4955 return -EPERM;
4956
Liu Boe54bfa312012-06-29 03:58:48 -06004957 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004958 if (ret)
4959 return ret;
4960
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004961again:
David Sterba171938e2017-03-28 14:44:21 +02004962 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004963 mutex_lock(&fs_info->balance_mutex);
4964 need_unlock = true;
4965 goto locked;
4966 }
4967
4968 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004969 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004970 * (1) some other op is running
4971 * (2) balance is running
4972 * (3) balance is paused -- special case (think resume)
4973 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004974 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004975 if (fs_info->balance_ctl) {
4976 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004977 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004978 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004979 /*
4980 * Lock released to allow other waiters to continue,
4981 * we'll reexamine the status again.
4982 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004983 mutex_lock(&fs_info->balance_mutex);
4984
4985 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004986 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004987 /* this is (3) */
4988 need_unlock = false;
4989 goto locked;
4990 }
4991
4992 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004993 goto again;
4994 } else {
4995 /* this is (2) */
4996 mutex_unlock(&fs_info->balance_mutex);
4997 ret = -EINPROGRESS;
4998 goto out;
4999 }
5000 } else {
5001 /* this is (1) */
5002 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08005003 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005004 goto out;
5005 }
5006
5007locked:
David Sterba171938e2017-03-28 14:44:21 +02005008 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005009
5010 if (arg) {
5011 bargs = memdup_user(arg, sizeof(*bargs));
5012 if (IS_ERR(bargs)) {
5013 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005014 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005015 }
Ilya Dryomovde322262012-01-16 22:04:49 +02005016
5017 if (bargs->flags & BTRFS_BALANCE_RESUME) {
5018 if (!fs_info->balance_ctl) {
5019 ret = -ENOTCONN;
5020 goto out_bargs;
5021 }
5022
5023 bctl = fs_info->balance_ctl;
5024 spin_lock(&fs_info->balance_lock);
5025 bctl->flags |= BTRFS_BALANCE_RESUME;
5026 spin_unlock(&fs_info->balance_lock);
5027
5028 goto do_balance;
5029 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005030 } else {
5031 bargs = NULL;
5032 }
5033
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005034 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005035 ret = -EINPROGRESS;
5036 goto out_bargs;
5037 }
5038
David Sterba8d2db782015-11-04 15:38:29 +01005039 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005040 if (!bctl) {
5041 ret = -ENOMEM;
5042 goto out_bargs;
5043 }
5044
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005045 if (arg) {
5046 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
5047 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
5048 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
5049
5050 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02005051 } else {
5052 /* balance everything - no filters */
5053 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005054 }
5055
David Sterba8eb93452015-10-12 16:55:54 +02005056 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
5057 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02005058 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02005059 }
5060
Ilya Dryomovde322262012-01-16 22:04:49 +02005061do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005062 /*
David Sterba149196a2018-03-20 20:23:09 +01005063 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
5064 * btrfs_balance. bctl is freed in reset_balance_state, or, if
5065 * restriper was paused all the way until unmount, in free_fs_info.
5066 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005067 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005068 need_unlock = false;
5069
David Sterba6fcf6e22018-05-07 17:44:03 +02005070 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02005071 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005072
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005073 if (arg) {
5074 if (copy_to_user(arg, bargs, sizeof(*bargs)))
5075 ret = -EFAULT;
5076 }
5077
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02005078out_bctl:
5079 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005080out_bargs:
5081 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005082out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005083 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005084 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02005085 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02005086out:
Liu Boe54bfa312012-06-29 03:58:48 -06005087 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005088 return ret;
5089}
5090
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005091static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005092{
5093 if (!capable(CAP_SYS_ADMIN))
5094 return -EPERM;
5095
5096 switch (cmd) {
5097 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005098 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02005099 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005100 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005101 }
5102
5103 return -EINVAL;
5104}
5105
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005106static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005107 void __user *arg)
5108{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005109 struct btrfs_ioctl_balance_args *bargs;
5110 int ret = 0;
5111
5112 if (!capable(CAP_SYS_ADMIN))
5113 return -EPERM;
5114
5115 mutex_lock(&fs_info->balance_mutex);
5116 if (!fs_info->balance_ctl) {
5117 ret = -ENOTCONN;
5118 goto out;
5119 }
5120
David Sterba8d2db782015-11-04 15:38:29 +01005121 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005122 if (!bargs) {
5123 ret = -ENOMEM;
5124 goto out;
5125 }
5126
David Sterba008ef092018-03-21 02:05:27 +01005127 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005128
5129 if (copy_to_user(arg, bargs, sizeof(*bargs)))
5130 ret = -EFAULT;
5131
5132 kfree(bargs);
5133out:
5134 mutex_unlock(&fs_info->balance_mutex);
5135 return ret;
5136}
5137
Miao Xie905b0dd2012-11-26 08:50:11 +00005138static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005139{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005140 struct inode *inode = file_inode(file);
5141 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02005142 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02005143 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005144
5145 if (!capable(CAP_SYS_ADMIN))
5146 return -EPERM;
5147
Miao Xie905b0dd2012-11-26 08:50:11 +00005148 ret = mnt_want_write_file(file);
5149 if (ret)
5150 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005151
5152 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005153 if (IS_ERR(sa)) {
5154 ret = PTR_ERR(sa);
5155 goto drop_write;
5156 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005157
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005158 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02005159
5160 switch (sa->cmd) {
5161 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03005162 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02005163 break;
5164 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03005165 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02005166 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02005167 default:
5168 ret = -EINVAL;
5169 break;
5170 }
5171
Arne Jansen5d13a372011-09-14 15:53:51 +02005172 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005173 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00005174drop_write:
5175 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005176 return ret;
5177}
5178
Miao Xie905b0dd2012-11-26 08:50:11 +00005179static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005181 struct inode *inode = file_inode(file);
5182 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5183 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005184 struct btrfs_ioctl_qgroup_assign_args *sa;
5185 struct btrfs_trans_handle *trans;
5186 int ret;
5187 int err;
5188
5189 if (!capable(CAP_SYS_ADMIN))
5190 return -EPERM;
5191
Miao Xie905b0dd2012-11-26 08:50:11 +00005192 ret = mnt_want_write_file(file);
5193 if (ret)
5194 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005195
5196 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005197 if (IS_ERR(sa)) {
5198 ret = PTR_ERR(sa);
5199 goto drop_write;
5200 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005201
5202 trans = btrfs_join_transaction(root);
5203 if (IS_ERR(trans)) {
5204 ret = PTR_ERR(trans);
5205 goto out;
5206 }
5207
Arne Jansen5d13a372011-09-14 15:53:51 +02005208 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08005209 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02005210 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08005211 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02005212 }
5213
Qu Wenruoe082f562015-02-27 16:24:28 +08005214 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08005215 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08005216 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005217 btrfs_handle_fs_error(fs_info, err,
5218 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005219 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005220 if (err && !ret)
5221 ret = err;
5222
5223out:
5224 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005225drop_write:
5226 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005227 return ret;
5228}
5229
Miao Xie905b0dd2012-11-26 08:50:11 +00005230static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005231{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005232 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005233 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005234 struct btrfs_ioctl_qgroup_create_args *sa;
5235 struct btrfs_trans_handle *trans;
5236 int ret;
5237 int err;
5238
5239 if (!capable(CAP_SYS_ADMIN))
5240 return -EPERM;
5241
Miao Xie905b0dd2012-11-26 08:50:11 +00005242 ret = mnt_want_write_file(file);
5243 if (ret)
5244 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005245
5246 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005247 if (IS_ERR(sa)) {
5248 ret = PTR_ERR(sa);
5249 goto drop_write;
5250 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005251
Miao Xied86e56c2012-11-15 11:35:41 +00005252 if (!sa->qgroupid) {
5253 ret = -EINVAL;
5254 goto out;
5255 }
5256
Arne Jansen5d13a372011-09-14 15:53:51 +02005257 trans = btrfs_join_transaction(root);
5258 if (IS_ERR(trans)) {
5259 ret = PTR_ERR(trans);
5260 goto out;
5261 }
5262
Arne Jansen5d13a372011-09-14 15:53:51 +02005263 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08005264 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02005265 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08005266 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02005267 }
5268
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005269 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005270 if (err && !ret)
5271 ret = err;
5272
5273out:
5274 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005275drop_write:
5276 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005277 return ret;
5278}
5279
Miao Xie905b0dd2012-11-26 08:50:11 +00005280static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005282 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005283 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005284 struct btrfs_ioctl_qgroup_limit_args *sa;
5285 struct btrfs_trans_handle *trans;
5286 int ret;
5287 int err;
5288 u64 qgroupid;
5289
5290 if (!capable(CAP_SYS_ADMIN))
5291 return -EPERM;
5292
Miao Xie905b0dd2012-11-26 08:50:11 +00005293 ret = mnt_want_write_file(file);
5294 if (ret)
5295 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005296
5297 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005298 if (IS_ERR(sa)) {
5299 ret = PTR_ERR(sa);
5300 goto drop_write;
5301 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005302
5303 trans = btrfs_join_transaction(root);
5304 if (IS_ERR(trans)) {
5305 ret = PTR_ERR(trans);
5306 goto out;
5307 }
5308
5309 qgroupid = sa->qgroupid;
5310 if (!qgroupid) {
5311 /* take the current subvol as qgroup */
5312 qgroupid = root->root_key.objectid;
5313 }
5314
Lu Fengqif0042d52018-07-18 14:45:35 +08005315 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005316
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005317 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005318 if (err && !ret)
5319 ret = err;
5320
5321out:
5322 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005323drop_write:
5324 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005325 return ret;
5326}
5327
Jan Schmidt2f232032013-04-25 16:04:51 +00005328static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5329{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005330 struct inode *inode = file_inode(file);
5331 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005332 struct btrfs_ioctl_quota_rescan_args *qsa;
5333 int ret;
5334
5335 if (!capable(CAP_SYS_ADMIN))
5336 return -EPERM;
5337
5338 ret = mnt_want_write_file(file);
5339 if (ret)
5340 return ret;
5341
5342 qsa = memdup_user(arg, sizeof(*qsa));
5343 if (IS_ERR(qsa)) {
5344 ret = PTR_ERR(qsa);
5345 goto drop_write;
5346 }
5347
5348 if (qsa->flags) {
5349 ret = -EINVAL;
5350 goto out;
5351 }
5352
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005353 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005354
5355out:
5356 kfree(qsa);
5357drop_write:
5358 mnt_drop_write_file(file);
5359 return ret;
5360}
5361
5362static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005364 struct inode *inode = file_inode(file);
5365 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005366 struct btrfs_ioctl_quota_rescan_args *qsa;
5367 int ret = 0;
5368
5369 if (!capable(CAP_SYS_ADMIN))
5370 return -EPERM;
5371
David Sterba8d2db782015-11-04 15:38:29 +01005372 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005373 if (!qsa)
5374 return -ENOMEM;
5375
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005376 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005377 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005378 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005379 }
5380
5381 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5382 ret = -EFAULT;
5383
5384 kfree(qsa);
5385 return ret;
5386}
5387
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005388static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005390 struct inode *inode = file_inode(file);
5391 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005392
5393 if (!capable(CAP_SYS_ADMIN))
5394 return -EPERM;
5395
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005396 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005397}
5398
Hugo Millsabccd002014-01-30 20:17:00 +00005399static long _btrfs_ioctl_set_received_subvol(struct file *file,
5400 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005401{
Al Viro496ad9a2013-01-23 17:07:38 -05005402 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005403 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005404 struct btrfs_root *root = BTRFS_I(inode)->root;
5405 struct btrfs_root_item *root_item = &root->root_item;
5406 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07005407 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005408 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005409 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005410
David Sterbabd60ea02014-01-16 15:50:22 +01005411 if (!inode_owner_or_capable(inode))
5412 return -EPERM;
5413
Alexander Block8ea05e32012-07-25 17:35:53 +02005414 ret = mnt_want_write_file(file);
5415 if (ret < 0)
5416 return ret;
5417
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005418 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005419
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005420 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005421 ret = -EINVAL;
5422 goto out;
5423 }
5424
5425 if (btrfs_root_readonly(root)) {
5426 ret = -EROFS;
5427 goto out;
5428 }
5429
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005430 /*
5431 * 1 - root item
5432 * 2 - uuid items (received uuid + subvol uuid)
5433 */
5434 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005435 if (IS_ERR(trans)) {
5436 ret = PTR_ERR(trans);
5437 trans = NULL;
5438 goto out;
5439 }
5440
5441 sa->rtransid = trans->transid;
5442 sa->rtime.sec = ct.tv_sec;
5443 sa->rtime.nsec = ct.tv_nsec;
5444
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005445 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5446 BTRFS_UUID_SIZE);
5447 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005448 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005449 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005450 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5451 root->root_key.objectid);
5452 if (ret && ret != -ENOENT) {
5453 btrfs_abort_transaction(trans, ret);
5454 btrfs_end_transaction(trans);
5455 goto out;
5456 }
5457 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005458 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5459 btrfs_set_root_stransid(root_item, sa->stransid);
5460 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005461 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5462 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5463 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5464 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005465
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005466 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005467 &root->root_key, &root->root_item);
5468 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005469 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005470 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005471 }
5472 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005473 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005474 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5475 root->root_key.objectid);
5476 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005477 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005478 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005479 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005480 }
5481 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005482 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005483out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005484 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005485 mnt_drop_write_file(file);
5486 return ret;
5487}
5488
5489#ifdef CONFIG_64BIT
5490static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5491 void __user *arg)
5492{
5493 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5494 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5495 int ret = 0;
5496
5497 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305498 if (IS_ERR(args32))
5499 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005500
David Sterba8d2db782015-11-04 15:38:29 +01005501 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005502 if (!args64) {
5503 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005504 goto out;
5505 }
5506
5507 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5508 args64->stransid = args32->stransid;
5509 args64->rtransid = args32->rtransid;
5510 args64->stime.sec = args32->stime.sec;
5511 args64->stime.nsec = args32->stime.nsec;
5512 args64->rtime.sec = args32->rtime.sec;
5513 args64->rtime.nsec = args32->rtime.nsec;
5514 args64->flags = args32->flags;
5515
5516 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5517 if (ret)
5518 goto out;
5519
5520 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5521 args32->stransid = args64->stransid;
5522 args32->rtransid = args64->rtransid;
5523 args32->stime.sec = args64->stime.sec;
5524 args32->stime.nsec = args64->stime.nsec;
5525 args32->rtime.sec = args64->rtime.sec;
5526 args32->rtime.nsec = args64->rtime.nsec;
5527 args32->flags = args64->flags;
5528
5529 ret = copy_to_user(arg, args32, sizeof(*args32));
5530 if (ret)
5531 ret = -EFAULT;
5532
5533out:
5534 kfree(args32);
5535 kfree(args64);
5536 return ret;
5537}
5538#endif
5539
5540static long btrfs_ioctl_set_received_subvol(struct file *file,
5541 void __user *arg)
5542{
5543 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5544 int ret = 0;
5545
5546 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305547 if (IS_ERR(sa))
5548 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005549
5550 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5551
5552 if (ret)
5553 goto out;
5554
Alexander Block8ea05e32012-07-25 17:35:53 +02005555 ret = copy_to_user(arg, sa, sizeof(*sa));
5556 if (ret)
5557 ret = -EFAULT;
5558
5559out:
5560 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005561 return ret;
5562}
5563
jeff.liu867ab662013-01-05 02:48:01 +00005564static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5565{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005566 struct inode *inode = file_inode(file);
5567 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005568 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005569 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005570 char label[BTRFS_LABEL_SIZE];
5571
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005572 spin_lock(&fs_info->super_lock);
5573 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5574 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005575
5576 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005577
5578 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005579 btrfs_warn(fs_info,
5580 "label is too long, return the first %zu bytes",
5581 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005582 }
5583
jeff.liu867ab662013-01-05 02:48:01 +00005584 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005585
5586 return ret ? -EFAULT : 0;
5587}
5588
jeff.liua8bfd4a2013-01-05 02:48:08 +00005589static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5590{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005591 struct inode *inode = file_inode(file);
5592 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5593 struct btrfs_root *root = BTRFS_I(inode)->root;
5594 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005595 struct btrfs_trans_handle *trans;
5596 char label[BTRFS_LABEL_SIZE];
5597 int ret;
5598
5599 if (!capable(CAP_SYS_ADMIN))
5600 return -EPERM;
5601
5602 if (copy_from_user(label, arg, sizeof(label)))
5603 return -EFAULT;
5604
5605 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005606 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005607 "unable to set label with more than %d bytes",
5608 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005609 return -EINVAL;
5610 }
5611
5612 ret = mnt_want_write_file(file);
5613 if (ret)
5614 return ret;
5615
jeff.liua8bfd4a2013-01-05 02:48:08 +00005616 trans = btrfs_start_transaction(root, 0);
5617 if (IS_ERR(trans)) {
5618 ret = PTR_ERR(trans);
5619 goto out_unlock;
5620 }
5621
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005622 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005623 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005624 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005625 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005626
5627out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005628 mnt_drop_write_file(file);
5629 return ret;
5630}
5631
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005632#define INIT_FEATURE_FLAGS(suffix) \
5633 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5634 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5635 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5636
David Sterbad5131b62016-02-17 15:26:27 +01005637int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005638{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005639 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005640 INIT_FEATURE_FLAGS(SUPP),
5641 INIT_FEATURE_FLAGS(SAFE_SET),
5642 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5643 };
5644
5645 if (copy_to_user(arg, &features, sizeof(features)))
5646 return -EFAULT;
5647
5648 return 0;
5649}
5650
5651static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5652{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005653 struct inode *inode = file_inode(file);
5654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5655 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005656 struct btrfs_ioctl_feature_flags features;
5657
5658 features.compat_flags = btrfs_super_compat_flags(super_block);
5659 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5660 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5661
5662 if (copy_to_user(arg, &features, sizeof(features)))
5663 return -EFAULT;
5664
5665 return 0;
5666}
5667
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005668static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005669 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005670 u64 change_mask, u64 flags, u64 supported_flags,
5671 u64 safe_set, u64 safe_clear)
5672{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005673 const char *type = btrfs_feature_set_names[set];
5674 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005675 u64 disallowed, unsupported;
5676 u64 set_mask = flags & change_mask;
5677 u64 clear_mask = ~flags & change_mask;
5678
5679 unsupported = set_mask & ~supported_flags;
5680 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005681 names = btrfs_printable_features(set, unsupported);
5682 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005683 btrfs_warn(fs_info,
5684 "this kernel does not support the %s feature bit%s",
5685 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005686 kfree(names);
5687 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005688 btrfs_warn(fs_info,
5689 "this kernel does not support %s bits 0x%llx",
5690 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005691 return -EOPNOTSUPP;
5692 }
5693
5694 disallowed = set_mask & ~safe_set;
5695 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005696 names = btrfs_printable_features(set, disallowed);
5697 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005698 btrfs_warn(fs_info,
5699 "can't set the %s feature bit%s while mounted",
5700 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005701 kfree(names);
5702 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005703 btrfs_warn(fs_info,
5704 "can't set %s bits 0x%llx while mounted",
5705 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005706 return -EPERM;
5707 }
5708
5709 disallowed = clear_mask & ~safe_clear;
5710 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005711 names = btrfs_printable_features(set, disallowed);
5712 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005713 btrfs_warn(fs_info,
5714 "can't clear the %s feature bit%s while mounted",
5715 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005716 kfree(names);
5717 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005718 btrfs_warn(fs_info,
5719 "can't clear %s bits 0x%llx while mounted",
5720 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005721 return -EPERM;
5722 }
5723
5724 return 0;
5725}
5726
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005727#define check_feature(fs_info, change_mask, flags, mask_base) \
5728check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005729 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5730 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5731 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5732
5733static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5734{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005735 struct inode *inode = file_inode(file);
5736 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5737 struct btrfs_root *root = BTRFS_I(inode)->root;
5738 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005739 struct btrfs_ioctl_feature_flags flags[2];
5740 struct btrfs_trans_handle *trans;
5741 u64 newflags;
5742 int ret;
5743
5744 if (!capable(CAP_SYS_ADMIN))
5745 return -EPERM;
5746
5747 if (copy_from_user(flags, arg, sizeof(flags)))
5748 return -EFAULT;
5749
5750 /* Nothing to do */
5751 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5752 !flags[0].incompat_flags)
5753 return 0;
5754
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005755 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005756 flags[1].compat_flags, COMPAT);
5757 if (ret)
5758 return ret;
5759
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005760 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005761 flags[1].compat_ro_flags, COMPAT_RO);
5762 if (ret)
5763 return ret;
5764
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005765 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005766 flags[1].incompat_flags, INCOMPAT);
5767 if (ret)
5768 return ret;
5769
David Sterba7ab19622016-05-04 11:32:00 +02005770 ret = mnt_want_write_file(file);
5771 if (ret)
5772 return ret;
5773
David Sterba8051aa12014-02-07 14:34:04 +01005774 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005775 if (IS_ERR(trans)) {
5776 ret = PTR_ERR(trans);
5777 goto out_drop_write;
5778 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005779
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005780 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005781 newflags = btrfs_super_compat_flags(super_block);
5782 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5783 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5784 btrfs_set_super_compat_flags(super_block, newflags);
5785
5786 newflags = btrfs_super_compat_ro_flags(super_block);
5787 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5788 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5789 btrfs_set_super_compat_ro_flags(super_block, newflags);
5790
5791 newflags = btrfs_super_incompat_flags(super_block);
5792 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5793 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5794 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005795 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005796
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005797 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005798out_drop_write:
5799 mnt_drop_write_file(file);
5800
5801 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005802}
5803
Josef Bacik2351f432017-09-27 10:43:13 -04005804static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5805{
5806 struct btrfs_ioctl_send_args *arg;
5807 int ret;
5808
5809 if (compat) {
5810#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5811 struct btrfs_ioctl_send_args_32 args32;
5812
5813 ret = copy_from_user(&args32, argp, sizeof(args32));
5814 if (ret)
5815 return -EFAULT;
5816 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5817 if (!arg)
5818 return -ENOMEM;
5819 arg->send_fd = args32.send_fd;
5820 arg->clone_sources_count = args32.clone_sources_count;
5821 arg->clone_sources = compat_ptr(args32.clone_sources);
5822 arg->parent_root = args32.parent_root;
5823 arg->flags = args32.flags;
5824 memcpy(arg->reserved, args32.reserved,
5825 sizeof(args32.reserved));
5826#else
5827 return -ENOTTY;
5828#endif
5829 } else {
5830 arg = memdup_user(argp, sizeof(*arg));
5831 if (IS_ERR(arg))
5832 return PTR_ERR(arg);
5833 }
5834 ret = btrfs_ioctl_send(file, arg);
5835 kfree(arg);
5836 return ret;
5837}
5838
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005839long btrfs_ioctl(struct file *file, unsigned int
5840 cmd, unsigned long arg)
5841{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005842 struct inode *inode = file_inode(file);
5843 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5844 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005845 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005846
5847 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005848 case FS_IOC_GETFLAGS:
5849 return btrfs_ioctl_getflags(file, argp);
5850 case FS_IOC_SETFLAGS:
5851 return btrfs_ioctl_setflags(file, argp);
5852 case FS_IOC_GETVERSION:
5853 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005854 case FITRIM:
5855 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005856 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005857 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005858 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005859 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005860 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005861 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005862 case BTRFS_IOC_SUBVOL_CREATE_V2:
5863 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005864 case BTRFS_IOC_SNAP_DESTROY:
5865 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005866 case BTRFS_IOC_SUBVOL_GETFLAGS:
5867 return btrfs_ioctl_subvol_getflags(file, argp);
5868 case BTRFS_IOC_SUBVOL_SETFLAGS:
5869 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005870 case BTRFS_IOC_DEFAULT_SUBVOL:
5871 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005872 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005873 return btrfs_ioctl_defrag(file, NULL);
5874 case BTRFS_IOC_DEFRAG_RANGE:
5875 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005876 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005877 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005878 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005879 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005880 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005881 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005882 case BTRFS_IOC_RM_DEV_V2:
5883 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005884 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005885 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005886 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005887 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005888 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005889 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005890 case BTRFS_IOC_TREE_SEARCH:
5891 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005892 case BTRFS_IOC_TREE_SEARCH_V2:
5893 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005894 case BTRFS_IOC_INO_LOOKUP:
5895 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005896 case BTRFS_IOC_INO_PATHS:
5897 return btrfs_ioctl_ino_to_path(root, argp);
5898 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005899 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5900 case BTRFS_IOC_LOGICAL_INO_V2:
5901 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005902 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005903 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005904 case BTRFS_IOC_SYNC: {
5905 int ret;
5906
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005907 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005908 if (ret)
5909 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005910 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005911 /*
5912 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005913 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005914 * processing uncleaned subvols.
5915 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005916 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005917 return ret;
5918 }
Sage Weil46204592010-10-29 15:41:32 -04005919 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005920 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005921 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005922 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005923 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005924 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005925 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005926 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005927 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005928 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005929 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005930 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005931 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005932 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005933 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005934 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005935 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5936 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005937#ifdef CONFIG_64BIT
5938 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5939 return btrfs_ioctl_set_received_subvol_32(file, argp);
5940#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005941 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005942 return _btrfs_ioctl_send(file, argp, false);
5943#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5944 case BTRFS_IOC_SEND_32:
5945 return _btrfs_ioctl_send(file, argp, true);
5946#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005947 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005948 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005949 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005950 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005951 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005952 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005953 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005954 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005955 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005956 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005957 case BTRFS_IOC_QUOTA_RESCAN:
5958 return btrfs_ioctl_quota_rescan(file, argp);
5959 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5960 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005961 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5962 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005963 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005964 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005965 case BTRFS_IOC_GET_FSLABEL:
5966 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005967 case BTRFS_IOC_SET_FSLABEL:
5968 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005969 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005970 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005971 case BTRFS_IOC_GET_FEATURES:
5972 return btrfs_ioctl_get_features(file, argp);
5973 case BTRFS_IOC_SET_FEATURES:
5974 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005975 case FS_IOC_FSGETXATTR:
5976 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005977 case FS_IOC_FSSETXATTR:
5978 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005979 case BTRFS_IOC_GET_SUBVOL_INFO:
5980 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005981 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5982 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005983 case BTRFS_IOC_INO_LOOKUP_USER:
5984 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005985 }
5986
5987 return -ENOTTY;
5988}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005989
5990#ifdef CONFIG_COMPAT
5991long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5992{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005993 /*
5994 * These all access 32-bit values anyway so no further
5995 * handling is necessary.
5996 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005997 switch (cmd) {
5998 case FS_IOC32_GETFLAGS:
5999 cmd = FS_IOC_GETFLAGS;
6000 break;
6001 case FS_IOC32_SETFLAGS:
6002 cmd = FS_IOC_SETFLAGS;
6003 break;
6004 case FS_IOC32_GETVERSION:
6005 cmd = FS_IOC_GETVERSION;
6006 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00006007 }
6008
6009 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
6010}
6011#endif