blob: c3100f3988e2258f4d4c52dc923cfaae629731ab [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
David Sterbae4202ac2018-03-26 19:51:16 +0200373/*
374 * Set the xflags from the internal inode flags. The remaining items of fsxattr
375 * are zeroed.
376 */
377static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
378{
379 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
380 struct fsxattr fa;
381
382 memset(&fa, 0, sizeof(fa));
383 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
384
385 if (copy_to_user(arg, &fa, sizeof(fa)))
386 return -EFAULT;
387
388 return 0;
389}
390
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200391static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
392{
Al Viro496ad9a2013-01-23 17:07:38 -0500393 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200394
395 return put_user(inode->i_generation, arg);
396}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397
Li Dongyangf7039b12011-03-24 10:24:28 +0000398static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
399{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400400 struct inode *inode = file_inode(file);
401 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000402 struct btrfs_device *device;
403 struct request_queue *q;
404 struct fstrim_range range;
405 u64 minlen = ULLONG_MAX;
406 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500407 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000408 int ret;
409
410 if (!capable(CAP_SYS_ADMIN))
411 return -EPERM;
412
Xiao Guangrong1f781602011-04-20 10:09:16 +0000413 rcu_read_lock();
414 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
415 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000416 if (!device->bdev)
417 continue;
418 q = bdev_get_queue(device->bdev);
419 if (blk_queue_discard(q)) {
420 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100421 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000422 minlen);
423 }
424 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000425 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200426
Li Dongyangf7039b12011-03-24 10:24:28 +0000427 if (!num_devices)
428 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000429 if (copy_from_user(&range, arg, sizeof(range)))
430 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000431 if (range.start > total_bytes ||
432 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200433 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000434
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200435 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000436 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400437 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000438 if (ret < 0)
439 return ret;
440
441 if (copy_to_user(arg, &range, sizeof(range)))
442 return -EFAULT;
443
444 return 0;
445}
446
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200447int btrfs_is_empty_uuid(u8 *uuid)
448{
Chris Mason46e0f662013-11-15 12:14:55 +0100449 int i;
450
451 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
452 if (uuid[i])
453 return 0;
454 }
455 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200456}
457
Miao Xied5c12072013-02-28 10:04:33 +0000458static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400459 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100460 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200461 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000462 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400463{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400464 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400465 struct btrfs_trans_handle *trans;
466 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100467 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400468 struct btrfs_inode_item *inode_item;
469 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000470 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400471 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000472 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700473 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900474 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400475 int ret;
476 int err;
477 u64 objectid;
478 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500479 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000480 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200481 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400482
David Sterba49a3c4d2016-03-24 17:49:22 +0100483 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
484 if (!root_item)
485 return -ENOMEM;
486
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400487 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400488 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100489 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000490
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800491 /*
492 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400493 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800494 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100495 if (btrfs_qgroup_level(objectid)) {
496 ret = -ENOSPC;
497 goto fail_free;
498 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800499
Miao Xied5c12072013-02-28 10:04:33 +0000500 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400501 /*
Miao Xied5c12072013-02-28 10:04:33 +0000502 * The same as the snapshot creation, please see the comment
503 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400504 */
Miao Xied5c12072013-02-28 10:04:33 +0000505 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200506 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000507 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100508 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400509
Miao Xied5c12072013-02-28 10:04:33 +0000510 trans = btrfs_start_transaction(root, 0);
511 if (IS_ERR(trans)) {
512 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100513 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100514 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000515 }
516 trans->block_rsv = &block_rsv;
517 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400518
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400519 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200520 if (ret)
521 goto fail;
522
David Sterba4d75f8a2014-06-15 01:54:12 +0200523 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400524 if (IS_ERR(leaf)) {
525 ret = PTR_ERR(leaf);
526 goto fail;
527 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400528
David Sterbab159fa22016-11-08 18:09:03 +0100529 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400530 btrfs_set_header_bytenr(leaf, leaf->start);
531 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400532 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400533 btrfs_set_header_owner(leaf, objectid);
534
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400535 write_extent_buffer_fsid(leaf, fs_info->fsid);
536 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400537 btrfs_mark_buffer_dirty(leaf);
538
David Sterba49a3c4d2016-03-24 17:49:22 +0100539 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800540 btrfs_set_stack_inode_generation(inode_item, 1);
541 btrfs_set_stack_inode_size(inode_item, 3);
542 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400543 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400544 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800545 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400546
David Sterba49a3c4d2016-03-24 17:49:22 +0100547 btrfs_set_root_flags(root_item, 0);
548 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800549 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000550
David Sterba49a3c4d2016-03-24 17:49:22 +0100551 btrfs_set_root_bytenr(root_item, leaf->start);
552 btrfs_set_root_generation(root_item, trans->transid);
553 btrfs_set_root_level(root_item, 0);
554 btrfs_set_root_refs(root_item, 1);
555 btrfs_set_root_used(root_item, leaf->len);
556 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557
David Sterba49a3c4d2016-03-24 17:49:22 +0100558 btrfs_set_root_generation_v2(root_item,
559 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200560 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100561 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
562 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
563 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
564 root_item->ctime = root_item->otime;
565 btrfs_set_root_ctransid(root_item, trans->transid);
566 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567
Chris Mason925baed2008-06-25 16:01:30 -0400568 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400569 free_extent_buffer(leaf);
570 leaf = NULL;
571
David Sterba49a3c4d2016-03-24 17:49:22 +0100572 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400573
574 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400575 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200576 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400577 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100578 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400579 if (ret)
580 goto fail;
581
Yan, Zheng76dda932009-09-21 16:00:26 -0400582 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400583 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100584 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400586 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100587 goto fail;
588 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400589
590 btrfs_record_root_in_trans(trans, new_root);
591
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000592 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700593 if (ret) {
594 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400595 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700596 goto fail;
597 }
598
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530599 mutex_lock(&new_root->objectid_mutex);
600 new_root->highest_objectid = new_dirid;
601 mutex_unlock(&new_root->objectid_mutex);
602
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400603 /*
604 * insert the directory item
605 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200606 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100607 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400608 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100609 goto fail;
610 }
Chris Mason3de45862008-11-17 21:02:50 -0500611
612 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200613 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500614 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100615 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400616 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400617 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100618 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500619
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200620 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500621 ret = btrfs_update_inode(trans, root, dir);
622 BUG_ON(ret);
623
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400624 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400625 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200626 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500627 BUG_ON(ret);
628
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400629 ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400630 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200631 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400632 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200633
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100635 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000636 trans->block_rsv = NULL;
637 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100638 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800639
Sage Weil72fd0322010-10-29 15:41:32 -0400640 if (async_transid) {
641 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400642 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000643 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400644 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400645 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400646 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400647 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400648 if (err && !ret)
649 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500650
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900651 if (!ret) {
652 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800653 if (IS_ERR(inode))
654 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900655 d_instantiate(dentry, inode);
656 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400657 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100658
659fail_free:
660 kfree(root_item);
661 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400662}
663
Miao Xiee9662f72013-02-28 10:01:15 +0000664static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100665 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000666 u64 *async_transid, bool readonly,
667 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400668{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000670 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400671 struct btrfs_pending_snapshot *pending_snapshot;
672 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000673 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400674
Miao Xie27cdeb72014-04-02 19:51:05 +0800675 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400676 return -EINVAL;
677
David Sterba23269bf2017-02-13 11:03:44 +0100678 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100679 if (!pending_snapshot)
680 return -ENOMEM;
681
David Sterbab0c0ea62015-11-10 18:54:00 +0100682 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100683 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100684 pending_snapshot->path = btrfs_alloc_path();
685 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100686 ret = -ENOMEM;
687 goto free_pending;
688 }
689
David Sterbaea14b57f2017-06-22 02:19:11 +0200690 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100691 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600692 /* wait for no snapshot writes */
693 wait_event(root->subv_writers->wait,
694 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800695
Nikolay Borisov76f32e22018-04-23 10:54:14 +0300696 ret = btrfs_start_delalloc_inodes(root);
Miao Xie6a038432013-05-15 07:48:24 +0000697 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100698 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000699
Chris Mason6374e57a2017-06-23 09:48:21 -0700700 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000701
Miao Xie66d8f3d2012-09-06 04:02:28 -0600702 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
703 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000704 /*
705 * 1 - parent dir inode
706 * 2 - dir entries
707 * 1 - root item
708 * 2 - root ref/backref
709 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200710 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000711 */
712 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200713 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400714 &pending_snapshot->qgroup_reserved,
715 false);
Miao Xied5c12072013-02-28 10:04:33 +0000716 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100717 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000718
Yan, Zhenga22285a2010-05-16 10:48:46 -0400719 pending_snapshot->dentry = dentry;
720 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800721 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000722 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000723 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400724
Miao Xied5c12072013-02-28 10:04:33 +0000725 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400726 if (IS_ERR(trans)) {
727 ret = PTR_ERR(trans);
728 goto fail;
729 }
730
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400731 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400732 list_add(&pending_snapshot->list,
733 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400734 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400735 if (async_transid) {
736 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400737 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000738 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400739 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400740 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400741 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400742 }
Miao Xieaec80302013-03-04 09:44:29 +0000743 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400744 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400745
746 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400747 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000748 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749
Chris Masond3797302014-10-15 13:50:56 -0700750 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
751 if (ret)
752 goto fail;
753
David Howells2b0143b2015-03-17 22:25:59 +0000754 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000755 if (IS_ERR(inode)) {
756 ret = PTR_ERR(inode);
757 goto fail;
758 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900759
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000760 d_instantiate(dentry, inode);
761 ret = 0;
762fail:
David Sterba7775c812017-02-10 19:18:18 +0100763 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100764dec_and_free:
David Sterbaea14b57f2017-06-22 02:19:11 +0200765 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100766 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100767free_pending:
768 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100769 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100770 kfree(pending_snapshot);
771
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400772 return ret;
773}
774
Sage Weil4260f7c2010-10-29 15:46:43 -0400775/* copy of may_delete in fs/namei.c()
776 * Check whether we can remove a link victim from directory dir, check
777 * whether the type of victim is right.
778 * 1. We can't do it if dir is read-only (done in permission())
779 * 2. We should have write and exec permissions on dir
780 * 3. We can't remove anything from append-only dir
781 * 4. We can't do anything with immutable dir (done in permission())
782 * 5. If the sticky bit on dir is set we should either
783 * a. be owner of dir, or
784 * b. be owner of victim, or
785 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400786 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400787 * links pointing to it.
788 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
789 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
790 * 9. We can't remove a root or mountpoint.
791 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
792 * nfs_async_unlink().
793 */
794
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530795static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400796{
797 int error;
798
David Howells2b0143b2015-03-17 22:25:59 +0000799 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400800 return -ENOENT;
801
David Howells2b0143b2015-03-17 22:25:59 +0000802 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400803 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400804
805 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
806 if (error)
807 return error;
808 if (IS_APPEND(dir))
809 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000810 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
811 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400812 return -EPERM;
813 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000814 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400815 return -ENOTDIR;
816 if (IS_ROOT(victim))
817 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000818 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400819 return -EISDIR;
820 if (IS_DEADDIR(dir))
821 return -ENOENT;
822 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
823 return -EBUSY;
824 return 0;
825}
826
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400827/* copy of may_create in fs/namei.c() */
828static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
829{
David Howells2b0143b2015-03-17 22:25:59 +0000830 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400831 return -EEXIST;
832 if (IS_DEADDIR(dir))
833 return -ENOENT;
834 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
835}
836
837/*
838 * Create a new subvolume below @parent. This is largely modeled after
839 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
840 * inside this filesystem so it's quite a bit simpler.
841 */
Al Viro92872092016-11-20 19:34:31 -0500842static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100843 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400844 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200845 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000846 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400847{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400848 struct inode *dir = d_inode(parent->dentry);
849 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400850 struct dentry *dentry;
851 int error;
852
Al Viro00235412016-05-26 00:05:12 -0400853 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
854 if (error == -EINTR)
855 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400856
857 dentry = lookup_one_len(name, parent->dentry, namelen);
858 error = PTR_ERR(dentry);
859 if (IS_ERR(dentry))
860 goto out_unlock;
861
Yan, Zheng76dda932009-09-21 16:00:26 -0400862 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400863 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600864 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400865
Chris Mason9c520572012-12-17 14:26:57 -0500866 /*
867 * even if this name doesn't exist, we may get hash collisions.
868 * check for them now when we can safely fail
869 */
870 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
871 dir->i_ino, name,
872 namelen);
873 if (error)
874 goto out_dput;
875
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400876 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400877
878 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
879 goto out_up_read;
880
Chris Mason3de45862008-11-17 21:02:50 -0500881 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100882 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200883 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500884 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000885 error = create_subvol(dir, dentry, name, namelen,
886 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500887 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400888 if (!error)
889 fsnotify_mkdir(dir, dentry);
890out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400891 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400892out_dput:
893 dput(dentry);
894out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500895 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400896 return error;
897}
898
Chris Mason4cb53002011-05-24 15:35:30 -0400899/*
900 * When we're defragging a range, we don't want to kick it off again
901 * if it is really just waiting for delalloc to send it down.
902 * If we find a nice big extent or delalloc range for the bytes in the
903 * file you want to defrag, we return 0 to let you know to skip this
904 * part of the file
905 */
David Sterbaaab110a2014-07-29 17:32:10 +0200906static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400907{
908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
909 struct extent_map *em = NULL;
910 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
911 u64 end;
912
913 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300914 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -0400915 read_unlock(&em_tree->lock);
916
917 if (em) {
918 end = extent_map_end(em);
919 free_extent_map(em);
920 if (end - offset > thresh)
921 return 0;
922 }
923 /* if we already have a nice delalloc here, just stop */
924 thresh /= 2;
925 end = count_range_bits(io_tree, &offset, offset + thresh,
926 thresh, EXTENT_DELALLOC, 1);
927 if (end >= thresh)
928 return 0;
929 return 1;
930}
931
932/*
933 * helper function to walk through a file and find extents
934 * newer than a specific transid, and smaller than thresh.
935 *
936 * This is used by the defragging code to find new and small
937 * extents
938 */
939static int find_new_extents(struct btrfs_root *root,
940 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200941 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400942{
943 struct btrfs_path *path;
944 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400945 struct extent_buffer *leaf;
946 struct btrfs_file_extent_item *extent;
947 int type;
948 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200949 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -0400950
951 path = btrfs_alloc_path();
952 if (!path)
953 return -ENOMEM;
954
David Sterbaa4689d22011-05-31 17:08:14 +0000955 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400956 min_key.type = BTRFS_EXTENT_DATA_KEY;
957 min_key.offset = *off;
958
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530959 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100960 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400961 if (ret != 0)
962 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000963process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000964 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400965 goto none;
966 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
967 goto none;
968
969 leaf = path->nodes[0];
970 extent = btrfs_item_ptr(leaf, path->slots[0],
971 struct btrfs_file_extent_item);
972
973 type = btrfs_file_extent_type(leaf, extent);
974 if (type == BTRFS_FILE_EXTENT_REG &&
975 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
976 check_defrag_in_cache(inode, min_key.offset, thresh)) {
977 *off = min_key.offset;
978 btrfs_free_path(path);
979 return 0;
980 }
981
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000982 path->slots[0]++;
983 if (path->slots[0] < btrfs_header_nritems(leaf)) {
984 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
985 goto process_slot;
986 }
987
Chris Mason4cb53002011-05-24 15:35:30 -0400988 if (min_key.offset == (u64)-1)
989 goto none;
990
991 min_key.offset++;
992 btrfs_release_path(path);
993 }
994none:
995 btrfs_free_path(path);
996 return -ENOENT;
997}
998
Li Zefan6c282eb2012-06-11 16:03:35 +0800999static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001000{
1001 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001002 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001003 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001004 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001005
1006 /*
1007 * hopefully we have this extent in the tree already, try without
1008 * the full extent lock
1009 */
1010 read_lock(&em_tree->lock);
1011 em = lookup_extent_mapping(em_tree, start, len);
1012 read_unlock(&em_tree->lock);
1013
1014 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001015 struct extent_state *cached = NULL;
1016 u64 end = start + len - 1;
1017
Chris Mason940100a2010-03-10 10:52:59 -05001018 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001019 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02001020 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001021 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001022
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001023 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001024 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001025 }
1026
Li Zefan6c282eb2012-06-11 16:03:35 +08001027 return em;
1028}
1029
1030static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1031{
1032 struct extent_map *next;
1033 bool ret = true;
1034
1035 /* this is the last extent */
1036 if (em->start + em->len >= i_size_read(inode))
1037 return false;
1038
1039 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001040 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1041 ret = false;
1042 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001043 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001044 ret = false;
1045
1046 free_extent_map(next);
1047 return ret;
1048}
1049
David Sterbaaab110a2014-07-29 17:32:10 +02001050static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001051 u64 *last_len, u64 *skip, u64 *defrag_end,
1052 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001053{
1054 struct extent_map *em;
1055 int ret = 1;
1056 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001057 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001058
1059 /*
1060 * make sure that once we start defragging an extent, we keep on
1061 * defragging it
1062 */
1063 if (start < *defrag_end)
1064 return 1;
1065
1066 *skip = 0;
1067
1068 em = defrag_lookup_extent(inode, start);
1069 if (!em)
1070 return 0;
1071
Chris Mason940100a2010-03-10 10:52:59 -05001072 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001073 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001074 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001075 goto out;
1076 }
1077
Liu Bo4a3560c2015-08-07 16:48:41 +08001078 if (!*defrag_end)
1079 prev_mergeable = false;
1080
Li Zefan6c282eb2012-06-11 16:03:35 +08001081 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001082 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001083 * we hit a real extent, if it is big or the next extent is not a
1084 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001085 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001086 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001087 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001088 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001089out:
Chris Mason940100a2010-03-10 10:52:59 -05001090 /*
1091 * last_len ends up being a counter of how many bytes we've defragged.
1092 * every time we choose not to defrag an extent, we reset *last_len
1093 * so that the next tiny extent will force a defrag.
1094 *
1095 * The end result of this is that tiny extents before a single big
1096 * extent will force at least part of that big extent to be defragged.
1097 */
1098 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001099 *defrag_end = extent_map_end(em);
1100 } else {
1101 *last_len = 0;
1102 *skip = extent_map_end(em);
1103 *defrag_end = 0;
1104 }
1105
1106 free_extent_map(em);
1107 return ret;
1108}
1109
Chris Mason4cb53002011-05-24 15:35:30 -04001110/*
1111 * it doesn't do much good to defrag one or two pages
1112 * at a time. This pulls in a nice chunk of pages
1113 * to COW and defrag.
1114 *
1115 * It also makes sure the delalloc code has enough
1116 * dirty data to avoid making new small extents as part
1117 * of the defrag
1118 *
1119 * It's a good idea to start RA on this range
1120 * before calling this.
1121 */
1122static int cluster_pages_for_defrag(struct inode *inode,
1123 struct page **pages,
1124 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001125 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001126{
Chris Mason4cb53002011-05-24 15:35:30 -04001127 unsigned long file_end;
1128 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001129 u64 page_start;
1130 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001131 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001132 int ret;
1133 int i;
1134 int i_done;
1135 struct btrfs_ordered_extent *ordered;
1136 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001137 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001138 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001139 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001140
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001141 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001142 if (!isize || start_index > file_end)
1143 return 0;
1144
1145 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001146
Qu Wenruo364ecf32017-02-27 15:10:38 +08001147 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001148 start_index << PAGE_SHIFT,
1149 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001150 if (ret)
1151 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001152 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001153 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001154
1155 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001156 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001157 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001158again:
Josef Bacika94733d2011-07-11 10:47:06 -04001159 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001160 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001161 if (!page)
1162 break;
1163
Miao Xie600a45e2012-02-16 15:01:24 +08001164 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001165 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001166 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001167 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001168 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001169 ordered = btrfs_lookup_ordered_extent(inode,
1170 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001171 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001172 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001173 if (!ordered)
1174 break;
1175
1176 unlock_page(page);
1177 btrfs_start_ordered_extent(inode, ordered, 1);
1178 btrfs_put_ordered_extent(ordered);
1179 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001180 /*
1181 * we unlocked the page above, so we need check if
1182 * it was released or not.
1183 */
1184 if (page->mapping != inode->i_mapping) {
1185 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001186 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001187 goto again;
1188 }
Miao Xie600a45e2012-02-16 15:01:24 +08001189 }
1190
Chris Mason4cb53002011-05-24 15:35:30 -04001191 if (!PageUptodate(page)) {
1192 btrfs_readpage(NULL, page);
1193 lock_page(page);
1194 if (!PageUptodate(page)) {
1195 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001196 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001197 ret = -EIO;
1198 break;
1199 }
1200 }
Miao Xie600a45e2012-02-16 15:01:24 +08001201
Miao Xie600a45e2012-02-16 15:01:24 +08001202 if (page->mapping != inode->i_mapping) {
1203 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001204 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001205 goto again;
1206 }
1207
Chris Mason4cb53002011-05-24 15:35:30 -04001208 pages[i] = page;
1209 i_done++;
1210 }
1211 if (!i_done || ret)
1212 goto out;
1213
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001214 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001215 goto out;
1216
1217 /*
1218 * so now we have a nice long stream of locked
1219 * and up to date pages, lets wait on them
1220 */
1221 for (i = 0; i < i_done; i++)
1222 wait_on_page_writeback(pages[i]);
1223
1224 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001225 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001226
1227 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001228 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001229 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1230 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001231 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001232 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001233
Liu Bo1f12bd02012-03-29 09:57:44 -04001234 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001235 spin_lock(&BTRFS_I(inode)->lock);
1236 BTRFS_I(inode)->outstanding_extents++;
1237 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001238 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001239 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001240 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001241 }
1242
1243
Liu Bo9e8a4a82012-09-05 19:10:51 -06001244 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001245 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001246
1247 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001248 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001249
1250 for (i = 0; i < i_done; i++) {
1251 clear_page_dirty_for_io(pages[i]);
1252 ClearPageChecked(pages[i]);
1253 set_page_extent_mapped(pages[i]);
1254 set_page_dirty(pages[i]);
1255 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001256 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001257 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001258 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1259 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001260 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001261 return i_done;
1262out:
1263 for (i = 0; i < i_done; i++) {
1264 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001265 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001266 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001267 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001268 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001269 page_cnt << PAGE_SHIFT, true);
1270 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1271 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001272 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001273 return ret;
1274
1275}
1276
1277int btrfs_defrag_file(struct inode *inode, struct file *file,
1278 struct btrfs_ioctl_defrag_range_args *range,
1279 u64 newer_than, unsigned long max_to_defrag)
1280{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001282 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001283 struct file_ra_state *ra = NULL;
1284 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001285 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001286 u64 last_len = 0;
1287 u64 skip = 0;
1288 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001289 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001290 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001291 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001292 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001293 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001294 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001295 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001296 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001297 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001298 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001299 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001300 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001301
Liu Bo0abd5b12013-04-16 09:20:28 +00001302 if (isize == 0)
1303 return 0;
1304
1305 if (range->start >= isize)
1306 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001307
David Sterba1e2ef462017-07-17 20:01:59 +02001308 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001309 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1310 return -EINVAL;
1311 if (range->compress_type)
1312 compress_type = range->compress_type;
1313 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001314
Liu Bo0abd5b12013-04-16 09:20:28 +00001315 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001316 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001317
Chris Mason4cb53002011-05-24 15:35:30 -04001318 /*
David Sterba0a52d102017-06-22 03:22:58 +02001319 * If we were not given a file, allocate a readahead context. As
1320 * readahead is just an optimization, defrag will work without it so
1321 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001322 */
1323 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001324 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001325 if (ra)
1326 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001327 } else {
1328 ra = &file->f_ra;
1329 }
1330
David Sterba63e727e2017-06-22 03:13:02 +02001331 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001332 if (!pages) {
1333 ret = -ENOMEM;
1334 goto out_ra;
1335 }
1336
1337 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001338 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001339 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001340 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001341 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001342 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001343 }
1344
Chris Mason4cb53002011-05-24 15:35:30 -04001345 if (newer_than) {
1346 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001347 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001348 if (!ret) {
1349 range->start = newer_off;
1350 /*
1351 * we always align our defrag to help keep
1352 * the extents in the file evenly spaced
1353 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001354 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001355 } else
1356 goto out_ra;
1357 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001358 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001359 }
1360 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301361 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001362
Li Zefan2a0f7f52011-10-10 15:43:34 -04001363 /*
1364 * make writeback starts from i, so the defrag range can be
1365 * written sequentially.
1366 */
1367 if (i < inode->i_mapping->writeback_index)
1368 inode->i_mapping->writeback_index = i;
1369
Chris Masonf7f43cc2011-10-11 11:41:40 -04001370 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001371 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001372 /*
1373 * make sure we stop running if someone unmounts
1374 * the FS
1375 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001376 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001377 break;
1378
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001379 if (btrfs_defrag_cancelled(fs_info)) {
1380 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001381 ret = -EAGAIN;
1382 break;
1383 }
1384
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001385 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001386 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001387 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001388 unsigned long next;
1389 /*
1390 * the should_defrag function tells us how much to skip
1391 * bump our counter by the suggested amount
1392 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001393 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001394 i = max(i + 1, next);
1395 continue;
1396 }
Li Zefan008873e2011-09-02 15:57:07 +08001397
1398 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001399 cluster = (PAGE_ALIGN(defrag_end) >>
1400 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001401 cluster = min(cluster, max_cluster);
1402 } else {
1403 cluster = max_cluster;
1404 }
1405
Li Zefan008873e2011-09-02 15:57:07 +08001406 if (i + cluster > ra_index) {
1407 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001408 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001409 page_cache_sync_readahead(inode->i_mapping, ra,
1410 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301411 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001412 }
Chris Mason940100a2010-03-10 10:52:59 -05001413
Al Viro59551022016-01-22 15:40:57 -05001414 inode_lock(inode);
David Sterba1e2ef462017-07-17 20:01:59 +02001415 if (do_compress)
David Sterbaeec63c62017-07-17 19:41:31 +02001416 BTRFS_I(inode)->defrag_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001417 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001418 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001419 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001420 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001421 }
Chris Mason940100a2010-03-10 10:52:59 -05001422
Chris Mason4cb53002011-05-24 15:35:30 -04001423 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001424 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001425 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001426
1427 if (newer_than) {
1428 if (newer_off == (u64)-1)
1429 break;
1430
Liu Boe1f041e2012-03-29 09:57:45 -04001431 if (ret > 0)
1432 i += ret;
1433
Chris Mason4cb53002011-05-24 15:35:30 -04001434 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001435 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001436
Byongho Leeee221842015-12-15 01:42:10 +09001437 ret = find_new_extents(root, inode, newer_than,
1438 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001439 if (!ret) {
1440 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001441 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001442 } else {
1443 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001444 }
Chris Mason4cb53002011-05-24 15:35:30 -04001445 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001446 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001447 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001448 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001449 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001450 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001451 last_len = 0;
1452 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001453 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001454 }
1455
Filipe Mananadec8ef92014-03-01 10:55:54 +00001456 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001457 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001458 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1459 &BTRFS_I(inode)->runtime_flags))
1460 filemap_flush(inode->i_mapping);
1461 }
Chris Mason1e701a32010-03-11 09:42:04 -05001462
Li Zefan1a419d82010-10-25 15:12:50 +08001463 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001464 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001465 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1466 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001467 }
1468
Diego Calleja60ccf822011-09-01 16:33:57 +02001469 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001470
Chris Mason4cb53002011-05-24 15:35:30 -04001471out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001472 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001473 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001474 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001475 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001476 }
Chris Mason4cb53002011-05-24 15:35:30 -04001477 if (!file)
1478 kfree(ra);
1479 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001480 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001481}
1482
Miao Xie198605a2012-11-26 08:43:45 +00001483static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001484 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001485{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001486 struct inode *inode = file_inode(file);
1487 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001488 u64 new_size;
1489 u64 old_size;
1490 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001491 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001492 struct btrfs_ioctl_vol_args *vol_args;
1493 struct btrfs_trans_handle *trans;
1494 struct btrfs_device *device = NULL;
1495 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001496 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001497 char *devstr = NULL;
1498 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001499 int mod = 0;
1500
Chris Masone441d542009-01-05 16:57:23 -05001501 if (!capable(CAP_SYS_ADMIN))
1502 return -EPERM;
1503
Miao Xie198605a2012-11-26 08:43:45 +00001504 ret = mnt_want_write_file(file);
1505 if (ret)
1506 return ret;
1507
David Sterba171938e2017-03-28 14:44:21 +02001508 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001509 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001510 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001511 }
1512
Li Zefandae7b662009-04-08 15:06:54 +08001513 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001514 if (IS_ERR(vol_args)) {
1515 ret = PTR_ERR(vol_args);
1516 goto out;
1517 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001518
1519 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001521 sizestr = vol_args->name;
1522 devstr = strchr(sizestr, ':');
1523 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001524 sizestr = devstr + 1;
1525 *devstr = '\0';
1526 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001527 ret = kstrtoull(devstr, 10, &devid);
1528 if (ret)
1529 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001530 if (!devid) {
1531 ret = -EINVAL;
1532 goto out_free;
1533 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001534 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001535 }
Miao Xiedba60f32012-12-21 09:19:51 +00001536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001537 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001538 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001539 btrfs_info(fs_info, "resizer unable to find device %llu",
1540 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001541 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001542 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001543 }
Miao Xiedba60f32012-12-21 09:19:51 +00001544
Anand Jainebbede42017-12-04 12:54:52 +08001545 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001546 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001547 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001548 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001549 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001550 goto out_free;
1551 }
1552
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001553 if (!strcmp(sizestr, "max"))
1554 new_size = device->bdev->bd_inode->i_size;
1555 else {
1556 if (sizestr[0] == '-') {
1557 mod = -1;
1558 sizestr++;
1559 } else if (sizestr[0] == '+') {
1560 mod = 1;
1561 sizestr++;
1562 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001563 new_size = memparse(sizestr, &retptr);
1564 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001565 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001566 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001567 }
1568 }
1569
Anand Jain401e29c2017-12-04 12:54:55 +08001570 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001571 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001572 goto out_free;
1573 }
1574
Miao Xie7cc8e582014-09-03 21:35:38 +08001575 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001576
1577 if (mod < 0) {
1578 if (new_size > old_size) {
1579 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001580 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001581 }
1582 new_size = old_size - new_size;
1583 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001584 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001585 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001586 goto out_free;
1587 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001588 new_size = old_size + new_size;
1589 }
1590
Byongho Leeee221842015-12-15 01:42:10 +09001591 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001592 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001593 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001594 }
1595 if (new_size > device->bdev->bd_inode->i_size) {
1596 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001597 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001598 }
1599
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001600 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001601
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001602 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1603 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001604
1605 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001606 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001607 if (IS_ERR(trans)) {
1608 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001609 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001610 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001611 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001612 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001613 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001615 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001617out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001618 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001619out:
David Sterba171938e2017-03-28 14:44:21 +02001620 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001621 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001622 return ret;
1623}
1624
Sage Weil72fd0322010-10-29 15:41:32 -04001625static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001626 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001627 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001628 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001629{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001630 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001631 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001632
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001633 if (!S_ISDIR(file_inode(file)->i_mode))
1634 return -ENOTDIR;
1635
Liu Boa874a632012-06-29 03:58:46 -06001636 ret = mnt_want_write_file(file);
1637 if (ret)
1638 goto out;
1639
Sage Weil72fd0322010-10-29 15:41:32 -04001640 namelen = strlen(name);
1641 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001642 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001643 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001644 }
1645
Chris Mason16780ca2012-02-20 22:14:55 -05001646 if (name[0] == '.' &&
1647 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1648 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001649 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001650 }
1651
Chris Mason3de45862008-11-17 21:02:50 -05001652 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001653 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001654 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001655 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001656 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001657 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001658 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001659 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001660 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001661 }
1662
Al Viro496ad9a2013-01-23 17:07:38 -05001663 src_inode = file_inode(src.file);
1664 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001665 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001666 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001667 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001668 } else if (!inode_owner_or_capable(src_inode)) {
1669 /*
1670 * Subvolume creation is not restricted, but snapshots
1671 * are limited to own subvolumes only
1672 */
1673 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001674 } else {
1675 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1676 BTRFS_I(src_inode)->root,
1677 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001678 }
Al Viro2903ff02012-08-28 12:52:22 -04001679 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001680 }
Liu Boa874a632012-06-29 03:58:46 -06001681out_drop_write:
1682 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001683out:
Sage Weil72fd0322010-10-29 15:41:32 -04001684 return ret;
1685}
1686
1687static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001688 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001689{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001690 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001691 int ret;
1692
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001693 if (!S_ISDIR(file_inode(file)->i_mode))
1694 return -ENOTDIR;
1695
Li Zefanfa0d2b92010-12-20 15:53:28 +08001696 vol_args = memdup_user(arg, sizeof(*vol_args));
1697 if (IS_ERR(vol_args))
1698 return PTR_ERR(vol_args);
1699 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001700
Li Zefanfa0d2b92010-12-20 15:53:28 +08001701 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001702 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001703 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001704
Li Zefanfa0d2b92010-12-20 15:53:28 +08001705 kfree(vol_args);
1706 return ret;
1707}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001708
Li Zefanfa0d2b92010-12-20 15:53:28 +08001709static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1710 void __user *arg, int subvol)
1711{
1712 struct btrfs_ioctl_vol_args_v2 *vol_args;
1713 int ret;
1714 u64 transid = 0;
1715 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001716 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001717 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001718
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001719 if (!S_ISDIR(file_inode(file)->i_mode))
1720 return -ENOTDIR;
1721
Li Zefanfa0d2b92010-12-20 15:53:28 +08001722 vol_args = memdup_user(arg, sizeof(*vol_args));
1723 if (IS_ERR(vol_args))
1724 return PTR_ERR(vol_args);
1725 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001726
Li Zefanb83cc962010-12-20 16:04:08 +08001727 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001728 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1729 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001730 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001731 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001732 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001733
1734 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1735 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001736 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1737 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001738 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001739 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001740 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001741 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001742 }
1743 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1744 if (IS_ERR(inherit)) {
1745 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001746 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001747 }
1748 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001749
1750 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001751 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001752 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001753 if (ret)
1754 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001755
Dan Carpenterc47ca322014-09-04 14:09:15 +03001756 if (ptr && copy_to_user(arg +
1757 offsetof(struct btrfs_ioctl_vol_args_v2,
1758 transid),
1759 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001760 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001761
1762free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001763 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001764free_args:
1765 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001766 return ret;
1767}
1768
Li Zefan0caa1022010-12-20 16:30:25 +08001769static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1770 void __user *arg)
1771{
Al Viro496ad9a2013-01-23 17:07:38 -05001772 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001773 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001774 struct btrfs_root *root = BTRFS_I(inode)->root;
1775 int ret = 0;
1776 u64 flags = 0;
1777
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001778 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001779 return -EINVAL;
1780
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001781 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001782 if (btrfs_root_readonly(root))
1783 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001784 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001785
1786 if (copy_to_user(arg, &flags, sizeof(flags)))
1787 ret = -EFAULT;
1788
1789 return ret;
1790}
1791
1792static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1793 void __user *arg)
1794{
Al Viro496ad9a2013-01-23 17:07:38 -05001795 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001796 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001797 struct btrfs_root *root = BTRFS_I(inode)->root;
1798 struct btrfs_trans_handle *trans;
1799 u64 root_flags;
1800 u64 flags;
1801 int ret = 0;
1802
David Sterbabd60ea02014-01-16 15:50:22 +01001803 if (!inode_owner_or_capable(inode))
1804 return -EPERM;
1805
Liu Bob9ca0662012-06-29 03:58:49 -06001806 ret = mnt_want_write_file(file);
1807 if (ret)
1808 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001809
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001810 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001811 ret = -EINVAL;
1812 goto out_drop_write;
1813 }
Li Zefan0caa1022010-12-20 16:30:25 +08001814
Liu Bob9ca0662012-06-29 03:58:49 -06001815 if (copy_from_user(&flags, arg, sizeof(flags))) {
1816 ret = -EFAULT;
1817 goto out_drop_write;
1818 }
Li Zefan0caa1022010-12-20 16:30:25 +08001819
Liu Bob9ca0662012-06-29 03:58:49 -06001820 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1821 ret = -EINVAL;
1822 goto out_drop_write;
1823 }
Li Zefan0caa1022010-12-20 16:30:25 +08001824
Liu Bob9ca0662012-06-29 03:58:49 -06001825 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1826 ret = -EOPNOTSUPP;
1827 goto out_drop_write;
1828 }
Li Zefan0caa1022010-12-20 16:30:25 +08001829
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001830 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001831
1832 /* nothing to do */
1833 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001834 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001835
1836 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001837 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001838 btrfs_set_root_flags(&root->root_item,
1839 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001840 } else {
1841 /*
1842 * Block RO -> RW transition if this subvolume is involved in
1843 * send
1844 */
1845 spin_lock(&root->root_item_lock);
1846 if (root->send_in_progress == 0) {
1847 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001848 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001849 spin_unlock(&root->root_item_lock);
1850 } else {
1851 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001852 btrfs_warn(fs_info,
1853 "Attempt to set subvolume %llu read-write during send",
1854 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001855 ret = -EPERM;
1856 goto out_drop_sem;
1857 }
1858 }
Li Zefan0caa1022010-12-20 16:30:25 +08001859
1860 trans = btrfs_start_transaction(root, 1);
1861 if (IS_ERR(trans)) {
1862 ret = PTR_ERR(trans);
1863 goto out_reset;
1864 }
1865
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001866 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001867 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001868 if (ret < 0) {
1869 btrfs_end_transaction(trans);
1870 goto out_reset;
1871 }
Li Zefan0caa1022010-12-20 16:30:25 +08001872
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001873 ret = btrfs_commit_transaction(trans);
1874
Li Zefan0caa1022010-12-20 16:30:25 +08001875out_reset:
1876 if (ret)
1877 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001878out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001879 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001880out_drop_write:
1881 mnt_drop_write_file(file);
1882out:
Li Zefan0caa1022010-12-20 16:30:25 +08001883 return ret;
1884}
1885
Chris Masonac8e9812010-02-28 15:39:26 -05001886static noinline int key_in_sk(struct btrfs_key *key,
1887 struct btrfs_ioctl_search_key *sk)
1888{
Chris Masonabc6e132010-03-18 12:10:08 -04001889 struct btrfs_key test;
1890 int ret;
1891
1892 test.objectid = sk->min_objectid;
1893 test.type = sk->min_type;
1894 test.offset = sk->min_offset;
1895
1896 ret = btrfs_comp_cpu_keys(key, &test);
1897 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001898 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001899
1900 test.objectid = sk->max_objectid;
1901 test.type = sk->max_type;
1902 test.offset = sk->max_offset;
1903
1904 ret = btrfs_comp_cpu_keys(key, &test);
1905 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001906 return 0;
1907 return 1;
1908}
1909
Jeff Mahoneydf397562016-06-21 20:18:21 -04001910static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001911 struct btrfs_key *key,
1912 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001913 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001914 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001915 unsigned long *sk_offset,
1916 int *num_found)
1917{
1918 u64 found_transid;
1919 struct extent_buffer *leaf;
1920 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001921 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001922 unsigned long item_off;
1923 unsigned long item_len;
1924 int nritems;
1925 int i;
1926 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001927 int ret = 0;
1928
1929 leaf = path->nodes[0];
1930 slot = path->slots[0];
1931 nritems = btrfs_header_nritems(leaf);
1932
1933 if (btrfs_header_generation(leaf) > sk->max_transid) {
1934 i = nritems;
1935 goto advance_key;
1936 }
1937 found_transid = btrfs_header_generation(leaf);
1938
1939 for (i = slot; i < nritems; i++) {
1940 item_off = btrfs_item_ptr_offset(leaf, i);
1941 item_len = btrfs_item_size_nr(leaf, i);
1942
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001943 btrfs_item_key_to_cpu(leaf, key, i);
1944 if (!key_in_sk(key, sk))
1945 continue;
1946
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001947 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001948 if (*num_found) {
1949 ret = 1;
1950 goto out;
1951 }
Chris Masonac8e9812010-02-28 15:39:26 -05001952
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001953 /*
1954 * return one empty item back for v1, which does not
1955 * handle -EOVERFLOW
1956 */
1957
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001958 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001959 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001960 ret = -EOVERFLOW;
1961 }
Chris Masonac8e9812010-02-28 15:39:26 -05001962
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001963 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05001964 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001965 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001966 }
1967
Chris Masonac8e9812010-02-28 15:39:26 -05001968 sh.objectid = key->objectid;
1969 sh.offset = key->offset;
1970 sh.type = key->type;
1971 sh.len = item_len;
1972 sh.transid = found_transid;
1973
1974 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01001975 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
1976 ret = -EFAULT;
1977 goto out;
1978 }
1979
Chris Masonac8e9812010-02-28 15:39:26 -05001980 *sk_offset += sizeof(sh);
1981
1982 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01001983 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05001984 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01001985 if (read_extent_buffer_to_user(leaf, up,
1986 item_off, item_len)) {
1987 ret = -EFAULT;
1988 goto out;
1989 }
1990
Chris Masonac8e9812010-02-28 15:39:26 -05001991 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001992 }
Hugo Millse2156862011-05-14 17:43:41 +00001993 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001994
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001995 if (ret) /* -EOVERFLOW from above */
1996 goto out;
1997
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001998 if (*num_found >= sk->nr_items) {
1999 ret = 1;
2000 goto out;
2001 }
Chris Masonac8e9812010-02-28 15:39:26 -05002002 }
2003advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002004 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002005 test.objectid = sk->max_objectid;
2006 test.type = sk->max_type;
2007 test.offset = sk->max_offset;
2008 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2009 ret = 1;
2010 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002011 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002012 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002013 key->offset = 0;
2014 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002015 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002016 key->offset = 0;
2017 key->type = 0;
2018 key->objectid++;
2019 } else
2020 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002021out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002022 /*
2023 * 0: all items from this leaf copied, continue with next
2024 * 1: * more items can be copied, but unused buffer is too small
2025 * * all items were found
2026 * Either way, it will stops the loop which iterates to the next
2027 * leaf
2028 * -EOVERFLOW: item was to large for buffer
2029 * -EFAULT: could not copy extent buffer back to userspace
2030 */
Chris Masonac8e9812010-02-28 15:39:26 -05002031 return ret;
2032}
2033
2034static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002035 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002036 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002037 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002038{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002039 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002040 struct btrfs_root *root;
2041 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002042 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002043 int ret;
2044 int num_found = 0;
2045 unsigned long sk_offset = 0;
2046
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002047 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2048 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002049 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002050 }
Gerhard Heift12544442014-01-30 16:23:58 +01002051
Chris Masonac8e9812010-02-28 15:39:26 -05002052 path = btrfs_alloc_path();
2053 if (!path)
2054 return -ENOMEM;
2055
2056 if (sk->tree_id == 0) {
2057 /* search the root of the inode that was passed */
2058 root = BTRFS_I(inode)->root;
2059 } else {
2060 key.objectid = sk->tree_id;
2061 key.type = BTRFS_ROOT_ITEM_KEY;
2062 key.offset = (u64)-1;
2063 root = btrfs_read_fs_root_no_name(info, &key);
2064 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002065 btrfs_free_path(path);
2066 return -ENOENT;
2067 }
2068 }
2069
2070 key.objectid = sk->min_objectid;
2071 key.type = sk->min_type;
2072 key.offset = sk->min_offset;
2073
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302074 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002075 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002076 if (ret != 0) {
2077 if (ret > 0)
2078 ret = 0;
2079 goto err;
2080 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002081 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002082 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002083 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002084 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002085 break;
2086
2087 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002088 if (ret > 0)
2089 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002090err:
2091 sk->nr_items = num_found;
2092 btrfs_free_path(path);
2093 return ret;
2094}
2095
2096static noinline int btrfs_ioctl_tree_search(struct file *file,
2097 void __user *argp)
2098{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002099 struct btrfs_ioctl_search_args __user *uargs;
2100 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002101 struct inode *inode;
2102 int ret;
2103 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002104
2105 if (!capable(CAP_SYS_ADMIN))
2106 return -EPERM;
2107
Gerhard Heiftba346b32014-01-30 16:24:02 +01002108 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002109
Gerhard Heiftba346b32014-01-30 16:24:02 +01002110 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2111 return -EFAULT;
2112
2113 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002114
Al Viro496ad9a2013-01-23 17:07:38 -05002115 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002116 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002117
2118 /*
2119 * In the origin implementation an overflow is handled by returning a
2120 * search header with a len of zero, so reset ret.
2121 */
2122 if (ret == -EOVERFLOW)
2123 ret = 0;
2124
Gerhard Heiftba346b32014-01-30 16:24:02 +01002125 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002126 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002127 return ret;
2128}
2129
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002130static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2131 void __user *argp)
2132{
2133 struct btrfs_ioctl_search_args_v2 __user *uarg;
2134 struct btrfs_ioctl_search_args_v2 args;
2135 struct inode *inode;
2136 int ret;
2137 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002138 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002139
2140 if (!capable(CAP_SYS_ADMIN))
2141 return -EPERM;
2142
2143 /* copy search header and buffer size */
2144 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2145 if (copy_from_user(&args, uarg, sizeof(args)))
2146 return -EFAULT;
2147
2148 buf_size = args.buf_size;
2149
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002150 /* limit result size to 16MB */
2151 if (buf_size > buf_limit)
2152 buf_size = buf_limit;
2153
2154 inode = file_inode(file);
2155 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002156 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002157 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2158 ret = -EFAULT;
2159 else if (ret == -EOVERFLOW &&
2160 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2161 ret = -EFAULT;
2162
Yan, Zheng76dda932009-09-21 16:00:26 -04002163 return ret;
2164}
2165
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002166/*
Chris Masonac8e9812010-02-28 15:39:26 -05002167 * Search INODE_REFs to identify path name of 'dirid' directory
2168 * in a 'tree_id' tree. and sets path name to 'name'.
2169 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002170static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2171 u64 tree_id, u64 dirid, char *name)
2172{
2173 struct btrfs_root *root;
2174 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002175 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002176 int ret = -1;
2177 int slot;
2178 int len;
2179 int total_len = 0;
2180 struct btrfs_inode_ref *iref;
2181 struct extent_buffer *l;
2182 struct btrfs_path *path;
2183
2184 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2185 name[0]='\0';
2186 return 0;
2187 }
2188
2189 path = btrfs_alloc_path();
2190 if (!path)
2191 return -ENOMEM;
2192
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002193 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002194
2195 key.objectid = tree_id;
2196 key.type = BTRFS_ROOT_ITEM_KEY;
2197 key.offset = (u64)-1;
2198 root = btrfs_read_fs_root_no_name(info, &key);
2199 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002200 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002201 ret = -ENOENT;
2202 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002203 }
2204
2205 key.objectid = dirid;
2206 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002207 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002208
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302209 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002210 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2211 if (ret < 0)
2212 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002213 else if (ret > 0) {
2214 ret = btrfs_previous_item(root, path, dirid,
2215 BTRFS_INODE_REF_KEY);
2216 if (ret < 0)
2217 goto out;
2218 else if (ret > 0) {
2219 ret = -ENOENT;
2220 goto out;
2221 }
2222 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002223
2224 l = path->nodes[0];
2225 slot = path->slots[0];
2226 btrfs_item_key_to_cpu(l, &key, slot);
2227
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002228 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2229 len = btrfs_inode_ref_name_len(l, iref);
2230 ptr -= len + 1;
2231 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002232 if (ptr < name) {
2233 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002234 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002235 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002236
2237 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302238 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002239
2240 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2241 break;
2242
David Sterbab3b4aa72011-04-21 01:20:15 +02002243 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002244 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002245 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002246 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002247 }
Li Zefan77906a502011-07-14 03:16:00 +00002248 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302249 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002250 ret = 0;
2251out:
2252 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002253 return ret;
2254}
2255
2256static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2257 void __user *argp)
2258{
2259 struct btrfs_ioctl_ino_lookup_args *args;
2260 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002261 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002262
Julia Lawall2354d082010-10-29 15:14:18 -04002263 args = memdup_user(argp, sizeof(*args));
2264 if (IS_ERR(args))
2265 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002266
Al Viro496ad9a2013-01-23 17:07:38 -05002267 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002268
David Sterba01b810b2015-05-12 19:14:49 +02002269 /*
2270 * Unprivileged query to obtain the containing subvolume root id. The
2271 * path is reset so it's consistent with btrfs_search_path_in_tree.
2272 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002273 if (args->treeid == 0)
2274 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2275
David Sterba01b810b2015-05-12 19:14:49 +02002276 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2277 args->name[0] = 0;
2278 goto out;
2279 }
2280
2281 if (!capable(CAP_SYS_ADMIN)) {
2282 ret = -EPERM;
2283 goto out;
2284 }
2285
Chris Masonac8e9812010-02-28 15:39:26 -05002286 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2287 args->treeid, args->objectid,
2288 args->name);
2289
David Sterba01b810b2015-05-12 19:14:49 +02002290out:
Chris Masonac8e9812010-02-28 15:39:26 -05002291 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2292 ret = -EFAULT;
2293
2294 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002295 return ret;
2296}
2297
Yan, Zheng76dda932009-09-21 16:00:26 -04002298static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2299 void __user *arg)
2300{
Al Viro54563d42013-09-01 15:57:51 -04002301 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002302 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002303 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002304 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002305 struct inode *inode;
2306 struct btrfs_root *root = BTRFS_I(dir)->root;
2307 struct btrfs_root *dest = NULL;
2308 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002309 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002310 int err = 0;
2311
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002312 if (!S_ISDIR(dir->i_mode))
2313 return -ENOTDIR;
2314
Yan, Zheng76dda932009-09-21 16:00:26 -04002315 vol_args = memdup_user(arg, sizeof(*vol_args));
2316 if (IS_ERR(vol_args))
2317 return PTR_ERR(vol_args);
2318
2319 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2320 namelen = strlen(vol_args->name);
2321 if (strchr(vol_args->name, '/') ||
2322 strncmp(vol_args->name, "..", namelen) == 0) {
2323 err = -EINVAL;
2324 goto out;
2325 }
2326
Al Viroa561be72011-11-23 11:57:51 -05002327 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002328 if (err)
2329 goto out;
2330
David Sterba521e0542014-04-15 16:41:44 +02002331
Al Viro00235412016-05-26 00:05:12 -04002332 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2333 if (err == -EINTR)
2334 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002335 dentry = lookup_one_len(vol_args->name, parent, namelen);
2336 if (IS_ERR(dentry)) {
2337 err = PTR_ERR(dentry);
2338 goto out_unlock_dir;
2339 }
2340
David Howells2b0143b2015-03-17 22:25:59 +00002341 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002342 err = -ENOENT;
2343 goto out_dput;
2344 }
2345
David Howells2b0143b2015-03-17 22:25:59 +00002346 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002347 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302348 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002349 /*
2350 * Regular user. Only allow this with a special mount
2351 * option, when the user has write+exec access to the
2352 * subvol root, and when rmdir(2) would have been
2353 * allowed.
2354 *
2355 * Note that this is _not_ check that the subvol is
2356 * empty or doesn't contain data that we wouldn't
2357 * otherwise be able to delete.
2358 *
2359 * Users who want to delete empty subvols should try
2360 * rmdir(2).
2361 */
2362 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002363 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002364 goto out_dput;
2365
2366 /*
2367 * Do not allow deletion if the parent dir is the same
2368 * as the dir to be deleted. That means the ioctl
2369 * must be called on the dentry referencing the root
2370 * of the subvol, not a random directory contained
2371 * within it.
2372 */
2373 err = -EINVAL;
2374 if (root == dest)
2375 goto out_dput;
2376
2377 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2378 if (err)
2379 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002380 }
2381
Miao Xie5c39da52012-10-22 11:39:53 +00002382 /* check if subvolume may be deleted by a user */
2383 err = btrfs_may_delete(dir, dentry, 1);
2384 if (err)
2385 goto out_dput;
2386
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002387 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002388 err = -EINVAL;
2389 goto out_dput;
2390 }
2391
Al Viro59551022016-01-22 15:40:57 -05002392 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002393 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002394 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002395 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002396 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002397
Yan, Zheng76dda932009-09-21 16:00:26 -04002398out_dput:
2399 dput(dentry);
2400out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002401 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002402out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002403 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002404out:
2405 kfree(vol_args);
2406 return err;
2407}
2408
Chris Mason1e701a32010-03-11 09:42:04 -05002409static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002410{
Al Viro496ad9a2013-01-23 17:07:38 -05002411 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002412 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002413 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002414 int ret;
2415
Ilya Dryomov25122d12013-01-20 15:57:57 +02002416 ret = mnt_want_write_file(file);
2417 if (ret)
2418 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002419
Ilya Dryomov25122d12013-01-20 15:57:57 +02002420 if (btrfs_root_readonly(root)) {
2421 ret = -EROFS;
2422 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002423 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002424
2425 switch (inode->i_mode & S_IFMT) {
2426 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002427 if (!capable(CAP_SYS_ADMIN)) {
2428 ret = -EPERM;
2429 goto out;
2430 }
Eric Sandeende78b512013-01-31 18:21:12 +00002431 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002432 break;
2433 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002434 if (!(file->f_mode & FMODE_WRITE)) {
2435 ret = -EINVAL;
2436 goto out;
2437 }
Chris Mason1e701a32010-03-11 09:42:04 -05002438
2439 range = kzalloc(sizeof(*range), GFP_KERNEL);
2440 if (!range) {
2441 ret = -ENOMEM;
2442 goto out;
2443 }
2444
2445 if (argp) {
2446 if (copy_from_user(range, argp,
2447 sizeof(*range))) {
2448 ret = -EFAULT;
2449 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002450 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002451 }
2452 /* compression requires us to start the IO */
2453 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2454 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2455 range->extent_thresh = (u32)-1;
2456 }
2457 } else {
2458 /* the rest are all set to zero by kzalloc */
2459 range->len = (u64)-1;
2460 }
Al Viro496ad9a2013-01-23 17:07:38 -05002461 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002462 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002463 if (ret > 0)
2464 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002465 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002466 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002467 default:
2468 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002469 }
Chris Masone441d542009-01-05 16:57:23 -05002470out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002471 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002472 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002473}
2474
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002475static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002476{
2477 struct btrfs_ioctl_vol_args *vol_args;
2478 int ret;
2479
Chris Masone441d542009-01-05 16:57:23 -05002480 if (!capable(CAP_SYS_ADMIN))
2481 return -EPERM;
2482
David Sterba171938e2017-03-28 14:44:21 +02002483 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002484 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002485
Li Zefandae7b662009-04-08 15:06:54 +08002486 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002487 if (IS_ERR(vol_args)) {
2488 ret = PTR_ERR(vol_args);
2489 goto out;
2490 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002491
Mark Fasheh5516e592008-07-24 12:20:14 -04002492 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002493 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002494
Anand Jain43d20762014-07-01 00:58:56 +08002495 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002496 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002497
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002498 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002499out:
David Sterba171938e2017-03-28 14:44:21 +02002500 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002501 return ret;
2502}
2503
Anand Jain6b526ed2016-02-13 10:01:39 +08002504static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002505{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002506 struct inode *inode = file_inode(file);
2507 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002508 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002509 int ret;
2510
Chris Masone441d542009-01-05 16:57:23 -05002511 if (!capable(CAP_SYS_ADMIN))
2512 return -EPERM;
2513
Miao Xieda249272012-11-26 08:44:50 +00002514 ret = mnt_want_write_file(file);
2515 if (ret)
2516 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002517
Li Zefandae7b662009-04-08 15:06:54 +08002518 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002519 if (IS_ERR(vol_args)) {
2520 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002521 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002522 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002523
Anand Jain6b526ed2016-02-13 10:01:39 +08002524 /* Check for compatibility reject unknown flags */
David Sterba735654e2016-02-15 18:15:21 +01002525 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2526 return -EOPNOTSUPP;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002527
David Sterba171938e2017-03-28 14:44:21 +02002528 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002529 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2530 goto out;
2531 }
2532
David Sterba735654e2016-02-15 18:15:21 +01002533 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002534 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002535 } else {
2536 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002537 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002538 }
David Sterba171938e2017-03-28 14:44:21 +02002539 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002540
Anand Jain6b526ed2016-02-13 10:01:39 +08002541 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002542 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002543 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002544 vol_args->devid);
2545 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002546 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002547 vol_args->name);
2548 }
Anand Jain183860f2013-05-17 10:52:45 +00002549out:
2550 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002551err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002552 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002553 return ret;
2554}
2555
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002556static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002558 struct inode *inode = file_inode(file);
2559 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002560 struct btrfs_ioctl_vol_args *vol_args;
2561 int ret;
2562
2563 if (!capable(CAP_SYS_ADMIN))
2564 return -EPERM;
2565
2566 ret = mnt_want_write_file(file);
2567 if (ret)
2568 return ret;
2569
David Sterba171938e2017-03-28 14:44:21 +02002570 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002571 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002572 goto out_drop_write;
2573 }
2574
2575 vol_args = memdup_user(arg, sizeof(*vol_args));
2576 if (IS_ERR(vol_args)) {
2577 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002578 goto out;
2579 }
2580
David Sterba58d7bbf2016-05-04 14:10:47 +02002581 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002582 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002583
2584 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002585 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002586 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002587out:
David Sterba171938e2017-03-28 14:44:21 +02002588 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002589out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002590 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002591
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002592 return ret;
2593}
2594
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002595static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2596 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002597{
Li Zefan027ed2f2011-06-08 08:27:56 +00002598 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002599 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002600 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002601 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002602
Li Zefan027ed2f2011-06-08 08:27:56 +00002603 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2604 if (!fi_args)
2605 return -ENOMEM;
2606
David Sterbad03262c2017-06-16 00:09:21 +02002607 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00002608 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002609
David Sterbad03262c2017-06-16 00:09:21 +02002610 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002611 if (device->devid > fi_args->max_id)
2612 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002613 }
David Sterbad03262c2017-06-16 00:09:21 +02002614 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002615
David Sterbad03262c2017-06-16 00:09:21 +02002616 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07002617 fi_args->nodesize = fs_info->nodesize;
2618 fi_args->sectorsize = fs_info->sectorsize;
2619 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002620
Li Zefan027ed2f2011-06-08 08:27:56 +00002621 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2622 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002623
Li Zefan027ed2f2011-06-08 08:27:56 +00002624 kfree(fi_args);
2625 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002626}
2627
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002628static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2629 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002630{
2631 struct btrfs_ioctl_dev_info_args *di_args;
2632 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01002633 int ret = 0;
2634 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002635
Jan Schmidt475f6382011-03-11 15:41:01 +01002636 di_args = memdup_user(arg, sizeof(*di_args));
2637 if (IS_ERR(di_args))
2638 return PTR_ERR(di_args);
2639
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002640 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002641 s_uuid = di_args->uuid;
2642
David Sterbac5593ca2017-06-16 00:09:21 +02002643 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002644 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002645
2646 if (!dev) {
2647 ret = -ENODEV;
2648 goto out;
2649 }
2650
2651 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002652 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2653 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002654 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002655 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002656 struct rcu_string *name;
2657
Josef Bacik606686e2012-06-04 14:03:51 -04002658 name = rcu_dereference(dev->name);
Xiongfeng Wang6670d4c2018-01-08 19:51:22 +08002659 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02002660 di_args->path[sizeof(di_args->path) - 1] = 0;
2661 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002662 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002663 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002664
2665out:
David Sterbac5593ca2017-06-16 00:09:21 +02002666 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002667 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2668 ret = -EFAULT;
2669
2670 kfree(di_args);
2671 return ret;
2672}
2673
Mark Fashehf4414602015-06-30 14:42:05 -07002674static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002675{
2676 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002677
Mark Fasheh416161d2013-08-06 11:42:51 -07002678 page = grab_cache_page(inode->i_mapping, index);
2679 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002680 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002681
2682 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002683 int ret;
2684
2685 ret = btrfs_readpage(NULL, page);
2686 if (ret)
2687 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002688 lock_page(page);
2689 if (!PageUptodate(page)) {
2690 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002691 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002692 return ERR_PTR(-EIO);
2693 }
2694 if (page->mapping != inode->i_mapping) {
2695 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002696 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002697 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002698 }
2699 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002700
2701 return page;
2702}
2703
Mark Fashehf4414602015-06-30 14:42:05 -07002704static int gather_extent_pages(struct inode *inode, struct page **pages,
2705 int num_pages, u64 off)
2706{
2707 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002708 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002709
2710 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002711again:
Mark Fashehf4414602015-06-30 14:42:05 -07002712 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002713 if (IS_ERR(pages[i])) {
2714 int err = PTR_ERR(pages[i]);
2715
2716 if (err == -EAGAIN)
2717 goto again;
2718 pages[i] = NULL;
2719 return err;
2720 }
Mark Fashehf4414602015-06-30 14:42:05 -07002721 }
2722 return 0;
2723}
2724
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002725static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2726 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002727{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002728 /*
2729 * Do any pending delalloc/csum calculations on inode, one way or
2730 * another, and lock file content.
2731 * The locking order is:
2732 *
2733 * 1) pages
2734 * 2) range in the inode's io tree
2735 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002736 while (1) {
2737 struct btrfs_ordered_extent *ordered;
2738 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2739 ordered = btrfs_lookup_first_ordered_extent(inode,
2740 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002741 if ((!ordered ||
2742 ordered->file_offset + ordered->len <= off ||
2743 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002744 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002745 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2746 if (ordered)
2747 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002748 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002749 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002750 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2751 if (ordered)
2752 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002753 if (!retry_range_locking)
2754 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002755 btrfs_wait_ordered_range(inode, off, len);
2756 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002757 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002758}
2759
Mark Fashehf4414602015-06-30 14:42:05 -07002760static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002761{
Al Viro59551022016-01-22 15:40:57 -05002762 inode_unlock(inode1);
2763 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002764}
2765
Mark Fashehf4414602015-06-30 14:42:05 -07002766static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2767{
2768 if (inode1 < inode2)
2769 swap(inode1, inode2);
2770
Al Viro59551022016-01-22 15:40:57 -05002771 inode_lock_nested(inode1, I_MUTEX_PARENT);
2772 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002773}
2774
2775static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2776 struct inode *inode2, u64 loff2, u64 len)
2777{
2778 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2779 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2780}
2781
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002782static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2783 struct inode *inode2, u64 loff2, u64 len,
2784 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002785{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002786 int ret;
2787
Mark Fasheh416161d2013-08-06 11:42:51 -07002788 if (inode1 < inode2) {
2789 swap(inode1, inode2);
2790 swap(loff1, loff2);
2791 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002792 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2793 if (ret)
2794 return ret;
2795 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2796 if (ret)
2797 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2798 loff1 + len - 1);
2799 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002800}
2801
2802struct cmp_pages {
2803 int num_pages;
2804 struct page **src_pages;
2805 struct page **dst_pages;
2806};
2807
2808static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2809{
2810 int i;
2811 struct page *pg;
2812
2813 for (i = 0; i < cmp->num_pages; i++) {
2814 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002815 if (pg) {
2816 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002817 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002818 }
Mark Fashehf4414602015-06-30 14:42:05 -07002819 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002820 if (pg) {
2821 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002822 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002823 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002824 }
Mark Fashehf4414602015-06-30 14:42:05 -07002825 kfree(cmp->src_pages);
2826 kfree(cmp->dst_pages);
2827}
2828
2829static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2830 struct inode *dst, u64 dst_loff,
2831 u64 len, struct cmp_pages *cmp)
2832{
2833 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002834 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002835 struct page **src_pgarr, **dst_pgarr;
2836
2837 /*
2838 * We must gather up all the pages before we initiate our
2839 * extent locking. We use an array for the page pointers. Size
2840 * of the array is bounded by len, which is in turn bounded by
2841 * BTRFS_MAX_DEDUPE_LEN.
2842 */
David Sterba66722f72016-02-11 15:01:38 +01002843 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
2844 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07002845 if (!src_pgarr || !dst_pgarr) {
2846 kfree(src_pgarr);
2847 kfree(dst_pgarr);
2848 return -ENOMEM;
2849 }
2850 cmp->num_pages = num_pages;
2851 cmp->src_pages = src_pgarr;
2852 cmp->dst_pages = dst_pgarr;
2853
Filipe Mananab1517622017-02-21 17:14:52 +00002854 /*
2855 * If deduping ranges in the same inode, locking rules make it mandatory
2856 * to always lock pages in ascending order to avoid deadlocks with
2857 * concurrent tasks (such as starting writeback/delalloc).
2858 */
2859 if (src == dst && dst_loff < loff) {
2860 swap(src_pgarr, dst_pgarr);
2861 swap(loff, dst_loff);
2862 }
2863
2864 ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002865 if (ret)
2866 goto out;
2867
Filipe Mananab1517622017-02-21 17:14:52 +00002868 ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002869
2870out:
2871 if (ret)
2872 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09002873 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07002874}
2875
David Sterba1a287cf2017-02-10 20:15:10 +01002876static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002877{
2878 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07002879 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07002880 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002881 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002882 void *addr, *dst_addr;
2883
Mark Fashehf4414602015-06-30 14:42:05 -07002884 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002885 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002886 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07002887 cmp_len = len;
2888
Mark Fashehf4414602015-06-30 14:42:05 -07002889 BUG_ON(i >= cmp->num_pages);
2890
2891 src_page = cmp->src_pages[i];
2892 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002893 ASSERT(PageLocked(src_page));
2894 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07002895
Mark Fasheh416161d2013-08-06 11:42:51 -07002896 addr = kmap_atomic(src_page);
2897 dst_addr = kmap_atomic(dst_page);
2898
2899 flush_dcache_page(src_page);
2900 flush_dcache_page(dst_page);
2901
2902 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08002903 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002904
2905 kunmap_atomic(addr);
2906 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07002907
2908 if (ret)
2909 break;
2910
Mark Fasheh416161d2013-08-06 11:42:51 -07002911 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07002912 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07002913 }
2914
2915 return ret;
2916}
2917
Mark Fashehe1d227a2015-06-08 15:05:25 -07002918static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
2919 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07002920{
Mark Fashehe1d227a2015-06-08 15:05:25 -07002921 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07002922 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2923
Mark Fashehe1d227a2015-06-08 15:05:25 -07002924 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07002925 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07002926
2927 /* if we extend to eof, continue to block boundary */
2928 if (off + len == inode->i_size)
2929 *plen = len = ALIGN(inode->i_size, bs) - off;
2930
Mark Fasheh416161d2013-08-06 11:42:51 -07002931 /* Check that we are block aligned - btrfs_clone() requires this */
2932 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2933 return -EINVAL;
2934
2935 return 0;
2936}
2937
Mark Fashehe1d227a2015-06-08 15:05:25 -07002938static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07002939 struct inode *dst, u64 dst_loff)
2940{
2941 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07002942 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07002943 struct cmp_pages cmp;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002944 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07002945 u64 same_lock_start = 0;
2946 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002947
Filipe Manana113e8282015-03-30 18:26:47 +01002948 if (len == 0)
2949 return 0;
2950
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002951 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05002952 inode_lock(src);
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002953 else
2954 btrfs_double_inode_lock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07002955
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002956 ret = extent_same_check_offsets(src, loff, &len, olen);
2957 if (ret)
2958 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07002959
Omar Sandovalfc4badd2017-01-17 23:37:38 -08002960 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
2961 if (ret)
2962 goto out_unlock;
2963
2964 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07002965 /*
2966 * Single inode case wants the same checks, except we
2967 * don't want our length pushed out past i_size as
2968 * comparing that data range makes no sense.
2969 *
2970 * extent_same_check_offsets() will do this for an
2971 * unaligned length at i_size, so catch it here and
2972 * reject the request.
2973 *
2974 * This effectively means we require aligned extents
2975 * for the single-inode case, whereas the other cases
2976 * allow an unaligned length so long as it ends at
2977 * i_size.
2978 */
2979 if (len != olen) {
2980 ret = -EINVAL;
2981 goto out_unlock;
2982 }
2983
2984 /* Check for overlapping ranges */
2985 if (dst_loff + len > loff && dst_loff < loff + len) {
2986 ret = -EINVAL;
2987 goto out_unlock;
2988 }
2989
2990 same_lock_start = min_t(u64, loff, dst_loff);
2991 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07002992 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002993
2994 /* don't make the dst file partly checksummed */
2995 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2996 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2997 ret = -EINVAL;
2998 goto out_unlock;
2999 }
3000
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003001again:
Mark Fashehf4414602015-06-30 14:42:05 -07003002 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3003 if (ret)
3004 goto out_unlock;
3005
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003006 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003007 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3008 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003009 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003010 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3011 false);
3012 /*
3013 * If one of the inodes has dirty pages in the respective range or
3014 * ordered extents, we need to flush dellaloc and wait for all ordered
3015 * extents in the range. We must unlock the pages and the ranges in the
3016 * io trees to avoid deadlocks when flushing delalloc (requires locking
3017 * pages) and when waiting for ordered extents to complete (they require
3018 * range locking).
3019 */
3020 if (ret == -EAGAIN) {
3021 /*
3022 * Ranges in the io trees already unlocked. Now unlock all
3023 * pages before waiting for all IO to complete.
3024 */
3025 btrfs_cmp_data_free(&cmp);
3026 if (same_inode) {
3027 btrfs_wait_ordered_range(src, same_lock_start,
3028 same_lock_len);
3029 } else {
3030 btrfs_wait_ordered_range(src, loff, len);
3031 btrfs_wait_ordered_range(dst, dst_loff, len);
3032 }
3033 goto again;
3034 }
3035 ASSERT(ret == 0);
3036 if (WARN_ON(ret)) {
3037 /* ranges in the io trees already unlocked */
3038 btrfs_cmp_data_free(&cmp);
3039 return ret;
3040 }
Mark Fashehf4414602015-06-30 14:42:05 -07003041
Mark Fasheh207910d2015-06-30 14:42:04 -07003042 /* pass original length for comparison so we stay within i_size */
David Sterba1a287cf2017-02-10 20:15:10 +01003043 ret = btrfs_cmp_data(olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003044 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003045 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003046
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003047 if (same_inode)
3048 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3049 same_lock_start + same_lock_len - 1);
3050 else
3051 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003052
3053 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003054out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003055 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003056 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003057 else
3058 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003059
3060 return ret;
3061}
3062
Byongho Leeee221842015-12-15 01:42:10 +09003063#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003064
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003065ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3066 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003067{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003068 struct inode *src = file_inode(src_file);
3069 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003070 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003071 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003072
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003073 if (olen > BTRFS_MAX_DEDUPE_LEN)
3074 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003075
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003076 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003077 /*
3078 * Btrfs does not support blocksize < page_size. As a
3079 * result, btrfs_cmp_data() won't correctly handle
3080 * this situation without an update.
3081 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003082 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003083 }
3084
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003085 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3086 if (res)
3087 return res;
3088 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003089}
3090
Filipe Mananaf82a9902014-06-01 01:50:28 +01003091static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3092 struct inode *inode,
3093 u64 endoff,
3094 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003095 const u64 olen,
3096 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003097{
3098 struct btrfs_root *root = BTRFS_I(inode)->root;
3099 int ret;
3100
3101 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003102 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003103 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003104 /*
3105 * We round up to the block size at eof when determining which
3106 * extents to clone above, but shouldn't round up the file size.
3107 */
3108 if (endoff > destoff + olen)
3109 endoff = destoff + olen;
3110 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003111 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003112
3113 ret = btrfs_update_inode(trans, root, inode);
3114 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003115 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003116 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003117 goto out;
3118 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003119 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003120out:
3121 return ret;
3122}
3123
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003124static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003125 const struct btrfs_trans_handle *trans,
3126 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003127 const u64 hole_offset,
3128 const u64 hole_len)
3129{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003130 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003131 struct extent_map *em;
3132 int ret;
3133
3134 em = alloc_extent_map();
3135 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003136 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003137 return;
3138 }
3139
Filipe Manana14f59792014-06-29 21:45:40 +01003140 if (path) {
3141 struct btrfs_file_extent_item *fi;
3142
3143 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3144 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003145 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003146 em->generation = -1;
3147 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3148 BTRFS_FILE_EXTENT_INLINE)
3149 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003150 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003151 } else {
3152 em->start = hole_offset;
3153 em->len = hole_len;
3154 em->ram_bytes = em->len;
3155 em->orig_start = hole_offset;
3156 em->block_start = EXTENT_MAP_HOLE;
3157 em->block_len = 0;
3158 em->orig_block_len = 0;
3159 em->compress_type = BTRFS_COMPRESS_NONE;
3160 em->generation = trans->transid;
3161 }
3162
3163 while (1) {
3164 write_lock(&em_tree->lock);
3165 ret = add_extent_mapping(em_tree, em, 1);
3166 write_unlock(&em_tree->lock);
3167 if (ret != -EEXIST) {
3168 free_extent_map(em);
3169 break;
3170 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003171 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003172 em->start + em->len - 1, 0);
3173 }
3174
David Sterbaee39b432014-09-30 01:33:33 +02003175 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003176 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003177}
3178
Filipe Manana8039d872015-10-13 15:15:00 +01003179/*
3180 * Make sure we do not end up inserting an inline extent into a file that has
3181 * already other (non-inline) extents. If a file has an inline extent it can
3182 * not have any other extents and the (single) inline extent must start at the
3183 * file offset 0. Failing to respect these rules will lead to file corruption,
3184 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3185 *
3186 * We can have extents that have been already written to disk or we can have
3187 * dirty ranges still in delalloc, in which case the extent maps and items are
3188 * created only when we run delalloc, and the delalloc ranges might fall outside
3189 * the range we are currently locking in the inode's io tree. So we check the
3190 * inode's i_size because of that (i_size updates are done while holding the
3191 * i_mutex, which we are holding here).
3192 * We also check to see if the inode has a size not greater than "datal" but has
3193 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3194 * protected against such concurrent fallocate calls by the i_mutex).
3195 *
3196 * If the file has no extents but a size greater than datal, do not allow the
3197 * copy because we would need turn the inline extent into a non-inline one (even
3198 * with NO_HOLES enabled). If we find our destination inode only has one inline
3199 * extent, just overwrite it with the source inline extent if its size is less
3200 * than the source extent's size, or we could copy the source inline extent's
3201 * data into the destination inode's inline extent if the later is greater then
3202 * the former.
3203 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003204static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003205 struct btrfs_trans_handle *trans,
3206 struct btrfs_path *path,
3207 struct btrfs_key *new_key,
3208 const u64 drop_start,
3209 const u64 datal,
3210 const u64 skip,
3211 const u64 size,
3212 char *inline_data)
3213{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003214 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003215 struct btrfs_root *root = BTRFS_I(dst)->root;
3216 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003217 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003218 int ret;
3219 struct btrfs_key key;
3220
3221 if (new_key->offset > 0)
3222 return -EOPNOTSUPP;
3223
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003224 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003225 key.type = BTRFS_EXTENT_DATA_KEY;
3226 key.offset = 0;
3227 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3228 if (ret < 0) {
3229 return ret;
3230 } else if (ret > 0) {
3231 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3232 ret = btrfs_next_leaf(root, path);
3233 if (ret < 0)
3234 return ret;
3235 else if (ret > 0)
3236 goto copy_inline_extent;
3237 }
3238 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003239 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003240 key.type == BTRFS_EXTENT_DATA_KEY) {
3241 ASSERT(key.offset > 0);
3242 return -EOPNOTSUPP;
3243 }
3244 } else if (i_size_read(dst) <= datal) {
3245 struct btrfs_file_extent_item *ei;
3246 u64 ext_len;
3247
3248 /*
3249 * If the file size is <= datal, make sure there are no other
3250 * extents following (can happen do to an fallocate call with
3251 * the flag FALLOC_FL_KEEP_SIZE).
3252 */
3253 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3254 struct btrfs_file_extent_item);
3255 /*
3256 * If it's an inline extent, it can not have other extents
3257 * following it.
3258 */
3259 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3260 BTRFS_FILE_EXTENT_INLINE)
3261 goto copy_inline_extent;
3262
3263 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3264 if (ext_len > aligned_end)
3265 return -EOPNOTSUPP;
3266
3267 ret = btrfs_next_item(root, path);
3268 if (ret < 0) {
3269 return ret;
3270 } else if (ret == 0) {
3271 btrfs_item_key_to_cpu(path->nodes[0], &key,
3272 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003273 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003274 key.type == BTRFS_EXTENT_DATA_KEY)
3275 return -EOPNOTSUPP;
3276 }
3277 }
3278
3279copy_inline_extent:
3280 /*
3281 * We have no extent items, or we have an extent at offset 0 which may
3282 * or may not be inlined. All these cases are dealt the same way.
3283 */
3284 if (i_size_read(dst) > datal) {
3285 /*
3286 * If the destination inode has an inline extent...
3287 * This would require copying the data from the source inline
3288 * extent into the beginning of the destination's inline extent.
3289 * But this is really complex, both extents can be compressed
3290 * or just one of them, which would require decompressing and
3291 * re-compressing data (which could increase the new compressed
3292 * size, not allowing the compressed data to fit anymore in an
3293 * inline extent).
3294 * So just don't support this case for now (it should be rare,
3295 * we are not really saving space when cloning inline extents).
3296 */
3297 return -EOPNOTSUPP;
3298 }
3299
3300 btrfs_release_path(path);
3301 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3302 if (ret)
3303 return ret;
3304 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3305 if (ret)
3306 return ret;
3307
3308 if (skip) {
3309 const u32 start = btrfs_file_extent_calc_inline_size(0);
3310
3311 memmove(inline_data + start, inline_data + start + skip, datal);
3312 }
3313
3314 write_extent_buffer(path->nodes[0], inline_data,
3315 btrfs_item_ptr_offset(path->nodes[0],
3316 path->slots[0]),
3317 size);
3318 inode_add_bytes(dst, datal);
3319
3320 return 0;
3321}
3322
Mark Fasheh32b7c682013-08-06 11:42:49 -07003323/**
3324 * btrfs_clone() - clone a range from inode file to another
3325 *
3326 * @src: Inode to clone from
3327 * @inode: Inode to clone to
3328 * @off: Offset within source to start clone from
3329 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003330 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003331 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003332 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003333 */
3334static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003335 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003336 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003339 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003340 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003341 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003342 struct btrfs_trans_handle *trans;
3343 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003344 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003345 u32 nritems;
3346 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003347 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003348 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003349 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003350
3351 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003352 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3353 if (!buf)
3354 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003355
3356 path = btrfs_alloc_path();
3357 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003358 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003359 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003360 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003361
David Sterbae4058b52015-11-27 16:31:35 +01003362 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003363 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003364 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003365 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003366 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003367
3368 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003369 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003370
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003371 /*
3372 * note the key will change type as we walk through the
3373 * tree.
3374 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003375 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003376 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3377 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003378 if (ret < 0)
3379 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003380 /*
3381 * First search, if no extent item that starts at offset off was
3382 * found but the previous item is an extent item, it's possible
3383 * it might overlap our target range, therefore process it.
3384 */
3385 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3386 btrfs_item_key_to_cpu(path->nodes[0], &key,
3387 path->slots[0] - 1);
3388 if (key.type == BTRFS_EXTENT_DATA_KEY)
3389 path->slots[0]--;
3390 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003391
Yan Zhengae01a0a2008-08-04 23:23:47 -04003392 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003393process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003394 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003395 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003396 if (ret < 0)
3397 goto out;
3398 if (ret > 0)
3399 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003400 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003401 }
3402 leaf = path->nodes[0];
3403 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003404
Yan Zhengae01a0a2008-08-04 23:23:47 -04003405 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003406 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003407 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003408 break;
3409
David Sterba962a2982014-06-04 18:41:45 +02003410 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003411 struct btrfs_file_extent_item *extent;
3412 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003413 u32 size;
3414 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003415 u64 disko = 0, diskl = 0;
3416 u64 datao = 0, datal = 0;
3417 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003418 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003419
Sage Weilc5c9cd42008-11-12 14:32:25 -05003420 extent = btrfs_item_ptr(leaf, slot,
3421 struct btrfs_file_extent_item);
3422 comp = btrfs_file_extent_compression(leaf, extent);
3423 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003424 if (type == BTRFS_FILE_EXTENT_REG ||
3425 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003426 disko = btrfs_file_extent_disk_bytenr(leaf,
3427 extent);
3428 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3429 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003430 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003431 datal = btrfs_file_extent_num_bytes(leaf,
3432 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003433 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3434 /* take upper bound, may be compressed */
3435 datal = btrfs_file_extent_ram_bytes(leaf,
3436 extent);
3437 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003438
Filipe Manana2c463822014-05-31 02:31:05 +01003439 /*
3440 * The first search might have left us at an extent
3441 * item that ends before our target range's start, can
3442 * happen if we have holes and NO_HOLES feature enabled.
3443 */
3444 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003445 path->slots[0]++;
3446 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003447 } else if (key.offset >= off + len) {
3448 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003449 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003450 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003451 size = btrfs_item_size_nr(leaf, slot);
3452 read_extent_buffer(leaf, buf,
3453 btrfs_item_ptr_offset(leaf, slot),
3454 size);
3455
3456 btrfs_release_path(path);
3457 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003458
Zheng Yan31840ae2008-09-23 13:14:14 -04003459 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003460 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003461 if (off <= key.offset)
3462 new_key.offset = key.offset + destoff - off;
3463 else
3464 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003465
Sage Weilb6f34092011-09-20 14:48:51 -04003466 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003467 * Deal with a hole that doesn't have an extent item
3468 * that represents it (NO_HOLES feature enabled).
3469 * This hole is either in the middle of the cloning
3470 * range or at the beginning (fully overlaps it or
3471 * partially overlaps it).
3472 */
3473 if (new_key.offset != last_dest_end)
3474 drop_start = last_dest_end;
3475 else
3476 drop_start = new_key.offset;
3477
3478 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003479 * 1 - adjusting old extent (we may have to split it)
3480 * 1 - add new extent
3481 * 1 - inode update
3482 */
3483 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003484 if (IS_ERR(trans)) {
3485 ret = PTR_ERR(trans);
3486 goto out;
3487 }
3488
Chris Masonc8a894d2009-06-27 21:07:03 -04003489 if (type == BTRFS_FILE_EXTENT_REG ||
3490 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003491 /*
3492 * a | --- range to clone ---| b
3493 * | ------------- extent ------------- |
3494 */
3495
Antonio Ospite93915582014-06-04 14:03:48 +02003496 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003497 if (key.offset + datal > off + len)
3498 datal = off + len - key.offset;
3499
Antonio Ospite93915582014-06-04 14:03:48 +02003500 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003501 if (off > key.offset) {
3502 datao += off - key.offset;
3503 datal -= off - key.offset;
3504 }
3505
Josef Bacik5dc562c2012-08-17 13:14:17 -04003506 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003507 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003508 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003509 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003510 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003511 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003512 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003513 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003514 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003515 goto out;
3516 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003517
Sage Weilc5c9cd42008-11-12 14:32:25 -05003518 ret = btrfs_insert_empty_item(trans, root, path,
3519 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003520 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003521 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003522 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003523 goto out;
3524 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003525
3526 leaf = path->nodes[0];
3527 slot = path->slots[0];
3528 write_extent_buffer(leaf, buf,
3529 btrfs_item_ptr_offset(leaf, slot),
3530 size);
3531
3532 extent = btrfs_item_ptr(leaf, slot,
3533 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003534
Sage Weilc5c9cd42008-11-12 14:32:25 -05003535 /* disko == 0 means it's a hole */
3536 if (!disko)
3537 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003538
3539 btrfs_set_file_extent_offset(leaf, extent,
3540 datao);
3541 btrfs_set_file_extent_num_bytes(leaf, extent,
3542 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003543
Sage Weilc5c9cd42008-11-12 14:32:25 -05003544 if (disko) {
3545 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003546 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003547 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003548 disko, diskl, 0,
3549 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003550 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003551 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003552 if (ret) {
3553 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003554 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003555 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003556 goto out;
3557
3558 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003559 }
3560 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3561 u64 skip = 0;
3562 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003563
Sage Weilc5c9cd42008-11-12 14:32:25 -05003564 if (off > key.offset) {
3565 skip = off - key.offset;
3566 new_key.offset += skip;
3567 }
Chris Masond3977122009-01-05 21:25:51 -05003568
Liu Boaa42ffd2012-09-18 03:52:23 -06003569 if (key.offset + datal > off + len)
3570 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003571
Sage Weilc5c9cd42008-11-12 14:32:25 -05003572 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003573 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003574 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003575 goto out;
3576 }
3577 size -= skip + trim;
3578 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003579
David Sterba4a0ab9d2017-02-10 20:18:49 +01003580 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003581 trans, path,
3582 &new_key,
3583 drop_start,
3584 datal,
3585 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003586 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003587 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003588 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003589 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003590 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003591 goto out;
3592 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003593 leaf = path->nodes[0];
3594 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003595 }
3596
Filipe Manana7ffbb592014-06-09 03:48:05 +01003597 /* If we have an implicit hole (NO_HOLES feature). */
3598 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003599 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003600 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003601 new_key.offset - drop_start);
3602
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003603 clone_update_extent_map(BTRFS_I(inode), trans,
3604 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003605
Sage Weilc5c9cd42008-11-12 14:32:25 -05003606 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003607 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003608
Filipe Manana62e23902014-08-08 02:47:06 +01003609 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003610 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003611 ret = clone_finish_inode_update(trans, inode,
3612 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003613 destoff, olen,
3614 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003615 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003616 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003617 if (new_key.offset + datal >= destoff + len)
3618 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003619 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003620 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003621 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003622
3623 if (fatal_signal_pending(current)) {
3624 ret = -EINTR;
3625 goto out;
3626 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003627 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003628 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003629
Filipe Mananaf82a9902014-06-01 01:50:28 +01003630 if (last_dest_end < destoff + len) {
3631 /*
3632 * We have an implicit hole (NO_HOLES feature is enabled) that
3633 * fully or partially overlaps our cloning range at its end.
3634 */
3635 btrfs_release_path(path);
3636
3637 /*
3638 * 1 - remove extent(s)
3639 * 1 - inode update
3640 */
3641 trans = btrfs_start_transaction(root, 2);
3642 if (IS_ERR(trans)) {
3643 ret = PTR_ERR(trans);
3644 goto out;
3645 }
3646 ret = btrfs_drop_extents(trans, root, inode,
3647 last_dest_end, destoff + len, 1);
3648 if (ret) {
3649 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003650 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003651 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003652 goto out;
3653 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003654 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3655 last_dest_end,
3656 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003657 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003658 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003659 }
3660
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003661out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003662 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003663 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003664 return ret;
3665}
3666
Zach Brown3db11b22015-11-10 16:53:32 -05003667static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3668 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003669{
Al Viro54563d42013-09-01 15:57:51 -04003670 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003671 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003672 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003673 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003674 int ret;
3675 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003676 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003677 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003678
3679 /*
3680 * TODO:
3681 * - split compressed inline extents. annoying: we need to
3682 * decompress into destination's address_space (the file offset
3683 * may change, so source mapping won't do), then recompress (or
3684 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003685 *
3686 * - split destination inode's inline extents. The inline extents can
3687 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003688 */
3689
Mark Fasheh32b7c682013-08-06 11:42:49 -07003690 if (btrfs_root_readonly(root))
3691 return -EROFS;
3692
Zach Brown3db11b22015-11-10 16:53:32 -05003693 if (file_src->f_path.mnt != file->f_path.mnt ||
3694 src->i_sb != inode->i_sb)
3695 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003696
3697 /* don't make the dst file partly checksummed */
3698 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3699 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003700 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003701
Mark Fasheh32b7c682013-08-06 11:42:49 -07003702 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003703 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003704
3705 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003706 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003707 } else {
Al Viro59551022016-01-22 15:40:57 -05003708 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003709 }
3710
3711 /* determine range to clone */
3712 ret = -EINVAL;
3713 if (off + len > src->i_size || off + len < off)
3714 goto out_unlock;
3715 if (len == 0)
3716 olen = len = src->i_size - off;
3717 /* if we extend to eof, continue to block boundary */
3718 if (off + len == src->i_size)
3719 len = ALIGN(src->i_size, bs) - off;
3720
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003721 if (len == 0) {
3722 ret = 0;
3723 goto out_unlock;
3724 }
3725
Mark Fasheh32b7c682013-08-06 11:42:49 -07003726 /* verify the end result is block aligned */
3727 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3728 !IS_ALIGNED(destoff, bs))
3729 goto out_unlock;
3730
3731 /* verify if ranges are overlapped within the same file */
3732 if (same_inode) {
3733 if (destoff + len > off && destoff < off + len)
3734 goto out_unlock;
3735 }
3736
3737 if (destoff > inode->i_size) {
3738 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3739 if (ret)
3740 goto out_unlock;
3741 }
3742
Filipe Mananac125b8b2014-05-23 05:03:34 +01003743 /*
3744 * Lock the target range too. Right after we replace the file extent
3745 * items in the fs tree (which now point to the cloned data), we might
3746 * have a worker replace them with extent items relative to a write
3747 * operation that was issued before this clone operation (i.e. confront
3748 * with inode.c:btrfs_finish_ordered_io).
3749 */
3750 if (same_inode) {
3751 u64 lock_start = min_t(u64, off, destoff);
3752 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003753
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003754 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003755 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003756 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3757 true);
3758 }
3759 ASSERT(ret == 0);
3760 if (WARN_ON(ret)) {
3761 /* ranges in the io trees already unlocked */
3762 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003763 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003764
Mark Fasheh1c919a52015-06-30 14:42:08 -07003765 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003766
Filipe Mananac125b8b2014-05-23 05:03:34 +01003767 if (same_inode) {
3768 u64 lock_start = min_t(u64, off, destoff);
3769 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3770
3771 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3772 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003773 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003774 }
3775 /*
3776 * Truncate page cache pages so that future reads will see the cloned
3777 * data immediately and not the previous data.
3778 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303779 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003780 round_down(destoff, PAGE_SIZE),
3781 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003782out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003783 if (!same_inode)
3784 btrfs_double_inode_unlock(src, inode);
3785 else
Al Viro59551022016-01-22 15:40:57 -05003786 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003787 return ret;
3788}
3789
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003790int btrfs_clone_file_range(struct file *src_file, loff_t off,
3791 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003792{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003793 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003794}
3795
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003796static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3797{
Al Viro496ad9a2013-01-23 17:07:38 -05003798 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003800 struct btrfs_root *root = BTRFS_I(inode)->root;
3801 struct btrfs_root *new_root;
3802 struct btrfs_dir_item *di;
3803 struct btrfs_trans_handle *trans;
3804 struct btrfs_path *path;
3805 struct btrfs_key location;
3806 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003807 u64 objectid = 0;
3808 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003809 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003810
3811 if (!capable(CAP_SYS_ADMIN))
3812 return -EPERM;
3813
Miao Xie3c04ce02012-11-26 08:43:07 +00003814 ret = mnt_want_write_file(file);
3815 if (ret)
3816 return ret;
3817
3818 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3819 ret = -EFAULT;
3820 goto out;
3821 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003822
3823 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303824 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003825
3826 location.objectid = objectid;
3827 location.type = BTRFS_ROOT_ITEM_KEY;
3828 location.offset = (u64)-1;
3829
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003830 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003831 if (IS_ERR(new_root)) {
3832 ret = PTR_ERR(new_root);
3833 goto out;
3834 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09003835 if (!is_fstree(new_root->objectid)) {
3836 ret = -ENOENT;
3837 goto out;
3838 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003839
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003840 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003841 if (!path) {
3842 ret = -ENOMEM;
3843 goto out;
3844 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003845 path->leave_spinning = 1;
3846
3847 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003848 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003849 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003850 ret = PTR_ERR(trans);
3851 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003852 }
3853
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003854 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3855 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003856 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003857 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003858 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003859 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003860 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003861 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003862 ret = -ENOENT;
3863 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003864 }
3865
3866 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3867 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3868 btrfs_mark_buffer_dirty(path->nodes[0]);
3869 btrfs_free_path(path);
3870
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003871 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003872 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00003873out:
3874 mnt_drop_write_file(file);
3875 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003876}
3877
Su Yuec065f5b12018-04-02 17:24:11 +08003878static void get_block_group_info(struct list_head *groups_list,
3879 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003880{
3881 struct btrfs_block_group_cache *block_group;
3882
3883 space->total_bytes = 0;
3884 space->used_bytes = 0;
3885 space->flags = 0;
3886 list_for_each_entry(block_group, groups_list, list) {
3887 space->flags = block_group->flags;
3888 space->total_bytes += block_group->key.offset;
3889 space->used_bytes +=
3890 btrfs_block_group_used(&block_group->item);
3891 }
3892}
3893
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003894static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3895 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003896{
3897 struct btrfs_ioctl_space_args space_args;
3898 struct btrfs_ioctl_space_info space;
3899 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003900 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003901 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003902 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01003903 static const u64 types[] = {
3904 BTRFS_BLOCK_GROUP_DATA,
3905 BTRFS_BLOCK_GROUP_SYSTEM,
3906 BTRFS_BLOCK_GROUP_METADATA,
3907 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3908 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003909 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003910 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003911 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003912 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003913 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003914
3915 if (copy_from_user(&space_args,
3916 (struct btrfs_ioctl_space_args __user *)arg,
3917 sizeof(space_args)))
3918 return -EFAULT;
3919
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003920 for (i = 0; i < num_types; i++) {
3921 struct btrfs_space_info *tmp;
3922
3923 info = NULL;
3924 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003925 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003926 list) {
3927 if (tmp->flags == types[i]) {
3928 info = tmp;
3929 break;
3930 }
3931 }
3932 rcu_read_unlock();
3933
3934 if (!info)
3935 continue;
3936
3937 down_read(&info->groups_sem);
3938 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3939 if (!list_empty(&info->block_groups[c]))
3940 slot_count++;
3941 }
3942 up_read(&info->groups_sem);
3943 }
Josef Bacik1406e432010-01-13 18:19:06 +00003944
David Sterba36523e952014-02-07 14:34:12 +01003945 /*
3946 * Global block reserve, exported as a space_info
3947 */
3948 slot_count++;
3949
Chris Mason7fde62b2010-03-16 15:40:10 -04003950 /* space_slots == 0 means they are asking for a count */
3951 if (space_args.space_slots == 0) {
3952 space_args.total_spaces = slot_count;
3953 goto out;
3954 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003955
Dan Rosenberg51788b12011-02-14 16:04:23 -05003956 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003957
Chris Mason7fde62b2010-03-16 15:40:10 -04003958 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003959
Chris Mason7fde62b2010-03-16 15:40:10 -04003960 /* we generally have at most 6 or so space infos, one for each raid
3961 * level. So, a whole page should be more than enough for everyone
3962 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003963 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04003964 return -ENOMEM;
3965
3966 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01003967 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04003968 if (!dest)
3969 return -ENOMEM;
3970 dest_orig = dest;
3971
3972 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003973 for (i = 0; i < num_types; i++) {
3974 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003975
Dan Rosenberg51788b12011-02-14 16:04:23 -05003976 if (!slot_count)
3977 break;
3978
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003979 info = NULL;
3980 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003981 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003982 list) {
3983 if (tmp->flags == types[i]) {
3984 info = tmp;
3985 break;
3986 }
3987 }
3988 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003989
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003990 if (!info)
3991 continue;
3992 down_read(&info->groups_sem);
3993 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3994 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08003995 get_block_group_info(&info->block_groups[c],
3996 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003997 memcpy(dest, &space, sizeof(space));
3998 dest++;
3999 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004000 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004001 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004002 if (!slot_count)
4003 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004004 }
4005 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004006 }
Josef Bacik1406e432010-01-13 18:19:06 +00004007
David Sterba36523e952014-02-07 14:34:12 +01004008 /*
4009 * Add global block reserve
4010 */
4011 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004012 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004013
4014 spin_lock(&block_rsv->lock);
4015 space.total_bytes = block_rsv->size;
4016 space.used_bytes = block_rsv->size - block_rsv->reserved;
4017 spin_unlock(&block_rsv->lock);
4018 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4019 memcpy(dest, &space, sizeof(space));
4020 space_args.total_spaces++;
4021 }
4022
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004023 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004024 (arg + sizeof(struct btrfs_ioctl_space_args));
4025
4026 if (copy_to_user(user_dest, dest_orig, alloc_size))
4027 ret = -EFAULT;
4028
4029 kfree(dest_orig);
4030out:
4031 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004032 ret = -EFAULT;
4033
4034 return ret;
4035}
4036
Miao Xie9a8c28b2012-11-26 08:40:43 +00004037static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4038 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004039{
Sage Weil46204592010-10-29 15:41:32 -04004040 struct btrfs_trans_handle *trans;
4041 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004042 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004043
Miao Xied4edf392013-02-20 09:17:06 +00004044 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004045 if (IS_ERR(trans)) {
4046 if (PTR_ERR(trans) != -ENOENT)
4047 return PTR_ERR(trans);
4048
4049 /* No running transaction, don't bother */
4050 transid = root->fs_info->last_trans_committed;
4051 goto out;
4052 }
Sage Weil46204592010-10-29 15:41:32 -04004053 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004054 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004055 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004056 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004057 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004058 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004059out:
Sage Weil46204592010-10-29 15:41:32 -04004060 if (argp)
4061 if (copy_to_user(argp, &transid, sizeof(transid)))
4062 return -EFAULT;
4063 return 0;
4064}
4065
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004066static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004067 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004068{
Sage Weil46204592010-10-29 15:41:32 -04004069 u64 transid;
4070
4071 if (argp) {
4072 if (copy_from_user(&transid, argp, sizeof(transid)))
4073 return -EFAULT;
4074 } else {
4075 transid = 0; /* current trans */
4076 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004077 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004078}
4079
Miao Xieb8e95482012-11-26 08:48:01 +00004080static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004082 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004083 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004084 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004085
4086 if (!capable(CAP_SYS_ADMIN))
4087 return -EPERM;
4088
4089 sa = memdup_user(arg, sizeof(*sa));
4090 if (IS_ERR(sa))
4091 return PTR_ERR(sa);
4092
Miao Xieb8e95482012-11-26 08:48:01 +00004093 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4094 ret = mnt_want_write_file(file);
4095 if (ret)
4096 goto out;
4097 }
4098
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004099 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004100 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4101 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004102
4103 if (copy_to_user(arg, sa, sizeof(*sa)))
4104 ret = -EFAULT;
4105
Miao Xieb8e95482012-11-26 08:48:01 +00004106 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4107 mnt_drop_write_file(file);
4108out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004109 kfree(sa);
4110 return ret;
4111}
4112
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004113static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004114{
4115 if (!capable(CAP_SYS_ADMIN))
4116 return -EPERM;
4117
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004118 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004119}
4120
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004121static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004122 void __user *arg)
4123{
4124 struct btrfs_ioctl_scrub_args *sa;
4125 int ret;
4126
4127 if (!capable(CAP_SYS_ADMIN))
4128 return -EPERM;
4129
4130 sa = memdup_user(arg, sizeof(*sa));
4131 if (IS_ERR(sa))
4132 return PTR_ERR(sa);
4133
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004134 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004135
4136 if (copy_to_user(arg, sa, sizeof(*sa)))
4137 ret = -EFAULT;
4138
4139 kfree(sa);
4140 return ret;
4141}
4142
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004143static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004144 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004145{
4146 struct btrfs_ioctl_get_dev_stats *sa;
4147 int ret;
4148
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004149 sa = memdup_user(arg, sizeof(*sa));
4150 if (IS_ERR(sa))
4151 return PTR_ERR(sa);
4152
David Sterbab27f7c02012-06-22 06:30:39 -06004153 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4154 kfree(sa);
4155 return -EPERM;
4156 }
4157
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004158 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004159
4160 if (copy_to_user(arg, sa, sizeof(*sa)))
4161 ret = -EFAULT;
4162
4163 kfree(sa);
4164 return ret;
4165}
4166
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004167static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4168 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004169{
4170 struct btrfs_ioctl_dev_replace_args *p;
4171 int ret;
4172
4173 if (!capable(CAP_SYS_ADMIN))
4174 return -EPERM;
4175
4176 p = memdup_user(arg, sizeof(*p));
4177 if (IS_ERR(p))
4178 return PTR_ERR(p);
4179
4180 switch (p->cmd) {
4181 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004182 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004183 ret = -EROFS;
4184 goto out;
4185 }
David Sterba171938e2017-03-28 14:44:21 +02004186 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004187 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004188 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004189 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004190 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004191 }
4192 break;
4193 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004194 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004195 ret = 0;
4196 break;
4197 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004198 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004199 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004200 break;
4201 default:
4202 ret = -EINVAL;
4203 break;
4204 }
4205
4206 if (copy_to_user(arg, p, sizeof(*p)))
4207 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004208out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004209 kfree(p);
4210 return ret;
4211}
4212
Jan Schmidtd7728c92011-07-07 16:48:38 +02004213static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4214{
4215 int ret = 0;
4216 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004217 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004218 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004219 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004220 struct inode_fs_paths *ipath = NULL;
4221 struct btrfs_path *path;
4222
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004223 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004224 return -EPERM;
4225
4226 path = btrfs_alloc_path();
4227 if (!path) {
4228 ret = -ENOMEM;
4229 goto out;
4230 }
4231
4232 ipa = memdup_user(arg, sizeof(*ipa));
4233 if (IS_ERR(ipa)) {
4234 ret = PTR_ERR(ipa);
4235 ipa = NULL;
4236 goto out;
4237 }
4238
4239 size = min_t(u32, ipa->size, 4096);
4240 ipath = init_ipath(size, root, path);
4241 if (IS_ERR(ipath)) {
4242 ret = PTR_ERR(ipath);
4243 ipath = NULL;
4244 goto out;
4245 }
4246
4247 ret = paths_from_inode(ipa->inum, ipath);
4248 if (ret < 0)
4249 goto out;
4250
4251 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004252 rel_ptr = ipath->fspath->val[i] -
4253 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004254 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004255 }
4256
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004257 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4258 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004259 if (ret) {
4260 ret = -EFAULT;
4261 goto out;
4262 }
4263
4264out:
4265 btrfs_free_path(path);
4266 free_ipath(ipath);
4267 kfree(ipa);
4268
4269 return ret;
4270}
4271
4272static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4273{
4274 struct btrfs_data_container *inodes = ctx;
4275 const size_t c = 3 * sizeof(u64);
4276
4277 if (inodes->bytes_left >= c) {
4278 inodes->bytes_left -= c;
4279 inodes->val[inodes->elem_cnt] = inum;
4280 inodes->val[inodes->elem_cnt + 1] = offset;
4281 inodes->val[inodes->elem_cnt + 2] = root;
4282 inodes->elem_cnt += 3;
4283 } else {
4284 inodes->bytes_missing += c - inodes->bytes_left;
4285 inodes->bytes_left = 0;
4286 inodes->elem_missed += 3;
4287 }
4288
4289 return 0;
4290}
4291
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004292static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004293 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004294{
4295 int ret = 0;
4296 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004297 struct btrfs_ioctl_logical_ino_args *loi;
4298 struct btrfs_data_container *inodes = NULL;
4299 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004300 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004301
4302 if (!capable(CAP_SYS_ADMIN))
4303 return -EPERM;
4304
4305 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304306 if (IS_ERR(loi))
4307 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004308
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004309 if (version == 1) {
4310 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004311 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004312 } else {
4313 /* All reserved bits must be 0 for now */
4314 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4315 ret = -EINVAL;
4316 goto out_loi;
4317 }
4318 /* Only accept flags we have defined so far */
4319 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4320 ret = -EINVAL;
4321 goto out_loi;
4322 }
4323 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004324 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004325 }
4326
Jan Schmidtd7728c92011-07-07 16:48:38 +02004327 path = btrfs_alloc_path();
4328 if (!path) {
4329 ret = -ENOMEM;
4330 goto out;
4331 }
4332
Jan Schmidtd7728c92011-07-07 16:48:38 +02004333 inodes = init_data_container(size);
4334 if (IS_ERR(inodes)) {
4335 ret = PTR_ERR(inodes);
4336 inodes = NULL;
4337 goto out;
4338 }
4339
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004340 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004341 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004342 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004343 ret = -ENOENT;
4344 if (ret < 0)
4345 goto out;
4346
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004347 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4348 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004349 if (ret)
4350 ret = -EFAULT;
4351
4352out:
4353 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004354 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004355out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004356 kfree(loi);
4357
4358 return ret;
4359}
4360
David Sterba008ef092018-03-21 02:05:27 +01004361void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004362 struct btrfs_ioctl_balance_args *bargs)
4363{
4364 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4365
4366 bargs->flags = bctl->flags;
4367
David Sterba3009a622018-03-21 01:31:04 +01004368 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004369 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4370 if (atomic_read(&fs_info->balance_pause_req))
4371 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004372 if (atomic_read(&fs_info->balance_cancel_req))
4373 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004374
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004375 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4376 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4377 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004378
David Sterba008ef092018-03-21 02:05:27 +01004379 spin_lock(&fs_info->balance_lock);
4380 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4381 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004382}
4383
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004384static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004385{
Al Viro496ad9a2013-01-23 17:07:38 -05004386 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004387 struct btrfs_fs_info *fs_info = root->fs_info;
4388 struct btrfs_ioctl_balance_args *bargs;
4389 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004390 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004391 int ret;
4392
4393 if (!capable(CAP_SYS_ADMIN))
4394 return -EPERM;
4395
Liu Boe54bfa312012-06-29 03:58:48 -06004396 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004397 if (ret)
4398 return ret;
4399
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004400again:
David Sterba171938e2017-03-28 14:44:21 +02004401 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004402 mutex_lock(&fs_info->balance_mutex);
4403 need_unlock = true;
4404 goto locked;
4405 }
4406
4407 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004408 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004409 * (1) some other op is running
4410 * (2) balance is running
4411 * (3) balance is paused -- special case (think resume)
4412 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004413 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004414 if (fs_info->balance_ctl) {
4415 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004416 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004417 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004418 /*
4419 * Lock released to allow other waiters to continue,
4420 * we'll reexamine the status again.
4421 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004422 mutex_lock(&fs_info->balance_mutex);
4423
4424 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004425 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004426 /* this is (3) */
4427 need_unlock = false;
4428 goto locked;
4429 }
4430
4431 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004432 goto again;
4433 } else {
4434 /* this is (2) */
4435 mutex_unlock(&fs_info->balance_mutex);
4436 ret = -EINPROGRESS;
4437 goto out;
4438 }
4439 } else {
4440 /* this is (1) */
4441 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004442 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004443 goto out;
4444 }
4445
4446locked:
David Sterba171938e2017-03-28 14:44:21 +02004447 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004448
4449 if (arg) {
4450 bargs = memdup_user(arg, sizeof(*bargs));
4451 if (IS_ERR(bargs)) {
4452 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004453 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004454 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004455
4456 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4457 if (!fs_info->balance_ctl) {
4458 ret = -ENOTCONN;
4459 goto out_bargs;
4460 }
4461
4462 bctl = fs_info->balance_ctl;
4463 spin_lock(&fs_info->balance_lock);
4464 bctl->flags |= BTRFS_BALANCE_RESUME;
4465 spin_unlock(&fs_info->balance_lock);
4466
4467 goto do_balance;
4468 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004469 } else {
4470 bargs = NULL;
4471 }
4472
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004473 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004474 ret = -EINPROGRESS;
4475 goto out_bargs;
4476 }
4477
David Sterba8d2db782015-11-04 15:38:29 +01004478 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004479 if (!bctl) {
4480 ret = -ENOMEM;
4481 goto out_bargs;
4482 }
4483
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004484 if (arg) {
4485 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4486 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4487 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4488
4489 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004490 } else {
4491 /* balance everything - no filters */
4492 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004493 }
4494
David Sterba8eb93452015-10-12 16:55:54 +02004495 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4496 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004497 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004498 }
4499
Ilya Dryomovde322262012-01-16 22:04:49 +02004500do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004501 /*
David Sterba149196a2018-03-20 20:23:09 +01004502 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4503 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4504 * restriper was paused all the way until unmount, in free_fs_info.
4505 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004506 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004507 need_unlock = false;
4508
David Sterba6fcf6e22018-05-07 17:44:03 +02004509 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004510 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004511
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004512 if (arg) {
4513 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4514 ret = -EFAULT;
4515 }
4516
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004517out_bctl:
4518 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004519out_bargs:
4520 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004521out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004522 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004523 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004524 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004525out:
Liu Boe54bfa312012-06-29 03:58:48 -06004526 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004527 return ret;
4528}
4529
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004530static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004531{
4532 if (!capable(CAP_SYS_ADMIN))
4533 return -EPERM;
4534
4535 switch (cmd) {
4536 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004537 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004538 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004539 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004540 }
4541
4542 return -EINVAL;
4543}
4544
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004545static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004546 void __user *arg)
4547{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004548 struct btrfs_ioctl_balance_args *bargs;
4549 int ret = 0;
4550
4551 if (!capable(CAP_SYS_ADMIN))
4552 return -EPERM;
4553
4554 mutex_lock(&fs_info->balance_mutex);
4555 if (!fs_info->balance_ctl) {
4556 ret = -ENOTCONN;
4557 goto out;
4558 }
4559
David Sterba8d2db782015-11-04 15:38:29 +01004560 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004561 if (!bargs) {
4562 ret = -ENOMEM;
4563 goto out;
4564 }
4565
David Sterba008ef092018-03-21 02:05:27 +01004566 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004567
4568 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4569 ret = -EFAULT;
4570
4571 kfree(bargs);
4572out:
4573 mutex_unlock(&fs_info->balance_mutex);
4574 return ret;
4575}
4576
Miao Xie905b0dd2012-11-26 08:50:11 +00004577static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004578{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004579 struct inode *inode = file_inode(file);
4580 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004581 struct btrfs_ioctl_quota_ctl_args *sa;
4582 struct btrfs_trans_handle *trans = NULL;
4583 int ret;
4584 int err;
4585
4586 if (!capable(CAP_SYS_ADMIN))
4587 return -EPERM;
4588
Miao Xie905b0dd2012-11-26 08:50:11 +00004589 ret = mnt_want_write_file(file);
4590 if (ret)
4591 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004592
4593 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004594 if (IS_ERR(sa)) {
4595 ret = PTR_ERR(sa);
4596 goto drop_write;
4597 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004598
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004599 down_write(&fs_info->subvol_sem);
4600 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004601 if (IS_ERR(trans)) {
4602 ret = PTR_ERR(trans);
4603 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004604 }
4605
4606 switch (sa->cmd) {
4607 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004608 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004609 break;
4610 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004611 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004612 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004613 default:
4614 ret = -EINVAL;
4615 break;
4616 }
4617
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004618 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004619 if (err && !ret)
4620 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004621out:
4622 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004623 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004624drop_write:
4625 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004626 return ret;
4627}
4628
Miao Xie905b0dd2012-11-26 08:50:11 +00004629static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004630{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004631 struct inode *inode = file_inode(file);
4632 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4633 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004634 struct btrfs_ioctl_qgroup_assign_args *sa;
4635 struct btrfs_trans_handle *trans;
4636 int ret;
4637 int err;
4638
4639 if (!capable(CAP_SYS_ADMIN))
4640 return -EPERM;
4641
Miao Xie905b0dd2012-11-26 08:50:11 +00004642 ret = mnt_want_write_file(file);
4643 if (ret)
4644 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004645
4646 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004647 if (IS_ERR(sa)) {
4648 ret = PTR_ERR(sa);
4649 goto drop_write;
4650 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004651
4652 trans = btrfs_join_transaction(root);
4653 if (IS_ERR(trans)) {
4654 ret = PTR_ERR(trans);
4655 goto out;
4656 }
4657
Arne Jansen5d13a372011-09-14 15:53:51 +02004658 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004659 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004660 sa->src, sa->dst);
4661 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004662 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004663 sa->src, sa->dst);
4664 }
4665
Qu Wenruoe082f562015-02-27 16:24:28 +08004666 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004667 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004668 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004669 btrfs_handle_fs_error(fs_info, err,
4670 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004671 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004672 if (err && !ret)
4673 ret = err;
4674
4675out:
4676 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004677drop_write:
4678 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004679 return ret;
4680}
4681
Miao Xie905b0dd2012-11-26 08:50:11 +00004682static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004683{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004684 struct inode *inode = file_inode(file);
4685 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4686 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004687 struct btrfs_ioctl_qgroup_create_args *sa;
4688 struct btrfs_trans_handle *trans;
4689 int ret;
4690 int err;
4691
4692 if (!capable(CAP_SYS_ADMIN))
4693 return -EPERM;
4694
Miao Xie905b0dd2012-11-26 08:50:11 +00004695 ret = mnt_want_write_file(file);
4696 if (ret)
4697 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004698
4699 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004700 if (IS_ERR(sa)) {
4701 ret = PTR_ERR(sa);
4702 goto drop_write;
4703 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004704
Miao Xied86e56c2012-11-15 11:35:41 +00004705 if (!sa->qgroupid) {
4706 ret = -EINVAL;
4707 goto out;
4708 }
4709
Arne Jansen5d13a372011-09-14 15:53:51 +02004710 trans = btrfs_join_transaction(root);
4711 if (IS_ERR(trans)) {
4712 ret = PTR_ERR(trans);
4713 goto out;
4714 }
4715
Arne Jansen5d13a372011-09-14 15:53:51 +02004716 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004717 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004718 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004719 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004720 }
4721
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004722 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004723 if (err && !ret)
4724 ret = err;
4725
4726out:
4727 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004728drop_write:
4729 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004730 return ret;
4731}
4732
Miao Xie905b0dd2012-11-26 08:50:11 +00004733static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004734{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004735 struct inode *inode = file_inode(file);
4736 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4737 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004738 struct btrfs_ioctl_qgroup_limit_args *sa;
4739 struct btrfs_trans_handle *trans;
4740 int ret;
4741 int err;
4742 u64 qgroupid;
4743
4744 if (!capable(CAP_SYS_ADMIN))
4745 return -EPERM;
4746
Miao Xie905b0dd2012-11-26 08:50:11 +00004747 ret = mnt_want_write_file(file);
4748 if (ret)
4749 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004750
4751 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004752 if (IS_ERR(sa)) {
4753 ret = PTR_ERR(sa);
4754 goto drop_write;
4755 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004756
4757 trans = btrfs_join_transaction(root);
4758 if (IS_ERR(trans)) {
4759 ret = PTR_ERR(trans);
4760 goto out;
4761 }
4762
4763 qgroupid = sa->qgroupid;
4764 if (!qgroupid) {
4765 /* take the current subvol as qgroup */
4766 qgroupid = root->root_key.objectid;
4767 }
4768
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004769 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004770
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004771 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004772 if (err && !ret)
4773 ret = err;
4774
4775out:
4776 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004777drop_write:
4778 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004779 return ret;
4780}
4781
Jan Schmidt2f232032013-04-25 16:04:51 +00004782static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4783{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004784 struct inode *inode = file_inode(file);
4785 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004786 struct btrfs_ioctl_quota_rescan_args *qsa;
4787 int ret;
4788
4789 if (!capable(CAP_SYS_ADMIN))
4790 return -EPERM;
4791
4792 ret = mnt_want_write_file(file);
4793 if (ret)
4794 return ret;
4795
4796 qsa = memdup_user(arg, sizeof(*qsa));
4797 if (IS_ERR(qsa)) {
4798 ret = PTR_ERR(qsa);
4799 goto drop_write;
4800 }
4801
4802 if (qsa->flags) {
4803 ret = -EINVAL;
4804 goto out;
4805 }
4806
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004807 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004808
4809out:
4810 kfree(qsa);
4811drop_write:
4812 mnt_drop_write_file(file);
4813 return ret;
4814}
4815
4816static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4817{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004818 struct inode *inode = file_inode(file);
4819 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004820 struct btrfs_ioctl_quota_rescan_args *qsa;
4821 int ret = 0;
4822
4823 if (!capable(CAP_SYS_ADMIN))
4824 return -EPERM;
4825
David Sterba8d2db782015-11-04 15:38:29 +01004826 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004827 if (!qsa)
4828 return -ENOMEM;
4829
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004830 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00004831 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004832 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004833 }
4834
4835 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4836 ret = -EFAULT;
4837
4838 kfree(qsa);
4839 return ret;
4840}
4841
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004842static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4843{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004844 struct inode *inode = file_inode(file);
4845 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004846
4847 if (!capable(CAP_SYS_ADMIN))
4848 return -EPERM;
4849
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004850 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004851}
4852
Hugo Millsabccd002014-01-30 20:17:00 +00004853static long _btrfs_ioctl_set_received_subvol(struct file *file,
4854 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004855{
Al Viro496ad9a2013-01-23 17:07:38 -05004856 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004858 struct btrfs_root *root = BTRFS_I(inode)->root;
4859 struct btrfs_root_item *root_item = &root->root_item;
4860 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004861 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004862 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004863 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004864
David Sterbabd60ea02014-01-16 15:50:22 +01004865 if (!inode_owner_or_capable(inode))
4866 return -EPERM;
4867
Alexander Block8ea05e32012-07-25 17:35:53 +02004868 ret = mnt_want_write_file(file);
4869 if (ret < 0)
4870 return ret;
4871
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004872 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004873
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004874 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004875 ret = -EINVAL;
4876 goto out;
4877 }
4878
4879 if (btrfs_root_readonly(root)) {
4880 ret = -EROFS;
4881 goto out;
4882 }
4883
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004884 /*
4885 * 1 - root item
4886 * 2 - uuid items (received uuid + subvol uuid)
4887 */
4888 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004889 if (IS_ERR(trans)) {
4890 ret = PTR_ERR(trans);
4891 trans = NULL;
4892 goto out;
4893 }
4894
4895 sa->rtransid = trans->transid;
4896 sa->rtime.sec = ct.tv_sec;
4897 sa->rtime.nsec = ct.tv_nsec;
4898
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004899 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4900 BTRFS_UUID_SIZE);
4901 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004902 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4903 ret = btrfs_uuid_tree_rem(trans, fs_info,
4904 root_item->received_uuid,
4905 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4906 root->root_key.objectid);
4907 if (ret && ret != -ENOENT) {
4908 btrfs_abort_transaction(trans, ret);
4909 btrfs_end_transaction(trans);
4910 goto out;
4911 }
4912 }
Alexander Block8ea05e32012-07-25 17:35:53 +02004913 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4914 btrfs_set_root_stransid(root_item, sa->stransid);
4915 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004916 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4917 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4918 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4919 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004920
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02004922 &root->root_key, &root->root_item);
4923 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004924 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004925 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004926 }
4927 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004928 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004929 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4930 root->root_key.objectid);
4931 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004932 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03004933 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004934 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004935 }
4936 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004937 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00004938out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004939 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00004940 mnt_drop_write_file(file);
4941 return ret;
4942}
4943
4944#ifdef CONFIG_64BIT
4945static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4946 void __user *arg)
4947{
4948 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4949 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4950 int ret = 0;
4951
4952 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304953 if (IS_ERR(args32))
4954 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00004955
David Sterba8d2db782015-11-04 15:38:29 +01004956 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03004957 if (!args64) {
4958 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00004959 goto out;
4960 }
4961
4962 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4963 args64->stransid = args32->stransid;
4964 args64->rtransid = args32->rtransid;
4965 args64->stime.sec = args32->stime.sec;
4966 args64->stime.nsec = args32->stime.nsec;
4967 args64->rtime.sec = args32->rtime.sec;
4968 args64->rtime.nsec = args32->rtime.nsec;
4969 args64->flags = args32->flags;
4970
4971 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4972 if (ret)
4973 goto out;
4974
4975 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4976 args32->stransid = args64->stransid;
4977 args32->rtransid = args64->rtransid;
4978 args32->stime.sec = args64->stime.sec;
4979 args32->stime.nsec = args64->stime.nsec;
4980 args32->rtime.sec = args64->rtime.sec;
4981 args32->rtime.nsec = args64->rtime.nsec;
4982 args32->flags = args64->flags;
4983
4984 ret = copy_to_user(arg, args32, sizeof(*args32));
4985 if (ret)
4986 ret = -EFAULT;
4987
4988out:
4989 kfree(args32);
4990 kfree(args64);
4991 return ret;
4992}
4993#endif
4994
4995static long btrfs_ioctl_set_received_subvol(struct file *file,
4996 void __user *arg)
4997{
4998 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4999 int ret = 0;
5000
5001 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305002 if (IS_ERR(sa))
5003 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005004
5005 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5006
5007 if (ret)
5008 goto out;
5009
Alexander Block8ea05e32012-07-25 17:35:53 +02005010 ret = copy_to_user(arg, sa, sizeof(*sa));
5011 if (ret)
5012 ret = -EFAULT;
5013
5014out:
5015 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005016 return ret;
5017}
5018
jeff.liu867ab662013-01-05 02:48:01 +00005019static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5020{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005021 struct inode *inode = file_inode(file);
5022 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005023 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005024 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005025 char label[BTRFS_LABEL_SIZE];
5026
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005027 spin_lock(&fs_info->super_lock);
5028 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5029 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005030
5031 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005032
5033 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005034 btrfs_warn(fs_info,
5035 "label is too long, return the first %zu bytes",
5036 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005037 }
5038
jeff.liu867ab662013-01-05 02:48:01 +00005039 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005040
5041 return ret ? -EFAULT : 0;
5042}
5043
jeff.liua8bfd4a2013-01-05 02:48:08 +00005044static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5045{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005046 struct inode *inode = file_inode(file);
5047 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5048 struct btrfs_root *root = BTRFS_I(inode)->root;
5049 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005050 struct btrfs_trans_handle *trans;
5051 char label[BTRFS_LABEL_SIZE];
5052 int ret;
5053
5054 if (!capable(CAP_SYS_ADMIN))
5055 return -EPERM;
5056
5057 if (copy_from_user(label, arg, sizeof(label)))
5058 return -EFAULT;
5059
5060 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005061 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005062 "unable to set label with more than %d bytes",
5063 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005064 return -EINVAL;
5065 }
5066
5067 ret = mnt_want_write_file(file);
5068 if (ret)
5069 return ret;
5070
jeff.liua8bfd4a2013-01-05 02:48:08 +00005071 trans = btrfs_start_transaction(root, 0);
5072 if (IS_ERR(trans)) {
5073 ret = PTR_ERR(trans);
5074 goto out_unlock;
5075 }
5076
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005077 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005078 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005079 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005080 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005081
5082out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005083 mnt_drop_write_file(file);
5084 return ret;
5085}
5086
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005087#define INIT_FEATURE_FLAGS(suffix) \
5088 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5089 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5090 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5091
David Sterbad5131b62016-02-17 15:26:27 +01005092int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005093{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005094 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005095 INIT_FEATURE_FLAGS(SUPP),
5096 INIT_FEATURE_FLAGS(SAFE_SET),
5097 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5098 };
5099
5100 if (copy_to_user(arg, &features, sizeof(features)))
5101 return -EFAULT;
5102
5103 return 0;
5104}
5105
5106static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5107{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005108 struct inode *inode = file_inode(file);
5109 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5110 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005111 struct btrfs_ioctl_feature_flags features;
5112
5113 features.compat_flags = btrfs_super_compat_flags(super_block);
5114 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5115 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5116
5117 if (copy_to_user(arg, &features, sizeof(features)))
5118 return -EFAULT;
5119
5120 return 0;
5121}
5122
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005123static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005124 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005125 u64 change_mask, u64 flags, u64 supported_flags,
5126 u64 safe_set, u64 safe_clear)
5127{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005128 const char *type = btrfs_feature_set_names[set];
5129 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005130 u64 disallowed, unsupported;
5131 u64 set_mask = flags & change_mask;
5132 u64 clear_mask = ~flags & change_mask;
5133
5134 unsupported = set_mask & ~supported_flags;
5135 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005136 names = btrfs_printable_features(set, unsupported);
5137 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005138 btrfs_warn(fs_info,
5139 "this kernel does not support the %s feature bit%s",
5140 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005141 kfree(names);
5142 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005143 btrfs_warn(fs_info,
5144 "this kernel does not support %s bits 0x%llx",
5145 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005146 return -EOPNOTSUPP;
5147 }
5148
5149 disallowed = set_mask & ~safe_set;
5150 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005151 names = btrfs_printable_features(set, disallowed);
5152 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005153 btrfs_warn(fs_info,
5154 "can't set the %s feature bit%s while mounted",
5155 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005156 kfree(names);
5157 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005158 btrfs_warn(fs_info,
5159 "can't set %s bits 0x%llx while mounted",
5160 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005161 return -EPERM;
5162 }
5163
5164 disallowed = clear_mask & ~safe_clear;
5165 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005166 names = btrfs_printable_features(set, disallowed);
5167 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005168 btrfs_warn(fs_info,
5169 "can't clear the %s feature bit%s while mounted",
5170 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005171 kfree(names);
5172 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005173 btrfs_warn(fs_info,
5174 "can't clear %s bits 0x%llx while mounted",
5175 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005176 return -EPERM;
5177 }
5178
5179 return 0;
5180}
5181
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005182#define check_feature(fs_info, change_mask, flags, mask_base) \
5183check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005184 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5185 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5186 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5187
5188static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5189{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005190 struct inode *inode = file_inode(file);
5191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5192 struct btrfs_root *root = BTRFS_I(inode)->root;
5193 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005194 struct btrfs_ioctl_feature_flags flags[2];
5195 struct btrfs_trans_handle *trans;
5196 u64 newflags;
5197 int ret;
5198
5199 if (!capable(CAP_SYS_ADMIN))
5200 return -EPERM;
5201
5202 if (copy_from_user(flags, arg, sizeof(flags)))
5203 return -EFAULT;
5204
5205 /* Nothing to do */
5206 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5207 !flags[0].incompat_flags)
5208 return 0;
5209
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005210 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005211 flags[1].compat_flags, COMPAT);
5212 if (ret)
5213 return ret;
5214
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005215 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005216 flags[1].compat_ro_flags, COMPAT_RO);
5217 if (ret)
5218 return ret;
5219
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005220 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005221 flags[1].incompat_flags, INCOMPAT);
5222 if (ret)
5223 return ret;
5224
David Sterba7ab19622016-05-04 11:32:00 +02005225 ret = mnt_want_write_file(file);
5226 if (ret)
5227 return ret;
5228
David Sterba8051aa12014-02-07 14:34:04 +01005229 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005230 if (IS_ERR(trans)) {
5231 ret = PTR_ERR(trans);
5232 goto out_drop_write;
5233 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005235 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005236 newflags = btrfs_super_compat_flags(super_block);
5237 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5238 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5239 btrfs_set_super_compat_flags(super_block, newflags);
5240
5241 newflags = btrfs_super_compat_ro_flags(super_block);
5242 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5243 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5244 btrfs_set_super_compat_ro_flags(super_block, newflags);
5245
5246 newflags = btrfs_super_incompat_flags(super_block);
5247 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5248 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5249 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005250 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005251
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005252 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005253out_drop_write:
5254 mnt_drop_write_file(file);
5255
5256 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005257}
5258
Josef Bacik2351f432017-09-27 10:43:13 -04005259static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5260{
5261 struct btrfs_ioctl_send_args *arg;
5262 int ret;
5263
5264 if (compat) {
5265#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5266 struct btrfs_ioctl_send_args_32 args32;
5267
5268 ret = copy_from_user(&args32, argp, sizeof(args32));
5269 if (ret)
5270 return -EFAULT;
5271 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5272 if (!arg)
5273 return -ENOMEM;
5274 arg->send_fd = args32.send_fd;
5275 arg->clone_sources_count = args32.clone_sources_count;
5276 arg->clone_sources = compat_ptr(args32.clone_sources);
5277 arg->parent_root = args32.parent_root;
5278 arg->flags = args32.flags;
5279 memcpy(arg->reserved, args32.reserved,
5280 sizeof(args32.reserved));
5281#else
5282 return -ENOTTY;
5283#endif
5284 } else {
5285 arg = memdup_user(argp, sizeof(*arg));
5286 if (IS_ERR(arg))
5287 return PTR_ERR(arg);
5288 }
5289 ret = btrfs_ioctl_send(file, arg);
5290 kfree(arg);
5291 return ret;
5292}
5293
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005294long btrfs_ioctl(struct file *file, unsigned int
5295 cmd, unsigned long arg)
5296{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005297 struct inode *inode = file_inode(file);
5298 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5299 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005300 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005301
5302 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005303 case FS_IOC_GETFLAGS:
5304 return btrfs_ioctl_getflags(file, argp);
5305 case FS_IOC_SETFLAGS:
5306 return btrfs_ioctl_setflags(file, argp);
5307 case FS_IOC_GETVERSION:
5308 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005309 case FITRIM:
5310 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005311 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005312 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005313 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005314 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005315 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005316 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005317 case BTRFS_IOC_SUBVOL_CREATE_V2:
5318 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005319 case BTRFS_IOC_SNAP_DESTROY:
5320 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005321 case BTRFS_IOC_SUBVOL_GETFLAGS:
5322 return btrfs_ioctl_subvol_getflags(file, argp);
5323 case BTRFS_IOC_SUBVOL_SETFLAGS:
5324 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005325 case BTRFS_IOC_DEFAULT_SUBVOL:
5326 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005327 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005328 return btrfs_ioctl_defrag(file, NULL);
5329 case BTRFS_IOC_DEFRAG_RANGE:
5330 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005331 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005332 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005333 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005334 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005335 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005336 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005337 case BTRFS_IOC_RM_DEV_V2:
5338 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005339 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005340 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005341 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005342 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005343 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005344 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005345 case BTRFS_IOC_TREE_SEARCH:
5346 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005347 case BTRFS_IOC_TREE_SEARCH_V2:
5348 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005349 case BTRFS_IOC_INO_LOOKUP:
5350 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005351 case BTRFS_IOC_INO_PATHS:
5352 return btrfs_ioctl_ino_to_path(root, argp);
5353 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005354 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5355 case BTRFS_IOC_LOGICAL_INO_V2:
5356 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005357 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005358 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005359 case BTRFS_IOC_SYNC: {
5360 int ret;
5361
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005362 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005363 if (ret)
5364 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005365 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005366 /*
5367 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005368 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005369 * processing uncleaned subvols.
5370 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005371 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005372 return ret;
5373 }
Sage Weil46204592010-10-29 15:41:32 -04005374 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005375 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005376 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005377 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005378 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005379 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005380 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005381 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005382 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005383 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005384 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005385 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005386 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005387 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005388 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005389 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005390 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5391 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005392#ifdef CONFIG_64BIT
5393 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5394 return btrfs_ioctl_set_received_subvol_32(file, argp);
5395#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005396 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005397 return _btrfs_ioctl_send(file, argp, false);
5398#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5399 case BTRFS_IOC_SEND_32:
5400 return _btrfs_ioctl_send(file, argp, true);
5401#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005402 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005403 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005404 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005405 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005406 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005407 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005408 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005409 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005410 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005411 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005412 case BTRFS_IOC_QUOTA_RESCAN:
5413 return btrfs_ioctl_quota_rescan(file, argp);
5414 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5415 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005416 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5417 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005418 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005419 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005420 case BTRFS_IOC_GET_FSLABEL:
5421 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005422 case BTRFS_IOC_SET_FSLABEL:
5423 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005424 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005425 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005426 case BTRFS_IOC_GET_FEATURES:
5427 return btrfs_ioctl_get_features(file, argp);
5428 case BTRFS_IOC_SET_FEATURES:
5429 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005430 case FS_IOC_FSGETXATTR:
5431 return btrfs_ioctl_fsgetxattr(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005432 }
5433
5434 return -ENOTTY;
5435}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005436
5437#ifdef CONFIG_COMPAT
5438long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5439{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005440 /*
5441 * These all access 32-bit values anyway so no further
5442 * handling is necessary.
5443 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005444 switch (cmd) {
5445 case FS_IOC32_GETFLAGS:
5446 cmd = FS_IOC_GETFLAGS;
5447 break;
5448 case FS_IOC32_SETFLAGS:
5449 cmd = FS_IOC_SETFLAGS;
5450 break;
5451 case FS_IOC32_GETVERSION:
5452 cmd = FS_IOC_GETVERSION;
5453 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005454 }
5455
5456 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5457}
5458#endif