blob: b9963d94d7275993ed08ed331d2c9a57be253f1b [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040036#include <linux/compat.h>
37#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040038#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040039#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020040#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000042#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020043#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000044#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070045#include <linux/uaccess.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040050#include "print-tree.h"
51#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040052#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080053#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020054#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040055#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020056#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010057#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000058#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040059#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070060#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000061#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080062#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040063
Hugo Millsabccd002014-01-30 20:17:00 +000064#ifdef CONFIG_64BIT
65/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
66 * structures are incorrect, as the timespec structure from userspace
67 * is 4 bytes too small. We define these alternatives here to teach
68 * the kernel about the 32-bit struct packing.
69 */
70struct btrfs_ioctl_timespec_32 {
71 __u64 sec;
72 __u32 nsec;
73} __attribute__ ((__packed__));
74
75struct btrfs_ioctl_received_subvol_args_32 {
76 char uuid[BTRFS_UUID_SIZE]; /* in */
77 __u64 stransid; /* in */
78 __u64 rtransid; /* out */
79 struct btrfs_ioctl_timespec_32 stime; /* in */
80 struct btrfs_ioctl_timespec_32 rtime; /* out */
81 __u64 flags; /* in */
82 __u64 reserved[16]; /* in */
83} __attribute__ ((__packed__));
84
85#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
86 struct btrfs_ioctl_received_subvol_args_32)
87#endif
88
89
Mark Fasheh416161d2013-08-06 11:42:51 -070090static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070091 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
92 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070093
Christoph Hellwig6cbff002009-04-17 10:37:41 +020094/* Mask out flags that are inappropriate for the given type of inode. */
95static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
96{
97 if (S_ISDIR(mode))
98 return flags;
99 else if (S_ISREG(mode))
100 return flags & ~FS_DIRSYNC_FL;
101 else
102 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
103}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400104
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105/*
106 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
107 */
108static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
109{
110 unsigned int iflags = 0;
111
112 if (flags & BTRFS_INODE_SYNC)
113 iflags |= FS_SYNC_FL;
114 if (flags & BTRFS_INODE_IMMUTABLE)
115 iflags |= FS_IMMUTABLE_FL;
116 if (flags & BTRFS_INODE_APPEND)
117 iflags |= FS_APPEND_FL;
118 if (flags & BTRFS_INODE_NODUMP)
119 iflags |= FS_NODUMP_FL;
120 if (flags & BTRFS_INODE_NOATIME)
121 iflags |= FS_NOATIME_FL;
122 if (flags & BTRFS_INODE_DIRSYNC)
123 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000124 if (flags & BTRFS_INODE_NODATACOW)
125 iflags |= FS_NOCOW_FL;
126
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900127 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000128 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900129 else if (flags & BTRFS_INODE_COMPRESS)
130 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200131
132 return iflags;
133}
134
135/*
136 * Update inode->i_flags based on the btrfs internal flags.
137 */
138void btrfs_update_iflags(struct inode *inode)
139{
140 struct btrfs_inode *ip = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100141 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142
143 if (ip->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145 if (ip->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147 if (ip->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200149 if (ip->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200151 if (ip->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100152 new_fl |= S_DIRSYNC;
153
154 set_mask_bits(&inode->i_flags,
155 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
156 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157}
158
159/*
160 * Inherit flags from the parent inode.
161 *
Josef Bacike27425d2011-09-27 11:01:30 -0400162 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200163 */
164void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
165{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400166 unsigned int flags;
167
168 if (!dir)
169 return;
170
171 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200172
Josef Bacike27425d2011-09-27 11:01:30 -0400173 if (flags & BTRFS_INODE_NOCOMPRESS) {
174 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
175 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
176 } else if (flags & BTRFS_INODE_COMPRESS) {
177 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
178 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
179 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200180
Liu Bo213490b2012-09-11 08:33:50 -0600181 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400182 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600183 if (S_ISREG(inode->i_mode))
184 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
185 }
Josef Bacike27425d2011-09-27 11:01:30 -0400186
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200187 btrfs_update_iflags(inode);
188}
189
190static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
191{
Al Viro496ad9a2013-01-23 17:07:38 -0500192 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
194
195 if (copy_to_user(arg, &flags, sizeof(flags)))
196 return -EFAULT;
197 return 0;
198}
199
Liu Bo75e7cb72011-03-22 10:12:20 +0000200static int check_flags(unsigned int flags)
201{
202 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
203 FS_NOATIME_FL | FS_NODUMP_FL | \
204 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000205 FS_NOCOMP_FL | FS_COMPR_FL |
206 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000207 return -EOPNOTSUPP;
208
209 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
210 return -EINVAL;
211
Liu Bo75e7cb72011-03-22 10:12:20 +0000212 return 0;
213}
214
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200215static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
216{
Al Viro496ad9a2013-01-23 17:07:38 -0500217 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400218 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200219 struct btrfs_inode *ip = BTRFS_I(inode);
220 struct btrfs_root *root = ip->root;
221 struct btrfs_trans_handle *trans;
222 unsigned int flags, oldflags;
223 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800224 u64 ip_oldflags;
225 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600226 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200227
David Sterbabd60ea02014-01-16 15:50:22 +0100228 if (!inode_owner_or_capable(inode))
229 return -EPERM;
230
Li Zefanb83cc962010-12-20 16:04:08 +0800231 if (btrfs_root_readonly(root))
232 return -EROFS;
233
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200234 if (copy_from_user(&flags, arg, sizeof(flags)))
235 return -EFAULT;
236
Liu Bo75e7cb72011-03-22 10:12:20 +0000237 ret = check_flags(flags);
238 if (ret)
239 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200240
Jan Karae7848682012-06-12 16:20:32 +0200241 ret = mnt_want_write_file(file);
242 if (ret)
243 return ret;
244
Al Viro59551022016-01-22 15:40:57 -0500245 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200246
Li Zefanf062abf02011-12-29 13:36:45 +0800247 ip_oldflags = ip->flags;
248 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600249 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800250
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200251 flags = btrfs_mask_flags(inode->i_mode, flags);
252 oldflags = btrfs_flags_to_ioctl(ip->flags);
253 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
254 if (!capable(CAP_LINUX_IMMUTABLE)) {
255 ret = -EPERM;
256 goto out_unlock;
257 }
258 }
259
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200260 if (flags & FS_SYNC_FL)
261 ip->flags |= BTRFS_INODE_SYNC;
262 else
263 ip->flags &= ~BTRFS_INODE_SYNC;
264 if (flags & FS_IMMUTABLE_FL)
265 ip->flags |= BTRFS_INODE_IMMUTABLE;
266 else
267 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
268 if (flags & FS_APPEND_FL)
269 ip->flags |= BTRFS_INODE_APPEND;
270 else
271 ip->flags &= ~BTRFS_INODE_APPEND;
272 if (flags & FS_NODUMP_FL)
273 ip->flags |= BTRFS_INODE_NODUMP;
274 else
275 ip->flags &= ~BTRFS_INODE_NODUMP;
276 if (flags & FS_NOATIME_FL)
277 ip->flags |= BTRFS_INODE_NOATIME;
278 else
279 ip->flags &= ~BTRFS_INODE_NOATIME;
280 if (flags & FS_DIRSYNC_FL)
281 ip->flags |= BTRFS_INODE_DIRSYNC;
282 else
283 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600284 if (flags & FS_NOCOW_FL) {
285 if (S_ISREG(mode)) {
286 /*
287 * It's safe to turn csums off here, no extents exist.
288 * Otherwise we want the flag to reflect the real COW
289 * status of the file and will not set it.
290 */
291 if (inode->i_size == 0)
292 ip->flags |= BTRFS_INODE_NODATACOW
293 | BTRFS_INODE_NODATASUM;
294 } else {
295 ip->flags |= BTRFS_INODE_NODATACOW;
296 }
297 } else {
298 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400299 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600300 */
301 if (S_ISREG(mode)) {
302 if (inode->i_size == 0)
303 ip->flags &= ~(BTRFS_INODE_NODATACOW
304 | BTRFS_INODE_NODATASUM);
305 } else {
306 ip->flags &= ~BTRFS_INODE_NODATACOW;
307 }
308 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200309
Liu Bo75e7cb72011-03-22 10:12:20 +0000310 /*
311 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
312 * flag may be changed automatically if compression code won't make
313 * things smaller.
314 */
315 if (flags & FS_NOCOMP_FL) {
316 ip->flags &= ~BTRFS_INODE_COMPRESS;
317 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000318
319 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
320 if (ret && ret != -ENODATA)
321 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000322 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000323 const char *comp;
324
Liu Bo75e7cb72011-03-22 10:12:20 +0000325 ip->flags |= BTRFS_INODE_COMPRESS;
326 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000327
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400328 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000329 comp = "lzo";
Nick Terrell5c1aab12017-08-09 19:39:02 -0700330 else if (fs_info->compress_type == BTRFS_COMPRESS_ZLIB)
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000331 comp = "zlib";
Nick Terrell5c1aab12017-08-09 19:39:02 -0700332 else
333 comp = "zstd";
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000334 ret = btrfs_set_prop(inode, "btrfs.compression",
335 comp, strlen(comp), 0);
336 if (ret)
337 goto out_drop;
338
Li Zefanebcb9042011-04-15 03:03:17 +0000339 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100340 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
341 if (ret && ret != -ENODATA)
342 goto out_drop;
Li Zefanebcb9042011-04-15 03:03:17 +0000343 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000344 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200345
Li Zefan4da6f1a2011-12-29 13:39:50 +0800346 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800347 if (IS_ERR(trans)) {
348 ret = PTR_ERR(trans);
349 goto out_drop;
350 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200351
Li Zefan306424cc2011-12-14 20:12:02 -0500352 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400353 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700354 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200355 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200356
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400357 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800358 out_drop:
359 if (ret) {
360 ip->flags = ip_oldflags;
361 inode->i_flags = i_oldflags;
362 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200363
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200364 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500365 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200366 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000367 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200368}
369
370static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
371{
Al Viro496ad9a2013-01-23 17:07:38 -0500372 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200373
374 return put_user(inode->i_generation, arg);
375}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400376
Li Dongyangf7039b12011-03-24 10:24:28 +0000377static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
378{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400379 struct inode *inode = file_inode(file);
380 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000381 struct btrfs_device *device;
382 struct request_queue *q;
383 struct fstrim_range range;
384 u64 minlen = ULLONG_MAX;
385 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500386 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000387 int ret;
388
389 if (!capable(CAP_SYS_ADMIN))
390 return -EPERM;
391
Xiao Guangrong1f781602011-04-20 10:09:16 +0000392 rcu_read_lock();
393 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
394 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000395 if (!device->bdev)
396 continue;
397 q = bdev_get_queue(device->bdev);
398 if (blk_queue_discard(q)) {
399 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100400 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000401 minlen);
402 }
403 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000404 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200405
Li Dongyangf7039b12011-03-24 10:24:28 +0000406 if (!num_devices)
407 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000408 if (copy_from_user(&range, arg, sizeof(range)))
409 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000410 if (range.start > total_bytes ||
411 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200412 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000413
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200414 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000415 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400416 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000417 if (ret < 0)
418 return ret;
419
420 if (copy_to_user(arg, &range, sizeof(range)))
421 return -EFAULT;
422
423 return 0;
424}
425
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200426int btrfs_is_empty_uuid(u8 *uuid)
427{
Chris Mason46e0f662013-11-15 12:14:55 +0100428 int i;
429
430 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
431 if (uuid[i])
432 return 0;
433 }
434 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200435}
436
Miao Xied5c12072013-02-28 10:04:33 +0000437static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400438 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100439 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200440 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000441 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400443 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400444 struct btrfs_trans_handle *trans;
445 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100446 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400447 struct btrfs_inode_item *inode_item;
448 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000449 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400450 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000451 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700452 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900453 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400454 int ret;
455 int err;
456 u64 objectid;
457 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500458 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000459 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200460 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400461
David Sterba49a3c4d2016-03-24 17:49:22 +0100462 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
463 if (!root_item)
464 return -ENOMEM;
465
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400466 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400467 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100468 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000469
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800470 /*
471 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400472 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800473 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100474 if (btrfs_qgroup_level(objectid)) {
475 ret = -ENOSPC;
476 goto fail_free;
477 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800478
Miao Xied5c12072013-02-28 10:04:33 +0000479 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400480 /*
Miao Xied5c12072013-02-28 10:04:33 +0000481 * The same as the snapshot creation, please see the comment
482 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400483 */
Miao Xied5c12072013-02-28 10:04:33 +0000484 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200485 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000486 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100487 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400488
Miao Xied5c12072013-02-28 10:04:33 +0000489 trans = btrfs_start_transaction(root, 0);
490 if (IS_ERR(trans)) {
491 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100492 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100493 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000494 }
495 trans->block_rsv = &block_rsv;
496 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400497
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400498 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200499 if (ret)
500 goto fail;
501
David Sterba4d75f8a2014-06-15 01:54:12 +0200502 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400503 if (IS_ERR(leaf)) {
504 ret = PTR_ERR(leaf);
505 goto fail;
506 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400507
David Sterbab159fa22016-11-08 18:09:03 +0100508 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400509 btrfs_set_header_bytenr(leaf, leaf->start);
510 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400511 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400512 btrfs_set_header_owner(leaf, objectid);
513
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400514 write_extent_buffer_fsid(leaf, fs_info->fsid);
515 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400516 btrfs_mark_buffer_dirty(leaf);
517
David Sterba49a3c4d2016-03-24 17:49:22 +0100518 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800519 btrfs_set_stack_inode_generation(inode_item, 1);
520 btrfs_set_stack_inode_size(inode_item, 3);
521 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400522 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400523 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800524 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525
David Sterba49a3c4d2016-03-24 17:49:22 +0100526 btrfs_set_root_flags(root_item, 0);
527 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800528 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000529
David Sterba49a3c4d2016-03-24 17:49:22 +0100530 btrfs_set_root_bytenr(root_item, leaf->start);
531 btrfs_set_root_generation(root_item, trans->transid);
532 btrfs_set_root_level(root_item, 0);
533 btrfs_set_root_refs(root_item, 1);
534 btrfs_set_root_used(root_item, leaf->len);
535 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400536
David Sterba49a3c4d2016-03-24 17:49:22 +0100537 btrfs_set_root_generation_v2(root_item,
538 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200539 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100540 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
541 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
542 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
543 root_item->ctime = root_item->otime;
544 btrfs_set_root_ctransid(root_item, trans->transid);
545 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400546
Chris Mason925baed2008-06-25 16:01:30 -0400547 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400548 free_extent_buffer(leaf);
549 leaf = NULL;
550
David Sterba49a3c4d2016-03-24 17:49:22 +0100551 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400552
553 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200555 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400556 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100557 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558 if (ret)
559 goto fail;
560
Yan, Zheng76dda932009-09-21 16:00:26 -0400561 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400562 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100563 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100564 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400565 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100566 goto fail;
567 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400568
569 btrfs_record_root_in_trans(trans, new_root);
570
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000571 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700572 if (ret) {
573 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400574 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700575 goto fail;
576 }
577
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530578 mutex_lock(&new_root->objectid_mutex);
579 new_root->highest_objectid = new_dirid;
580 mutex_unlock(&new_root->objectid_mutex);
581
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400582 /*
583 * insert the directory item
584 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200585 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100586 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400587 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100588 goto fail;
589 }
Chris Mason3de45862008-11-17 21:02:50 -0500590
591 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200592 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500593 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100594 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400595 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400596 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100597 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500598
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200599 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500600 ret = btrfs_update_inode(trans, root, dir);
601 BUG_ON(ret);
602
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400603 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400604 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200605 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500606 BUG_ON(ret);
607
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400608 ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400609 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200610 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400611 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200612
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400613fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100614 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000615 trans->block_rsv = NULL;
616 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100617 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800618
Sage Weil72fd0322010-10-29 15:41:32 -0400619 if (async_transid) {
620 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400621 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000622 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400623 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400624 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400625 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400626 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400627 if (err && !ret)
628 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500629
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900630 if (!ret) {
631 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800632 if (IS_ERR(inode))
633 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900634 d_instantiate(dentry, inode);
635 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400636 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100637
638fail_free:
639 kfree(root_item);
640 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400641}
642
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000643static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +0800644{
645 s64 writers;
646 DEFINE_WAIT(wait);
647
648 do {
649 prepare_to_wait(&root->subv_writers->wait, &wait,
650 TASK_UNINTERRUPTIBLE);
651
652 writers = percpu_counter_sum(&root->subv_writers->counter);
653 if (writers)
654 schedule();
655
656 finish_wait(&root->subv_writers->wait, &wait);
657 } while (writers);
658}
659
Miao Xiee9662f72013-02-28 10:01:15 +0000660static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100661 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000662 u64 *async_transid, bool readonly,
663 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400665 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000666 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400667 struct btrfs_pending_snapshot *pending_snapshot;
668 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000669 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400670
Miao Xie27cdeb72014-04-02 19:51:05 +0800671 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400672 return -EINVAL;
673
David Sterba23269bf2017-02-13 11:03:44 +0100674 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100675 if (!pending_snapshot)
676 return -ENOMEM;
677
David Sterbab0c0ea62015-11-10 18:54:00 +0100678 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100679 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100680 pending_snapshot->path = btrfs_alloc_path();
681 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100682 ret = -ENOMEM;
683 goto free_pending;
684 }
685
Miao Xie8257b2d2014-03-06 13:38:19 +0800686 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100687 smp_mb__after_atomic();
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000688 btrfs_wait_for_no_snapshoting_writes(root);
Miao Xie8257b2d2014-03-06 13:38:19 +0800689
Miao Xie6a038432013-05-15 07:48:24 +0000690 ret = btrfs_start_delalloc_inodes(root, 0);
691 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100692 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000693
Chris Mason6374e57a2017-06-23 09:48:21 -0700694 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000695
Miao Xie66d8f3d2012-09-06 04:02:28 -0600696 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
697 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000698 /*
699 * 1 - parent dir inode
700 * 2 - dir entries
701 * 1 - root item
702 * 2 - root ref/backref
703 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200704 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000705 */
706 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200707 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400708 &pending_snapshot->qgroup_reserved,
709 false);
Miao Xied5c12072013-02-28 10:04:33 +0000710 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100711 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000712
Yan, Zhenga22285a2010-05-16 10:48:46 -0400713 pending_snapshot->dentry = dentry;
714 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800715 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000716 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000717 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400718
Miao Xied5c12072013-02-28 10:04:33 +0000719 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400720 if (IS_ERR(trans)) {
721 ret = PTR_ERR(trans);
722 goto fail;
723 }
724
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400725 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400726 list_add(&pending_snapshot->list,
727 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400728 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400729 if (async_transid) {
730 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400731 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000732 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400733 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400734 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400735 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400736 }
Miao Xieaec80302013-03-04 09:44:29 +0000737 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400738 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400739
740 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400741 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000742 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400743
Chris Masond3797302014-10-15 13:50:56 -0700744 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
745 if (ret)
746 goto fail;
747
David Howells2b0143b2015-03-17 22:25:59 +0000748 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000749 if (IS_ERR(inode)) {
750 ret = PTR_ERR(inode);
751 goto fail;
752 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900753
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000754 d_instantiate(dentry, inode);
755 ret = 0;
756fail:
David Sterba7775c812017-02-10 19:18:18 +0100757 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100758dec_and_free:
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000759 if (atomic_dec_and_test(&root->will_be_snapshoted))
760 wake_up_atomic_t(&root->will_be_snapshoted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100761free_pending:
762 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100763 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100764 kfree(pending_snapshot);
765
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400766 return ret;
767}
768
Sage Weil4260f7c2010-10-29 15:46:43 -0400769/* copy of may_delete in fs/namei.c()
770 * Check whether we can remove a link victim from directory dir, check
771 * whether the type of victim is right.
772 * 1. We can't do it if dir is read-only (done in permission())
773 * 2. We should have write and exec permissions on dir
774 * 3. We can't remove anything from append-only dir
775 * 4. We can't do anything with immutable dir (done in permission())
776 * 5. If the sticky bit on dir is set we should either
777 * a. be owner of dir, or
778 * b. be owner of victim, or
779 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400780 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400781 * links pointing to it.
782 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
783 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
784 * 9. We can't remove a root or mountpoint.
785 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
786 * nfs_async_unlink().
787 */
788
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530789static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400790{
791 int error;
792
David Howells2b0143b2015-03-17 22:25:59 +0000793 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400794 return -ENOENT;
795
David Howells2b0143b2015-03-17 22:25:59 +0000796 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400797 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400798
799 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
800 if (error)
801 return error;
802 if (IS_APPEND(dir))
803 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000804 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
805 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400806 return -EPERM;
807 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000808 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400809 return -ENOTDIR;
810 if (IS_ROOT(victim))
811 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000812 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400813 return -EISDIR;
814 if (IS_DEADDIR(dir))
815 return -ENOENT;
816 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
817 return -EBUSY;
818 return 0;
819}
820
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400821/* copy of may_create in fs/namei.c() */
822static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
823{
David Howells2b0143b2015-03-17 22:25:59 +0000824 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400825 return -EEXIST;
826 if (IS_DEADDIR(dir))
827 return -ENOENT;
828 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
829}
830
831/*
832 * Create a new subvolume below @parent. This is largely modeled after
833 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
834 * inside this filesystem so it's quite a bit simpler.
835 */
Al Viro92872092016-11-20 19:34:31 -0500836static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100837 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400838 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200839 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000840 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400841{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400842 struct inode *dir = d_inode(parent->dentry);
843 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400844 struct dentry *dentry;
845 int error;
846
Al Viro00235412016-05-26 00:05:12 -0400847 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
848 if (error == -EINTR)
849 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400850
851 dentry = lookup_one_len(name, parent->dentry, namelen);
852 error = PTR_ERR(dentry);
853 if (IS_ERR(dentry))
854 goto out_unlock;
855
Yan, Zheng76dda932009-09-21 16:00:26 -0400856 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400857 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600858 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400859
Chris Mason9c520572012-12-17 14:26:57 -0500860 /*
861 * even if this name doesn't exist, we may get hash collisions.
862 * check for them now when we can safely fail
863 */
864 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
865 dir->i_ino, name,
866 namelen);
867 if (error)
868 goto out_dput;
869
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400870 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400871
872 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
873 goto out_up_read;
874
Chris Mason3de45862008-11-17 21:02:50 -0500875 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100876 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200877 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500878 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000879 error = create_subvol(dir, dentry, name, namelen,
880 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500881 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400882 if (!error)
883 fsnotify_mkdir(dir, dentry);
884out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400885 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400886out_dput:
887 dput(dentry);
888out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500889 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400890 return error;
891}
892
Chris Mason4cb53002011-05-24 15:35:30 -0400893/*
894 * When we're defragging a range, we don't want to kick it off again
895 * if it is really just waiting for delalloc to send it down.
896 * If we find a nice big extent or delalloc range for the bytes in the
897 * file you want to defrag, we return 0 to let you know to skip this
898 * part of the file
899 */
David Sterbaaab110a2014-07-29 17:32:10 +0200900static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400901{
902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
903 struct extent_map *em = NULL;
904 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
905 u64 end;
906
907 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300908 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -0400909 read_unlock(&em_tree->lock);
910
911 if (em) {
912 end = extent_map_end(em);
913 free_extent_map(em);
914 if (end - offset > thresh)
915 return 0;
916 }
917 /* if we already have a nice delalloc here, just stop */
918 thresh /= 2;
919 end = count_range_bits(io_tree, &offset, offset + thresh,
920 thresh, EXTENT_DELALLOC, 1);
921 if (end >= thresh)
922 return 0;
923 return 1;
924}
925
926/*
927 * helper function to walk through a file and find extents
928 * newer than a specific transid, and smaller than thresh.
929 *
930 * This is used by the defragging code to find new and small
931 * extents
932 */
933static int find_new_extents(struct btrfs_root *root,
934 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200935 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400936{
937 struct btrfs_path *path;
938 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400939 struct extent_buffer *leaf;
940 struct btrfs_file_extent_item *extent;
941 int type;
942 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200943 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -0400944
945 path = btrfs_alloc_path();
946 if (!path)
947 return -ENOMEM;
948
David Sterbaa4689d22011-05-31 17:08:14 +0000949 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400950 min_key.type = BTRFS_EXTENT_DATA_KEY;
951 min_key.offset = *off;
952
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530953 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100954 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400955 if (ret != 0)
956 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000957process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000958 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400959 goto none;
960 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
961 goto none;
962
963 leaf = path->nodes[0];
964 extent = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_file_extent_item);
966
967 type = btrfs_file_extent_type(leaf, extent);
968 if (type == BTRFS_FILE_EXTENT_REG &&
969 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
970 check_defrag_in_cache(inode, min_key.offset, thresh)) {
971 *off = min_key.offset;
972 btrfs_free_path(path);
973 return 0;
974 }
975
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000976 path->slots[0]++;
977 if (path->slots[0] < btrfs_header_nritems(leaf)) {
978 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
979 goto process_slot;
980 }
981
Chris Mason4cb53002011-05-24 15:35:30 -0400982 if (min_key.offset == (u64)-1)
983 goto none;
984
985 min_key.offset++;
986 btrfs_release_path(path);
987 }
988none:
989 btrfs_free_path(path);
990 return -ENOENT;
991}
992
Li Zefan6c282eb2012-06-11 16:03:35 +0800993static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400994{
995 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500996 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800997 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300998 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500999
1000 /*
1001 * hopefully we have this extent in the tree already, try without
1002 * the full extent lock
1003 */
1004 read_lock(&em_tree->lock);
1005 em = lookup_extent_mapping(em_tree, start, len);
1006 read_unlock(&em_tree->lock);
1007
1008 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001009 struct extent_state *cached = NULL;
1010 u64 end = start + len - 1;
1011
Chris Mason940100a2010-03-10 10:52:59 -05001012 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001013 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001014 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +00001015 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -05001016
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001017 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001018 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001019 }
1020
Li Zefan6c282eb2012-06-11 16:03:35 +08001021 return em;
1022}
1023
1024static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1025{
1026 struct extent_map *next;
1027 bool ret = true;
1028
1029 /* this is the last extent */
1030 if (em->start + em->len >= i_size_read(inode))
1031 return false;
1032
1033 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001034 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1035 ret = false;
1036 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001037 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001038 ret = false;
1039
1040 free_extent_map(next);
1041 return ret;
1042}
1043
David Sterbaaab110a2014-07-29 17:32:10 +02001044static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001045 u64 *last_len, u64 *skip, u64 *defrag_end,
1046 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001047{
1048 struct extent_map *em;
1049 int ret = 1;
1050 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001051 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001052
1053 /*
1054 * make sure that once we start defragging an extent, we keep on
1055 * defragging it
1056 */
1057 if (start < *defrag_end)
1058 return 1;
1059
1060 *skip = 0;
1061
1062 em = defrag_lookup_extent(inode, start);
1063 if (!em)
1064 return 0;
1065
Chris Mason940100a2010-03-10 10:52:59 -05001066 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001067 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001068 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001069 goto out;
1070 }
1071
Liu Bo4a3560c2015-08-07 16:48:41 +08001072 if (!*defrag_end)
1073 prev_mergeable = false;
1074
Li Zefan6c282eb2012-06-11 16:03:35 +08001075 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001076 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001077 * we hit a real extent, if it is big or the next extent is not a
1078 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001079 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001080 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001081 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001082 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001083out:
Chris Mason940100a2010-03-10 10:52:59 -05001084 /*
1085 * last_len ends up being a counter of how many bytes we've defragged.
1086 * every time we choose not to defrag an extent, we reset *last_len
1087 * so that the next tiny extent will force a defrag.
1088 *
1089 * The end result of this is that tiny extents before a single big
1090 * extent will force at least part of that big extent to be defragged.
1091 */
1092 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001093 *defrag_end = extent_map_end(em);
1094 } else {
1095 *last_len = 0;
1096 *skip = extent_map_end(em);
1097 *defrag_end = 0;
1098 }
1099
1100 free_extent_map(em);
1101 return ret;
1102}
1103
Chris Mason4cb53002011-05-24 15:35:30 -04001104/*
1105 * it doesn't do much good to defrag one or two pages
1106 * at a time. This pulls in a nice chunk of pages
1107 * to COW and defrag.
1108 *
1109 * It also makes sure the delalloc code has enough
1110 * dirty data to avoid making new small extents as part
1111 * of the defrag
1112 *
1113 * It's a good idea to start RA on this range
1114 * before calling this.
1115 */
1116static int cluster_pages_for_defrag(struct inode *inode,
1117 struct page **pages,
1118 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001119 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001120{
Chris Mason4cb53002011-05-24 15:35:30 -04001121 unsigned long file_end;
1122 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001123 u64 page_start;
1124 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001125 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001126 int ret;
1127 int i;
1128 int i_done;
1129 struct btrfs_ordered_extent *ordered;
1130 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001131 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001132 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001133 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001134
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001135 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001136 if (!isize || start_index > file_end)
1137 return 0;
1138
1139 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001140
Qu Wenruo364ecf32017-02-27 15:10:38 +08001141 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001142 start_index << PAGE_SHIFT,
1143 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001144 if (ret)
1145 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001146 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001147 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001148
1149 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001150 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001151 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001152again:
Josef Bacika94733d2011-07-11 10:47:06 -04001153 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001154 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001155 if (!page)
1156 break;
1157
Miao Xie600a45e2012-02-16 15:01:24 +08001158 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001159 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001160 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001161 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001162 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001163 ordered = btrfs_lookup_ordered_extent(inode,
1164 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001165 unlock_extent_cached(tree, page_start, page_end,
1166 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001167 if (!ordered)
1168 break;
1169
1170 unlock_page(page);
1171 btrfs_start_ordered_extent(inode, ordered, 1);
1172 btrfs_put_ordered_extent(ordered);
1173 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001174 /*
1175 * we unlocked the page above, so we need check if
1176 * it was released or not.
1177 */
1178 if (page->mapping != inode->i_mapping) {
1179 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001180 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001181 goto again;
1182 }
Miao Xie600a45e2012-02-16 15:01:24 +08001183 }
1184
Chris Mason4cb53002011-05-24 15:35:30 -04001185 if (!PageUptodate(page)) {
1186 btrfs_readpage(NULL, page);
1187 lock_page(page);
1188 if (!PageUptodate(page)) {
1189 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001190 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001191 ret = -EIO;
1192 break;
1193 }
1194 }
Miao Xie600a45e2012-02-16 15:01:24 +08001195
Miao Xie600a45e2012-02-16 15:01:24 +08001196 if (page->mapping != inode->i_mapping) {
1197 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001198 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001199 goto again;
1200 }
1201
Chris Mason4cb53002011-05-24 15:35:30 -04001202 pages[i] = page;
1203 i_done++;
1204 }
1205 if (!i_done || ret)
1206 goto out;
1207
1208 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1209 goto out;
1210
1211 /*
1212 * so now we have a nice long stream of locked
1213 * and up to date pages, lets wait on them
1214 */
1215 for (i = 0; i < i_done; i++)
1216 wait_on_page_writeback(pages[i]);
1217
1218 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001219 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001220
1221 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001222 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001223 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1224 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001225 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1226 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001227
Liu Bo1f12bd02012-03-29 09:57:44 -04001228 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001229 spin_lock(&BTRFS_I(inode)->lock);
1230 BTRFS_I(inode)->outstanding_extents++;
1231 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001232 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001233 start_index << PAGE_SHIFT,
1234 (page_cnt - i_done) << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001235 }
1236
1237
Liu Bo9e8a4a82012-09-05 19:10:51 -06001238 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001239 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001240
1241 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1242 page_start, page_end - 1, &cached_state,
1243 GFP_NOFS);
1244
1245 for (i = 0; i < i_done; i++) {
1246 clear_page_dirty_for_io(pages[i]);
1247 ClearPageChecked(pages[i]);
1248 set_page_extent_mapped(pages[i]);
1249 set_page_dirty(pages[i]);
1250 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001251 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001252 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08001253 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001254 return i_done;
1255out:
1256 for (i = 0; i < i_done; i++) {
1257 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001258 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001259 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001260 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001261 start_index << PAGE_SHIFT,
1262 page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001263 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001264 return ret;
1265
1266}
1267
1268int btrfs_defrag_file(struct inode *inode, struct file *file,
1269 struct btrfs_ioctl_defrag_range_args *range,
1270 u64 newer_than, unsigned long max_to_defrag)
1271{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001272 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001273 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001274 struct file_ra_state *ra = NULL;
1275 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001276 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001277 u64 last_len = 0;
1278 u64 skip = 0;
1279 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001280 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001281 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001282 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001283 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001284 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001285 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001286 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001287 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001288 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001289 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001290 struct page **pages = NULL;
1291
Liu Bo0abd5b12013-04-16 09:20:28 +00001292 if (isize == 0)
1293 return 0;
1294
1295 if (range->start >= isize)
1296 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001297
1298 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1299 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1300 return -EINVAL;
1301 if (range->compress_type)
1302 compress_type = range->compress_type;
1303 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001304
Liu Bo0abd5b12013-04-16 09:20:28 +00001305 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001306 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001307
Chris Mason4cb53002011-05-24 15:35:30 -04001308 /*
1309 * if we were not given a file, allocate a readahead
1310 * context
1311 */
1312 if (!file) {
1313 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1314 if (!ra)
1315 return -ENOMEM;
1316 file_ra_state_init(ra, inode->i_mapping);
1317 } else {
1318 ra = &file->f_ra;
1319 }
1320
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301321 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001322 GFP_NOFS);
1323 if (!pages) {
1324 ret = -ENOMEM;
1325 goto out_ra;
1326 }
1327
1328 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001329 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001330 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001331 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001332 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001333 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001334 }
1335
Chris Mason4cb53002011-05-24 15:35:30 -04001336 if (newer_than) {
1337 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001338 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001339 if (!ret) {
1340 range->start = newer_off;
1341 /*
1342 * we always align our defrag to help keep
1343 * the extents in the file evenly spaced
1344 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001345 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001346 } else
1347 goto out_ra;
1348 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001349 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001350 }
1351 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301352 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001353
Li Zefan2a0f7f52011-10-10 15:43:34 -04001354 /*
1355 * make writeback starts from i, so the defrag range can be
1356 * written sequentially.
1357 */
1358 if (i < inode->i_mapping->writeback_index)
1359 inode->i_mapping->writeback_index = i;
1360
Chris Masonf7f43cc2011-10-11 11:41:40 -04001361 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001362 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001363 /*
1364 * make sure we stop running if someone unmounts
1365 * the FS
1366 */
1367 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1368 break;
1369
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001370 if (btrfs_defrag_cancelled(fs_info)) {
1371 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001372 ret = -EAGAIN;
1373 break;
1374 }
1375
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001376 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001377 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001378 &defrag_end, range->flags &
1379 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001380 unsigned long next;
1381 /*
1382 * the should_defrag function tells us how much to skip
1383 * bump our counter by the suggested amount
1384 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001385 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001386 i = max(i + 1, next);
1387 continue;
1388 }
Li Zefan008873e2011-09-02 15:57:07 +08001389
1390 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001391 cluster = (PAGE_ALIGN(defrag_end) >>
1392 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001393 cluster = min(cluster, max_cluster);
1394 } else {
1395 cluster = max_cluster;
1396 }
1397
Li Zefan008873e2011-09-02 15:57:07 +08001398 if (i + cluster > ra_index) {
1399 ra_index = max(i, ra_index);
1400 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1401 cluster);
chandane4826a52015-06-09 17:38:32 +05301402 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001403 }
Chris Mason940100a2010-03-10 10:52:59 -05001404
Al Viro59551022016-01-22 15:40:57 -05001405 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001406 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1407 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001408 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001409 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001410 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001411 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001412 }
Chris Mason940100a2010-03-10 10:52:59 -05001413
Chris Mason4cb53002011-05-24 15:35:30 -04001414 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001415 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001416 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001417
1418 if (newer_than) {
1419 if (newer_off == (u64)-1)
1420 break;
1421
Liu Boe1f041e2012-03-29 09:57:45 -04001422 if (ret > 0)
1423 i += ret;
1424
Chris Mason4cb53002011-05-24 15:35:30 -04001425 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001426 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001427
Byongho Leeee221842015-12-15 01:42:10 +09001428 ret = find_new_extents(root, inode, newer_than,
1429 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001430 if (!ret) {
1431 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001432 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001433 } else {
1434 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001435 }
Chris Mason4cb53002011-05-24 15:35:30 -04001436 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001437 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001438 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001439 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001440 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001441 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001442 last_len = 0;
1443 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001444 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001445 }
1446
Filipe Mananadec8ef92014-03-01 10:55:54 +00001447 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001448 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001449 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1450 &BTRFS_I(inode)->runtime_flags))
1451 filemap_flush(inode->i_mapping);
1452 }
Chris Mason1e701a32010-03-11 09:42:04 -05001453
1454 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1455 /* the filemap_flush will queue IO into the worker threads, but
1456 * we have to make sure the IO is actually started and that
1457 * ordered extents get created before we return
1458 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001459 atomic_inc(&fs_info->async_submit_draining);
1460 while (atomic_read(&fs_info->nr_async_submits) ||
1461 atomic_read(&fs_info->async_delalloc_pages)) {
1462 wait_event(fs_info->async_submit_wait,
1463 (atomic_read(&fs_info->nr_async_submits) == 0 &&
1464 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason1e701a32010-03-11 09:42:04 -05001465 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001466 atomic_dec(&fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001467 }
1468
Li Zefan1a419d82010-10-25 15:12:50 +08001469 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001470 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001471 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1472 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001473 }
1474
Diego Calleja60ccf822011-09-01 16:33:57 +02001475 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001476
Chris Mason4cb53002011-05-24 15:35:30 -04001477out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001478 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
Al Viro59551022016-01-22 15:40:57 -05001479 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001480 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001481 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001482 }
Chris Mason4cb53002011-05-24 15:35:30 -04001483 if (!file)
1484 kfree(ra);
1485 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001486 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001487}
1488
Miao Xie198605a2012-11-26 08:43:45 +00001489static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001490 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001491{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001492 struct inode *inode = file_inode(file);
1493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001494 u64 new_size;
1495 u64 old_size;
1496 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001498 struct btrfs_ioctl_vol_args *vol_args;
1499 struct btrfs_trans_handle *trans;
1500 struct btrfs_device *device = NULL;
1501 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001502 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001503 char *devstr = NULL;
1504 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001505 int mod = 0;
1506
Chris Masone441d542009-01-05 16:57:23 -05001507 if (!capable(CAP_SYS_ADMIN))
1508 return -EPERM;
1509
Miao Xie198605a2012-11-26 08:43:45 +00001510 ret = mnt_want_write_file(file);
1511 if (ret)
1512 return ret;
1513
David Sterba171938e2017-03-28 14:44:21 +02001514 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001515 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001516 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001517 }
1518
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001519 mutex_lock(&fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001520 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001521 if (IS_ERR(vol_args)) {
1522 ret = PTR_ERR(vol_args);
1523 goto out;
1524 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001525
1526 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001527
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001528 sizestr = vol_args->name;
1529 devstr = strchr(sizestr, ':');
1530 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001531 sizestr = devstr + 1;
1532 *devstr = '\0';
1533 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001534 ret = kstrtoull(devstr, 10, &devid);
1535 if (ret)
1536 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001537 if (!devid) {
1538 ret = -EINVAL;
1539 goto out_free;
1540 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001541 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001542 }
Miao Xiedba60f32012-12-21 09:19:51 +00001543
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001544 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001545 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001546 btrfs_info(fs_info, "resizer unable to find device %llu",
1547 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001548 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001549 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001550 }
Miao Xiedba60f32012-12-21 09:19:51 +00001551
1552 if (!device->writeable) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001553 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001554 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001555 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001556 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001557 goto out_free;
1558 }
1559
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001560 if (!strcmp(sizestr, "max"))
1561 new_size = device->bdev->bd_inode->i_size;
1562 else {
1563 if (sizestr[0] == '-') {
1564 mod = -1;
1565 sizestr++;
1566 } else if (sizestr[0] == '+') {
1567 mod = 1;
1568 sizestr++;
1569 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001570 new_size = memparse(sizestr, &retptr);
1571 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001573 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001574 }
1575 }
1576
Stefan Behrens63a212a2012-11-05 18:29:28 +01001577 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001578 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001579 goto out_free;
1580 }
1581
Miao Xie7cc8e582014-09-03 21:35:38 +08001582 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001583
1584 if (mod < 0) {
1585 if (new_size > old_size) {
1586 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001587 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001588 }
1589 new_size = old_size - new_size;
1590 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001591 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001592 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001593 goto out_free;
1594 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001595 new_size = old_size + new_size;
1596 }
1597
Byongho Leeee221842015-12-15 01:42:10 +09001598 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001599 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001600 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001601 }
1602 if (new_size > device->bdev->bd_inode->i_size) {
1603 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001604 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001605 }
1606
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001607 new_size = div_u64(new_size, fs_info->sectorsize);
1608 new_size *= fs_info->sectorsize;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001609
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001610 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1611 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001612
1613 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001614 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001615 if (IS_ERR(trans)) {
1616 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001617 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001618 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001619 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001620 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001621 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001622 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001623 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001624
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001625out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001627out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001628 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02001629 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001630 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001631 return ret;
1632}
1633
Sage Weil72fd0322010-10-29 15:41:32 -04001634static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001635 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001636 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001637 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001638{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001639 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001640 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001641
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001642 if (!S_ISDIR(file_inode(file)->i_mode))
1643 return -ENOTDIR;
1644
Liu Boa874a632012-06-29 03:58:46 -06001645 ret = mnt_want_write_file(file);
1646 if (ret)
1647 goto out;
1648
Sage Weil72fd0322010-10-29 15:41:32 -04001649 namelen = strlen(name);
1650 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001651 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001652 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001653 }
1654
Chris Mason16780ca2012-02-20 22:14:55 -05001655 if (name[0] == '.' &&
1656 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1657 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001658 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001659 }
1660
Chris Mason3de45862008-11-17 21:02:50 -05001661 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001662 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001663 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001664 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001665 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001666 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001667 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001668 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001669 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001670 }
1671
Al Viro496ad9a2013-01-23 17:07:38 -05001672 src_inode = file_inode(src.file);
1673 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001674 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001675 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001676 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001677 } else if (!inode_owner_or_capable(src_inode)) {
1678 /*
1679 * Subvolume creation is not restricted, but snapshots
1680 * are limited to own subvolumes only
1681 */
1682 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001683 } else {
1684 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1685 BTRFS_I(src_inode)->root,
1686 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001687 }
Al Viro2903ff02012-08-28 12:52:22 -04001688 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001689 }
Liu Boa874a632012-06-29 03:58:46 -06001690out_drop_write:
1691 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001692out:
Sage Weil72fd0322010-10-29 15:41:32 -04001693 return ret;
1694}
1695
1696static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001697 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001698{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001699 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001700 int ret;
1701
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001702 if (!S_ISDIR(file_inode(file)->i_mode))
1703 return -ENOTDIR;
1704
Li Zefanfa0d2b92010-12-20 15:53:28 +08001705 vol_args = memdup_user(arg, sizeof(*vol_args));
1706 if (IS_ERR(vol_args))
1707 return PTR_ERR(vol_args);
1708 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001709
Li Zefanfa0d2b92010-12-20 15:53:28 +08001710 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001711 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001712 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001713
Li Zefanfa0d2b92010-12-20 15:53:28 +08001714 kfree(vol_args);
1715 return ret;
1716}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001717
Li Zefanfa0d2b92010-12-20 15:53:28 +08001718static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1719 void __user *arg, int subvol)
1720{
1721 struct btrfs_ioctl_vol_args_v2 *vol_args;
1722 int ret;
1723 u64 transid = 0;
1724 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001725 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001726 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001727
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001728 if (!S_ISDIR(file_inode(file)->i_mode))
1729 return -ENOTDIR;
1730
Li Zefanfa0d2b92010-12-20 15:53:28 +08001731 vol_args = memdup_user(arg, sizeof(*vol_args));
1732 if (IS_ERR(vol_args))
1733 return PTR_ERR(vol_args);
1734 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001735
Li Zefanb83cc962010-12-20 16:04:08 +08001736 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001737 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1738 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001739 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001740 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001741 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001742
1743 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1744 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001745 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1746 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001747 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001748 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001749 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001750 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001751 }
1752 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1753 if (IS_ERR(inherit)) {
1754 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001755 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001756 }
1757 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001758
1759 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001760 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001761 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001762 if (ret)
1763 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001764
Dan Carpenterc47ca322014-09-04 14:09:15 +03001765 if (ptr && copy_to_user(arg +
1766 offsetof(struct btrfs_ioctl_vol_args_v2,
1767 transid),
1768 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001769 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001770
1771free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001772 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001773free_args:
1774 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001775 return ret;
1776}
1777
Li Zefan0caa1022010-12-20 16:30:25 +08001778static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1779 void __user *arg)
1780{
Al Viro496ad9a2013-01-23 17:07:38 -05001781 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001782 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001783 struct btrfs_root *root = BTRFS_I(inode)->root;
1784 int ret = 0;
1785 u64 flags = 0;
1786
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001787 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001788 return -EINVAL;
1789
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001790 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001791 if (btrfs_root_readonly(root))
1792 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001793 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001794
1795 if (copy_to_user(arg, &flags, sizeof(flags)))
1796 ret = -EFAULT;
1797
1798 return ret;
1799}
1800
1801static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1802 void __user *arg)
1803{
Al Viro496ad9a2013-01-23 17:07:38 -05001804 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001805 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001806 struct btrfs_root *root = BTRFS_I(inode)->root;
1807 struct btrfs_trans_handle *trans;
1808 u64 root_flags;
1809 u64 flags;
1810 int ret = 0;
1811
David Sterbabd60ea02014-01-16 15:50:22 +01001812 if (!inode_owner_or_capable(inode))
1813 return -EPERM;
1814
Liu Bob9ca0662012-06-29 03:58:49 -06001815 ret = mnt_want_write_file(file);
1816 if (ret)
1817 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001818
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001819 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001820 ret = -EINVAL;
1821 goto out_drop_write;
1822 }
Li Zefan0caa1022010-12-20 16:30:25 +08001823
Liu Bob9ca0662012-06-29 03:58:49 -06001824 if (copy_from_user(&flags, arg, sizeof(flags))) {
1825 ret = -EFAULT;
1826 goto out_drop_write;
1827 }
Li Zefan0caa1022010-12-20 16:30:25 +08001828
Liu Bob9ca0662012-06-29 03:58:49 -06001829 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1830 ret = -EINVAL;
1831 goto out_drop_write;
1832 }
Li Zefan0caa1022010-12-20 16:30:25 +08001833
Liu Bob9ca0662012-06-29 03:58:49 -06001834 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1835 ret = -EOPNOTSUPP;
1836 goto out_drop_write;
1837 }
Li Zefan0caa1022010-12-20 16:30:25 +08001838
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001839 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001840
1841 /* nothing to do */
1842 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001843 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001844
1845 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001846 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001847 btrfs_set_root_flags(&root->root_item,
1848 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001849 } else {
1850 /*
1851 * Block RO -> RW transition if this subvolume is involved in
1852 * send
1853 */
1854 spin_lock(&root->root_item_lock);
1855 if (root->send_in_progress == 0) {
1856 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001857 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001858 spin_unlock(&root->root_item_lock);
1859 } else {
1860 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001861 btrfs_warn(fs_info,
1862 "Attempt to set subvolume %llu read-write during send",
1863 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001864 ret = -EPERM;
1865 goto out_drop_sem;
1866 }
1867 }
Li Zefan0caa1022010-12-20 16:30:25 +08001868
1869 trans = btrfs_start_transaction(root, 1);
1870 if (IS_ERR(trans)) {
1871 ret = PTR_ERR(trans);
1872 goto out_reset;
1873 }
1874
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001875 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001876 &root->root_key, &root->root_item);
1877
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001878 btrfs_commit_transaction(trans);
Li Zefan0caa1022010-12-20 16:30:25 +08001879out_reset:
1880 if (ret)
1881 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001882out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001883 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001884out_drop_write:
1885 mnt_drop_write_file(file);
1886out:
Li Zefan0caa1022010-12-20 16:30:25 +08001887 return ret;
1888}
1889
Yan, Zheng76dda932009-09-21 16:00:26 -04001890/*
1891 * helper to check if the subvolume references other subvolumes
1892 */
1893static noinline int may_destroy_subvol(struct btrfs_root *root)
1894{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001895 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04001896 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001897 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001898 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001899 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001900 int ret;
1901
1902 path = btrfs_alloc_path();
1903 if (!path)
1904 return -ENOMEM;
1905
Josef Bacik175a2b82013-08-12 15:36:44 -04001906 /* Make sure this root isn't set as the default subvol */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001907 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1908 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
Josef Bacik175a2b82013-08-12 15:36:44 -04001909 dir_id, "default", 7, 0);
1910 if (di && !IS_ERR(di)) {
1911 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1912 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001913 ret = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001914 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001915 "deleting default subvolume %llu is not allowed",
1916 key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001917 goto out;
1918 }
1919 btrfs_release_path(path);
1920 }
1921
Yan, Zheng76dda932009-09-21 16:00:26 -04001922 key.objectid = root->root_key.objectid;
1923 key.type = BTRFS_ROOT_REF_KEY;
1924 key.offset = (u64)-1;
1925
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001926 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04001927 if (ret < 0)
1928 goto out;
1929 BUG_ON(ret == 0);
1930
1931 ret = 0;
1932 if (path->slots[0] > 0) {
1933 path->slots[0]--;
1934 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1935 if (key.objectid == root->root_key.objectid &&
1936 key.type == BTRFS_ROOT_REF_KEY)
1937 ret = -ENOTEMPTY;
1938 }
1939out:
1940 btrfs_free_path(path);
1941 return ret;
1942}
1943
Chris Masonac8e9812010-02-28 15:39:26 -05001944static noinline int key_in_sk(struct btrfs_key *key,
1945 struct btrfs_ioctl_search_key *sk)
1946{
Chris Masonabc6e132010-03-18 12:10:08 -04001947 struct btrfs_key test;
1948 int ret;
1949
1950 test.objectid = sk->min_objectid;
1951 test.type = sk->min_type;
1952 test.offset = sk->min_offset;
1953
1954 ret = btrfs_comp_cpu_keys(key, &test);
1955 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001956 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001957
1958 test.objectid = sk->max_objectid;
1959 test.type = sk->max_type;
1960 test.offset = sk->max_offset;
1961
1962 ret = btrfs_comp_cpu_keys(key, &test);
1963 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001964 return 0;
1965 return 1;
1966}
1967
Jeff Mahoneydf397562016-06-21 20:18:21 -04001968static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001969 struct btrfs_key *key,
1970 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001971 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001972 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001973 unsigned long *sk_offset,
1974 int *num_found)
1975{
1976 u64 found_transid;
1977 struct extent_buffer *leaf;
1978 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001979 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001980 unsigned long item_off;
1981 unsigned long item_len;
1982 int nritems;
1983 int i;
1984 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001985 int ret = 0;
1986
1987 leaf = path->nodes[0];
1988 slot = path->slots[0];
1989 nritems = btrfs_header_nritems(leaf);
1990
1991 if (btrfs_header_generation(leaf) > sk->max_transid) {
1992 i = nritems;
1993 goto advance_key;
1994 }
1995 found_transid = btrfs_header_generation(leaf);
1996
1997 for (i = slot; i < nritems; i++) {
1998 item_off = btrfs_item_ptr_offset(leaf, i);
1999 item_len = btrfs_item_size_nr(leaf, i);
2000
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002001 btrfs_item_key_to_cpu(leaf, key, i);
2002 if (!key_in_sk(key, sk))
2003 continue;
2004
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002005 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002006 if (*num_found) {
2007 ret = 1;
2008 goto out;
2009 }
Chris Masonac8e9812010-02-28 15:39:26 -05002010
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002011 /*
2012 * return one empty item back for v1, which does not
2013 * handle -EOVERFLOW
2014 */
2015
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002016 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002017 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002018 ret = -EOVERFLOW;
2019 }
Chris Masonac8e9812010-02-28 15:39:26 -05002020
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002021 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002022 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002023 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002024 }
2025
Chris Masonac8e9812010-02-28 15:39:26 -05002026 sh.objectid = key->objectid;
2027 sh.offset = key->offset;
2028 sh.type = key->type;
2029 sh.len = item_len;
2030 sh.transid = found_transid;
2031
2032 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002033 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2034 ret = -EFAULT;
2035 goto out;
2036 }
2037
Chris Masonac8e9812010-02-28 15:39:26 -05002038 *sk_offset += sizeof(sh);
2039
2040 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002041 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002042 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002043 if (read_extent_buffer_to_user(leaf, up,
2044 item_off, item_len)) {
2045 ret = -EFAULT;
2046 goto out;
2047 }
2048
Chris Masonac8e9812010-02-28 15:39:26 -05002049 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002050 }
Hugo Millse2156862011-05-14 17:43:41 +00002051 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002052
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002053 if (ret) /* -EOVERFLOW from above */
2054 goto out;
2055
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002056 if (*num_found >= sk->nr_items) {
2057 ret = 1;
2058 goto out;
2059 }
Chris Masonac8e9812010-02-28 15:39:26 -05002060 }
2061advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002062 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002063 test.objectid = sk->max_objectid;
2064 test.type = sk->max_type;
2065 test.offset = sk->max_offset;
2066 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2067 ret = 1;
2068 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002069 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002070 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002071 key->offset = 0;
2072 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002073 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002074 key->offset = 0;
2075 key->type = 0;
2076 key->objectid++;
2077 } else
2078 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002079out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002080 /*
2081 * 0: all items from this leaf copied, continue with next
2082 * 1: * more items can be copied, but unused buffer is too small
2083 * * all items were found
2084 * Either way, it will stops the loop which iterates to the next
2085 * leaf
2086 * -EOVERFLOW: item was to large for buffer
2087 * -EFAULT: could not copy extent buffer back to userspace
2088 */
Chris Masonac8e9812010-02-28 15:39:26 -05002089 return ret;
2090}
2091
2092static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002093 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002094 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002095 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002096{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002097 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002098 struct btrfs_root *root;
2099 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002100 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002101 int ret;
2102 int num_found = 0;
2103 unsigned long sk_offset = 0;
2104
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002105 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2106 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002107 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002108 }
Gerhard Heift12544442014-01-30 16:23:58 +01002109
Chris Masonac8e9812010-02-28 15:39:26 -05002110 path = btrfs_alloc_path();
2111 if (!path)
2112 return -ENOMEM;
2113
2114 if (sk->tree_id == 0) {
2115 /* search the root of the inode that was passed */
2116 root = BTRFS_I(inode)->root;
2117 } else {
2118 key.objectid = sk->tree_id;
2119 key.type = BTRFS_ROOT_ITEM_KEY;
2120 key.offset = (u64)-1;
2121 root = btrfs_read_fs_root_no_name(info, &key);
2122 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002123 btrfs_free_path(path);
2124 return -ENOENT;
2125 }
2126 }
2127
2128 key.objectid = sk->min_objectid;
2129 key.type = sk->min_type;
2130 key.offset = sk->min_offset;
2131
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302132 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002133 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002134 if (ret != 0) {
2135 if (ret > 0)
2136 ret = 0;
2137 goto err;
2138 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002139 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002140 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002141 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002142 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002143 break;
2144
2145 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002146 if (ret > 0)
2147 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002148err:
2149 sk->nr_items = num_found;
2150 btrfs_free_path(path);
2151 return ret;
2152}
2153
2154static noinline int btrfs_ioctl_tree_search(struct file *file,
2155 void __user *argp)
2156{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002157 struct btrfs_ioctl_search_args __user *uargs;
2158 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002159 struct inode *inode;
2160 int ret;
2161 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002162
2163 if (!capable(CAP_SYS_ADMIN))
2164 return -EPERM;
2165
Gerhard Heiftba346b32014-01-30 16:24:02 +01002166 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002167
Gerhard Heiftba346b32014-01-30 16:24:02 +01002168 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2169 return -EFAULT;
2170
2171 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002172
Al Viro496ad9a2013-01-23 17:07:38 -05002173 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002174 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002175
2176 /*
2177 * In the origin implementation an overflow is handled by returning a
2178 * search header with a len of zero, so reset ret.
2179 */
2180 if (ret == -EOVERFLOW)
2181 ret = 0;
2182
Gerhard Heiftba346b32014-01-30 16:24:02 +01002183 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002184 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002185 return ret;
2186}
2187
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002188static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2189 void __user *argp)
2190{
2191 struct btrfs_ioctl_search_args_v2 __user *uarg;
2192 struct btrfs_ioctl_search_args_v2 args;
2193 struct inode *inode;
2194 int ret;
2195 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002196 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002197
2198 if (!capable(CAP_SYS_ADMIN))
2199 return -EPERM;
2200
2201 /* copy search header and buffer size */
2202 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2203 if (copy_from_user(&args, uarg, sizeof(args)))
2204 return -EFAULT;
2205
2206 buf_size = args.buf_size;
2207
2208 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2209 return -EOVERFLOW;
2210
2211 /* limit result size to 16MB */
2212 if (buf_size > buf_limit)
2213 buf_size = buf_limit;
2214
2215 inode = file_inode(file);
2216 ret = search_ioctl(inode, &args.key, &buf_size,
2217 (char *)(&uarg->buf[0]));
2218 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2219 ret = -EFAULT;
2220 else if (ret == -EOVERFLOW &&
2221 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2222 ret = -EFAULT;
2223
Yan, Zheng76dda932009-09-21 16:00:26 -04002224 return ret;
2225}
2226
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002227/*
Chris Masonac8e9812010-02-28 15:39:26 -05002228 * Search INODE_REFs to identify path name of 'dirid' directory
2229 * in a 'tree_id' tree. and sets path name to 'name'.
2230 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002231static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2232 u64 tree_id, u64 dirid, char *name)
2233{
2234 struct btrfs_root *root;
2235 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002236 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002237 int ret = -1;
2238 int slot;
2239 int len;
2240 int total_len = 0;
2241 struct btrfs_inode_ref *iref;
2242 struct extent_buffer *l;
2243 struct btrfs_path *path;
2244
2245 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2246 name[0]='\0';
2247 return 0;
2248 }
2249
2250 path = btrfs_alloc_path();
2251 if (!path)
2252 return -ENOMEM;
2253
Chris Masonac8e9812010-02-28 15:39:26 -05002254 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002255
2256 key.objectid = tree_id;
2257 key.type = BTRFS_ROOT_ITEM_KEY;
2258 key.offset = (u64)-1;
2259 root = btrfs_read_fs_root_no_name(info, &key);
2260 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002261 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002262 ret = -ENOENT;
2263 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002264 }
2265
2266 key.objectid = dirid;
2267 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002268 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002269
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302270 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002271 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2272 if (ret < 0)
2273 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002274 else if (ret > 0) {
2275 ret = btrfs_previous_item(root, path, dirid,
2276 BTRFS_INODE_REF_KEY);
2277 if (ret < 0)
2278 goto out;
2279 else if (ret > 0) {
2280 ret = -ENOENT;
2281 goto out;
2282 }
2283 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002284
2285 l = path->nodes[0];
2286 slot = path->slots[0];
2287 btrfs_item_key_to_cpu(l, &key, slot);
2288
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002289 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2290 len = btrfs_inode_ref_name_len(l, iref);
2291 ptr -= len + 1;
2292 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002293 if (ptr < name) {
2294 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002295 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002296 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002297
2298 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302299 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002300
2301 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2302 break;
2303
David Sterbab3b4aa72011-04-21 01:20:15 +02002304 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002305 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002306 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002307 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002308 }
Li Zefan77906a502011-07-14 03:16:00 +00002309 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302310 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002311 ret = 0;
2312out:
2313 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002314 return ret;
2315}
2316
2317static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2318 void __user *argp)
2319{
2320 struct btrfs_ioctl_ino_lookup_args *args;
2321 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002322 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002323
Julia Lawall2354d08f2010-10-29 15:14:18 -04002324 args = memdup_user(argp, sizeof(*args));
2325 if (IS_ERR(args))
2326 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002327
Al Viro496ad9a2013-01-23 17:07:38 -05002328 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002329
David Sterba01b810b2015-05-12 19:14:49 +02002330 /*
2331 * Unprivileged query to obtain the containing subvolume root id. The
2332 * path is reset so it's consistent with btrfs_search_path_in_tree.
2333 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002334 if (args->treeid == 0)
2335 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2336
David Sterba01b810b2015-05-12 19:14:49 +02002337 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2338 args->name[0] = 0;
2339 goto out;
2340 }
2341
2342 if (!capable(CAP_SYS_ADMIN)) {
2343 ret = -EPERM;
2344 goto out;
2345 }
2346
Chris Masonac8e9812010-02-28 15:39:26 -05002347 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2348 args->treeid, args->objectid,
2349 args->name);
2350
David Sterba01b810b2015-05-12 19:14:49 +02002351out:
Chris Masonac8e9812010-02-28 15:39:26 -05002352 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2353 ret = -EFAULT;
2354
2355 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002356 return ret;
2357}
2358
Yan, Zheng76dda932009-09-21 16:00:26 -04002359static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2360 void __user *arg)
2361{
Al Viro54563d42013-09-01 15:57:51 -04002362 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002363 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002364 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002365 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002366 struct inode *inode;
2367 struct btrfs_root *root = BTRFS_I(dir)->root;
2368 struct btrfs_root *dest = NULL;
2369 struct btrfs_ioctl_vol_args *vol_args;
2370 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002371 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002372 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002373 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002374 int namelen;
2375 int ret;
2376 int err = 0;
2377
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002378 if (!S_ISDIR(dir->i_mode))
2379 return -ENOTDIR;
2380
Yan, Zheng76dda932009-09-21 16:00:26 -04002381 vol_args = memdup_user(arg, sizeof(*vol_args));
2382 if (IS_ERR(vol_args))
2383 return PTR_ERR(vol_args);
2384
2385 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2386 namelen = strlen(vol_args->name);
2387 if (strchr(vol_args->name, '/') ||
2388 strncmp(vol_args->name, "..", namelen) == 0) {
2389 err = -EINVAL;
2390 goto out;
2391 }
2392
Al Viroa561be72011-11-23 11:57:51 -05002393 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002394 if (err)
2395 goto out;
2396
David Sterba521e0542014-04-15 16:41:44 +02002397
Al Viro00235412016-05-26 00:05:12 -04002398 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2399 if (err == -EINTR)
2400 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002401 dentry = lookup_one_len(vol_args->name, parent, namelen);
2402 if (IS_ERR(dentry)) {
2403 err = PTR_ERR(dentry);
2404 goto out_unlock_dir;
2405 }
2406
David Howells2b0143b2015-03-17 22:25:59 +00002407 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002408 err = -ENOENT;
2409 goto out_dput;
2410 }
2411
David Howells2b0143b2015-03-17 22:25:59 +00002412 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002413 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302414 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002415 /*
2416 * Regular user. Only allow this with a special mount
2417 * option, when the user has write+exec access to the
2418 * subvol root, and when rmdir(2) would have been
2419 * allowed.
2420 *
2421 * Note that this is _not_ check that the subvol is
2422 * empty or doesn't contain data that we wouldn't
2423 * otherwise be able to delete.
2424 *
2425 * Users who want to delete empty subvols should try
2426 * rmdir(2).
2427 */
2428 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002429 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002430 goto out_dput;
2431
2432 /*
2433 * Do not allow deletion if the parent dir is the same
2434 * as the dir to be deleted. That means the ioctl
2435 * must be called on the dentry referencing the root
2436 * of the subvol, not a random directory contained
2437 * within it.
2438 */
2439 err = -EINVAL;
2440 if (root == dest)
2441 goto out_dput;
2442
2443 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2444 if (err)
2445 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002446 }
2447
Miao Xie5c39da52012-10-22 11:39:53 +00002448 /* check if subvolume may be deleted by a user */
2449 err = btrfs_may_delete(dir, dentry, 1);
2450 if (err)
2451 goto out_dput;
2452
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002453 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002454 err = -EINVAL;
2455 goto out_dput;
2456 }
2457
Al Viro59551022016-01-22 15:40:57 -05002458 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002459
2460 /*
2461 * Don't allow to delete a subvolume with send in progress. This is
2462 * inside the i_mutex so the error handling that has to drop the bit
2463 * again is not run concurrently.
2464 */
2465 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002466 root_flags = btrfs_root_flags(&dest->root_item);
2467 if (dest->send_in_progress == 0) {
2468 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002469 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2470 spin_unlock(&dest->root_item_lock);
2471 } else {
2472 spin_unlock(&dest->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002473 btrfs_warn(fs_info,
2474 "Attempt to delete subvolume %llu during send",
2475 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002476 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002477 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002478 }
2479
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002480 down_write(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -04002481
2482 err = may_destroy_subvol(dest);
2483 if (err)
2484 goto out_up_write;
2485
Miao Xiec58aaad2013-02-28 10:05:36 +00002486 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2487 /*
2488 * One for dir inode, two for dir entries, two for root
2489 * ref/backref.
2490 */
2491 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002492 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002493 if (err)
2494 goto out_up_write;
2495
Yan, Zhenga22285a2010-05-16 10:48:46 -04002496 trans = btrfs_start_transaction(root, 0);
2497 if (IS_ERR(trans)) {
2498 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002499 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002500 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002501 trans->block_rsv = &block_rsv;
2502 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002503
Nikolay Borisov43663552017-01-18 00:31:29 +02002504 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
Filipe Manana2be63d52016-02-12 11:34:23 +00002505
Yan, Zheng76dda932009-09-21 16:00:26 -04002506 ret = btrfs_unlink_subvol(trans, root, dir,
2507 dest->root_key.objectid,
2508 dentry->d_name.name,
2509 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002510 if (ret) {
2511 err = ret;
Jeff Mahoney66642832016-06-10 18:19:25 -04002512 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002513 goto out_end_trans;
2514 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002515
2516 btrfs_record_root_in_trans(trans, dest);
2517
2518 memset(&dest->root_item.drop_progress, 0,
2519 sizeof(dest->root_item.drop_progress));
2520 dest->root_item.drop_level = 0;
2521 btrfs_set_root_refs(&dest->root_item, 0);
2522
Miao Xie27cdeb72014-04-02 19:51:05 +08002523 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002524 ret = btrfs_insert_orphan_item(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002525 fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04002526 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002527 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002528 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002529 err = ret;
2530 goto out_end_trans;
2531 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002532 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002533
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002534 ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002535 BTRFS_UUID_KEY_SUBVOL,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002536 dest->root_key.objectid);
2537 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002538 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002539 err = ret;
2540 goto out_end_trans;
2541 }
2542 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002543 ret = btrfs_uuid_tree_rem(trans, fs_info,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002544 dest->root_item.received_uuid,
2545 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2546 dest->root_key.objectid);
2547 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002548 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002549 err = ret;
2550 goto out_end_trans;
2551 }
2552 }
2553
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002554out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002555 trans->block_rsv = NULL;
2556 trans->bytes_reserved = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002557 ret = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002558 if (ret && !err)
2559 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002560 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002561out_release:
David Sterba7775c812017-02-10 19:18:18 +01002562 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Yan, Zheng76dda932009-09-21 16:00:26 -04002563out_up_write:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002564 up_write(&fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002565 if (err) {
2566 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002567 root_flags = btrfs_root_flags(&dest->root_item);
2568 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002569 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2570 spin_unlock(&dest->root_item_lock);
2571 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002572out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002573 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002574 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002575 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002576 btrfs_invalidate_inodes(dest);
2577 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002578 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002579
2580 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002581 if (dest->ino_cache_inode) {
2582 iput(dest->ino_cache_inode);
2583 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002584 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002585 }
2586out_dput:
2587 dput(dentry);
2588out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002589 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002590out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002591 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002592out:
2593 kfree(vol_args);
2594 return err;
2595}
2596
Chris Mason1e701a32010-03-11 09:42:04 -05002597static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002598{
Al Viro496ad9a2013-01-23 17:07:38 -05002599 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002600 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002601 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002602 int ret;
2603
Ilya Dryomov25122d12013-01-20 15:57:57 +02002604 ret = mnt_want_write_file(file);
2605 if (ret)
2606 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002607
Ilya Dryomov25122d12013-01-20 15:57:57 +02002608 if (btrfs_root_readonly(root)) {
2609 ret = -EROFS;
2610 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002611 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002612
2613 switch (inode->i_mode & S_IFMT) {
2614 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002615 if (!capable(CAP_SYS_ADMIN)) {
2616 ret = -EPERM;
2617 goto out;
2618 }
Eric Sandeende78b512013-01-31 18:21:12 +00002619 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002620 break;
2621 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002622 if (!(file->f_mode & FMODE_WRITE)) {
2623 ret = -EINVAL;
2624 goto out;
2625 }
Chris Mason1e701a32010-03-11 09:42:04 -05002626
2627 range = kzalloc(sizeof(*range), GFP_KERNEL);
2628 if (!range) {
2629 ret = -ENOMEM;
2630 goto out;
2631 }
2632
2633 if (argp) {
2634 if (copy_from_user(range, argp,
2635 sizeof(*range))) {
2636 ret = -EFAULT;
2637 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002638 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002639 }
2640 /* compression requires us to start the IO */
2641 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2642 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2643 range->extent_thresh = (u32)-1;
2644 }
2645 } else {
2646 /* the rest are all set to zero by kzalloc */
2647 range->len = (u64)-1;
2648 }
Al Viro496ad9a2013-01-23 17:07:38 -05002649 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002650 range, 0, 0);
2651 if (ret > 0)
2652 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002653 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002654 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002655 default:
2656 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002657 }
Chris Masone441d542009-01-05 16:57:23 -05002658out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002659 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002660 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002661}
2662
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002663static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002664{
2665 struct btrfs_ioctl_vol_args *vol_args;
2666 int ret;
2667
Chris Masone441d542009-01-05 16:57:23 -05002668 if (!capable(CAP_SYS_ADMIN))
2669 return -EPERM;
2670
David Sterba171938e2017-03-28 14:44:21 +02002671 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002672 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002673
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002674 mutex_lock(&fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002675 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002676 if (IS_ERR(vol_args)) {
2677 ret = PTR_ERR(vol_args);
2678 goto out;
2679 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002680
Mark Fasheh5516e592008-07-24 12:20:14 -04002681 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002682 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002683
Anand Jain43d20762014-07-01 00:58:56 +08002684 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002685 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002686
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002687 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002688out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002689 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02002690 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002691 return ret;
2692}
2693
Anand Jain6b526ed2016-02-13 10:01:39 +08002694static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002695{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002696 struct inode *inode = file_inode(file);
2697 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002698 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002699 int ret;
2700
Chris Masone441d542009-01-05 16:57:23 -05002701 if (!capable(CAP_SYS_ADMIN))
2702 return -EPERM;
2703
Miao Xieda249272012-11-26 08:44:50 +00002704 ret = mnt_want_write_file(file);
2705 if (ret)
2706 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002707
Li Zefandae7b662009-04-08 15:06:54 +08002708 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002709 if (IS_ERR(vol_args)) {
2710 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002711 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002712 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002713
Anand Jain6b526ed2016-02-13 10:01:39 +08002714 /* Check for compatibility reject unknown flags */
David Sterba735654e2016-02-15 18:15:21 +01002715 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2716 return -EOPNOTSUPP;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002717
David Sterba171938e2017-03-28 14:44:21 +02002718 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002719 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2720 goto out;
2721 }
2722
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002723 mutex_lock(&fs_info->volume_mutex);
David Sterba735654e2016-02-15 18:15:21 +01002724 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002725 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002726 } else {
2727 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002728 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002729 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002730 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02002731 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002732
Anand Jain6b526ed2016-02-13 10:01:39 +08002733 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002734 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002735 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002736 vol_args->devid);
2737 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002738 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002739 vol_args->name);
2740 }
Anand Jain183860f2013-05-17 10:52:45 +00002741out:
2742 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002743err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002744 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002745 return ret;
2746}
2747
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002748static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2749{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002750 struct inode *inode = file_inode(file);
2751 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002752 struct btrfs_ioctl_vol_args *vol_args;
2753 int ret;
2754
2755 if (!capable(CAP_SYS_ADMIN))
2756 return -EPERM;
2757
2758 ret = mnt_want_write_file(file);
2759 if (ret)
2760 return ret;
2761
David Sterba171938e2017-03-28 14:44:21 +02002762 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002763 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002764 goto out_drop_write;
2765 }
2766
2767 vol_args = memdup_user(arg, sizeof(*vol_args));
2768 if (IS_ERR(vol_args)) {
2769 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002770 goto out;
2771 }
2772
David Sterba58d7bbf2016-05-04 14:10:47 +02002773 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002774 mutex_lock(&fs_info->volume_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002775 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002776 mutex_unlock(&fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002777
2778 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002779 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002780 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002781out:
David Sterba171938e2017-03-28 14:44:21 +02002782 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002783out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002784 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002785
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002786 return ret;
2787}
2788
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002789static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2790 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002791{
Li Zefan027ed2f2011-06-08 08:27:56 +00002792 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002793 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002794 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002795 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002796
Li Zefan027ed2f2011-06-08 08:27:56 +00002797 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2798 if (!fi_args)
2799 return -ENOMEM;
2800
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002801 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002802 fi_args->num_devices = fs_devices->num_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002803 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002804
Byongho Leed7641a42015-09-01 23:10:57 +09002805 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002806 if (device->devid > fi_args->max_id)
2807 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002808 }
2809 mutex_unlock(&fs_devices->device_list_mutex);
2810
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002811 fi_args->nodesize = fs_info->super_copy->nodesize;
2812 fi_args->sectorsize = fs_info->super_copy->sectorsize;
2813 fi_args->clone_alignment = fs_info->super_copy->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002814
Li Zefan027ed2f2011-06-08 08:27:56 +00002815 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2816 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002817
Li Zefan027ed2f2011-06-08 08:27:56 +00002818 kfree(fi_args);
2819 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002820}
2821
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002822static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2823 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002824{
2825 struct btrfs_ioctl_dev_info_args *di_args;
2826 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002827 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002828 int ret = 0;
2829 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002830
Jan Schmidt475f6382011-03-11 15:41:01 +01002831 di_args = memdup_user(arg, sizeof(*di_args));
2832 if (IS_ERR(di_args))
2833 return PTR_ERR(di_args);
2834
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002835 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002836 s_uuid = di_args->uuid;
2837
2838 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002839 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002840
2841 if (!dev) {
2842 ret = -ENODEV;
2843 goto out;
2844 }
2845
2846 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002847 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2848 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002849 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002850 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002851 struct rcu_string *name;
2852
2853 rcu_read_lock();
2854 name = rcu_dereference(dev->name);
2855 strncpy(di_args->path, name->str, sizeof(di_args->path));
2856 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002857 di_args->path[sizeof(di_args->path) - 1] = 0;
2858 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002859 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002860 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002861
2862out:
David Sterba55793c02013-04-26 15:20:23 +00002863 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002864 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2865 ret = -EFAULT;
2866
2867 kfree(di_args);
2868 return ret;
2869}
2870
Mark Fashehf4414602015-06-30 14:42:05 -07002871static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002872{
2873 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002874
Mark Fasheh416161d2013-08-06 11:42:51 -07002875 page = grab_cache_page(inode->i_mapping, index);
2876 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002877 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002878
2879 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002880 int ret;
2881
2882 ret = btrfs_readpage(NULL, page);
2883 if (ret)
2884 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002885 lock_page(page);
2886 if (!PageUptodate(page)) {
2887 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002888 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002889 return ERR_PTR(-EIO);
2890 }
2891 if (page->mapping != inode->i_mapping) {
2892 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002893 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002894 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002895 }
2896 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002897
2898 return page;
2899}
2900
Mark Fashehf4414602015-06-30 14:42:05 -07002901static int gather_extent_pages(struct inode *inode, struct page **pages,
2902 int num_pages, u64 off)
2903{
2904 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002905 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002906
2907 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002908again:
Mark Fashehf4414602015-06-30 14:42:05 -07002909 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002910 if (IS_ERR(pages[i])) {
2911 int err = PTR_ERR(pages[i]);
2912
2913 if (err == -EAGAIN)
2914 goto again;
2915 pages[i] = NULL;
2916 return err;
2917 }
Mark Fashehf4414602015-06-30 14:42:05 -07002918 }
2919 return 0;
2920}
2921
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002922static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2923 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002924{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002925 /*
2926 * Do any pending delalloc/csum calculations on inode, one way or
2927 * another, and lock file content.
2928 * The locking order is:
2929 *
2930 * 1) pages
2931 * 2) range in the inode's io tree
2932 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002933 while (1) {
2934 struct btrfs_ordered_extent *ordered;
2935 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2936 ordered = btrfs_lookup_first_ordered_extent(inode,
2937 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002938 if ((!ordered ||
2939 ordered->file_offset + ordered->len <= off ||
2940 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002941 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002942 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2943 if (ordered)
2944 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002945 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002946 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002947 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2948 if (ordered)
2949 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002950 if (!retry_range_locking)
2951 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002952 btrfs_wait_ordered_range(inode, off, len);
2953 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002954 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002955}
2956
Mark Fashehf4414602015-06-30 14:42:05 -07002957static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002958{
Al Viro59551022016-01-22 15:40:57 -05002959 inode_unlock(inode1);
2960 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002961}
2962
Mark Fashehf4414602015-06-30 14:42:05 -07002963static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2964{
2965 if (inode1 < inode2)
2966 swap(inode1, inode2);
2967
Al Viro59551022016-01-22 15:40:57 -05002968 inode_lock_nested(inode1, I_MUTEX_PARENT);
2969 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002970}
2971
2972static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2973 struct inode *inode2, u64 loff2, u64 len)
2974{
2975 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2976 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2977}
2978
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002979static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2980 struct inode *inode2, u64 loff2, u64 len,
2981 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002982{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002983 int ret;
2984
Mark Fasheh416161d2013-08-06 11:42:51 -07002985 if (inode1 < inode2) {
2986 swap(inode1, inode2);
2987 swap(loff1, loff2);
2988 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002989 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2990 if (ret)
2991 return ret;
2992 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2993 if (ret)
2994 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2995 loff1 + len - 1);
2996 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002997}
2998
2999struct cmp_pages {
3000 int num_pages;
3001 struct page **src_pages;
3002 struct page **dst_pages;
3003};
3004
3005static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3006{
3007 int i;
3008 struct page *pg;
3009
3010 for (i = 0; i < cmp->num_pages; i++) {
3011 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003012 if (pg) {
3013 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003014 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003015 }
Mark Fashehf4414602015-06-30 14:42:05 -07003016 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003017 if (pg) {
3018 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003019 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003020 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003021 }
Mark Fashehf4414602015-06-30 14:42:05 -07003022 kfree(cmp->src_pages);
3023 kfree(cmp->dst_pages);
3024}
3025
3026static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3027 struct inode *dst, u64 dst_loff,
3028 u64 len, struct cmp_pages *cmp)
3029{
3030 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003031 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003032 struct page **src_pgarr, **dst_pgarr;
3033
3034 /*
3035 * We must gather up all the pages before we initiate our
3036 * extent locking. We use an array for the page pointers. Size
3037 * of the array is bounded by len, which is in turn bounded by
3038 * BTRFS_MAX_DEDUPE_LEN.
3039 */
David Sterba66722f72016-02-11 15:01:38 +01003040 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
3041 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07003042 if (!src_pgarr || !dst_pgarr) {
3043 kfree(src_pgarr);
3044 kfree(dst_pgarr);
3045 return -ENOMEM;
3046 }
3047 cmp->num_pages = num_pages;
3048 cmp->src_pages = src_pgarr;
3049 cmp->dst_pages = dst_pgarr;
3050
Filipe Mananab1517622017-02-21 17:14:52 +00003051 /*
3052 * If deduping ranges in the same inode, locking rules make it mandatory
3053 * to always lock pages in ascending order to avoid deadlocks with
3054 * concurrent tasks (such as starting writeback/delalloc).
3055 */
3056 if (src == dst && dst_loff < loff) {
3057 swap(src_pgarr, dst_pgarr);
3058 swap(loff, dst_loff);
3059 }
3060
3061 ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003062 if (ret)
3063 goto out;
3064
Filipe Mananab1517622017-02-21 17:14:52 +00003065 ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003066
3067out:
3068 if (ret)
3069 btrfs_cmp_data_free(cmp);
3070 return 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003071}
3072
David Sterba1a287cf2017-02-10 20:15:10 +01003073static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003074{
3075 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003076 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003077 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003078 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003079 void *addr, *dst_addr;
3080
Mark Fashehf4414602015-06-30 14:42:05 -07003081 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003082 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003083 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003084 cmp_len = len;
3085
Mark Fashehf4414602015-06-30 14:42:05 -07003086 BUG_ON(i >= cmp->num_pages);
3087
3088 src_page = cmp->src_pages[i];
3089 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003090 ASSERT(PageLocked(src_page));
3091 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003092
Mark Fasheh416161d2013-08-06 11:42:51 -07003093 addr = kmap_atomic(src_page);
3094 dst_addr = kmap_atomic(dst_page);
3095
3096 flush_dcache_page(src_page);
3097 flush_dcache_page(dst_page);
3098
3099 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003100 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003101
3102 kunmap_atomic(addr);
3103 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003104
3105 if (ret)
3106 break;
3107
Mark Fasheh416161d2013-08-06 11:42:51 -07003108 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003109 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003110 }
3111
3112 return ret;
3113}
3114
Mark Fashehe1d227a2015-06-08 15:05:25 -07003115static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3116 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003117{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003118 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003119 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3120
Mark Fashehe1d227a2015-06-08 15:05:25 -07003121 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003122 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003123
3124 /* if we extend to eof, continue to block boundary */
3125 if (off + len == inode->i_size)
3126 *plen = len = ALIGN(inode->i_size, bs) - off;
3127
Mark Fasheh416161d2013-08-06 11:42:51 -07003128 /* Check that we are block aligned - btrfs_clone() requires this */
3129 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3130 return -EINVAL;
3131
3132 return 0;
3133}
3134
Mark Fashehe1d227a2015-06-08 15:05:25 -07003135static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003136 struct inode *dst, u64 dst_loff)
3137{
3138 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003139 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003140 struct cmp_pages cmp;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003141 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003142 u64 same_lock_start = 0;
3143 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003144
Filipe Manana113e8282015-03-30 18:26:47 +01003145 if (len == 0)
3146 return 0;
3147
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003148 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003149 inode_lock(src);
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003150 else
3151 btrfs_double_inode_lock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003152
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003153 ret = extent_same_check_offsets(src, loff, &len, olen);
3154 if (ret)
3155 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003156
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003157 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3158 if (ret)
3159 goto out_unlock;
3160
3161 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003162 /*
3163 * Single inode case wants the same checks, except we
3164 * don't want our length pushed out past i_size as
3165 * comparing that data range makes no sense.
3166 *
3167 * extent_same_check_offsets() will do this for an
3168 * unaligned length at i_size, so catch it here and
3169 * reject the request.
3170 *
3171 * This effectively means we require aligned extents
3172 * for the single-inode case, whereas the other cases
3173 * allow an unaligned length so long as it ends at
3174 * i_size.
3175 */
3176 if (len != olen) {
3177 ret = -EINVAL;
3178 goto out_unlock;
3179 }
3180
3181 /* Check for overlapping ranges */
3182 if (dst_loff + len > loff && dst_loff < loff + len) {
3183 ret = -EINVAL;
3184 goto out_unlock;
3185 }
3186
3187 same_lock_start = min_t(u64, loff, dst_loff);
3188 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003189 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003190
3191 /* don't make the dst file partly checksummed */
3192 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3193 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3194 ret = -EINVAL;
3195 goto out_unlock;
3196 }
3197
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003198again:
Mark Fashehf4414602015-06-30 14:42:05 -07003199 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3200 if (ret)
3201 goto out_unlock;
3202
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003203 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003204 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3205 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003206 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003207 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3208 false);
3209 /*
3210 * If one of the inodes has dirty pages in the respective range or
3211 * ordered extents, we need to flush dellaloc and wait for all ordered
3212 * extents in the range. We must unlock the pages and the ranges in the
3213 * io trees to avoid deadlocks when flushing delalloc (requires locking
3214 * pages) and when waiting for ordered extents to complete (they require
3215 * range locking).
3216 */
3217 if (ret == -EAGAIN) {
3218 /*
3219 * Ranges in the io trees already unlocked. Now unlock all
3220 * pages before waiting for all IO to complete.
3221 */
3222 btrfs_cmp_data_free(&cmp);
3223 if (same_inode) {
3224 btrfs_wait_ordered_range(src, same_lock_start,
3225 same_lock_len);
3226 } else {
3227 btrfs_wait_ordered_range(src, loff, len);
3228 btrfs_wait_ordered_range(dst, dst_loff, len);
3229 }
3230 goto again;
3231 }
3232 ASSERT(ret == 0);
3233 if (WARN_ON(ret)) {
3234 /* ranges in the io trees already unlocked */
3235 btrfs_cmp_data_free(&cmp);
3236 return ret;
3237 }
Mark Fashehf4414602015-06-30 14:42:05 -07003238
Mark Fasheh207910d2015-06-30 14:42:04 -07003239 /* pass original length for comparison so we stay within i_size */
David Sterba1a287cf2017-02-10 20:15:10 +01003240 ret = btrfs_cmp_data(olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003241 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003242 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003243
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003244 if (same_inode)
3245 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3246 same_lock_start + same_lock_len - 1);
3247 else
3248 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003249
3250 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003251out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003252 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003253 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003254 else
3255 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003256
3257 return ret;
3258}
3259
Byongho Leeee221842015-12-15 01:42:10 +09003260#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003261
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003262ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3263 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003264{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003265 struct inode *src = file_inode(src_file);
3266 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003267 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003268 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003269
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003270 if (olen > BTRFS_MAX_DEDUPE_LEN)
3271 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003272
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003273 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003274 /*
3275 * Btrfs does not support blocksize < page_size. As a
3276 * result, btrfs_cmp_data() won't correctly handle
3277 * this situation without an update.
3278 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003279 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003280 }
3281
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003282 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3283 if (res)
3284 return res;
3285 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003286}
3287
Filipe Mananaf82a9902014-06-01 01:50:28 +01003288static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3289 struct inode *inode,
3290 u64 endoff,
3291 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003292 const u64 olen,
3293 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003294{
3295 struct btrfs_root *root = BTRFS_I(inode)->root;
3296 int ret;
3297
3298 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003299 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003300 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003301 /*
3302 * We round up to the block size at eof when determining which
3303 * extents to clone above, but shouldn't round up the file size.
3304 */
3305 if (endoff > destoff + olen)
3306 endoff = destoff + olen;
3307 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003308 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003309
3310 ret = btrfs_update_inode(trans, root, inode);
3311 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003312 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003313 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003314 goto out;
3315 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003316 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003317out:
3318 return ret;
3319}
3320
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003321static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003322 const struct btrfs_trans_handle *trans,
3323 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003324 const u64 hole_offset,
3325 const u64 hole_len)
3326{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003327 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003328 struct extent_map *em;
3329 int ret;
3330
3331 em = alloc_extent_map();
3332 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003333 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003334 return;
3335 }
3336
Filipe Manana14f59792014-06-29 21:45:40 +01003337 if (path) {
3338 struct btrfs_file_extent_item *fi;
3339
3340 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3341 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003342 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003343 em->generation = -1;
3344 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3345 BTRFS_FILE_EXTENT_INLINE)
3346 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003347 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003348 } else {
3349 em->start = hole_offset;
3350 em->len = hole_len;
3351 em->ram_bytes = em->len;
3352 em->orig_start = hole_offset;
3353 em->block_start = EXTENT_MAP_HOLE;
3354 em->block_len = 0;
3355 em->orig_block_len = 0;
3356 em->compress_type = BTRFS_COMPRESS_NONE;
3357 em->generation = trans->transid;
3358 }
3359
3360 while (1) {
3361 write_lock(&em_tree->lock);
3362 ret = add_extent_mapping(em_tree, em, 1);
3363 write_unlock(&em_tree->lock);
3364 if (ret != -EEXIST) {
3365 free_extent_map(em);
3366 break;
3367 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003368 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003369 em->start + em->len - 1, 0);
3370 }
3371
David Sterbaee39b432014-09-30 01:33:33 +02003372 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003373 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003374}
3375
Filipe Manana8039d872015-10-13 15:15:00 +01003376/*
3377 * Make sure we do not end up inserting an inline extent into a file that has
3378 * already other (non-inline) extents. If a file has an inline extent it can
3379 * not have any other extents and the (single) inline extent must start at the
3380 * file offset 0. Failing to respect these rules will lead to file corruption,
3381 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3382 *
3383 * We can have extents that have been already written to disk or we can have
3384 * dirty ranges still in delalloc, in which case the extent maps and items are
3385 * created only when we run delalloc, and the delalloc ranges might fall outside
3386 * the range we are currently locking in the inode's io tree. So we check the
3387 * inode's i_size because of that (i_size updates are done while holding the
3388 * i_mutex, which we are holding here).
3389 * We also check to see if the inode has a size not greater than "datal" but has
3390 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3391 * protected against such concurrent fallocate calls by the i_mutex).
3392 *
3393 * If the file has no extents but a size greater than datal, do not allow the
3394 * copy because we would need turn the inline extent into a non-inline one (even
3395 * with NO_HOLES enabled). If we find our destination inode only has one inline
3396 * extent, just overwrite it with the source inline extent if its size is less
3397 * than the source extent's size, or we could copy the source inline extent's
3398 * data into the destination inode's inline extent if the later is greater then
3399 * the former.
3400 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003401static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003402 struct btrfs_trans_handle *trans,
3403 struct btrfs_path *path,
3404 struct btrfs_key *new_key,
3405 const u64 drop_start,
3406 const u64 datal,
3407 const u64 skip,
3408 const u64 size,
3409 char *inline_data)
3410{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003411 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003412 struct btrfs_root *root = BTRFS_I(dst)->root;
3413 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003414 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003415 int ret;
3416 struct btrfs_key key;
3417
3418 if (new_key->offset > 0)
3419 return -EOPNOTSUPP;
3420
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003421 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003422 key.type = BTRFS_EXTENT_DATA_KEY;
3423 key.offset = 0;
3424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3425 if (ret < 0) {
3426 return ret;
3427 } else if (ret > 0) {
3428 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3429 ret = btrfs_next_leaf(root, path);
3430 if (ret < 0)
3431 return ret;
3432 else if (ret > 0)
3433 goto copy_inline_extent;
3434 }
3435 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003436 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003437 key.type == BTRFS_EXTENT_DATA_KEY) {
3438 ASSERT(key.offset > 0);
3439 return -EOPNOTSUPP;
3440 }
3441 } else if (i_size_read(dst) <= datal) {
3442 struct btrfs_file_extent_item *ei;
3443 u64 ext_len;
3444
3445 /*
3446 * If the file size is <= datal, make sure there are no other
3447 * extents following (can happen do to an fallocate call with
3448 * the flag FALLOC_FL_KEEP_SIZE).
3449 */
3450 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3451 struct btrfs_file_extent_item);
3452 /*
3453 * If it's an inline extent, it can not have other extents
3454 * following it.
3455 */
3456 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3457 BTRFS_FILE_EXTENT_INLINE)
3458 goto copy_inline_extent;
3459
3460 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3461 if (ext_len > aligned_end)
3462 return -EOPNOTSUPP;
3463
3464 ret = btrfs_next_item(root, path);
3465 if (ret < 0) {
3466 return ret;
3467 } else if (ret == 0) {
3468 btrfs_item_key_to_cpu(path->nodes[0], &key,
3469 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003470 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003471 key.type == BTRFS_EXTENT_DATA_KEY)
3472 return -EOPNOTSUPP;
3473 }
3474 }
3475
3476copy_inline_extent:
3477 /*
3478 * We have no extent items, or we have an extent at offset 0 which may
3479 * or may not be inlined. All these cases are dealt the same way.
3480 */
3481 if (i_size_read(dst) > datal) {
3482 /*
3483 * If the destination inode has an inline extent...
3484 * This would require copying the data from the source inline
3485 * extent into the beginning of the destination's inline extent.
3486 * But this is really complex, both extents can be compressed
3487 * or just one of them, which would require decompressing and
3488 * re-compressing data (which could increase the new compressed
3489 * size, not allowing the compressed data to fit anymore in an
3490 * inline extent).
3491 * So just don't support this case for now (it should be rare,
3492 * we are not really saving space when cloning inline extents).
3493 */
3494 return -EOPNOTSUPP;
3495 }
3496
3497 btrfs_release_path(path);
3498 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3499 if (ret)
3500 return ret;
3501 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3502 if (ret)
3503 return ret;
3504
3505 if (skip) {
3506 const u32 start = btrfs_file_extent_calc_inline_size(0);
3507
3508 memmove(inline_data + start, inline_data + start + skip, datal);
3509 }
3510
3511 write_extent_buffer(path->nodes[0], inline_data,
3512 btrfs_item_ptr_offset(path->nodes[0],
3513 path->slots[0]),
3514 size);
3515 inode_add_bytes(dst, datal);
3516
3517 return 0;
3518}
3519
Mark Fasheh32b7c682013-08-06 11:42:49 -07003520/**
3521 * btrfs_clone() - clone a range from inode file to another
3522 *
3523 * @src: Inode to clone from
3524 * @inode: Inode to clone to
3525 * @off: Offset within source to start clone from
3526 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003527 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003528 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003529 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003530 */
3531static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003532 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003533 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003534{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003535 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003536 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003537 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003538 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003539 struct btrfs_trans_handle *trans;
3540 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003541 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003542 u32 nritems;
3543 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003544 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003545 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003546 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003547
3548 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003549 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3550 if (!buf)
3551 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003552
3553 path = btrfs_alloc_path();
3554 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003555 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003556 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003557 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003558
David Sterbae4058b52015-11-27 16:31:35 +01003559 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003560 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003561 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003562 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003563 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003564
3565 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003566 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003567
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003568 /*
3569 * note the key will change type as we walk through the
3570 * tree.
3571 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003572 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003573 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3574 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003575 if (ret < 0)
3576 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003577 /*
3578 * First search, if no extent item that starts at offset off was
3579 * found but the previous item is an extent item, it's possible
3580 * it might overlap our target range, therefore process it.
3581 */
3582 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3583 btrfs_item_key_to_cpu(path->nodes[0], &key,
3584 path->slots[0] - 1);
3585 if (key.type == BTRFS_EXTENT_DATA_KEY)
3586 path->slots[0]--;
3587 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003588
Yan Zhengae01a0a2008-08-04 23:23:47 -04003589 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003590process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003591 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003592 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003593 if (ret < 0)
3594 goto out;
3595 if (ret > 0)
3596 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003597 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003598 }
3599 leaf = path->nodes[0];
3600 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003601
Yan Zhengae01a0a2008-08-04 23:23:47 -04003602 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003603 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003604 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003605 break;
3606
David Sterba962a2982014-06-04 18:41:45 +02003607 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003608 struct btrfs_file_extent_item *extent;
3609 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003610 u32 size;
3611 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003612 u64 disko = 0, diskl = 0;
3613 u64 datao = 0, datal = 0;
3614 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003615 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003616
Sage Weilc5c9cd42008-11-12 14:32:25 -05003617 extent = btrfs_item_ptr(leaf, slot,
3618 struct btrfs_file_extent_item);
3619 comp = btrfs_file_extent_compression(leaf, extent);
3620 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003621 if (type == BTRFS_FILE_EXTENT_REG ||
3622 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003623 disko = btrfs_file_extent_disk_bytenr(leaf,
3624 extent);
3625 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3626 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003627 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003628 datal = btrfs_file_extent_num_bytes(leaf,
3629 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003630 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3631 /* take upper bound, may be compressed */
3632 datal = btrfs_file_extent_ram_bytes(leaf,
3633 extent);
3634 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003635
Filipe Manana2c463822014-05-31 02:31:05 +01003636 /*
3637 * The first search might have left us at an extent
3638 * item that ends before our target range's start, can
3639 * happen if we have holes and NO_HOLES feature enabled.
3640 */
3641 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003642 path->slots[0]++;
3643 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003644 } else if (key.offset >= off + len) {
3645 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003646 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003647 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003648 size = btrfs_item_size_nr(leaf, slot);
3649 read_extent_buffer(leaf, buf,
3650 btrfs_item_ptr_offset(leaf, slot),
3651 size);
3652
3653 btrfs_release_path(path);
3654 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003655
Zheng Yan31840ae2008-09-23 13:14:14 -04003656 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003657 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003658 if (off <= key.offset)
3659 new_key.offset = key.offset + destoff - off;
3660 else
3661 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003662
Sage Weilb6f34092011-09-20 14:48:51 -04003663 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003664 * Deal with a hole that doesn't have an extent item
3665 * that represents it (NO_HOLES feature enabled).
3666 * This hole is either in the middle of the cloning
3667 * range or at the beginning (fully overlaps it or
3668 * partially overlaps it).
3669 */
3670 if (new_key.offset != last_dest_end)
3671 drop_start = last_dest_end;
3672 else
3673 drop_start = new_key.offset;
3674
3675 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003676 * 1 - adjusting old extent (we may have to split it)
3677 * 1 - add new extent
3678 * 1 - inode update
3679 */
3680 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681 if (IS_ERR(trans)) {
3682 ret = PTR_ERR(trans);
3683 goto out;
3684 }
3685
Chris Masonc8a894d2009-06-27 21:07:03 -04003686 if (type == BTRFS_FILE_EXTENT_REG ||
3687 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003688 /*
3689 * a | --- range to clone ---| b
3690 * | ------------- extent ------------- |
3691 */
3692
Antonio Ospite93915582014-06-04 14:03:48 +02003693 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003694 if (key.offset + datal > off + len)
3695 datal = off + len - key.offset;
3696
Antonio Ospite93915582014-06-04 14:03:48 +02003697 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003698 if (off > key.offset) {
3699 datao += off - key.offset;
3700 datal -= off - key.offset;
3701 }
3702
Josef Bacik5dc562c2012-08-17 13:14:17 -04003703 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003704 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003705 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003706 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003707 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003708 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003709 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003710 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003711 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003712 goto out;
3713 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003714
Sage Weilc5c9cd42008-11-12 14:32:25 -05003715 ret = btrfs_insert_empty_item(trans, root, path,
3716 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003717 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003718 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003719 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003720 goto out;
3721 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003722
3723 leaf = path->nodes[0];
3724 slot = path->slots[0];
3725 write_extent_buffer(leaf, buf,
3726 btrfs_item_ptr_offset(leaf, slot),
3727 size);
3728
3729 extent = btrfs_item_ptr(leaf, slot,
3730 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003731
Sage Weilc5c9cd42008-11-12 14:32:25 -05003732 /* disko == 0 means it's a hole */
3733 if (!disko)
3734 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003735
3736 btrfs_set_file_extent_offset(leaf, extent,
3737 datao);
3738 btrfs_set_file_extent_num_bytes(leaf, extent,
3739 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003740
Sage Weilc5c9cd42008-11-12 14:32:25 -05003741 if (disko) {
3742 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003743 ret = btrfs_inc_extent_ref(trans,
3744 fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003745 disko, diskl, 0,
3746 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003747 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003748 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003749 if (ret) {
3750 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003751 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003752 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003753 goto out;
3754
3755 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003756 }
3757 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3758 u64 skip = 0;
3759 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003760
Sage Weilc5c9cd42008-11-12 14:32:25 -05003761 if (off > key.offset) {
3762 skip = off - key.offset;
3763 new_key.offset += skip;
3764 }
Chris Masond3977122009-01-05 21:25:51 -05003765
Liu Boaa42ffd2012-09-18 03:52:23 -06003766 if (key.offset + datal > off + len)
3767 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003768
Sage Weilc5c9cd42008-11-12 14:32:25 -05003769 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003770 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003771 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003772 goto out;
3773 }
3774 size -= skip + trim;
3775 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003776
David Sterba4a0ab9d2017-02-10 20:18:49 +01003777 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003778 trans, path,
3779 &new_key,
3780 drop_start,
3781 datal,
3782 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003783 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003784 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003785 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003786 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003787 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003788 goto out;
3789 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003790 leaf = path->nodes[0];
3791 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003792 }
3793
Filipe Manana7ffbb592014-06-09 03:48:05 +01003794 /* If we have an implicit hole (NO_HOLES feature). */
3795 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003796 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003797 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003798 new_key.offset - drop_start);
3799
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003800 clone_update_extent_map(BTRFS_I(inode), trans,
3801 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003802
Sage Weilc5c9cd42008-11-12 14:32:25 -05003803 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003804 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003805
Filipe Manana62e23902014-08-08 02:47:06 +01003806 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003807 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003808 ret = clone_finish_inode_update(trans, inode,
3809 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003810 destoff, olen,
3811 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003812 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003813 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003814 if (new_key.offset + datal >= destoff + len)
3815 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003816 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003817 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003818 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003819
3820 if (fatal_signal_pending(current)) {
3821 ret = -EINTR;
3822 goto out;
3823 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003824 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003825 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003826
Filipe Mananaf82a9902014-06-01 01:50:28 +01003827 if (last_dest_end < destoff + len) {
3828 /*
3829 * We have an implicit hole (NO_HOLES feature is enabled) that
3830 * fully or partially overlaps our cloning range at its end.
3831 */
3832 btrfs_release_path(path);
3833
3834 /*
3835 * 1 - remove extent(s)
3836 * 1 - inode update
3837 */
3838 trans = btrfs_start_transaction(root, 2);
3839 if (IS_ERR(trans)) {
3840 ret = PTR_ERR(trans);
3841 goto out;
3842 }
3843 ret = btrfs_drop_extents(trans, root, inode,
3844 last_dest_end, destoff + len, 1);
3845 if (ret) {
3846 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003847 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003848 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003849 goto out;
3850 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003851 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3852 last_dest_end,
3853 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003854 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003855 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003856 }
3857
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003858out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003859 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003860 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003861 return ret;
3862}
3863
Zach Brown3db11b22015-11-10 16:53:32 -05003864static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3865 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003866{
Al Viro54563d42013-09-01 15:57:51 -04003867 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003868 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003869 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003870 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003871 int ret;
3872 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003873 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003874 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003875
3876 /*
3877 * TODO:
3878 * - split compressed inline extents. annoying: we need to
3879 * decompress into destination's address_space (the file offset
3880 * may change, so source mapping won't do), then recompress (or
3881 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003882 *
3883 * - split destination inode's inline extents. The inline extents can
3884 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003885 */
3886
Mark Fasheh32b7c682013-08-06 11:42:49 -07003887 if (btrfs_root_readonly(root))
3888 return -EROFS;
3889
Zach Brown3db11b22015-11-10 16:53:32 -05003890 if (file_src->f_path.mnt != file->f_path.mnt ||
3891 src->i_sb != inode->i_sb)
3892 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003893
3894 /* don't make the dst file partly checksummed */
3895 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3896 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003897 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003898
Mark Fasheh32b7c682013-08-06 11:42:49 -07003899 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003900 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003901
3902 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003903 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003904 } else {
Al Viro59551022016-01-22 15:40:57 -05003905 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003906 }
3907
3908 /* determine range to clone */
3909 ret = -EINVAL;
3910 if (off + len > src->i_size || off + len < off)
3911 goto out_unlock;
3912 if (len == 0)
3913 olen = len = src->i_size - off;
3914 /* if we extend to eof, continue to block boundary */
3915 if (off + len == src->i_size)
3916 len = ALIGN(src->i_size, bs) - off;
3917
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003918 if (len == 0) {
3919 ret = 0;
3920 goto out_unlock;
3921 }
3922
Mark Fasheh32b7c682013-08-06 11:42:49 -07003923 /* verify the end result is block aligned */
3924 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3925 !IS_ALIGNED(destoff, bs))
3926 goto out_unlock;
3927
3928 /* verify if ranges are overlapped within the same file */
3929 if (same_inode) {
3930 if (destoff + len > off && destoff < off + len)
3931 goto out_unlock;
3932 }
3933
3934 if (destoff > inode->i_size) {
3935 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3936 if (ret)
3937 goto out_unlock;
3938 }
3939
Filipe Mananac125b8b2014-05-23 05:03:34 +01003940 /*
3941 * Lock the target range too. Right after we replace the file extent
3942 * items in the fs tree (which now point to the cloned data), we might
3943 * have a worker replace them with extent items relative to a write
3944 * operation that was issued before this clone operation (i.e. confront
3945 * with inode.c:btrfs_finish_ordered_io).
3946 */
3947 if (same_inode) {
3948 u64 lock_start = min_t(u64, off, destoff);
3949 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003950
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003951 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003952 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003953 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3954 true);
3955 }
3956 ASSERT(ret == 0);
3957 if (WARN_ON(ret)) {
3958 /* ranges in the io trees already unlocked */
3959 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003960 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003961
Mark Fasheh1c919a52015-06-30 14:42:08 -07003962 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003963
Filipe Mananac125b8b2014-05-23 05:03:34 +01003964 if (same_inode) {
3965 u64 lock_start = min_t(u64, off, destoff);
3966 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3967
3968 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3969 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003970 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003971 }
3972 /*
3973 * Truncate page cache pages so that future reads will see the cloned
3974 * data immediately and not the previous data.
3975 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303976 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003977 round_down(destoff, PAGE_SIZE),
3978 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003979out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003980 if (!same_inode)
3981 btrfs_double_inode_unlock(src, inode);
3982 else
Al Viro59551022016-01-22 15:40:57 -05003983 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003984 return ret;
3985}
3986
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003987int btrfs_clone_file_range(struct file *src_file, loff_t off,
3988 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003989{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003990 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003991}
3992
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003993/*
3994 * there are many ways the trans_start and trans_end ioctls can lead
3995 * to deadlocks. They should only be used by applications that
3996 * basically own the machine, and have a very in depth understanding
3997 * of all the possible deadlocks and enospc problems.
3998 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003999static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004000{
Al Viro496ad9a2013-01-23 17:07:38 -05004001 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004002 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004003 struct btrfs_root *root = BTRFS_I(inode)->root;
4004 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004005 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004006
Sage Weil1ab86ae2009-09-29 18:38:44 -04004007 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04004008 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004009 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004010
4011 ret = -EINPROGRESS;
4012 if (file->private_data)
4013 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004014
Li Zefanb83cc962010-12-20 16:04:08 +08004015 ret = -EROFS;
4016 if (btrfs_root_readonly(root))
4017 goto out;
4018
Al Viroa561be72011-11-23 11:57:51 -05004019 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05004020 if (ret)
4021 goto out;
4022
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004023 atomic_inc(&fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004024
Sage Weil1ab86ae2009-09-29 18:38:44 -04004025 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004026 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00004027 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04004028 goto out_drop;
4029
4030 file->private_data = trans;
4031 return 0;
4032
4033out_drop:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004034 atomic_dec(&fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05004035 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004036out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004037 return ret;
4038}
4039
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004040static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4041{
Al Viro496ad9a2013-01-23 17:07:38 -05004042 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004043 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004044 struct btrfs_root *root = BTRFS_I(inode)->root;
4045 struct btrfs_root *new_root;
4046 struct btrfs_dir_item *di;
4047 struct btrfs_trans_handle *trans;
4048 struct btrfs_path *path;
4049 struct btrfs_key location;
4050 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004051 u64 objectid = 0;
4052 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004053 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004054
4055 if (!capable(CAP_SYS_ADMIN))
4056 return -EPERM;
4057
Miao Xie3c04ce02012-11-26 08:43:07 +00004058 ret = mnt_want_write_file(file);
4059 if (ret)
4060 return ret;
4061
4062 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4063 ret = -EFAULT;
4064 goto out;
4065 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004066
4067 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304068 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004069
4070 location.objectid = objectid;
4071 location.type = BTRFS_ROOT_ITEM_KEY;
4072 location.offset = (u64)-1;
4073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004074 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004075 if (IS_ERR(new_root)) {
4076 ret = PTR_ERR(new_root);
4077 goto out;
4078 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004079
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004080 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004081 if (!path) {
4082 ret = -ENOMEM;
4083 goto out;
4084 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004085 path->leave_spinning = 1;
4086
4087 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004088 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004089 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004090 ret = PTR_ERR(trans);
4091 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004092 }
4093
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004094 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4095 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004096 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004097 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004098 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004099 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004100 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004101 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004102 ret = -ENOENT;
4103 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004104 }
4105
4106 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4107 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4108 btrfs_mark_buffer_dirty(path->nodes[0]);
4109 btrfs_free_path(path);
4110
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004111 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004112 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004113out:
4114 mnt_drop_write_file(file);
4115 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004116}
4117
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004118void btrfs_get_block_group_info(struct list_head *groups_list,
4119 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004120{
4121 struct btrfs_block_group_cache *block_group;
4122
4123 space->total_bytes = 0;
4124 space->used_bytes = 0;
4125 space->flags = 0;
4126 list_for_each_entry(block_group, groups_list, list) {
4127 space->flags = block_group->flags;
4128 space->total_bytes += block_group->key.offset;
4129 space->used_bytes +=
4130 btrfs_block_group_used(&block_group->item);
4131 }
4132}
4133
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004134static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4135 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004136{
4137 struct btrfs_ioctl_space_args space_args;
4138 struct btrfs_ioctl_space_info space;
4139 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004140 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004141 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004142 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004143 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4144 BTRFS_BLOCK_GROUP_SYSTEM,
4145 BTRFS_BLOCK_GROUP_METADATA,
4146 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4147 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004148 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004149 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004150 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004151 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004152
4153 if (copy_from_user(&space_args,
4154 (struct btrfs_ioctl_space_args __user *)arg,
4155 sizeof(space_args)))
4156 return -EFAULT;
4157
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004158 for (i = 0; i < num_types; i++) {
4159 struct btrfs_space_info *tmp;
4160
4161 info = NULL;
4162 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004163 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004164 list) {
4165 if (tmp->flags == types[i]) {
4166 info = tmp;
4167 break;
4168 }
4169 }
4170 rcu_read_unlock();
4171
4172 if (!info)
4173 continue;
4174
4175 down_read(&info->groups_sem);
4176 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4177 if (!list_empty(&info->block_groups[c]))
4178 slot_count++;
4179 }
4180 up_read(&info->groups_sem);
4181 }
Josef Bacik1406e432010-01-13 18:19:06 +00004182
David Sterba36523e952014-02-07 14:34:12 +01004183 /*
4184 * Global block reserve, exported as a space_info
4185 */
4186 slot_count++;
4187
Chris Mason7fde62b2010-03-16 15:40:10 -04004188 /* space_slots == 0 means they are asking for a count */
4189 if (space_args.space_slots == 0) {
4190 space_args.total_spaces = slot_count;
4191 goto out;
4192 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004193
Dan Rosenberg51788b12011-02-14 16:04:23 -05004194 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004195
Chris Mason7fde62b2010-03-16 15:40:10 -04004196 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004197
Chris Mason7fde62b2010-03-16 15:40:10 -04004198 /* we generally have at most 6 or so space infos, one for each raid
4199 * level. So, a whole page should be more than enough for everyone
4200 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004201 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004202 return -ENOMEM;
4203
4204 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004205 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004206 if (!dest)
4207 return -ENOMEM;
4208 dest_orig = dest;
4209
4210 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004211 for (i = 0; i < num_types; i++) {
4212 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004213
Dan Rosenberg51788b12011-02-14 16:04:23 -05004214 if (!slot_count)
4215 break;
4216
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004217 info = NULL;
4218 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004219 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004220 list) {
4221 if (tmp->flags == types[i]) {
4222 info = tmp;
4223 break;
4224 }
4225 }
4226 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004227
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004228 if (!info)
4229 continue;
4230 down_read(&info->groups_sem);
4231 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4232 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004233 btrfs_get_block_group_info(
4234 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004235 memcpy(dest, &space, sizeof(space));
4236 dest++;
4237 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004238 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004239 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004240 if (!slot_count)
4241 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004242 }
4243 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004244 }
Josef Bacik1406e432010-01-13 18:19:06 +00004245
David Sterba36523e952014-02-07 14:34:12 +01004246 /*
4247 * Add global block reserve
4248 */
4249 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004250 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004251
4252 spin_lock(&block_rsv->lock);
4253 space.total_bytes = block_rsv->size;
4254 space.used_bytes = block_rsv->size - block_rsv->reserved;
4255 spin_unlock(&block_rsv->lock);
4256 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4257 memcpy(dest, &space, sizeof(space));
4258 space_args.total_spaces++;
4259 }
4260
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004261 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004262 (arg + sizeof(struct btrfs_ioctl_space_args));
4263
4264 if (copy_to_user(user_dest, dest_orig, alloc_size))
4265 ret = -EFAULT;
4266
4267 kfree(dest_orig);
4268out:
4269 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004270 ret = -EFAULT;
4271
4272 return ret;
4273}
4274
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004275/*
4276 * there are many ways the trans_start and trans_end ioctls can lead
4277 * to deadlocks. They should only be used by applications that
4278 * basically own the machine, and have a very in depth understanding
4279 * of all the possible deadlocks and enospc problems.
4280 */
4281long btrfs_ioctl_trans_end(struct file *file)
4282{
Al Viro496ad9a2013-01-23 17:07:38 -05004283 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004284 struct btrfs_root *root = BTRFS_I(inode)->root;
4285 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004286
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004287 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004288 if (!trans)
4289 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004290 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004291
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004292 btrfs_end_transaction(trans);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004293
Josef Bacika4abeea2011-04-11 17:25:13 -04004294 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004295
Al Viro2a79f172011-12-09 08:06:57 -05004296 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004297 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004298}
4299
Miao Xie9a8c28b2012-11-26 08:40:43 +00004300static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4301 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004302{
Sage Weil46204592010-10-29 15:41:32 -04004303 struct btrfs_trans_handle *trans;
4304 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004305 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004306
Miao Xied4edf392013-02-20 09:17:06 +00004307 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004308 if (IS_ERR(trans)) {
4309 if (PTR_ERR(trans) != -ENOENT)
4310 return PTR_ERR(trans);
4311
4312 /* No running transaction, don't bother */
4313 transid = root->fs_info->last_trans_committed;
4314 goto out;
4315 }
Sage Weil46204592010-10-29 15:41:32 -04004316 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004317 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004318 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004319 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004320 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004321 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004322out:
Sage Weil46204592010-10-29 15:41:32 -04004323 if (argp)
4324 if (copy_to_user(argp, &transid, sizeof(transid)))
4325 return -EFAULT;
4326 return 0;
4327}
4328
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004329static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004330 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004331{
Sage Weil46204592010-10-29 15:41:32 -04004332 u64 transid;
4333
4334 if (argp) {
4335 if (copy_from_user(&transid, argp, sizeof(transid)))
4336 return -EFAULT;
4337 } else {
4338 transid = 0; /* current trans */
4339 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004340 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004341}
4342
Miao Xieb8e95482012-11-26 08:48:01 +00004343static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004344{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004345 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004346 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004347 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004348
4349 if (!capable(CAP_SYS_ADMIN))
4350 return -EPERM;
4351
4352 sa = memdup_user(arg, sizeof(*sa));
4353 if (IS_ERR(sa))
4354 return PTR_ERR(sa);
4355
Miao Xieb8e95482012-11-26 08:48:01 +00004356 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4357 ret = mnt_want_write_file(file);
4358 if (ret)
4359 goto out;
4360 }
4361
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004362 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004363 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4364 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004365
4366 if (copy_to_user(arg, sa, sizeof(*sa)))
4367 ret = -EFAULT;
4368
Miao Xieb8e95482012-11-26 08:48:01 +00004369 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4370 mnt_drop_write_file(file);
4371out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004372 kfree(sa);
4373 return ret;
4374}
4375
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004376static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004377{
4378 if (!capable(CAP_SYS_ADMIN))
4379 return -EPERM;
4380
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004381 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004382}
4383
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004384static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004385 void __user *arg)
4386{
4387 struct btrfs_ioctl_scrub_args *sa;
4388 int ret;
4389
4390 if (!capable(CAP_SYS_ADMIN))
4391 return -EPERM;
4392
4393 sa = memdup_user(arg, sizeof(*sa));
4394 if (IS_ERR(sa))
4395 return PTR_ERR(sa);
4396
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004397 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004398
4399 if (copy_to_user(arg, sa, sizeof(*sa)))
4400 ret = -EFAULT;
4401
4402 kfree(sa);
4403 return ret;
4404}
4405
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004406static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004407 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004408{
4409 struct btrfs_ioctl_get_dev_stats *sa;
4410 int ret;
4411
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004412 sa = memdup_user(arg, sizeof(*sa));
4413 if (IS_ERR(sa))
4414 return PTR_ERR(sa);
4415
David Sterbab27f7c02012-06-22 06:30:39 -06004416 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4417 kfree(sa);
4418 return -EPERM;
4419 }
4420
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004421 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004422
4423 if (copy_to_user(arg, sa, sizeof(*sa)))
4424 ret = -EFAULT;
4425
4426 kfree(sa);
4427 return ret;
4428}
4429
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004430static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4431 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004432{
4433 struct btrfs_ioctl_dev_replace_args *p;
4434 int ret;
4435
4436 if (!capable(CAP_SYS_ADMIN))
4437 return -EPERM;
4438
4439 p = memdup_user(arg, sizeof(*p));
4440 if (IS_ERR(p))
4441 return PTR_ERR(p);
4442
4443 switch (p->cmd) {
4444 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004445 if (fs_info->sb->s_flags & MS_RDONLY) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004446 ret = -EROFS;
4447 goto out;
4448 }
David Sterba171938e2017-03-28 14:44:21 +02004449 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004450 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004451 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004452 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004453 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004454 }
4455 break;
4456 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004457 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004458 ret = 0;
4459 break;
4460 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004461 ret = btrfs_dev_replace_cancel(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004462 break;
4463 default:
4464 ret = -EINVAL;
4465 break;
4466 }
4467
4468 if (copy_to_user(arg, p, sizeof(*p)))
4469 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004470out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004471 kfree(p);
4472 return ret;
4473}
4474
Jan Schmidtd7728c92011-07-07 16:48:38 +02004475static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4476{
4477 int ret = 0;
4478 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004479 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004480 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004481 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004482 struct inode_fs_paths *ipath = NULL;
4483 struct btrfs_path *path;
4484
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004485 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004486 return -EPERM;
4487
4488 path = btrfs_alloc_path();
4489 if (!path) {
4490 ret = -ENOMEM;
4491 goto out;
4492 }
4493
4494 ipa = memdup_user(arg, sizeof(*ipa));
4495 if (IS_ERR(ipa)) {
4496 ret = PTR_ERR(ipa);
4497 ipa = NULL;
4498 goto out;
4499 }
4500
4501 size = min_t(u32, ipa->size, 4096);
4502 ipath = init_ipath(size, root, path);
4503 if (IS_ERR(ipath)) {
4504 ret = PTR_ERR(ipath);
4505 ipath = NULL;
4506 goto out;
4507 }
4508
4509 ret = paths_from_inode(ipa->inum, ipath);
4510 if (ret < 0)
4511 goto out;
4512
4513 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004514 rel_ptr = ipath->fspath->val[i] -
4515 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004516 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004517 }
4518
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004519 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4520 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004521 if (ret) {
4522 ret = -EFAULT;
4523 goto out;
4524 }
4525
4526out:
4527 btrfs_free_path(path);
4528 free_ipath(ipath);
4529 kfree(ipa);
4530
4531 return ret;
4532}
4533
4534static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4535{
4536 struct btrfs_data_container *inodes = ctx;
4537 const size_t c = 3 * sizeof(u64);
4538
4539 if (inodes->bytes_left >= c) {
4540 inodes->bytes_left -= c;
4541 inodes->val[inodes->elem_cnt] = inum;
4542 inodes->val[inodes->elem_cnt + 1] = offset;
4543 inodes->val[inodes->elem_cnt + 2] = root;
4544 inodes->elem_cnt += 3;
4545 } else {
4546 inodes->bytes_missing += c - inodes->bytes_left;
4547 inodes->bytes_left = 0;
4548 inodes->elem_missed += 3;
4549 }
4550
4551 return 0;
4552}
4553
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004554static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Jan Schmidtd7728c92011-07-07 16:48:38 +02004555 void __user *arg)
4556{
4557 int ret = 0;
4558 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004559 struct btrfs_ioctl_logical_ino_args *loi;
4560 struct btrfs_data_container *inodes = NULL;
4561 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004562
4563 if (!capable(CAP_SYS_ADMIN))
4564 return -EPERM;
4565
4566 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304567 if (IS_ERR(loi))
4568 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004569
4570 path = btrfs_alloc_path();
4571 if (!path) {
4572 ret = -ENOMEM;
4573 goto out;
4574 }
4575
Byongho Leeee221842015-12-15 01:42:10 +09004576 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004577 inodes = init_data_container(size);
4578 if (IS_ERR(inodes)) {
4579 ret = PTR_ERR(inodes);
4580 inodes = NULL;
4581 goto out;
4582 }
4583
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004584 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Liu Bodf031f02012-09-07 20:01:29 -06004585 build_ino_list, inodes);
4586 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004587 ret = -ENOENT;
4588 if (ret < 0)
4589 goto out;
4590
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004591 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4592 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004593 if (ret)
4594 ret = -EFAULT;
4595
4596out:
4597 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004598 kvfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004599 kfree(loi);
4600
4601 return ret;
4602}
4603
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004604void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004605 struct btrfs_ioctl_balance_args *bargs)
4606{
4607 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4608
4609 bargs->flags = bctl->flags;
4610
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004611 if (atomic_read(&fs_info->balance_running))
4612 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4613 if (atomic_read(&fs_info->balance_pause_req))
4614 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004615 if (atomic_read(&fs_info->balance_cancel_req))
4616 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004617
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004618 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4619 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4620 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004621
4622 if (lock) {
4623 spin_lock(&fs_info->balance_lock);
4624 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4625 spin_unlock(&fs_info->balance_lock);
4626 } else {
4627 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4628 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004629}
4630
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004631static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004632{
Al Viro496ad9a2013-01-23 17:07:38 -05004633 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004634 struct btrfs_fs_info *fs_info = root->fs_info;
4635 struct btrfs_ioctl_balance_args *bargs;
4636 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004637 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004638 int ret;
4639
4640 if (!capable(CAP_SYS_ADMIN))
4641 return -EPERM;
4642
Liu Boe54bfa312012-06-29 03:58:48 -06004643 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004644 if (ret)
4645 return ret;
4646
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004647again:
David Sterba171938e2017-03-28 14:44:21 +02004648 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004649 mutex_lock(&fs_info->volume_mutex);
4650 mutex_lock(&fs_info->balance_mutex);
4651 need_unlock = true;
4652 goto locked;
4653 }
4654
4655 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004656 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004657 * (1) some other op is running
4658 * (2) balance is running
4659 * (3) balance is paused -- special case (think resume)
4660 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004661 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004662 if (fs_info->balance_ctl) {
4663 /* this is either (2) or (3) */
4664 if (!atomic_read(&fs_info->balance_running)) {
4665 mutex_unlock(&fs_info->balance_mutex);
4666 if (!mutex_trylock(&fs_info->volume_mutex))
4667 goto again;
4668 mutex_lock(&fs_info->balance_mutex);
4669
4670 if (fs_info->balance_ctl &&
4671 !atomic_read(&fs_info->balance_running)) {
4672 /* this is (3) */
4673 need_unlock = false;
4674 goto locked;
4675 }
4676
4677 mutex_unlock(&fs_info->balance_mutex);
4678 mutex_unlock(&fs_info->volume_mutex);
4679 goto again;
4680 } else {
4681 /* this is (2) */
4682 mutex_unlock(&fs_info->balance_mutex);
4683 ret = -EINPROGRESS;
4684 goto out;
4685 }
4686 } else {
4687 /* this is (1) */
4688 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004689 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004690 goto out;
4691 }
4692
4693locked:
David Sterba171938e2017-03-28 14:44:21 +02004694 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004695
4696 if (arg) {
4697 bargs = memdup_user(arg, sizeof(*bargs));
4698 if (IS_ERR(bargs)) {
4699 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004700 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004701 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004702
4703 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4704 if (!fs_info->balance_ctl) {
4705 ret = -ENOTCONN;
4706 goto out_bargs;
4707 }
4708
4709 bctl = fs_info->balance_ctl;
4710 spin_lock(&fs_info->balance_lock);
4711 bctl->flags |= BTRFS_BALANCE_RESUME;
4712 spin_unlock(&fs_info->balance_lock);
4713
4714 goto do_balance;
4715 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004716 } else {
4717 bargs = NULL;
4718 }
4719
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004720 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004721 ret = -EINPROGRESS;
4722 goto out_bargs;
4723 }
4724
David Sterba8d2db782015-11-04 15:38:29 +01004725 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004726 if (!bctl) {
4727 ret = -ENOMEM;
4728 goto out_bargs;
4729 }
4730
4731 bctl->fs_info = fs_info;
4732 if (arg) {
4733 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4734 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4735 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4736
4737 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004738 } else {
4739 /* balance everything - no filters */
4740 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004741 }
4742
David Sterba8eb93452015-10-12 16:55:54 +02004743 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4744 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004745 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004746 }
4747
Ilya Dryomovde322262012-01-16 22:04:49 +02004748do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004749 /*
David Sterba171938e2017-03-28 14:44:21 +02004750 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004751 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4752 * or, if restriper was paused all the way until unmount, in
David Sterba171938e2017-03-28 14:44:21 +02004753 * free_fs_info. The flag is cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004754 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004755 need_unlock = false;
4756
4757 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004758 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004759
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004760 if (arg) {
4761 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4762 ret = -EFAULT;
4763 }
4764
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004765out_bctl:
4766 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004767out_bargs:
4768 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004769out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004770 mutex_unlock(&fs_info->balance_mutex);
4771 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004772 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004773 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004774out:
Liu Boe54bfa312012-06-29 03:58:48 -06004775 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004776 return ret;
4777}
4778
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004779static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004780{
4781 if (!capable(CAP_SYS_ADMIN))
4782 return -EPERM;
4783
4784 switch (cmd) {
4785 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004786 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004787 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004788 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004789 }
4790
4791 return -EINVAL;
4792}
4793
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004794static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004795 void __user *arg)
4796{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004797 struct btrfs_ioctl_balance_args *bargs;
4798 int ret = 0;
4799
4800 if (!capable(CAP_SYS_ADMIN))
4801 return -EPERM;
4802
4803 mutex_lock(&fs_info->balance_mutex);
4804 if (!fs_info->balance_ctl) {
4805 ret = -ENOTCONN;
4806 goto out;
4807 }
4808
David Sterba8d2db782015-11-04 15:38:29 +01004809 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004810 if (!bargs) {
4811 ret = -ENOMEM;
4812 goto out;
4813 }
4814
4815 update_ioctl_balance_args(fs_info, 1, bargs);
4816
4817 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4818 ret = -EFAULT;
4819
4820 kfree(bargs);
4821out:
4822 mutex_unlock(&fs_info->balance_mutex);
4823 return ret;
4824}
4825
Miao Xie905b0dd2012-11-26 08:50:11 +00004826static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004827{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004828 struct inode *inode = file_inode(file);
4829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004830 struct btrfs_ioctl_quota_ctl_args *sa;
4831 struct btrfs_trans_handle *trans = NULL;
4832 int ret;
4833 int err;
4834
4835 if (!capable(CAP_SYS_ADMIN))
4836 return -EPERM;
4837
Miao Xie905b0dd2012-11-26 08:50:11 +00004838 ret = mnt_want_write_file(file);
4839 if (ret)
4840 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004841
4842 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004843 if (IS_ERR(sa)) {
4844 ret = PTR_ERR(sa);
4845 goto drop_write;
4846 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004847
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004848 down_write(&fs_info->subvol_sem);
4849 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004850 if (IS_ERR(trans)) {
4851 ret = PTR_ERR(trans);
4852 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004853 }
4854
4855 switch (sa->cmd) {
4856 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004858 break;
4859 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004860 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004861 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004862 default:
4863 ret = -EINVAL;
4864 break;
4865 }
4866
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004867 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004868 if (err && !ret)
4869 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004870out:
4871 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004872 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004873drop_write:
4874 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004875 return ret;
4876}
4877
Miao Xie905b0dd2012-11-26 08:50:11 +00004878static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004879{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004880 struct inode *inode = file_inode(file);
4881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4882 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004883 struct btrfs_ioctl_qgroup_assign_args *sa;
4884 struct btrfs_trans_handle *trans;
4885 int ret;
4886 int err;
4887
4888 if (!capable(CAP_SYS_ADMIN))
4889 return -EPERM;
4890
Miao Xie905b0dd2012-11-26 08:50:11 +00004891 ret = mnt_want_write_file(file);
4892 if (ret)
4893 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004894
4895 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004896 if (IS_ERR(sa)) {
4897 ret = PTR_ERR(sa);
4898 goto drop_write;
4899 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004900
4901 trans = btrfs_join_transaction(root);
4902 if (IS_ERR(trans)) {
4903 ret = PTR_ERR(trans);
4904 goto out;
4905 }
4906
Arne Jansen5d13a372011-09-14 15:53:51 +02004907 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004908 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004909 sa->src, sa->dst);
4910 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004911 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004912 sa->src, sa->dst);
4913 }
4914
Qu Wenruoe082f562015-02-27 16:24:28 +08004915 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004916 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004917 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004918 btrfs_handle_fs_error(fs_info, err,
4919 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004920 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004921 if (err && !ret)
4922 ret = err;
4923
4924out:
4925 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004926drop_write:
4927 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004928 return ret;
4929}
4930
Miao Xie905b0dd2012-11-26 08:50:11 +00004931static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004932{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 struct inode *inode = file_inode(file);
4934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4935 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004936 struct btrfs_ioctl_qgroup_create_args *sa;
4937 struct btrfs_trans_handle *trans;
4938 int ret;
4939 int err;
4940
4941 if (!capable(CAP_SYS_ADMIN))
4942 return -EPERM;
4943
Miao Xie905b0dd2012-11-26 08:50:11 +00004944 ret = mnt_want_write_file(file);
4945 if (ret)
4946 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004947
4948 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004949 if (IS_ERR(sa)) {
4950 ret = PTR_ERR(sa);
4951 goto drop_write;
4952 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004953
Miao Xied86e56c2012-11-15 11:35:41 +00004954 if (!sa->qgroupid) {
4955 ret = -EINVAL;
4956 goto out;
4957 }
4958
Arne Jansen5d13a372011-09-14 15:53:51 +02004959 trans = btrfs_join_transaction(root);
4960 if (IS_ERR(trans)) {
4961 ret = PTR_ERR(trans);
4962 goto out;
4963 }
4964
Arne Jansen5d13a372011-09-14 15:53:51 +02004965 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004966 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004967 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004969 }
4970
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004971 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004972 if (err && !ret)
4973 ret = err;
4974
4975out:
4976 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004977drop_write:
4978 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004979 return ret;
4980}
4981
Miao Xie905b0dd2012-11-26 08:50:11 +00004982static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004983{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004984 struct inode *inode = file_inode(file);
4985 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4986 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004987 struct btrfs_ioctl_qgroup_limit_args *sa;
4988 struct btrfs_trans_handle *trans;
4989 int ret;
4990 int err;
4991 u64 qgroupid;
4992
4993 if (!capable(CAP_SYS_ADMIN))
4994 return -EPERM;
4995
Miao Xie905b0dd2012-11-26 08:50:11 +00004996 ret = mnt_want_write_file(file);
4997 if (ret)
4998 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004999
5000 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005001 if (IS_ERR(sa)) {
5002 ret = PTR_ERR(sa);
5003 goto drop_write;
5004 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005005
5006 trans = btrfs_join_transaction(root);
5007 if (IS_ERR(trans)) {
5008 ret = PTR_ERR(trans);
5009 goto out;
5010 }
5011
5012 qgroupid = sa->qgroupid;
5013 if (!qgroupid) {
5014 /* take the current subvol as qgroup */
5015 qgroupid = root->root_key.objectid;
5016 }
5017
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005018 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005019
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005020 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005021 if (err && !ret)
5022 ret = err;
5023
5024out:
5025 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005026drop_write:
5027 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005028 return ret;
5029}
5030
Jan Schmidt2f232032013-04-25 16:04:51 +00005031static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5032{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005033 struct inode *inode = file_inode(file);
5034 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005035 struct btrfs_ioctl_quota_rescan_args *qsa;
5036 int ret;
5037
5038 if (!capable(CAP_SYS_ADMIN))
5039 return -EPERM;
5040
5041 ret = mnt_want_write_file(file);
5042 if (ret)
5043 return ret;
5044
5045 qsa = memdup_user(arg, sizeof(*qsa));
5046 if (IS_ERR(qsa)) {
5047 ret = PTR_ERR(qsa);
5048 goto drop_write;
5049 }
5050
5051 if (qsa->flags) {
5052 ret = -EINVAL;
5053 goto out;
5054 }
5055
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005056 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005057
5058out:
5059 kfree(qsa);
5060drop_write:
5061 mnt_drop_write_file(file);
5062 return ret;
5063}
5064
5065static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5066{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005067 struct inode *inode = file_inode(file);
5068 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005069 struct btrfs_ioctl_quota_rescan_args *qsa;
5070 int ret = 0;
5071
5072 if (!capable(CAP_SYS_ADMIN))
5073 return -EPERM;
5074
David Sterba8d2db782015-11-04 15:38:29 +01005075 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005076 if (!qsa)
5077 return -ENOMEM;
5078
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005079 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005080 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005081 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005082 }
5083
5084 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5085 ret = -EFAULT;
5086
5087 kfree(qsa);
5088 return ret;
5089}
5090
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005091static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5092{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005093 struct inode *inode = file_inode(file);
5094 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005095
5096 if (!capable(CAP_SYS_ADMIN))
5097 return -EPERM;
5098
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005099 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005100}
5101
Hugo Millsabccd002014-01-30 20:17:00 +00005102static long _btrfs_ioctl_set_received_subvol(struct file *file,
5103 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005104{
Al Viro496ad9a2013-01-23 17:07:38 -05005105 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005107 struct btrfs_root *root = BTRFS_I(inode)->root;
5108 struct btrfs_root_item *root_item = &root->root_item;
5109 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005110 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005111 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005112 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005113
David Sterbabd60ea02014-01-16 15:50:22 +01005114 if (!inode_owner_or_capable(inode))
5115 return -EPERM;
5116
Alexander Block8ea05e32012-07-25 17:35:53 +02005117 ret = mnt_want_write_file(file);
5118 if (ret < 0)
5119 return ret;
5120
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005121 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005122
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005123 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005124 ret = -EINVAL;
5125 goto out;
5126 }
5127
5128 if (btrfs_root_readonly(root)) {
5129 ret = -EROFS;
5130 goto out;
5131 }
5132
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005133 /*
5134 * 1 - root item
5135 * 2 - uuid items (received uuid + subvol uuid)
5136 */
5137 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005138 if (IS_ERR(trans)) {
5139 ret = PTR_ERR(trans);
5140 trans = NULL;
5141 goto out;
5142 }
5143
5144 sa->rtransid = trans->transid;
5145 sa->rtime.sec = ct.tv_sec;
5146 sa->rtime.nsec = ct.tv_nsec;
5147
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005148 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5149 BTRFS_UUID_SIZE);
5150 if (received_uuid_changed &&
5151 !btrfs_is_empty_uuid(root_item->received_uuid))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005152 btrfs_uuid_tree_rem(trans, fs_info, root_item->received_uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005153 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5154 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005155 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5156 btrfs_set_root_stransid(root_item, sa->stransid);
5157 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005158 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5159 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5160 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5161 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005162
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005163 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005164 &root->root_key, &root->root_item);
5165 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005166 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005167 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005168 }
5169 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005170 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005171 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5172 root->root_key.objectid);
5173 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005174 btrfs_abort_transaction(trans, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005175 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005176 }
5177 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005178 ret = btrfs_commit_transaction(trans);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005179 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005180 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005181 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005182 }
5183
Hugo Millsabccd002014-01-30 20:17:00 +00005184out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005185 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005186 mnt_drop_write_file(file);
5187 return ret;
5188}
5189
5190#ifdef CONFIG_64BIT
5191static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5192 void __user *arg)
5193{
5194 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5195 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5196 int ret = 0;
5197
5198 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305199 if (IS_ERR(args32))
5200 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005201
David Sterba8d2db782015-11-04 15:38:29 +01005202 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005203 if (!args64) {
5204 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005205 goto out;
5206 }
5207
5208 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5209 args64->stransid = args32->stransid;
5210 args64->rtransid = args32->rtransid;
5211 args64->stime.sec = args32->stime.sec;
5212 args64->stime.nsec = args32->stime.nsec;
5213 args64->rtime.sec = args32->rtime.sec;
5214 args64->rtime.nsec = args32->rtime.nsec;
5215 args64->flags = args32->flags;
5216
5217 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5218 if (ret)
5219 goto out;
5220
5221 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5222 args32->stransid = args64->stransid;
5223 args32->rtransid = args64->rtransid;
5224 args32->stime.sec = args64->stime.sec;
5225 args32->stime.nsec = args64->stime.nsec;
5226 args32->rtime.sec = args64->rtime.sec;
5227 args32->rtime.nsec = args64->rtime.nsec;
5228 args32->flags = args64->flags;
5229
5230 ret = copy_to_user(arg, args32, sizeof(*args32));
5231 if (ret)
5232 ret = -EFAULT;
5233
5234out:
5235 kfree(args32);
5236 kfree(args64);
5237 return ret;
5238}
5239#endif
5240
5241static long btrfs_ioctl_set_received_subvol(struct file *file,
5242 void __user *arg)
5243{
5244 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5245 int ret = 0;
5246
5247 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305248 if (IS_ERR(sa))
5249 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005250
5251 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5252
5253 if (ret)
5254 goto out;
5255
Alexander Block8ea05e32012-07-25 17:35:53 +02005256 ret = copy_to_user(arg, sa, sizeof(*sa));
5257 if (ret)
5258 ret = -EFAULT;
5259
5260out:
5261 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005262 return ret;
5263}
5264
jeff.liu867ab662013-01-05 02:48:01 +00005265static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5266{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005267 struct inode *inode = file_inode(file);
5268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005269 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005270 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005271 char label[BTRFS_LABEL_SIZE];
5272
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005273 spin_lock(&fs_info->super_lock);
5274 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5275 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005276
5277 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005278
5279 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 btrfs_warn(fs_info,
5281 "label is too long, return the first %zu bytes",
5282 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005283 }
5284
jeff.liu867ab662013-01-05 02:48:01 +00005285 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005286
5287 return ret ? -EFAULT : 0;
5288}
5289
jeff.liua8bfd4a2013-01-05 02:48:08 +00005290static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5291{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005292 struct inode *inode = file_inode(file);
5293 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5294 struct btrfs_root *root = BTRFS_I(inode)->root;
5295 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005296 struct btrfs_trans_handle *trans;
5297 char label[BTRFS_LABEL_SIZE];
5298 int ret;
5299
5300 if (!capable(CAP_SYS_ADMIN))
5301 return -EPERM;
5302
5303 if (copy_from_user(label, arg, sizeof(label)))
5304 return -EFAULT;
5305
5306 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005307 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005308 "unable to set label with more than %d bytes",
5309 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005310 return -EINVAL;
5311 }
5312
5313 ret = mnt_want_write_file(file);
5314 if (ret)
5315 return ret;
5316
jeff.liua8bfd4a2013-01-05 02:48:08 +00005317 trans = btrfs_start_transaction(root, 0);
5318 if (IS_ERR(trans)) {
5319 ret = PTR_ERR(trans);
5320 goto out_unlock;
5321 }
5322
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005323 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005324 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005325 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005326 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005327
5328out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005329 mnt_drop_write_file(file);
5330 return ret;
5331}
5332
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005333#define INIT_FEATURE_FLAGS(suffix) \
5334 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5335 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5336 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5337
David Sterbad5131b62016-02-17 15:26:27 +01005338int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005339{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005340 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005341 INIT_FEATURE_FLAGS(SUPP),
5342 INIT_FEATURE_FLAGS(SAFE_SET),
5343 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5344 };
5345
5346 if (copy_to_user(arg, &features, sizeof(features)))
5347 return -EFAULT;
5348
5349 return 0;
5350}
5351
5352static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005354 struct inode *inode = file_inode(file);
5355 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5356 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005357 struct btrfs_ioctl_feature_flags features;
5358
5359 features.compat_flags = btrfs_super_compat_flags(super_block);
5360 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5361 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5362
5363 if (copy_to_user(arg, &features, sizeof(features)))
5364 return -EFAULT;
5365
5366 return 0;
5367}
5368
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005369static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005370 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005371 u64 change_mask, u64 flags, u64 supported_flags,
5372 u64 safe_set, u64 safe_clear)
5373{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005374 const char *type = btrfs_feature_set_names[set];
5375 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005376 u64 disallowed, unsupported;
5377 u64 set_mask = flags & change_mask;
5378 u64 clear_mask = ~flags & change_mask;
5379
5380 unsupported = set_mask & ~supported_flags;
5381 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005382 names = btrfs_printable_features(set, unsupported);
5383 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005384 btrfs_warn(fs_info,
5385 "this kernel does not support the %s feature bit%s",
5386 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005387 kfree(names);
5388 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005389 btrfs_warn(fs_info,
5390 "this kernel does not support %s bits 0x%llx",
5391 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005392 return -EOPNOTSUPP;
5393 }
5394
5395 disallowed = set_mask & ~safe_set;
5396 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005397 names = btrfs_printable_features(set, disallowed);
5398 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005399 btrfs_warn(fs_info,
5400 "can't set the %s feature bit%s while mounted",
5401 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005402 kfree(names);
5403 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005404 btrfs_warn(fs_info,
5405 "can't set %s bits 0x%llx while mounted",
5406 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005407 return -EPERM;
5408 }
5409
5410 disallowed = clear_mask & ~safe_clear;
5411 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005412 names = btrfs_printable_features(set, disallowed);
5413 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005414 btrfs_warn(fs_info,
5415 "can't clear the %s feature bit%s while mounted",
5416 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005417 kfree(names);
5418 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005419 btrfs_warn(fs_info,
5420 "can't clear %s bits 0x%llx while mounted",
5421 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005422 return -EPERM;
5423 }
5424
5425 return 0;
5426}
5427
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005428#define check_feature(fs_info, change_mask, flags, mask_base) \
5429check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005430 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5431 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5432 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5433
5434static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5435{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005436 struct inode *inode = file_inode(file);
5437 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5438 struct btrfs_root *root = BTRFS_I(inode)->root;
5439 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005440 struct btrfs_ioctl_feature_flags flags[2];
5441 struct btrfs_trans_handle *trans;
5442 u64 newflags;
5443 int ret;
5444
5445 if (!capable(CAP_SYS_ADMIN))
5446 return -EPERM;
5447
5448 if (copy_from_user(flags, arg, sizeof(flags)))
5449 return -EFAULT;
5450
5451 /* Nothing to do */
5452 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5453 !flags[0].incompat_flags)
5454 return 0;
5455
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005456 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005457 flags[1].compat_flags, COMPAT);
5458 if (ret)
5459 return ret;
5460
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005461 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005462 flags[1].compat_ro_flags, COMPAT_RO);
5463 if (ret)
5464 return ret;
5465
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005466 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005467 flags[1].incompat_flags, INCOMPAT);
5468 if (ret)
5469 return ret;
5470
David Sterba7ab19622016-05-04 11:32:00 +02005471 ret = mnt_want_write_file(file);
5472 if (ret)
5473 return ret;
5474
David Sterba8051aa12014-02-07 14:34:04 +01005475 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005476 if (IS_ERR(trans)) {
5477 ret = PTR_ERR(trans);
5478 goto out_drop_write;
5479 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005480
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005481 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005482 newflags = btrfs_super_compat_flags(super_block);
5483 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5484 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5485 btrfs_set_super_compat_flags(super_block, newflags);
5486
5487 newflags = btrfs_super_compat_ro_flags(super_block);
5488 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5489 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5490 btrfs_set_super_compat_ro_flags(super_block, newflags);
5491
5492 newflags = btrfs_super_incompat_flags(super_block);
5493 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5494 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5495 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005496 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005497
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005498 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005499out_drop_write:
5500 mnt_drop_write_file(file);
5501
5502 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005503}
5504
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005505long btrfs_ioctl(struct file *file, unsigned int
5506 cmd, unsigned long arg)
5507{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005508 struct inode *inode = file_inode(file);
5509 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5510 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005511 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005512
5513 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005514 case FS_IOC_GETFLAGS:
5515 return btrfs_ioctl_getflags(file, argp);
5516 case FS_IOC_SETFLAGS:
5517 return btrfs_ioctl_setflags(file, argp);
5518 case FS_IOC_GETVERSION:
5519 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005520 case FITRIM:
5521 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005522 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005523 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005524 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005525 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005526 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005527 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005528 case BTRFS_IOC_SUBVOL_CREATE_V2:
5529 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 case BTRFS_IOC_SNAP_DESTROY:
5531 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005532 case BTRFS_IOC_SUBVOL_GETFLAGS:
5533 return btrfs_ioctl_subvol_getflags(file, argp);
5534 case BTRFS_IOC_SUBVOL_SETFLAGS:
5535 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005536 case BTRFS_IOC_DEFAULT_SUBVOL:
5537 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005538 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005539 return btrfs_ioctl_defrag(file, NULL);
5540 case BTRFS_IOC_DEFRAG_RANGE:
5541 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005542 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005543 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005544 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005545 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005546 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005547 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005548 case BTRFS_IOC_RM_DEV_V2:
5549 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005550 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005551 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005552 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005553 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005554 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005555 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005556 case BTRFS_IOC_TRANS_START:
5557 return btrfs_ioctl_trans_start(file);
5558 case BTRFS_IOC_TRANS_END:
5559 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005560 case BTRFS_IOC_TREE_SEARCH:
5561 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005562 case BTRFS_IOC_TREE_SEARCH_V2:
5563 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005564 case BTRFS_IOC_INO_LOOKUP:
5565 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005566 case BTRFS_IOC_INO_PATHS:
5567 return btrfs_ioctl_ino_to_path(root, argp);
5568 case BTRFS_IOC_LOGICAL_INO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005569 return btrfs_ioctl_logical_to_ino(fs_info, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005570 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005571 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005572 case BTRFS_IOC_SYNC: {
5573 int ret;
5574
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005575 ret = btrfs_start_delalloc_roots(fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005576 if (ret)
5577 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005578 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005579 /*
5580 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005581 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005582 * processing uncleaned subvols.
5583 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005584 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005585 return ret;
5586 }
Sage Weil46204592010-10-29 15:41:32 -04005587 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005588 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005589 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005590 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005591 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005592 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005593 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005594 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005595 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005596 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005597 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005598 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005599 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005600 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005601 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005602 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005603 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5604 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005605#ifdef CONFIG_64BIT
5606 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5607 return btrfs_ioctl_set_received_subvol_32(file, argp);
5608#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005609 case BTRFS_IOC_SEND:
5610 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005611 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005612 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005613 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005614 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005615 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005616 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005617 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005618 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005619 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005620 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005621 case BTRFS_IOC_QUOTA_RESCAN:
5622 return btrfs_ioctl_quota_rescan(file, argp);
5623 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5624 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005625 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5626 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005627 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005628 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005629 case BTRFS_IOC_GET_FSLABEL:
5630 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005631 case BTRFS_IOC_SET_FSLABEL:
5632 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005633 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005634 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005635 case BTRFS_IOC_GET_FEATURES:
5636 return btrfs_ioctl_get_features(file, argp);
5637 case BTRFS_IOC_SET_FEATURES:
5638 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005639 }
5640
5641 return -ENOTTY;
5642}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005643
5644#ifdef CONFIG_COMPAT
5645long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5646{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005647 /*
5648 * These all access 32-bit values anyway so no further
5649 * handling is necessary.
5650 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005651 switch (cmd) {
5652 case FS_IOC32_GETFLAGS:
5653 cmd = FS_IOC_GETFLAGS;
5654 break;
5655 case FS_IOC32_SETFLAGS:
5656 cmd = FS_IOC_SETFLAGS;
5657 break;
5658 case FS_IOC32_GETVERSION:
5659 cmd = FS_IOC_GETVERSION;
5660 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005661 }
5662
5663 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5664}
5665#endif