blob: c21cc408556d2c31c535e4be9e2e96336aab2c2d [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008#include <linux/file.h>
9#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040010#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040011#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040014#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040015#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040016#include <linux/mount.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040017#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040018#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/compat.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020022#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000024#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020025#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000026#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070027#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050028#include <linux/iversion.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040029#include "ctree.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040033#include "print-tree.h"
34#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080036#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020037#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040038#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020039#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010040#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000041#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040042#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070043#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000044#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080045#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046
Hugo Millsabccd002014-01-30 20:17:00 +000047#ifdef CONFIG_64BIT
48/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
49 * structures are incorrect, as the timespec structure from userspace
50 * is 4 bytes too small. We define these alternatives here to teach
51 * the kernel about the 32-bit struct packing.
52 */
53struct btrfs_ioctl_timespec_32 {
54 __u64 sec;
55 __u32 nsec;
56} __attribute__ ((__packed__));
57
58struct btrfs_ioctl_received_subvol_args_32 {
59 char uuid[BTRFS_UUID_SIZE]; /* in */
60 __u64 stransid; /* in */
61 __u64 rtransid; /* out */
62 struct btrfs_ioctl_timespec_32 stime; /* in */
63 struct btrfs_ioctl_timespec_32 rtime; /* out */
64 __u64 flags; /* in */
65 __u64 reserved[16]; /* in */
66} __attribute__ ((__packed__));
67
68#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
69 struct btrfs_ioctl_received_subvol_args_32)
70#endif
71
Josef Bacik2351f432017-09-27 10:43:13 -040072#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
73struct btrfs_ioctl_send_args_32 {
74 __s64 send_fd; /* in */
75 __u64 clone_sources_count; /* in */
76 compat_uptr_t clone_sources; /* in */
77 __u64 parent_root; /* in */
78 __u64 flags; /* in */
79 __u64 reserved[4]; /* in */
80} __attribute__ ((__packed__));
81
82#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
83 struct btrfs_ioctl_send_args_32)
84#endif
Hugo Millsabccd002014-01-30 20:17:00 +000085
Mark Fasheh416161d2013-08-06 11:42:51 -070086static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070087 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
88 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070089
Christoph Hellwig6cbff002009-04-17 10:37:41 +020090/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020091static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
92 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093{
David Sterba1905a0f2018-03-26 18:52:15 +020094 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020096 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097 return flags & ~FS_DIRSYNC_FL;
98 else
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
100}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400101
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
104 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200106static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107{
108 unsigned int iflags = 0;
109
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
124
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900125 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000126 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129
130 return iflags;
131}
132
133/*
134 * Update inode->i_flags based on the btrfs internal flags.
135 */
David Sterba7b6a2212018-03-26 18:40:21 +0200136void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200137{
David Sterba5c57b8b2018-04-23 15:45:18 +0200138 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100139 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140
David Sterba5c57b8b2018-04-23 15:45:18 +0200141 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100142 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200145 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200147 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200149 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_DIRSYNC;
151
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
154 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200155}
156
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
David Sterba5c57b8b2018-04-23 15:45:18 +0200159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
David Sterba5ba76ab2018-03-26 18:52:15 +0200167/* Check if @flags are a supported and valid set of FS_*_FL flags */
168static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000169{
170 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
171 FS_NOATIME_FL | FS_NODUMP_FL | \
172 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000173 FS_NOCOMP_FL | FS_COMPR_FL |
174 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 return -EOPNOTSUPP;
176
177 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
178 return -EINVAL;
179
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return 0;
181}
182
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200183static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
184{
Al Viro496ad9a2013-01-23 17:07:38 -0500185 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400186 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200187 struct btrfs_inode *binode = BTRFS_I(inode);
188 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200189 struct btrfs_trans_handle *trans;
David Sterba5c57b8b2018-04-23 15:45:18 +0200190 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191 int ret;
David Sterba5c57b8b2018-04-23 15:45:18 +0200192 u64 old_flags;
193 unsigned int old_i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600194 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200195
David Sterbabd60ea02014-01-16 15:50:22 +0100196 if (!inode_owner_or_capable(inode))
197 return -EPERM;
198
Li Zefanb83cc962010-12-20 16:04:08 +0800199 if (btrfs_root_readonly(root))
200 return -EROFS;
201
David Sterba5c57b8b2018-04-23 15:45:18 +0200202 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203 return -EFAULT;
204
David Sterba5c57b8b2018-04-23 15:45:18 +0200205 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000206 if (ret)
207 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200208
Jan Karae7848682012-06-12 16:20:32 +0200209 ret = mnt_want_write_file(file);
210 if (ret)
211 return ret;
212
Al Viro59551022016-01-22 15:40:57 -0500213 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200214
David Sterba5c57b8b2018-04-23 15:45:18 +0200215 old_flags = binode->flags;
216 old_i_flags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600217 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800218
David Sterba5c57b8b2018-04-23 15:45:18 +0200219 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
220 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
221 if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222 if (!capable(CAP_LINUX_IMMUTABLE)) {
223 ret = -EPERM;
224 goto out_unlock;
225 }
226 }
227
David Sterba5c57b8b2018-04-23 15:45:18 +0200228 if (fsflags & FS_SYNC_FL)
229 binode->flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200230 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200231 binode->flags &= ~BTRFS_INODE_SYNC;
232 if (fsflags & FS_IMMUTABLE_FL)
233 binode->flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200234 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200235 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
236 if (fsflags & FS_APPEND_FL)
237 binode->flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200239 binode->flags &= ~BTRFS_INODE_APPEND;
240 if (fsflags & FS_NODUMP_FL)
241 binode->flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200242 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200243 binode->flags &= ~BTRFS_INODE_NODUMP;
244 if (fsflags & FS_NOATIME_FL)
245 binode->flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200246 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200247 binode->flags &= ~BTRFS_INODE_NOATIME;
248 if (fsflags & FS_DIRSYNC_FL)
249 binode->flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200250 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200251 binode->flags &= ~BTRFS_INODE_DIRSYNC;
252 if (fsflags & FS_NOCOW_FL) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600253 if (S_ISREG(mode)) {
254 /*
255 * It's safe to turn csums off here, no extents exist.
256 * Otherwise we want the flag to reflect the real COW
257 * status of the file and will not set it.
258 */
259 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200260 binode->flags |= BTRFS_INODE_NODATACOW
261 | BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600262 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200263 binode->flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600264 }
265 } else {
266 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400267 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600268 */
269 if (S_ISREG(mode)) {
270 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200271 binode->flags &= ~(BTRFS_INODE_NODATACOW
David Sterba7e97b8d2012-09-07 05:56:55 -0600272 | BTRFS_INODE_NODATASUM);
273 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200274 binode->flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600275 }
276 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200277
Liu Bo75e7cb72011-03-22 10:12:20 +0000278 /*
279 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
280 * flag may be changed automatically if compression code won't make
281 * things smaller.
282 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200283 if (fsflags & FS_NOCOMP_FL) {
284 binode->flags &= ~BTRFS_INODE_COMPRESS;
285 binode->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000286
287 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
288 if (ret && ret != -ENODATA)
289 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200290 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000291 const char *comp;
292
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700293 if (IS_SWAPFILE(inode)) {
294 ret = -ETXTBSY;
295 goto out_unlock;
296 }
297
David Sterba5c57b8b2018-04-23 15:45:18 +0200298 binode->flags |= BTRFS_INODE_COMPRESS;
299 binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000300
David Sterba93370502017-10-31 17:32:41 +0100301 comp = btrfs_compress_type2str(fs_info->compress_type);
302 if (!comp || comp[0] == 0)
303 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
304
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000305 ret = btrfs_set_prop(inode, "btrfs.compression",
306 comp, strlen(comp), 0);
307 if (ret)
308 goto out_drop;
309
Li Zefanebcb9042011-04-15 03:03:17 +0000310 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100311 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
312 if (ret && ret != -ENODATA)
313 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200314 binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000315 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200316
Li Zefan4da6f1a2011-12-29 13:39:50 +0800317 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800318 if (IS_ERR(trans)) {
319 ret = PTR_ERR(trans);
320 goto out_drop;
321 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322
David Sterba7b6a2212018-03-26 18:40:21 +0200323 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400324 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700325 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200326 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200327
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400328 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800329 out_drop:
330 if (ret) {
David Sterba5c57b8b2018-04-23 15:45:18 +0200331 binode->flags = old_flags;
332 inode->i_flags = old_i_flags;
Li Zefanf062abf02011-12-29 13:36:45 +0800333 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200334
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500336 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200337 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000338 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200339}
340
David Sterba19f93b32018-03-26 19:42:05 +0200341/*
342 * Translate btrfs internal inode flags to xflags as expected by the
343 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
344 * silently dropped.
345 */
346static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
347{
348 unsigned int xflags = 0;
349
350 if (flags & BTRFS_INODE_APPEND)
351 xflags |= FS_XFLAG_APPEND;
352 if (flags & BTRFS_INODE_IMMUTABLE)
353 xflags |= FS_XFLAG_IMMUTABLE;
354 if (flags & BTRFS_INODE_NOATIME)
355 xflags |= FS_XFLAG_NOATIME;
356 if (flags & BTRFS_INODE_NODUMP)
357 xflags |= FS_XFLAG_NODUMP;
358 if (flags & BTRFS_INODE_SYNC)
359 xflags |= FS_XFLAG_SYNC;
360
361 return xflags;
362}
363
364/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
365static int check_xflags(unsigned int flags)
366{
367 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
368 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
369 return -EOPNOTSUPP;
370 return 0;
371}
372
David Sterbae4202ac2018-03-26 19:51:16 +0200373/*
374 * Set the xflags from the internal inode flags. The remaining items of fsxattr
375 * are zeroed.
376 */
377static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
378{
379 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
380 struct fsxattr fa;
381
382 memset(&fa, 0, sizeof(fa));
383 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
384
385 if (copy_to_user(arg, &fa, sizeof(fa)))
386 return -EFAULT;
387
388 return 0;
389}
390
David Sterba025f2122018-03-26 19:51:16 +0200391static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
392{
393 struct inode *inode = file_inode(file);
394 struct btrfs_inode *binode = BTRFS_I(inode);
395 struct btrfs_root *root = binode->root;
396 struct btrfs_trans_handle *trans;
397 struct fsxattr fa;
398 unsigned old_flags;
399 unsigned old_i_flags;
400 int ret = 0;
401
402 if (!inode_owner_or_capable(inode))
403 return -EPERM;
404
405 if (btrfs_root_readonly(root))
406 return -EROFS;
407
408 memset(&fa, 0, sizeof(fa));
409 if (copy_from_user(&fa, arg, sizeof(fa)))
410 return -EFAULT;
411
412 ret = check_xflags(fa.fsx_xflags);
413 if (ret)
414 return ret;
415
416 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
417 return -EOPNOTSUPP;
418
419 ret = mnt_want_write_file(file);
420 if (ret)
421 return ret;
422
423 inode_lock(inode);
424
425 old_flags = binode->flags;
426 old_i_flags = inode->i_flags;
427
428 /* We need the capabilities to change append-only or immutable inode */
429 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
430 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
431 !capable(CAP_LINUX_IMMUTABLE)) {
432 ret = -EPERM;
433 goto out_unlock;
434 }
435
436 if (fa.fsx_xflags & FS_XFLAG_SYNC)
437 binode->flags |= BTRFS_INODE_SYNC;
438 else
439 binode->flags &= ~BTRFS_INODE_SYNC;
440 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
441 binode->flags |= BTRFS_INODE_IMMUTABLE;
442 else
443 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
444 if (fa.fsx_xflags & FS_XFLAG_APPEND)
445 binode->flags |= BTRFS_INODE_APPEND;
446 else
447 binode->flags &= ~BTRFS_INODE_APPEND;
448 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
449 binode->flags |= BTRFS_INODE_NODUMP;
450 else
451 binode->flags &= ~BTRFS_INODE_NODUMP;
452 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
453 binode->flags |= BTRFS_INODE_NOATIME;
454 else
455 binode->flags &= ~BTRFS_INODE_NOATIME;
456
457 /* 1 item for the inode */
458 trans = btrfs_start_transaction(root, 1);
459 if (IS_ERR(trans)) {
460 ret = PTR_ERR(trans);
461 goto out_unlock;
462 }
463
464 btrfs_sync_inode_flags_to_i_flags(inode);
465 inode_inc_iversion(inode);
466 inode->i_ctime = current_time(inode);
467 ret = btrfs_update_inode(trans, root, inode);
468
469 btrfs_end_transaction(trans);
470
471out_unlock:
472 if (ret) {
473 binode->flags = old_flags;
474 inode->i_flags = old_i_flags;
475 }
476
477 inode_unlock(inode);
478 mnt_drop_write_file(file);
479
480 return ret;
481}
482
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200483static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
484{
Al Viro496ad9a2013-01-23 17:07:38 -0500485 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200486
487 return put_user(inode->i_generation, arg);
488}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489
Li Dongyangf7039b12011-03-24 10:24:28 +0000490static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
491{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400492 struct inode *inode = file_inode(file);
493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000494 struct btrfs_device *device;
495 struct request_queue *q;
496 struct fstrim_range range;
497 u64 minlen = ULLONG_MAX;
498 u64 num_devices = 0;
499 int ret;
500
501 if (!capable(CAP_SYS_ADMIN))
502 return -EPERM;
503
Xiao Guangrong1f781602011-04-20 10:09:16 +0000504 rcu_read_lock();
505 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
506 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000507 if (!device->bdev)
508 continue;
509 q = bdev_get_queue(device->bdev);
510 if (blk_queue_discard(q)) {
511 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100512 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000513 minlen);
514 }
515 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000516 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200517
Li Dongyangf7039b12011-03-24 10:24:28 +0000518 if (!num_devices)
519 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000520 if (copy_from_user(&range, arg, sizeof(range)))
521 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800522
523 /*
524 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
525 * block group is in the logical address space, which can be any
526 * sectorsize aligned bytenr in the range [0, U64_MAX].
527 */
528 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200529 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000530
531 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400532 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000533 if (ret < 0)
534 return ret;
535
536 if (copy_to_user(arg, &range, sizeof(range)))
537 return -EFAULT;
538
539 return 0;
540}
541
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200542int btrfs_is_empty_uuid(u8 *uuid)
543{
Chris Mason46e0f662013-11-15 12:14:55 +0100544 int i;
545
546 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
547 if (uuid[i])
548 return 0;
549 }
550 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200551}
552
Miao Xied5c12072013-02-28 10:04:33 +0000553static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400554 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100555 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200556 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000557 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400559 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 struct btrfs_trans_handle *trans;
561 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100562 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400563 struct btrfs_inode_item *inode_item;
564 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000565 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400566 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000567 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700568 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900569 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570 int ret;
571 int err;
572 u64 objectid;
573 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500574 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200575 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400576
David Sterba49a3c4d2016-03-24 17:49:22 +0100577 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
578 if (!root_item)
579 return -ENOMEM;
580
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400581 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400582 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100583 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000584
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800585 /*
586 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400587 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800588 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100589 if (btrfs_qgroup_level(objectid)) {
590 ret = -ENOSPC;
591 goto fail_free;
592 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800593
Miao Xied5c12072013-02-28 10:04:33 +0000594 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400595 /*
Miao Xied5c12072013-02-28 10:04:33 +0000596 * The same as the snapshot creation, please see the comment
597 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400598 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800599 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000600 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100601 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400602
Miao Xied5c12072013-02-28 10:04:33 +0000603 trans = btrfs_start_transaction(root, 0);
604 if (IS_ERR(trans)) {
605 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100606 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100607 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000608 }
609 trans->block_rsv = &block_rsv;
610 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400611
Lu Fengqia93774222018-07-18 14:45:41 +0800612 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200613 if (ret)
614 goto fail;
615
David Sterba4d75f8a2014-06-15 01:54:12 +0200616 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400617 if (IS_ERR(leaf)) {
618 ret = PTR_ERR(leaf);
619 goto fail;
620 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400621
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400622 btrfs_mark_buffer_dirty(leaf);
623
David Sterba49a3c4d2016-03-24 17:49:22 +0100624 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800625 btrfs_set_stack_inode_generation(inode_item, 1);
626 btrfs_set_stack_inode_size(inode_item, 3);
627 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400628 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400629 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800630 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400631
David Sterba49a3c4d2016-03-24 17:49:22 +0100632 btrfs_set_root_flags(root_item, 0);
633 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800634 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000635
David Sterba49a3c4d2016-03-24 17:49:22 +0100636 btrfs_set_root_bytenr(root_item, leaf->start);
637 btrfs_set_root_generation(root_item, trans->transid);
638 btrfs_set_root_level(root_item, 0);
639 btrfs_set_root_refs(root_item, 1);
640 btrfs_set_root_used(root_item, leaf->len);
641 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400642
David Sterba49a3c4d2016-03-24 17:49:22 +0100643 btrfs_set_root_generation_v2(root_item,
644 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200645 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100646 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
647 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
648 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
649 root_item->ctime = root_item->otime;
650 btrfs_set_root_ctransid(root_item, trans->transid);
651 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400652
Chris Mason925baed2008-06-25 16:01:30 -0400653 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654 free_extent_buffer(leaf);
655 leaf = NULL;
656
David Sterba49a3c4d2016-03-24 17:49:22 +0100657 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400658
659 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400660 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200661 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400662 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100663 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664 if (ret)
665 goto fail;
666
Yan, Zheng76dda932009-09-21 16:00:26 -0400667 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400668 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100669 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100670 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400671 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100672 goto fail;
673 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400674
675 btrfs_record_root_in_trans(trans, new_root);
676
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000677 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700678 if (ret) {
679 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400680 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700681 goto fail;
682 }
683
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530684 mutex_lock(&new_root->objectid_mutex);
685 new_root->highest_objectid = new_dirid;
686 mutex_unlock(&new_root->objectid_mutex);
687
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400688 /*
689 * insert the directory item
690 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200691 ret = btrfs_set_inode_index(BTRFS_I(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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100694 goto fail;
695 }
Chris Mason3de45862008-11-17 21:02:50 -0500696
Lu Fengqi684572d2018-08-04 21:10:57 +0800697 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500698 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400700 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400701 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100702 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500703
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200704 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500705 ret = btrfs_update_inode(trans, root, dir);
706 BUG_ON(ret);
707
Lu Fengqi6025c192018-08-01 11:32:29 +0800708 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200709 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500710 BUG_ON(ret);
711
Lu Fengqicdb345a2018-05-29 15:01:53 +0800712 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400713 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200714 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400715 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200716
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400717fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100718 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000719 trans->block_rsv = NULL;
720 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100721 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800722
Sage Weil72fd0322010-10-29 15:41:32 -0400723 if (async_transid) {
724 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400725 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000726 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400727 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400728 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400729 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400730 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400731 if (err && !ret)
732 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500733
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900734 if (!ret) {
735 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800736 if (IS_ERR(inode))
737 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900738 d_instantiate(dentry, inode);
739 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400740 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100741
742fail_free:
743 kfree(root_item);
744 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400745}
746
Miao Xiee9662f72013-02-28 10:01:15 +0000747static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100748 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000749 u64 *async_transid, bool readonly,
750 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400751{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400752 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000753 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400754 struct btrfs_pending_snapshot *pending_snapshot;
755 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000756 int ret;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800757 bool snapshot_force_cow = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400758
Miao Xie27cdeb72014-04-02 19:51:05 +0800759 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400760 return -EINVAL;
761
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700762 if (atomic_read(&root->nr_swapfiles)) {
763 btrfs_warn(fs_info,
764 "cannot snapshot subvolume with active swapfile");
765 return -ETXTBSY;
766 }
767
David Sterba23269bf2017-02-13 11:03:44 +0100768 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100769 if (!pending_snapshot)
770 return -ENOMEM;
771
David Sterbab0c0ea62015-11-10 18:54:00 +0100772 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100773 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100774 pending_snapshot->path = btrfs_alloc_path();
775 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100776 ret = -ENOMEM;
777 goto free_pending;
778 }
779
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800780 /*
781 * Force new buffered writes to reserve space even when NOCOW is
782 * possible. This is to avoid later writeback (running dealloc) to
783 * fallback to COW mode and unexpectedly fail with ENOSPC.
784 */
David Sterbaea14b57f2017-06-22 02:19:11 +0200785 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100786 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600787 /* wait for no snapshot writes */
788 wait_event(root->subv_writers->wait,
789 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800790
Ethan Lien3cd24c62018-11-01 14:49:03 +0800791 ret = btrfs_start_delalloc_snapshot(root);
Miao Xie6a038432013-05-15 07:48:24 +0000792 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100793 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000794
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800795 /*
796 * All previous writes have started writeback in NOCOW mode, so now
797 * we force future writes to fallback to COW mode during snapshot
798 * creation.
799 */
800 atomic_inc(&root->snapshot_force_cow);
801 snapshot_force_cow = true;
802
Chris Mason6374e57a2017-06-23 09:48:21 -0700803 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000804
Miao Xie66d8f3d2012-09-06 04:02:28 -0600805 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
806 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000807 /*
808 * 1 - parent dir inode
809 * 2 - dir entries
810 * 1 - root item
811 * 2 - root ref/backref
812 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200813 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000814 */
815 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200816 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400817 false);
Miao Xied5c12072013-02-28 10:04:33 +0000818 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100819 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000820
Yan, Zhenga22285a2010-05-16 10:48:46 -0400821 pending_snapshot->dentry = dentry;
822 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800823 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000824 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000825 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400826
Miao Xied5c12072013-02-28 10:04:33 +0000827 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400828 if (IS_ERR(trans)) {
829 ret = PTR_ERR(trans);
830 goto fail;
831 }
832
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400833 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400834 list_add(&pending_snapshot->list,
835 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400836 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400837 if (async_transid) {
838 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400839 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000840 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400841 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400842 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400843 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400844 }
Miao Xieaec80302013-03-04 09:44:29 +0000845 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400846 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400847
848 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400849 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000850 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400851
Chris Masond3797302014-10-15 13:50:56 -0700852 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
853 if (ret)
854 goto fail;
855
David Howells2b0143b2015-03-17 22:25:59 +0000856 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000857 if (IS_ERR(inode)) {
858 ret = PTR_ERR(inode);
859 goto fail;
860 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900861
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000862 d_instantiate(dentry, inode);
863 ret = 0;
864fail:
David Sterba7775c812017-02-10 19:18:18 +0100865 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100866dec_and_free:
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800867 if (snapshot_force_cow)
868 atomic_dec(&root->snapshot_force_cow);
David Sterbaea14b57f2017-06-22 02:19:11 +0200869 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100870 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100871free_pending:
872 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100873 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100874 kfree(pending_snapshot);
875
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400876 return ret;
877}
878
Sage Weil4260f7c2010-10-29 15:46:43 -0400879/* copy of may_delete in fs/namei.c()
880 * Check whether we can remove a link victim from directory dir, check
881 * whether the type of victim is right.
882 * 1. We can't do it if dir is read-only (done in permission())
883 * 2. We should have write and exec permissions on dir
884 * 3. We can't remove anything from append-only dir
885 * 4. We can't do anything with immutable dir (done in permission())
886 * 5. If the sticky bit on dir is set we should either
887 * a. be owner of dir, or
888 * b. be owner of victim, or
889 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400890 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400891 * links pointing to it.
892 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
893 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
894 * 9. We can't remove a root or mountpoint.
895 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
896 * nfs_async_unlink().
897 */
898
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530899static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400900{
901 int error;
902
David Howells2b0143b2015-03-17 22:25:59 +0000903 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400904 return -ENOENT;
905
David Howells2b0143b2015-03-17 22:25:59 +0000906 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400907 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400908
909 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
910 if (error)
911 return error;
912 if (IS_APPEND(dir))
913 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000914 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
915 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400916 return -EPERM;
917 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000918 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400919 return -ENOTDIR;
920 if (IS_ROOT(victim))
921 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000922 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400923 return -EISDIR;
924 if (IS_DEADDIR(dir))
925 return -ENOENT;
926 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
927 return -EBUSY;
928 return 0;
929}
930
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400931/* copy of may_create in fs/namei.c() */
932static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
933{
David Howells2b0143b2015-03-17 22:25:59 +0000934 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400935 return -EEXIST;
936 if (IS_DEADDIR(dir))
937 return -ENOENT;
938 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
939}
940
941/*
942 * Create a new subvolume below @parent. This is largely modeled after
943 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
944 * inside this filesystem so it's quite a bit simpler.
945 */
Al Viro92872092016-11-20 19:34:31 -0500946static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100947 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400948 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200949 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000950 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400951{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400952 struct inode *dir = d_inode(parent->dentry);
953 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400954 struct dentry *dentry;
955 int error;
956
Al Viro00235412016-05-26 00:05:12 -0400957 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
958 if (error == -EINTR)
959 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400960
961 dentry = lookup_one_len(name, parent->dentry, namelen);
962 error = PTR_ERR(dentry);
963 if (IS_ERR(dentry))
964 goto out_unlock;
965
Yan, Zheng76dda932009-09-21 16:00:26 -0400966 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400967 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600968 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400969
Chris Mason9c520572012-12-17 14:26:57 -0500970 /*
971 * even if this name doesn't exist, we may get hash collisions.
972 * check for them now when we can safely fail
973 */
974 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
975 dir->i_ino, name,
976 namelen);
977 if (error)
978 goto out_dput;
979
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400980 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400981
982 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
983 goto out_up_read;
984
Chris Mason3de45862008-11-17 21:02:50 -0500985 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100986 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200987 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500988 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000989 error = create_subvol(dir, dentry, name, namelen,
990 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500991 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400992 if (!error)
993 fsnotify_mkdir(dir, dentry);
994out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400995 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400996out_dput:
997 dput(dentry);
998out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500999 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001000 return error;
1001}
1002
Chris Mason4cb53002011-05-24 15:35:30 -04001003/*
1004 * When we're defragging a range, we don't want to kick it off again
1005 * if it is really just waiting for delalloc to send it down.
1006 * If we find a nice big extent or delalloc range for the bytes in the
1007 * file you want to defrag, we return 0 to let you know to skip this
1008 * part of the file
1009 */
David Sterbaaab110a2014-07-29 17:32:10 +02001010static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001011{
1012 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1013 struct extent_map *em = NULL;
1014 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1015 u64 end;
1016
1017 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001018 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001019 read_unlock(&em_tree->lock);
1020
1021 if (em) {
1022 end = extent_map_end(em);
1023 free_extent_map(em);
1024 if (end - offset > thresh)
1025 return 0;
1026 }
1027 /* if we already have a nice delalloc here, just stop */
1028 thresh /= 2;
1029 end = count_range_bits(io_tree, &offset, offset + thresh,
1030 thresh, EXTENT_DELALLOC, 1);
1031 if (end >= thresh)
1032 return 0;
1033 return 1;
1034}
1035
1036/*
1037 * helper function to walk through a file and find extents
1038 * newer than a specific transid, and smaller than thresh.
1039 *
1040 * This is used by the defragging code to find new and small
1041 * extents
1042 */
1043static int find_new_extents(struct btrfs_root *root,
1044 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001045 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001046{
1047 struct btrfs_path *path;
1048 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001049 struct extent_buffer *leaf;
1050 struct btrfs_file_extent_item *extent;
1051 int type;
1052 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001053 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001054
1055 path = btrfs_alloc_path();
1056 if (!path)
1057 return -ENOMEM;
1058
David Sterbaa4689d22011-05-31 17:08:14 +00001059 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001060 min_key.type = BTRFS_EXTENT_DATA_KEY;
1061 min_key.offset = *off;
1062
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301063 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001064 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001065 if (ret != 0)
1066 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001067process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001068 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001069 goto none;
1070 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1071 goto none;
1072
1073 leaf = path->nodes[0];
1074 extent = btrfs_item_ptr(leaf, path->slots[0],
1075 struct btrfs_file_extent_item);
1076
1077 type = btrfs_file_extent_type(leaf, extent);
1078 if (type == BTRFS_FILE_EXTENT_REG &&
1079 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1080 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1081 *off = min_key.offset;
1082 btrfs_free_path(path);
1083 return 0;
1084 }
1085
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001086 path->slots[0]++;
1087 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1088 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1089 goto process_slot;
1090 }
1091
Chris Mason4cb53002011-05-24 15:35:30 -04001092 if (min_key.offset == (u64)-1)
1093 goto none;
1094
1095 min_key.offset++;
1096 btrfs_release_path(path);
1097 }
1098none:
1099 btrfs_free_path(path);
1100 return -ENOENT;
1101}
1102
Li Zefan6c282eb2012-06-11 16:03:35 +08001103static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001104{
1105 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001106 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001107 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001108 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001109
1110 /*
1111 * hopefully we have this extent in the tree already, try without
1112 * the full extent lock
1113 */
1114 read_lock(&em_tree->lock);
1115 em = lookup_extent_mapping(em_tree, start, len);
1116 read_unlock(&em_tree->lock);
1117
1118 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001119 struct extent_state *cached = NULL;
1120 u64 end = start + len - 1;
1121
Chris Mason940100a2010-03-10 10:52:59 -05001122 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001123 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001124 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001125 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001126
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001127 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001128 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001129 }
1130
Li Zefan6c282eb2012-06-11 16:03:35 +08001131 return em;
1132}
1133
1134static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1135{
1136 struct extent_map *next;
1137 bool ret = true;
1138
1139 /* this is the last extent */
1140 if (em->start + em->len >= i_size_read(inode))
1141 return false;
1142
1143 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001144 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1145 ret = false;
1146 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001147 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001148 ret = false;
1149
1150 free_extent_map(next);
1151 return ret;
1152}
1153
David Sterbaaab110a2014-07-29 17:32:10 +02001154static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001155 u64 *last_len, u64 *skip, u64 *defrag_end,
1156 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001157{
1158 struct extent_map *em;
1159 int ret = 1;
1160 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001161 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001162
1163 /*
1164 * make sure that once we start defragging an extent, we keep on
1165 * defragging it
1166 */
1167 if (start < *defrag_end)
1168 return 1;
1169
1170 *skip = 0;
1171
1172 em = defrag_lookup_extent(inode, start);
1173 if (!em)
1174 return 0;
1175
Chris Mason940100a2010-03-10 10:52:59 -05001176 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001177 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001178 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001179 goto out;
1180 }
1181
Liu Bo4a3560c2015-08-07 16:48:41 +08001182 if (!*defrag_end)
1183 prev_mergeable = false;
1184
Li Zefan6c282eb2012-06-11 16:03:35 +08001185 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001186 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001187 * we hit a real extent, if it is big or the next extent is not a
1188 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001189 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001190 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001191 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001192 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001193out:
Chris Mason940100a2010-03-10 10:52:59 -05001194 /*
1195 * last_len ends up being a counter of how many bytes we've defragged.
1196 * every time we choose not to defrag an extent, we reset *last_len
1197 * so that the next tiny extent will force a defrag.
1198 *
1199 * The end result of this is that tiny extents before a single big
1200 * extent will force at least part of that big extent to be defragged.
1201 */
1202 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001203 *defrag_end = extent_map_end(em);
1204 } else {
1205 *last_len = 0;
1206 *skip = extent_map_end(em);
1207 *defrag_end = 0;
1208 }
1209
1210 free_extent_map(em);
1211 return ret;
1212}
1213
Chris Mason4cb53002011-05-24 15:35:30 -04001214/*
1215 * it doesn't do much good to defrag one or two pages
1216 * at a time. This pulls in a nice chunk of pages
1217 * to COW and defrag.
1218 *
1219 * It also makes sure the delalloc code has enough
1220 * dirty data to avoid making new small extents as part
1221 * of the defrag
1222 *
1223 * It's a good idea to start RA on this range
1224 * before calling this.
1225 */
1226static int cluster_pages_for_defrag(struct inode *inode,
1227 struct page **pages,
1228 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001229 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001230{
Chris Mason4cb53002011-05-24 15:35:30 -04001231 unsigned long file_end;
1232 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001233 u64 page_start;
1234 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001235 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001236 int ret;
1237 int i;
1238 int i_done;
1239 struct btrfs_ordered_extent *ordered;
1240 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001241 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001242 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001243 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001244
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001245 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001246 if (!isize || start_index > file_end)
1247 return 0;
1248
1249 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001250
Qu Wenruo364ecf32017-02-27 15:10:38 +08001251 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001252 start_index << PAGE_SHIFT,
1253 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001254 if (ret)
1255 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001256 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001257 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001258
1259 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001260 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001261 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001262again:
Josef Bacika94733d2011-07-11 10:47:06 -04001263 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001264 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001265 if (!page)
1266 break;
1267
Miao Xie600a45e2012-02-16 15:01:24 +08001268 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001269 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001270 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001271 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001272 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001273 ordered = btrfs_lookup_ordered_extent(inode,
1274 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001275 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001276 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001277 if (!ordered)
1278 break;
1279
1280 unlock_page(page);
1281 btrfs_start_ordered_extent(inode, ordered, 1);
1282 btrfs_put_ordered_extent(ordered);
1283 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001284 /*
1285 * we unlocked the page above, so we need check if
1286 * it was released or not.
1287 */
1288 if (page->mapping != inode->i_mapping) {
1289 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001290 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001291 goto again;
1292 }
Miao Xie600a45e2012-02-16 15:01:24 +08001293 }
1294
Chris Mason4cb53002011-05-24 15:35:30 -04001295 if (!PageUptodate(page)) {
1296 btrfs_readpage(NULL, page);
1297 lock_page(page);
1298 if (!PageUptodate(page)) {
1299 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001300 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001301 ret = -EIO;
1302 break;
1303 }
1304 }
Miao Xie600a45e2012-02-16 15:01:24 +08001305
Miao Xie600a45e2012-02-16 15:01:24 +08001306 if (page->mapping != inode->i_mapping) {
1307 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001308 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001309 goto again;
1310 }
1311
Chris Mason4cb53002011-05-24 15:35:30 -04001312 pages[i] = page;
1313 i_done++;
1314 }
1315 if (!i_done || ret)
1316 goto out;
1317
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001318 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001319 goto out;
1320
1321 /*
1322 * so now we have a nice long stream of locked
1323 * and up to date pages, lets wait on them
1324 */
1325 for (i = 0; i < i_done; i++)
1326 wait_on_page_writeback(pages[i]);
1327
1328 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001329 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001330
1331 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001332 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001333 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1334 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001335 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001336 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001337
Liu Bo1f12bd02012-03-29 09:57:44 -04001338 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001339 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001340 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001341 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001342 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001343 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001344 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001345 }
1346
1347
Liu Bo9e8a4a82012-09-05 19:10:51 -06001348 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001349 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001350
1351 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001352 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001353
1354 for (i = 0; i < i_done; i++) {
1355 clear_page_dirty_for_io(pages[i]);
1356 ClearPageChecked(pages[i]);
1357 set_page_extent_mapped(pages[i]);
1358 set_page_dirty(pages[i]);
1359 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001360 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001361 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001362 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1363 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001364 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001365 return i_done;
1366out:
1367 for (i = 0; i < i_done; i++) {
1368 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001369 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001370 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001371 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001372 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001373 page_cnt << PAGE_SHIFT, true);
1374 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1375 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001376 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001377 return ret;
1378
1379}
1380
1381int btrfs_defrag_file(struct inode *inode, struct file *file,
1382 struct btrfs_ioctl_defrag_range_args *range,
1383 u64 newer_than, unsigned long max_to_defrag)
1384{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001385 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001386 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001387 struct file_ra_state *ra = NULL;
1388 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001389 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001390 u64 last_len = 0;
1391 u64 skip = 0;
1392 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001393 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001394 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001395 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001396 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001397 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001398 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001399 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001400 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001401 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001402 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001403 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001404 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001405
Liu Bo0abd5b12013-04-16 09:20:28 +00001406 if (isize == 0)
1407 return 0;
1408
1409 if (range->start >= isize)
1410 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001411
David Sterba1e2ef462017-07-17 20:01:59 +02001412 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001413 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1414 return -EINVAL;
1415 if (range->compress_type)
1416 compress_type = range->compress_type;
1417 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001418
Liu Bo0abd5b12013-04-16 09:20:28 +00001419 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001420 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001421
Chris Mason4cb53002011-05-24 15:35:30 -04001422 /*
David Sterba0a52d102017-06-22 03:22:58 +02001423 * If we were not given a file, allocate a readahead context. As
1424 * readahead is just an optimization, defrag will work without it so
1425 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001426 */
1427 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001428 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001429 if (ra)
1430 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001431 } else {
1432 ra = &file->f_ra;
1433 }
1434
David Sterba63e727e2017-06-22 03:13:02 +02001435 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001436 if (!pages) {
1437 ret = -ENOMEM;
1438 goto out_ra;
1439 }
1440
1441 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001442 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001443 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001444 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001445 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001446 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001447 }
1448
Chris Mason4cb53002011-05-24 15:35:30 -04001449 if (newer_than) {
1450 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001451 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001452 if (!ret) {
1453 range->start = newer_off;
1454 /*
1455 * we always align our defrag to help keep
1456 * the extents in the file evenly spaced
1457 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001458 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001459 } else
1460 goto out_ra;
1461 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001462 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001463 }
1464 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301465 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001466
Li Zefan2a0f7f52011-10-10 15:43:34 -04001467 /*
1468 * make writeback starts from i, so the defrag range can be
1469 * written sequentially.
1470 */
1471 if (i < inode->i_mapping->writeback_index)
1472 inode->i_mapping->writeback_index = i;
1473
Chris Masonf7f43cc2011-10-11 11:41:40 -04001474 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001475 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001476 /*
1477 * make sure we stop running if someone unmounts
1478 * the FS
1479 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001480 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001481 break;
1482
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001483 if (btrfs_defrag_cancelled(fs_info)) {
1484 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001485 ret = -EAGAIN;
1486 break;
1487 }
1488
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001489 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001490 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001491 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001492 unsigned long next;
1493 /*
1494 * the should_defrag function tells us how much to skip
1495 * bump our counter by the suggested amount
1496 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001497 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001498 i = max(i + 1, next);
1499 continue;
1500 }
Li Zefan008873e2011-09-02 15:57:07 +08001501
1502 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001503 cluster = (PAGE_ALIGN(defrag_end) >>
1504 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001505 cluster = min(cluster, max_cluster);
1506 } else {
1507 cluster = max_cluster;
1508 }
1509
Li Zefan008873e2011-09-02 15:57:07 +08001510 if (i + cluster > ra_index) {
1511 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001512 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001513 page_cache_sync_readahead(inode->i_mapping, ra,
1514 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301515 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001516 }
Chris Mason940100a2010-03-10 10:52:59 -05001517
Al Viro59551022016-01-22 15:40:57 -05001518 inode_lock(inode);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001519 if (IS_SWAPFILE(inode)) {
1520 ret = -ETXTBSY;
1521 } else {
1522 if (do_compress)
1523 BTRFS_I(inode)->defrag_compress = compress_type;
1524 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1525 }
Liu Boecb8bea2012-03-29 09:57:44 -04001526 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001527 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001528 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001529 }
Chris Mason940100a2010-03-10 10:52:59 -05001530
Chris Mason4cb53002011-05-24 15:35:30 -04001531 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001532 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001533 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001534
1535 if (newer_than) {
1536 if (newer_off == (u64)-1)
1537 break;
1538
Liu Boe1f041e2012-03-29 09:57:45 -04001539 if (ret > 0)
1540 i += ret;
1541
Chris Mason4cb53002011-05-24 15:35:30 -04001542 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001543 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001544
Byongho Leeee221842015-12-15 01:42:10 +09001545 ret = find_new_extents(root, inode, newer_than,
1546 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001547 if (!ret) {
1548 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001549 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001550 } else {
1551 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001552 }
Chris Mason4cb53002011-05-24 15:35:30 -04001553 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001554 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001555 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001556 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001557 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001558 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001559 last_len = 0;
1560 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001561 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001562 }
1563
Filipe Mananadec8ef92014-03-01 10:55:54 +00001564 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001565 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001566 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1567 &BTRFS_I(inode)->runtime_flags))
1568 filemap_flush(inode->i_mapping);
1569 }
Chris Mason1e701a32010-03-11 09:42:04 -05001570
Li Zefan1a419d82010-10-25 15:12:50 +08001571 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001572 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001573 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1574 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001575 }
1576
Diego Calleja60ccf822011-09-01 16:33:57 +02001577 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001578
Chris Mason4cb53002011-05-24 15:35:30 -04001579out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001580 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001581 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001582 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001583 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001584 }
Chris Mason4cb53002011-05-24 15:35:30 -04001585 if (!file)
1586 kfree(ra);
1587 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001588 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001589}
1590
Miao Xie198605a2012-11-26 08:43:45 +00001591static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001592 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001593{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001594 struct inode *inode = file_inode(file);
1595 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001596 u64 new_size;
1597 u64 old_size;
1598 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001599 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001600 struct btrfs_ioctl_vol_args *vol_args;
1601 struct btrfs_trans_handle *trans;
1602 struct btrfs_device *device = NULL;
1603 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001604 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001605 char *devstr = NULL;
1606 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001607 int mod = 0;
1608
Chris Masone441d542009-01-05 16:57:23 -05001609 if (!capable(CAP_SYS_ADMIN))
1610 return -EPERM;
1611
Miao Xie198605a2012-11-26 08:43:45 +00001612 ret = mnt_want_write_file(file);
1613 if (ret)
1614 return ret;
1615
David Sterba171938e2017-03-28 14:44:21 +02001616 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001617 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001618 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001619 }
1620
Li Zefandae7b662009-04-08 15:06:54 +08001621 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001622 if (IS_ERR(vol_args)) {
1623 ret = PTR_ERR(vol_args);
1624 goto out;
1625 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001626
1627 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001628
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001629 sizestr = vol_args->name;
1630 devstr = strchr(sizestr, ':');
1631 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001632 sizestr = devstr + 1;
1633 *devstr = '\0';
1634 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001635 ret = kstrtoull(devstr, 10, &devid);
1636 if (ret)
1637 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001638 if (!devid) {
1639 ret = -EINVAL;
1640 goto out_free;
1641 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001642 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001643 }
Miao Xiedba60f32012-12-21 09:19:51 +00001644
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001645 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001646 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001647 btrfs_info(fs_info, "resizer unable to find device %llu",
1648 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001649 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001650 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001651 }
Miao Xiedba60f32012-12-21 09:19:51 +00001652
Anand Jainebbede42017-12-04 12:54:52 +08001653 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001654 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001655 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001656 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001657 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001658 goto out_free;
1659 }
1660
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001661 if (!strcmp(sizestr, "max"))
1662 new_size = device->bdev->bd_inode->i_size;
1663 else {
1664 if (sizestr[0] == '-') {
1665 mod = -1;
1666 sizestr++;
1667 } else if (sizestr[0] == '+') {
1668 mod = 1;
1669 sizestr++;
1670 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001671 new_size = memparse(sizestr, &retptr);
1672 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001673 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001674 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001675 }
1676 }
1677
Anand Jain401e29c2017-12-04 12:54:55 +08001678 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001679 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001680 goto out_free;
1681 }
1682
Miao Xie7cc8e582014-09-03 21:35:38 +08001683 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001684
1685 if (mod < 0) {
1686 if (new_size > old_size) {
1687 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001688 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001689 }
1690 new_size = old_size - new_size;
1691 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001692 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001693 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001694 goto out_free;
1695 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001696 new_size = old_size + new_size;
1697 }
1698
Byongho Leeee221842015-12-15 01:42:10 +09001699 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001700 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001701 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001702 }
1703 if (new_size > device->bdev->bd_inode->i_size) {
1704 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001705 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 }
1707
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001708 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001709
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001710 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1711 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001712
1713 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001714 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001715 if (IS_ERR(trans)) {
1716 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001717 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001718 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001719 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001720 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001721 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001722 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001723 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001724
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001725out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001726 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001727out:
David Sterba171938e2017-03-28 14:44:21 +02001728 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001729 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001730 return ret;
1731}
1732
Sage Weil72fd0322010-10-29 15:41:32 -04001733static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001734 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001735 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001736 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001737{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001738 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001739 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001740
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001741 if (!S_ISDIR(file_inode(file)->i_mode))
1742 return -ENOTDIR;
1743
Liu Boa874a632012-06-29 03:58:46 -06001744 ret = mnt_want_write_file(file);
1745 if (ret)
1746 goto out;
1747
Sage Weil72fd0322010-10-29 15:41:32 -04001748 namelen = strlen(name);
1749 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001751 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001752 }
1753
Chris Mason16780ca2012-02-20 22:14:55 -05001754 if (name[0] == '.' &&
1755 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1756 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001757 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001758 }
1759
Chris Mason3de45862008-11-17 21:02:50 -05001760 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001761 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001762 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001763 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001764 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001765 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001766 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001767 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001768 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001769 }
1770
Al Viro496ad9a2013-01-23 17:07:38 -05001771 src_inode = file_inode(src.file);
1772 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001773 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001774 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001775 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001776 } else if (!inode_owner_or_capable(src_inode)) {
1777 /*
1778 * Subvolume creation is not restricted, but snapshots
1779 * are limited to own subvolumes only
1780 */
1781 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001782 } else {
1783 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1784 BTRFS_I(src_inode)->root,
1785 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001786 }
Al Viro2903ff02012-08-28 12:52:22 -04001787 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001788 }
Liu Boa874a632012-06-29 03:58:46 -06001789out_drop_write:
1790 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001791out:
Sage Weil72fd0322010-10-29 15:41:32 -04001792 return ret;
1793}
1794
1795static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001796 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001797{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001798 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001799 int ret;
1800
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001801 if (!S_ISDIR(file_inode(file)->i_mode))
1802 return -ENOTDIR;
1803
Li Zefanfa0d2b92010-12-20 15:53:28 +08001804 vol_args = memdup_user(arg, sizeof(*vol_args));
1805 if (IS_ERR(vol_args))
1806 return PTR_ERR(vol_args);
1807 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001808
Li Zefanfa0d2b92010-12-20 15:53:28 +08001809 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001810 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001811 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001812
Li Zefanfa0d2b92010-12-20 15:53:28 +08001813 kfree(vol_args);
1814 return ret;
1815}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001816
Li Zefanfa0d2b92010-12-20 15:53:28 +08001817static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1818 void __user *arg, int subvol)
1819{
1820 struct btrfs_ioctl_vol_args_v2 *vol_args;
1821 int ret;
1822 u64 transid = 0;
1823 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001824 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001825 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001826
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001827 if (!S_ISDIR(file_inode(file)->i_mode))
1828 return -ENOTDIR;
1829
Li Zefanfa0d2b92010-12-20 15:53:28 +08001830 vol_args = memdup_user(arg, sizeof(*vol_args));
1831 if (IS_ERR(vol_args))
1832 return PTR_ERR(vol_args);
1833 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001834
Li Zefanb83cc962010-12-20 16:04:08 +08001835 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001836 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1837 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001838 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001839 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001840 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001841
1842 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1843 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001844 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1845 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001846 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001847 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001848 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001849 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001850 }
1851 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1852 if (IS_ERR(inherit)) {
1853 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001854 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001855 }
1856 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001857
1858 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001859 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001860 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001861 if (ret)
1862 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001863
Dan Carpenterc47ca322014-09-04 14:09:15 +03001864 if (ptr && copy_to_user(arg +
1865 offsetof(struct btrfs_ioctl_vol_args_v2,
1866 transid),
1867 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001868 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001869
1870free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001871 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001872free_args:
1873 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001874 return ret;
1875}
1876
Li Zefan0caa1022010-12-20 16:30:25 +08001877static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1878 void __user *arg)
1879{
Al Viro496ad9a2013-01-23 17:07:38 -05001880 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001882 struct btrfs_root *root = BTRFS_I(inode)->root;
1883 int ret = 0;
1884 u64 flags = 0;
1885
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001886 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001887 return -EINVAL;
1888
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001889 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001890 if (btrfs_root_readonly(root))
1891 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001892 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001893
1894 if (copy_to_user(arg, &flags, sizeof(flags)))
1895 ret = -EFAULT;
1896
1897 return ret;
1898}
1899
1900static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1901 void __user *arg)
1902{
Al Viro496ad9a2013-01-23 17:07:38 -05001903 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001904 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001905 struct btrfs_root *root = BTRFS_I(inode)->root;
1906 struct btrfs_trans_handle *trans;
1907 u64 root_flags;
1908 u64 flags;
1909 int ret = 0;
1910
David Sterbabd60ea02014-01-16 15:50:22 +01001911 if (!inode_owner_or_capable(inode))
1912 return -EPERM;
1913
Liu Bob9ca0662012-06-29 03:58:49 -06001914 ret = mnt_want_write_file(file);
1915 if (ret)
1916 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001917
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001918 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001919 ret = -EINVAL;
1920 goto out_drop_write;
1921 }
Li Zefan0caa1022010-12-20 16:30:25 +08001922
Liu Bob9ca0662012-06-29 03:58:49 -06001923 if (copy_from_user(&flags, arg, sizeof(flags))) {
1924 ret = -EFAULT;
1925 goto out_drop_write;
1926 }
Li Zefan0caa1022010-12-20 16:30:25 +08001927
Liu Bob9ca0662012-06-29 03:58:49 -06001928 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1929 ret = -EINVAL;
1930 goto out_drop_write;
1931 }
Li Zefan0caa1022010-12-20 16:30:25 +08001932
Liu Bob9ca0662012-06-29 03:58:49 -06001933 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1934 ret = -EOPNOTSUPP;
1935 goto out_drop_write;
1936 }
Li Zefan0caa1022010-12-20 16:30:25 +08001937
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001938 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001939
1940 /* nothing to do */
1941 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001942 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001943
1944 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001945 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001946 btrfs_set_root_flags(&root->root_item,
1947 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001948 } else {
1949 /*
1950 * Block RO -> RW transition if this subvolume is involved in
1951 * send
1952 */
1953 spin_lock(&root->root_item_lock);
1954 if (root->send_in_progress == 0) {
1955 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001956 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001957 spin_unlock(&root->root_item_lock);
1958 } else {
1959 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001960 btrfs_warn(fs_info,
1961 "Attempt to set subvolume %llu read-write during send",
1962 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001963 ret = -EPERM;
1964 goto out_drop_sem;
1965 }
1966 }
Li Zefan0caa1022010-12-20 16:30:25 +08001967
1968 trans = btrfs_start_transaction(root, 1);
1969 if (IS_ERR(trans)) {
1970 ret = PTR_ERR(trans);
1971 goto out_reset;
1972 }
1973
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001974 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001975 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001976 if (ret < 0) {
1977 btrfs_end_transaction(trans);
1978 goto out_reset;
1979 }
Li Zefan0caa1022010-12-20 16:30:25 +08001980
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001981 ret = btrfs_commit_transaction(trans);
1982
Li Zefan0caa1022010-12-20 16:30:25 +08001983out_reset:
1984 if (ret)
1985 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001986out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001987 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001988out_drop_write:
1989 mnt_drop_write_file(file);
1990out:
Li Zefan0caa1022010-12-20 16:30:25 +08001991 return ret;
1992}
1993
Chris Masonac8e9812010-02-28 15:39:26 -05001994static noinline int key_in_sk(struct btrfs_key *key,
1995 struct btrfs_ioctl_search_key *sk)
1996{
Chris Masonabc6e132010-03-18 12:10:08 -04001997 struct btrfs_key test;
1998 int ret;
1999
2000 test.objectid = sk->min_objectid;
2001 test.type = sk->min_type;
2002 test.offset = sk->min_offset;
2003
2004 ret = btrfs_comp_cpu_keys(key, &test);
2005 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002006 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002007
2008 test.objectid = sk->max_objectid;
2009 test.type = sk->max_type;
2010 test.offset = sk->max_offset;
2011
2012 ret = btrfs_comp_cpu_keys(key, &test);
2013 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002014 return 0;
2015 return 1;
2016}
2017
Jeff Mahoneydf397562016-06-21 20:18:21 -04002018static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002019 struct btrfs_key *key,
2020 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002021 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002022 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002023 unsigned long *sk_offset,
2024 int *num_found)
2025{
2026 u64 found_transid;
2027 struct extent_buffer *leaf;
2028 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002029 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002030 unsigned long item_off;
2031 unsigned long item_len;
2032 int nritems;
2033 int i;
2034 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002035 int ret = 0;
2036
2037 leaf = path->nodes[0];
2038 slot = path->slots[0];
2039 nritems = btrfs_header_nritems(leaf);
2040
2041 if (btrfs_header_generation(leaf) > sk->max_transid) {
2042 i = nritems;
2043 goto advance_key;
2044 }
2045 found_transid = btrfs_header_generation(leaf);
2046
2047 for (i = slot; i < nritems; i++) {
2048 item_off = btrfs_item_ptr_offset(leaf, i);
2049 item_len = btrfs_item_size_nr(leaf, i);
2050
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002051 btrfs_item_key_to_cpu(leaf, key, i);
2052 if (!key_in_sk(key, sk))
2053 continue;
2054
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002055 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002056 if (*num_found) {
2057 ret = 1;
2058 goto out;
2059 }
Chris Masonac8e9812010-02-28 15:39:26 -05002060
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002061 /*
2062 * return one empty item back for v1, which does not
2063 * handle -EOVERFLOW
2064 */
2065
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002066 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002067 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002068 ret = -EOVERFLOW;
2069 }
Chris Masonac8e9812010-02-28 15:39:26 -05002070
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002071 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002072 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002073 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002074 }
2075
Chris Masonac8e9812010-02-28 15:39:26 -05002076 sh.objectid = key->objectid;
2077 sh.offset = key->offset;
2078 sh.type = key->type;
2079 sh.len = item_len;
2080 sh.transid = found_transid;
2081
2082 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002083 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2084 ret = -EFAULT;
2085 goto out;
2086 }
2087
Chris Masonac8e9812010-02-28 15:39:26 -05002088 *sk_offset += sizeof(sh);
2089
2090 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002091 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002092 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002093 if (read_extent_buffer_to_user(leaf, up,
2094 item_off, item_len)) {
2095 ret = -EFAULT;
2096 goto out;
2097 }
2098
Chris Masonac8e9812010-02-28 15:39:26 -05002099 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002100 }
Hugo Millse2156862011-05-14 17:43:41 +00002101 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002102
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002103 if (ret) /* -EOVERFLOW from above */
2104 goto out;
2105
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002106 if (*num_found >= sk->nr_items) {
2107 ret = 1;
2108 goto out;
2109 }
Chris Masonac8e9812010-02-28 15:39:26 -05002110 }
2111advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002112 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002113 test.objectid = sk->max_objectid;
2114 test.type = sk->max_type;
2115 test.offset = sk->max_offset;
2116 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2117 ret = 1;
2118 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002119 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002120 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002121 key->offset = 0;
2122 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002123 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002124 key->offset = 0;
2125 key->type = 0;
2126 key->objectid++;
2127 } else
2128 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002129out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002130 /*
2131 * 0: all items from this leaf copied, continue with next
2132 * 1: * more items can be copied, but unused buffer is too small
2133 * * all items were found
2134 * Either way, it will stops the loop which iterates to the next
2135 * leaf
2136 * -EOVERFLOW: item was to large for buffer
2137 * -EFAULT: could not copy extent buffer back to userspace
2138 */
Chris Masonac8e9812010-02-28 15:39:26 -05002139 return ret;
2140}
2141
2142static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002143 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002144 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002145 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002146{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002147 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002148 struct btrfs_root *root;
2149 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002150 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002151 int ret;
2152 int num_found = 0;
2153 unsigned long sk_offset = 0;
2154
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002155 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2156 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002157 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002158 }
Gerhard Heift12544442014-01-30 16:23:58 +01002159
Chris Masonac8e9812010-02-28 15:39:26 -05002160 path = btrfs_alloc_path();
2161 if (!path)
2162 return -ENOMEM;
2163
2164 if (sk->tree_id == 0) {
2165 /* search the root of the inode that was passed */
2166 root = BTRFS_I(inode)->root;
2167 } else {
2168 key.objectid = sk->tree_id;
2169 key.type = BTRFS_ROOT_ITEM_KEY;
2170 key.offset = (u64)-1;
2171 root = btrfs_read_fs_root_no_name(info, &key);
2172 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002173 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002174 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002175 }
2176 }
2177
2178 key.objectid = sk->min_objectid;
2179 key.type = sk->min_type;
2180 key.offset = sk->min_offset;
2181
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302182 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002183 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002184 if (ret != 0) {
2185 if (ret > 0)
2186 ret = 0;
2187 goto err;
2188 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002189 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002190 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002191 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002192 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002193 break;
2194
2195 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002196 if (ret > 0)
2197 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002198err:
2199 sk->nr_items = num_found;
2200 btrfs_free_path(path);
2201 return ret;
2202}
2203
2204static noinline int btrfs_ioctl_tree_search(struct file *file,
2205 void __user *argp)
2206{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002207 struct btrfs_ioctl_search_args __user *uargs;
2208 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002209 struct inode *inode;
2210 int ret;
2211 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002212
2213 if (!capable(CAP_SYS_ADMIN))
2214 return -EPERM;
2215
Gerhard Heiftba346b32014-01-30 16:24:02 +01002216 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002217
Gerhard Heiftba346b32014-01-30 16:24:02 +01002218 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2219 return -EFAULT;
2220
2221 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002222
Al Viro496ad9a2013-01-23 17:07:38 -05002223 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002224 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002225
2226 /*
2227 * In the origin implementation an overflow is handled by returning a
2228 * search header with a len of zero, so reset ret.
2229 */
2230 if (ret == -EOVERFLOW)
2231 ret = 0;
2232
Gerhard Heiftba346b32014-01-30 16:24:02 +01002233 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002234 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002235 return ret;
2236}
2237
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002238static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2239 void __user *argp)
2240{
2241 struct btrfs_ioctl_search_args_v2 __user *uarg;
2242 struct btrfs_ioctl_search_args_v2 args;
2243 struct inode *inode;
2244 int ret;
2245 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002246 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002247
2248 if (!capable(CAP_SYS_ADMIN))
2249 return -EPERM;
2250
2251 /* copy search header and buffer size */
2252 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2253 if (copy_from_user(&args, uarg, sizeof(args)))
2254 return -EFAULT;
2255
2256 buf_size = args.buf_size;
2257
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002258 /* limit result size to 16MB */
2259 if (buf_size > buf_limit)
2260 buf_size = buf_limit;
2261
2262 inode = file_inode(file);
2263 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002264 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002265 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2266 ret = -EFAULT;
2267 else if (ret == -EOVERFLOW &&
2268 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2269 ret = -EFAULT;
2270
Yan, Zheng76dda932009-09-21 16:00:26 -04002271 return ret;
2272}
2273
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002274/*
Chris Masonac8e9812010-02-28 15:39:26 -05002275 * Search INODE_REFs to identify path name of 'dirid' directory
2276 * in a 'tree_id' tree. and sets path name to 'name'.
2277 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002278static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2279 u64 tree_id, u64 dirid, char *name)
2280{
2281 struct btrfs_root *root;
2282 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002283 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002284 int ret = -1;
2285 int slot;
2286 int len;
2287 int total_len = 0;
2288 struct btrfs_inode_ref *iref;
2289 struct extent_buffer *l;
2290 struct btrfs_path *path;
2291
2292 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2293 name[0]='\0';
2294 return 0;
2295 }
2296
2297 path = btrfs_alloc_path();
2298 if (!path)
2299 return -ENOMEM;
2300
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002301 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002302
2303 key.objectid = tree_id;
2304 key.type = BTRFS_ROOT_ITEM_KEY;
2305 key.offset = (u64)-1;
2306 root = btrfs_read_fs_root_no_name(info, &key);
2307 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002308 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002309 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002310 }
2311
2312 key.objectid = dirid;
2313 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002314 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002315
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302316 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2318 if (ret < 0)
2319 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002320 else if (ret > 0) {
2321 ret = btrfs_previous_item(root, path, dirid,
2322 BTRFS_INODE_REF_KEY);
2323 if (ret < 0)
2324 goto out;
2325 else if (ret > 0) {
2326 ret = -ENOENT;
2327 goto out;
2328 }
2329 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002330
2331 l = path->nodes[0];
2332 slot = path->slots[0];
2333 btrfs_item_key_to_cpu(l, &key, slot);
2334
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002335 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2336 len = btrfs_inode_ref_name_len(l, iref);
2337 ptr -= len + 1;
2338 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002339 if (ptr < name) {
2340 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002341 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002342 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002343
2344 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302345 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002346
2347 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2348 break;
2349
David Sterbab3b4aa72011-04-21 01:20:15 +02002350 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002351 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002352 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002353 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002354 }
Li Zefan77906a502011-07-14 03:16:00 +00002355 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302356 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002357 ret = 0;
2358out:
2359 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002360 return ret;
2361}
2362
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002363static int btrfs_search_path_in_tree_user(struct inode *inode,
2364 struct btrfs_ioctl_ino_lookup_user_args *args)
2365{
2366 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2367 struct super_block *sb = inode->i_sb;
2368 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2369 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2370 u64 dirid = args->dirid;
2371 unsigned long item_off;
2372 unsigned long item_len;
2373 struct btrfs_inode_ref *iref;
2374 struct btrfs_root_ref *rref;
2375 struct btrfs_root *root;
2376 struct btrfs_path *path;
2377 struct btrfs_key key, key2;
2378 struct extent_buffer *leaf;
2379 struct inode *temp_inode;
2380 char *ptr;
2381 int slot;
2382 int len;
2383 int total_len = 0;
2384 int ret;
2385
2386 path = btrfs_alloc_path();
2387 if (!path)
2388 return -ENOMEM;
2389
2390 /*
2391 * If the bottom subvolume does not exist directly under upper_limit,
2392 * construct the path in from the bottom up.
2393 */
2394 if (dirid != upper_limit.objectid) {
2395 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2396
2397 key.objectid = treeid;
2398 key.type = BTRFS_ROOT_ITEM_KEY;
2399 key.offset = (u64)-1;
2400 root = btrfs_read_fs_root_no_name(fs_info, &key);
2401 if (IS_ERR(root)) {
2402 ret = PTR_ERR(root);
2403 goto out;
2404 }
2405
2406 key.objectid = dirid;
2407 key.type = BTRFS_INODE_REF_KEY;
2408 key.offset = (u64)-1;
2409 while (1) {
2410 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2411 if (ret < 0) {
2412 goto out;
2413 } else if (ret > 0) {
2414 ret = btrfs_previous_item(root, path, dirid,
2415 BTRFS_INODE_REF_KEY);
2416 if (ret < 0) {
2417 goto out;
2418 } else if (ret > 0) {
2419 ret = -ENOENT;
2420 goto out;
2421 }
2422 }
2423
2424 leaf = path->nodes[0];
2425 slot = path->slots[0];
2426 btrfs_item_key_to_cpu(leaf, &key, slot);
2427
2428 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2429 len = btrfs_inode_ref_name_len(leaf, iref);
2430 ptr -= len + 1;
2431 total_len += len + 1;
2432 if (ptr < args->path) {
2433 ret = -ENAMETOOLONG;
2434 goto out;
2435 }
2436
2437 *(ptr + len) = '/';
2438 read_extent_buffer(leaf, ptr,
2439 (unsigned long)(iref + 1), len);
2440
2441 /* Check the read+exec permission of this directory */
2442 ret = btrfs_previous_item(root, path, dirid,
2443 BTRFS_INODE_ITEM_KEY);
2444 if (ret < 0) {
2445 goto out;
2446 } else if (ret > 0) {
2447 ret = -ENOENT;
2448 goto out;
2449 }
2450
2451 leaf = path->nodes[0];
2452 slot = path->slots[0];
2453 btrfs_item_key_to_cpu(leaf, &key2, slot);
2454 if (key2.objectid != dirid) {
2455 ret = -ENOENT;
2456 goto out;
2457 }
2458
2459 temp_inode = btrfs_iget(sb, &key2, root, NULL);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002460 if (IS_ERR(temp_inode)) {
2461 ret = PTR_ERR(temp_inode);
2462 goto out;
2463 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002464 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2465 iput(temp_inode);
2466 if (ret) {
2467 ret = -EACCES;
2468 goto out;
2469 }
2470
2471 if (key.offset == upper_limit.objectid)
2472 break;
2473 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2474 ret = -EACCES;
2475 goto out;
2476 }
2477
2478 btrfs_release_path(path);
2479 key.objectid = key.offset;
2480 key.offset = (u64)-1;
2481 dirid = key.objectid;
2482 }
2483
2484 memmove(args->path, ptr, total_len);
2485 args->path[total_len] = '\0';
2486 btrfs_release_path(path);
2487 }
2488
2489 /* Get the bottom subvolume's name from ROOT_REF */
2490 root = fs_info->tree_root;
2491 key.objectid = treeid;
2492 key.type = BTRFS_ROOT_REF_KEY;
2493 key.offset = args->treeid;
2494 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2495 if (ret < 0) {
2496 goto out;
2497 } else if (ret > 0) {
2498 ret = -ENOENT;
2499 goto out;
2500 }
2501
2502 leaf = path->nodes[0];
2503 slot = path->slots[0];
2504 btrfs_item_key_to_cpu(leaf, &key, slot);
2505
2506 item_off = btrfs_item_ptr_offset(leaf, slot);
2507 item_len = btrfs_item_size_nr(leaf, slot);
2508 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2509 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2510 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2511 ret = -EINVAL;
2512 goto out;
2513 }
2514
2515 /* Copy subvolume's name */
2516 item_off += sizeof(struct btrfs_root_ref);
2517 item_len -= sizeof(struct btrfs_root_ref);
2518 read_extent_buffer(leaf, args->name, item_off, item_len);
2519 args->name[item_len] = 0;
2520
2521out:
2522 btrfs_free_path(path);
2523 return ret;
2524}
2525
Chris Masonac8e9812010-02-28 15:39:26 -05002526static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2527 void __user *argp)
2528{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002529 struct btrfs_ioctl_ino_lookup_args *args;
2530 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002531 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002532
Julia Lawall2354d08f2010-10-29 15:14:18 -04002533 args = memdup_user(argp, sizeof(*args));
2534 if (IS_ERR(args))
2535 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002536
Al Viro496ad9a2013-01-23 17:07:38 -05002537 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002538
David Sterba01b810b2015-05-12 19:14:49 +02002539 /*
2540 * Unprivileged query to obtain the containing subvolume root id. The
2541 * path is reset so it's consistent with btrfs_search_path_in_tree.
2542 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002543 if (args->treeid == 0)
2544 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2545
David Sterba01b810b2015-05-12 19:14:49 +02002546 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2547 args->name[0] = 0;
2548 goto out;
2549 }
2550
2551 if (!capable(CAP_SYS_ADMIN)) {
2552 ret = -EPERM;
2553 goto out;
2554 }
2555
Chris Masonac8e9812010-02-28 15:39:26 -05002556 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2557 args->treeid, args->objectid,
2558 args->name);
2559
David Sterba01b810b2015-05-12 19:14:49 +02002560out:
Chris Masonac8e9812010-02-28 15:39:26 -05002561 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2562 ret = -EFAULT;
2563
2564 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002565 return ret;
2566}
2567
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002568/*
2569 * Version of ino_lookup ioctl (unprivileged)
2570 *
2571 * The main differences from ino_lookup ioctl are:
2572 *
2573 * 1. Read + Exec permission will be checked using inode_permission() during
2574 * path construction. -EACCES will be returned in case of failure.
2575 * 2. Path construction will be stopped at the inode number which corresponds
2576 * to the fd with which this ioctl is called. If constructed path does not
2577 * exist under fd's inode, -EACCES will be returned.
2578 * 3. The name of bottom subvolume is also searched and filled.
2579 */
2580static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2581{
2582 struct btrfs_ioctl_ino_lookup_user_args *args;
2583 struct inode *inode;
2584 int ret;
2585
2586 args = memdup_user(argp, sizeof(*args));
2587 if (IS_ERR(args))
2588 return PTR_ERR(args);
2589
2590 inode = file_inode(file);
2591
2592 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2593 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2594 /*
2595 * The subvolume does not exist under fd with which this is
2596 * called
2597 */
2598 kfree(args);
2599 return -EACCES;
2600 }
2601
2602 ret = btrfs_search_path_in_tree_user(inode, args);
2603
2604 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2605 ret = -EFAULT;
2606
2607 kfree(args);
2608 return ret;
2609}
2610
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002611/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2612static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2613{
2614 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2615 struct btrfs_fs_info *fs_info;
2616 struct btrfs_root *root;
2617 struct btrfs_path *path;
2618 struct btrfs_key key;
2619 struct btrfs_root_item *root_item;
2620 struct btrfs_root_ref *rref;
2621 struct extent_buffer *leaf;
2622 unsigned long item_off;
2623 unsigned long item_len;
2624 struct inode *inode;
2625 int slot;
2626 int ret = 0;
2627
2628 path = btrfs_alloc_path();
2629 if (!path)
2630 return -ENOMEM;
2631
2632 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2633 if (!subvol_info) {
2634 btrfs_free_path(path);
2635 return -ENOMEM;
2636 }
2637
2638 inode = file_inode(file);
2639 fs_info = BTRFS_I(inode)->root->fs_info;
2640
2641 /* Get root_item of inode's subvolume */
2642 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2643 key.type = BTRFS_ROOT_ITEM_KEY;
2644 key.offset = (u64)-1;
2645 root = btrfs_read_fs_root_no_name(fs_info, &key);
2646 if (IS_ERR(root)) {
2647 ret = PTR_ERR(root);
2648 goto out;
2649 }
2650 root_item = &root->root_item;
2651
2652 subvol_info->treeid = key.objectid;
2653
2654 subvol_info->generation = btrfs_root_generation(root_item);
2655 subvol_info->flags = btrfs_root_flags(root_item);
2656
2657 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2658 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2659 BTRFS_UUID_SIZE);
2660 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2661 BTRFS_UUID_SIZE);
2662
2663 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2664 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2665 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2666
2667 subvol_info->otransid = btrfs_root_otransid(root_item);
2668 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2669 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2670
2671 subvol_info->stransid = btrfs_root_stransid(root_item);
2672 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2673 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2674
2675 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2676 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2677 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2678
2679 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2680 /* Search root tree for ROOT_BACKREF of this subvolume */
2681 root = fs_info->tree_root;
2682
2683 key.type = BTRFS_ROOT_BACKREF_KEY;
2684 key.offset = 0;
2685 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2686 if (ret < 0) {
2687 goto out;
2688 } else if (path->slots[0] >=
2689 btrfs_header_nritems(path->nodes[0])) {
2690 ret = btrfs_next_leaf(root, path);
2691 if (ret < 0) {
2692 goto out;
2693 } else if (ret > 0) {
2694 ret = -EUCLEAN;
2695 goto out;
2696 }
2697 }
2698
2699 leaf = path->nodes[0];
2700 slot = path->slots[0];
2701 btrfs_item_key_to_cpu(leaf, &key, slot);
2702 if (key.objectid == subvol_info->treeid &&
2703 key.type == BTRFS_ROOT_BACKREF_KEY) {
2704 subvol_info->parent_id = key.offset;
2705
2706 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2707 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2708
2709 item_off = btrfs_item_ptr_offset(leaf, slot)
2710 + sizeof(struct btrfs_root_ref);
2711 item_len = btrfs_item_size_nr(leaf, slot)
2712 - sizeof(struct btrfs_root_ref);
2713 read_extent_buffer(leaf, subvol_info->name,
2714 item_off, item_len);
2715 } else {
2716 ret = -ENOENT;
2717 goto out;
2718 }
2719 }
2720
2721 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2722 ret = -EFAULT;
2723
2724out:
2725 btrfs_free_path(path);
2726 kzfree(subvol_info);
2727 return ret;
2728}
2729
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002730/*
2731 * Return ROOT_REF information of the subvolume containing this inode
2732 * except the subvolume name.
2733 */
2734static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2735{
2736 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2737 struct btrfs_root_ref *rref;
2738 struct btrfs_root *root;
2739 struct btrfs_path *path;
2740 struct btrfs_key key;
2741 struct extent_buffer *leaf;
2742 struct inode *inode;
2743 u64 objectid;
2744 int slot;
2745 int ret;
2746 u8 found;
2747
2748 path = btrfs_alloc_path();
2749 if (!path)
2750 return -ENOMEM;
2751
2752 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2753 if (IS_ERR(rootrefs)) {
2754 btrfs_free_path(path);
2755 return PTR_ERR(rootrefs);
2756 }
2757
2758 inode = file_inode(file);
2759 root = BTRFS_I(inode)->root->fs_info->tree_root;
2760 objectid = BTRFS_I(inode)->root->root_key.objectid;
2761
2762 key.objectid = objectid;
2763 key.type = BTRFS_ROOT_REF_KEY;
2764 key.offset = rootrefs->min_treeid;
2765 found = 0;
2766
2767 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2768 if (ret < 0) {
2769 goto out;
2770 } else if (path->slots[0] >=
2771 btrfs_header_nritems(path->nodes[0])) {
2772 ret = btrfs_next_leaf(root, path);
2773 if (ret < 0) {
2774 goto out;
2775 } else if (ret > 0) {
2776 ret = -EUCLEAN;
2777 goto out;
2778 }
2779 }
2780 while (1) {
2781 leaf = path->nodes[0];
2782 slot = path->slots[0];
2783
2784 btrfs_item_key_to_cpu(leaf, &key, slot);
2785 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2786 ret = 0;
2787 goto out;
2788 }
2789
2790 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2791 ret = -EOVERFLOW;
2792 goto out;
2793 }
2794
2795 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2796 rootrefs->rootref[found].treeid = key.offset;
2797 rootrefs->rootref[found].dirid =
2798 btrfs_root_ref_dirid(leaf, rref);
2799 found++;
2800
2801 ret = btrfs_next_item(root, path);
2802 if (ret < 0) {
2803 goto out;
2804 } else if (ret > 0) {
2805 ret = -EUCLEAN;
2806 goto out;
2807 }
2808 }
2809
2810out:
2811 if (!ret || ret == -EOVERFLOW) {
2812 rootrefs->num_items = found;
2813 /* update min_treeid for next search */
2814 if (found)
2815 rootrefs->min_treeid =
2816 rootrefs->rootref[found - 1].treeid + 1;
2817 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2818 ret = -EFAULT;
2819 }
2820
2821 kfree(rootrefs);
2822 btrfs_free_path(path);
2823
2824 return ret;
2825}
2826
Yan, Zheng76dda932009-09-21 16:00:26 -04002827static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2828 void __user *arg)
2829{
Al Viro54563d42013-09-01 15:57:51 -04002830 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002831 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002832 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002833 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002834 struct inode *inode;
2835 struct btrfs_root *root = BTRFS_I(dir)->root;
2836 struct btrfs_root *dest = NULL;
2837 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002838 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002839 int err = 0;
2840
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002841 if (!S_ISDIR(dir->i_mode))
2842 return -ENOTDIR;
2843
Yan, Zheng76dda932009-09-21 16:00:26 -04002844 vol_args = memdup_user(arg, sizeof(*vol_args));
2845 if (IS_ERR(vol_args))
2846 return PTR_ERR(vol_args);
2847
2848 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2849 namelen = strlen(vol_args->name);
2850 if (strchr(vol_args->name, '/') ||
2851 strncmp(vol_args->name, "..", namelen) == 0) {
2852 err = -EINVAL;
2853 goto out;
2854 }
2855
Al Viroa561be72011-11-23 11:57:51 -05002856 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002857 if (err)
2858 goto out;
2859
David Sterba521e0542014-04-15 16:41:44 +02002860
Al Viro00235412016-05-26 00:05:12 -04002861 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2862 if (err == -EINTR)
2863 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002864 dentry = lookup_one_len(vol_args->name, parent, namelen);
2865 if (IS_ERR(dentry)) {
2866 err = PTR_ERR(dentry);
2867 goto out_unlock_dir;
2868 }
2869
David Howells2b0143b2015-03-17 22:25:59 +00002870 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002871 err = -ENOENT;
2872 goto out_dput;
2873 }
2874
David Howells2b0143b2015-03-17 22:25:59 +00002875 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002876 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302877 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002878 /*
2879 * Regular user. Only allow this with a special mount
2880 * option, when the user has write+exec access to the
2881 * subvol root, and when rmdir(2) would have been
2882 * allowed.
2883 *
2884 * Note that this is _not_ check that the subvol is
2885 * empty or doesn't contain data that we wouldn't
2886 * otherwise be able to delete.
2887 *
2888 * Users who want to delete empty subvols should try
2889 * rmdir(2).
2890 */
2891 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002892 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002893 goto out_dput;
2894
2895 /*
2896 * Do not allow deletion if the parent dir is the same
2897 * as the dir to be deleted. That means the ioctl
2898 * must be called on the dentry referencing the root
2899 * of the subvol, not a random directory contained
2900 * within it.
2901 */
2902 err = -EINVAL;
2903 if (root == dest)
2904 goto out_dput;
2905
2906 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2907 if (err)
2908 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002909 }
2910
Miao Xie5c39da52012-10-22 11:39:53 +00002911 /* check if subvolume may be deleted by a user */
2912 err = btrfs_may_delete(dir, dentry, 1);
2913 if (err)
2914 goto out_dput;
2915
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002916 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002917 err = -EINVAL;
2918 goto out_dput;
2919 }
2920
Al Viro59551022016-01-22 15:40:57 -05002921 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002922 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002923 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002924 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002925 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002926
Yan, Zheng76dda932009-09-21 16:00:26 -04002927out_dput:
2928 dput(dentry);
2929out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002930 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002931out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002932 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002933out:
2934 kfree(vol_args);
2935 return err;
2936}
2937
Chris Mason1e701a32010-03-11 09:42:04 -05002938static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002939{
Al Viro496ad9a2013-01-23 17:07:38 -05002940 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002941 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002942 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002943 int ret;
2944
Ilya Dryomov25122d12013-01-20 15:57:57 +02002945 ret = mnt_want_write_file(file);
2946 if (ret)
2947 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002948
Ilya Dryomov25122d12013-01-20 15:57:57 +02002949 if (btrfs_root_readonly(root)) {
2950 ret = -EROFS;
2951 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002952 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002953
2954 switch (inode->i_mode & S_IFMT) {
2955 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002956 if (!capable(CAP_SYS_ADMIN)) {
2957 ret = -EPERM;
2958 goto out;
2959 }
Eric Sandeende78b512013-01-31 18:21:12 +00002960 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002961 break;
2962 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02002963 /*
2964 * Note that this does not check the file descriptor for write
2965 * access. This prevents defragmenting executables that are
2966 * running and allows defrag on files open in read-only mode.
2967 */
2968 if (!capable(CAP_SYS_ADMIN) &&
2969 inode_permission(inode, MAY_WRITE)) {
2970 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05002971 goto out;
2972 }
Chris Mason1e701a32010-03-11 09:42:04 -05002973
2974 range = kzalloc(sizeof(*range), GFP_KERNEL);
2975 if (!range) {
2976 ret = -ENOMEM;
2977 goto out;
2978 }
2979
2980 if (argp) {
2981 if (copy_from_user(range, argp,
2982 sizeof(*range))) {
2983 ret = -EFAULT;
2984 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002985 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002986 }
2987 /* compression requires us to start the IO */
2988 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2989 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2990 range->extent_thresh = (u32)-1;
2991 }
2992 } else {
2993 /* the rest are all set to zero by kzalloc */
2994 range->len = (u64)-1;
2995 }
Al Viro496ad9a2013-01-23 17:07:38 -05002996 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002997 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002998 if (ret > 0)
2999 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003000 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003001 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003002 default:
3003 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003004 }
Chris Masone441d542009-01-05 16:57:23 -05003005out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003006 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003007 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003008}
3009
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003010static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003011{
3012 struct btrfs_ioctl_vol_args *vol_args;
3013 int ret;
3014
Chris Masone441d542009-01-05 16:57:23 -05003015 if (!capable(CAP_SYS_ADMIN))
3016 return -EPERM;
3017
David Sterba171938e2017-03-28 14:44:21 +02003018 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003019 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003020
Li Zefandae7b662009-04-08 15:06:54 +08003021 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003022 if (IS_ERR(vol_args)) {
3023 ret = PTR_ERR(vol_args);
3024 goto out;
3025 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003026
Mark Fasheh5516e592008-07-24 12:20:14 -04003027 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003028 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003029
Anand Jain43d20762014-07-01 00:58:56 +08003030 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003031 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003032
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003033 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003034out:
David Sterba171938e2017-03-28 14:44:21 +02003035 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003036 return ret;
3037}
3038
Anand Jain6b526ed2016-02-13 10:01:39 +08003039static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003040{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003041 struct inode *inode = file_inode(file);
3042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003043 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003044 int ret;
3045
Chris Masone441d542009-01-05 16:57:23 -05003046 if (!capable(CAP_SYS_ADMIN))
3047 return -EPERM;
3048
Miao Xieda249272012-11-26 08:44:50 +00003049 ret = mnt_want_write_file(file);
3050 if (ret)
3051 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003052
Li Zefandae7b662009-04-08 15:06:54 +08003053 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003054 if (IS_ERR(vol_args)) {
3055 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003056 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003057 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003058
Anand Jain6b526ed2016-02-13 10:01:39 +08003059 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003060 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3061 ret = -EOPNOTSUPP;
3062 goto out;
3063 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003064
David Sterba171938e2017-03-28 14:44:21 +02003065 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003066 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3067 goto out;
3068 }
3069
David Sterba735654e2016-02-15 18:15:21 +01003070 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003071 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003072 } else {
3073 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003074 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003075 }
David Sterba171938e2017-03-28 14:44:21 +02003076 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003077
Anand Jain6b526ed2016-02-13 10:01:39 +08003078 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003079 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003080 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003081 vol_args->devid);
3082 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003083 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003084 vol_args->name);
3085 }
Anand Jain183860f2013-05-17 10:52:45 +00003086out:
3087 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003088err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003089 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003090 return ret;
3091}
3092
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003093static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3094{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003095 struct inode *inode = file_inode(file);
3096 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003097 struct btrfs_ioctl_vol_args *vol_args;
3098 int ret;
3099
3100 if (!capable(CAP_SYS_ADMIN))
3101 return -EPERM;
3102
3103 ret = mnt_want_write_file(file);
3104 if (ret)
3105 return ret;
3106
David Sterba171938e2017-03-28 14:44:21 +02003107 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003108 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003109 goto out_drop_write;
3110 }
3111
3112 vol_args = memdup_user(arg, sizeof(*vol_args));
3113 if (IS_ERR(vol_args)) {
3114 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003115 goto out;
3116 }
3117
David Sterba58d7bbf2016-05-04 14:10:47 +02003118 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003119 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003120
3121 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003122 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003123 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003124out:
David Sterba171938e2017-03-28 14:44:21 +02003125 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003126out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003127 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003128
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003129 return ret;
3130}
3131
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003132static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3133 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003134{
Li Zefan027ed2f2011-06-08 08:27:56 +00003135 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003136 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003137 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003138 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003139
Li Zefan027ed2f2011-06-08 08:27:56 +00003140 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3141 if (!fi_args)
3142 return -ENOMEM;
3143
David Sterbad03262c2017-06-16 00:09:21 +02003144 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003145 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003146
David Sterbad03262c2017-06-16 00:09:21 +02003147 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003148 if (device->devid > fi_args->max_id)
3149 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003150 }
David Sterbad03262c2017-06-16 00:09:21 +02003151 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003152
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003153 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003154 fi_args->nodesize = fs_info->nodesize;
3155 fi_args->sectorsize = fs_info->sectorsize;
3156 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003157
Li Zefan027ed2f2011-06-08 08:27:56 +00003158 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3159 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003160
Li Zefan027ed2f2011-06-08 08:27:56 +00003161 kfree(fi_args);
3162 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003163}
3164
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003165static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3166 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003167{
3168 struct btrfs_ioctl_dev_info_args *di_args;
3169 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003170 int ret = 0;
3171 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003172
Jan Schmidt475f6382011-03-11 15:41:01 +01003173 di_args = memdup_user(arg, sizeof(*di_args));
3174 if (IS_ERR(di_args))
3175 return PTR_ERR(di_args);
3176
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003177 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003178 s_uuid = di_args->uuid;
3179
David Sterbac5593ca2017-06-16 00:09:21 +02003180 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003181 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01003182
3183 if (!dev) {
3184 ret = -ENODEV;
3185 goto out;
3186 }
3187
3188 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003189 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3190 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003191 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003192 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003193 strncpy(di_args->path, rcu_str_deref(dev->name),
3194 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003195 di_args->path[sizeof(di_args->path) - 1] = 0;
3196 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003197 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003198 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003199
3200out:
David Sterbac5593ca2017-06-16 00:09:21 +02003201 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003202 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3203 ret = -EFAULT;
3204
3205 kfree(di_args);
3206 return ret;
3207}
3208
Mark Fashehf4414602015-06-30 14:42:05 -07003209static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07003210{
Al Viro59551022016-01-22 15:40:57 -05003211 inode_unlock(inode1);
3212 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07003213}
3214
Mark Fashehf4414602015-06-30 14:42:05 -07003215static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
3216{
3217 if (inode1 < inode2)
3218 swap(inode1, inode2);
3219
Al Viro59551022016-01-22 15:40:57 -05003220 inode_lock_nested(inode1, I_MUTEX_PARENT);
3221 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07003222}
3223
Filipe Mananad8b55242019-01-08 11:43:07 +00003224static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3225 struct inode *inode2, u64 loff2, u64 len)
3226{
3227 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3228 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3229}
3230
3231static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3232 struct inode *inode2, u64 loff2, u64 len)
3233{
3234 if (inode1 < inode2) {
3235 swap(inode1, inode2);
3236 swap(loff1, loff2);
3237 } else if (inode1 == inode2 && loff2 < loff1) {
3238 swap(loff1, loff2);
3239 }
3240 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3241 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3242}
3243
Timofey Titovets39739092018-05-02 08:15:36 +03003244static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
Filipe Manana34a28e32018-12-07 15:25:38 +00003245 struct inode *dst, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003246{
Filipe Manana34a28e32018-12-07 15:25:38 +00003247 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Mark Fasheh416161d2013-08-06 11:42:51 -07003248 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003249 u64 len = olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003250
Filipe Manana34a28e32018-12-07 15:25:38 +00003251 if (loff + len == src->i_size)
3252 len = ALIGN(src->i_size, bs) - loff;
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003253 /*
Filipe Manana34a28e32018-12-07 15:25:38 +00003254 * For same inode case we don't want our length pushed out past i_size
3255 * as comparing that data range makes no sense.
3256 *
3257 * This effectively means we require aligned extents for the single
3258 * inode case, whereas the other cases allow an unaligned length so long
3259 * as it ends at i_size.
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003260 */
Filipe Manana34a28e32018-12-07 15:25:38 +00003261 if (dst == src && len != olen)
3262 return -EINVAL;
Mark Fashehf4414602015-06-30 14:42:05 -07003263
Filipe Manana34a28e32018-12-07 15:25:38 +00003264 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003265 * Lock destination range to serialize with concurrent readpages() and
3266 * source range to serialize with relocation.
Filipe Manana34a28e32018-12-07 15:25:38 +00003267 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003268 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Filipe Manana34a28e32018-12-07 15:25:38 +00003269 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Filipe Mananad8b55242019-01-08 11:43:07 +00003270 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Timofey Titovets39739092018-05-02 08:15:36 +03003271
3272 return ret;
3273}
3274
Timofey Titovetsb6728762018-05-02 08:15:37 +03003275#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3276
Timofey Titovets39739092018-05-02 08:15:36 +03003277static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3278 struct inode *dst, u64 dst_loff)
3279{
3280 int ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003281 u64 i, tail_len, chunk_count;
Timofey Titovets39739092018-05-02 08:15:36 +03003282
Timofey Titovets39739092018-05-02 08:15:36 +03003283 /* don't make the dst file partly checksummed */
3284 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
Filipe Manana34a28e32018-12-07 15:25:38 +00003285 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM))
3286 return -EINVAL;
Timofey Titovets39739092018-05-02 08:15:36 +03003287
Timofey Titovetsb6728762018-05-02 08:15:37 +03003288 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3289 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003290
Timofey Titovetsb6728762018-05-02 08:15:37 +03003291 for (i = 0; i < chunk_count; i++) {
3292 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Filipe Manana34a28e32018-12-07 15:25:38 +00003293 dst, dst_loff);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003294 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003295 return ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003296
3297 loff += BTRFS_MAX_DEDUPE_LEN;
3298 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3299 }
3300
3301 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003302 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
Filipe Manana34a28e32018-12-07 15:25:38 +00003303 dst_loff);
Mark Fasheh416161d2013-08-06 11:42:51 -07003304
3305 return ret;
3306}
3307
Filipe Mananaf82a9902014-06-01 01:50:28 +01003308static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3309 struct inode *inode,
3310 u64 endoff,
3311 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003312 const u64 olen,
3313 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003314{
3315 struct btrfs_root *root = BTRFS_I(inode)->root;
3316 int ret;
3317
3318 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003319 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003320 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003321 /*
3322 * We round up to the block size at eof when determining which
3323 * extents to clone above, but shouldn't round up the file size.
3324 */
3325 if (endoff > destoff + olen)
3326 endoff = destoff + olen;
3327 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003328 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003329
3330 ret = btrfs_update_inode(trans, root, inode);
3331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003332 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003333 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003334 goto out;
3335 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003336 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003337out:
3338 return ret;
3339}
3340
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003341static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003342 const struct btrfs_trans_handle *trans,
3343 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003344 const u64 hole_offset,
3345 const u64 hole_len)
3346{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003347 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003348 struct extent_map *em;
3349 int ret;
3350
3351 em = alloc_extent_map();
3352 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003353 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003354 return;
3355 }
3356
Filipe Manana14f59792014-06-29 21:45:40 +01003357 if (path) {
3358 struct btrfs_file_extent_item *fi;
3359
3360 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3361 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003362 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003363 em->generation = -1;
3364 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3365 BTRFS_FILE_EXTENT_INLINE)
3366 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003367 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003368 } else {
3369 em->start = hole_offset;
3370 em->len = hole_len;
3371 em->ram_bytes = em->len;
3372 em->orig_start = hole_offset;
3373 em->block_start = EXTENT_MAP_HOLE;
3374 em->block_len = 0;
3375 em->orig_block_len = 0;
3376 em->compress_type = BTRFS_COMPRESS_NONE;
3377 em->generation = trans->transid;
3378 }
3379
3380 while (1) {
3381 write_lock(&em_tree->lock);
3382 ret = add_extent_mapping(em_tree, em, 1);
3383 write_unlock(&em_tree->lock);
3384 if (ret != -EEXIST) {
3385 free_extent_map(em);
3386 break;
3387 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003388 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003389 em->start + em->len - 1, 0);
3390 }
3391
David Sterbaee39b432014-09-30 01:33:33 +02003392 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003393 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003394}
3395
Filipe Manana8039d872015-10-13 15:15:00 +01003396/*
3397 * Make sure we do not end up inserting an inline extent into a file that has
3398 * already other (non-inline) extents. If a file has an inline extent it can
3399 * not have any other extents and the (single) inline extent must start at the
3400 * file offset 0. Failing to respect these rules will lead to file corruption,
3401 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3402 *
3403 * We can have extents that have been already written to disk or we can have
3404 * dirty ranges still in delalloc, in which case the extent maps and items are
3405 * created only when we run delalloc, and the delalloc ranges might fall outside
3406 * the range we are currently locking in the inode's io tree. So we check the
3407 * inode's i_size because of that (i_size updates are done while holding the
3408 * i_mutex, which we are holding here).
3409 * We also check to see if the inode has a size not greater than "datal" but has
3410 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3411 * protected against such concurrent fallocate calls by the i_mutex).
3412 *
3413 * If the file has no extents but a size greater than datal, do not allow the
3414 * copy because we would need turn the inline extent into a non-inline one (even
3415 * with NO_HOLES enabled). If we find our destination inode only has one inline
3416 * extent, just overwrite it with the source inline extent if its size is less
3417 * than the source extent's size, or we could copy the source inline extent's
3418 * data into the destination inode's inline extent if the later is greater then
3419 * the former.
3420 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003421static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003422 struct btrfs_trans_handle *trans,
3423 struct btrfs_path *path,
3424 struct btrfs_key *new_key,
3425 const u64 drop_start,
3426 const u64 datal,
3427 const u64 skip,
3428 const u64 size,
3429 char *inline_data)
3430{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003431 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003432 struct btrfs_root *root = BTRFS_I(dst)->root;
3433 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003435 int ret;
3436 struct btrfs_key key;
3437
3438 if (new_key->offset > 0)
3439 return -EOPNOTSUPP;
3440
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003441 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003442 key.type = BTRFS_EXTENT_DATA_KEY;
3443 key.offset = 0;
3444 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3445 if (ret < 0) {
3446 return ret;
3447 } else if (ret > 0) {
3448 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3449 ret = btrfs_next_leaf(root, path);
3450 if (ret < 0)
3451 return ret;
3452 else if (ret > 0)
3453 goto copy_inline_extent;
3454 }
3455 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003456 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003457 key.type == BTRFS_EXTENT_DATA_KEY) {
3458 ASSERT(key.offset > 0);
3459 return -EOPNOTSUPP;
3460 }
3461 } else if (i_size_read(dst) <= datal) {
3462 struct btrfs_file_extent_item *ei;
3463 u64 ext_len;
3464
3465 /*
3466 * If the file size is <= datal, make sure there are no other
3467 * extents following (can happen do to an fallocate call with
3468 * the flag FALLOC_FL_KEEP_SIZE).
3469 */
3470 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3471 struct btrfs_file_extent_item);
3472 /*
3473 * If it's an inline extent, it can not have other extents
3474 * following it.
3475 */
3476 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3477 BTRFS_FILE_EXTENT_INLINE)
3478 goto copy_inline_extent;
3479
3480 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3481 if (ext_len > aligned_end)
3482 return -EOPNOTSUPP;
3483
3484 ret = btrfs_next_item(root, path);
3485 if (ret < 0) {
3486 return ret;
3487 } else if (ret == 0) {
3488 btrfs_item_key_to_cpu(path->nodes[0], &key,
3489 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003490 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003491 key.type == BTRFS_EXTENT_DATA_KEY)
3492 return -EOPNOTSUPP;
3493 }
3494 }
3495
3496copy_inline_extent:
3497 /*
3498 * We have no extent items, or we have an extent at offset 0 which may
3499 * or may not be inlined. All these cases are dealt the same way.
3500 */
3501 if (i_size_read(dst) > datal) {
3502 /*
3503 * If the destination inode has an inline extent...
3504 * This would require copying the data from the source inline
3505 * extent into the beginning of the destination's inline extent.
3506 * But this is really complex, both extents can be compressed
3507 * or just one of them, which would require decompressing and
3508 * re-compressing data (which could increase the new compressed
3509 * size, not allowing the compressed data to fit anymore in an
3510 * inline extent).
3511 * So just don't support this case for now (it should be rare,
3512 * we are not really saving space when cloning inline extents).
3513 */
3514 return -EOPNOTSUPP;
3515 }
3516
3517 btrfs_release_path(path);
3518 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3519 if (ret)
3520 return ret;
3521 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3522 if (ret)
3523 return ret;
3524
3525 if (skip) {
3526 const u32 start = btrfs_file_extent_calc_inline_size(0);
3527
3528 memmove(inline_data + start, inline_data + start + skip, datal);
3529 }
3530
3531 write_extent_buffer(path->nodes[0], inline_data,
3532 btrfs_item_ptr_offset(path->nodes[0],
3533 path->slots[0]),
3534 size);
3535 inode_add_bytes(dst, datal);
3536
3537 return 0;
3538}
3539
Mark Fasheh32b7c682013-08-06 11:42:49 -07003540/**
3541 * btrfs_clone() - clone a range from inode file to another
3542 *
3543 * @src: Inode to clone from
3544 * @inode: Inode to clone to
3545 * @off: Offset within source to start clone from
3546 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003547 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003548 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003549 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003550 */
3551static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003552 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003553 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003554{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003555 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003556 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003557 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003558 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003559 struct btrfs_trans_handle *trans;
3560 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003561 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003562 u32 nritems;
3563 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003564 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003565 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003566 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003567
3568 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003569 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3570 if (!buf)
3571 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003572
3573 path = btrfs_alloc_path();
3574 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003575 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003576 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003577 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003578
David Sterbae4058b52015-11-27 16:31:35 +01003579 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003580 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003581 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003582 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003583 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003584
3585 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003586 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003587
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003588 /*
3589 * note the key will change type as we walk through the
3590 * tree.
3591 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003592 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003593 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3594 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003595 if (ret < 0)
3596 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003597 /*
3598 * First search, if no extent item that starts at offset off was
3599 * found but the previous item is an extent item, it's possible
3600 * it might overlap our target range, therefore process it.
3601 */
3602 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3603 btrfs_item_key_to_cpu(path->nodes[0], &key,
3604 path->slots[0] - 1);
3605 if (key.type == BTRFS_EXTENT_DATA_KEY)
3606 path->slots[0]--;
3607 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003608
Yan Zhengae01a0a2008-08-04 23:23:47 -04003609 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003610process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003611 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003612 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003613 if (ret < 0)
3614 goto out;
3615 if (ret > 0)
3616 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003617 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003618 }
3619 leaf = path->nodes[0];
3620 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003621
Yan Zhengae01a0a2008-08-04 23:23:47 -04003622 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003623 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003624 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003625 break;
3626
David Sterba962a2982014-06-04 18:41:45 +02003627 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003628 struct btrfs_file_extent_item *extent;
3629 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003630 u32 size;
3631 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003632 u64 disko = 0, diskl = 0;
3633 u64 datao = 0, datal = 0;
3634 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003635 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003636
Sage Weilc5c9cd42008-11-12 14:32:25 -05003637 extent = btrfs_item_ptr(leaf, slot,
3638 struct btrfs_file_extent_item);
3639 comp = btrfs_file_extent_compression(leaf, extent);
3640 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003641 if (type == BTRFS_FILE_EXTENT_REG ||
3642 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003643 disko = btrfs_file_extent_disk_bytenr(leaf,
3644 extent);
3645 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3646 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003647 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003648 datal = btrfs_file_extent_num_bytes(leaf,
3649 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003650 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3651 /* take upper bound, may be compressed */
3652 datal = btrfs_file_extent_ram_bytes(leaf,
3653 extent);
3654 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003655
Filipe Manana2c463822014-05-31 02:31:05 +01003656 /*
3657 * The first search might have left us at an extent
3658 * item that ends before our target range's start, can
3659 * happen if we have holes and NO_HOLES feature enabled.
3660 */
3661 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003662 path->slots[0]++;
3663 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003664 } else if (key.offset >= off + len) {
3665 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003666 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003667 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003668 size = btrfs_item_size_nr(leaf, slot);
3669 read_extent_buffer(leaf, buf,
3670 btrfs_item_ptr_offset(leaf, slot),
3671 size);
3672
3673 btrfs_release_path(path);
3674 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003675
Zheng Yan31840ae2008-09-23 13:14:14 -04003676 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003677 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003678 if (off <= key.offset)
3679 new_key.offset = key.offset + destoff - off;
3680 else
3681 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003682
Sage Weilb6f34092011-09-20 14:48:51 -04003683 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003684 * Deal with a hole that doesn't have an extent item
3685 * that represents it (NO_HOLES feature enabled).
3686 * This hole is either in the middle of the cloning
3687 * range or at the beginning (fully overlaps it or
3688 * partially overlaps it).
3689 */
3690 if (new_key.offset != last_dest_end)
3691 drop_start = last_dest_end;
3692 else
3693 drop_start = new_key.offset;
3694
3695 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003696 * 1 - adjusting old extent (we may have to split it)
3697 * 1 - add new extent
3698 * 1 - inode update
3699 */
3700 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003701 if (IS_ERR(trans)) {
3702 ret = PTR_ERR(trans);
3703 goto out;
3704 }
3705
Chris Masonc8a894d2009-06-27 21:07:03 -04003706 if (type == BTRFS_FILE_EXTENT_REG ||
3707 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003708 /*
3709 * a | --- range to clone ---| b
3710 * | ------------- extent ------------- |
3711 */
3712
Antonio Ospite93915582014-06-04 14:03:48 +02003713 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003714 if (key.offset + datal > off + len)
3715 datal = off + len - key.offset;
3716
Antonio Ospite93915582014-06-04 14:03:48 +02003717 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003718 if (off > key.offset) {
3719 datao += off - key.offset;
3720 datal -= off - key.offset;
3721 }
3722
Josef Bacik5dc562c2012-08-17 13:14:17 -04003723 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003724 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003725 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003726 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003727 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003728 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003729 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003730 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003731 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003732 goto out;
3733 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003734
Sage Weilc5c9cd42008-11-12 14:32:25 -05003735 ret = btrfs_insert_empty_item(trans, root, path,
3736 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003737 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003738 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003739 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003740 goto out;
3741 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003742
3743 leaf = path->nodes[0];
3744 slot = path->slots[0];
3745 write_extent_buffer(leaf, buf,
3746 btrfs_item_ptr_offset(leaf, slot),
3747 size);
3748
3749 extent = btrfs_item_ptr(leaf, slot,
3750 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003751
Sage Weilc5c9cd42008-11-12 14:32:25 -05003752 /* disko == 0 means it's a hole */
3753 if (!disko)
3754 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003755
3756 btrfs_set_file_extent_offset(leaf, extent,
3757 datao);
3758 btrfs_set_file_extent_num_bytes(leaf, extent,
3759 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003760
Sage Weilc5c9cd42008-11-12 14:32:25 -05003761 if (disko) {
3762 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003763 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003764 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003765 disko, diskl, 0,
3766 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003767 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003768 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003769 if (ret) {
3770 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003771 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003772 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003773 goto out;
3774
3775 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003776 }
3777 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3778 u64 skip = 0;
3779 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003780
Sage Weilc5c9cd42008-11-12 14:32:25 -05003781 if (off > key.offset) {
3782 skip = off - key.offset;
3783 new_key.offset += skip;
3784 }
Chris Masond3977122009-01-05 21:25:51 -05003785
Liu Boaa42ffd2012-09-18 03:52:23 -06003786 if (key.offset + datal > off + len)
3787 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003788
Sage Weilc5c9cd42008-11-12 14:32:25 -05003789 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003790 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003791 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003792 goto out;
3793 }
3794 size -= skip + trim;
3795 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003796
David Sterba4a0ab9d2017-02-10 20:18:49 +01003797 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003798 trans, path,
3799 &new_key,
3800 drop_start,
3801 datal,
3802 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003803 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003804 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003805 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003806 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003807 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003808 goto out;
3809 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003810 leaf = path->nodes[0];
3811 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003812 }
3813
Filipe Manana7ffbb592014-06-09 03:48:05 +01003814 /* If we have an implicit hole (NO_HOLES feature). */
3815 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003816 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003817 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003818 new_key.offset - drop_start);
3819
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003820 clone_update_extent_map(BTRFS_I(inode), trans,
3821 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003822
Sage Weilc5c9cd42008-11-12 14:32:25 -05003823 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003824 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003825
Filipe Manana62e23902014-08-08 02:47:06 +01003826 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003827 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003828 ret = clone_finish_inode_update(trans, inode,
3829 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003830 destoff, olen,
3831 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003832 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003833 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003834 if (new_key.offset + datal >= destoff + len)
3835 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003836 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003837 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003838 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003839
3840 if (fatal_signal_pending(current)) {
3841 ret = -EINTR;
3842 goto out;
3843 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003844 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003845 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003846
Filipe Mananaf82a9902014-06-01 01:50:28 +01003847 if (last_dest_end < destoff + len) {
3848 /*
3849 * We have an implicit hole (NO_HOLES feature is enabled) that
3850 * fully or partially overlaps our cloning range at its end.
3851 */
3852 btrfs_release_path(path);
3853
3854 /*
3855 * 1 - remove extent(s)
3856 * 1 - inode update
3857 */
3858 trans = btrfs_start_transaction(root, 2);
3859 if (IS_ERR(trans)) {
3860 ret = PTR_ERR(trans);
3861 goto out;
3862 }
3863 ret = btrfs_drop_extents(trans, root, inode,
3864 last_dest_end, destoff + len, 1);
3865 if (ret) {
3866 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003867 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003868 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003869 goto out;
3870 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003871 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3872 last_dest_end,
3873 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003874 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003875 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003876 }
3877
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003878out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003879 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003880 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003881 return ret;
3882}
3883
Zach Brown3db11b22015-11-10 16:53:32 -05003884static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3885 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003886{
Al Viro54563d42013-09-01 15:57:51 -04003887 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003888 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003890 int ret;
3891 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003892 u64 bs = fs_info->sb->s_blocksize;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003893
3894 /*
3895 * TODO:
3896 * - split compressed inline extents. annoying: we need to
3897 * decompress into destination's address_space (the file offset
3898 * may change, so source mapping won't do), then recompress (or
3899 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003900 *
3901 * - split destination inode's inline extents. The inline extents can
3902 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003903 */
3904
Omar Sandovalb5c40d52018-05-22 15:02:12 -07003905 /* don't make the dst file partly checksummed */
3906 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
Filipe Manana34a28e32018-12-07 15:25:38 +00003907 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3908 return -EINVAL;
Omar Sandovalb5c40d52018-05-22 15:02:12 -07003909
Filipe Mananaac765f82018-11-05 11:14:17 +00003910 /*
Filipe Manana34a28e32018-12-07 15:25:38 +00003911 * VFS's generic_remap_file_range_prep() protects us from cloning the
3912 * eof block into the middle of a file, which would result in corruption
3913 * if the file size is not blocksize aligned. So we don't need to check
3914 * for that case here.
Filipe Mananaac765f82018-11-05 11:14:17 +00003915 */
Filipe Manana34a28e32018-12-07 15:25:38 +00003916 if (off + len == src->i_size)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003917 len = ALIGN(src->i_size, bs) - off;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003918
3919 if (destoff > inode->i_size) {
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003920 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3921
Mark Fasheh32b7c682013-08-06 11:42:49 -07003922 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3923 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003924 return ret;
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003925 /*
3926 * We may have truncated the last block if the inode's size is
3927 * not sector size aligned, so we need to wait for writeback to
3928 * complete before proceeding further, otherwise we can race
3929 * with cloning and attempt to increment a reference to an
3930 * extent that no longer exists (writeback completed right after
3931 * we found the previous extent covering eof and before we
3932 * attempted to increment its reference count).
3933 */
3934 ret = btrfs_wait_ordered_range(inode, wb_start,
3935 destoff - wb_start);
3936 if (ret)
3937 return ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003938 }
3939
Filipe Mananac125b8b2014-05-23 05:03:34 +01003940 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003941 * Lock destination range to serialize with concurrent readpages() and
3942 * source range to serialize with relocation.
Filipe Mananac125b8b2014-05-23 05:03:34 +01003943 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003944 btrfs_double_extent_lock(src, off, inode, destoff, len);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003945 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Filipe Mananad8b55242019-01-08 11:43:07 +00003946 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003947 /*
3948 * Truncate page cache pages so that future reads will see the cloned
3949 * data immediately and not the previous data.
3950 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303951 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003952 round_down(destoff, PAGE_SIZE),
3953 round_up(destoff + len, PAGE_SIZE) - 1);
Filipe Manana34a28e32018-12-07 15:25:38 +00003954
3955 return ret;
3956}
3957
3958static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3959 struct file *file_out, loff_t pos_out,
3960 loff_t *len, unsigned int remap_flags)
3961{
3962 struct inode *inode_in = file_inode(file_in);
3963 struct inode *inode_out = file_inode(file_out);
3964 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3965 bool same_inode = inode_out == inode_in;
3966 u64 wb_len;
3967 int ret;
3968
3969 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3970 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3971
3972 if (btrfs_root_readonly(root_out))
3973 return -EROFS;
3974
3975 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3976 inode_in->i_sb != inode_out->i_sb)
3977 return -EXDEV;
3978 }
3979
3980 if (same_inode)
3981 inode_lock(inode_in);
Mark Fasheh293a8482015-06-30 14:42:06 -07003982 else
Filipe Manana34a28e32018-12-07 15:25:38 +00003983 btrfs_double_inode_lock(inode_in, inode_out);
3984
3985 /*
3986 * Now that the inodes are locked, we need to start writeback ourselves
3987 * and can not rely on the writeback from the VFS's generic helper
3988 * generic_remap_file_range_prep() because:
3989 *
3990 * 1) For compression we must call filemap_fdatawrite_range() range
3991 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3992 * helper only calls it once;
3993 *
3994 * 2) filemap_fdatawrite_range(), called by the generic helper only
3995 * waits for the writeback to complete, i.e. for IO to be done, and
3996 * not for the ordered extents to complete. We need to wait for them
3997 * to complete so that new file extent items are in the fs tree.
3998 */
3999 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
4000 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
4001 else
4002 wb_len = ALIGN(*len, bs);
4003
4004 /*
4005 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
4006 * any in progress could create its ordered extents after we wait for
4007 * existing ordered extents below).
4008 */
4009 inode_dio_wait(inode_in);
4010 if (!same_inode)
4011 inode_dio_wait(inode_out);
4012
4013 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
4014 wb_len);
4015 if (ret < 0)
4016 goto out_unlock;
4017 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
4018 wb_len);
4019 if (ret < 0)
4020 goto out_unlock;
4021
4022 ret = generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
4023 len, remap_flags);
4024 if (ret < 0 || *len == 0)
4025 goto out_unlock;
4026
4027 return 0;
4028
4029 out_unlock:
4030 if (same_inode)
4031 inode_unlock(inode_in);
4032 else
4033 btrfs_double_inode_unlock(inode_in, inode_out);
4034
Zach Brown3db11b22015-11-10 16:53:32 -05004035 return ret;
4036}
4037
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004038loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4039 struct file *dst_file, loff_t destoff, loff_t len,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004040 unsigned int remap_flags)
Zach Brown3db11b22015-11-10 16:53:32 -05004041{
Filipe Manana34a28e32018-12-07 15:25:38 +00004042 struct inode *src_inode = file_inode(src_file);
4043 struct inode *dst_inode = file_inode(dst_file);
4044 bool same_inode = dst_inode == src_inode;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004045 int ret;
4046
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004047 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4048 return -EINVAL;
4049
Filipe Manana34a28e32018-12-07 15:25:38 +00004050 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4051 &len, remap_flags);
4052 if (ret < 0 || len == 0)
4053 return ret;
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004054
Filipe Manana34a28e32018-12-07 15:25:38 +00004055 if (remap_flags & REMAP_FILE_DEDUP)
4056 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4057 else
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004058 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
Filipe Manana34a28e32018-12-07 15:25:38 +00004059
4060 if (same_inode)
4061 inode_unlock(src_inode);
4062 else
4063 btrfs_double_inode_unlock(src_inode, dst_inode);
4064
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004065 return ret < 0 ? ret : len;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004066}
4067
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004068static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4069{
Al Viro496ad9a2013-01-23 17:07:38 -05004070 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004071 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004072 struct btrfs_root *root = BTRFS_I(inode)->root;
4073 struct btrfs_root *new_root;
4074 struct btrfs_dir_item *di;
4075 struct btrfs_trans_handle *trans;
4076 struct btrfs_path *path;
4077 struct btrfs_key location;
4078 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004079 u64 objectid = 0;
4080 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004081 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004082
4083 if (!capable(CAP_SYS_ADMIN))
4084 return -EPERM;
4085
Miao Xie3c04ce02012-11-26 08:43:07 +00004086 ret = mnt_want_write_file(file);
4087 if (ret)
4088 return ret;
4089
4090 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4091 ret = -EFAULT;
4092 goto out;
4093 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004094
4095 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304096 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004097
4098 location.objectid = objectid;
4099 location.type = BTRFS_ROOT_ITEM_KEY;
4100 location.offset = (u64)-1;
4101
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004102 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004103 if (IS_ERR(new_root)) {
4104 ret = PTR_ERR(new_root);
4105 goto out;
4106 }
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09004107 if (!is_fstree(new_root->root_key.objectid)) {
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004108 ret = -ENOENT;
4109 goto out;
4110 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004111
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004112 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004113 if (!path) {
4114 ret = -ENOMEM;
4115 goto out;
4116 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004117 path->leave_spinning = 1;
4118
4119 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004120 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004121 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004122 ret = PTR_ERR(trans);
4123 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004124 }
4125
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004126 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4127 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004128 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004129 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004130 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004131 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004132 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004133 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004134 ret = -ENOENT;
4135 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004136 }
4137
4138 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4139 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4140 btrfs_mark_buffer_dirty(path->nodes[0]);
4141 btrfs_free_path(path);
4142
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004143 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004144 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004145out:
4146 mnt_drop_write_file(file);
4147 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004148}
4149
Su Yuec065f5b12018-04-02 17:24:11 +08004150static void get_block_group_info(struct list_head *groups_list,
4151 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004152{
4153 struct btrfs_block_group_cache *block_group;
4154
4155 space->total_bytes = 0;
4156 space->used_bytes = 0;
4157 space->flags = 0;
4158 list_for_each_entry(block_group, groups_list, list) {
4159 space->flags = block_group->flags;
4160 space->total_bytes += block_group->key.offset;
4161 space->used_bytes +=
4162 btrfs_block_group_used(&block_group->item);
4163 }
4164}
4165
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004166static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4167 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004168{
4169 struct btrfs_ioctl_space_args space_args;
4170 struct btrfs_ioctl_space_info space;
4171 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004172 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004173 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004174 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004175 static const u64 types[] = {
4176 BTRFS_BLOCK_GROUP_DATA,
4177 BTRFS_BLOCK_GROUP_SYSTEM,
4178 BTRFS_BLOCK_GROUP_METADATA,
4179 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4180 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004181 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004182 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004183 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004184 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004185 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004186
4187 if (copy_from_user(&space_args,
4188 (struct btrfs_ioctl_space_args __user *)arg,
4189 sizeof(space_args)))
4190 return -EFAULT;
4191
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004192 for (i = 0; i < num_types; i++) {
4193 struct btrfs_space_info *tmp;
4194
4195 info = NULL;
4196 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004197 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004198 list) {
4199 if (tmp->flags == types[i]) {
4200 info = tmp;
4201 break;
4202 }
4203 }
4204 rcu_read_unlock();
4205
4206 if (!info)
4207 continue;
4208
4209 down_read(&info->groups_sem);
4210 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4211 if (!list_empty(&info->block_groups[c]))
4212 slot_count++;
4213 }
4214 up_read(&info->groups_sem);
4215 }
Josef Bacik1406e432010-01-13 18:19:06 +00004216
David Sterba36523e952014-02-07 14:34:12 +01004217 /*
4218 * Global block reserve, exported as a space_info
4219 */
4220 slot_count++;
4221
Chris Mason7fde62b2010-03-16 15:40:10 -04004222 /* space_slots == 0 means they are asking for a count */
4223 if (space_args.space_slots == 0) {
4224 space_args.total_spaces = slot_count;
4225 goto out;
4226 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004227
Dan Rosenberg51788b12011-02-14 16:04:23 -05004228 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004229
Chris Mason7fde62b2010-03-16 15:40:10 -04004230 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004231
Chris Mason7fde62b2010-03-16 15:40:10 -04004232 /* we generally have at most 6 or so space infos, one for each raid
4233 * level. So, a whole page should be more than enough for everyone
4234 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004235 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004236 return -ENOMEM;
4237
4238 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004239 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004240 if (!dest)
4241 return -ENOMEM;
4242 dest_orig = dest;
4243
4244 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004245 for (i = 0; i < num_types; i++) {
4246 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004247
Dan Rosenberg51788b12011-02-14 16:04:23 -05004248 if (!slot_count)
4249 break;
4250
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004251 info = NULL;
4252 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004253 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004254 list) {
4255 if (tmp->flags == types[i]) {
4256 info = tmp;
4257 break;
4258 }
4259 }
4260 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004261
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004262 if (!info)
4263 continue;
4264 down_read(&info->groups_sem);
4265 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4266 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004267 get_block_group_info(&info->block_groups[c],
4268 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004269 memcpy(dest, &space, sizeof(space));
4270 dest++;
4271 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004272 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004273 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004274 if (!slot_count)
4275 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004276 }
4277 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004278 }
Josef Bacik1406e432010-01-13 18:19:06 +00004279
David Sterba36523e952014-02-07 14:34:12 +01004280 /*
4281 * Add global block reserve
4282 */
4283 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004284 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004285
4286 spin_lock(&block_rsv->lock);
4287 space.total_bytes = block_rsv->size;
4288 space.used_bytes = block_rsv->size - block_rsv->reserved;
4289 spin_unlock(&block_rsv->lock);
4290 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4291 memcpy(dest, &space, sizeof(space));
4292 space_args.total_spaces++;
4293 }
4294
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004295 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004296 (arg + sizeof(struct btrfs_ioctl_space_args));
4297
4298 if (copy_to_user(user_dest, dest_orig, alloc_size))
4299 ret = -EFAULT;
4300
4301 kfree(dest_orig);
4302out:
4303 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004304 ret = -EFAULT;
4305
4306 return ret;
4307}
4308
Miao Xie9a8c28b2012-11-26 08:40:43 +00004309static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4310 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004311{
Sage Weil46204592010-10-29 15:41:32 -04004312 struct btrfs_trans_handle *trans;
4313 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004314 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004315
Miao Xied4edf392013-02-20 09:17:06 +00004316 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004317 if (IS_ERR(trans)) {
4318 if (PTR_ERR(trans) != -ENOENT)
4319 return PTR_ERR(trans);
4320
4321 /* No running transaction, don't bother */
4322 transid = root->fs_info->last_trans_committed;
4323 goto out;
4324 }
Sage Weil46204592010-10-29 15:41:32 -04004325 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004326 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004327 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004328 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004329 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004330 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004331out:
Sage Weil46204592010-10-29 15:41:32 -04004332 if (argp)
4333 if (copy_to_user(argp, &transid, sizeof(transid)))
4334 return -EFAULT;
4335 return 0;
4336}
4337
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004338static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004339 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004340{
Sage Weil46204592010-10-29 15:41:32 -04004341 u64 transid;
4342
4343 if (argp) {
4344 if (copy_from_user(&transid, argp, sizeof(transid)))
4345 return -EFAULT;
4346 } else {
4347 transid = 0; /* current trans */
4348 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004349 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004350}
4351
Miao Xieb8e95482012-11-26 08:48:01 +00004352static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004354 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004355 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004356 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004357
4358 if (!capable(CAP_SYS_ADMIN))
4359 return -EPERM;
4360
4361 sa = memdup_user(arg, sizeof(*sa));
4362 if (IS_ERR(sa))
4363 return PTR_ERR(sa);
4364
Miao Xieb8e95482012-11-26 08:48:01 +00004365 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4366 ret = mnt_want_write_file(file);
4367 if (ret)
4368 goto out;
4369 }
4370
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004371 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004372 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4373 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004374
Filipe Manana06fe39a2018-12-14 19:50:17 +00004375 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004376 ret = -EFAULT;
4377
Miao Xieb8e95482012-11-26 08:48:01 +00004378 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4379 mnt_drop_write_file(file);
4380out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004381 kfree(sa);
4382 return ret;
4383}
4384
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004385static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004386{
4387 if (!capable(CAP_SYS_ADMIN))
4388 return -EPERM;
4389
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004390 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004391}
4392
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004393static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004394 void __user *arg)
4395{
4396 struct btrfs_ioctl_scrub_args *sa;
4397 int ret;
4398
4399 if (!capable(CAP_SYS_ADMIN))
4400 return -EPERM;
4401
4402 sa = memdup_user(arg, sizeof(*sa));
4403 if (IS_ERR(sa))
4404 return PTR_ERR(sa);
4405
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004406 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004407
Filipe Manana4fa99b02018-12-14 19:45:13 +00004408 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004409 ret = -EFAULT;
4410
4411 kfree(sa);
4412 return ret;
4413}
4414
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004415static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004416 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004417{
4418 struct btrfs_ioctl_get_dev_stats *sa;
4419 int ret;
4420
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004421 sa = memdup_user(arg, sizeof(*sa));
4422 if (IS_ERR(sa))
4423 return PTR_ERR(sa);
4424
David Sterbab27f7c02012-06-22 06:30:39 -06004425 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4426 kfree(sa);
4427 return -EPERM;
4428 }
4429
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004430 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004431
Filipe Mananaeee99572018-12-14 19:45:22 +00004432 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004433 ret = -EFAULT;
4434
4435 kfree(sa);
4436 return ret;
4437}
4438
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004439static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4440 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004441{
4442 struct btrfs_ioctl_dev_replace_args *p;
4443 int ret;
4444
4445 if (!capable(CAP_SYS_ADMIN))
4446 return -EPERM;
4447
4448 p = memdup_user(arg, sizeof(*p));
4449 if (IS_ERR(p))
4450 return PTR_ERR(p);
4451
4452 switch (p->cmd) {
4453 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004454 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004455 ret = -EROFS;
4456 goto out;
4457 }
David Sterba171938e2017-03-28 14:44:21 +02004458 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004459 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004460 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004461 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004462 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004463 }
4464 break;
4465 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004466 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004467 ret = 0;
4468 break;
4469 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004470 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004471 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004472 break;
4473 default:
4474 ret = -EINVAL;
4475 break;
4476 }
4477
Filipe Mananad3a53282019-01-08 11:42:09 +00004478 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004479 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004480out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004481 kfree(p);
4482 return ret;
4483}
4484
Jan Schmidtd7728c92011-07-07 16:48:38 +02004485static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4486{
4487 int ret = 0;
4488 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004489 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004490 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004491 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004492 struct inode_fs_paths *ipath = NULL;
4493 struct btrfs_path *path;
4494
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004495 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004496 return -EPERM;
4497
4498 path = btrfs_alloc_path();
4499 if (!path) {
4500 ret = -ENOMEM;
4501 goto out;
4502 }
4503
4504 ipa = memdup_user(arg, sizeof(*ipa));
4505 if (IS_ERR(ipa)) {
4506 ret = PTR_ERR(ipa);
4507 ipa = NULL;
4508 goto out;
4509 }
4510
4511 size = min_t(u32, ipa->size, 4096);
4512 ipath = init_ipath(size, root, path);
4513 if (IS_ERR(ipath)) {
4514 ret = PTR_ERR(ipath);
4515 ipath = NULL;
4516 goto out;
4517 }
4518
4519 ret = paths_from_inode(ipa->inum, ipath);
4520 if (ret < 0)
4521 goto out;
4522
4523 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004524 rel_ptr = ipath->fspath->val[i] -
4525 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004526 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004527 }
4528
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004529 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4530 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004531 if (ret) {
4532 ret = -EFAULT;
4533 goto out;
4534 }
4535
4536out:
4537 btrfs_free_path(path);
4538 free_ipath(ipath);
4539 kfree(ipa);
4540
4541 return ret;
4542}
4543
4544static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4545{
4546 struct btrfs_data_container *inodes = ctx;
4547 const size_t c = 3 * sizeof(u64);
4548
4549 if (inodes->bytes_left >= c) {
4550 inodes->bytes_left -= c;
4551 inodes->val[inodes->elem_cnt] = inum;
4552 inodes->val[inodes->elem_cnt + 1] = offset;
4553 inodes->val[inodes->elem_cnt + 2] = root;
4554 inodes->elem_cnt += 3;
4555 } else {
4556 inodes->bytes_missing += c - inodes->bytes_left;
4557 inodes->bytes_left = 0;
4558 inodes->elem_missed += 3;
4559 }
4560
4561 return 0;
4562}
4563
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004564static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004565 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004566{
4567 int ret = 0;
4568 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004569 struct btrfs_ioctl_logical_ino_args *loi;
4570 struct btrfs_data_container *inodes = NULL;
4571 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004572 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004573
4574 if (!capable(CAP_SYS_ADMIN))
4575 return -EPERM;
4576
4577 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304578 if (IS_ERR(loi))
4579 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004580
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004581 if (version == 1) {
4582 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004583 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004584 } else {
4585 /* All reserved bits must be 0 for now */
4586 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4587 ret = -EINVAL;
4588 goto out_loi;
4589 }
4590 /* Only accept flags we have defined so far */
4591 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4592 ret = -EINVAL;
4593 goto out_loi;
4594 }
4595 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004596 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004597 }
4598
Jan Schmidtd7728c92011-07-07 16:48:38 +02004599 path = btrfs_alloc_path();
4600 if (!path) {
4601 ret = -ENOMEM;
4602 goto out;
4603 }
4604
Jan Schmidtd7728c92011-07-07 16:48:38 +02004605 inodes = init_data_container(size);
4606 if (IS_ERR(inodes)) {
4607 ret = PTR_ERR(inodes);
4608 inodes = NULL;
4609 goto out;
4610 }
4611
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004612 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004613 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004614 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004615 ret = -ENOENT;
4616 if (ret < 0)
4617 goto out;
4618
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004619 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4620 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004621 if (ret)
4622 ret = -EFAULT;
4623
4624out:
4625 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004626 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004627out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004628 kfree(loi);
4629
4630 return ret;
4631}
4632
David Sterba008ef092018-03-21 02:05:27 +01004633void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004634 struct btrfs_ioctl_balance_args *bargs)
4635{
4636 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4637
4638 bargs->flags = bctl->flags;
4639
David Sterba3009a622018-03-21 01:31:04 +01004640 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004641 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4642 if (atomic_read(&fs_info->balance_pause_req))
4643 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004644 if (atomic_read(&fs_info->balance_cancel_req))
4645 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004646
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004647 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4648 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4649 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004650
David Sterba008ef092018-03-21 02:05:27 +01004651 spin_lock(&fs_info->balance_lock);
4652 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4653 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004654}
4655
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004656static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004657{
Al Viro496ad9a2013-01-23 17:07:38 -05004658 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004659 struct btrfs_fs_info *fs_info = root->fs_info;
4660 struct btrfs_ioctl_balance_args *bargs;
4661 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004662 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004663 int ret;
4664
4665 if (!capable(CAP_SYS_ADMIN))
4666 return -EPERM;
4667
Liu Boe54bfa312012-06-29 03:58:48 -06004668 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004669 if (ret)
4670 return ret;
4671
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004672again:
David Sterba171938e2017-03-28 14:44:21 +02004673 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004674 mutex_lock(&fs_info->balance_mutex);
4675 need_unlock = true;
4676 goto locked;
4677 }
4678
4679 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004680 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004681 * (1) some other op is running
4682 * (2) balance is running
4683 * (3) balance is paused -- special case (think resume)
4684 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004685 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004686 if (fs_info->balance_ctl) {
4687 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004688 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004689 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004690 /*
4691 * Lock released to allow other waiters to continue,
4692 * we'll reexamine the status again.
4693 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004694 mutex_lock(&fs_info->balance_mutex);
4695
4696 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004697 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004698 /* this is (3) */
4699 need_unlock = false;
4700 goto locked;
4701 }
4702
4703 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004704 goto again;
4705 } else {
4706 /* this is (2) */
4707 mutex_unlock(&fs_info->balance_mutex);
4708 ret = -EINPROGRESS;
4709 goto out;
4710 }
4711 } else {
4712 /* this is (1) */
4713 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004714 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004715 goto out;
4716 }
4717
4718locked:
David Sterba171938e2017-03-28 14:44:21 +02004719 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004720
4721 if (arg) {
4722 bargs = memdup_user(arg, sizeof(*bargs));
4723 if (IS_ERR(bargs)) {
4724 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004725 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004726 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004727
4728 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4729 if (!fs_info->balance_ctl) {
4730 ret = -ENOTCONN;
4731 goto out_bargs;
4732 }
4733
4734 bctl = fs_info->balance_ctl;
4735 spin_lock(&fs_info->balance_lock);
4736 bctl->flags |= BTRFS_BALANCE_RESUME;
4737 spin_unlock(&fs_info->balance_lock);
4738
4739 goto do_balance;
4740 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004741 } else {
4742 bargs = NULL;
4743 }
4744
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004745 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004746 ret = -EINPROGRESS;
4747 goto out_bargs;
4748 }
4749
David Sterba8d2db782015-11-04 15:38:29 +01004750 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004751 if (!bctl) {
4752 ret = -ENOMEM;
4753 goto out_bargs;
4754 }
4755
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004756 if (arg) {
4757 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4758 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4759 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4760
4761 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004762 } else {
4763 /* balance everything - no filters */
4764 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004765 }
4766
David Sterba8eb93452015-10-12 16:55:54 +02004767 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4768 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004769 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004770 }
4771
Ilya Dryomovde322262012-01-16 22:04:49 +02004772do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004773 /*
David Sterba149196a2018-03-20 20:23:09 +01004774 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4775 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4776 * restriper was paused all the way until unmount, in free_fs_info.
4777 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004778 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004779 need_unlock = false;
4780
David Sterba6fcf6e22018-05-07 17:44:03 +02004781 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004782 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004783
Filipe Mananad00c2d92019-01-08 11:42:01 +00004784 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004785 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4786 ret = -EFAULT;
4787 }
4788
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004789out_bctl:
4790 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004791out_bargs:
4792 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004793out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004794 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004795 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004796 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004797out:
Liu Boe54bfa312012-06-29 03:58:48 -06004798 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004799 return ret;
4800}
4801
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004802static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004803{
4804 if (!capable(CAP_SYS_ADMIN))
4805 return -EPERM;
4806
4807 switch (cmd) {
4808 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004809 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004810 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004811 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004812 }
4813
4814 return -EINVAL;
4815}
4816
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004817static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004818 void __user *arg)
4819{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004820 struct btrfs_ioctl_balance_args *bargs;
4821 int ret = 0;
4822
4823 if (!capable(CAP_SYS_ADMIN))
4824 return -EPERM;
4825
4826 mutex_lock(&fs_info->balance_mutex);
4827 if (!fs_info->balance_ctl) {
4828 ret = -ENOTCONN;
4829 goto out;
4830 }
4831
David Sterba8d2db782015-11-04 15:38:29 +01004832 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004833 if (!bargs) {
4834 ret = -ENOMEM;
4835 goto out;
4836 }
4837
David Sterba008ef092018-03-21 02:05:27 +01004838 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004839
4840 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4841 ret = -EFAULT;
4842
4843 kfree(bargs);
4844out:
4845 mutex_unlock(&fs_info->balance_mutex);
4846 return ret;
4847}
4848
Miao Xie905b0dd2012-11-26 08:50:11 +00004849static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004850{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004851 struct inode *inode = file_inode(file);
4852 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004853 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004854 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004855
4856 if (!capable(CAP_SYS_ADMIN))
4857 return -EPERM;
4858
Miao Xie905b0dd2012-11-26 08:50:11 +00004859 ret = mnt_want_write_file(file);
4860 if (ret)
4861 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004862
4863 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004864 if (IS_ERR(sa)) {
4865 ret = PTR_ERR(sa);
4866 goto drop_write;
4867 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004868
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004869 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004870
4871 switch (sa->cmd) {
4872 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004873 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004874 break;
4875 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004876 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004877 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004878 default:
4879 ret = -EINVAL;
4880 break;
4881 }
4882
Arne Jansen5d13a372011-09-14 15:53:51 +02004883 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004885drop_write:
4886 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004887 return ret;
4888}
4889
Miao Xie905b0dd2012-11-26 08:50:11 +00004890static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004891{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004892 struct inode *inode = file_inode(file);
4893 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4894 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004895 struct btrfs_ioctl_qgroup_assign_args *sa;
4896 struct btrfs_trans_handle *trans;
4897 int ret;
4898 int err;
4899
4900 if (!capable(CAP_SYS_ADMIN))
4901 return -EPERM;
4902
Miao Xie905b0dd2012-11-26 08:50:11 +00004903 ret = mnt_want_write_file(file);
4904 if (ret)
4905 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004906
4907 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004908 if (IS_ERR(sa)) {
4909 ret = PTR_ERR(sa);
4910 goto drop_write;
4911 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004912
4913 trans = btrfs_join_transaction(root);
4914 if (IS_ERR(trans)) {
4915 ret = PTR_ERR(trans);
4916 goto out;
4917 }
4918
Arne Jansen5d13a372011-09-14 15:53:51 +02004919 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004920 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004921 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004922 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004923 }
4924
Qu Wenruoe082f562015-02-27 16:24:28 +08004925 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004926 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004927 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004928 btrfs_handle_fs_error(fs_info, err,
4929 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004930 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004931 if (err && !ret)
4932 ret = err;
4933
4934out:
4935 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004936drop_write:
4937 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004938 return ret;
4939}
4940
Miao Xie905b0dd2012-11-26 08:50:11 +00004941static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004942{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004943 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004944 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004945 struct btrfs_ioctl_qgroup_create_args *sa;
4946 struct btrfs_trans_handle *trans;
4947 int ret;
4948 int err;
4949
4950 if (!capable(CAP_SYS_ADMIN))
4951 return -EPERM;
4952
Miao Xie905b0dd2012-11-26 08:50:11 +00004953 ret = mnt_want_write_file(file);
4954 if (ret)
4955 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004956
4957 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004958 if (IS_ERR(sa)) {
4959 ret = PTR_ERR(sa);
4960 goto drop_write;
4961 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004962
Miao Xied86e56c2012-11-15 11:35:41 +00004963 if (!sa->qgroupid) {
4964 ret = -EINVAL;
4965 goto out;
4966 }
4967
Arne Jansen5d13a372011-09-14 15:53:51 +02004968 trans = btrfs_join_transaction(root);
4969 if (IS_ERR(trans)) {
4970 ret = PTR_ERR(trans);
4971 goto out;
4972 }
4973
Arne Jansen5d13a372011-09-14 15:53:51 +02004974 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004975 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004976 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004977 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004978 }
4979
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004980 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004981 if (err && !ret)
4982 ret = err;
4983
4984out:
4985 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004986drop_write:
4987 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004988 return ret;
4989}
4990
Miao Xie905b0dd2012-11-26 08:50:11 +00004991static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004992{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004993 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004994 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004995 struct btrfs_ioctl_qgroup_limit_args *sa;
4996 struct btrfs_trans_handle *trans;
4997 int ret;
4998 int err;
4999 u64 qgroupid;
5000
5001 if (!capable(CAP_SYS_ADMIN))
5002 return -EPERM;
5003
Miao Xie905b0dd2012-11-26 08:50:11 +00005004 ret = mnt_want_write_file(file);
5005 if (ret)
5006 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005007
5008 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005009 if (IS_ERR(sa)) {
5010 ret = PTR_ERR(sa);
5011 goto drop_write;
5012 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005013
5014 trans = btrfs_join_transaction(root);
5015 if (IS_ERR(trans)) {
5016 ret = PTR_ERR(trans);
5017 goto out;
5018 }
5019
5020 qgroupid = sa->qgroupid;
5021 if (!qgroupid) {
5022 /* take the current subvol as qgroup */
5023 qgroupid = root->root_key.objectid;
5024 }
5025
Lu Fengqif0042d52018-07-18 14:45:35 +08005026 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005027
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005028 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005029 if (err && !ret)
5030 ret = err;
5031
5032out:
5033 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005034drop_write:
5035 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005036 return ret;
5037}
5038
Jan Schmidt2f232032013-04-25 16:04:51 +00005039static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5040{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005041 struct inode *inode = file_inode(file);
5042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005043 struct btrfs_ioctl_quota_rescan_args *qsa;
5044 int ret;
5045
5046 if (!capable(CAP_SYS_ADMIN))
5047 return -EPERM;
5048
5049 ret = mnt_want_write_file(file);
5050 if (ret)
5051 return ret;
5052
5053 qsa = memdup_user(arg, sizeof(*qsa));
5054 if (IS_ERR(qsa)) {
5055 ret = PTR_ERR(qsa);
5056 goto drop_write;
5057 }
5058
5059 if (qsa->flags) {
5060 ret = -EINVAL;
5061 goto out;
5062 }
5063
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005064 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005065
5066out:
5067 kfree(qsa);
5068drop_write:
5069 mnt_drop_write_file(file);
5070 return ret;
5071}
5072
5073static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5074{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005075 struct inode *inode = file_inode(file);
5076 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005077 struct btrfs_ioctl_quota_rescan_args *qsa;
5078 int ret = 0;
5079
5080 if (!capable(CAP_SYS_ADMIN))
5081 return -EPERM;
5082
David Sterba8d2db782015-11-04 15:38:29 +01005083 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005084 if (!qsa)
5085 return -ENOMEM;
5086
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005087 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005088 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005089 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005090 }
5091
5092 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5093 ret = -EFAULT;
5094
5095 kfree(qsa);
5096 return ret;
5097}
5098
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005099static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5100{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005101 struct inode *inode = file_inode(file);
5102 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005103
5104 if (!capable(CAP_SYS_ADMIN))
5105 return -EPERM;
5106
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005107 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005108}
5109
Hugo Millsabccd002014-01-30 20:17:00 +00005110static long _btrfs_ioctl_set_received_subvol(struct file *file,
5111 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005112{
Al Viro496ad9a2013-01-23 17:07:38 -05005113 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005114 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005115 struct btrfs_root *root = BTRFS_I(inode)->root;
5116 struct btrfs_root_item *root_item = &root->root_item;
5117 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07005118 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005119 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005120 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005121
David Sterbabd60ea02014-01-16 15:50:22 +01005122 if (!inode_owner_or_capable(inode))
5123 return -EPERM;
5124
Alexander Block8ea05e32012-07-25 17:35:53 +02005125 ret = mnt_want_write_file(file);
5126 if (ret < 0)
5127 return ret;
5128
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005129 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005130
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005131 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005132 ret = -EINVAL;
5133 goto out;
5134 }
5135
5136 if (btrfs_root_readonly(root)) {
5137 ret = -EROFS;
5138 goto out;
5139 }
5140
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005141 /*
5142 * 1 - root item
5143 * 2 - uuid items (received uuid + subvol uuid)
5144 */
5145 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005146 if (IS_ERR(trans)) {
5147 ret = PTR_ERR(trans);
5148 trans = NULL;
5149 goto out;
5150 }
5151
5152 sa->rtransid = trans->transid;
5153 sa->rtime.sec = ct.tv_sec;
5154 sa->rtime.nsec = ct.tv_nsec;
5155
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005156 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5157 BTRFS_UUID_SIZE);
5158 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005159 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005160 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005161 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5162 root->root_key.objectid);
5163 if (ret && ret != -ENOENT) {
5164 btrfs_abort_transaction(trans, ret);
5165 btrfs_end_transaction(trans);
5166 goto out;
5167 }
5168 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005169 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5170 btrfs_set_root_stransid(root_item, sa->stransid);
5171 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005172 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5173 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5174 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5175 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005176
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005177 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005178 &root->root_key, &root->root_item);
5179 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005180 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005181 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005182 }
5183 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005184 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005185 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5186 root->root_key.objectid);
5187 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005188 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005189 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005190 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005191 }
5192 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005193 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005194out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005195 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005196 mnt_drop_write_file(file);
5197 return ret;
5198}
5199
5200#ifdef CONFIG_64BIT
5201static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5202 void __user *arg)
5203{
5204 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5205 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5206 int ret = 0;
5207
5208 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305209 if (IS_ERR(args32))
5210 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005211
David Sterba8d2db782015-11-04 15:38:29 +01005212 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005213 if (!args64) {
5214 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005215 goto out;
5216 }
5217
5218 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5219 args64->stransid = args32->stransid;
5220 args64->rtransid = args32->rtransid;
5221 args64->stime.sec = args32->stime.sec;
5222 args64->stime.nsec = args32->stime.nsec;
5223 args64->rtime.sec = args32->rtime.sec;
5224 args64->rtime.nsec = args32->rtime.nsec;
5225 args64->flags = args32->flags;
5226
5227 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5228 if (ret)
5229 goto out;
5230
5231 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5232 args32->stransid = args64->stransid;
5233 args32->rtransid = args64->rtransid;
5234 args32->stime.sec = args64->stime.sec;
5235 args32->stime.nsec = args64->stime.nsec;
5236 args32->rtime.sec = args64->rtime.sec;
5237 args32->rtime.nsec = args64->rtime.nsec;
5238 args32->flags = args64->flags;
5239
5240 ret = copy_to_user(arg, args32, sizeof(*args32));
5241 if (ret)
5242 ret = -EFAULT;
5243
5244out:
5245 kfree(args32);
5246 kfree(args64);
5247 return ret;
5248}
5249#endif
5250
5251static long btrfs_ioctl_set_received_subvol(struct file *file,
5252 void __user *arg)
5253{
5254 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5255 int ret = 0;
5256
5257 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305258 if (IS_ERR(sa))
5259 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005260
5261 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5262
5263 if (ret)
5264 goto out;
5265
Alexander Block8ea05e32012-07-25 17:35:53 +02005266 ret = copy_to_user(arg, sa, sizeof(*sa));
5267 if (ret)
5268 ret = -EFAULT;
5269
5270out:
5271 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005272 return ret;
5273}
5274
jeff.liu867ab662013-01-05 02:48:01 +00005275static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5276{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005277 struct inode *inode = file_inode(file);
5278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005279 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005280 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005281 char label[BTRFS_LABEL_SIZE];
5282
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005283 spin_lock(&fs_info->super_lock);
5284 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5285 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005286
5287 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005288
5289 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005290 btrfs_warn(fs_info,
5291 "label is too long, return the first %zu bytes",
5292 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005293 }
5294
jeff.liu867ab662013-01-05 02:48:01 +00005295 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005296
5297 return ret ? -EFAULT : 0;
5298}
5299
jeff.liua8bfd4a2013-01-05 02:48:08 +00005300static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5301{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005302 struct inode *inode = file_inode(file);
5303 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5304 struct btrfs_root *root = BTRFS_I(inode)->root;
5305 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005306 struct btrfs_trans_handle *trans;
5307 char label[BTRFS_LABEL_SIZE];
5308 int ret;
5309
5310 if (!capable(CAP_SYS_ADMIN))
5311 return -EPERM;
5312
5313 if (copy_from_user(label, arg, sizeof(label)))
5314 return -EFAULT;
5315
5316 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005317 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005318 "unable to set label with more than %d bytes",
5319 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005320 return -EINVAL;
5321 }
5322
5323 ret = mnt_want_write_file(file);
5324 if (ret)
5325 return ret;
5326
jeff.liua8bfd4a2013-01-05 02:48:08 +00005327 trans = btrfs_start_transaction(root, 0);
5328 if (IS_ERR(trans)) {
5329 ret = PTR_ERR(trans);
5330 goto out_unlock;
5331 }
5332
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005333 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005334 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005335 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005336 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005337
5338out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005339 mnt_drop_write_file(file);
5340 return ret;
5341}
5342
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005343#define INIT_FEATURE_FLAGS(suffix) \
5344 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5345 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5346 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5347
David Sterbad5131b62016-02-17 15:26:27 +01005348int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005349{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005350 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005351 INIT_FEATURE_FLAGS(SUPP),
5352 INIT_FEATURE_FLAGS(SAFE_SET),
5353 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5354 };
5355
5356 if (copy_to_user(arg, &features, sizeof(features)))
5357 return -EFAULT;
5358
5359 return 0;
5360}
5361
5362static int btrfs_ioctl_get_features(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);
5366 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005367 struct btrfs_ioctl_feature_flags features;
5368
5369 features.compat_flags = btrfs_super_compat_flags(super_block);
5370 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5371 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5372
5373 if (copy_to_user(arg, &features, sizeof(features)))
5374 return -EFAULT;
5375
5376 return 0;
5377}
5378
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005379static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005380 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005381 u64 change_mask, u64 flags, u64 supported_flags,
5382 u64 safe_set, u64 safe_clear)
5383{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005384 const char *type = btrfs_feature_set_names[set];
5385 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005386 u64 disallowed, unsupported;
5387 u64 set_mask = flags & change_mask;
5388 u64 clear_mask = ~flags & change_mask;
5389
5390 unsupported = set_mask & ~supported_flags;
5391 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005392 names = btrfs_printable_features(set, unsupported);
5393 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005394 btrfs_warn(fs_info,
5395 "this kernel does not support the %s feature bit%s",
5396 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005397 kfree(names);
5398 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005399 btrfs_warn(fs_info,
5400 "this kernel does not support %s bits 0x%llx",
5401 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005402 return -EOPNOTSUPP;
5403 }
5404
5405 disallowed = set_mask & ~safe_set;
5406 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005407 names = btrfs_printable_features(set, disallowed);
5408 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 btrfs_warn(fs_info,
5410 "can't set the %s feature bit%s while mounted",
5411 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005412 kfree(names);
5413 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005414 btrfs_warn(fs_info,
5415 "can't set %s bits 0x%llx while mounted",
5416 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005417 return -EPERM;
5418 }
5419
5420 disallowed = clear_mask & ~safe_clear;
5421 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005422 names = btrfs_printable_features(set, disallowed);
5423 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005424 btrfs_warn(fs_info,
5425 "can't clear the %s feature bit%s while mounted",
5426 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005427 kfree(names);
5428 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005429 btrfs_warn(fs_info,
5430 "can't clear %s bits 0x%llx while mounted",
5431 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005432 return -EPERM;
5433 }
5434
5435 return 0;
5436}
5437
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005438#define check_feature(fs_info, change_mask, flags, mask_base) \
5439check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005440 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5441 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5442 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5443
5444static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5445{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005446 struct inode *inode = file_inode(file);
5447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5448 struct btrfs_root *root = BTRFS_I(inode)->root;
5449 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005450 struct btrfs_ioctl_feature_flags flags[2];
5451 struct btrfs_trans_handle *trans;
5452 u64 newflags;
5453 int ret;
5454
5455 if (!capable(CAP_SYS_ADMIN))
5456 return -EPERM;
5457
5458 if (copy_from_user(flags, arg, sizeof(flags)))
5459 return -EFAULT;
5460
5461 /* Nothing to do */
5462 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5463 !flags[0].incompat_flags)
5464 return 0;
5465
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005466 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005467 flags[1].compat_flags, COMPAT);
5468 if (ret)
5469 return ret;
5470
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005471 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005472 flags[1].compat_ro_flags, COMPAT_RO);
5473 if (ret)
5474 return ret;
5475
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005476 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005477 flags[1].incompat_flags, INCOMPAT);
5478 if (ret)
5479 return ret;
5480
David Sterba7ab19622016-05-04 11:32:00 +02005481 ret = mnt_want_write_file(file);
5482 if (ret)
5483 return ret;
5484
David Sterba8051aa12014-02-07 14:34:04 +01005485 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005486 if (IS_ERR(trans)) {
5487 ret = PTR_ERR(trans);
5488 goto out_drop_write;
5489 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005490
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005491 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005492 newflags = btrfs_super_compat_flags(super_block);
5493 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5494 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5495 btrfs_set_super_compat_flags(super_block, newflags);
5496
5497 newflags = btrfs_super_compat_ro_flags(super_block);
5498 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5499 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5500 btrfs_set_super_compat_ro_flags(super_block, newflags);
5501
5502 newflags = btrfs_super_incompat_flags(super_block);
5503 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5504 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5505 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005506 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005507
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005508 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005509out_drop_write:
5510 mnt_drop_write_file(file);
5511
5512 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005513}
5514
Josef Bacik2351f432017-09-27 10:43:13 -04005515static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5516{
5517 struct btrfs_ioctl_send_args *arg;
5518 int ret;
5519
5520 if (compat) {
5521#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5522 struct btrfs_ioctl_send_args_32 args32;
5523
5524 ret = copy_from_user(&args32, argp, sizeof(args32));
5525 if (ret)
5526 return -EFAULT;
5527 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5528 if (!arg)
5529 return -ENOMEM;
5530 arg->send_fd = args32.send_fd;
5531 arg->clone_sources_count = args32.clone_sources_count;
5532 arg->clone_sources = compat_ptr(args32.clone_sources);
5533 arg->parent_root = args32.parent_root;
5534 arg->flags = args32.flags;
5535 memcpy(arg->reserved, args32.reserved,
5536 sizeof(args32.reserved));
5537#else
5538 return -ENOTTY;
5539#endif
5540 } else {
5541 arg = memdup_user(argp, sizeof(*arg));
5542 if (IS_ERR(arg))
5543 return PTR_ERR(arg);
5544 }
5545 ret = btrfs_ioctl_send(file, arg);
5546 kfree(arg);
5547 return ret;
5548}
5549
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005550long btrfs_ioctl(struct file *file, unsigned int
5551 cmd, unsigned long arg)
5552{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005553 struct inode *inode = file_inode(file);
5554 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5555 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005556 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005557
5558 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005559 case FS_IOC_GETFLAGS:
5560 return btrfs_ioctl_getflags(file, argp);
5561 case FS_IOC_SETFLAGS:
5562 return btrfs_ioctl_setflags(file, argp);
5563 case FS_IOC_GETVERSION:
5564 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005565 case FITRIM:
5566 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005567 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005568 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005569 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005570 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005571 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005572 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005573 case BTRFS_IOC_SUBVOL_CREATE_V2:
5574 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005575 case BTRFS_IOC_SNAP_DESTROY:
5576 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005577 case BTRFS_IOC_SUBVOL_GETFLAGS:
5578 return btrfs_ioctl_subvol_getflags(file, argp);
5579 case BTRFS_IOC_SUBVOL_SETFLAGS:
5580 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005581 case BTRFS_IOC_DEFAULT_SUBVOL:
5582 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005583 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005584 return btrfs_ioctl_defrag(file, NULL);
5585 case BTRFS_IOC_DEFRAG_RANGE:
5586 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005587 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005588 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005589 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005590 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005591 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005592 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005593 case BTRFS_IOC_RM_DEV_V2:
5594 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005595 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005596 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005597 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005598 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005599 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005600 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005601 case BTRFS_IOC_TREE_SEARCH:
5602 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005603 case BTRFS_IOC_TREE_SEARCH_V2:
5604 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005605 case BTRFS_IOC_INO_LOOKUP:
5606 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005607 case BTRFS_IOC_INO_PATHS:
5608 return btrfs_ioctl_ino_to_path(root, argp);
5609 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005610 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5611 case BTRFS_IOC_LOGICAL_INO_V2:
5612 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005613 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005614 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005615 case BTRFS_IOC_SYNC: {
5616 int ret;
5617
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005618 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005619 if (ret)
5620 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005621 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005622 /*
5623 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005624 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005625 * processing uncleaned subvols.
5626 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005627 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005628 return ret;
5629 }
Sage Weil46204592010-10-29 15:41:32 -04005630 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005631 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005632 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005633 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005634 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005635 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005636 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005637 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005638 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005639 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005640 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005641 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005642 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005643 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005644 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005645 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005646 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5647 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005648#ifdef CONFIG_64BIT
5649 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5650 return btrfs_ioctl_set_received_subvol_32(file, argp);
5651#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005652 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005653 return _btrfs_ioctl_send(file, argp, false);
5654#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5655 case BTRFS_IOC_SEND_32:
5656 return _btrfs_ioctl_send(file, argp, true);
5657#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005658 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005659 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005660 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005661 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005662 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005663 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005664 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005665 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005666 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005667 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005668 case BTRFS_IOC_QUOTA_RESCAN:
5669 return btrfs_ioctl_quota_rescan(file, argp);
5670 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5671 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005672 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5673 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005674 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005675 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005676 case BTRFS_IOC_GET_FSLABEL:
5677 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005678 case BTRFS_IOC_SET_FSLABEL:
5679 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005680 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005681 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005682 case BTRFS_IOC_GET_FEATURES:
5683 return btrfs_ioctl_get_features(file, argp);
5684 case BTRFS_IOC_SET_FEATURES:
5685 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005686 case FS_IOC_FSGETXATTR:
5687 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005688 case FS_IOC_FSSETXATTR:
5689 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005690 case BTRFS_IOC_GET_SUBVOL_INFO:
5691 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005692 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5693 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005694 case BTRFS_IOC_INO_LOOKUP_USER:
5695 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005696 }
5697
5698 return -ENOTTY;
5699}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005700
5701#ifdef CONFIG_COMPAT
5702long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5703{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005704 /*
5705 * These all access 32-bit values anyway so no further
5706 * handling is necessary.
5707 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005708 switch (cmd) {
5709 case FS_IOC32_GETFLAGS:
5710 cmd = FS_IOC_GETFLAGS;
5711 break;
5712 case FS_IOC32_SETFLAGS:
5713 cmd = FS_IOC_SETFLAGS;
5714 break;
5715 case FS_IOC32_GETVERSION:
5716 cmd = FS_IOC_GETVERSION;
5717 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005718 }
5719
5720 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5721}
5722#endif