blob: 054fa20ef193435abec99583194ce99eb030a1b2 [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>
8#include <linux/buffer_head.h>
9#include <linux/file.h>
10#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040011#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040012#include <linux/pagemap.h>
13#include <linux/highmem.h>
14#include <linux/time.h>
15#include <linux/init.h>
16#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040017#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040018#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/swap.h>
22#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040023#include <linux/compat.h>
24#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040025#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040026#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020027#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000029#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020030#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000031#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070032#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050033#include <linux/iversion.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040038#include "print-tree.h"
39#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040040#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080041#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020042#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040043#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020044#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010045#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000046#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040047#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070048#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000049#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080050#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051
Hugo Millsabccd002014-01-30 20:17:00 +000052#ifdef CONFIG_64BIT
53/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
54 * structures are incorrect, as the timespec structure from userspace
55 * is 4 bytes too small. We define these alternatives here to teach
56 * the kernel about the 32-bit struct packing.
57 */
58struct btrfs_ioctl_timespec_32 {
59 __u64 sec;
60 __u32 nsec;
61} __attribute__ ((__packed__));
62
63struct btrfs_ioctl_received_subvol_args_32 {
64 char uuid[BTRFS_UUID_SIZE]; /* in */
65 __u64 stransid; /* in */
66 __u64 rtransid; /* out */
67 struct btrfs_ioctl_timespec_32 stime; /* in */
68 struct btrfs_ioctl_timespec_32 rtime; /* out */
69 __u64 flags; /* in */
70 __u64 reserved[16]; /* in */
71} __attribute__ ((__packed__));
72
73#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
74 struct btrfs_ioctl_received_subvol_args_32)
75#endif
76
Josef Bacik2351f432017-09-27 10:43:13 -040077#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
78struct btrfs_ioctl_send_args_32 {
79 __s64 send_fd; /* in */
80 __u64 clone_sources_count; /* in */
81 compat_uptr_t clone_sources; /* in */
82 __u64 parent_root; /* in */
83 __u64 flags; /* in */
84 __u64 reserved[4]; /* in */
85} __attribute__ ((__packed__));
86
87#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
88 struct btrfs_ioctl_send_args_32)
89#endif
Hugo Millsabccd002014-01-30 20:17:00 +000090
Mark Fasheh416161d2013-08-06 11:42:51 -070091static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070092 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
93 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070094
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020096static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
97 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020098{
David Sterba1905a0f2018-03-26 18:52:15 +020099 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200100 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +0200101 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102 return flags & ~FS_DIRSYNC_FL;
103 else
104 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
105}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400106
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200108 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
109 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200110 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200111static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200112{
113 unsigned int iflags = 0;
114
115 if (flags & BTRFS_INODE_SYNC)
116 iflags |= FS_SYNC_FL;
117 if (flags & BTRFS_INODE_IMMUTABLE)
118 iflags |= FS_IMMUTABLE_FL;
119 if (flags & BTRFS_INODE_APPEND)
120 iflags |= FS_APPEND_FL;
121 if (flags & BTRFS_INODE_NODUMP)
122 iflags |= FS_NODUMP_FL;
123 if (flags & BTRFS_INODE_NOATIME)
124 iflags |= FS_NOATIME_FL;
125 if (flags & BTRFS_INODE_DIRSYNC)
126 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000127 if (flags & BTRFS_INODE_NODATACOW)
128 iflags |= FS_NOCOW_FL;
129
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900130 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000131 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900132 else if (flags & BTRFS_INODE_COMPRESS)
133 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200134
135 return iflags;
136}
137
138/*
139 * Update inode->i_flags based on the btrfs internal flags.
140 */
David Sterba7b6a2212018-03-26 18:40:21 +0200141void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142{
143 struct btrfs_inode *ip = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145
146 if (ip->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100147 new_fl |= S_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200148 if (ip->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100149 new_fl |= S_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200150 if (ip->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100151 new_fl |= S_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200152 if (ip->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100153 new_fl |= S_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200154 if (ip->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100155 new_fl |= S_DIRSYNC;
156
157 set_mask_bits(&inode->i_flags,
158 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
159 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160}
161
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200162static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
163{
Al Viro496ad9a2013-01-23 17:07:38 -0500164 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
David Sterbaa157d4f2018-03-26 19:12:25 +0200165 unsigned int flags = btrfs_inode_flags_to_fsflags(ip->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200166
167 if (copy_to_user(arg, &flags, sizeof(flags)))
168 return -EFAULT;
169 return 0;
170}
171
David Sterba5ba76ab2018-03-26 18:52:15 +0200172/* Check if @flags are a supported and valid set of FS_*_FL flags */
173static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000174{
175 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
176 FS_NOATIME_FL | FS_NODUMP_FL | \
177 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000178 FS_NOCOMP_FL | FS_COMPR_FL |
179 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return -EOPNOTSUPP;
181
182 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
183 return -EINVAL;
184
Liu Bo75e7cb72011-03-22 10:12:20 +0000185 return 0;
186}
187
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200188static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
189{
Al Viro496ad9a2013-01-23 17:07:38 -0500190 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200192 struct btrfs_inode *ip = BTRFS_I(inode);
193 struct btrfs_root *root = ip->root;
194 struct btrfs_trans_handle *trans;
195 unsigned int flags, oldflags;
196 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800197 u64 ip_oldflags;
198 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600199 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200
David Sterbabd60ea02014-01-16 15:50:22 +0100201 if (!inode_owner_or_capable(inode))
202 return -EPERM;
203
Li Zefanb83cc962010-12-20 16:04:08 +0800204 if (btrfs_root_readonly(root))
205 return -EROFS;
206
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200207 if (copy_from_user(&flags, arg, sizeof(flags)))
208 return -EFAULT;
209
David Sterba5ba76ab2018-03-26 18:52:15 +0200210 ret = check_fsflags(flags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000211 if (ret)
212 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200213
Jan Karae7848682012-06-12 16:20:32 +0200214 ret = mnt_want_write_file(file);
215 if (ret)
216 return ret;
217
Al Viro59551022016-01-22 15:40:57 -0500218 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200219
Li Zefanf062abf02011-12-29 13:36:45 +0800220 ip_oldflags = ip->flags;
221 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600222 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800223
David Sterba1905a0f2018-03-26 18:52:15 +0200224 flags = btrfs_mask_fsflags_for_type(inode, flags);
David Sterbaa157d4f2018-03-26 19:12:25 +0200225 oldflags = btrfs_inode_flags_to_fsflags(ip->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
227 if (!capable(CAP_LINUX_IMMUTABLE)) {
228 ret = -EPERM;
229 goto out_unlock;
230 }
231 }
232
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200233 if (flags & FS_SYNC_FL)
234 ip->flags |= BTRFS_INODE_SYNC;
235 else
236 ip->flags &= ~BTRFS_INODE_SYNC;
237 if (flags & FS_IMMUTABLE_FL)
238 ip->flags |= BTRFS_INODE_IMMUTABLE;
239 else
240 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
241 if (flags & FS_APPEND_FL)
242 ip->flags |= BTRFS_INODE_APPEND;
243 else
244 ip->flags &= ~BTRFS_INODE_APPEND;
245 if (flags & FS_NODUMP_FL)
246 ip->flags |= BTRFS_INODE_NODUMP;
247 else
248 ip->flags &= ~BTRFS_INODE_NODUMP;
249 if (flags & FS_NOATIME_FL)
250 ip->flags |= BTRFS_INODE_NOATIME;
251 else
252 ip->flags &= ~BTRFS_INODE_NOATIME;
253 if (flags & FS_DIRSYNC_FL)
254 ip->flags |= BTRFS_INODE_DIRSYNC;
255 else
256 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600257 if (flags & FS_NOCOW_FL) {
258 if (S_ISREG(mode)) {
259 /*
260 * It's safe to turn csums off here, no extents exist.
261 * Otherwise we want the flag to reflect the real COW
262 * status of the file and will not set it.
263 */
264 if (inode->i_size == 0)
265 ip->flags |= BTRFS_INODE_NODATACOW
266 | BTRFS_INODE_NODATASUM;
267 } else {
268 ip->flags |= BTRFS_INODE_NODATACOW;
269 }
270 } else {
271 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400272 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600273 */
274 if (S_ISREG(mode)) {
275 if (inode->i_size == 0)
276 ip->flags &= ~(BTRFS_INODE_NODATACOW
277 | BTRFS_INODE_NODATASUM);
278 } else {
279 ip->flags &= ~BTRFS_INODE_NODATACOW;
280 }
281 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200282
Liu Bo75e7cb72011-03-22 10:12:20 +0000283 /*
284 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
285 * flag may be changed automatically if compression code won't make
286 * things smaller.
287 */
288 if (flags & FS_NOCOMP_FL) {
289 ip->flags &= ~BTRFS_INODE_COMPRESS;
290 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000291
292 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
293 if (ret && ret != -ENODATA)
294 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000295 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000296 const char *comp;
297
Liu Bo75e7cb72011-03-22 10:12:20 +0000298 ip->flags |= BTRFS_INODE_COMPRESS;
299 ip->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;
Li Zefanebcb9042011-04-15 03:03:17 +0000314 ip->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) {
331 ip->flags = ip_oldflags;
332 inode->i_flags = i_oldflags;
333 }
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
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200373static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
374{
Al Viro496ad9a2013-01-23 17:07:38 -0500375 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200376
377 return put_user(inode->i_generation, arg);
378}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400379
Li Dongyangf7039b12011-03-24 10:24:28 +0000380static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
381{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400382 struct inode *inode = file_inode(file);
383 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000384 struct btrfs_device *device;
385 struct request_queue *q;
386 struct fstrim_range range;
387 u64 minlen = ULLONG_MAX;
388 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500389 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000390 int ret;
391
392 if (!capable(CAP_SYS_ADMIN))
393 return -EPERM;
394
Xiao Guangrong1f781602011-04-20 10:09:16 +0000395 rcu_read_lock();
396 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
397 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000398 if (!device->bdev)
399 continue;
400 q = bdev_get_queue(device->bdev);
401 if (blk_queue_discard(q)) {
402 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100403 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000404 minlen);
405 }
406 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000407 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200408
Li Dongyangf7039b12011-03-24 10:24:28 +0000409 if (!num_devices)
410 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000411 if (copy_from_user(&range, arg, sizeof(range)))
412 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000413 if (range.start > total_bytes ||
414 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200415 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000416
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200417 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000418 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400419 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000420 if (ret < 0)
421 return ret;
422
423 if (copy_to_user(arg, &range, sizeof(range)))
424 return -EFAULT;
425
426 return 0;
427}
428
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200429int btrfs_is_empty_uuid(u8 *uuid)
430{
Chris Mason46e0f662013-11-15 12:14:55 +0100431 int i;
432
433 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
434 if (uuid[i])
435 return 0;
436 }
437 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200438}
439
Miao Xied5c12072013-02-28 10:04:33 +0000440static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400441 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100442 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200443 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000444 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400446 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400447 struct btrfs_trans_handle *trans;
448 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100449 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400450 struct btrfs_inode_item *inode_item;
451 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000452 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400453 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000454 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700455 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900456 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457 int ret;
458 int err;
459 u64 objectid;
460 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500461 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000462 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200463 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464
David Sterba49a3c4d2016-03-24 17:49:22 +0100465 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
466 if (!root_item)
467 return -ENOMEM;
468
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400469 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400470 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100471 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000472
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800473 /*
474 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400475 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800476 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100477 if (btrfs_qgroup_level(objectid)) {
478 ret = -ENOSPC;
479 goto fail_free;
480 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800481
Miao Xied5c12072013-02-28 10:04:33 +0000482 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400483 /*
Miao Xied5c12072013-02-28 10:04:33 +0000484 * The same as the snapshot creation, please see the comment
485 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400486 */
Miao Xied5c12072013-02-28 10:04:33 +0000487 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200488 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000489 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100490 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491
Miao Xied5c12072013-02-28 10:04:33 +0000492 trans = btrfs_start_transaction(root, 0);
493 if (IS_ERR(trans)) {
494 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100495 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100496 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000497 }
498 trans->block_rsv = &block_rsv;
499 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400500
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400501 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200502 if (ret)
503 goto fail;
504
David Sterba4d75f8a2014-06-15 01:54:12 +0200505 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400506 if (IS_ERR(leaf)) {
507 ret = PTR_ERR(leaf);
508 goto fail;
509 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400510
David Sterbab159fa22016-11-08 18:09:03 +0100511 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400512 btrfs_set_header_bytenr(leaf, leaf->start);
513 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400514 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515 btrfs_set_header_owner(leaf, objectid);
516
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400517 write_extent_buffer_fsid(leaf, fs_info->fsid);
518 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519 btrfs_mark_buffer_dirty(leaf);
520
David Sterba49a3c4d2016-03-24 17:49:22 +0100521 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800522 btrfs_set_stack_inode_generation(inode_item, 1);
523 btrfs_set_stack_inode_size(inode_item, 3);
524 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400525 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400526 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800527 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400528
David Sterba49a3c4d2016-03-24 17:49:22 +0100529 btrfs_set_root_flags(root_item, 0);
530 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800531 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000532
David Sterba49a3c4d2016-03-24 17:49:22 +0100533 btrfs_set_root_bytenr(root_item, leaf->start);
534 btrfs_set_root_generation(root_item, trans->transid);
535 btrfs_set_root_level(root_item, 0);
536 btrfs_set_root_refs(root_item, 1);
537 btrfs_set_root_used(root_item, leaf->len);
538 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539
David Sterba49a3c4d2016-03-24 17:49:22 +0100540 btrfs_set_root_generation_v2(root_item,
541 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200542 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100543 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
544 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
545 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
546 root_item->ctime = root_item->otime;
547 btrfs_set_root_ctransid(root_item, trans->transid);
548 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400549
Chris Mason925baed2008-06-25 16:01:30 -0400550 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400551 free_extent_buffer(leaf);
552 leaf = NULL;
553
David Sterba49a3c4d2016-03-24 17:49:22 +0100554 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555
556 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400557 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200558 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400559 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100560 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400561 if (ret)
562 goto fail;
563
Yan, Zheng76dda932009-09-21 16:00:26 -0400564 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400565 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100566 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100567 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400568 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100569 goto fail;
570 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400571
572 btrfs_record_root_in_trans(trans, new_root);
573
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000574 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700575 if (ret) {
576 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400577 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700578 goto fail;
579 }
580
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530581 mutex_lock(&new_root->objectid_mutex);
582 new_root->highest_objectid = new_dirid;
583 mutex_unlock(&new_root->objectid_mutex);
584
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400585 /*
586 * insert the directory item
587 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200588 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100589 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400590 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100591 goto fail;
592 }
Chris Mason3de45862008-11-17 21:02:50 -0500593
594 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200595 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500596 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100597 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400598 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400599 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100600 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500601
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200602 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500603 ret = btrfs_update_inode(trans, root, dir);
604 BUG_ON(ret);
605
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400606 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400607 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200608 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500609 BUG_ON(ret);
610
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400611 ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400612 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200613 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400614 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200615
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400616fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100617 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000618 trans->block_rsv = NULL;
619 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100620 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800621
Sage Weil72fd0322010-10-29 15:41:32 -0400622 if (async_transid) {
623 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400624 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000625 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400626 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400627 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400628 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400629 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630 if (err && !ret)
631 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500632
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900633 if (!ret) {
634 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800635 if (IS_ERR(inode))
636 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900637 d_instantiate(dentry, inode);
638 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400639 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100640
641fail_free:
642 kfree(root_item);
643 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400644}
645
Miao Xiee9662f72013-02-28 10:01:15 +0000646static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100647 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000648 u64 *async_transid, bool readonly,
649 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400651 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000652 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400653 struct btrfs_pending_snapshot *pending_snapshot;
654 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000655 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656
Miao Xie27cdeb72014-04-02 19:51:05 +0800657 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400658 return -EINVAL;
659
David Sterba23269bf2017-02-13 11:03:44 +0100660 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100661 if (!pending_snapshot)
662 return -ENOMEM;
663
David Sterbab0c0ea62015-11-10 18:54:00 +0100664 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100665 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100666 pending_snapshot->path = btrfs_alloc_path();
667 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100668 ret = -ENOMEM;
669 goto free_pending;
670 }
671
David Sterbaea14b57f2017-06-22 02:19:11 +0200672 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100673 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600674 /* wait for no snapshot writes */
675 wait_event(root->subv_writers->wait,
676 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800677
Nikolay Borisov76f32e22018-04-23 10:54:14 +0300678 ret = btrfs_start_delalloc_inodes(root);
Miao Xie6a038432013-05-15 07:48:24 +0000679 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100680 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000681
Chris Mason6374e57a2017-06-23 09:48:21 -0700682 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000683
Miao Xie66d8f3d2012-09-06 04:02:28 -0600684 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
685 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000686 /*
687 * 1 - parent dir inode
688 * 2 - dir entries
689 * 1 - root item
690 * 2 - root ref/backref
691 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200692 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000693 */
694 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200695 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400696 &pending_snapshot->qgroup_reserved,
697 false);
Miao Xied5c12072013-02-28 10:04:33 +0000698 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100699 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000700
Yan, Zhenga22285a2010-05-16 10:48:46 -0400701 pending_snapshot->dentry = dentry;
702 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800703 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000704 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000705 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400706
Miao Xied5c12072013-02-28 10:04:33 +0000707 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400708 if (IS_ERR(trans)) {
709 ret = PTR_ERR(trans);
710 goto fail;
711 }
712
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400713 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400714 list_add(&pending_snapshot->list,
715 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400716 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400717 if (async_transid) {
718 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400719 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000720 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400721 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400722 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400723 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400724 }
Miao Xieaec80302013-03-04 09:44:29 +0000725 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400726 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400727
728 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400729 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000730 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400731
Chris Masond3797302014-10-15 13:50:56 -0700732 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
733 if (ret)
734 goto fail;
735
David Howells2b0143b2015-03-17 22:25:59 +0000736 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000737 if (IS_ERR(inode)) {
738 ret = PTR_ERR(inode);
739 goto fail;
740 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900741
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000742 d_instantiate(dentry, inode);
743 ret = 0;
744fail:
David Sterba7775c812017-02-10 19:18:18 +0100745 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100746dec_and_free:
David Sterbaea14b57f2017-06-22 02:19:11 +0200747 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100748 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100749free_pending:
750 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100751 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100752 kfree(pending_snapshot);
753
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400754 return ret;
755}
756
Sage Weil4260f7c2010-10-29 15:46:43 -0400757/* copy of may_delete in fs/namei.c()
758 * Check whether we can remove a link victim from directory dir, check
759 * whether the type of victim is right.
760 * 1. We can't do it if dir is read-only (done in permission())
761 * 2. We should have write and exec permissions on dir
762 * 3. We can't remove anything from append-only dir
763 * 4. We can't do anything with immutable dir (done in permission())
764 * 5. If the sticky bit on dir is set we should either
765 * a. be owner of dir, or
766 * b. be owner of victim, or
767 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400768 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400769 * links pointing to it.
770 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
771 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
772 * 9. We can't remove a root or mountpoint.
773 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
774 * nfs_async_unlink().
775 */
776
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530777static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400778{
779 int error;
780
David Howells2b0143b2015-03-17 22:25:59 +0000781 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400782 return -ENOENT;
783
David Howells2b0143b2015-03-17 22:25:59 +0000784 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400785 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400786
787 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
788 if (error)
789 return error;
790 if (IS_APPEND(dir))
791 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000792 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
793 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400794 return -EPERM;
795 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000796 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400797 return -ENOTDIR;
798 if (IS_ROOT(victim))
799 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000800 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400801 return -EISDIR;
802 if (IS_DEADDIR(dir))
803 return -ENOENT;
804 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
805 return -EBUSY;
806 return 0;
807}
808
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400809/* copy of may_create in fs/namei.c() */
810static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
811{
David Howells2b0143b2015-03-17 22:25:59 +0000812 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400813 return -EEXIST;
814 if (IS_DEADDIR(dir))
815 return -ENOENT;
816 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
817}
818
819/*
820 * Create a new subvolume below @parent. This is largely modeled after
821 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
822 * inside this filesystem so it's quite a bit simpler.
823 */
Al Viro92872092016-11-20 19:34:31 -0500824static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100825 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400826 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200827 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000828 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400829{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400830 struct inode *dir = d_inode(parent->dentry);
831 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400832 struct dentry *dentry;
833 int error;
834
Al Viro00235412016-05-26 00:05:12 -0400835 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
836 if (error == -EINTR)
837 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400838
839 dentry = lookup_one_len(name, parent->dentry, namelen);
840 error = PTR_ERR(dentry);
841 if (IS_ERR(dentry))
842 goto out_unlock;
843
Yan, Zheng76dda932009-09-21 16:00:26 -0400844 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400845 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600846 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400847
Chris Mason9c520572012-12-17 14:26:57 -0500848 /*
849 * even if this name doesn't exist, we may get hash collisions.
850 * check for them now when we can safely fail
851 */
852 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
853 dir->i_ino, name,
854 namelen);
855 if (error)
856 goto out_dput;
857
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400858 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400859
860 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
861 goto out_up_read;
862
Chris Mason3de45862008-11-17 21:02:50 -0500863 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100864 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200865 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500866 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000867 error = create_subvol(dir, dentry, name, namelen,
868 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500869 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400870 if (!error)
871 fsnotify_mkdir(dir, dentry);
872out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400873 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400874out_dput:
875 dput(dentry);
876out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500877 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400878 return error;
879}
880
Chris Mason4cb53002011-05-24 15:35:30 -0400881/*
882 * When we're defragging a range, we don't want to kick it off again
883 * if it is really just waiting for delalloc to send it down.
884 * If we find a nice big extent or delalloc range for the bytes in the
885 * file you want to defrag, we return 0 to let you know to skip this
886 * part of the file
887 */
David Sterbaaab110a2014-07-29 17:32:10 +0200888static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400889{
890 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
891 struct extent_map *em = NULL;
892 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
893 u64 end;
894
895 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300896 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -0400897 read_unlock(&em_tree->lock);
898
899 if (em) {
900 end = extent_map_end(em);
901 free_extent_map(em);
902 if (end - offset > thresh)
903 return 0;
904 }
905 /* if we already have a nice delalloc here, just stop */
906 thresh /= 2;
907 end = count_range_bits(io_tree, &offset, offset + thresh,
908 thresh, EXTENT_DELALLOC, 1);
909 if (end >= thresh)
910 return 0;
911 return 1;
912}
913
914/*
915 * helper function to walk through a file and find extents
916 * newer than a specific transid, and smaller than thresh.
917 *
918 * This is used by the defragging code to find new and small
919 * extents
920 */
921static int find_new_extents(struct btrfs_root *root,
922 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200923 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400924{
925 struct btrfs_path *path;
926 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400927 struct extent_buffer *leaf;
928 struct btrfs_file_extent_item *extent;
929 int type;
930 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200931 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -0400932
933 path = btrfs_alloc_path();
934 if (!path)
935 return -ENOMEM;
936
David Sterbaa4689d22011-05-31 17:08:14 +0000937 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400938 min_key.type = BTRFS_EXTENT_DATA_KEY;
939 min_key.offset = *off;
940
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530941 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100942 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400943 if (ret != 0)
944 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000945process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000946 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400947 goto none;
948 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
949 goto none;
950
951 leaf = path->nodes[0];
952 extent = btrfs_item_ptr(leaf, path->slots[0],
953 struct btrfs_file_extent_item);
954
955 type = btrfs_file_extent_type(leaf, extent);
956 if (type == BTRFS_FILE_EXTENT_REG &&
957 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
958 check_defrag_in_cache(inode, min_key.offset, thresh)) {
959 *off = min_key.offset;
960 btrfs_free_path(path);
961 return 0;
962 }
963
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000964 path->slots[0]++;
965 if (path->slots[0] < btrfs_header_nritems(leaf)) {
966 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
967 goto process_slot;
968 }
969
Chris Mason4cb53002011-05-24 15:35:30 -0400970 if (min_key.offset == (u64)-1)
971 goto none;
972
973 min_key.offset++;
974 btrfs_release_path(path);
975 }
976none:
977 btrfs_free_path(path);
978 return -ENOENT;
979}
980
Li Zefan6c282eb2012-06-11 16:03:35 +0800981static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400982{
983 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500984 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800985 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300986 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500987
988 /*
989 * hopefully we have this extent in the tree already, try without
990 * the full extent lock
991 */
992 read_lock(&em_tree->lock);
993 em = lookup_extent_mapping(em_tree, start, len);
994 read_unlock(&em_tree->lock);
995
996 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +0000997 struct extent_state *cached = NULL;
998 u64 end = start + len - 1;
999
Chris Mason940100a2010-03-10 10:52:59 -05001000 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001001 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001002 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001003 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001004
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001005 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001006 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001007 }
1008
Li Zefan6c282eb2012-06-11 16:03:35 +08001009 return em;
1010}
1011
1012static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1013{
1014 struct extent_map *next;
1015 bool ret = true;
1016
1017 /* this is the last extent */
1018 if (em->start + em->len >= i_size_read(inode))
1019 return false;
1020
1021 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001022 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1023 ret = false;
1024 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001025 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001026 ret = false;
1027
1028 free_extent_map(next);
1029 return ret;
1030}
1031
David Sterbaaab110a2014-07-29 17:32:10 +02001032static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001033 u64 *last_len, u64 *skip, u64 *defrag_end,
1034 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001035{
1036 struct extent_map *em;
1037 int ret = 1;
1038 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001039 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001040
1041 /*
1042 * make sure that once we start defragging an extent, we keep on
1043 * defragging it
1044 */
1045 if (start < *defrag_end)
1046 return 1;
1047
1048 *skip = 0;
1049
1050 em = defrag_lookup_extent(inode, start);
1051 if (!em)
1052 return 0;
1053
Chris Mason940100a2010-03-10 10:52:59 -05001054 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001055 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001056 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001057 goto out;
1058 }
1059
Liu Bo4a3560c2015-08-07 16:48:41 +08001060 if (!*defrag_end)
1061 prev_mergeable = false;
1062
Li Zefan6c282eb2012-06-11 16:03:35 +08001063 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001064 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001065 * we hit a real extent, if it is big or the next extent is not a
1066 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001067 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001068 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001069 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001070 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001071out:
Chris Mason940100a2010-03-10 10:52:59 -05001072 /*
1073 * last_len ends up being a counter of how many bytes we've defragged.
1074 * every time we choose not to defrag an extent, we reset *last_len
1075 * so that the next tiny extent will force a defrag.
1076 *
1077 * The end result of this is that tiny extents before a single big
1078 * extent will force at least part of that big extent to be defragged.
1079 */
1080 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001081 *defrag_end = extent_map_end(em);
1082 } else {
1083 *last_len = 0;
1084 *skip = extent_map_end(em);
1085 *defrag_end = 0;
1086 }
1087
1088 free_extent_map(em);
1089 return ret;
1090}
1091
Chris Mason4cb53002011-05-24 15:35:30 -04001092/*
1093 * it doesn't do much good to defrag one or two pages
1094 * at a time. This pulls in a nice chunk of pages
1095 * to COW and defrag.
1096 *
1097 * It also makes sure the delalloc code has enough
1098 * dirty data to avoid making new small extents as part
1099 * of the defrag
1100 *
1101 * It's a good idea to start RA on this range
1102 * before calling this.
1103 */
1104static int cluster_pages_for_defrag(struct inode *inode,
1105 struct page **pages,
1106 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001107 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001108{
Chris Mason4cb53002011-05-24 15:35:30 -04001109 unsigned long file_end;
1110 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001111 u64 page_start;
1112 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001113 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001114 int ret;
1115 int i;
1116 int i_done;
1117 struct btrfs_ordered_extent *ordered;
1118 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001119 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001120 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001121 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001122
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001123 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001124 if (!isize || start_index > file_end)
1125 return 0;
1126
1127 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001128
Qu Wenruo364ecf32017-02-27 15:10:38 +08001129 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001130 start_index << PAGE_SHIFT,
1131 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001132 if (ret)
1133 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001134 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001135 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001136
1137 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001138 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001139 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001140again:
Josef Bacika94733d2011-07-11 10:47:06 -04001141 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001142 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001143 if (!page)
1144 break;
1145
Miao Xie600a45e2012-02-16 15:01:24 +08001146 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001147 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001148 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001149 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001150 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001151 ordered = btrfs_lookup_ordered_extent(inode,
1152 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001153 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001154 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001155 if (!ordered)
1156 break;
1157
1158 unlock_page(page);
1159 btrfs_start_ordered_extent(inode, ordered, 1);
1160 btrfs_put_ordered_extent(ordered);
1161 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001162 /*
1163 * we unlocked the page above, so we need check if
1164 * it was released or not.
1165 */
1166 if (page->mapping != inode->i_mapping) {
1167 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001168 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001169 goto again;
1170 }
Miao Xie600a45e2012-02-16 15:01:24 +08001171 }
1172
Chris Mason4cb53002011-05-24 15:35:30 -04001173 if (!PageUptodate(page)) {
1174 btrfs_readpage(NULL, page);
1175 lock_page(page);
1176 if (!PageUptodate(page)) {
1177 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001178 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001179 ret = -EIO;
1180 break;
1181 }
1182 }
Miao Xie600a45e2012-02-16 15:01:24 +08001183
Miao Xie600a45e2012-02-16 15:01:24 +08001184 if (page->mapping != inode->i_mapping) {
1185 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001186 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001187 goto again;
1188 }
1189
Chris Mason4cb53002011-05-24 15:35:30 -04001190 pages[i] = page;
1191 i_done++;
1192 }
1193 if (!i_done || ret)
1194 goto out;
1195
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001196 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001197 goto out;
1198
1199 /*
1200 * so now we have a nice long stream of locked
1201 * and up to date pages, lets wait on them
1202 */
1203 for (i = 0; i < i_done; i++)
1204 wait_on_page_writeback(pages[i]);
1205
1206 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001207 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001208
1209 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001210 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001211 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1212 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001213 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001214 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001215
Liu Bo1f12bd02012-03-29 09:57:44 -04001216 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001217 spin_lock(&BTRFS_I(inode)->lock);
1218 BTRFS_I(inode)->outstanding_extents++;
1219 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001220 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001221 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001222 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001223 }
1224
1225
Liu Bo9e8a4a82012-09-05 19:10:51 -06001226 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001227 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001228
1229 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001230 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001231
1232 for (i = 0; i < i_done; i++) {
1233 clear_page_dirty_for_io(pages[i]);
1234 ClearPageChecked(pages[i]);
1235 set_page_extent_mapped(pages[i]);
1236 set_page_dirty(pages[i]);
1237 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001238 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001239 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001240 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1241 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001242 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001243 return i_done;
1244out:
1245 for (i = 0; i < i_done; i++) {
1246 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001247 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001248 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001249 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001250 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001251 page_cnt << PAGE_SHIFT, true);
1252 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1253 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001254 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001255 return ret;
1256
1257}
1258
1259int btrfs_defrag_file(struct inode *inode, struct file *file,
1260 struct btrfs_ioctl_defrag_range_args *range,
1261 u64 newer_than, unsigned long max_to_defrag)
1262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001264 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001265 struct file_ra_state *ra = NULL;
1266 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001267 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001268 u64 last_len = 0;
1269 u64 skip = 0;
1270 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001271 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001272 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001273 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001274 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001275 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001276 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001277 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001278 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001279 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001280 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001281 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001282 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001283
Liu Bo0abd5b12013-04-16 09:20:28 +00001284 if (isize == 0)
1285 return 0;
1286
1287 if (range->start >= isize)
1288 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001289
David Sterba1e2ef462017-07-17 20:01:59 +02001290 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001291 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1292 return -EINVAL;
1293 if (range->compress_type)
1294 compress_type = range->compress_type;
1295 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001296
Liu Bo0abd5b12013-04-16 09:20:28 +00001297 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001298 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001299
Chris Mason4cb53002011-05-24 15:35:30 -04001300 /*
David Sterba0a52d102017-06-22 03:22:58 +02001301 * If we were not given a file, allocate a readahead context. As
1302 * readahead is just an optimization, defrag will work without it so
1303 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001304 */
1305 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001306 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001307 if (ra)
1308 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001309 } else {
1310 ra = &file->f_ra;
1311 }
1312
David Sterba63e727e2017-06-22 03:13:02 +02001313 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001314 if (!pages) {
1315 ret = -ENOMEM;
1316 goto out_ra;
1317 }
1318
1319 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001320 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001321 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001322 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001323 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001324 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001325 }
1326
Chris Mason4cb53002011-05-24 15:35:30 -04001327 if (newer_than) {
1328 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001329 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001330 if (!ret) {
1331 range->start = newer_off;
1332 /*
1333 * we always align our defrag to help keep
1334 * the extents in the file evenly spaced
1335 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001336 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001337 } else
1338 goto out_ra;
1339 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001340 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001341 }
1342 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301343 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001344
Li Zefan2a0f7f52011-10-10 15:43:34 -04001345 /*
1346 * make writeback starts from i, so the defrag range can be
1347 * written sequentially.
1348 */
1349 if (i < inode->i_mapping->writeback_index)
1350 inode->i_mapping->writeback_index = i;
1351
Chris Masonf7f43cc2011-10-11 11:41:40 -04001352 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001353 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001354 /*
1355 * make sure we stop running if someone unmounts
1356 * the FS
1357 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001358 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001359 break;
1360
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001361 if (btrfs_defrag_cancelled(fs_info)) {
1362 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001363 ret = -EAGAIN;
1364 break;
1365 }
1366
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001367 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001368 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001369 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001370 unsigned long next;
1371 /*
1372 * the should_defrag function tells us how much to skip
1373 * bump our counter by the suggested amount
1374 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001375 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001376 i = max(i + 1, next);
1377 continue;
1378 }
Li Zefan008873e2011-09-02 15:57:07 +08001379
1380 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001381 cluster = (PAGE_ALIGN(defrag_end) >>
1382 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001383 cluster = min(cluster, max_cluster);
1384 } else {
1385 cluster = max_cluster;
1386 }
1387
Li Zefan008873e2011-09-02 15:57:07 +08001388 if (i + cluster > ra_index) {
1389 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001390 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001391 page_cache_sync_readahead(inode->i_mapping, ra,
1392 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301393 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001394 }
Chris Mason940100a2010-03-10 10:52:59 -05001395
Al Viro59551022016-01-22 15:40:57 -05001396 inode_lock(inode);
David Sterba1e2ef462017-07-17 20:01:59 +02001397 if (do_compress)
David Sterbaeec63c62017-07-17 19:41:31 +02001398 BTRFS_I(inode)->defrag_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001399 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001400 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001401 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001402 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001403 }
Chris Mason940100a2010-03-10 10:52:59 -05001404
Chris Mason4cb53002011-05-24 15:35:30 -04001405 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001406 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001407 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001408
1409 if (newer_than) {
1410 if (newer_off == (u64)-1)
1411 break;
1412
Liu Boe1f041e2012-03-29 09:57:45 -04001413 if (ret > 0)
1414 i += ret;
1415
Chris Mason4cb53002011-05-24 15:35:30 -04001416 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001417 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001418
Byongho Leeee221842015-12-15 01:42:10 +09001419 ret = find_new_extents(root, inode, newer_than,
1420 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001421 if (!ret) {
1422 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001423 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001424 } else {
1425 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001426 }
Chris Mason4cb53002011-05-24 15:35:30 -04001427 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001428 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001429 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001430 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001431 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001432 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001433 last_len = 0;
1434 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001435 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001436 }
1437
Filipe Mananadec8ef92014-03-01 10:55:54 +00001438 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001439 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001440 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1441 &BTRFS_I(inode)->runtime_flags))
1442 filemap_flush(inode->i_mapping);
1443 }
Chris Mason1e701a32010-03-11 09:42:04 -05001444
Li Zefan1a419d82010-10-25 15:12:50 +08001445 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001446 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001447 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1448 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001449 }
1450
Diego Calleja60ccf822011-09-01 16:33:57 +02001451 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001452
Chris Mason4cb53002011-05-24 15:35:30 -04001453out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001454 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001455 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001456 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001457 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001458 }
Chris Mason4cb53002011-05-24 15:35:30 -04001459 if (!file)
1460 kfree(ra);
1461 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001462 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001463}
1464
Miao Xie198605a2012-11-26 08:43:45 +00001465static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001466 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001467{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001468 struct inode *inode = file_inode(file);
1469 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001470 u64 new_size;
1471 u64 old_size;
1472 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001473 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001474 struct btrfs_ioctl_vol_args *vol_args;
1475 struct btrfs_trans_handle *trans;
1476 struct btrfs_device *device = NULL;
1477 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001478 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001479 char *devstr = NULL;
1480 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001481 int mod = 0;
1482
Chris Masone441d542009-01-05 16:57:23 -05001483 if (!capable(CAP_SYS_ADMIN))
1484 return -EPERM;
1485
Miao Xie198605a2012-11-26 08:43:45 +00001486 ret = mnt_want_write_file(file);
1487 if (ret)
1488 return ret;
1489
David Sterba171938e2017-03-28 14:44:21 +02001490 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001491 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001492 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001493 }
1494
Li Zefandae7b662009-04-08 15:06:54 +08001495 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001496 if (IS_ERR(vol_args)) {
1497 ret = PTR_ERR(vol_args);
1498 goto out;
1499 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001500
1501 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001502
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001503 sizestr = vol_args->name;
1504 devstr = strchr(sizestr, ':');
1505 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001506 sizestr = devstr + 1;
1507 *devstr = '\0';
1508 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001509 ret = kstrtoull(devstr, 10, &devid);
1510 if (ret)
1511 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001512 if (!devid) {
1513 ret = -EINVAL;
1514 goto out_free;
1515 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001516 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001517 }
Miao Xiedba60f32012-12-21 09:19:51 +00001518
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001519 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001521 btrfs_info(fs_info, "resizer unable to find device %llu",
1522 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001523 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001524 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001525 }
Miao Xiedba60f32012-12-21 09:19:51 +00001526
Anand Jainebbede42017-12-04 12:54:52 +08001527 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001528 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001529 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001530 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001531 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001532 goto out_free;
1533 }
1534
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001535 if (!strcmp(sizestr, "max"))
1536 new_size = device->bdev->bd_inode->i_size;
1537 else {
1538 if (sizestr[0] == '-') {
1539 mod = -1;
1540 sizestr++;
1541 } else if (sizestr[0] == '+') {
1542 mod = 1;
1543 sizestr++;
1544 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001545 new_size = memparse(sizestr, &retptr);
1546 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001547 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001548 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001549 }
1550 }
1551
Anand Jain401e29c2017-12-04 12:54:55 +08001552 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001553 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001554 goto out_free;
1555 }
1556
Miao Xie7cc8e582014-09-03 21:35:38 +08001557 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001558
1559 if (mod < 0) {
1560 if (new_size > old_size) {
1561 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001562 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001563 }
1564 new_size = old_size - new_size;
1565 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001566 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001567 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001568 goto out_free;
1569 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001570 new_size = old_size + new_size;
1571 }
1572
Byongho Leeee221842015-12-15 01:42:10 +09001573 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001574 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001575 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001576 }
1577 if (new_size > device->bdev->bd_inode->i_size) {
1578 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001579 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001580 }
1581
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001582 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001583
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001584 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1585 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001586
1587 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001588 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001589 if (IS_ERR(trans)) {
1590 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001591 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001592 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001593 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001594 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001595 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001596 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001597 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001598
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001599out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001600 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001601out:
David Sterba171938e2017-03-28 14:44:21 +02001602 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001603 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001604 return ret;
1605}
1606
Sage Weil72fd0322010-10-29 15:41:32 -04001607static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001608 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001609 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001610 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001611{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001612 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001613 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001615 if (!S_ISDIR(file_inode(file)->i_mode))
1616 return -ENOTDIR;
1617
Liu Boa874a632012-06-29 03:58:46 -06001618 ret = mnt_want_write_file(file);
1619 if (ret)
1620 goto out;
1621
Sage Weil72fd0322010-10-29 15:41:32 -04001622 namelen = strlen(name);
1623 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001624 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001625 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626 }
1627
Chris Mason16780ca2012-02-20 22:14:55 -05001628 if (name[0] == '.' &&
1629 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1630 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001631 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001632 }
1633
Chris Mason3de45862008-11-17 21:02:50 -05001634 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001635 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001636 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001637 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001638 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001639 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001640 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001641 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001642 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001643 }
1644
Al Viro496ad9a2013-01-23 17:07:38 -05001645 src_inode = file_inode(src.file);
1646 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001647 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001648 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001649 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001650 } else if (!inode_owner_or_capable(src_inode)) {
1651 /*
1652 * Subvolume creation is not restricted, but snapshots
1653 * are limited to own subvolumes only
1654 */
1655 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001656 } else {
1657 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1658 BTRFS_I(src_inode)->root,
1659 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001660 }
Al Viro2903ff02012-08-28 12:52:22 -04001661 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001662 }
Liu Boa874a632012-06-29 03:58:46 -06001663out_drop_write:
1664 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001665out:
Sage Weil72fd0322010-10-29 15:41:32 -04001666 return ret;
1667}
1668
1669static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001670 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001671{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001672 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001673 int ret;
1674
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001675 if (!S_ISDIR(file_inode(file)->i_mode))
1676 return -ENOTDIR;
1677
Li Zefanfa0d2b92010-12-20 15:53:28 +08001678 vol_args = memdup_user(arg, sizeof(*vol_args));
1679 if (IS_ERR(vol_args))
1680 return PTR_ERR(vol_args);
1681 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001682
Li Zefanfa0d2b92010-12-20 15:53:28 +08001683 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001684 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001685 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001686
Li Zefanfa0d2b92010-12-20 15:53:28 +08001687 kfree(vol_args);
1688 return ret;
1689}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001690
Li Zefanfa0d2b92010-12-20 15:53:28 +08001691static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1692 void __user *arg, int subvol)
1693{
1694 struct btrfs_ioctl_vol_args_v2 *vol_args;
1695 int ret;
1696 u64 transid = 0;
1697 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001698 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001699 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001700
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001701 if (!S_ISDIR(file_inode(file)->i_mode))
1702 return -ENOTDIR;
1703
Li Zefanfa0d2b92010-12-20 15:53:28 +08001704 vol_args = memdup_user(arg, sizeof(*vol_args));
1705 if (IS_ERR(vol_args))
1706 return PTR_ERR(vol_args);
1707 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001708
Li Zefanb83cc962010-12-20 16:04:08 +08001709 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001710 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1711 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001712 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001713 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001714 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001715
1716 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1717 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001718 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1719 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001720 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001721 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001722 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001723 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001724 }
1725 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1726 if (IS_ERR(inherit)) {
1727 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001728 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001729 }
1730 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001731
1732 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001733 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001734 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001735 if (ret)
1736 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001737
Dan Carpenterc47ca322014-09-04 14:09:15 +03001738 if (ptr && copy_to_user(arg +
1739 offsetof(struct btrfs_ioctl_vol_args_v2,
1740 transid),
1741 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001742 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001743
1744free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001745 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001746free_args:
1747 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001748 return ret;
1749}
1750
Li Zefan0caa1022010-12-20 16:30:25 +08001751static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1752 void __user *arg)
1753{
Al Viro496ad9a2013-01-23 17:07:38 -05001754 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001755 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001756 struct btrfs_root *root = BTRFS_I(inode)->root;
1757 int ret = 0;
1758 u64 flags = 0;
1759
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001760 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001761 return -EINVAL;
1762
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001763 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001764 if (btrfs_root_readonly(root))
1765 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001766 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001767
1768 if (copy_to_user(arg, &flags, sizeof(flags)))
1769 ret = -EFAULT;
1770
1771 return ret;
1772}
1773
1774static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1775 void __user *arg)
1776{
Al Viro496ad9a2013-01-23 17:07:38 -05001777 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001778 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001779 struct btrfs_root *root = BTRFS_I(inode)->root;
1780 struct btrfs_trans_handle *trans;
1781 u64 root_flags;
1782 u64 flags;
1783 int ret = 0;
1784
David Sterbabd60ea02014-01-16 15:50:22 +01001785 if (!inode_owner_or_capable(inode))
1786 return -EPERM;
1787
Liu Bob9ca0662012-06-29 03:58:49 -06001788 ret = mnt_want_write_file(file);
1789 if (ret)
1790 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001791
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001792 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001793 ret = -EINVAL;
1794 goto out_drop_write;
1795 }
Li Zefan0caa1022010-12-20 16:30:25 +08001796
Liu Bob9ca0662012-06-29 03:58:49 -06001797 if (copy_from_user(&flags, arg, sizeof(flags))) {
1798 ret = -EFAULT;
1799 goto out_drop_write;
1800 }
Li Zefan0caa1022010-12-20 16:30:25 +08001801
Liu Bob9ca0662012-06-29 03:58:49 -06001802 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1803 ret = -EINVAL;
1804 goto out_drop_write;
1805 }
Li Zefan0caa1022010-12-20 16:30:25 +08001806
Liu Bob9ca0662012-06-29 03:58:49 -06001807 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1808 ret = -EOPNOTSUPP;
1809 goto out_drop_write;
1810 }
Li Zefan0caa1022010-12-20 16:30:25 +08001811
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001812 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001813
1814 /* nothing to do */
1815 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001816 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001817
1818 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001819 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001820 btrfs_set_root_flags(&root->root_item,
1821 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001822 } else {
1823 /*
1824 * Block RO -> RW transition if this subvolume is involved in
1825 * send
1826 */
1827 spin_lock(&root->root_item_lock);
1828 if (root->send_in_progress == 0) {
1829 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001830 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001831 spin_unlock(&root->root_item_lock);
1832 } else {
1833 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001834 btrfs_warn(fs_info,
1835 "Attempt to set subvolume %llu read-write during send",
1836 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001837 ret = -EPERM;
1838 goto out_drop_sem;
1839 }
1840 }
Li Zefan0caa1022010-12-20 16:30:25 +08001841
1842 trans = btrfs_start_transaction(root, 1);
1843 if (IS_ERR(trans)) {
1844 ret = PTR_ERR(trans);
1845 goto out_reset;
1846 }
1847
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001849 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001850 if (ret < 0) {
1851 btrfs_end_transaction(trans);
1852 goto out_reset;
1853 }
Li Zefan0caa1022010-12-20 16:30:25 +08001854
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001855 ret = btrfs_commit_transaction(trans);
1856
Li Zefan0caa1022010-12-20 16:30:25 +08001857out_reset:
1858 if (ret)
1859 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001860out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001861 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001862out_drop_write:
1863 mnt_drop_write_file(file);
1864out:
Li Zefan0caa1022010-12-20 16:30:25 +08001865 return ret;
1866}
1867
Chris Masonac8e9812010-02-28 15:39:26 -05001868static noinline int key_in_sk(struct btrfs_key *key,
1869 struct btrfs_ioctl_search_key *sk)
1870{
Chris Masonabc6e132010-03-18 12:10:08 -04001871 struct btrfs_key test;
1872 int ret;
1873
1874 test.objectid = sk->min_objectid;
1875 test.type = sk->min_type;
1876 test.offset = sk->min_offset;
1877
1878 ret = btrfs_comp_cpu_keys(key, &test);
1879 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001880 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001881
1882 test.objectid = sk->max_objectid;
1883 test.type = sk->max_type;
1884 test.offset = sk->max_offset;
1885
1886 ret = btrfs_comp_cpu_keys(key, &test);
1887 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001888 return 0;
1889 return 1;
1890}
1891
Jeff Mahoneydf397562016-06-21 20:18:21 -04001892static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001893 struct btrfs_key *key,
1894 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001895 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001896 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001897 unsigned long *sk_offset,
1898 int *num_found)
1899{
1900 u64 found_transid;
1901 struct extent_buffer *leaf;
1902 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001903 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001904 unsigned long item_off;
1905 unsigned long item_len;
1906 int nritems;
1907 int i;
1908 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001909 int ret = 0;
1910
1911 leaf = path->nodes[0];
1912 slot = path->slots[0];
1913 nritems = btrfs_header_nritems(leaf);
1914
1915 if (btrfs_header_generation(leaf) > sk->max_transid) {
1916 i = nritems;
1917 goto advance_key;
1918 }
1919 found_transid = btrfs_header_generation(leaf);
1920
1921 for (i = slot; i < nritems; i++) {
1922 item_off = btrfs_item_ptr_offset(leaf, i);
1923 item_len = btrfs_item_size_nr(leaf, i);
1924
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001925 btrfs_item_key_to_cpu(leaf, key, i);
1926 if (!key_in_sk(key, sk))
1927 continue;
1928
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001929 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001930 if (*num_found) {
1931 ret = 1;
1932 goto out;
1933 }
Chris Masonac8e9812010-02-28 15:39:26 -05001934
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001935 /*
1936 * return one empty item back for v1, which does not
1937 * handle -EOVERFLOW
1938 */
1939
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001940 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001941 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001942 ret = -EOVERFLOW;
1943 }
Chris Masonac8e9812010-02-28 15:39:26 -05001944
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001945 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05001946 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001947 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001948 }
1949
Chris Masonac8e9812010-02-28 15:39:26 -05001950 sh.objectid = key->objectid;
1951 sh.offset = key->offset;
1952 sh.type = key->type;
1953 sh.len = item_len;
1954 sh.transid = found_transid;
1955
1956 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01001957 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
1958 ret = -EFAULT;
1959 goto out;
1960 }
1961
Chris Masonac8e9812010-02-28 15:39:26 -05001962 *sk_offset += sizeof(sh);
1963
1964 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01001965 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05001966 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01001967 if (read_extent_buffer_to_user(leaf, up,
1968 item_off, item_len)) {
1969 ret = -EFAULT;
1970 goto out;
1971 }
1972
Chris Masonac8e9812010-02-28 15:39:26 -05001973 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001974 }
Hugo Millse2156862011-05-14 17:43:41 +00001975 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001976
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001977 if (ret) /* -EOVERFLOW from above */
1978 goto out;
1979
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001980 if (*num_found >= sk->nr_items) {
1981 ret = 1;
1982 goto out;
1983 }
Chris Masonac8e9812010-02-28 15:39:26 -05001984 }
1985advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001986 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001987 test.objectid = sk->max_objectid;
1988 test.type = sk->max_type;
1989 test.offset = sk->max_offset;
1990 if (btrfs_comp_cpu_keys(key, &test) >= 0)
1991 ret = 1;
1992 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04001993 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001994 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04001995 key->offset = 0;
1996 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001997 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04001998 key->offset = 0;
1999 key->type = 0;
2000 key->objectid++;
2001 } else
2002 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002003out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002004 /*
2005 * 0: all items from this leaf copied, continue with next
2006 * 1: * more items can be copied, but unused buffer is too small
2007 * * all items were found
2008 * Either way, it will stops the loop which iterates to the next
2009 * leaf
2010 * -EOVERFLOW: item was to large for buffer
2011 * -EFAULT: could not copy extent buffer back to userspace
2012 */
Chris Masonac8e9812010-02-28 15:39:26 -05002013 return ret;
2014}
2015
2016static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002017 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002018 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002019 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002020{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002021 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002022 struct btrfs_root *root;
2023 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002024 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002025 int ret;
2026 int num_found = 0;
2027 unsigned long sk_offset = 0;
2028
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002029 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2030 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002031 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002032 }
Gerhard Heift12544442014-01-30 16:23:58 +01002033
Chris Masonac8e9812010-02-28 15:39:26 -05002034 path = btrfs_alloc_path();
2035 if (!path)
2036 return -ENOMEM;
2037
2038 if (sk->tree_id == 0) {
2039 /* search the root of the inode that was passed */
2040 root = BTRFS_I(inode)->root;
2041 } else {
2042 key.objectid = sk->tree_id;
2043 key.type = BTRFS_ROOT_ITEM_KEY;
2044 key.offset = (u64)-1;
2045 root = btrfs_read_fs_root_no_name(info, &key);
2046 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002047 btrfs_free_path(path);
2048 return -ENOENT;
2049 }
2050 }
2051
2052 key.objectid = sk->min_objectid;
2053 key.type = sk->min_type;
2054 key.offset = sk->min_offset;
2055
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302056 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002057 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002058 if (ret != 0) {
2059 if (ret > 0)
2060 ret = 0;
2061 goto err;
2062 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002063 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002064 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002065 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002066 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002067 break;
2068
2069 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002070 if (ret > 0)
2071 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002072err:
2073 sk->nr_items = num_found;
2074 btrfs_free_path(path);
2075 return ret;
2076}
2077
2078static noinline int btrfs_ioctl_tree_search(struct file *file,
2079 void __user *argp)
2080{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002081 struct btrfs_ioctl_search_args __user *uargs;
2082 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002083 struct inode *inode;
2084 int ret;
2085 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002086
2087 if (!capable(CAP_SYS_ADMIN))
2088 return -EPERM;
2089
Gerhard Heiftba346b32014-01-30 16:24:02 +01002090 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002091
Gerhard Heiftba346b32014-01-30 16:24:02 +01002092 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2093 return -EFAULT;
2094
2095 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002096
Al Viro496ad9a2013-01-23 17:07:38 -05002097 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002098 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002099
2100 /*
2101 * In the origin implementation an overflow is handled by returning a
2102 * search header with a len of zero, so reset ret.
2103 */
2104 if (ret == -EOVERFLOW)
2105 ret = 0;
2106
Gerhard Heiftba346b32014-01-30 16:24:02 +01002107 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002108 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002109 return ret;
2110}
2111
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002112static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2113 void __user *argp)
2114{
2115 struct btrfs_ioctl_search_args_v2 __user *uarg;
2116 struct btrfs_ioctl_search_args_v2 args;
2117 struct inode *inode;
2118 int ret;
2119 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002120 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002121
2122 if (!capable(CAP_SYS_ADMIN))
2123 return -EPERM;
2124
2125 /* copy search header and buffer size */
2126 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2127 if (copy_from_user(&args, uarg, sizeof(args)))
2128 return -EFAULT;
2129
2130 buf_size = args.buf_size;
2131
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002132 /* limit result size to 16MB */
2133 if (buf_size > buf_limit)
2134 buf_size = buf_limit;
2135
2136 inode = file_inode(file);
2137 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002138 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002139 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2140 ret = -EFAULT;
2141 else if (ret == -EOVERFLOW &&
2142 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2143 ret = -EFAULT;
2144
Yan, Zheng76dda932009-09-21 16:00:26 -04002145 return ret;
2146}
2147
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002148/*
Chris Masonac8e9812010-02-28 15:39:26 -05002149 * Search INODE_REFs to identify path name of 'dirid' directory
2150 * in a 'tree_id' tree. and sets path name to 'name'.
2151 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002152static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2153 u64 tree_id, u64 dirid, char *name)
2154{
2155 struct btrfs_root *root;
2156 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002157 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002158 int ret = -1;
2159 int slot;
2160 int len;
2161 int total_len = 0;
2162 struct btrfs_inode_ref *iref;
2163 struct extent_buffer *l;
2164 struct btrfs_path *path;
2165
2166 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2167 name[0]='\0';
2168 return 0;
2169 }
2170
2171 path = btrfs_alloc_path();
2172 if (!path)
2173 return -ENOMEM;
2174
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002175 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002176
2177 key.objectid = tree_id;
2178 key.type = BTRFS_ROOT_ITEM_KEY;
2179 key.offset = (u64)-1;
2180 root = btrfs_read_fs_root_no_name(info, &key);
2181 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002182 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002183 ret = -ENOENT;
2184 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002185 }
2186
2187 key.objectid = dirid;
2188 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002189 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002190
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302191 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002192 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2193 if (ret < 0)
2194 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002195 else if (ret > 0) {
2196 ret = btrfs_previous_item(root, path, dirid,
2197 BTRFS_INODE_REF_KEY);
2198 if (ret < 0)
2199 goto out;
2200 else if (ret > 0) {
2201 ret = -ENOENT;
2202 goto out;
2203 }
2204 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002205
2206 l = path->nodes[0];
2207 slot = path->slots[0];
2208 btrfs_item_key_to_cpu(l, &key, slot);
2209
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002210 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2211 len = btrfs_inode_ref_name_len(l, iref);
2212 ptr -= len + 1;
2213 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002214 if (ptr < name) {
2215 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002216 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002217 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002218
2219 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302220 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002221
2222 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2223 break;
2224
David Sterbab3b4aa72011-04-21 01:20:15 +02002225 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002226 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002227 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002228 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002229 }
Li Zefan77906a502011-07-14 03:16:00 +00002230 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302231 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002232 ret = 0;
2233out:
2234 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002235 return ret;
2236}
2237
2238static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2239 void __user *argp)
2240{
2241 struct btrfs_ioctl_ino_lookup_args *args;
2242 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002243 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002244
Julia Lawall2354d08f2010-10-29 15:14:18 -04002245 args = memdup_user(argp, sizeof(*args));
2246 if (IS_ERR(args))
2247 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002248
Al Viro496ad9a2013-01-23 17:07:38 -05002249 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002250
David Sterba01b810b2015-05-12 19:14:49 +02002251 /*
2252 * Unprivileged query to obtain the containing subvolume root id. The
2253 * path is reset so it's consistent with btrfs_search_path_in_tree.
2254 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002255 if (args->treeid == 0)
2256 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2257
David Sterba01b810b2015-05-12 19:14:49 +02002258 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2259 args->name[0] = 0;
2260 goto out;
2261 }
2262
2263 if (!capable(CAP_SYS_ADMIN)) {
2264 ret = -EPERM;
2265 goto out;
2266 }
2267
Chris Masonac8e9812010-02-28 15:39:26 -05002268 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2269 args->treeid, args->objectid,
2270 args->name);
2271
David Sterba01b810b2015-05-12 19:14:49 +02002272out:
Chris Masonac8e9812010-02-28 15:39:26 -05002273 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2274 ret = -EFAULT;
2275
2276 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002277 return ret;
2278}
2279
Yan, Zheng76dda932009-09-21 16:00:26 -04002280static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2281 void __user *arg)
2282{
Al Viro54563d42013-09-01 15:57:51 -04002283 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002284 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002285 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002286 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002287 struct inode *inode;
2288 struct btrfs_root *root = BTRFS_I(dir)->root;
2289 struct btrfs_root *dest = NULL;
2290 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002291 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002292 int err = 0;
2293
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002294 if (!S_ISDIR(dir->i_mode))
2295 return -ENOTDIR;
2296
Yan, Zheng76dda932009-09-21 16:00:26 -04002297 vol_args = memdup_user(arg, sizeof(*vol_args));
2298 if (IS_ERR(vol_args))
2299 return PTR_ERR(vol_args);
2300
2301 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2302 namelen = strlen(vol_args->name);
2303 if (strchr(vol_args->name, '/') ||
2304 strncmp(vol_args->name, "..", namelen) == 0) {
2305 err = -EINVAL;
2306 goto out;
2307 }
2308
Al Viroa561be72011-11-23 11:57:51 -05002309 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002310 if (err)
2311 goto out;
2312
David Sterba521e0542014-04-15 16:41:44 +02002313
Al Viro00235412016-05-26 00:05:12 -04002314 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2315 if (err == -EINTR)
2316 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002317 dentry = lookup_one_len(vol_args->name, parent, namelen);
2318 if (IS_ERR(dentry)) {
2319 err = PTR_ERR(dentry);
2320 goto out_unlock_dir;
2321 }
2322
David Howells2b0143b2015-03-17 22:25:59 +00002323 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002324 err = -ENOENT;
2325 goto out_dput;
2326 }
2327
David Howells2b0143b2015-03-17 22:25:59 +00002328 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002329 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302330 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002331 /*
2332 * Regular user. Only allow this with a special mount
2333 * option, when the user has write+exec access to the
2334 * subvol root, and when rmdir(2) would have been
2335 * allowed.
2336 *
2337 * Note that this is _not_ check that the subvol is
2338 * empty or doesn't contain data that we wouldn't
2339 * otherwise be able to delete.
2340 *
2341 * Users who want to delete empty subvols should try
2342 * rmdir(2).
2343 */
2344 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002345 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002346 goto out_dput;
2347
2348 /*
2349 * Do not allow deletion if the parent dir is the same
2350 * as the dir to be deleted. That means the ioctl
2351 * must be called on the dentry referencing the root
2352 * of the subvol, not a random directory contained
2353 * within it.
2354 */
2355 err = -EINVAL;
2356 if (root == dest)
2357 goto out_dput;
2358
2359 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2360 if (err)
2361 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002362 }
2363
Miao Xie5c39da52012-10-22 11:39:53 +00002364 /* check if subvolume may be deleted by a user */
2365 err = btrfs_may_delete(dir, dentry, 1);
2366 if (err)
2367 goto out_dput;
2368
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002369 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002370 err = -EINVAL;
2371 goto out_dput;
2372 }
2373
Al Viro59551022016-01-22 15:40:57 -05002374 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002375 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002376 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002377 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002378 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002379
Yan, Zheng76dda932009-09-21 16:00:26 -04002380out_dput:
2381 dput(dentry);
2382out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002383 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002384out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002385 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002386out:
2387 kfree(vol_args);
2388 return err;
2389}
2390
Chris Mason1e701a32010-03-11 09:42:04 -05002391static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002392{
Al Viro496ad9a2013-01-23 17:07:38 -05002393 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002394 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002395 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002396 int ret;
2397
Ilya Dryomov25122d12013-01-20 15:57:57 +02002398 ret = mnt_want_write_file(file);
2399 if (ret)
2400 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002401
Ilya Dryomov25122d12013-01-20 15:57:57 +02002402 if (btrfs_root_readonly(root)) {
2403 ret = -EROFS;
2404 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002405 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002406
2407 switch (inode->i_mode & S_IFMT) {
2408 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002409 if (!capable(CAP_SYS_ADMIN)) {
2410 ret = -EPERM;
2411 goto out;
2412 }
Eric Sandeende78b512013-01-31 18:21:12 +00002413 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002414 break;
2415 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002416 if (!(file->f_mode & FMODE_WRITE)) {
2417 ret = -EINVAL;
2418 goto out;
2419 }
Chris Mason1e701a32010-03-11 09:42:04 -05002420
2421 range = kzalloc(sizeof(*range), GFP_KERNEL);
2422 if (!range) {
2423 ret = -ENOMEM;
2424 goto out;
2425 }
2426
2427 if (argp) {
2428 if (copy_from_user(range, argp,
2429 sizeof(*range))) {
2430 ret = -EFAULT;
2431 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002432 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002433 }
2434 /* compression requires us to start the IO */
2435 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2436 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2437 range->extent_thresh = (u32)-1;
2438 }
2439 } else {
2440 /* the rest are all set to zero by kzalloc */
2441 range->len = (u64)-1;
2442 }
Al Viro496ad9a2013-01-23 17:07:38 -05002443 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002444 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002445 if (ret > 0)
2446 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002447 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002448 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002449 default:
2450 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002451 }
Chris Masone441d542009-01-05 16:57:23 -05002452out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002453 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002454 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002455}
2456
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002457static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002458{
2459 struct btrfs_ioctl_vol_args *vol_args;
2460 int ret;
2461
Chris Masone441d542009-01-05 16:57:23 -05002462 if (!capable(CAP_SYS_ADMIN))
2463 return -EPERM;
2464
David Sterba171938e2017-03-28 14:44:21 +02002465 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002466 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002467
Li Zefandae7b662009-04-08 15:06:54 +08002468 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002469 if (IS_ERR(vol_args)) {
2470 ret = PTR_ERR(vol_args);
2471 goto out;
2472 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002473
Mark Fasheh5516e592008-07-24 12:20:14 -04002474 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002475 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002476
Anand Jain43d20762014-07-01 00:58:56 +08002477 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002478 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002479
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002480 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002481out:
David Sterba171938e2017-03-28 14:44:21 +02002482 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002483 return ret;
2484}
2485
Anand Jain6b526ed2016-02-13 10:01:39 +08002486static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002487{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 struct inode *inode = file_inode(file);
2489 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002490 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002491 int ret;
2492
Chris Masone441d542009-01-05 16:57:23 -05002493 if (!capable(CAP_SYS_ADMIN))
2494 return -EPERM;
2495
Miao Xieda249272012-11-26 08:44:50 +00002496 ret = mnt_want_write_file(file);
2497 if (ret)
2498 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002499
Li Zefandae7b662009-04-08 15:06:54 +08002500 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002501 if (IS_ERR(vol_args)) {
2502 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002503 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002504 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002505
Anand Jain6b526ed2016-02-13 10:01:39 +08002506 /* Check for compatibility reject unknown flags */
David Sterba735654e2016-02-15 18:15:21 +01002507 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2508 return -EOPNOTSUPP;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002509
David Sterba171938e2017-03-28 14:44:21 +02002510 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002511 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2512 goto out;
2513 }
2514
David Sterba735654e2016-02-15 18:15:21 +01002515 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002516 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002517 } else {
2518 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002519 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002520 }
David Sterba171938e2017-03-28 14:44:21 +02002521 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002522
Anand Jain6b526ed2016-02-13 10:01:39 +08002523 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002524 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002525 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002526 vol_args->devid);
2527 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002528 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002529 vol_args->name);
2530 }
Anand Jain183860f2013-05-17 10:52:45 +00002531out:
2532 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002533err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002534 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002535 return ret;
2536}
2537
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002538static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2539{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002540 struct inode *inode = file_inode(file);
2541 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002542 struct btrfs_ioctl_vol_args *vol_args;
2543 int ret;
2544
2545 if (!capable(CAP_SYS_ADMIN))
2546 return -EPERM;
2547
2548 ret = mnt_want_write_file(file);
2549 if (ret)
2550 return ret;
2551
David Sterba171938e2017-03-28 14:44:21 +02002552 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002553 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002554 goto out_drop_write;
2555 }
2556
2557 vol_args = memdup_user(arg, sizeof(*vol_args));
2558 if (IS_ERR(vol_args)) {
2559 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002560 goto out;
2561 }
2562
David Sterba58d7bbf2016-05-04 14:10:47 +02002563 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002564 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002565
2566 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002567 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002568 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002569out:
David Sterba171938e2017-03-28 14:44:21 +02002570 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002571out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002572 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002573
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002574 return ret;
2575}
2576
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002577static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2578 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002579{
Li Zefan027ed2f2011-06-08 08:27:56 +00002580 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002581 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002582 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002583 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002584
Li Zefan027ed2f2011-06-08 08:27:56 +00002585 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2586 if (!fi_args)
2587 return -ENOMEM;
2588
David Sterbad03262c2017-06-16 00:09:21 +02002589 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00002590 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002591
David Sterbad03262c2017-06-16 00:09:21 +02002592 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002593 if (device->devid > fi_args->max_id)
2594 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002595 }
David Sterbad03262c2017-06-16 00:09:21 +02002596 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002597
David Sterbad03262c2017-06-16 00:09:21 +02002598 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07002599 fi_args->nodesize = fs_info->nodesize;
2600 fi_args->sectorsize = fs_info->sectorsize;
2601 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002602
Li Zefan027ed2f2011-06-08 08:27:56 +00002603 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2604 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002605
Li Zefan027ed2f2011-06-08 08:27:56 +00002606 kfree(fi_args);
2607 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002608}
2609
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002610static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2611 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002612{
2613 struct btrfs_ioctl_dev_info_args *di_args;
2614 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01002615 int ret = 0;
2616 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002617
Jan Schmidt475f6382011-03-11 15:41:01 +01002618 di_args = memdup_user(arg, sizeof(*di_args));
2619 if (IS_ERR(di_args))
2620 return PTR_ERR(di_args);
2621
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002622 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002623 s_uuid = di_args->uuid;
2624
David Sterbac5593ca2017-06-16 00:09:21 +02002625 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002626 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002627
2628 if (!dev) {
2629 ret = -ENODEV;
2630 goto out;
2631 }
2632
2633 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002634 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2635 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002636 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002637 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002638 struct rcu_string *name;
2639
Josef Bacik606686e2012-06-04 14:03:51 -04002640 name = rcu_dereference(dev->name);
Xiongfeng Wang6670d4c2018-01-08 19:51:22 +08002641 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02002642 di_args->path[sizeof(di_args->path) - 1] = 0;
2643 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002644 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002645 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002646
2647out:
David Sterbac5593ca2017-06-16 00:09:21 +02002648 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002649 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2650 ret = -EFAULT;
2651
2652 kfree(di_args);
2653 return ret;
2654}
2655
Mark Fashehf4414602015-06-30 14:42:05 -07002656static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002657{
2658 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002659
Mark Fasheh416161d2013-08-06 11:42:51 -07002660 page = grab_cache_page(inode->i_mapping, index);
2661 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002662 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002663
2664 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002665 int ret;
2666
2667 ret = btrfs_readpage(NULL, page);
2668 if (ret)
2669 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002670 lock_page(page);
2671 if (!PageUptodate(page)) {
2672 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002673 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002674 return ERR_PTR(-EIO);
2675 }
2676 if (page->mapping != inode->i_mapping) {
2677 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002678 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002679 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002680 }
2681 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002682
2683 return page;
2684}
2685
Mark Fashehf4414602015-06-30 14:42:05 -07002686static int gather_extent_pages(struct inode *inode, struct page **pages,
2687 int num_pages, u64 off)
2688{
2689 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002690 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002691
2692 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002693again:
Mark Fashehf4414602015-06-30 14:42:05 -07002694 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002695 if (IS_ERR(pages[i])) {
2696 int err = PTR_ERR(pages[i]);
2697
2698 if (err == -EAGAIN)
2699 goto again;
2700 pages[i] = NULL;
2701 return err;
2702 }
Mark Fashehf4414602015-06-30 14:42:05 -07002703 }
2704 return 0;
2705}
2706
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002707static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2708 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002709{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002710 /*
2711 * Do any pending delalloc/csum calculations on inode, one way or
2712 * another, and lock file content.
2713 * The locking order is:
2714 *
2715 * 1) pages
2716 * 2) range in the inode's io tree
2717 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002718 while (1) {
2719 struct btrfs_ordered_extent *ordered;
2720 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2721 ordered = btrfs_lookup_first_ordered_extent(inode,
2722 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002723 if ((!ordered ||
2724 ordered->file_offset + ordered->len <= off ||
2725 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002726 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002727 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2728 if (ordered)
2729 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002730 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002731 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002732 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2733 if (ordered)
2734 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002735 if (!retry_range_locking)
2736 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002737 btrfs_wait_ordered_range(inode, off, len);
2738 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002739 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002740}
2741
Mark Fashehf4414602015-06-30 14:42:05 -07002742static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002743{
Al Viro59551022016-01-22 15:40:57 -05002744 inode_unlock(inode1);
2745 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002746}
2747
Mark Fashehf4414602015-06-30 14:42:05 -07002748static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2749{
2750 if (inode1 < inode2)
2751 swap(inode1, inode2);
2752
Al Viro59551022016-01-22 15:40:57 -05002753 inode_lock_nested(inode1, I_MUTEX_PARENT);
2754 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002755}
2756
2757static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2758 struct inode *inode2, u64 loff2, u64 len)
2759{
2760 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2761 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2762}
2763
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002764static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2765 struct inode *inode2, u64 loff2, u64 len,
2766 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002767{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002768 int ret;
2769
Mark Fasheh416161d2013-08-06 11:42:51 -07002770 if (inode1 < inode2) {
2771 swap(inode1, inode2);
2772 swap(loff1, loff2);
2773 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002774 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2775 if (ret)
2776 return ret;
2777 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2778 if (ret)
2779 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2780 loff1 + len - 1);
2781 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002782}
2783
2784struct cmp_pages {
2785 int num_pages;
2786 struct page **src_pages;
2787 struct page **dst_pages;
2788};
2789
2790static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2791{
2792 int i;
2793 struct page *pg;
2794
2795 for (i = 0; i < cmp->num_pages; i++) {
2796 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002797 if (pg) {
2798 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002799 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002800 }
Mark Fashehf4414602015-06-30 14:42:05 -07002801 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002802 if (pg) {
2803 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002804 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002805 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002806 }
Mark Fashehf4414602015-06-30 14:42:05 -07002807 kfree(cmp->src_pages);
2808 kfree(cmp->dst_pages);
2809}
2810
2811static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2812 struct inode *dst, u64 dst_loff,
2813 u64 len, struct cmp_pages *cmp)
2814{
2815 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002816 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002817 struct page **src_pgarr, **dst_pgarr;
2818
2819 /*
2820 * We must gather up all the pages before we initiate our
2821 * extent locking. We use an array for the page pointers. Size
2822 * of the array is bounded by len, which is in turn bounded by
2823 * BTRFS_MAX_DEDUPE_LEN.
2824 */
David Sterba66722f72016-02-11 15:01:38 +01002825 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
2826 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07002827 if (!src_pgarr || !dst_pgarr) {
2828 kfree(src_pgarr);
2829 kfree(dst_pgarr);
2830 return -ENOMEM;
2831 }
2832 cmp->num_pages = num_pages;
2833 cmp->src_pages = src_pgarr;
2834 cmp->dst_pages = dst_pgarr;
2835
Filipe Mananab1517622017-02-21 17:14:52 +00002836 /*
2837 * If deduping ranges in the same inode, locking rules make it mandatory
2838 * to always lock pages in ascending order to avoid deadlocks with
2839 * concurrent tasks (such as starting writeback/delalloc).
2840 */
2841 if (src == dst && dst_loff < loff) {
2842 swap(src_pgarr, dst_pgarr);
2843 swap(loff, dst_loff);
2844 }
2845
2846 ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002847 if (ret)
2848 goto out;
2849
Filipe Mananab1517622017-02-21 17:14:52 +00002850 ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002851
2852out:
2853 if (ret)
2854 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09002855 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07002856}
2857
David Sterba1a287cf2017-02-10 20:15:10 +01002858static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002859{
2860 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07002861 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07002862 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002863 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002864 void *addr, *dst_addr;
2865
Mark Fashehf4414602015-06-30 14:42:05 -07002866 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002867 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002868 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07002869 cmp_len = len;
2870
Mark Fashehf4414602015-06-30 14:42:05 -07002871 BUG_ON(i >= cmp->num_pages);
2872
2873 src_page = cmp->src_pages[i];
2874 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002875 ASSERT(PageLocked(src_page));
2876 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07002877
Mark Fasheh416161d2013-08-06 11:42:51 -07002878 addr = kmap_atomic(src_page);
2879 dst_addr = kmap_atomic(dst_page);
2880
2881 flush_dcache_page(src_page);
2882 flush_dcache_page(dst_page);
2883
2884 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08002885 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002886
2887 kunmap_atomic(addr);
2888 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07002889
2890 if (ret)
2891 break;
2892
Mark Fasheh416161d2013-08-06 11:42:51 -07002893 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07002894 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07002895 }
2896
2897 return ret;
2898}
2899
Mark Fashehe1d227a2015-06-08 15:05:25 -07002900static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
2901 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07002902{
Mark Fashehe1d227a2015-06-08 15:05:25 -07002903 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07002904 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2905
Mark Fashehe1d227a2015-06-08 15:05:25 -07002906 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07002907 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07002908
2909 /* if we extend to eof, continue to block boundary */
2910 if (off + len == inode->i_size)
2911 *plen = len = ALIGN(inode->i_size, bs) - off;
2912
Mark Fasheh416161d2013-08-06 11:42:51 -07002913 /* Check that we are block aligned - btrfs_clone() requires this */
2914 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2915 return -EINVAL;
2916
2917 return 0;
2918}
2919
Mark Fashehe1d227a2015-06-08 15:05:25 -07002920static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07002921 struct inode *dst, u64 dst_loff)
2922{
2923 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07002924 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07002925 struct cmp_pages cmp;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002926 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07002927 u64 same_lock_start = 0;
2928 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002929
Filipe Manana113e8282015-03-30 18:26:47 +01002930 if (len == 0)
2931 return 0;
2932
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002933 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05002934 inode_lock(src);
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002935 else
2936 btrfs_double_inode_lock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07002937
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002938 ret = extent_same_check_offsets(src, loff, &len, olen);
2939 if (ret)
2940 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07002941
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002942 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
2943 if (ret)
2944 goto out_unlock;
2945
2946 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07002947 /*
2948 * Single inode case wants the same checks, except we
2949 * don't want our length pushed out past i_size as
2950 * comparing that data range makes no sense.
2951 *
2952 * extent_same_check_offsets() will do this for an
2953 * unaligned length at i_size, so catch it here and
2954 * reject the request.
2955 *
2956 * This effectively means we require aligned extents
2957 * for the single-inode case, whereas the other cases
2958 * allow an unaligned length so long as it ends at
2959 * i_size.
2960 */
2961 if (len != olen) {
2962 ret = -EINVAL;
2963 goto out_unlock;
2964 }
2965
2966 /* Check for overlapping ranges */
2967 if (dst_loff + len > loff && dst_loff < loff + len) {
2968 ret = -EINVAL;
2969 goto out_unlock;
2970 }
2971
2972 same_lock_start = min_t(u64, loff, dst_loff);
2973 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07002974 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002975
2976 /* don't make the dst file partly checksummed */
2977 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2978 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2979 ret = -EINVAL;
2980 goto out_unlock;
2981 }
2982
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002983again:
Mark Fashehf4414602015-06-30 14:42:05 -07002984 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
2985 if (ret)
2986 goto out_unlock;
2987
Mark Fasheh0efa9f42015-06-30 14:42:07 -07002988 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002989 ret = lock_extent_range(src, same_lock_start, same_lock_len,
2990 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07002991 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002992 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
2993 false);
2994 /*
2995 * If one of the inodes has dirty pages in the respective range or
2996 * ordered extents, we need to flush dellaloc and wait for all ordered
2997 * extents in the range. We must unlock the pages and the ranges in the
2998 * io trees to avoid deadlocks when flushing delalloc (requires locking
2999 * pages) and when waiting for ordered extents to complete (they require
3000 * range locking).
3001 */
3002 if (ret == -EAGAIN) {
3003 /*
3004 * Ranges in the io trees already unlocked. Now unlock all
3005 * pages before waiting for all IO to complete.
3006 */
3007 btrfs_cmp_data_free(&cmp);
3008 if (same_inode) {
3009 btrfs_wait_ordered_range(src, same_lock_start,
3010 same_lock_len);
3011 } else {
3012 btrfs_wait_ordered_range(src, loff, len);
3013 btrfs_wait_ordered_range(dst, dst_loff, len);
3014 }
3015 goto again;
3016 }
3017 ASSERT(ret == 0);
3018 if (WARN_ON(ret)) {
3019 /* ranges in the io trees already unlocked */
3020 btrfs_cmp_data_free(&cmp);
3021 return ret;
3022 }
Mark Fashehf4414602015-06-30 14:42:05 -07003023
Mark Fasheh207910d2015-06-30 14:42:04 -07003024 /* pass original length for comparison so we stay within i_size */
David Sterba1a287cf2017-02-10 20:15:10 +01003025 ret = btrfs_cmp_data(olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003026 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003027 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003028
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003029 if (same_inode)
3030 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3031 same_lock_start + same_lock_len - 1);
3032 else
3033 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003034
3035 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003036out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003037 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003038 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003039 else
3040 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003041
3042 return ret;
3043}
3044
Byongho Leeee221842015-12-15 01:42:10 +09003045#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003046
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003047ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3048 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003049{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003050 struct inode *src = file_inode(src_file);
3051 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003052 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003053 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003054
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003055 if (olen > BTRFS_MAX_DEDUPE_LEN)
3056 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003057
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003058 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003059 /*
3060 * Btrfs does not support blocksize < page_size. As a
3061 * result, btrfs_cmp_data() won't correctly handle
3062 * this situation without an update.
3063 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003064 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003065 }
3066
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003067 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3068 if (res)
3069 return res;
3070 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003071}
3072
Filipe Mananaf82a9902014-06-01 01:50:28 +01003073static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3074 struct inode *inode,
3075 u64 endoff,
3076 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003077 const u64 olen,
3078 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003079{
3080 struct btrfs_root *root = BTRFS_I(inode)->root;
3081 int ret;
3082
3083 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003084 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003085 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003086 /*
3087 * We round up to the block size at eof when determining which
3088 * extents to clone above, but shouldn't round up the file size.
3089 */
3090 if (endoff > destoff + olen)
3091 endoff = destoff + olen;
3092 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003093 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003094
3095 ret = btrfs_update_inode(trans, root, inode);
3096 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003097 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003098 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003099 goto out;
3100 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003101 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003102out:
3103 return ret;
3104}
3105
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003106static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003107 const struct btrfs_trans_handle *trans,
3108 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003109 const u64 hole_offset,
3110 const u64 hole_len)
3111{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003112 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003113 struct extent_map *em;
3114 int ret;
3115
3116 em = alloc_extent_map();
3117 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003118 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003119 return;
3120 }
3121
Filipe Manana14f59792014-06-29 21:45:40 +01003122 if (path) {
3123 struct btrfs_file_extent_item *fi;
3124
3125 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3126 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003127 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003128 em->generation = -1;
3129 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3130 BTRFS_FILE_EXTENT_INLINE)
3131 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003132 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003133 } else {
3134 em->start = hole_offset;
3135 em->len = hole_len;
3136 em->ram_bytes = em->len;
3137 em->orig_start = hole_offset;
3138 em->block_start = EXTENT_MAP_HOLE;
3139 em->block_len = 0;
3140 em->orig_block_len = 0;
3141 em->compress_type = BTRFS_COMPRESS_NONE;
3142 em->generation = trans->transid;
3143 }
3144
3145 while (1) {
3146 write_lock(&em_tree->lock);
3147 ret = add_extent_mapping(em_tree, em, 1);
3148 write_unlock(&em_tree->lock);
3149 if (ret != -EEXIST) {
3150 free_extent_map(em);
3151 break;
3152 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003153 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003154 em->start + em->len - 1, 0);
3155 }
3156
David Sterbaee39b432014-09-30 01:33:33 +02003157 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003158 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003159}
3160
Filipe Manana8039d872015-10-13 15:15:00 +01003161/*
3162 * Make sure we do not end up inserting an inline extent into a file that has
3163 * already other (non-inline) extents. If a file has an inline extent it can
3164 * not have any other extents and the (single) inline extent must start at the
3165 * file offset 0. Failing to respect these rules will lead to file corruption,
3166 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3167 *
3168 * We can have extents that have been already written to disk or we can have
3169 * dirty ranges still in delalloc, in which case the extent maps and items are
3170 * created only when we run delalloc, and the delalloc ranges might fall outside
3171 * the range we are currently locking in the inode's io tree. So we check the
3172 * inode's i_size because of that (i_size updates are done while holding the
3173 * i_mutex, which we are holding here).
3174 * We also check to see if the inode has a size not greater than "datal" but has
3175 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3176 * protected against such concurrent fallocate calls by the i_mutex).
3177 *
3178 * If the file has no extents but a size greater than datal, do not allow the
3179 * copy because we would need turn the inline extent into a non-inline one (even
3180 * with NO_HOLES enabled). If we find our destination inode only has one inline
3181 * extent, just overwrite it with the source inline extent if its size is less
3182 * than the source extent's size, or we could copy the source inline extent's
3183 * data into the destination inode's inline extent if the later is greater then
3184 * the former.
3185 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003186static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003187 struct btrfs_trans_handle *trans,
3188 struct btrfs_path *path,
3189 struct btrfs_key *new_key,
3190 const u64 drop_start,
3191 const u64 datal,
3192 const u64 skip,
3193 const u64 size,
3194 char *inline_data)
3195{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003196 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003197 struct btrfs_root *root = BTRFS_I(dst)->root;
3198 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003199 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003200 int ret;
3201 struct btrfs_key key;
3202
3203 if (new_key->offset > 0)
3204 return -EOPNOTSUPP;
3205
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003206 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003207 key.type = BTRFS_EXTENT_DATA_KEY;
3208 key.offset = 0;
3209 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3210 if (ret < 0) {
3211 return ret;
3212 } else if (ret > 0) {
3213 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3214 ret = btrfs_next_leaf(root, path);
3215 if (ret < 0)
3216 return ret;
3217 else if (ret > 0)
3218 goto copy_inline_extent;
3219 }
3220 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003221 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003222 key.type == BTRFS_EXTENT_DATA_KEY) {
3223 ASSERT(key.offset > 0);
3224 return -EOPNOTSUPP;
3225 }
3226 } else if (i_size_read(dst) <= datal) {
3227 struct btrfs_file_extent_item *ei;
3228 u64 ext_len;
3229
3230 /*
3231 * If the file size is <= datal, make sure there are no other
3232 * extents following (can happen do to an fallocate call with
3233 * the flag FALLOC_FL_KEEP_SIZE).
3234 */
3235 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3236 struct btrfs_file_extent_item);
3237 /*
3238 * If it's an inline extent, it can not have other extents
3239 * following it.
3240 */
3241 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3242 BTRFS_FILE_EXTENT_INLINE)
3243 goto copy_inline_extent;
3244
3245 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3246 if (ext_len > aligned_end)
3247 return -EOPNOTSUPP;
3248
3249 ret = btrfs_next_item(root, path);
3250 if (ret < 0) {
3251 return ret;
3252 } else if (ret == 0) {
3253 btrfs_item_key_to_cpu(path->nodes[0], &key,
3254 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003255 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003256 key.type == BTRFS_EXTENT_DATA_KEY)
3257 return -EOPNOTSUPP;
3258 }
3259 }
3260
3261copy_inline_extent:
3262 /*
3263 * We have no extent items, or we have an extent at offset 0 which may
3264 * or may not be inlined. All these cases are dealt the same way.
3265 */
3266 if (i_size_read(dst) > datal) {
3267 /*
3268 * If the destination inode has an inline extent...
3269 * This would require copying the data from the source inline
3270 * extent into the beginning of the destination's inline extent.
3271 * But this is really complex, both extents can be compressed
3272 * or just one of them, which would require decompressing and
3273 * re-compressing data (which could increase the new compressed
3274 * size, not allowing the compressed data to fit anymore in an
3275 * inline extent).
3276 * So just don't support this case for now (it should be rare,
3277 * we are not really saving space when cloning inline extents).
3278 */
3279 return -EOPNOTSUPP;
3280 }
3281
3282 btrfs_release_path(path);
3283 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3284 if (ret)
3285 return ret;
3286 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3287 if (ret)
3288 return ret;
3289
3290 if (skip) {
3291 const u32 start = btrfs_file_extent_calc_inline_size(0);
3292
3293 memmove(inline_data + start, inline_data + start + skip, datal);
3294 }
3295
3296 write_extent_buffer(path->nodes[0], inline_data,
3297 btrfs_item_ptr_offset(path->nodes[0],
3298 path->slots[0]),
3299 size);
3300 inode_add_bytes(dst, datal);
3301
3302 return 0;
3303}
3304
Mark Fasheh32b7c682013-08-06 11:42:49 -07003305/**
3306 * btrfs_clone() - clone a range from inode file to another
3307 *
3308 * @src: Inode to clone from
3309 * @inode: Inode to clone to
3310 * @off: Offset within source to start clone from
3311 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003312 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003313 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003314 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003315 */
3316static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003317 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003318 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003320 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003321 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003322 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003323 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003324 struct btrfs_trans_handle *trans;
3325 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003326 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003327 u32 nritems;
3328 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003329 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003330 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003331 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003332
3333 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003334 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3335 if (!buf)
3336 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003337
3338 path = btrfs_alloc_path();
3339 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003340 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003341 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003342 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003343
David Sterbae4058b52015-11-27 16:31:35 +01003344 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003345 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003346 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003347 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003348 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003349
3350 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003351 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003352
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003353 /*
3354 * note the key will change type as we walk through the
3355 * tree.
3356 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003357 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003358 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3359 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003360 if (ret < 0)
3361 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003362 /*
3363 * First search, if no extent item that starts at offset off was
3364 * found but the previous item is an extent item, it's possible
3365 * it might overlap our target range, therefore process it.
3366 */
3367 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3368 btrfs_item_key_to_cpu(path->nodes[0], &key,
3369 path->slots[0] - 1);
3370 if (key.type == BTRFS_EXTENT_DATA_KEY)
3371 path->slots[0]--;
3372 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003373
Yan Zhengae01a0a2008-08-04 23:23:47 -04003374 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003375process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003376 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003377 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003378 if (ret < 0)
3379 goto out;
3380 if (ret > 0)
3381 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003382 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003383 }
3384 leaf = path->nodes[0];
3385 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003386
Yan Zhengae01a0a2008-08-04 23:23:47 -04003387 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003388 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003389 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003390 break;
3391
David Sterba962a2982014-06-04 18:41:45 +02003392 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003393 struct btrfs_file_extent_item *extent;
3394 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003395 u32 size;
3396 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003397 u64 disko = 0, diskl = 0;
3398 u64 datao = 0, datal = 0;
3399 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003400 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003401
Sage Weilc5c9cd42008-11-12 14:32:25 -05003402 extent = btrfs_item_ptr(leaf, slot,
3403 struct btrfs_file_extent_item);
3404 comp = btrfs_file_extent_compression(leaf, extent);
3405 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003406 if (type == BTRFS_FILE_EXTENT_REG ||
3407 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003408 disko = btrfs_file_extent_disk_bytenr(leaf,
3409 extent);
3410 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3411 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003412 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003413 datal = btrfs_file_extent_num_bytes(leaf,
3414 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003415 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3416 /* take upper bound, may be compressed */
3417 datal = btrfs_file_extent_ram_bytes(leaf,
3418 extent);
3419 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003420
Filipe Manana2c463822014-05-31 02:31:05 +01003421 /*
3422 * The first search might have left us at an extent
3423 * item that ends before our target range's start, can
3424 * happen if we have holes and NO_HOLES feature enabled.
3425 */
3426 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003427 path->slots[0]++;
3428 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003429 } else if (key.offset >= off + len) {
3430 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003431 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003432 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003433 size = btrfs_item_size_nr(leaf, slot);
3434 read_extent_buffer(leaf, buf,
3435 btrfs_item_ptr_offset(leaf, slot),
3436 size);
3437
3438 btrfs_release_path(path);
3439 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003440
Zheng Yan31840ae2008-09-23 13:14:14 -04003441 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003442 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003443 if (off <= key.offset)
3444 new_key.offset = key.offset + destoff - off;
3445 else
3446 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003447
Sage Weilb6f34092011-09-20 14:48:51 -04003448 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003449 * Deal with a hole that doesn't have an extent item
3450 * that represents it (NO_HOLES feature enabled).
3451 * This hole is either in the middle of the cloning
3452 * range or at the beginning (fully overlaps it or
3453 * partially overlaps it).
3454 */
3455 if (new_key.offset != last_dest_end)
3456 drop_start = last_dest_end;
3457 else
3458 drop_start = new_key.offset;
3459
3460 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003461 * 1 - adjusting old extent (we may have to split it)
3462 * 1 - add new extent
3463 * 1 - inode update
3464 */
3465 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003466 if (IS_ERR(trans)) {
3467 ret = PTR_ERR(trans);
3468 goto out;
3469 }
3470
Chris Masonc8a894d2009-06-27 21:07:03 -04003471 if (type == BTRFS_FILE_EXTENT_REG ||
3472 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003473 /*
3474 * a | --- range to clone ---| b
3475 * | ------------- extent ------------- |
3476 */
3477
Antonio Ospite93915582014-06-04 14:03:48 +02003478 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003479 if (key.offset + datal > off + len)
3480 datal = off + len - key.offset;
3481
Antonio Ospite93915582014-06-04 14:03:48 +02003482 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003483 if (off > key.offset) {
3484 datao += off - key.offset;
3485 datal -= off - key.offset;
3486 }
3487
Josef Bacik5dc562c2012-08-17 13:14:17 -04003488 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003489 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003490 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003491 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003492 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003493 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003494 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003495 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003496 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003497 goto out;
3498 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003499
Sage Weilc5c9cd42008-11-12 14:32:25 -05003500 ret = btrfs_insert_empty_item(trans, root, path,
3501 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003502 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003503 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003504 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003505 goto out;
3506 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003507
3508 leaf = path->nodes[0];
3509 slot = path->slots[0];
3510 write_extent_buffer(leaf, buf,
3511 btrfs_item_ptr_offset(leaf, slot),
3512 size);
3513
3514 extent = btrfs_item_ptr(leaf, slot,
3515 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003516
Sage Weilc5c9cd42008-11-12 14:32:25 -05003517 /* disko == 0 means it's a hole */
3518 if (!disko)
3519 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003520
3521 btrfs_set_file_extent_offset(leaf, extent,
3522 datao);
3523 btrfs_set_file_extent_num_bytes(leaf, extent,
3524 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003525
Sage Weilc5c9cd42008-11-12 14:32:25 -05003526 if (disko) {
3527 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003528 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003529 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003530 disko, diskl, 0,
3531 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003532 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003533 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003534 if (ret) {
3535 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003536 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003537 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003538 goto out;
3539
3540 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003541 }
3542 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3543 u64 skip = 0;
3544 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003545
Sage Weilc5c9cd42008-11-12 14:32:25 -05003546 if (off > key.offset) {
3547 skip = off - key.offset;
3548 new_key.offset += skip;
3549 }
Chris Masond3977122009-01-05 21:25:51 -05003550
Liu Boaa42ffd2012-09-18 03:52:23 -06003551 if (key.offset + datal > off + len)
3552 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003553
Sage Weilc5c9cd42008-11-12 14:32:25 -05003554 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003555 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003556 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003557 goto out;
3558 }
3559 size -= skip + trim;
3560 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003561
David Sterba4a0ab9d2017-02-10 20:18:49 +01003562 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003563 trans, path,
3564 &new_key,
3565 drop_start,
3566 datal,
3567 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003568 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003569 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003570 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003571 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003572 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003573 goto out;
3574 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003575 leaf = path->nodes[0];
3576 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003577 }
3578
Filipe Manana7ffbb592014-06-09 03:48:05 +01003579 /* If we have an implicit hole (NO_HOLES feature). */
3580 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003581 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003582 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003583 new_key.offset - drop_start);
3584
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003585 clone_update_extent_map(BTRFS_I(inode), trans,
3586 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003587
Sage Weilc5c9cd42008-11-12 14:32:25 -05003588 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003589 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003590
Filipe Manana62e23902014-08-08 02:47:06 +01003591 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003592 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003593 ret = clone_finish_inode_update(trans, inode,
3594 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003595 destoff, olen,
3596 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003597 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003598 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003599 if (new_key.offset + datal >= destoff + len)
3600 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003601 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003602 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003603 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003604
3605 if (fatal_signal_pending(current)) {
3606 ret = -EINTR;
3607 goto out;
3608 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003609 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003610 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003611
Filipe Mananaf82a9902014-06-01 01:50:28 +01003612 if (last_dest_end < destoff + len) {
3613 /*
3614 * We have an implicit hole (NO_HOLES feature is enabled) that
3615 * fully or partially overlaps our cloning range at its end.
3616 */
3617 btrfs_release_path(path);
3618
3619 /*
3620 * 1 - remove extent(s)
3621 * 1 - inode update
3622 */
3623 trans = btrfs_start_transaction(root, 2);
3624 if (IS_ERR(trans)) {
3625 ret = PTR_ERR(trans);
3626 goto out;
3627 }
3628 ret = btrfs_drop_extents(trans, root, inode,
3629 last_dest_end, destoff + len, 1);
3630 if (ret) {
3631 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003632 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003633 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003634 goto out;
3635 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003636 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3637 last_dest_end,
3638 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003639 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003640 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003641 }
3642
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003643out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003644 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003645 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003646 return ret;
3647}
3648
Zach Brown3db11b22015-11-10 16:53:32 -05003649static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3650 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003651{
Al Viro54563d42013-09-01 15:57:51 -04003652 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003653 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003655 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003656 int ret;
3657 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003658 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003659 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003660
3661 /*
3662 * TODO:
3663 * - split compressed inline extents. annoying: we need to
3664 * decompress into destination's address_space (the file offset
3665 * may change, so source mapping won't do), then recompress (or
3666 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003667 *
3668 * - split destination inode's inline extents. The inline extents can
3669 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003670 */
3671
Mark Fasheh32b7c682013-08-06 11:42:49 -07003672 if (btrfs_root_readonly(root))
3673 return -EROFS;
3674
Zach Brown3db11b22015-11-10 16:53:32 -05003675 if (file_src->f_path.mnt != file->f_path.mnt ||
3676 src->i_sb != inode->i_sb)
3677 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003678
3679 /* don't make the dst file partly checksummed */
3680 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3681 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003682 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003683
Mark Fasheh32b7c682013-08-06 11:42:49 -07003684 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003685 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003686
3687 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003688 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003689 } else {
Al Viro59551022016-01-22 15:40:57 -05003690 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003691 }
3692
3693 /* determine range to clone */
3694 ret = -EINVAL;
3695 if (off + len > src->i_size || off + len < off)
3696 goto out_unlock;
3697 if (len == 0)
3698 olen = len = src->i_size - off;
3699 /* if we extend to eof, continue to block boundary */
3700 if (off + len == src->i_size)
3701 len = ALIGN(src->i_size, bs) - off;
3702
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003703 if (len == 0) {
3704 ret = 0;
3705 goto out_unlock;
3706 }
3707
Mark Fasheh32b7c682013-08-06 11:42:49 -07003708 /* verify the end result is block aligned */
3709 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3710 !IS_ALIGNED(destoff, bs))
3711 goto out_unlock;
3712
3713 /* verify if ranges are overlapped within the same file */
3714 if (same_inode) {
3715 if (destoff + len > off && destoff < off + len)
3716 goto out_unlock;
3717 }
3718
3719 if (destoff > inode->i_size) {
3720 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3721 if (ret)
3722 goto out_unlock;
3723 }
3724
Filipe Mananac125b8b2014-05-23 05:03:34 +01003725 /*
3726 * Lock the target range too. Right after we replace the file extent
3727 * items in the fs tree (which now point to the cloned data), we might
3728 * have a worker replace them with extent items relative to a write
3729 * operation that was issued before this clone operation (i.e. confront
3730 * with inode.c:btrfs_finish_ordered_io).
3731 */
3732 if (same_inode) {
3733 u64 lock_start = min_t(u64, off, destoff);
3734 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003735
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003736 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003737 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003738 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3739 true);
3740 }
3741 ASSERT(ret == 0);
3742 if (WARN_ON(ret)) {
3743 /* ranges in the io trees already unlocked */
3744 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003745 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003746
Mark Fasheh1c919a52015-06-30 14:42:08 -07003747 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003748
Filipe Mananac125b8b2014-05-23 05:03:34 +01003749 if (same_inode) {
3750 u64 lock_start = min_t(u64, off, destoff);
3751 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3752
3753 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3754 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003755 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003756 }
3757 /*
3758 * Truncate page cache pages so that future reads will see the cloned
3759 * data immediately and not the previous data.
3760 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303761 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003762 round_down(destoff, PAGE_SIZE),
3763 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003764out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003765 if (!same_inode)
3766 btrfs_double_inode_unlock(src, inode);
3767 else
Al Viro59551022016-01-22 15:40:57 -05003768 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003769 return ret;
3770}
3771
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003772int btrfs_clone_file_range(struct file *src_file, loff_t off,
3773 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003774{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003775 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003776}
3777
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003778static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3779{
Al Viro496ad9a2013-01-23 17:07:38 -05003780 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003781 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003782 struct btrfs_root *root = BTRFS_I(inode)->root;
3783 struct btrfs_root *new_root;
3784 struct btrfs_dir_item *di;
3785 struct btrfs_trans_handle *trans;
3786 struct btrfs_path *path;
3787 struct btrfs_key location;
3788 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003789 u64 objectid = 0;
3790 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003791 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003792
3793 if (!capable(CAP_SYS_ADMIN))
3794 return -EPERM;
3795
Miao Xie3c04ce02012-11-26 08:43:07 +00003796 ret = mnt_want_write_file(file);
3797 if (ret)
3798 return ret;
3799
3800 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3801 ret = -EFAULT;
3802 goto out;
3803 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003804
3805 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303806 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003807
3808 location.objectid = objectid;
3809 location.type = BTRFS_ROOT_ITEM_KEY;
3810 location.offset = (u64)-1;
3811
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003812 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003813 if (IS_ERR(new_root)) {
3814 ret = PTR_ERR(new_root);
3815 goto out;
3816 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09003817 if (!is_fstree(new_root->objectid)) {
3818 ret = -ENOENT;
3819 goto out;
3820 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003821
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003822 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003823 if (!path) {
3824 ret = -ENOMEM;
3825 goto out;
3826 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003827 path->leave_spinning = 1;
3828
3829 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003830 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003831 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003832 ret = PTR_ERR(trans);
3833 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003834 }
3835
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003836 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3837 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003838 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003839 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003840 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003841 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003842 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003843 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003844 ret = -ENOENT;
3845 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003846 }
3847
3848 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3849 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3850 btrfs_mark_buffer_dirty(path->nodes[0]);
3851 btrfs_free_path(path);
3852
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003853 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003854 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00003855out:
3856 mnt_drop_write_file(file);
3857 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003858}
3859
Su Yuec065f5b12018-04-02 17:24:11 +08003860static void get_block_group_info(struct list_head *groups_list,
3861 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003862{
3863 struct btrfs_block_group_cache *block_group;
3864
3865 space->total_bytes = 0;
3866 space->used_bytes = 0;
3867 space->flags = 0;
3868 list_for_each_entry(block_group, groups_list, list) {
3869 space->flags = block_group->flags;
3870 space->total_bytes += block_group->key.offset;
3871 space->used_bytes +=
3872 btrfs_block_group_used(&block_group->item);
3873 }
3874}
3875
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003876static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3877 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003878{
3879 struct btrfs_ioctl_space_args space_args;
3880 struct btrfs_ioctl_space_info space;
3881 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003882 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003883 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003884 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01003885 static const u64 types[] = {
3886 BTRFS_BLOCK_GROUP_DATA,
3887 BTRFS_BLOCK_GROUP_SYSTEM,
3888 BTRFS_BLOCK_GROUP_METADATA,
3889 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3890 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003891 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003892 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003893 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003894 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003895 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003896
3897 if (copy_from_user(&space_args,
3898 (struct btrfs_ioctl_space_args __user *)arg,
3899 sizeof(space_args)))
3900 return -EFAULT;
3901
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003902 for (i = 0; i < num_types; i++) {
3903 struct btrfs_space_info *tmp;
3904
3905 info = NULL;
3906 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003907 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003908 list) {
3909 if (tmp->flags == types[i]) {
3910 info = tmp;
3911 break;
3912 }
3913 }
3914 rcu_read_unlock();
3915
3916 if (!info)
3917 continue;
3918
3919 down_read(&info->groups_sem);
3920 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3921 if (!list_empty(&info->block_groups[c]))
3922 slot_count++;
3923 }
3924 up_read(&info->groups_sem);
3925 }
Josef Bacik1406e432010-01-13 18:19:06 +00003926
David Sterba36523e952014-02-07 14:34:12 +01003927 /*
3928 * Global block reserve, exported as a space_info
3929 */
3930 slot_count++;
3931
Chris Mason7fde62b2010-03-16 15:40:10 -04003932 /* space_slots == 0 means they are asking for a count */
3933 if (space_args.space_slots == 0) {
3934 space_args.total_spaces = slot_count;
3935 goto out;
3936 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003937
Dan Rosenberg51788b12011-02-14 16:04:23 -05003938 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003939
Chris Mason7fde62b2010-03-16 15:40:10 -04003940 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003941
Chris Mason7fde62b2010-03-16 15:40:10 -04003942 /* we generally have at most 6 or so space infos, one for each raid
3943 * level. So, a whole page should be more than enough for everyone
3944 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003945 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04003946 return -ENOMEM;
3947
3948 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01003949 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04003950 if (!dest)
3951 return -ENOMEM;
3952 dest_orig = dest;
3953
3954 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003955 for (i = 0; i < num_types; i++) {
3956 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003957
Dan Rosenberg51788b12011-02-14 16:04:23 -05003958 if (!slot_count)
3959 break;
3960
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003961 info = NULL;
3962 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003963 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003964 list) {
3965 if (tmp->flags == types[i]) {
3966 info = tmp;
3967 break;
3968 }
3969 }
3970 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003971
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003972 if (!info)
3973 continue;
3974 down_read(&info->groups_sem);
3975 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3976 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08003977 get_block_group_info(&info->block_groups[c],
3978 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003979 memcpy(dest, &space, sizeof(space));
3980 dest++;
3981 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003982 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003983 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003984 if (!slot_count)
3985 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003986 }
3987 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003988 }
Josef Bacik1406e432010-01-13 18:19:06 +00003989
David Sterba36523e952014-02-07 14:34:12 +01003990 /*
3991 * Add global block reserve
3992 */
3993 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003994 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01003995
3996 spin_lock(&block_rsv->lock);
3997 space.total_bytes = block_rsv->size;
3998 space.used_bytes = block_rsv->size - block_rsv->reserved;
3999 spin_unlock(&block_rsv->lock);
4000 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4001 memcpy(dest, &space, sizeof(space));
4002 space_args.total_spaces++;
4003 }
4004
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004005 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004006 (arg + sizeof(struct btrfs_ioctl_space_args));
4007
4008 if (copy_to_user(user_dest, dest_orig, alloc_size))
4009 ret = -EFAULT;
4010
4011 kfree(dest_orig);
4012out:
4013 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004014 ret = -EFAULT;
4015
4016 return ret;
4017}
4018
Miao Xie9a8c28b2012-11-26 08:40:43 +00004019static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4020 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004021{
Sage Weil46204592010-10-29 15:41:32 -04004022 struct btrfs_trans_handle *trans;
4023 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004024 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004025
Miao Xied4edf392013-02-20 09:17:06 +00004026 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004027 if (IS_ERR(trans)) {
4028 if (PTR_ERR(trans) != -ENOENT)
4029 return PTR_ERR(trans);
4030
4031 /* No running transaction, don't bother */
4032 transid = root->fs_info->last_trans_committed;
4033 goto out;
4034 }
Sage Weil46204592010-10-29 15:41:32 -04004035 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004036 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004037 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004038 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004039 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004040 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004041out:
Sage Weil46204592010-10-29 15:41:32 -04004042 if (argp)
4043 if (copy_to_user(argp, &transid, sizeof(transid)))
4044 return -EFAULT;
4045 return 0;
4046}
4047
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004048static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004049 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004050{
Sage Weil46204592010-10-29 15:41:32 -04004051 u64 transid;
4052
4053 if (argp) {
4054 if (copy_from_user(&transid, argp, sizeof(transid)))
4055 return -EFAULT;
4056 } else {
4057 transid = 0; /* current trans */
4058 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004059 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004060}
4061
Miao Xieb8e95482012-11-26 08:48:01 +00004062static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004063{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004064 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004065 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004066 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004067
4068 if (!capable(CAP_SYS_ADMIN))
4069 return -EPERM;
4070
4071 sa = memdup_user(arg, sizeof(*sa));
4072 if (IS_ERR(sa))
4073 return PTR_ERR(sa);
4074
Miao Xieb8e95482012-11-26 08:48:01 +00004075 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4076 ret = mnt_want_write_file(file);
4077 if (ret)
4078 goto out;
4079 }
4080
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004081 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004082 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4083 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004084
4085 if (copy_to_user(arg, sa, sizeof(*sa)))
4086 ret = -EFAULT;
4087
Miao Xieb8e95482012-11-26 08:48:01 +00004088 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4089 mnt_drop_write_file(file);
4090out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004091 kfree(sa);
4092 return ret;
4093}
4094
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004095static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004096{
4097 if (!capable(CAP_SYS_ADMIN))
4098 return -EPERM;
4099
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004100 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004101}
4102
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004103static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004104 void __user *arg)
4105{
4106 struct btrfs_ioctl_scrub_args *sa;
4107 int ret;
4108
4109 if (!capable(CAP_SYS_ADMIN))
4110 return -EPERM;
4111
4112 sa = memdup_user(arg, sizeof(*sa));
4113 if (IS_ERR(sa))
4114 return PTR_ERR(sa);
4115
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004116 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004117
4118 if (copy_to_user(arg, sa, sizeof(*sa)))
4119 ret = -EFAULT;
4120
4121 kfree(sa);
4122 return ret;
4123}
4124
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004125static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004126 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004127{
4128 struct btrfs_ioctl_get_dev_stats *sa;
4129 int ret;
4130
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004131 sa = memdup_user(arg, sizeof(*sa));
4132 if (IS_ERR(sa))
4133 return PTR_ERR(sa);
4134
David Sterbab27f7c02012-06-22 06:30:39 -06004135 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4136 kfree(sa);
4137 return -EPERM;
4138 }
4139
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004140 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004141
4142 if (copy_to_user(arg, sa, sizeof(*sa)))
4143 ret = -EFAULT;
4144
4145 kfree(sa);
4146 return ret;
4147}
4148
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004149static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4150 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004151{
4152 struct btrfs_ioctl_dev_replace_args *p;
4153 int ret;
4154
4155 if (!capable(CAP_SYS_ADMIN))
4156 return -EPERM;
4157
4158 p = memdup_user(arg, sizeof(*p));
4159 if (IS_ERR(p))
4160 return PTR_ERR(p);
4161
4162 switch (p->cmd) {
4163 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004164 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004165 ret = -EROFS;
4166 goto out;
4167 }
David Sterba171938e2017-03-28 14:44:21 +02004168 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004169 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004170 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004171 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004172 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004173 }
4174 break;
4175 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004176 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004177 ret = 0;
4178 break;
4179 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004180 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004181 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004182 break;
4183 default:
4184 ret = -EINVAL;
4185 break;
4186 }
4187
4188 if (copy_to_user(arg, p, sizeof(*p)))
4189 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004190out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004191 kfree(p);
4192 return ret;
4193}
4194
Jan Schmidtd7728c92011-07-07 16:48:38 +02004195static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4196{
4197 int ret = 0;
4198 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004199 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004200 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004201 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004202 struct inode_fs_paths *ipath = NULL;
4203 struct btrfs_path *path;
4204
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004205 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004206 return -EPERM;
4207
4208 path = btrfs_alloc_path();
4209 if (!path) {
4210 ret = -ENOMEM;
4211 goto out;
4212 }
4213
4214 ipa = memdup_user(arg, sizeof(*ipa));
4215 if (IS_ERR(ipa)) {
4216 ret = PTR_ERR(ipa);
4217 ipa = NULL;
4218 goto out;
4219 }
4220
4221 size = min_t(u32, ipa->size, 4096);
4222 ipath = init_ipath(size, root, path);
4223 if (IS_ERR(ipath)) {
4224 ret = PTR_ERR(ipath);
4225 ipath = NULL;
4226 goto out;
4227 }
4228
4229 ret = paths_from_inode(ipa->inum, ipath);
4230 if (ret < 0)
4231 goto out;
4232
4233 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004234 rel_ptr = ipath->fspath->val[i] -
4235 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004236 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004237 }
4238
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004239 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4240 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004241 if (ret) {
4242 ret = -EFAULT;
4243 goto out;
4244 }
4245
4246out:
4247 btrfs_free_path(path);
4248 free_ipath(ipath);
4249 kfree(ipa);
4250
4251 return ret;
4252}
4253
4254static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4255{
4256 struct btrfs_data_container *inodes = ctx;
4257 const size_t c = 3 * sizeof(u64);
4258
4259 if (inodes->bytes_left >= c) {
4260 inodes->bytes_left -= c;
4261 inodes->val[inodes->elem_cnt] = inum;
4262 inodes->val[inodes->elem_cnt + 1] = offset;
4263 inodes->val[inodes->elem_cnt + 2] = root;
4264 inodes->elem_cnt += 3;
4265 } else {
4266 inodes->bytes_missing += c - inodes->bytes_left;
4267 inodes->bytes_left = 0;
4268 inodes->elem_missed += 3;
4269 }
4270
4271 return 0;
4272}
4273
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004274static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004275 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004276{
4277 int ret = 0;
4278 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004279 struct btrfs_ioctl_logical_ino_args *loi;
4280 struct btrfs_data_container *inodes = NULL;
4281 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004282 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004283
4284 if (!capable(CAP_SYS_ADMIN))
4285 return -EPERM;
4286
4287 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304288 if (IS_ERR(loi))
4289 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004290
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004291 if (version == 1) {
4292 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004293 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004294 } else {
4295 /* All reserved bits must be 0 for now */
4296 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4297 ret = -EINVAL;
4298 goto out_loi;
4299 }
4300 /* Only accept flags we have defined so far */
4301 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4302 ret = -EINVAL;
4303 goto out_loi;
4304 }
4305 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004306 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004307 }
4308
Jan Schmidtd7728c92011-07-07 16:48:38 +02004309 path = btrfs_alloc_path();
4310 if (!path) {
4311 ret = -ENOMEM;
4312 goto out;
4313 }
4314
Jan Schmidtd7728c92011-07-07 16:48:38 +02004315 inodes = init_data_container(size);
4316 if (IS_ERR(inodes)) {
4317 ret = PTR_ERR(inodes);
4318 inodes = NULL;
4319 goto out;
4320 }
4321
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004322 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004323 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004324 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004325 ret = -ENOENT;
4326 if (ret < 0)
4327 goto out;
4328
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004329 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4330 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004331 if (ret)
4332 ret = -EFAULT;
4333
4334out:
4335 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004336 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004337out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004338 kfree(loi);
4339
4340 return ret;
4341}
4342
David Sterba008ef092018-03-21 02:05:27 +01004343void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004344 struct btrfs_ioctl_balance_args *bargs)
4345{
4346 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4347
4348 bargs->flags = bctl->flags;
4349
David Sterba3009a622018-03-21 01:31:04 +01004350 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004351 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4352 if (atomic_read(&fs_info->balance_pause_req))
4353 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004354 if (atomic_read(&fs_info->balance_cancel_req))
4355 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004356
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004357 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4358 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4359 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004360
David Sterba008ef092018-03-21 02:05:27 +01004361 spin_lock(&fs_info->balance_lock);
4362 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4363 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004364}
4365
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004366static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004367{
Al Viro496ad9a2013-01-23 17:07:38 -05004368 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004369 struct btrfs_fs_info *fs_info = root->fs_info;
4370 struct btrfs_ioctl_balance_args *bargs;
4371 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004372 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004373 int ret;
4374
4375 if (!capable(CAP_SYS_ADMIN))
4376 return -EPERM;
4377
Liu Boe54bfa312012-06-29 03:58:48 -06004378 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004379 if (ret)
4380 return ret;
4381
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004382again:
David Sterba171938e2017-03-28 14:44:21 +02004383 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004384 mutex_lock(&fs_info->balance_mutex);
4385 need_unlock = true;
4386 goto locked;
4387 }
4388
4389 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004390 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004391 * (1) some other op is running
4392 * (2) balance is running
4393 * (3) balance is paused -- special case (think resume)
4394 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004395 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004396 if (fs_info->balance_ctl) {
4397 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004398 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004399 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004400 /*
4401 * Lock released to allow other waiters to continue,
4402 * we'll reexamine the status again.
4403 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004404 mutex_lock(&fs_info->balance_mutex);
4405
4406 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004407 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004408 /* this is (3) */
4409 need_unlock = false;
4410 goto locked;
4411 }
4412
4413 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004414 goto again;
4415 } else {
4416 /* this is (2) */
4417 mutex_unlock(&fs_info->balance_mutex);
4418 ret = -EINPROGRESS;
4419 goto out;
4420 }
4421 } else {
4422 /* this is (1) */
4423 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004424 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004425 goto out;
4426 }
4427
4428locked:
David Sterba171938e2017-03-28 14:44:21 +02004429 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004430
4431 if (arg) {
4432 bargs = memdup_user(arg, sizeof(*bargs));
4433 if (IS_ERR(bargs)) {
4434 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004435 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004436 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004437
4438 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4439 if (!fs_info->balance_ctl) {
4440 ret = -ENOTCONN;
4441 goto out_bargs;
4442 }
4443
4444 bctl = fs_info->balance_ctl;
4445 spin_lock(&fs_info->balance_lock);
4446 bctl->flags |= BTRFS_BALANCE_RESUME;
4447 spin_unlock(&fs_info->balance_lock);
4448
4449 goto do_balance;
4450 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004451 } else {
4452 bargs = NULL;
4453 }
4454
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004455 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004456 ret = -EINPROGRESS;
4457 goto out_bargs;
4458 }
4459
David Sterba8d2db782015-11-04 15:38:29 +01004460 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004461 if (!bctl) {
4462 ret = -ENOMEM;
4463 goto out_bargs;
4464 }
4465
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004466 if (arg) {
4467 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4468 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4469 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4470
4471 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004472 } else {
4473 /* balance everything - no filters */
4474 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004475 }
4476
David Sterba8eb93452015-10-12 16:55:54 +02004477 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4478 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004479 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004480 }
4481
Ilya Dryomovde322262012-01-16 22:04:49 +02004482do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004483 /*
David Sterba149196a2018-03-20 20:23:09 +01004484 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4485 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4486 * restriper was paused all the way until unmount, in free_fs_info.
4487 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004488 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004489 need_unlock = false;
4490
David Sterba6fcf6e22018-05-07 17:44:03 +02004491 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004492 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004493
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004494 if (arg) {
4495 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4496 ret = -EFAULT;
4497 }
4498
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004499out_bctl:
4500 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004501out_bargs:
4502 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004503out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004504 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004505 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004506 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004507out:
Liu Boe54bfa312012-06-29 03:58:48 -06004508 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004509 return ret;
4510}
4511
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004512static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004513{
4514 if (!capable(CAP_SYS_ADMIN))
4515 return -EPERM;
4516
4517 switch (cmd) {
4518 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004519 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004520 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004521 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004522 }
4523
4524 return -EINVAL;
4525}
4526
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004527static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004528 void __user *arg)
4529{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004530 struct btrfs_ioctl_balance_args *bargs;
4531 int ret = 0;
4532
4533 if (!capable(CAP_SYS_ADMIN))
4534 return -EPERM;
4535
4536 mutex_lock(&fs_info->balance_mutex);
4537 if (!fs_info->balance_ctl) {
4538 ret = -ENOTCONN;
4539 goto out;
4540 }
4541
David Sterba8d2db782015-11-04 15:38:29 +01004542 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004543 if (!bargs) {
4544 ret = -ENOMEM;
4545 goto out;
4546 }
4547
David Sterba008ef092018-03-21 02:05:27 +01004548 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004549
4550 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4551 ret = -EFAULT;
4552
4553 kfree(bargs);
4554out:
4555 mutex_unlock(&fs_info->balance_mutex);
4556 return ret;
4557}
4558
Miao Xie905b0dd2012-11-26 08:50:11 +00004559static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004560{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004561 struct inode *inode = file_inode(file);
4562 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004563 struct btrfs_ioctl_quota_ctl_args *sa;
4564 struct btrfs_trans_handle *trans = NULL;
4565 int ret;
4566 int err;
4567
4568 if (!capable(CAP_SYS_ADMIN))
4569 return -EPERM;
4570
Miao Xie905b0dd2012-11-26 08:50:11 +00004571 ret = mnt_want_write_file(file);
4572 if (ret)
4573 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004574
4575 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004576 if (IS_ERR(sa)) {
4577 ret = PTR_ERR(sa);
4578 goto drop_write;
4579 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004580
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004581 down_write(&fs_info->subvol_sem);
4582 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004583 if (IS_ERR(trans)) {
4584 ret = PTR_ERR(trans);
4585 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004586 }
4587
4588 switch (sa->cmd) {
4589 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004590 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004591 break;
4592 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004593 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004594 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004595 default:
4596 ret = -EINVAL;
4597 break;
4598 }
4599
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004600 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004601 if (err && !ret)
4602 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004603out:
4604 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004605 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004606drop_write:
4607 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004608 return ret;
4609}
4610
Miao Xie905b0dd2012-11-26 08:50:11 +00004611static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004612{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004613 struct inode *inode = file_inode(file);
4614 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4615 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004616 struct btrfs_ioctl_qgroup_assign_args *sa;
4617 struct btrfs_trans_handle *trans;
4618 int ret;
4619 int err;
4620
4621 if (!capable(CAP_SYS_ADMIN))
4622 return -EPERM;
4623
Miao Xie905b0dd2012-11-26 08:50:11 +00004624 ret = mnt_want_write_file(file);
4625 if (ret)
4626 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004627
4628 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004629 if (IS_ERR(sa)) {
4630 ret = PTR_ERR(sa);
4631 goto drop_write;
4632 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004633
4634 trans = btrfs_join_transaction(root);
4635 if (IS_ERR(trans)) {
4636 ret = PTR_ERR(trans);
4637 goto out;
4638 }
4639
Arne Jansen5d13a372011-09-14 15:53:51 +02004640 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004641 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004642 sa->src, sa->dst);
4643 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004644 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004645 sa->src, sa->dst);
4646 }
4647
Qu Wenruoe082f562015-02-27 16:24:28 +08004648 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004649 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004650 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 btrfs_handle_fs_error(fs_info, err,
4652 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004653 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004654 if (err && !ret)
4655 ret = err;
4656
4657out:
4658 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004659drop_write:
4660 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004661 return ret;
4662}
4663
Miao Xie905b0dd2012-11-26 08:50:11 +00004664static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004665{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004666 struct inode *inode = file_inode(file);
4667 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4668 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004669 struct btrfs_ioctl_qgroup_create_args *sa;
4670 struct btrfs_trans_handle *trans;
4671 int ret;
4672 int err;
4673
4674 if (!capable(CAP_SYS_ADMIN))
4675 return -EPERM;
4676
Miao Xie905b0dd2012-11-26 08:50:11 +00004677 ret = mnt_want_write_file(file);
4678 if (ret)
4679 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004680
4681 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004682 if (IS_ERR(sa)) {
4683 ret = PTR_ERR(sa);
4684 goto drop_write;
4685 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004686
Miao Xied86e56c2012-11-15 11:35:41 +00004687 if (!sa->qgroupid) {
4688 ret = -EINVAL;
4689 goto out;
4690 }
4691
Arne Jansen5d13a372011-09-14 15:53:51 +02004692 trans = btrfs_join_transaction(root);
4693 if (IS_ERR(trans)) {
4694 ret = PTR_ERR(trans);
4695 goto out;
4696 }
4697
Arne Jansen5d13a372011-09-14 15:53:51 +02004698 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004699 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004700 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004701 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004702 }
4703
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004704 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004705 if (err && !ret)
4706 ret = err;
4707
4708out:
4709 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004710drop_write:
4711 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004712 return ret;
4713}
4714
Miao Xie905b0dd2012-11-26 08:50:11 +00004715static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004716{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004717 struct inode *inode = file_inode(file);
4718 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4719 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004720 struct btrfs_ioctl_qgroup_limit_args *sa;
4721 struct btrfs_trans_handle *trans;
4722 int ret;
4723 int err;
4724 u64 qgroupid;
4725
4726 if (!capable(CAP_SYS_ADMIN))
4727 return -EPERM;
4728
Miao Xie905b0dd2012-11-26 08:50:11 +00004729 ret = mnt_want_write_file(file);
4730 if (ret)
4731 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004732
4733 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004734 if (IS_ERR(sa)) {
4735 ret = PTR_ERR(sa);
4736 goto drop_write;
4737 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004738
4739 trans = btrfs_join_transaction(root);
4740 if (IS_ERR(trans)) {
4741 ret = PTR_ERR(trans);
4742 goto out;
4743 }
4744
4745 qgroupid = sa->qgroupid;
4746 if (!qgroupid) {
4747 /* take the current subvol as qgroup */
4748 qgroupid = root->root_key.objectid;
4749 }
4750
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004751 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004752
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004753 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004754 if (err && !ret)
4755 ret = err;
4756
4757out:
4758 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004759drop_write:
4760 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004761 return ret;
4762}
4763
Jan Schmidt2f232032013-04-25 16:04:51 +00004764static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4765{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004766 struct inode *inode = file_inode(file);
4767 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004768 struct btrfs_ioctl_quota_rescan_args *qsa;
4769 int ret;
4770
4771 if (!capable(CAP_SYS_ADMIN))
4772 return -EPERM;
4773
4774 ret = mnt_want_write_file(file);
4775 if (ret)
4776 return ret;
4777
4778 qsa = memdup_user(arg, sizeof(*qsa));
4779 if (IS_ERR(qsa)) {
4780 ret = PTR_ERR(qsa);
4781 goto drop_write;
4782 }
4783
4784 if (qsa->flags) {
4785 ret = -EINVAL;
4786 goto out;
4787 }
4788
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004789 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004790
4791out:
4792 kfree(qsa);
4793drop_write:
4794 mnt_drop_write_file(file);
4795 return ret;
4796}
4797
4798static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4799{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004800 struct inode *inode = file_inode(file);
4801 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004802 struct btrfs_ioctl_quota_rescan_args *qsa;
4803 int ret = 0;
4804
4805 if (!capable(CAP_SYS_ADMIN))
4806 return -EPERM;
4807
David Sterba8d2db782015-11-04 15:38:29 +01004808 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004809 if (!qsa)
4810 return -ENOMEM;
4811
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004812 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00004813 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004814 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004815 }
4816
4817 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4818 ret = -EFAULT;
4819
4820 kfree(qsa);
4821 return ret;
4822}
4823
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004824static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4825{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004826 struct inode *inode = file_inode(file);
4827 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004828
4829 if (!capable(CAP_SYS_ADMIN))
4830 return -EPERM;
4831
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004832 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004833}
4834
Hugo Millsabccd002014-01-30 20:17:00 +00004835static long _btrfs_ioctl_set_received_subvol(struct file *file,
4836 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004837{
Al Viro496ad9a2013-01-23 17:07:38 -05004838 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004839 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004840 struct btrfs_root *root = BTRFS_I(inode)->root;
4841 struct btrfs_root_item *root_item = &root->root_item;
4842 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004843 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004844 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004845 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004846
David Sterbabd60ea02014-01-16 15:50:22 +01004847 if (!inode_owner_or_capable(inode))
4848 return -EPERM;
4849
Alexander Block8ea05e32012-07-25 17:35:53 +02004850 ret = mnt_want_write_file(file);
4851 if (ret < 0)
4852 return ret;
4853
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004854 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004855
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004856 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004857 ret = -EINVAL;
4858 goto out;
4859 }
4860
4861 if (btrfs_root_readonly(root)) {
4862 ret = -EROFS;
4863 goto out;
4864 }
4865
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004866 /*
4867 * 1 - root item
4868 * 2 - uuid items (received uuid + subvol uuid)
4869 */
4870 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004871 if (IS_ERR(trans)) {
4872 ret = PTR_ERR(trans);
4873 trans = NULL;
4874 goto out;
4875 }
4876
4877 sa->rtransid = trans->transid;
4878 sa->rtime.sec = ct.tv_sec;
4879 sa->rtime.nsec = ct.tv_nsec;
4880
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004881 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4882 BTRFS_UUID_SIZE);
4883 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004884 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4885 ret = btrfs_uuid_tree_rem(trans, fs_info,
4886 root_item->received_uuid,
4887 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4888 root->root_key.objectid);
4889 if (ret && ret != -ENOENT) {
4890 btrfs_abort_transaction(trans, ret);
4891 btrfs_end_transaction(trans);
4892 goto out;
4893 }
4894 }
Alexander Block8ea05e32012-07-25 17:35:53 +02004895 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4896 btrfs_set_root_stransid(root_item, sa->stransid);
4897 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004898 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4899 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4900 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4901 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004902
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004903 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02004904 &root->root_key, &root->root_item);
4905 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004906 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004907 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004908 }
4909 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004910 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004911 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4912 root->root_key.objectid);
4913 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004914 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03004915 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004916 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004917 }
4918 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004919 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00004920out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00004922 mnt_drop_write_file(file);
4923 return ret;
4924}
4925
4926#ifdef CONFIG_64BIT
4927static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4928 void __user *arg)
4929{
4930 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4931 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4932 int ret = 0;
4933
4934 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304935 if (IS_ERR(args32))
4936 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00004937
David Sterba8d2db782015-11-04 15:38:29 +01004938 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03004939 if (!args64) {
4940 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00004941 goto out;
4942 }
4943
4944 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4945 args64->stransid = args32->stransid;
4946 args64->rtransid = args32->rtransid;
4947 args64->stime.sec = args32->stime.sec;
4948 args64->stime.nsec = args32->stime.nsec;
4949 args64->rtime.sec = args32->rtime.sec;
4950 args64->rtime.nsec = args32->rtime.nsec;
4951 args64->flags = args32->flags;
4952
4953 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4954 if (ret)
4955 goto out;
4956
4957 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4958 args32->stransid = args64->stransid;
4959 args32->rtransid = args64->rtransid;
4960 args32->stime.sec = args64->stime.sec;
4961 args32->stime.nsec = args64->stime.nsec;
4962 args32->rtime.sec = args64->rtime.sec;
4963 args32->rtime.nsec = args64->rtime.nsec;
4964 args32->flags = args64->flags;
4965
4966 ret = copy_to_user(arg, args32, sizeof(*args32));
4967 if (ret)
4968 ret = -EFAULT;
4969
4970out:
4971 kfree(args32);
4972 kfree(args64);
4973 return ret;
4974}
4975#endif
4976
4977static long btrfs_ioctl_set_received_subvol(struct file *file,
4978 void __user *arg)
4979{
4980 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4981 int ret = 0;
4982
4983 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304984 if (IS_ERR(sa))
4985 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004986
4987 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4988
4989 if (ret)
4990 goto out;
4991
Alexander Block8ea05e32012-07-25 17:35:53 +02004992 ret = copy_to_user(arg, sa, sizeof(*sa));
4993 if (ret)
4994 ret = -EFAULT;
4995
4996out:
4997 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02004998 return ret;
4999}
5000
jeff.liu867ab662013-01-05 02:48:01 +00005001static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5002{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005003 struct inode *inode = file_inode(file);
5004 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005005 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005006 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005007 char label[BTRFS_LABEL_SIZE];
5008
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005009 spin_lock(&fs_info->super_lock);
5010 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5011 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005012
5013 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005014
5015 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005016 btrfs_warn(fs_info,
5017 "label is too long, return the first %zu bytes",
5018 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005019 }
5020
jeff.liu867ab662013-01-05 02:48:01 +00005021 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005022
5023 return ret ? -EFAULT : 0;
5024}
5025
jeff.liua8bfd4a2013-01-05 02:48:08 +00005026static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5027{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005028 struct inode *inode = file_inode(file);
5029 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5030 struct btrfs_root *root = BTRFS_I(inode)->root;
5031 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005032 struct btrfs_trans_handle *trans;
5033 char label[BTRFS_LABEL_SIZE];
5034 int ret;
5035
5036 if (!capable(CAP_SYS_ADMIN))
5037 return -EPERM;
5038
5039 if (copy_from_user(label, arg, sizeof(label)))
5040 return -EFAULT;
5041
5042 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005043 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005044 "unable to set label with more than %d bytes",
5045 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005046 return -EINVAL;
5047 }
5048
5049 ret = mnt_want_write_file(file);
5050 if (ret)
5051 return ret;
5052
jeff.liua8bfd4a2013-01-05 02:48:08 +00005053 trans = btrfs_start_transaction(root, 0);
5054 if (IS_ERR(trans)) {
5055 ret = PTR_ERR(trans);
5056 goto out_unlock;
5057 }
5058
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005059 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005060 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005061 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005062 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005063
5064out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005065 mnt_drop_write_file(file);
5066 return ret;
5067}
5068
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005069#define INIT_FEATURE_FLAGS(suffix) \
5070 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5071 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5072 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5073
David Sterbad5131b62016-02-17 15:26:27 +01005074int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005075{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005076 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005077 INIT_FEATURE_FLAGS(SUPP),
5078 INIT_FEATURE_FLAGS(SAFE_SET),
5079 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5080 };
5081
5082 if (copy_to_user(arg, &features, sizeof(features)))
5083 return -EFAULT;
5084
5085 return 0;
5086}
5087
5088static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5089{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005090 struct inode *inode = file_inode(file);
5091 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5092 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005093 struct btrfs_ioctl_feature_flags features;
5094
5095 features.compat_flags = btrfs_super_compat_flags(super_block);
5096 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5097 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5098
5099 if (copy_to_user(arg, &features, sizeof(features)))
5100 return -EFAULT;
5101
5102 return 0;
5103}
5104
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005105static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005106 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005107 u64 change_mask, u64 flags, u64 supported_flags,
5108 u64 safe_set, u64 safe_clear)
5109{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005110 const char *type = btrfs_feature_set_names[set];
5111 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005112 u64 disallowed, unsupported;
5113 u64 set_mask = flags & change_mask;
5114 u64 clear_mask = ~flags & change_mask;
5115
5116 unsupported = set_mask & ~supported_flags;
5117 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005118 names = btrfs_printable_features(set, unsupported);
5119 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005120 btrfs_warn(fs_info,
5121 "this kernel does not support the %s feature bit%s",
5122 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005123 kfree(names);
5124 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005125 btrfs_warn(fs_info,
5126 "this kernel does not support %s bits 0x%llx",
5127 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005128 return -EOPNOTSUPP;
5129 }
5130
5131 disallowed = set_mask & ~safe_set;
5132 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005133 names = btrfs_printable_features(set, disallowed);
5134 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005135 btrfs_warn(fs_info,
5136 "can't set the %s feature bit%s while mounted",
5137 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005138 kfree(names);
5139 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005140 btrfs_warn(fs_info,
5141 "can't set %s bits 0x%llx while mounted",
5142 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005143 return -EPERM;
5144 }
5145
5146 disallowed = clear_mask & ~safe_clear;
5147 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005148 names = btrfs_printable_features(set, disallowed);
5149 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005150 btrfs_warn(fs_info,
5151 "can't clear the %s feature bit%s while mounted",
5152 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005153 kfree(names);
5154 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005155 btrfs_warn(fs_info,
5156 "can't clear %s bits 0x%llx while mounted",
5157 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005158 return -EPERM;
5159 }
5160
5161 return 0;
5162}
5163
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005164#define check_feature(fs_info, change_mask, flags, mask_base) \
5165check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005166 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5167 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5168 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5169
5170static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5171{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005172 struct inode *inode = file_inode(file);
5173 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5174 struct btrfs_root *root = BTRFS_I(inode)->root;
5175 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005176 struct btrfs_ioctl_feature_flags flags[2];
5177 struct btrfs_trans_handle *trans;
5178 u64 newflags;
5179 int ret;
5180
5181 if (!capable(CAP_SYS_ADMIN))
5182 return -EPERM;
5183
5184 if (copy_from_user(flags, arg, sizeof(flags)))
5185 return -EFAULT;
5186
5187 /* Nothing to do */
5188 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5189 !flags[0].incompat_flags)
5190 return 0;
5191
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005192 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005193 flags[1].compat_flags, COMPAT);
5194 if (ret)
5195 return ret;
5196
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005197 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005198 flags[1].compat_ro_flags, COMPAT_RO);
5199 if (ret)
5200 return ret;
5201
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005202 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005203 flags[1].incompat_flags, INCOMPAT);
5204 if (ret)
5205 return ret;
5206
David Sterba7ab19622016-05-04 11:32:00 +02005207 ret = mnt_want_write_file(file);
5208 if (ret)
5209 return ret;
5210
David Sterba8051aa12014-02-07 14:34:04 +01005211 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005212 if (IS_ERR(trans)) {
5213 ret = PTR_ERR(trans);
5214 goto out_drop_write;
5215 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005216
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005217 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005218 newflags = btrfs_super_compat_flags(super_block);
5219 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5220 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5221 btrfs_set_super_compat_flags(super_block, newflags);
5222
5223 newflags = btrfs_super_compat_ro_flags(super_block);
5224 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5225 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5226 btrfs_set_super_compat_ro_flags(super_block, newflags);
5227
5228 newflags = btrfs_super_incompat_flags(super_block);
5229 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5230 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5231 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005232 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005233
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005234 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005235out_drop_write:
5236 mnt_drop_write_file(file);
5237
5238 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005239}
5240
Josef Bacik2351f432017-09-27 10:43:13 -04005241static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5242{
5243 struct btrfs_ioctl_send_args *arg;
5244 int ret;
5245
5246 if (compat) {
5247#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5248 struct btrfs_ioctl_send_args_32 args32;
5249
5250 ret = copy_from_user(&args32, argp, sizeof(args32));
5251 if (ret)
5252 return -EFAULT;
5253 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5254 if (!arg)
5255 return -ENOMEM;
5256 arg->send_fd = args32.send_fd;
5257 arg->clone_sources_count = args32.clone_sources_count;
5258 arg->clone_sources = compat_ptr(args32.clone_sources);
5259 arg->parent_root = args32.parent_root;
5260 arg->flags = args32.flags;
5261 memcpy(arg->reserved, args32.reserved,
5262 sizeof(args32.reserved));
5263#else
5264 return -ENOTTY;
5265#endif
5266 } else {
5267 arg = memdup_user(argp, sizeof(*arg));
5268 if (IS_ERR(arg))
5269 return PTR_ERR(arg);
5270 }
5271 ret = btrfs_ioctl_send(file, arg);
5272 kfree(arg);
5273 return ret;
5274}
5275
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005276long btrfs_ioctl(struct file *file, unsigned int
5277 cmd, unsigned long arg)
5278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005279 struct inode *inode = file_inode(file);
5280 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5281 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005282 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005283
5284 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005285 case FS_IOC_GETFLAGS:
5286 return btrfs_ioctl_getflags(file, argp);
5287 case FS_IOC_SETFLAGS:
5288 return btrfs_ioctl_setflags(file, argp);
5289 case FS_IOC_GETVERSION:
5290 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005291 case FITRIM:
5292 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005293 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005294 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005295 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005296 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005297 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005298 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005299 case BTRFS_IOC_SUBVOL_CREATE_V2:
5300 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005301 case BTRFS_IOC_SNAP_DESTROY:
5302 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005303 case BTRFS_IOC_SUBVOL_GETFLAGS:
5304 return btrfs_ioctl_subvol_getflags(file, argp);
5305 case BTRFS_IOC_SUBVOL_SETFLAGS:
5306 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005307 case BTRFS_IOC_DEFAULT_SUBVOL:
5308 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005309 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005310 return btrfs_ioctl_defrag(file, NULL);
5311 case BTRFS_IOC_DEFRAG_RANGE:
5312 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005313 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005314 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005315 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005316 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005317 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005318 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005319 case BTRFS_IOC_RM_DEV_V2:
5320 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005321 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005322 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005323 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005324 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005325 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005326 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005327 case BTRFS_IOC_TREE_SEARCH:
5328 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005329 case BTRFS_IOC_TREE_SEARCH_V2:
5330 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005331 case BTRFS_IOC_INO_LOOKUP:
5332 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005333 case BTRFS_IOC_INO_PATHS:
5334 return btrfs_ioctl_ino_to_path(root, argp);
5335 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005336 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5337 case BTRFS_IOC_LOGICAL_INO_V2:
5338 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005339 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005340 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005341 case BTRFS_IOC_SYNC: {
5342 int ret;
5343
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005344 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005345 if (ret)
5346 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005347 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005348 /*
5349 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005350 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005351 * processing uncleaned subvols.
5352 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005353 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005354 return ret;
5355 }
Sage Weil46204592010-10-29 15:41:32 -04005356 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005357 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005358 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005359 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005360 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005361 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005362 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005363 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005364 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005365 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005366 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005367 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005368 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005369 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005370 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005371 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005372 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5373 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005374#ifdef CONFIG_64BIT
5375 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5376 return btrfs_ioctl_set_received_subvol_32(file, argp);
5377#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005378 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005379 return _btrfs_ioctl_send(file, argp, false);
5380#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5381 case BTRFS_IOC_SEND_32:
5382 return _btrfs_ioctl_send(file, argp, true);
5383#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005384 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005385 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005386 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005387 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005388 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005389 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005390 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005391 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005392 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005393 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005394 case BTRFS_IOC_QUOTA_RESCAN:
5395 return btrfs_ioctl_quota_rescan(file, argp);
5396 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5397 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005398 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5399 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005400 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005401 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005402 case BTRFS_IOC_GET_FSLABEL:
5403 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005404 case BTRFS_IOC_SET_FSLABEL:
5405 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005406 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005407 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005408 case BTRFS_IOC_GET_FEATURES:
5409 return btrfs_ioctl_get_features(file, argp);
5410 case BTRFS_IOC_SET_FEATURES:
5411 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005412 }
5413
5414 return -ENOTTY;
5415}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005416
5417#ifdef CONFIG_COMPAT
5418long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5419{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005420 /*
5421 * These all access 32-bit values anyway so no further
5422 * handling is necessary.
5423 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005424 switch (cmd) {
5425 case FS_IOC32_GETFLAGS:
5426 cmd = FS_IOC_GETFLAGS;
5427 break;
5428 case FS_IOC32_SETFLAGS:
5429 cmd = FS_IOC_SETFLAGS;
5430 break;
5431 case FS_IOC32_GETVERSION:
5432 cmd = FS_IOC_GETVERSION;
5433 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005434 }
5435
5436 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5437}
5438#endif