blob: 4d1b07b815495a71b64fdd9b854950ae2636bfa0 [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
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200159static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
160{
Al Viro496ad9a2013-01-23 17:07:38 -0500161 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200162 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
163
164 if (copy_to_user(arg, &flags, sizeof(flags)))
165 return -EFAULT;
166 return 0;
167}
168
Liu Bo75e7cb72011-03-22 10:12:20 +0000169static int check_flags(unsigned int flags)
170{
171 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
172 FS_NOATIME_FL | FS_NODUMP_FL | \
173 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000174 FS_NOCOMP_FL | FS_COMPR_FL |
175 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000176 return -EOPNOTSUPP;
177
178 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
179 return -EINVAL;
180
Liu Bo75e7cb72011-03-22 10:12:20 +0000181 return 0;
182}
183
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200184static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
185{
Al Viro496ad9a2013-01-23 17:07:38 -0500186 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400187 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200188 struct btrfs_inode *ip = BTRFS_I(inode);
189 struct btrfs_root *root = ip->root;
190 struct btrfs_trans_handle *trans;
191 unsigned int flags, oldflags;
192 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800193 u64 ip_oldflags;
194 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600195 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200196
David Sterbabd60ea02014-01-16 15:50:22 +0100197 if (!inode_owner_or_capable(inode))
198 return -EPERM;
199
Li Zefanb83cc962010-12-20 16:04:08 +0800200 if (btrfs_root_readonly(root))
201 return -EROFS;
202
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203 if (copy_from_user(&flags, arg, sizeof(flags)))
204 return -EFAULT;
205
Liu Bo75e7cb72011-03-22 10:12:20 +0000206 ret = check_flags(flags);
207 if (ret)
208 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200209
Jan Karae7848682012-06-12 16:20:32 +0200210 ret = mnt_want_write_file(file);
211 if (ret)
212 return ret;
213
Al Viro59551022016-01-22 15:40:57 -0500214 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200215
Li Zefanf062abf02011-12-29 13:36:45 +0800216 ip_oldflags = ip->flags;
217 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600218 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800219
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200220 flags = btrfs_mask_flags(inode->i_mode, flags);
221 oldflags = btrfs_flags_to_ioctl(ip->flags);
222 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
223 if (!capable(CAP_LINUX_IMMUTABLE)) {
224 ret = -EPERM;
225 goto out_unlock;
226 }
227 }
228
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200229 if (flags & FS_SYNC_FL)
230 ip->flags |= BTRFS_INODE_SYNC;
231 else
232 ip->flags &= ~BTRFS_INODE_SYNC;
233 if (flags & FS_IMMUTABLE_FL)
234 ip->flags |= BTRFS_INODE_IMMUTABLE;
235 else
236 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
237 if (flags & FS_APPEND_FL)
238 ip->flags |= BTRFS_INODE_APPEND;
239 else
240 ip->flags &= ~BTRFS_INODE_APPEND;
241 if (flags & FS_NODUMP_FL)
242 ip->flags |= BTRFS_INODE_NODUMP;
243 else
244 ip->flags &= ~BTRFS_INODE_NODUMP;
245 if (flags & FS_NOATIME_FL)
246 ip->flags |= BTRFS_INODE_NOATIME;
247 else
248 ip->flags &= ~BTRFS_INODE_NOATIME;
249 if (flags & FS_DIRSYNC_FL)
250 ip->flags |= BTRFS_INODE_DIRSYNC;
251 else
252 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600253 if (flags & FS_NOCOW_FL) {
254 if (S_ISREG(mode)) {
255 /*
256 * It's safe to turn csums off here, no extents exist.
257 * Otherwise we want the flag to reflect the real COW
258 * status of the file and will not set it.
259 */
260 if (inode->i_size == 0)
261 ip->flags |= BTRFS_INODE_NODATACOW
262 | BTRFS_INODE_NODATASUM;
263 } else {
264 ip->flags |= BTRFS_INODE_NODATACOW;
265 }
266 } else {
267 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400268 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600269 */
270 if (S_ISREG(mode)) {
271 if (inode->i_size == 0)
272 ip->flags &= ~(BTRFS_INODE_NODATACOW
273 | BTRFS_INODE_NODATASUM);
274 } else {
275 ip->flags &= ~BTRFS_INODE_NODATACOW;
276 }
277 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200278
Liu Bo75e7cb72011-03-22 10:12:20 +0000279 /*
280 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
281 * flag may be changed automatically if compression code won't make
282 * things smaller.
283 */
284 if (flags & FS_NOCOMP_FL) {
285 ip->flags &= ~BTRFS_INODE_COMPRESS;
286 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000287
288 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
289 if (ret && ret != -ENODATA)
290 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000291 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000292 const char *comp;
293
Liu Bo75e7cb72011-03-22 10:12:20 +0000294 ip->flags |= BTRFS_INODE_COMPRESS;
295 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000296
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400297 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000298 comp = "lzo";
299 else
300 comp = "zlib";
301 ret = btrfs_set_prop(inode, "btrfs.compression",
302 comp, strlen(comp), 0);
303 if (ret)
304 goto out_drop;
305
Li Zefanebcb9042011-04-15 03:03:17 +0000306 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100307 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
308 if (ret && ret != -ENODATA)
309 goto out_drop;
Li Zefanebcb9042011-04-15 03:03:17 +0000310 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000311 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200312
Li Zefan4da6f1a2011-12-29 13:39:50 +0800313 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800314 if (IS_ERR(trans)) {
315 ret = PTR_ERR(trans);
316 goto out_drop;
317 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200318
Li Zefan306424cc2011-12-14 20:12:02 -0500319 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400320 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700321 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200323
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400324 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800325 out_drop:
326 if (ret) {
327 ip->flags = ip_oldflags;
328 inode->i_flags = i_oldflags;
329 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200330
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200331 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500332 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200333 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000334 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335}
336
337static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
338{
Al Viro496ad9a2013-01-23 17:07:38 -0500339 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200340
341 return put_user(inode->i_generation, arg);
342}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400343
Li Dongyangf7039b12011-03-24 10:24:28 +0000344static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
345{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400346 struct inode *inode = file_inode(file);
347 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000348 struct btrfs_device *device;
349 struct request_queue *q;
350 struct fstrim_range range;
351 u64 minlen = ULLONG_MAX;
352 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500353 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000354 int ret;
355
356 if (!capable(CAP_SYS_ADMIN))
357 return -EPERM;
358
Xiao Guangrong1f781602011-04-20 10:09:16 +0000359 rcu_read_lock();
360 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
361 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000362 if (!device->bdev)
363 continue;
364 q = bdev_get_queue(device->bdev);
365 if (blk_queue_discard(q)) {
366 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100367 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000368 minlen);
369 }
370 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000371 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200372
Li Dongyangf7039b12011-03-24 10:24:28 +0000373 if (!num_devices)
374 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000375 if (copy_from_user(&range, arg, sizeof(range)))
376 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000377 if (range.start > total_bytes ||
378 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200379 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000380
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200381 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000382 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400383 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000384 if (ret < 0)
385 return ret;
386
387 if (copy_to_user(arg, &range, sizeof(range)))
388 return -EFAULT;
389
390 return 0;
391}
392
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200393int btrfs_is_empty_uuid(u8 *uuid)
394{
Chris Mason46e0f662013-11-15 12:14:55 +0100395 int i;
396
397 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
398 if (uuid[i])
399 return 0;
400 }
401 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200402}
403
Miao Xied5c12072013-02-28 10:04:33 +0000404static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400405 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100406 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200407 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000408 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400410 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400411 struct btrfs_trans_handle *trans;
412 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100413 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414 struct btrfs_inode_item *inode_item;
415 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000416 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400417 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000418 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700419 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900420 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421 int ret;
422 int err;
423 u64 objectid;
424 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500425 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000426 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200427 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400428
David Sterba49a3c4d2016-03-24 17:49:22 +0100429 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
430 if (!root_item)
431 return -ENOMEM;
432
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400433 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400434 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100435 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000436
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800437 /*
438 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400439 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800440 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100441 if (btrfs_qgroup_level(objectid)) {
442 ret = -ENOSPC;
443 goto fail_free;
444 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800445
Miao Xied5c12072013-02-28 10:04:33 +0000446 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400447 /*
Miao Xied5c12072013-02-28 10:04:33 +0000448 * The same as the snapshot creation, please see the comment
449 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400450 */
Miao Xied5c12072013-02-28 10:04:33 +0000451 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200452 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000453 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100454 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400455
Miao Xied5c12072013-02-28 10:04:33 +0000456 trans = btrfs_start_transaction(root, 0);
457 if (IS_ERR(trans)) {
458 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100459 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100460 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000461 }
462 trans->block_rsv = &block_rsv;
463 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400465 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200466 if (ret)
467 goto fail;
468
David Sterba4d75f8a2014-06-15 01:54:12 +0200469 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400470 if (IS_ERR(leaf)) {
471 ret = PTR_ERR(leaf);
472 goto fail;
473 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400474
David Sterbab159fa22016-11-08 18:09:03 +0100475 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400476 btrfs_set_header_bytenr(leaf, leaf->start);
477 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400478 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400479 btrfs_set_header_owner(leaf, objectid);
480
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400481 write_extent_buffer_fsid(leaf, fs_info->fsid);
482 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400483 btrfs_mark_buffer_dirty(leaf);
484
David Sterba49a3c4d2016-03-24 17:49:22 +0100485 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800486 btrfs_set_stack_inode_generation(inode_item, 1);
487 btrfs_set_stack_inode_size(inode_item, 3);
488 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400489 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400490 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800491 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400492
David Sterba49a3c4d2016-03-24 17:49:22 +0100493 btrfs_set_root_flags(root_item, 0);
494 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800495 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000496
David Sterba49a3c4d2016-03-24 17:49:22 +0100497 btrfs_set_root_bytenr(root_item, leaf->start);
498 btrfs_set_root_generation(root_item, trans->transid);
499 btrfs_set_root_level(root_item, 0);
500 btrfs_set_root_refs(root_item, 1);
501 btrfs_set_root_used(root_item, leaf->len);
502 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400503
David Sterba49a3c4d2016-03-24 17:49:22 +0100504 btrfs_set_root_generation_v2(root_item,
505 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200506 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100507 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
508 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
509 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
510 root_item->ctime = root_item->otime;
511 btrfs_set_root_ctransid(root_item, trans->transid);
512 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400513
Chris Mason925baed2008-06-25 16:01:30 -0400514 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515 free_extent_buffer(leaf);
516 leaf = NULL;
517
David Sterba49a3c4d2016-03-24 17:49:22 +0100518 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519
520 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400521 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200522 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400523 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100524 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525 if (ret)
526 goto fail;
527
Yan, Zheng76dda932009-09-21 16:00:26 -0400528 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400529 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100530 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100531 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400532 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100533 goto fail;
534 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400535
536 btrfs_record_root_in_trans(trans, new_root);
537
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000538 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700539 if (ret) {
540 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400541 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700542 goto fail;
543 }
544
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530545 mutex_lock(&new_root->objectid_mutex);
546 new_root->highest_objectid = new_dirid;
547 mutex_unlock(&new_root->objectid_mutex);
548
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400549 /*
550 * insert the directory item
551 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200552 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100553 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400554 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100555 goto fail;
556 }
Chris Mason3de45862008-11-17 21:02:50 -0500557
558 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200559 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500560 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100561 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400562 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400563 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100564 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500565
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200566 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500567 ret = btrfs_update_inode(trans, root, dir);
568 BUG_ON(ret);
569
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400570 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400571 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200572 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500573 BUG_ON(ret);
574
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400575 ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400576 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200577 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400578 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200579
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400580fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100581 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000582 trans->block_rsv = NULL;
583 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100584 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800585
Sage Weil72fd0322010-10-29 15:41:32 -0400586 if (async_transid) {
587 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400588 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000589 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400590 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400591 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400592 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400593 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594 if (err && !ret)
595 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500596
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900597 if (!ret) {
598 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800599 if (IS_ERR(inode))
600 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900601 d_instantiate(dentry, inode);
602 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400603 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100604
605fail_free:
606 kfree(root_item);
607 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608}
609
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000610static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +0800611{
612 s64 writers;
613 DEFINE_WAIT(wait);
614
615 do {
616 prepare_to_wait(&root->subv_writers->wait, &wait,
617 TASK_UNINTERRUPTIBLE);
618
619 writers = percpu_counter_sum(&root->subv_writers->counter);
620 if (writers)
621 schedule();
622
623 finish_wait(&root->subv_writers->wait, &wait);
624 } while (writers);
625}
626
Miao Xiee9662f72013-02-28 10:01:15 +0000627static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100628 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000629 u64 *async_transid, bool readonly,
630 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400631{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400632 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000633 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634 struct btrfs_pending_snapshot *pending_snapshot;
635 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000636 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400637
Miao Xie27cdeb72014-04-02 19:51:05 +0800638 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400639 return -EINVAL;
640
David Sterba23269bf2017-02-13 11:03:44 +0100641 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100642 if (!pending_snapshot)
643 return -ENOMEM;
644
David Sterbab0c0ea62015-11-10 18:54:00 +0100645 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100646 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100647 pending_snapshot->path = btrfs_alloc_path();
648 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100649 ret = -ENOMEM;
650 goto free_pending;
651 }
652
Miao Xie8257b2d2014-03-06 13:38:19 +0800653 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100654 smp_mb__after_atomic();
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000655 btrfs_wait_for_no_snapshoting_writes(root);
Miao Xie8257b2d2014-03-06 13:38:19 +0800656
Miao Xie6a038432013-05-15 07:48:24 +0000657 ret = btrfs_start_delalloc_inodes(root, 0);
658 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100659 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000660
Chris Mason6374e57a2017-06-23 09:48:21 -0700661 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000662
Miao Xie66d8f3d2012-09-06 04:02:28 -0600663 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
664 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000665 /*
666 * 1 - parent dir inode
667 * 2 - dir entries
668 * 1 - root item
669 * 2 - root ref/backref
670 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200671 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000672 */
673 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200674 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400675 &pending_snapshot->qgroup_reserved,
676 false);
Miao Xied5c12072013-02-28 10:04:33 +0000677 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100678 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000679
Yan, Zhenga22285a2010-05-16 10:48:46 -0400680 pending_snapshot->dentry = dentry;
681 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800682 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000683 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000684 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400685
Miao Xied5c12072013-02-28 10:04:33 +0000686 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400687 if (IS_ERR(trans)) {
688 ret = PTR_ERR(trans);
689 goto fail;
690 }
691
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400692 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400693 list_add(&pending_snapshot->list,
694 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400695 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400696 if (async_transid) {
697 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400698 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000699 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400700 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400701 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400702 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400703 }
Miao Xieaec80302013-03-04 09:44:29 +0000704 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400705 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400706
707 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400708 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000709 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400710
Chris Masond3797302014-10-15 13:50:56 -0700711 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
712 if (ret)
713 goto fail;
714
David Howells2b0143b2015-03-17 22:25:59 +0000715 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000716 if (IS_ERR(inode)) {
717 ret = PTR_ERR(inode);
718 goto fail;
719 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900720
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000721 d_instantiate(dentry, inode);
722 ret = 0;
723fail:
David Sterba7775c812017-02-10 19:18:18 +0100724 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100725dec_and_free:
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000726 if (atomic_dec_and_test(&root->will_be_snapshoted))
727 wake_up_atomic_t(&root->will_be_snapshoted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100728free_pending:
729 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100730 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100731 kfree(pending_snapshot);
732
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400733 return ret;
734}
735
Sage Weil4260f7c2010-10-29 15:46:43 -0400736/* copy of may_delete in fs/namei.c()
737 * Check whether we can remove a link victim from directory dir, check
738 * whether the type of victim is right.
739 * 1. We can't do it if dir is read-only (done in permission())
740 * 2. We should have write and exec permissions on dir
741 * 3. We can't remove anything from append-only dir
742 * 4. We can't do anything with immutable dir (done in permission())
743 * 5. If the sticky bit on dir is set we should either
744 * a. be owner of dir, or
745 * b. be owner of victim, or
746 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400747 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400748 * links pointing to it.
749 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
750 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
751 * 9. We can't remove a root or mountpoint.
752 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
753 * nfs_async_unlink().
754 */
755
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530756static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400757{
758 int error;
759
David Howells2b0143b2015-03-17 22:25:59 +0000760 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400761 return -ENOENT;
762
David Howells2b0143b2015-03-17 22:25:59 +0000763 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400764 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400765
766 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
767 if (error)
768 return error;
769 if (IS_APPEND(dir))
770 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000771 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
772 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400773 return -EPERM;
774 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000775 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400776 return -ENOTDIR;
777 if (IS_ROOT(victim))
778 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000779 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400780 return -EISDIR;
781 if (IS_DEADDIR(dir))
782 return -ENOENT;
783 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
784 return -EBUSY;
785 return 0;
786}
787
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400788/* copy of may_create in fs/namei.c() */
789static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
790{
David Howells2b0143b2015-03-17 22:25:59 +0000791 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400792 return -EEXIST;
793 if (IS_DEADDIR(dir))
794 return -ENOENT;
795 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
796}
797
798/*
799 * Create a new subvolume below @parent. This is largely modeled after
800 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
801 * inside this filesystem so it's quite a bit simpler.
802 */
Al Viro92872092016-11-20 19:34:31 -0500803static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100804 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400805 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200806 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000807 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400808{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400809 struct inode *dir = d_inode(parent->dentry);
810 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400811 struct dentry *dentry;
812 int error;
813
Al Viro00235412016-05-26 00:05:12 -0400814 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
815 if (error == -EINTR)
816 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400817
818 dentry = lookup_one_len(name, parent->dentry, namelen);
819 error = PTR_ERR(dentry);
820 if (IS_ERR(dentry))
821 goto out_unlock;
822
Yan, Zheng76dda932009-09-21 16:00:26 -0400823 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400824 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600825 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400826
Chris Mason9c520572012-12-17 14:26:57 -0500827 /*
828 * even if this name doesn't exist, we may get hash collisions.
829 * check for them now when we can safely fail
830 */
831 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
832 dir->i_ino, name,
833 namelen);
834 if (error)
835 goto out_dput;
836
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400837 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400838
839 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
840 goto out_up_read;
841
Chris Mason3de45862008-11-17 21:02:50 -0500842 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100843 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200844 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500845 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000846 error = create_subvol(dir, dentry, name, namelen,
847 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500848 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400849 if (!error)
850 fsnotify_mkdir(dir, dentry);
851out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400852 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400853out_dput:
854 dput(dentry);
855out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500856 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400857 return error;
858}
859
Chris Mason4cb53002011-05-24 15:35:30 -0400860/*
861 * When we're defragging a range, we don't want to kick it off again
862 * if it is really just waiting for delalloc to send it down.
863 * If we find a nice big extent or delalloc range for the bytes in the
864 * file you want to defrag, we return 0 to let you know to skip this
865 * part of the file
866 */
David Sterbaaab110a2014-07-29 17:32:10 +0200867static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400868{
869 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
870 struct extent_map *em = NULL;
871 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
872 u64 end;
873
874 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300875 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -0400876 read_unlock(&em_tree->lock);
877
878 if (em) {
879 end = extent_map_end(em);
880 free_extent_map(em);
881 if (end - offset > thresh)
882 return 0;
883 }
884 /* if we already have a nice delalloc here, just stop */
885 thresh /= 2;
886 end = count_range_bits(io_tree, &offset, offset + thresh,
887 thresh, EXTENT_DELALLOC, 1);
888 if (end >= thresh)
889 return 0;
890 return 1;
891}
892
893/*
894 * helper function to walk through a file and find extents
895 * newer than a specific transid, and smaller than thresh.
896 *
897 * This is used by the defragging code to find new and small
898 * extents
899 */
900static int find_new_extents(struct btrfs_root *root,
901 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200902 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400903{
904 struct btrfs_path *path;
905 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400906 struct extent_buffer *leaf;
907 struct btrfs_file_extent_item *extent;
908 int type;
909 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200910 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -0400911
912 path = btrfs_alloc_path();
913 if (!path)
914 return -ENOMEM;
915
David Sterbaa4689d22011-05-31 17:08:14 +0000916 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400917 min_key.type = BTRFS_EXTENT_DATA_KEY;
918 min_key.offset = *off;
919
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530920 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100921 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400922 if (ret != 0)
923 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000924process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000925 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400926 goto none;
927 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
928 goto none;
929
930 leaf = path->nodes[0];
931 extent = btrfs_item_ptr(leaf, path->slots[0],
932 struct btrfs_file_extent_item);
933
934 type = btrfs_file_extent_type(leaf, extent);
935 if (type == BTRFS_FILE_EXTENT_REG &&
936 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
937 check_defrag_in_cache(inode, min_key.offset, thresh)) {
938 *off = min_key.offset;
939 btrfs_free_path(path);
940 return 0;
941 }
942
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000943 path->slots[0]++;
944 if (path->slots[0] < btrfs_header_nritems(leaf)) {
945 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
946 goto process_slot;
947 }
948
Chris Mason4cb53002011-05-24 15:35:30 -0400949 if (min_key.offset == (u64)-1)
950 goto none;
951
952 min_key.offset++;
953 btrfs_release_path(path);
954 }
955none:
956 btrfs_free_path(path);
957 return -ENOENT;
958}
959
Li Zefan6c282eb2012-06-11 16:03:35 +0800960static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400961{
962 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500963 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800964 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300965 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500966
967 /*
968 * hopefully we have this extent in the tree already, try without
969 * the full extent lock
970 */
971 read_lock(&em_tree->lock);
972 em = lookup_extent_mapping(em_tree, start, len);
973 read_unlock(&em_tree->lock);
974
975 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +0000976 struct extent_state *cached = NULL;
977 u64 end = start + len - 1;
978
Chris Mason940100a2010-03-10 10:52:59 -0500979 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +0100980 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +0200981 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +0000982 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500983
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000984 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800985 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500986 }
987
Li Zefan6c282eb2012-06-11 16:03:35 +0800988 return em;
989}
990
991static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
992{
993 struct extent_map *next;
994 bool ret = true;
995
996 /* this is the last extent */
997 if (em->start + em->len >= i_size_read(inode))
998 return false;
999
1000 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001001 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1002 ret = false;
1003 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001004 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001005 ret = false;
1006
1007 free_extent_map(next);
1008 return ret;
1009}
1010
David Sterbaaab110a2014-07-29 17:32:10 +02001011static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001012 u64 *last_len, u64 *skip, u64 *defrag_end,
1013 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001014{
1015 struct extent_map *em;
1016 int ret = 1;
1017 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001018 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001019
1020 /*
1021 * make sure that once we start defragging an extent, we keep on
1022 * defragging it
1023 */
1024 if (start < *defrag_end)
1025 return 1;
1026
1027 *skip = 0;
1028
1029 em = defrag_lookup_extent(inode, start);
1030 if (!em)
1031 return 0;
1032
Chris Mason940100a2010-03-10 10:52:59 -05001033 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001034 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001035 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001036 goto out;
1037 }
1038
Liu Bo4a3560c2015-08-07 16:48:41 +08001039 if (!*defrag_end)
1040 prev_mergeable = false;
1041
Li Zefan6c282eb2012-06-11 16:03:35 +08001042 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001043 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001044 * we hit a real extent, if it is big or the next extent is not a
1045 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001046 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001047 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001048 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001049 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001050out:
Chris Mason940100a2010-03-10 10:52:59 -05001051 /*
1052 * last_len ends up being a counter of how many bytes we've defragged.
1053 * every time we choose not to defrag an extent, we reset *last_len
1054 * so that the next tiny extent will force a defrag.
1055 *
1056 * The end result of this is that tiny extents before a single big
1057 * extent will force at least part of that big extent to be defragged.
1058 */
1059 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001060 *defrag_end = extent_map_end(em);
1061 } else {
1062 *last_len = 0;
1063 *skip = extent_map_end(em);
1064 *defrag_end = 0;
1065 }
1066
1067 free_extent_map(em);
1068 return ret;
1069}
1070
Chris Mason4cb53002011-05-24 15:35:30 -04001071/*
1072 * it doesn't do much good to defrag one or two pages
1073 * at a time. This pulls in a nice chunk of pages
1074 * to COW and defrag.
1075 *
1076 * It also makes sure the delalloc code has enough
1077 * dirty data to avoid making new small extents as part
1078 * of the defrag
1079 *
1080 * It's a good idea to start RA on this range
1081 * before calling this.
1082 */
1083static int cluster_pages_for_defrag(struct inode *inode,
1084 struct page **pages,
1085 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001086 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001087{
Chris Mason4cb53002011-05-24 15:35:30 -04001088 unsigned long file_end;
1089 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001090 u64 page_start;
1091 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001092 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001093 int ret;
1094 int i;
1095 int i_done;
1096 struct btrfs_ordered_extent *ordered;
1097 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001098 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001099 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001100 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001101
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001102 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001103 if (!isize || start_index > file_end)
1104 return 0;
1105
1106 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001107
Qu Wenruo364ecf32017-02-27 15:10:38 +08001108 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001109 start_index << PAGE_SHIFT,
1110 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001111 if (ret)
1112 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001113 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001114 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001115
1116 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001117 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001118 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001119again:
Josef Bacika94733d2011-07-11 10:47:06 -04001120 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001121 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001122 if (!page)
1123 break;
1124
Miao Xie600a45e2012-02-16 15:01:24 +08001125 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001126 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001127 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001128 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001129 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001130 ordered = btrfs_lookup_ordered_extent(inode,
1131 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001132 unlock_extent_cached(tree, page_start, page_end,
1133 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001134 if (!ordered)
1135 break;
1136
1137 unlock_page(page);
1138 btrfs_start_ordered_extent(inode, ordered, 1);
1139 btrfs_put_ordered_extent(ordered);
1140 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001141 /*
1142 * we unlocked the page above, so we need check if
1143 * it was released or not.
1144 */
1145 if (page->mapping != inode->i_mapping) {
1146 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001147 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001148 goto again;
1149 }
Miao Xie600a45e2012-02-16 15:01:24 +08001150 }
1151
Chris Mason4cb53002011-05-24 15:35:30 -04001152 if (!PageUptodate(page)) {
1153 btrfs_readpage(NULL, page);
1154 lock_page(page);
1155 if (!PageUptodate(page)) {
1156 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001157 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001158 ret = -EIO;
1159 break;
1160 }
1161 }
Miao Xie600a45e2012-02-16 15:01:24 +08001162
Miao Xie600a45e2012-02-16 15:01:24 +08001163 if (page->mapping != inode->i_mapping) {
1164 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001165 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001166 goto again;
1167 }
1168
Chris Mason4cb53002011-05-24 15:35:30 -04001169 pages[i] = page;
1170 i_done++;
1171 }
1172 if (!i_done || ret)
1173 goto out;
1174
1175 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1176 goto out;
1177
1178 /*
1179 * so now we have a nice long stream of locked
1180 * and up to date pages, lets wait on them
1181 */
1182 for (i = 0; i < i_done; i++)
1183 wait_on_page_writeback(pages[i]);
1184
1185 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001186 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001187
1188 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001189 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001190 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1191 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001192 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1193 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001194
Liu Bo1f12bd02012-03-29 09:57:44 -04001195 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001196 spin_lock(&BTRFS_I(inode)->lock);
1197 BTRFS_I(inode)->outstanding_extents++;
1198 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001199 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001200 start_index << PAGE_SHIFT,
1201 (page_cnt - i_done) << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001202 }
1203
1204
Liu Bo9e8a4a82012-09-05 19:10:51 -06001205 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001206 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001207
1208 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1209 page_start, page_end - 1, &cached_state,
1210 GFP_NOFS);
1211
1212 for (i = 0; i < i_done; i++) {
1213 clear_page_dirty_for_io(pages[i]);
1214 ClearPageChecked(pages[i]);
1215 set_page_extent_mapped(pages[i]);
1216 set_page_dirty(pages[i]);
1217 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001218 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001219 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08001220 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001221 return i_done;
1222out:
1223 for (i = 0; i < i_done; i++) {
1224 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001225 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001226 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001227 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 start_index << PAGE_SHIFT,
1229 page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001230 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001231 return ret;
1232
1233}
1234
1235int btrfs_defrag_file(struct inode *inode, struct file *file,
1236 struct btrfs_ioctl_defrag_range_args *range,
1237 u64 newer_than, unsigned long max_to_defrag)
1238{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001239 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001240 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001241 struct file_ra_state *ra = NULL;
1242 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001243 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001244 u64 last_len = 0;
1245 u64 skip = 0;
1246 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001247 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001248 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001249 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001250 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001251 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001252 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001253 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001254 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001255 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001256 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001257 struct page **pages = NULL;
1258
Liu Bo0abd5b12013-04-16 09:20:28 +00001259 if (isize == 0)
1260 return 0;
1261
1262 if (range->start >= isize)
1263 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001264
1265 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1266 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1267 return -EINVAL;
1268 if (range->compress_type)
1269 compress_type = range->compress_type;
1270 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001271
Liu Bo0abd5b12013-04-16 09:20:28 +00001272 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001273 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001274
Chris Mason4cb53002011-05-24 15:35:30 -04001275 /*
1276 * if we were not given a file, allocate a readahead
1277 * context
1278 */
1279 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001280 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001281 if (!ra)
1282 return -ENOMEM;
1283 file_ra_state_init(ra, inode->i_mapping);
1284 } else {
1285 ra = &file->f_ra;
1286 }
1287
David Sterba63e727e2017-06-22 03:13:02 +02001288 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001289 if (!pages) {
1290 ret = -ENOMEM;
1291 goto out_ra;
1292 }
1293
1294 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001295 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001296 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001297 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001298 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001299 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001300 }
1301
Chris Mason4cb53002011-05-24 15:35:30 -04001302 if (newer_than) {
1303 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001304 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001305 if (!ret) {
1306 range->start = newer_off;
1307 /*
1308 * we always align our defrag to help keep
1309 * the extents in the file evenly spaced
1310 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001311 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001312 } else
1313 goto out_ra;
1314 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001315 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001316 }
1317 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301318 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001319
Li Zefan2a0f7f52011-10-10 15:43:34 -04001320 /*
1321 * make writeback starts from i, so the defrag range can be
1322 * written sequentially.
1323 */
1324 if (i < inode->i_mapping->writeback_index)
1325 inode->i_mapping->writeback_index = i;
1326
Chris Masonf7f43cc2011-10-11 11:41:40 -04001327 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001328 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001329 /*
1330 * make sure we stop running if someone unmounts
1331 * the FS
1332 */
1333 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1334 break;
1335
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001336 if (btrfs_defrag_cancelled(fs_info)) {
1337 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001338 ret = -EAGAIN;
1339 break;
1340 }
1341
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001342 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001343 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001344 &defrag_end, range->flags &
1345 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001346 unsigned long next;
1347 /*
1348 * the should_defrag function tells us how much to skip
1349 * bump our counter by the suggested amount
1350 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001351 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001352 i = max(i + 1, next);
1353 continue;
1354 }
Li Zefan008873e2011-09-02 15:57:07 +08001355
1356 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001357 cluster = (PAGE_ALIGN(defrag_end) >>
1358 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001359 cluster = min(cluster, max_cluster);
1360 } else {
1361 cluster = max_cluster;
1362 }
1363
Li Zefan008873e2011-09-02 15:57:07 +08001364 if (i + cluster > ra_index) {
1365 ra_index = max(i, ra_index);
1366 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1367 cluster);
chandane4826a52015-06-09 17:38:32 +05301368 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001369 }
Chris Mason940100a2010-03-10 10:52:59 -05001370
Al Viro59551022016-01-22 15:40:57 -05001371 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001372 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1373 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001374 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001375 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001376 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001377 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001378 }
Chris Mason940100a2010-03-10 10:52:59 -05001379
Chris Mason4cb53002011-05-24 15:35:30 -04001380 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001381 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001382 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001383
1384 if (newer_than) {
1385 if (newer_off == (u64)-1)
1386 break;
1387
Liu Boe1f041e2012-03-29 09:57:45 -04001388 if (ret > 0)
1389 i += ret;
1390
Chris Mason4cb53002011-05-24 15:35:30 -04001391 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001392 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001393
Byongho Leeee221842015-12-15 01:42:10 +09001394 ret = find_new_extents(root, inode, newer_than,
1395 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001396 if (!ret) {
1397 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001398 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001399 } else {
1400 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001401 }
Chris Mason4cb53002011-05-24 15:35:30 -04001402 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001403 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001404 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001405 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001406 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001407 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001408 last_len = 0;
1409 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001410 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001411 }
1412
Filipe Mananadec8ef92014-03-01 10:55:54 +00001413 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001414 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001415 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1416 &BTRFS_I(inode)->runtime_flags))
1417 filemap_flush(inode->i_mapping);
1418 }
Chris Mason1e701a32010-03-11 09:42:04 -05001419
1420 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1421 /* the filemap_flush will queue IO into the worker threads, but
1422 * we have to make sure the IO is actually started and that
1423 * ordered extents get created before we return
1424 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001425 atomic_inc(&fs_info->async_submit_draining);
1426 while (atomic_read(&fs_info->nr_async_submits) ||
1427 atomic_read(&fs_info->async_delalloc_pages)) {
1428 wait_event(fs_info->async_submit_wait,
1429 (atomic_read(&fs_info->nr_async_submits) == 0 &&
1430 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason1e701a32010-03-11 09:42:04 -05001431 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001432 atomic_dec(&fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001433 }
1434
Li Zefan1a419d82010-10-25 15:12:50 +08001435 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001436 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001437 }
1438
Diego Calleja60ccf822011-09-01 16:33:57 +02001439 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001440
Chris Mason4cb53002011-05-24 15:35:30 -04001441out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001442 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
Al Viro59551022016-01-22 15:40:57 -05001443 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001444 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001445 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001446 }
Chris Mason4cb53002011-05-24 15:35:30 -04001447 if (!file)
1448 kfree(ra);
1449 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001450 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001451}
1452
Miao Xie198605a2012-11-26 08:43:45 +00001453static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001454 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001455{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001456 struct inode *inode = file_inode(file);
1457 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001458 u64 new_size;
1459 u64 old_size;
1460 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001461 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001462 struct btrfs_ioctl_vol_args *vol_args;
1463 struct btrfs_trans_handle *trans;
1464 struct btrfs_device *device = NULL;
1465 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001466 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001467 char *devstr = NULL;
1468 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001469 int mod = 0;
1470
Chris Masone441d542009-01-05 16:57:23 -05001471 if (!capable(CAP_SYS_ADMIN))
1472 return -EPERM;
1473
Miao Xie198605a2012-11-26 08:43:45 +00001474 ret = mnt_want_write_file(file);
1475 if (ret)
1476 return ret;
1477
David Sterba171938e2017-03-28 14:44:21 +02001478 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001479 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001480 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001481 }
1482
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001483 mutex_lock(&fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001484 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001485 if (IS_ERR(vol_args)) {
1486 ret = PTR_ERR(vol_args);
1487 goto out;
1488 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001489
1490 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001491
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001492 sizestr = vol_args->name;
1493 devstr = strchr(sizestr, ':');
1494 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001495 sizestr = devstr + 1;
1496 *devstr = '\0';
1497 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001498 ret = kstrtoull(devstr, 10, &devid);
1499 if (ret)
1500 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001501 if (!devid) {
1502 ret = -EINVAL;
1503 goto out_free;
1504 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001505 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001506 }
Miao Xiedba60f32012-12-21 09:19:51 +00001507
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001508 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001509 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001510 btrfs_info(fs_info, "resizer unable to find device %llu",
1511 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001512 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001513 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001514 }
Miao Xiedba60f32012-12-21 09:19:51 +00001515
1516 if (!device->writeable) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001517 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001518 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001519 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001520 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001521 goto out_free;
1522 }
1523
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001524 if (!strcmp(sizestr, "max"))
1525 new_size = device->bdev->bd_inode->i_size;
1526 else {
1527 if (sizestr[0] == '-') {
1528 mod = -1;
1529 sizestr++;
1530 } else if (sizestr[0] == '+') {
1531 mod = 1;
1532 sizestr++;
1533 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001534 new_size = memparse(sizestr, &retptr);
1535 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001536 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001537 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001538 }
1539 }
1540
Stefan Behrens63a212a2012-11-05 18:29:28 +01001541 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001542 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001543 goto out_free;
1544 }
1545
Miao Xie7cc8e582014-09-03 21:35:38 +08001546 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001547
1548 if (mod < 0) {
1549 if (new_size > old_size) {
1550 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001551 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001552 }
1553 new_size = old_size - new_size;
1554 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001555 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001556 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001557 goto out_free;
1558 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001559 new_size = old_size + new_size;
1560 }
1561
Byongho Leeee221842015-12-15 01:42:10 +09001562 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001563 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001564 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001565 }
1566 if (new_size > device->bdev->bd_inode->i_size) {
1567 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001568 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001569 }
1570
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001571 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001573 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1574 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001575
1576 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001577 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001578 if (IS_ERR(trans)) {
1579 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001580 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001581 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001582 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001583 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001584 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001585 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001586 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001587
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001588out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001589 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001590out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001591 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02001592 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001593 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001594 return ret;
1595}
1596
Sage Weil72fd0322010-10-29 15:41:32 -04001597static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001598 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001599 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001600 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001601{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001602 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001603 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001604
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001605 if (!S_ISDIR(file_inode(file)->i_mode))
1606 return -ENOTDIR;
1607
Liu Boa874a632012-06-29 03:58:46 -06001608 ret = mnt_want_write_file(file);
1609 if (ret)
1610 goto out;
1611
Sage Weil72fd0322010-10-29 15:41:32 -04001612 namelen = strlen(name);
1613 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001615 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616 }
1617
Chris Mason16780ca2012-02-20 22:14:55 -05001618 if (name[0] == '.' &&
1619 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1620 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001621 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001622 }
1623
Chris Mason3de45862008-11-17 21:02:50 -05001624 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001625 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001626 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001627 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001628 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001629 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001630 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001631 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001632 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001633 }
1634
Al Viro496ad9a2013-01-23 17:07:38 -05001635 src_inode = file_inode(src.file);
1636 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001637 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001638 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001639 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001640 } else if (!inode_owner_or_capable(src_inode)) {
1641 /*
1642 * Subvolume creation is not restricted, but snapshots
1643 * are limited to own subvolumes only
1644 */
1645 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001646 } else {
1647 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1648 BTRFS_I(src_inode)->root,
1649 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001650 }
Al Viro2903ff02012-08-28 12:52:22 -04001651 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001652 }
Liu Boa874a632012-06-29 03:58:46 -06001653out_drop_write:
1654 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001655out:
Sage Weil72fd0322010-10-29 15:41:32 -04001656 return ret;
1657}
1658
1659static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001660 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001661{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001662 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001663 int ret;
1664
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001665 if (!S_ISDIR(file_inode(file)->i_mode))
1666 return -ENOTDIR;
1667
Li Zefanfa0d2b92010-12-20 15:53:28 +08001668 vol_args = memdup_user(arg, sizeof(*vol_args));
1669 if (IS_ERR(vol_args))
1670 return PTR_ERR(vol_args);
1671 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001672
Li Zefanfa0d2b92010-12-20 15:53:28 +08001673 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001674 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001675 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001676
Li Zefanfa0d2b92010-12-20 15:53:28 +08001677 kfree(vol_args);
1678 return ret;
1679}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001680
Li Zefanfa0d2b92010-12-20 15:53:28 +08001681static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1682 void __user *arg, int subvol)
1683{
1684 struct btrfs_ioctl_vol_args_v2 *vol_args;
1685 int ret;
1686 u64 transid = 0;
1687 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001688 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001689 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001690
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001691 if (!S_ISDIR(file_inode(file)->i_mode))
1692 return -ENOTDIR;
1693
Li Zefanfa0d2b92010-12-20 15:53:28 +08001694 vol_args = memdup_user(arg, sizeof(*vol_args));
1695 if (IS_ERR(vol_args))
1696 return PTR_ERR(vol_args);
1697 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001698
Li Zefanb83cc962010-12-20 16:04:08 +08001699 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001700 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1701 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001702 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001703 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001704 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001705
1706 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1707 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001708 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1709 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001710 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001711 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001712 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001713 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001714 }
1715 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1716 if (IS_ERR(inherit)) {
1717 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001718 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001719 }
1720 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001721
1722 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001723 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001724 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001725 if (ret)
1726 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001727
Dan Carpenterc47ca322014-09-04 14:09:15 +03001728 if (ptr && copy_to_user(arg +
1729 offsetof(struct btrfs_ioctl_vol_args_v2,
1730 transid),
1731 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001732 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001733
1734free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001735 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001736free_args:
1737 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001738 return ret;
1739}
1740
Li Zefan0caa1022010-12-20 16:30:25 +08001741static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1742 void __user *arg)
1743{
Al Viro496ad9a2013-01-23 17:07:38 -05001744 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001746 struct btrfs_root *root = BTRFS_I(inode)->root;
1747 int ret = 0;
1748 u64 flags = 0;
1749
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001750 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001751 return -EINVAL;
1752
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001754 if (btrfs_root_readonly(root))
1755 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001756 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001757
1758 if (copy_to_user(arg, &flags, sizeof(flags)))
1759 ret = -EFAULT;
1760
1761 return ret;
1762}
1763
1764static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1765 void __user *arg)
1766{
Al Viro496ad9a2013-01-23 17:07:38 -05001767 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001768 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001769 struct btrfs_root *root = BTRFS_I(inode)->root;
1770 struct btrfs_trans_handle *trans;
1771 u64 root_flags;
1772 u64 flags;
1773 int ret = 0;
1774
David Sterbabd60ea02014-01-16 15:50:22 +01001775 if (!inode_owner_or_capable(inode))
1776 return -EPERM;
1777
Liu Bob9ca0662012-06-29 03:58:49 -06001778 ret = mnt_want_write_file(file);
1779 if (ret)
1780 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001781
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001782 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001783 ret = -EINVAL;
1784 goto out_drop_write;
1785 }
Li Zefan0caa1022010-12-20 16:30:25 +08001786
Liu Bob9ca0662012-06-29 03:58:49 -06001787 if (copy_from_user(&flags, arg, sizeof(flags))) {
1788 ret = -EFAULT;
1789 goto out_drop_write;
1790 }
Li Zefan0caa1022010-12-20 16:30:25 +08001791
Liu Bob9ca0662012-06-29 03:58:49 -06001792 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1793 ret = -EINVAL;
1794 goto out_drop_write;
1795 }
Li Zefan0caa1022010-12-20 16:30:25 +08001796
Liu Bob9ca0662012-06-29 03:58:49 -06001797 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1798 ret = -EOPNOTSUPP;
1799 goto out_drop_write;
1800 }
Li Zefan0caa1022010-12-20 16:30:25 +08001801
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001802 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001803
1804 /* nothing to do */
1805 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001806 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001807
1808 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001809 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001810 btrfs_set_root_flags(&root->root_item,
1811 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001812 } else {
1813 /*
1814 * Block RO -> RW transition if this subvolume is involved in
1815 * send
1816 */
1817 spin_lock(&root->root_item_lock);
1818 if (root->send_in_progress == 0) {
1819 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001820 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001821 spin_unlock(&root->root_item_lock);
1822 } else {
1823 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001824 btrfs_warn(fs_info,
1825 "Attempt to set subvolume %llu read-write during send",
1826 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001827 ret = -EPERM;
1828 goto out_drop_sem;
1829 }
1830 }
Li Zefan0caa1022010-12-20 16:30:25 +08001831
1832 trans = btrfs_start_transaction(root, 1);
1833 if (IS_ERR(trans)) {
1834 ret = PTR_ERR(trans);
1835 goto out_reset;
1836 }
1837
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001838 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001839 &root->root_key, &root->root_item);
1840
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001841 btrfs_commit_transaction(trans);
Li Zefan0caa1022010-12-20 16:30:25 +08001842out_reset:
1843 if (ret)
1844 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001845out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001846 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001847out_drop_write:
1848 mnt_drop_write_file(file);
1849out:
Li Zefan0caa1022010-12-20 16:30:25 +08001850 return ret;
1851}
1852
Yan, Zheng76dda932009-09-21 16:00:26 -04001853/*
1854 * helper to check if the subvolume references other subvolumes
1855 */
1856static noinline int may_destroy_subvol(struct btrfs_root *root)
1857{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001858 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04001859 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001860 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001861 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001862 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001863 int ret;
1864
1865 path = btrfs_alloc_path();
1866 if (!path)
1867 return -ENOMEM;
1868
Josef Bacik175a2b82013-08-12 15:36:44 -04001869 /* Make sure this root isn't set as the default subvol */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1871 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
Josef Bacik175a2b82013-08-12 15:36:44 -04001872 dir_id, "default", 7, 0);
1873 if (di && !IS_ERR(di)) {
1874 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1875 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001876 ret = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001877 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001878 "deleting default subvolume %llu is not allowed",
1879 key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001880 goto out;
1881 }
1882 btrfs_release_path(path);
1883 }
1884
Yan, Zheng76dda932009-09-21 16:00:26 -04001885 key.objectid = root->root_key.objectid;
1886 key.type = BTRFS_ROOT_REF_KEY;
1887 key.offset = (u64)-1;
1888
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001889 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04001890 if (ret < 0)
1891 goto out;
1892 BUG_ON(ret == 0);
1893
1894 ret = 0;
1895 if (path->slots[0] > 0) {
1896 path->slots[0]--;
1897 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1898 if (key.objectid == root->root_key.objectid &&
1899 key.type == BTRFS_ROOT_REF_KEY)
1900 ret = -ENOTEMPTY;
1901 }
1902out:
1903 btrfs_free_path(path);
1904 return ret;
1905}
1906
Chris Masonac8e9812010-02-28 15:39:26 -05001907static noinline int key_in_sk(struct btrfs_key *key,
1908 struct btrfs_ioctl_search_key *sk)
1909{
Chris Masonabc6e132010-03-18 12:10:08 -04001910 struct btrfs_key test;
1911 int ret;
1912
1913 test.objectid = sk->min_objectid;
1914 test.type = sk->min_type;
1915 test.offset = sk->min_offset;
1916
1917 ret = btrfs_comp_cpu_keys(key, &test);
1918 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001919 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001920
1921 test.objectid = sk->max_objectid;
1922 test.type = sk->max_type;
1923 test.offset = sk->max_offset;
1924
1925 ret = btrfs_comp_cpu_keys(key, &test);
1926 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001927 return 0;
1928 return 1;
1929}
1930
Jeff Mahoneydf397562016-06-21 20:18:21 -04001931static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001932 struct btrfs_key *key,
1933 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001934 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001935 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001936 unsigned long *sk_offset,
1937 int *num_found)
1938{
1939 u64 found_transid;
1940 struct extent_buffer *leaf;
1941 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001942 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001943 unsigned long item_off;
1944 unsigned long item_len;
1945 int nritems;
1946 int i;
1947 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001948 int ret = 0;
1949
1950 leaf = path->nodes[0];
1951 slot = path->slots[0];
1952 nritems = btrfs_header_nritems(leaf);
1953
1954 if (btrfs_header_generation(leaf) > sk->max_transid) {
1955 i = nritems;
1956 goto advance_key;
1957 }
1958 found_transid = btrfs_header_generation(leaf);
1959
1960 for (i = slot; i < nritems; i++) {
1961 item_off = btrfs_item_ptr_offset(leaf, i);
1962 item_len = btrfs_item_size_nr(leaf, i);
1963
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001964 btrfs_item_key_to_cpu(leaf, key, i);
1965 if (!key_in_sk(key, sk))
1966 continue;
1967
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001968 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001969 if (*num_found) {
1970 ret = 1;
1971 goto out;
1972 }
Chris Masonac8e9812010-02-28 15:39:26 -05001973
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001974 /*
1975 * return one empty item back for v1, which does not
1976 * handle -EOVERFLOW
1977 */
1978
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001979 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001980 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001981 ret = -EOVERFLOW;
1982 }
Chris Masonac8e9812010-02-28 15:39:26 -05001983
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001984 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05001985 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001986 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001987 }
1988
Chris Masonac8e9812010-02-28 15:39:26 -05001989 sh.objectid = key->objectid;
1990 sh.offset = key->offset;
1991 sh.type = key->type;
1992 sh.len = item_len;
1993 sh.transid = found_transid;
1994
1995 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01001996 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
1997 ret = -EFAULT;
1998 goto out;
1999 }
2000
Chris Masonac8e9812010-02-28 15:39:26 -05002001 *sk_offset += sizeof(sh);
2002
2003 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002004 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002005 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002006 if (read_extent_buffer_to_user(leaf, up,
2007 item_off, item_len)) {
2008 ret = -EFAULT;
2009 goto out;
2010 }
2011
Chris Masonac8e9812010-02-28 15:39:26 -05002012 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002013 }
Hugo Millse2156862011-05-14 17:43:41 +00002014 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002015
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002016 if (ret) /* -EOVERFLOW from above */
2017 goto out;
2018
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002019 if (*num_found >= sk->nr_items) {
2020 ret = 1;
2021 goto out;
2022 }
Chris Masonac8e9812010-02-28 15:39:26 -05002023 }
2024advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002025 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002026 test.objectid = sk->max_objectid;
2027 test.type = sk->max_type;
2028 test.offset = sk->max_offset;
2029 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2030 ret = 1;
2031 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002032 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002033 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002034 key->offset = 0;
2035 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002036 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002037 key->offset = 0;
2038 key->type = 0;
2039 key->objectid++;
2040 } else
2041 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002042out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002043 /*
2044 * 0: all items from this leaf copied, continue with next
2045 * 1: * more items can be copied, but unused buffer is too small
2046 * * all items were found
2047 * Either way, it will stops the loop which iterates to the next
2048 * leaf
2049 * -EOVERFLOW: item was to large for buffer
2050 * -EFAULT: could not copy extent buffer back to userspace
2051 */
Chris Masonac8e9812010-02-28 15:39:26 -05002052 return ret;
2053}
2054
2055static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002056 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002057 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002058 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002059{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002060 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002061 struct btrfs_root *root;
2062 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002063 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002064 int ret;
2065 int num_found = 0;
2066 unsigned long sk_offset = 0;
2067
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002068 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2069 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002070 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002071 }
Gerhard Heift12544442014-01-30 16:23:58 +01002072
Chris Masonac8e9812010-02-28 15:39:26 -05002073 path = btrfs_alloc_path();
2074 if (!path)
2075 return -ENOMEM;
2076
2077 if (sk->tree_id == 0) {
2078 /* search the root of the inode that was passed */
2079 root = BTRFS_I(inode)->root;
2080 } else {
2081 key.objectid = sk->tree_id;
2082 key.type = BTRFS_ROOT_ITEM_KEY;
2083 key.offset = (u64)-1;
2084 root = btrfs_read_fs_root_no_name(info, &key);
2085 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002086 btrfs_free_path(path);
2087 return -ENOENT;
2088 }
2089 }
2090
2091 key.objectid = sk->min_objectid;
2092 key.type = sk->min_type;
2093 key.offset = sk->min_offset;
2094
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302095 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002096 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002097 if (ret != 0) {
2098 if (ret > 0)
2099 ret = 0;
2100 goto err;
2101 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002102 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002103 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002104 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002105 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002106 break;
2107
2108 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002109 if (ret > 0)
2110 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002111err:
2112 sk->nr_items = num_found;
2113 btrfs_free_path(path);
2114 return ret;
2115}
2116
2117static noinline int btrfs_ioctl_tree_search(struct file *file,
2118 void __user *argp)
2119{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002120 struct btrfs_ioctl_search_args __user *uargs;
2121 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002122 struct inode *inode;
2123 int ret;
2124 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002125
2126 if (!capable(CAP_SYS_ADMIN))
2127 return -EPERM;
2128
Gerhard Heiftba346b32014-01-30 16:24:02 +01002129 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002130
Gerhard Heiftba346b32014-01-30 16:24:02 +01002131 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2132 return -EFAULT;
2133
2134 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002135
Al Viro496ad9a2013-01-23 17:07:38 -05002136 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002137 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002138
2139 /*
2140 * In the origin implementation an overflow is handled by returning a
2141 * search header with a len of zero, so reset ret.
2142 */
2143 if (ret == -EOVERFLOW)
2144 ret = 0;
2145
Gerhard Heiftba346b32014-01-30 16:24:02 +01002146 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002147 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002148 return ret;
2149}
2150
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002151static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2152 void __user *argp)
2153{
2154 struct btrfs_ioctl_search_args_v2 __user *uarg;
2155 struct btrfs_ioctl_search_args_v2 args;
2156 struct inode *inode;
2157 int ret;
2158 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002159 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002160
2161 if (!capable(CAP_SYS_ADMIN))
2162 return -EPERM;
2163
2164 /* copy search header and buffer size */
2165 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2166 if (copy_from_user(&args, uarg, sizeof(args)))
2167 return -EFAULT;
2168
2169 buf_size = args.buf_size;
2170
2171 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2172 return -EOVERFLOW;
2173
2174 /* limit result size to 16MB */
2175 if (buf_size > buf_limit)
2176 buf_size = buf_limit;
2177
2178 inode = file_inode(file);
2179 ret = search_ioctl(inode, &args.key, &buf_size,
2180 (char *)(&uarg->buf[0]));
2181 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2182 ret = -EFAULT;
2183 else if (ret == -EOVERFLOW &&
2184 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2185 ret = -EFAULT;
2186
Yan, Zheng76dda932009-09-21 16:00:26 -04002187 return ret;
2188}
2189
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002190/*
Chris Masonac8e9812010-02-28 15:39:26 -05002191 * Search INODE_REFs to identify path name of 'dirid' directory
2192 * in a 'tree_id' tree. and sets path name to 'name'.
2193 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002194static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2195 u64 tree_id, u64 dirid, char *name)
2196{
2197 struct btrfs_root *root;
2198 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002199 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002200 int ret = -1;
2201 int slot;
2202 int len;
2203 int total_len = 0;
2204 struct btrfs_inode_ref *iref;
2205 struct extent_buffer *l;
2206 struct btrfs_path *path;
2207
2208 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2209 name[0]='\0';
2210 return 0;
2211 }
2212
2213 path = btrfs_alloc_path();
2214 if (!path)
2215 return -ENOMEM;
2216
Chris Masonac8e9812010-02-28 15:39:26 -05002217 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002218
2219 key.objectid = tree_id;
2220 key.type = BTRFS_ROOT_ITEM_KEY;
2221 key.offset = (u64)-1;
2222 root = btrfs_read_fs_root_no_name(info, &key);
2223 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002224 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002225 ret = -ENOENT;
2226 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002227 }
2228
2229 key.objectid = dirid;
2230 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002231 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002232
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302233 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002234 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2235 if (ret < 0)
2236 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002237 else if (ret > 0) {
2238 ret = btrfs_previous_item(root, path, dirid,
2239 BTRFS_INODE_REF_KEY);
2240 if (ret < 0)
2241 goto out;
2242 else if (ret > 0) {
2243 ret = -ENOENT;
2244 goto out;
2245 }
2246 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002247
2248 l = path->nodes[0];
2249 slot = path->slots[0];
2250 btrfs_item_key_to_cpu(l, &key, slot);
2251
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002252 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2253 len = btrfs_inode_ref_name_len(l, iref);
2254 ptr -= len + 1;
2255 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002256 if (ptr < name) {
2257 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002258 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002259 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002260
2261 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302262 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002263
2264 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2265 break;
2266
David Sterbab3b4aa72011-04-21 01:20:15 +02002267 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002268 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002269 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002270 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002271 }
Li Zefan77906a502011-07-14 03:16:00 +00002272 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302273 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002274 ret = 0;
2275out:
2276 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002277 return ret;
2278}
2279
2280static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2281 void __user *argp)
2282{
2283 struct btrfs_ioctl_ino_lookup_args *args;
2284 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002285 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002286
Julia Lawall2354d082010-10-29 15:14:18 -04002287 args = memdup_user(argp, sizeof(*args));
2288 if (IS_ERR(args))
2289 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002290
Al Viro496ad9a2013-01-23 17:07:38 -05002291 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002292
David Sterba01b810b2015-05-12 19:14:49 +02002293 /*
2294 * Unprivileged query to obtain the containing subvolume root id. The
2295 * path is reset so it's consistent with btrfs_search_path_in_tree.
2296 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002297 if (args->treeid == 0)
2298 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2299
David Sterba01b810b2015-05-12 19:14:49 +02002300 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2301 args->name[0] = 0;
2302 goto out;
2303 }
2304
2305 if (!capable(CAP_SYS_ADMIN)) {
2306 ret = -EPERM;
2307 goto out;
2308 }
2309
Chris Masonac8e9812010-02-28 15:39:26 -05002310 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2311 args->treeid, args->objectid,
2312 args->name);
2313
David Sterba01b810b2015-05-12 19:14:49 +02002314out:
Chris Masonac8e9812010-02-28 15:39:26 -05002315 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2316 ret = -EFAULT;
2317
2318 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002319 return ret;
2320}
2321
Yan, Zheng76dda932009-09-21 16:00:26 -04002322static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2323 void __user *arg)
2324{
Al Viro54563d42013-09-01 15:57:51 -04002325 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002326 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002327 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002328 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002329 struct inode *inode;
2330 struct btrfs_root *root = BTRFS_I(dir)->root;
2331 struct btrfs_root *dest = NULL;
2332 struct btrfs_ioctl_vol_args *vol_args;
2333 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002334 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002335 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002336 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002337 int namelen;
2338 int ret;
2339 int err = 0;
2340
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002341 if (!S_ISDIR(dir->i_mode))
2342 return -ENOTDIR;
2343
Yan, Zheng76dda932009-09-21 16:00:26 -04002344 vol_args = memdup_user(arg, sizeof(*vol_args));
2345 if (IS_ERR(vol_args))
2346 return PTR_ERR(vol_args);
2347
2348 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2349 namelen = strlen(vol_args->name);
2350 if (strchr(vol_args->name, '/') ||
2351 strncmp(vol_args->name, "..", namelen) == 0) {
2352 err = -EINVAL;
2353 goto out;
2354 }
2355
Al Viroa561be72011-11-23 11:57:51 -05002356 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002357 if (err)
2358 goto out;
2359
David Sterba521e0542014-04-15 16:41:44 +02002360
Al Viro00235412016-05-26 00:05:12 -04002361 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2362 if (err == -EINTR)
2363 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002364 dentry = lookup_one_len(vol_args->name, parent, namelen);
2365 if (IS_ERR(dentry)) {
2366 err = PTR_ERR(dentry);
2367 goto out_unlock_dir;
2368 }
2369
David Howells2b0143b2015-03-17 22:25:59 +00002370 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002371 err = -ENOENT;
2372 goto out_dput;
2373 }
2374
David Howells2b0143b2015-03-17 22:25:59 +00002375 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002376 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302377 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002378 /*
2379 * Regular user. Only allow this with a special mount
2380 * option, when the user has write+exec access to the
2381 * subvol root, and when rmdir(2) would have been
2382 * allowed.
2383 *
2384 * Note that this is _not_ check that the subvol is
2385 * empty or doesn't contain data that we wouldn't
2386 * otherwise be able to delete.
2387 *
2388 * Users who want to delete empty subvols should try
2389 * rmdir(2).
2390 */
2391 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002392 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002393 goto out_dput;
2394
2395 /*
2396 * Do not allow deletion if the parent dir is the same
2397 * as the dir to be deleted. That means the ioctl
2398 * must be called on the dentry referencing the root
2399 * of the subvol, not a random directory contained
2400 * within it.
2401 */
2402 err = -EINVAL;
2403 if (root == dest)
2404 goto out_dput;
2405
2406 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2407 if (err)
2408 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002409 }
2410
Miao Xie5c39da52012-10-22 11:39:53 +00002411 /* check if subvolume may be deleted by a user */
2412 err = btrfs_may_delete(dir, dentry, 1);
2413 if (err)
2414 goto out_dput;
2415
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002416 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002417 err = -EINVAL;
2418 goto out_dput;
2419 }
2420
Al Viro59551022016-01-22 15:40:57 -05002421 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002422
2423 /*
2424 * Don't allow to delete a subvolume with send in progress. This is
2425 * inside the i_mutex so the error handling that has to drop the bit
2426 * again is not run concurrently.
2427 */
2428 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002429 root_flags = btrfs_root_flags(&dest->root_item);
2430 if (dest->send_in_progress == 0) {
2431 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002432 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2433 spin_unlock(&dest->root_item_lock);
2434 } else {
2435 spin_unlock(&dest->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002436 btrfs_warn(fs_info,
2437 "Attempt to delete subvolume %llu during send",
2438 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002439 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002440 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002441 }
2442
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002443 down_write(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -04002444
2445 err = may_destroy_subvol(dest);
2446 if (err)
2447 goto out_up_write;
2448
Miao Xiec58aaad2013-02-28 10:05:36 +00002449 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2450 /*
2451 * One for dir inode, two for dir entries, two for root
2452 * ref/backref.
2453 */
2454 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002455 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002456 if (err)
2457 goto out_up_write;
2458
Yan, Zhenga22285a2010-05-16 10:48:46 -04002459 trans = btrfs_start_transaction(root, 0);
2460 if (IS_ERR(trans)) {
2461 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002462 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002463 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002464 trans->block_rsv = &block_rsv;
2465 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002466
Nikolay Borisov43663552017-01-18 00:31:29 +02002467 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
Filipe Manana2be63d52016-02-12 11:34:23 +00002468
Yan, Zheng76dda932009-09-21 16:00:26 -04002469 ret = btrfs_unlink_subvol(trans, root, dir,
2470 dest->root_key.objectid,
2471 dentry->d_name.name,
2472 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002473 if (ret) {
2474 err = ret;
Jeff Mahoney66642832016-06-10 18:19:25 -04002475 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002476 goto out_end_trans;
2477 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002478
2479 btrfs_record_root_in_trans(trans, dest);
2480
2481 memset(&dest->root_item.drop_progress, 0,
2482 sizeof(dest->root_item.drop_progress));
2483 dest->root_item.drop_level = 0;
2484 btrfs_set_root_refs(&dest->root_item, 0);
2485
Miao Xie27cdeb72014-04-02 19:51:05 +08002486 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002487 ret = btrfs_insert_orphan_item(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04002489 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002490 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002491 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002492 err = ret;
2493 goto out_end_trans;
2494 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002495 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002496
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002497 ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002498 BTRFS_UUID_KEY_SUBVOL,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002499 dest->root_key.objectid);
2500 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002501 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002502 err = ret;
2503 goto out_end_trans;
2504 }
2505 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002506 ret = btrfs_uuid_tree_rem(trans, fs_info,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002507 dest->root_item.received_uuid,
2508 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2509 dest->root_key.objectid);
2510 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002511 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002512 err = ret;
2513 goto out_end_trans;
2514 }
2515 }
2516
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002517out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002518 trans->block_rsv = NULL;
2519 trans->bytes_reserved = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002520 ret = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002521 if (ret && !err)
2522 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002523 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002524out_release:
David Sterba7775c812017-02-10 19:18:18 +01002525 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Yan, Zheng76dda932009-09-21 16:00:26 -04002526out_up_write:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002527 up_write(&fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002528 if (err) {
2529 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002530 root_flags = btrfs_root_flags(&dest->root_item);
2531 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002532 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2533 spin_unlock(&dest->root_item_lock);
2534 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002535out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002536 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002537 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002538 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002539 btrfs_invalidate_inodes(dest);
2540 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002541 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002542
2543 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002544 if (dest->ino_cache_inode) {
2545 iput(dest->ino_cache_inode);
2546 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002547 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002548 }
2549out_dput:
2550 dput(dentry);
2551out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002552 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002553out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002554 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002555out:
2556 kfree(vol_args);
2557 return err;
2558}
2559
Chris Mason1e701a32010-03-11 09:42:04 -05002560static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002561{
Al Viro496ad9a2013-01-23 17:07:38 -05002562 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002563 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002564 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002565 int ret;
2566
Ilya Dryomov25122d12013-01-20 15:57:57 +02002567 ret = mnt_want_write_file(file);
2568 if (ret)
2569 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002570
Ilya Dryomov25122d12013-01-20 15:57:57 +02002571 if (btrfs_root_readonly(root)) {
2572 ret = -EROFS;
2573 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002574 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002575
2576 switch (inode->i_mode & S_IFMT) {
2577 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002578 if (!capable(CAP_SYS_ADMIN)) {
2579 ret = -EPERM;
2580 goto out;
2581 }
Eric Sandeende78b512013-01-31 18:21:12 +00002582 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002583 break;
2584 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002585 if (!(file->f_mode & FMODE_WRITE)) {
2586 ret = -EINVAL;
2587 goto out;
2588 }
Chris Mason1e701a32010-03-11 09:42:04 -05002589
2590 range = kzalloc(sizeof(*range), GFP_KERNEL);
2591 if (!range) {
2592 ret = -ENOMEM;
2593 goto out;
2594 }
2595
2596 if (argp) {
2597 if (copy_from_user(range, argp,
2598 sizeof(*range))) {
2599 ret = -EFAULT;
2600 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002601 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002602 }
2603 /* compression requires us to start the IO */
2604 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2605 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2606 range->extent_thresh = (u32)-1;
2607 }
2608 } else {
2609 /* the rest are all set to zero by kzalloc */
2610 range->len = (u64)-1;
2611 }
Al Viro496ad9a2013-01-23 17:07:38 -05002612 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002613 range, 0, 0);
2614 if (ret > 0)
2615 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002616 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002617 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002618 default:
2619 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002620 }
Chris Masone441d542009-01-05 16:57:23 -05002621out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002622 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002623 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002624}
2625
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002626static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002627{
2628 struct btrfs_ioctl_vol_args *vol_args;
2629 int ret;
2630
Chris Masone441d542009-01-05 16:57:23 -05002631 if (!capable(CAP_SYS_ADMIN))
2632 return -EPERM;
2633
David Sterba171938e2017-03-28 14:44:21 +02002634 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002635 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002636
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002637 mutex_lock(&fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002638 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002639 if (IS_ERR(vol_args)) {
2640 ret = PTR_ERR(vol_args);
2641 goto out;
2642 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002643
Mark Fasheh5516e592008-07-24 12:20:14 -04002644 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002645 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002646
Anand Jain43d20762014-07-01 00:58:56 +08002647 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002648 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002649
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002650 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002651out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002652 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02002653 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002654 return ret;
2655}
2656
Anand Jain6b526ed2016-02-13 10:01:39 +08002657static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002658{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002659 struct inode *inode = file_inode(file);
2660 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002661 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002662 int ret;
2663
Chris Masone441d542009-01-05 16:57:23 -05002664 if (!capable(CAP_SYS_ADMIN))
2665 return -EPERM;
2666
Miao Xieda249272012-11-26 08:44:50 +00002667 ret = mnt_want_write_file(file);
2668 if (ret)
2669 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002670
Li Zefandae7b662009-04-08 15:06:54 +08002671 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002672 if (IS_ERR(vol_args)) {
2673 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002674 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002675 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002676
Anand Jain6b526ed2016-02-13 10:01:39 +08002677 /* Check for compatibility reject unknown flags */
David Sterba735654e2016-02-15 18:15:21 +01002678 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2679 return -EOPNOTSUPP;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002680
David Sterba171938e2017-03-28 14:44:21 +02002681 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002682 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2683 goto out;
2684 }
2685
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002686 mutex_lock(&fs_info->volume_mutex);
David Sterba735654e2016-02-15 18:15:21 +01002687 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002688 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002689 } else {
2690 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002691 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002692 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002693 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02002694 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002695
Anand Jain6b526ed2016-02-13 10:01:39 +08002696 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002697 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002698 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002699 vol_args->devid);
2700 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002701 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002702 vol_args->name);
2703 }
Anand Jain183860f2013-05-17 10:52:45 +00002704out:
2705 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002706err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002707 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002708 return ret;
2709}
2710
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002711static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002713 struct inode *inode = file_inode(file);
2714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002715 struct btrfs_ioctl_vol_args *vol_args;
2716 int ret;
2717
2718 if (!capable(CAP_SYS_ADMIN))
2719 return -EPERM;
2720
2721 ret = mnt_want_write_file(file);
2722 if (ret)
2723 return ret;
2724
David Sterba171938e2017-03-28 14:44:21 +02002725 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002726 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002727 goto out_drop_write;
2728 }
2729
2730 vol_args = memdup_user(arg, sizeof(*vol_args));
2731 if (IS_ERR(vol_args)) {
2732 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002733 goto out;
2734 }
2735
David Sterba58d7bbf2016-05-04 14:10:47 +02002736 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002737 mutex_lock(&fs_info->volume_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002738 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002739 mutex_unlock(&fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002740
2741 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002742 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002743 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002744out:
David Sterba171938e2017-03-28 14:44:21 +02002745 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002746out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002747 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002748
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002749 return ret;
2750}
2751
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002752static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2753 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002754{
Li Zefan027ed2f2011-06-08 08:27:56 +00002755 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002756 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002757 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002758 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002759
Li Zefan027ed2f2011-06-08 08:27:56 +00002760 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2761 if (!fi_args)
2762 return -ENOMEM;
2763
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002764 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002765 fi_args->num_devices = fs_devices->num_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002766 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002767
Byongho Leed7641a42015-09-01 23:10:57 +09002768 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002769 if (device->devid > fi_args->max_id)
2770 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002771 }
2772 mutex_unlock(&fs_devices->device_list_mutex);
2773
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002774 fi_args->nodesize = fs_info->super_copy->nodesize;
2775 fi_args->sectorsize = fs_info->super_copy->sectorsize;
2776 fi_args->clone_alignment = fs_info->super_copy->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002777
Li Zefan027ed2f2011-06-08 08:27:56 +00002778 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2779 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002780
Li Zefan027ed2f2011-06-08 08:27:56 +00002781 kfree(fi_args);
2782 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002783}
2784
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002785static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2786 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002787{
2788 struct btrfs_ioctl_dev_info_args *di_args;
2789 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002790 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002791 int ret = 0;
2792 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002793
Jan Schmidt475f6382011-03-11 15:41:01 +01002794 di_args = memdup_user(arg, sizeof(*di_args));
2795 if (IS_ERR(di_args))
2796 return PTR_ERR(di_args);
2797
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002798 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002799 s_uuid = di_args->uuid;
2800
2801 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002802 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002803
2804 if (!dev) {
2805 ret = -ENODEV;
2806 goto out;
2807 }
2808
2809 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002810 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2811 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002812 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002813 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002814 struct rcu_string *name;
2815
2816 rcu_read_lock();
2817 name = rcu_dereference(dev->name);
2818 strncpy(di_args->path, name->str, sizeof(di_args->path));
2819 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002820 di_args->path[sizeof(di_args->path) - 1] = 0;
2821 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002822 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002823 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002824
2825out:
David Sterba55793c02013-04-26 15:20:23 +00002826 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002827 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2828 ret = -EFAULT;
2829
2830 kfree(di_args);
2831 return ret;
2832}
2833
Mark Fashehf4414602015-06-30 14:42:05 -07002834static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002835{
2836 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002837
Mark Fasheh416161d2013-08-06 11:42:51 -07002838 page = grab_cache_page(inode->i_mapping, index);
2839 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002840 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002841
2842 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002843 int ret;
2844
2845 ret = btrfs_readpage(NULL, page);
2846 if (ret)
2847 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002848 lock_page(page);
2849 if (!PageUptodate(page)) {
2850 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002851 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002852 return ERR_PTR(-EIO);
2853 }
2854 if (page->mapping != inode->i_mapping) {
2855 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002856 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002857 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002858 }
2859 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002860
2861 return page;
2862}
2863
Mark Fashehf4414602015-06-30 14:42:05 -07002864static int gather_extent_pages(struct inode *inode, struct page **pages,
2865 int num_pages, u64 off)
2866{
2867 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002868 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002869
2870 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002871again:
Mark Fashehf4414602015-06-30 14:42:05 -07002872 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002873 if (IS_ERR(pages[i])) {
2874 int err = PTR_ERR(pages[i]);
2875
2876 if (err == -EAGAIN)
2877 goto again;
2878 pages[i] = NULL;
2879 return err;
2880 }
Mark Fashehf4414602015-06-30 14:42:05 -07002881 }
2882 return 0;
2883}
2884
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002885static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2886 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002887{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002888 /*
2889 * Do any pending delalloc/csum calculations on inode, one way or
2890 * another, and lock file content.
2891 * The locking order is:
2892 *
2893 * 1) pages
2894 * 2) range in the inode's io tree
2895 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002896 while (1) {
2897 struct btrfs_ordered_extent *ordered;
2898 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2899 ordered = btrfs_lookup_first_ordered_extent(inode,
2900 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002901 if ((!ordered ||
2902 ordered->file_offset + ordered->len <= off ||
2903 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002904 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002905 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2906 if (ordered)
2907 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002908 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002909 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002910 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2911 if (ordered)
2912 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002913 if (!retry_range_locking)
2914 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002915 btrfs_wait_ordered_range(inode, off, len);
2916 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002917 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002918}
2919
Mark Fashehf4414602015-06-30 14:42:05 -07002920static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002921{
Al Viro59551022016-01-22 15:40:57 -05002922 inode_unlock(inode1);
2923 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002924}
2925
Mark Fashehf4414602015-06-30 14:42:05 -07002926static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2927{
2928 if (inode1 < inode2)
2929 swap(inode1, inode2);
2930
Al Viro59551022016-01-22 15:40:57 -05002931 inode_lock_nested(inode1, I_MUTEX_PARENT);
2932 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002933}
2934
2935static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2936 struct inode *inode2, u64 loff2, u64 len)
2937{
2938 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2939 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2940}
2941
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002942static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2943 struct inode *inode2, u64 loff2, u64 len,
2944 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002945{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002946 int ret;
2947
Mark Fasheh416161d2013-08-06 11:42:51 -07002948 if (inode1 < inode2) {
2949 swap(inode1, inode2);
2950 swap(loff1, loff2);
2951 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002952 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2953 if (ret)
2954 return ret;
2955 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2956 if (ret)
2957 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2958 loff1 + len - 1);
2959 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002960}
2961
2962struct cmp_pages {
2963 int num_pages;
2964 struct page **src_pages;
2965 struct page **dst_pages;
2966};
2967
2968static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2969{
2970 int i;
2971 struct page *pg;
2972
2973 for (i = 0; i < cmp->num_pages; i++) {
2974 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002975 if (pg) {
2976 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002977 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002978 }
Mark Fashehf4414602015-06-30 14:42:05 -07002979 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002980 if (pg) {
2981 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002982 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002983 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002984 }
Mark Fashehf4414602015-06-30 14:42:05 -07002985 kfree(cmp->src_pages);
2986 kfree(cmp->dst_pages);
2987}
2988
2989static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2990 struct inode *dst, u64 dst_loff,
2991 u64 len, struct cmp_pages *cmp)
2992{
2993 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002994 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002995 struct page **src_pgarr, **dst_pgarr;
2996
2997 /*
2998 * We must gather up all the pages before we initiate our
2999 * extent locking. We use an array for the page pointers. Size
3000 * of the array is bounded by len, which is in turn bounded by
3001 * BTRFS_MAX_DEDUPE_LEN.
3002 */
David Sterba66722f72016-02-11 15:01:38 +01003003 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
3004 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07003005 if (!src_pgarr || !dst_pgarr) {
3006 kfree(src_pgarr);
3007 kfree(dst_pgarr);
3008 return -ENOMEM;
3009 }
3010 cmp->num_pages = num_pages;
3011 cmp->src_pages = src_pgarr;
3012 cmp->dst_pages = dst_pgarr;
3013
Filipe Mananab1517622017-02-21 17:14:52 +00003014 /*
3015 * If deduping ranges in the same inode, locking rules make it mandatory
3016 * to always lock pages in ascending order to avoid deadlocks with
3017 * concurrent tasks (such as starting writeback/delalloc).
3018 */
3019 if (src == dst && dst_loff < loff) {
3020 swap(src_pgarr, dst_pgarr);
3021 swap(loff, dst_loff);
3022 }
3023
3024 ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003025 if (ret)
3026 goto out;
3027
Filipe Mananab1517622017-02-21 17:14:52 +00003028 ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003029
3030out:
3031 if (ret)
3032 btrfs_cmp_data_free(cmp);
3033 return 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003034}
3035
David Sterba1a287cf2017-02-10 20:15:10 +01003036static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003037{
3038 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003039 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003040 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003041 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003042 void *addr, *dst_addr;
3043
Mark Fashehf4414602015-06-30 14:42:05 -07003044 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003045 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003046 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003047 cmp_len = len;
3048
Mark Fashehf4414602015-06-30 14:42:05 -07003049 BUG_ON(i >= cmp->num_pages);
3050
3051 src_page = cmp->src_pages[i];
3052 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003053 ASSERT(PageLocked(src_page));
3054 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003055
Mark Fasheh416161d2013-08-06 11:42:51 -07003056 addr = kmap_atomic(src_page);
3057 dst_addr = kmap_atomic(dst_page);
3058
3059 flush_dcache_page(src_page);
3060 flush_dcache_page(dst_page);
3061
3062 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003063 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003064
3065 kunmap_atomic(addr);
3066 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003067
3068 if (ret)
3069 break;
3070
Mark Fasheh416161d2013-08-06 11:42:51 -07003071 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003072 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003073 }
3074
3075 return ret;
3076}
3077
Mark Fashehe1d227a2015-06-08 15:05:25 -07003078static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3079 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003080{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003081 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003082 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3083
Mark Fashehe1d227a2015-06-08 15:05:25 -07003084 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003085 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003086
3087 /* if we extend to eof, continue to block boundary */
3088 if (off + len == inode->i_size)
3089 *plen = len = ALIGN(inode->i_size, bs) - off;
3090
Mark Fasheh416161d2013-08-06 11:42:51 -07003091 /* Check that we are block aligned - btrfs_clone() requires this */
3092 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3093 return -EINVAL;
3094
3095 return 0;
3096}
3097
Mark Fashehe1d227a2015-06-08 15:05:25 -07003098static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003099 struct inode *dst, u64 dst_loff)
3100{
3101 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003102 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003103 struct cmp_pages cmp;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003104 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003105 u64 same_lock_start = 0;
3106 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003107
Filipe Manana113e8282015-03-30 18:26:47 +01003108 if (len == 0)
3109 return 0;
3110
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003111 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003112 inode_lock(src);
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003113 else
3114 btrfs_double_inode_lock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003115
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003116 ret = extent_same_check_offsets(src, loff, &len, olen);
3117 if (ret)
3118 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003119
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003120 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3121 if (ret)
3122 goto out_unlock;
3123
3124 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003125 /*
3126 * Single inode case wants the same checks, except we
3127 * don't want our length pushed out past i_size as
3128 * comparing that data range makes no sense.
3129 *
3130 * extent_same_check_offsets() will do this for an
3131 * unaligned length at i_size, so catch it here and
3132 * reject the request.
3133 *
3134 * This effectively means we require aligned extents
3135 * for the single-inode case, whereas the other cases
3136 * allow an unaligned length so long as it ends at
3137 * i_size.
3138 */
3139 if (len != olen) {
3140 ret = -EINVAL;
3141 goto out_unlock;
3142 }
3143
3144 /* Check for overlapping ranges */
3145 if (dst_loff + len > loff && dst_loff < loff + len) {
3146 ret = -EINVAL;
3147 goto out_unlock;
3148 }
3149
3150 same_lock_start = min_t(u64, loff, dst_loff);
3151 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003152 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003153
3154 /* don't make the dst file partly checksummed */
3155 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3156 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3157 ret = -EINVAL;
3158 goto out_unlock;
3159 }
3160
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003161again:
Mark Fashehf4414602015-06-30 14:42:05 -07003162 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3163 if (ret)
3164 goto out_unlock;
3165
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003166 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003167 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3168 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003169 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003170 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3171 false);
3172 /*
3173 * If one of the inodes has dirty pages in the respective range or
3174 * ordered extents, we need to flush dellaloc and wait for all ordered
3175 * extents in the range. We must unlock the pages and the ranges in the
3176 * io trees to avoid deadlocks when flushing delalloc (requires locking
3177 * pages) and when waiting for ordered extents to complete (they require
3178 * range locking).
3179 */
3180 if (ret == -EAGAIN) {
3181 /*
3182 * Ranges in the io trees already unlocked. Now unlock all
3183 * pages before waiting for all IO to complete.
3184 */
3185 btrfs_cmp_data_free(&cmp);
3186 if (same_inode) {
3187 btrfs_wait_ordered_range(src, same_lock_start,
3188 same_lock_len);
3189 } else {
3190 btrfs_wait_ordered_range(src, loff, len);
3191 btrfs_wait_ordered_range(dst, dst_loff, len);
3192 }
3193 goto again;
3194 }
3195 ASSERT(ret == 0);
3196 if (WARN_ON(ret)) {
3197 /* ranges in the io trees already unlocked */
3198 btrfs_cmp_data_free(&cmp);
3199 return ret;
3200 }
Mark Fashehf4414602015-06-30 14:42:05 -07003201
Mark Fasheh207910d2015-06-30 14:42:04 -07003202 /* pass original length for comparison so we stay within i_size */
David Sterba1a287cf2017-02-10 20:15:10 +01003203 ret = btrfs_cmp_data(olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003204 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003205 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003206
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003207 if (same_inode)
3208 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3209 same_lock_start + same_lock_len - 1);
3210 else
3211 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003212
3213 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003214out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003215 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003216 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003217 else
3218 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003219
3220 return ret;
3221}
3222
Byongho Leeee221842015-12-15 01:42:10 +09003223#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003224
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003225ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3226 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003227{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003228 struct inode *src = file_inode(src_file);
3229 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003230 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003231 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003232
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003233 if (olen > BTRFS_MAX_DEDUPE_LEN)
3234 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003235
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003236 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003237 /*
3238 * Btrfs does not support blocksize < page_size. As a
3239 * result, btrfs_cmp_data() won't correctly handle
3240 * this situation without an update.
3241 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003242 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003243 }
3244
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003245 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3246 if (res)
3247 return res;
3248 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003249}
3250
Filipe Mananaf82a9902014-06-01 01:50:28 +01003251static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3252 struct inode *inode,
3253 u64 endoff,
3254 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003255 const u64 olen,
3256 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003257{
3258 struct btrfs_root *root = BTRFS_I(inode)->root;
3259 int ret;
3260
3261 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003262 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003263 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003264 /*
3265 * We round up to the block size at eof when determining which
3266 * extents to clone above, but shouldn't round up the file size.
3267 */
3268 if (endoff > destoff + olen)
3269 endoff = destoff + olen;
3270 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003271 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003272
3273 ret = btrfs_update_inode(trans, root, inode);
3274 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003275 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003276 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003277 goto out;
3278 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003279 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003280out:
3281 return ret;
3282}
3283
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003284static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003285 const struct btrfs_trans_handle *trans,
3286 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003287 const u64 hole_offset,
3288 const u64 hole_len)
3289{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003290 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003291 struct extent_map *em;
3292 int ret;
3293
3294 em = alloc_extent_map();
3295 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003296 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003297 return;
3298 }
3299
Filipe Manana14f59792014-06-29 21:45:40 +01003300 if (path) {
3301 struct btrfs_file_extent_item *fi;
3302
3303 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3304 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003305 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003306 em->generation = -1;
3307 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3308 BTRFS_FILE_EXTENT_INLINE)
3309 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003310 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003311 } else {
3312 em->start = hole_offset;
3313 em->len = hole_len;
3314 em->ram_bytes = em->len;
3315 em->orig_start = hole_offset;
3316 em->block_start = EXTENT_MAP_HOLE;
3317 em->block_len = 0;
3318 em->orig_block_len = 0;
3319 em->compress_type = BTRFS_COMPRESS_NONE;
3320 em->generation = trans->transid;
3321 }
3322
3323 while (1) {
3324 write_lock(&em_tree->lock);
3325 ret = add_extent_mapping(em_tree, em, 1);
3326 write_unlock(&em_tree->lock);
3327 if (ret != -EEXIST) {
3328 free_extent_map(em);
3329 break;
3330 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003331 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003332 em->start + em->len - 1, 0);
3333 }
3334
David Sterbaee39b432014-09-30 01:33:33 +02003335 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003336 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003337}
3338
Filipe Manana8039d872015-10-13 15:15:00 +01003339/*
3340 * Make sure we do not end up inserting an inline extent into a file that has
3341 * already other (non-inline) extents. If a file has an inline extent it can
3342 * not have any other extents and the (single) inline extent must start at the
3343 * file offset 0. Failing to respect these rules will lead to file corruption,
3344 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3345 *
3346 * We can have extents that have been already written to disk or we can have
3347 * dirty ranges still in delalloc, in which case the extent maps and items are
3348 * created only when we run delalloc, and the delalloc ranges might fall outside
3349 * the range we are currently locking in the inode's io tree. So we check the
3350 * inode's i_size because of that (i_size updates are done while holding the
3351 * i_mutex, which we are holding here).
3352 * We also check to see if the inode has a size not greater than "datal" but has
3353 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3354 * protected against such concurrent fallocate calls by the i_mutex).
3355 *
3356 * If the file has no extents but a size greater than datal, do not allow the
3357 * copy because we would need turn the inline extent into a non-inline one (even
3358 * with NO_HOLES enabled). If we find our destination inode only has one inline
3359 * extent, just overwrite it with the source inline extent if its size is less
3360 * than the source extent's size, or we could copy the source inline extent's
3361 * data into the destination inode's inline extent if the later is greater then
3362 * the former.
3363 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003364static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003365 struct btrfs_trans_handle *trans,
3366 struct btrfs_path *path,
3367 struct btrfs_key *new_key,
3368 const u64 drop_start,
3369 const u64 datal,
3370 const u64 skip,
3371 const u64 size,
3372 char *inline_data)
3373{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003374 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003375 struct btrfs_root *root = BTRFS_I(dst)->root;
3376 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003377 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003378 int ret;
3379 struct btrfs_key key;
3380
3381 if (new_key->offset > 0)
3382 return -EOPNOTSUPP;
3383
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003384 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003385 key.type = BTRFS_EXTENT_DATA_KEY;
3386 key.offset = 0;
3387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3388 if (ret < 0) {
3389 return ret;
3390 } else if (ret > 0) {
3391 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3392 ret = btrfs_next_leaf(root, path);
3393 if (ret < 0)
3394 return ret;
3395 else if (ret > 0)
3396 goto copy_inline_extent;
3397 }
3398 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003399 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003400 key.type == BTRFS_EXTENT_DATA_KEY) {
3401 ASSERT(key.offset > 0);
3402 return -EOPNOTSUPP;
3403 }
3404 } else if (i_size_read(dst) <= datal) {
3405 struct btrfs_file_extent_item *ei;
3406 u64 ext_len;
3407
3408 /*
3409 * If the file size is <= datal, make sure there are no other
3410 * extents following (can happen do to an fallocate call with
3411 * the flag FALLOC_FL_KEEP_SIZE).
3412 */
3413 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3414 struct btrfs_file_extent_item);
3415 /*
3416 * If it's an inline extent, it can not have other extents
3417 * following it.
3418 */
3419 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3420 BTRFS_FILE_EXTENT_INLINE)
3421 goto copy_inline_extent;
3422
3423 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3424 if (ext_len > aligned_end)
3425 return -EOPNOTSUPP;
3426
3427 ret = btrfs_next_item(root, path);
3428 if (ret < 0) {
3429 return ret;
3430 } else if (ret == 0) {
3431 btrfs_item_key_to_cpu(path->nodes[0], &key,
3432 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003433 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003434 key.type == BTRFS_EXTENT_DATA_KEY)
3435 return -EOPNOTSUPP;
3436 }
3437 }
3438
3439copy_inline_extent:
3440 /*
3441 * We have no extent items, or we have an extent at offset 0 which may
3442 * or may not be inlined. All these cases are dealt the same way.
3443 */
3444 if (i_size_read(dst) > datal) {
3445 /*
3446 * If the destination inode has an inline extent...
3447 * This would require copying the data from the source inline
3448 * extent into the beginning of the destination's inline extent.
3449 * But this is really complex, both extents can be compressed
3450 * or just one of them, which would require decompressing and
3451 * re-compressing data (which could increase the new compressed
3452 * size, not allowing the compressed data to fit anymore in an
3453 * inline extent).
3454 * So just don't support this case for now (it should be rare,
3455 * we are not really saving space when cloning inline extents).
3456 */
3457 return -EOPNOTSUPP;
3458 }
3459
3460 btrfs_release_path(path);
3461 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3462 if (ret)
3463 return ret;
3464 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3465 if (ret)
3466 return ret;
3467
3468 if (skip) {
3469 const u32 start = btrfs_file_extent_calc_inline_size(0);
3470
3471 memmove(inline_data + start, inline_data + start + skip, datal);
3472 }
3473
3474 write_extent_buffer(path->nodes[0], inline_data,
3475 btrfs_item_ptr_offset(path->nodes[0],
3476 path->slots[0]),
3477 size);
3478 inode_add_bytes(dst, datal);
3479
3480 return 0;
3481}
3482
Mark Fasheh32b7c682013-08-06 11:42:49 -07003483/**
3484 * btrfs_clone() - clone a range from inode file to another
3485 *
3486 * @src: Inode to clone from
3487 * @inode: Inode to clone to
3488 * @off: Offset within source to start clone from
3489 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003490 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003491 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003492 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003493 */
3494static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003495 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003496 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003497{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003498 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003499 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003500 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003501 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003502 struct btrfs_trans_handle *trans;
3503 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003504 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003505 u32 nritems;
3506 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003507 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003508 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003509 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003510
3511 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003512 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3513 if (!buf)
3514 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003515
3516 path = btrfs_alloc_path();
3517 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003518 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003519 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003520 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003521
David Sterbae4058b52015-11-27 16:31:35 +01003522 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003523 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003524 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003525 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003526 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003527
3528 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003529 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003530
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003531 /*
3532 * note the key will change type as we walk through the
3533 * tree.
3534 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003535 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003536 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3537 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003538 if (ret < 0)
3539 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003540 /*
3541 * First search, if no extent item that starts at offset off was
3542 * found but the previous item is an extent item, it's possible
3543 * it might overlap our target range, therefore process it.
3544 */
3545 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3546 btrfs_item_key_to_cpu(path->nodes[0], &key,
3547 path->slots[0] - 1);
3548 if (key.type == BTRFS_EXTENT_DATA_KEY)
3549 path->slots[0]--;
3550 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003551
Yan Zhengae01a0a2008-08-04 23:23:47 -04003552 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003553process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003554 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003555 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003556 if (ret < 0)
3557 goto out;
3558 if (ret > 0)
3559 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003560 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003561 }
3562 leaf = path->nodes[0];
3563 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003564
Yan Zhengae01a0a2008-08-04 23:23:47 -04003565 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003566 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003567 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003568 break;
3569
David Sterba962a2982014-06-04 18:41:45 +02003570 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003571 struct btrfs_file_extent_item *extent;
3572 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003573 u32 size;
3574 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003575 u64 disko = 0, diskl = 0;
3576 u64 datao = 0, datal = 0;
3577 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003578 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003579
Sage Weilc5c9cd42008-11-12 14:32:25 -05003580 extent = btrfs_item_ptr(leaf, slot,
3581 struct btrfs_file_extent_item);
3582 comp = btrfs_file_extent_compression(leaf, extent);
3583 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003584 if (type == BTRFS_FILE_EXTENT_REG ||
3585 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003586 disko = btrfs_file_extent_disk_bytenr(leaf,
3587 extent);
3588 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3589 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003590 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003591 datal = btrfs_file_extent_num_bytes(leaf,
3592 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003593 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3594 /* take upper bound, may be compressed */
3595 datal = btrfs_file_extent_ram_bytes(leaf,
3596 extent);
3597 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003598
Filipe Manana2c463822014-05-31 02:31:05 +01003599 /*
3600 * The first search might have left us at an extent
3601 * item that ends before our target range's start, can
3602 * happen if we have holes and NO_HOLES feature enabled.
3603 */
3604 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003605 path->slots[0]++;
3606 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003607 } else if (key.offset >= off + len) {
3608 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003609 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003610 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003611 size = btrfs_item_size_nr(leaf, slot);
3612 read_extent_buffer(leaf, buf,
3613 btrfs_item_ptr_offset(leaf, slot),
3614 size);
3615
3616 btrfs_release_path(path);
3617 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003618
Zheng Yan31840ae2008-09-23 13:14:14 -04003619 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003620 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003621 if (off <= key.offset)
3622 new_key.offset = key.offset + destoff - off;
3623 else
3624 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003625
Sage Weilb6f34092011-09-20 14:48:51 -04003626 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003627 * Deal with a hole that doesn't have an extent item
3628 * that represents it (NO_HOLES feature enabled).
3629 * This hole is either in the middle of the cloning
3630 * range or at the beginning (fully overlaps it or
3631 * partially overlaps it).
3632 */
3633 if (new_key.offset != last_dest_end)
3634 drop_start = last_dest_end;
3635 else
3636 drop_start = new_key.offset;
3637
3638 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003639 * 1 - adjusting old extent (we may have to split it)
3640 * 1 - add new extent
3641 * 1 - inode update
3642 */
3643 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003644 if (IS_ERR(trans)) {
3645 ret = PTR_ERR(trans);
3646 goto out;
3647 }
3648
Chris Masonc8a894d2009-06-27 21:07:03 -04003649 if (type == BTRFS_FILE_EXTENT_REG ||
3650 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003651 /*
3652 * a | --- range to clone ---| b
3653 * | ------------- extent ------------- |
3654 */
3655
Antonio Ospite93915582014-06-04 14:03:48 +02003656 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003657 if (key.offset + datal > off + len)
3658 datal = off + len - key.offset;
3659
Antonio Ospite93915582014-06-04 14:03:48 +02003660 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003661 if (off > key.offset) {
3662 datao += off - key.offset;
3663 datal -= off - key.offset;
3664 }
3665
Josef Bacik5dc562c2012-08-17 13:14:17 -04003666 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003667 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003669 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003670 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003671 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003672 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003673 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003674 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003675 goto out;
3676 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003677
Sage Weilc5c9cd42008-11-12 14:32:25 -05003678 ret = btrfs_insert_empty_item(trans, root, path,
3679 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003680 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003681 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003682 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003683 goto out;
3684 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003685
3686 leaf = path->nodes[0];
3687 slot = path->slots[0];
3688 write_extent_buffer(leaf, buf,
3689 btrfs_item_ptr_offset(leaf, slot),
3690 size);
3691
3692 extent = btrfs_item_ptr(leaf, slot,
3693 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003694
Sage Weilc5c9cd42008-11-12 14:32:25 -05003695 /* disko == 0 means it's a hole */
3696 if (!disko)
3697 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003698
3699 btrfs_set_file_extent_offset(leaf, extent,
3700 datao);
3701 btrfs_set_file_extent_num_bytes(leaf, extent,
3702 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003703
Sage Weilc5c9cd42008-11-12 14:32:25 -05003704 if (disko) {
3705 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003706 ret = btrfs_inc_extent_ref(trans,
3707 fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003708 disko, diskl, 0,
3709 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003710 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003711 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003712 if (ret) {
3713 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003714 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003715 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003716 goto out;
3717
3718 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003719 }
3720 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3721 u64 skip = 0;
3722 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003723
Sage Weilc5c9cd42008-11-12 14:32:25 -05003724 if (off > key.offset) {
3725 skip = off - key.offset;
3726 new_key.offset += skip;
3727 }
Chris Masond3977122009-01-05 21:25:51 -05003728
Liu Boaa42ffd2012-09-18 03:52:23 -06003729 if (key.offset + datal > off + len)
3730 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003731
Sage Weilc5c9cd42008-11-12 14:32:25 -05003732 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003733 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003734 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003735 goto out;
3736 }
3737 size -= skip + trim;
3738 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739
David Sterba4a0ab9d2017-02-10 20:18:49 +01003740 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003741 trans, path,
3742 &new_key,
3743 drop_start,
3744 datal,
3745 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003746 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003747 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003748 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003749 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003750 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003751 goto out;
3752 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003753 leaf = path->nodes[0];
3754 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003755 }
3756
Filipe Manana7ffbb592014-06-09 03:48:05 +01003757 /* If we have an implicit hole (NO_HOLES feature). */
3758 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003759 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003760 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003761 new_key.offset - drop_start);
3762
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003763 clone_update_extent_map(BTRFS_I(inode), trans,
3764 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003765
Sage Weilc5c9cd42008-11-12 14:32:25 -05003766 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003767 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003768
Filipe Manana62e23902014-08-08 02:47:06 +01003769 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003770 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003771 ret = clone_finish_inode_update(trans, inode,
3772 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003773 destoff, olen,
3774 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003775 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003776 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003777 if (new_key.offset + datal >= destoff + len)
3778 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003779 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003780 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003781 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003782
3783 if (fatal_signal_pending(current)) {
3784 ret = -EINTR;
3785 goto out;
3786 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003787 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003788 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003789
Filipe Mananaf82a9902014-06-01 01:50:28 +01003790 if (last_dest_end < destoff + len) {
3791 /*
3792 * We have an implicit hole (NO_HOLES feature is enabled) that
3793 * fully or partially overlaps our cloning range at its end.
3794 */
3795 btrfs_release_path(path);
3796
3797 /*
3798 * 1 - remove extent(s)
3799 * 1 - inode update
3800 */
3801 trans = btrfs_start_transaction(root, 2);
3802 if (IS_ERR(trans)) {
3803 ret = PTR_ERR(trans);
3804 goto out;
3805 }
3806 ret = btrfs_drop_extents(trans, root, inode,
3807 last_dest_end, destoff + len, 1);
3808 if (ret) {
3809 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003810 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003811 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003812 goto out;
3813 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003814 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3815 last_dest_end,
3816 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003817 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003818 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003819 }
3820
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003821out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003822 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003823 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003824 return ret;
3825}
3826
Zach Brown3db11b22015-11-10 16:53:32 -05003827static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3828 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003829{
Al Viro54563d42013-09-01 15:57:51 -04003830 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003831 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003832 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003833 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003834 int ret;
3835 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003836 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003837 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003838
3839 /*
3840 * TODO:
3841 * - split compressed inline extents. annoying: we need to
3842 * decompress into destination's address_space (the file offset
3843 * may change, so source mapping won't do), then recompress (or
3844 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003845 *
3846 * - split destination inode's inline extents. The inline extents can
3847 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003848 */
3849
Mark Fasheh32b7c682013-08-06 11:42:49 -07003850 if (btrfs_root_readonly(root))
3851 return -EROFS;
3852
Zach Brown3db11b22015-11-10 16:53:32 -05003853 if (file_src->f_path.mnt != file->f_path.mnt ||
3854 src->i_sb != inode->i_sb)
3855 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003856
3857 /* don't make the dst file partly checksummed */
3858 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3859 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003860 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003861
Mark Fasheh32b7c682013-08-06 11:42:49 -07003862 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003863 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003864
3865 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003866 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003867 } else {
Al Viro59551022016-01-22 15:40:57 -05003868 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003869 }
3870
3871 /* determine range to clone */
3872 ret = -EINVAL;
3873 if (off + len > src->i_size || off + len < off)
3874 goto out_unlock;
3875 if (len == 0)
3876 olen = len = src->i_size - off;
3877 /* if we extend to eof, continue to block boundary */
3878 if (off + len == src->i_size)
3879 len = ALIGN(src->i_size, bs) - off;
3880
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003881 if (len == 0) {
3882 ret = 0;
3883 goto out_unlock;
3884 }
3885
Mark Fasheh32b7c682013-08-06 11:42:49 -07003886 /* verify the end result is block aligned */
3887 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3888 !IS_ALIGNED(destoff, bs))
3889 goto out_unlock;
3890
3891 /* verify if ranges are overlapped within the same file */
3892 if (same_inode) {
3893 if (destoff + len > off && destoff < off + len)
3894 goto out_unlock;
3895 }
3896
3897 if (destoff > inode->i_size) {
3898 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3899 if (ret)
3900 goto out_unlock;
3901 }
3902
Filipe Mananac125b8b2014-05-23 05:03:34 +01003903 /*
3904 * Lock the target range too. Right after we replace the file extent
3905 * items in the fs tree (which now point to the cloned data), we might
3906 * have a worker replace them with extent items relative to a write
3907 * operation that was issued before this clone operation (i.e. confront
3908 * with inode.c:btrfs_finish_ordered_io).
3909 */
3910 if (same_inode) {
3911 u64 lock_start = min_t(u64, off, destoff);
3912 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003913
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003914 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003915 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003916 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3917 true);
3918 }
3919 ASSERT(ret == 0);
3920 if (WARN_ON(ret)) {
3921 /* ranges in the io trees already unlocked */
3922 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003923 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003924
Mark Fasheh1c919a52015-06-30 14:42:08 -07003925 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003926
Filipe Mananac125b8b2014-05-23 05:03:34 +01003927 if (same_inode) {
3928 u64 lock_start = min_t(u64, off, destoff);
3929 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3930
3931 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3932 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003933 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003934 }
3935 /*
3936 * Truncate page cache pages so that future reads will see the cloned
3937 * data immediately and not the previous data.
3938 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303939 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003940 round_down(destoff, PAGE_SIZE),
3941 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003942out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003943 if (!same_inode)
3944 btrfs_double_inode_unlock(src, inode);
3945 else
Al Viro59551022016-01-22 15:40:57 -05003946 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003947 return ret;
3948}
3949
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003950int btrfs_clone_file_range(struct file *src_file, loff_t off,
3951 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003952{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003953 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003954}
3955
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003956/*
3957 * there are many ways the trans_start and trans_end ioctls can lead
3958 * to deadlocks. They should only be used by applications that
3959 * basically own the machine, and have a very in depth understanding
3960 * of all the possible deadlocks and enospc problems.
3961 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003962static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003963{
Al Viro496ad9a2013-01-23 17:07:38 -05003964 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003965 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003966 struct btrfs_root *root = BTRFS_I(inode)->root;
3967 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003968 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003969
Sage Weil1ab86ae2009-09-29 18:38:44 -04003970 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003971 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003972 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003973
3974 ret = -EINPROGRESS;
3975 if (file->private_data)
3976 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003977
Li Zefanb83cc962010-12-20 16:04:08 +08003978 ret = -EROFS;
3979 if (btrfs_root_readonly(root))
3980 goto out;
3981
Al Viroa561be72011-11-23 11:57:51 -05003982 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003983 if (ret)
3984 goto out;
3985
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003986 atomic_inc(&fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003987
Sage Weil1ab86ae2009-09-29 18:38:44 -04003988 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003989 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003990 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003991 goto out_drop;
3992
3993 file->private_data = trans;
3994 return 0;
3995
3996out_drop:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003997 atomic_dec(&fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003998 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003999out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004000 return ret;
4001}
4002
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004003static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4004{
Al Viro496ad9a2013-01-23 17:07:38 -05004005 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004006 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004007 struct btrfs_root *root = BTRFS_I(inode)->root;
4008 struct btrfs_root *new_root;
4009 struct btrfs_dir_item *di;
4010 struct btrfs_trans_handle *trans;
4011 struct btrfs_path *path;
4012 struct btrfs_key location;
4013 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004014 u64 objectid = 0;
4015 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004016 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004017
4018 if (!capable(CAP_SYS_ADMIN))
4019 return -EPERM;
4020
Miao Xie3c04ce02012-11-26 08:43:07 +00004021 ret = mnt_want_write_file(file);
4022 if (ret)
4023 return ret;
4024
4025 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4026 ret = -EFAULT;
4027 goto out;
4028 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004029
4030 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304031 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004032
4033 location.objectid = objectid;
4034 location.type = BTRFS_ROOT_ITEM_KEY;
4035 location.offset = (u64)-1;
4036
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004037 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004038 if (IS_ERR(new_root)) {
4039 ret = PTR_ERR(new_root);
4040 goto out;
4041 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004042
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004043 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004044 if (!path) {
4045 ret = -ENOMEM;
4046 goto out;
4047 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004048 path->leave_spinning = 1;
4049
4050 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004051 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004052 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004053 ret = PTR_ERR(trans);
4054 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004055 }
4056
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004057 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4058 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004059 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004060 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004061 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004062 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004063 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004064 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004065 ret = -ENOENT;
4066 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004067 }
4068
4069 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4070 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4071 btrfs_mark_buffer_dirty(path->nodes[0]);
4072 btrfs_free_path(path);
4073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004074 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004075 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004076out:
4077 mnt_drop_write_file(file);
4078 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004079}
4080
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004081void btrfs_get_block_group_info(struct list_head *groups_list,
4082 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004083{
4084 struct btrfs_block_group_cache *block_group;
4085
4086 space->total_bytes = 0;
4087 space->used_bytes = 0;
4088 space->flags = 0;
4089 list_for_each_entry(block_group, groups_list, list) {
4090 space->flags = block_group->flags;
4091 space->total_bytes += block_group->key.offset;
4092 space->used_bytes +=
4093 btrfs_block_group_used(&block_group->item);
4094 }
4095}
4096
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004097static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4098 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004099{
4100 struct btrfs_ioctl_space_args space_args;
4101 struct btrfs_ioctl_space_info space;
4102 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004103 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004104 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004105 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004106 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4107 BTRFS_BLOCK_GROUP_SYSTEM,
4108 BTRFS_BLOCK_GROUP_METADATA,
4109 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4110 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004111 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004112 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004113 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004114 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004115
4116 if (copy_from_user(&space_args,
4117 (struct btrfs_ioctl_space_args __user *)arg,
4118 sizeof(space_args)))
4119 return -EFAULT;
4120
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004121 for (i = 0; i < num_types; i++) {
4122 struct btrfs_space_info *tmp;
4123
4124 info = NULL;
4125 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004126 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004127 list) {
4128 if (tmp->flags == types[i]) {
4129 info = tmp;
4130 break;
4131 }
4132 }
4133 rcu_read_unlock();
4134
4135 if (!info)
4136 continue;
4137
4138 down_read(&info->groups_sem);
4139 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4140 if (!list_empty(&info->block_groups[c]))
4141 slot_count++;
4142 }
4143 up_read(&info->groups_sem);
4144 }
Josef Bacik1406e432010-01-13 18:19:06 +00004145
David Sterba36523e952014-02-07 14:34:12 +01004146 /*
4147 * Global block reserve, exported as a space_info
4148 */
4149 slot_count++;
4150
Chris Mason7fde62b2010-03-16 15:40:10 -04004151 /* space_slots == 0 means they are asking for a count */
4152 if (space_args.space_slots == 0) {
4153 space_args.total_spaces = slot_count;
4154 goto out;
4155 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004156
Dan Rosenberg51788b12011-02-14 16:04:23 -05004157 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004158
Chris Mason7fde62b2010-03-16 15:40:10 -04004159 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004160
Chris Mason7fde62b2010-03-16 15:40:10 -04004161 /* we generally have at most 6 or so space infos, one for each raid
4162 * level. So, a whole page should be more than enough for everyone
4163 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004164 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004165 return -ENOMEM;
4166
4167 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004168 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004169 if (!dest)
4170 return -ENOMEM;
4171 dest_orig = dest;
4172
4173 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004174 for (i = 0; i < num_types; i++) {
4175 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004176
Dan Rosenberg51788b12011-02-14 16:04:23 -05004177 if (!slot_count)
4178 break;
4179
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004180 info = NULL;
4181 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004182 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004183 list) {
4184 if (tmp->flags == types[i]) {
4185 info = tmp;
4186 break;
4187 }
4188 }
4189 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004190
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004191 if (!info)
4192 continue;
4193 down_read(&info->groups_sem);
4194 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4195 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004196 btrfs_get_block_group_info(
4197 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004198 memcpy(dest, &space, sizeof(space));
4199 dest++;
4200 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004201 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004202 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004203 if (!slot_count)
4204 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004205 }
4206 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004207 }
Josef Bacik1406e432010-01-13 18:19:06 +00004208
David Sterba36523e952014-02-07 14:34:12 +01004209 /*
4210 * Add global block reserve
4211 */
4212 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004213 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004214
4215 spin_lock(&block_rsv->lock);
4216 space.total_bytes = block_rsv->size;
4217 space.used_bytes = block_rsv->size - block_rsv->reserved;
4218 spin_unlock(&block_rsv->lock);
4219 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4220 memcpy(dest, &space, sizeof(space));
4221 space_args.total_spaces++;
4222 }
4223
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004224 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004225 (arg + sizeof(struct btrfs_ioctl_space_args));
4226
4227 if (copy_to_user(user_dest, dest_orig, alloc_size))
4228 ret = -EFAULT;
4229
4230 kfree(dest_orig);
4231out:
4232 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004233 ret = -EFAULT;
4234
4235 return ret;
4236}
4237
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004238/*
4239 * there are many ways the trans_start and trans_end ioctls can lead
4240 * to deadlocks. They should only be used by applications that
4241 * basically own the machine, and have a very in depth understanding
4242 * of all the possible deadlocks and enospc problems.
4243 */
4244long btrfs_ioctl_trans_end(struct file *file)
4245{
Al Viro496ad9a2013-01-23 17:07:38 -05004246 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004247 struct btrfs_root *root = BTRFS_I(inode)->root;
4248 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004249
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004250 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004251 if (!trans)
4252 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004253 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004254
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004255 btrfs_end_transaction(trans);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004256
Josef Bacika4abeea2011-04-11 17:25:13 -04004257 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004258
Al Viro2a79f172011-12-09 08:06:57 -05004259 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004260 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004261}
4262
Miao Xie9a8c28b2012-11-26 08:40:43 +00004263static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4264 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004265{
Sage Weil46204592010-10-29 15:41:32 -04004266 struct btrfs_trans_handle *trans;
4267 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004268 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004269
Miao Xied4edf392013-02-20 09:17:06 +00004270 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004271 if (IS_ERR(trans)) {
4272 if (PTR_ERR(trans) != -ENOENT)
4273 return PTR_ERR(trans);
4274
4275 /* No running transaction, don't bother */
4276 transid = root->fs_info->last_trans_committed;
4277 goto out;
4278 }
Sage Weil46204592010-10-29 15:41:32 -04004279 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004280 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004281 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004282 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004283 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004284 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004285out:
Sage Weil46204592010-10-29 15:41:32 -04004286 if (argp)
4287 if (copy_to_user(argp, &transid, sizeof(transid)))
4288 return -EFAULT;
4289 return 0;
4290}
4291
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004292static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004293 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004294{
Sage Weil46204592010-10-29 15:41:32 -04004295 u64 transid;
4296
4297 if (argp) {
4298 if (copy_from_user(&transid, argp, sizeof(transid)))
4299 return -EFAULT;
4300 } else {
4301 transid = 0; /* current trans */
4302 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004303 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004304}
4305
Miao Xieb8e95482012-11-26 08:48:01 +00004306static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004308 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004309 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004310 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004311
4312 if (!capable(CAP_SYS_ADMIN))
4313 return -EPERM;
4314
4315 sa = memdup_user(arg, sizeof(*sa));
4316 if (IS_ERR(sa))
4317 return PTR_ERR(sa);
4318
Miao Xieb8e95482012-11-26 08:48:01 +00004319 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4320 ret = mnt_want_write_file(file);
4321 if (ret)
4322 goto out;
4323 }
4324
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004325 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004326 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4327 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004328
4329 if (copy_to_user(arg, sa, sizeof(*sa)))
4330 ret = -EFAULT;
4331
Miao Xieb8e95482012-11-26 08:48:01 +00004332 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4333 mnt_drop_write_file(file);
4334out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004335 kfree(sa);
4336 return ret;
4337}
4338
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004339static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004340{
4341 if (!capable(CAP_SYS_ADMIN))
4342 return -EPERM;
4343
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004344 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004345}
4346
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004347static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004348 void __user *arg)
4349{
4350 struct btrfs_ioctl_scrub_args *sa;
4351 int ret;
4352
4353 if (!capable(CAP_SYS_ADMIN))
4354 return -EPERM;
4355
4356 sa = memdup_user(arg, sizeof(*sa));
4357 if (IS_ERR(sa))
4358 return PTR_ERR(sa);
4359
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004360 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004361
4362 if (copy_to_user(arg, sa, sizeof(*sa)))
4363 ret = -EFAULT;
4364
4365 kfree(sa);
4366 return ret;
4367}
4368
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004369static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004370 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004371{
4372 struct btrfs_ioctl_get_dev_stats *sa;
4373 int ret;
4374
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004375 sa = memdup_user(arg, sizeof(*sa));
4376 if (IS_ERR(sa))
4377 return PTR_ERR(sa);
4378
David Sterbab27f7c02012-06-22 06:30:39 -06004379 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4380 kfree(sa);
4381 return -EPERM;
4382 }
4383
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004384 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004385
4386 if (copy_to_user(arg, sa, sizeof(*sa)))
4387 ret = -EFAULT;
4388
4389 kfree(sa);
4390 return ret;
4391}
4392
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004393static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4394 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004395{
4396 struct btrfs_ioctl_dev_replace_args *p;
4397 int ret;
4398
4399 if (!capable(CAP_SYS_ADMIN))
4400 return -EPERM;
4401
4402 p = memdup_user(arg, sizeof(*p));
4403 if (IS_ERR(p))
4404 return PTR_ERR(p);
4405
4406 switch (p->cmd) {
4407 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004408 if (fs_info->sb->s_flags & MS_RDONLY) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004409 ret = -EROFS;
4410 goto out;
4411 }
David Sterba171938e2017-03-28 14:44:21 +02004412 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004413 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004414 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004415 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004416 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004417 }
4418 break;
4419 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004421 ret = 0;
4422 break;
4423 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004424 ret = btrfs_dev_replace_cancel(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004425 break;
4426 default:
4427 ret = -EINVAL;
4428 break;
4429 }
4430
4431 if (copy_to_user(arg, p, sizeof(*p)))
4432 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004433out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004434 kfree(p);
4435 return ret;
4436}
4437
Jan Schmidtd7728c92011-07-07 16:48:38 +02004438static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4439{
4440 int ret = 0;
4441 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004442 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004443 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004444 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004445 struct inode_fs_paths *ipath = NULL;
4446 struct btrfs_path *path;
4447
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004448 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004449 return -EPERM;
4450
4451 path = btrfs_alloc_path();
4452 if (!path) {
4453 ret = -ENOMEM;
4454 goto out;
4455 }
4456
4457 ipa = memdup_user(arg, sizeof(*ipa));
4458 if (IS_ERR(ipa)) {
4459 ret = PTR_ERR(ipa);
4460 ipa = NULL;
4461 goto out;
4462 }
4463
4464 size = min_t(u32, ipa->size, 4096);
4465 ipath = init_ipath(size, root, path);
4466 if (IS_ERR(ipath)) {
4467 ret = PTR_ERR(ipath);
4468 ipath = NULL;
4469 goto out;
4470 }
4471
4472 ret = paths_from_inode(ipa->inum, ipath);
4473 if (ret < 0)
4474 goto out;
4475
4476 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004477 rel_ptr = ipath->fspath->val[i] -
4478 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004479 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004480 }
4481
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004482 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4483 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004484 if (ret) {
4485 ret = -EFAULT;
4486 goto out;
4487 }
4488
4489out:
4490 btrfs_free_path(path);
4491 free_ipath(ipath);
4492 kfree(ipa);
4493
4494 return ret;
4495}
4496
4497static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4498{
4499 struct btrfs_data_container *inodes = ctx;
4500 const size_t c = 3 * sizeof(u64);
4501
4502 if (inodes->bytes_left >= c) {
4503 inodes->bytes_left -= c;
4504 inodes->val[inodes->elem_cnt] = inum;
4505 inodes->val[inodes->elem_cnt + 1] = offset;
4506 inodes->val[inodes->elem_cnt + 2] = root;
4507 inodes->elem_cnt += 3;
4508 } else {
4509 inodes->bytes_missing += c - inodes->bytes_left;
4510 inodes->bytes_left = 0;
4511 inodes->elem_missed += 3;
4512 }
4513
4514 return 0;
4515}
4516
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004517static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Jan Schmidtd7728c92011-07-07 16:48:38 +02004518 void __user *arg)
4519{
4520 int ret = 0;
4521 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004522 struct btrfs_ioctl_logical_ino_args *loi;
4523 struct btrfs_data_container *inodes = NULL;
4524 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004525
4526 if (!capable(CAP_SYS_ADMIN))
4527 return -EPERM;
4528
4529 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304530 if (IS_ERR(loi))
4531 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004532
4533 path = btrfs_alloc_path();
4534 if (!path) {
4535 ret = -ENOMEM;
4536 goto out;
4537 }
4538
Byongho Leeee221842015-12-15 01:42:10 +09004539 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004540 inodes = init_data_container(size);
4541 if (IS_ERR(inodes)) {
4542 ret = PTR_ERR(inodes);
4543 inodes = NULL;
4544 goto out;
4545 }
4546
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004547 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Liu Bodf031f02012-09-07 20:01:29 -06004548 build_ino_list, inodes);
4549 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004550 ret = -ENOENT;
4551 if (ret < 0)
4552 goto out;
4553
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004554 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4555 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004556 if (ret)
4557 ret = -EFAULT;
4558
4559out:
4560 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004561 kvfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004562 kfree(loi);
4563
4564 return ret;
4565}
4566
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004567void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004568 struct btrfs_ioctl_balance_args *bargs)
4569{
4570 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4571
4572 bargs->flags = bctl->flags;
4573
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004574 if (atomic_read(&fs_info->balance_running))
4575 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4576 if (atomic_read(&fs_info->balance_pause_req))
4577 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004578 if (atomic_read(&fs_info->balance_cancel_req))
4579 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004580
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004581 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4582 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4583 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004584
4585 if (lock) {
4586 spin_lock(&fs_info->balance_lock);
4587 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4588 spin_unlock(&fs_info->balance_lock);
4589 } else {
4590 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4591 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004592}
4593
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004594static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004595{
Al Viro496ad9a2013-01-23 17:07:38 -05004596 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004597 struct btrfs_fs_info *fs_info = root->fs_info;
4598 struct btrfs_ioctl_balance_args *bargs;
4599 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004600 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004601 int ret;
4602
4603 if (!capable(CAP_SYS_ADMIN))
4604 return -EPERM;
4605
Liu Boe54bfa312012-06-29 03:58:48 -06004606 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004607 if (ret)
4608 return ret;
4609
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004610again:
David Sterba171938e2017-03-28 14:44:21 +02004611 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004612 mutex_lock(&fs_info->volume_mutex);
4613 mutex_lock(&fs_info->balance_mutex);
4614 need_unlock = true;
4615 goto locked;
4616 }
4617
4618 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004619 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004620 * (1) some other op is running
4621 * (2) balance is running
4622 * (3) balance is paused -- special case (think resume)
4623 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004624 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004625 if (fs_info->balance_ctl) {
4626 /* this is either (2) or (3) */
4627 if (!atomic_read(&fs_info->balance_running)) {
4628 mutex_unlock(&fs_info->balance_mutex);
4629 if (!mutex_trylock(&fs_info->volume_mutex))
4630 goto again;
4631 mutex_lock(&fs_info->balance_mutex);
4632
4633 if (fs_info->balance_ctl &&
4634 !atomic_read(&fs_info->balance_running)) {
4635 /* this is (3) */
4636 need_unlock = false;
4637 goto locked;
4638 }
4639
4640 mutex_unlock(&fs_info->balance_mutex);
4641 mutex_unlock(&fs_info->volume_mutex);
4642 goto again;
4643 } else {
4644 /* this is (2) */
4645 mutex_unlock(&fs_info->balance_mutex);
4646 ret = -EINPROGRESS;
4647 goto out;
4648 }
4649 } else {
4650 /* this is (1) */
4651 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004652 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004653 goto out;
4654 }
4655
4656locked:
David Sterba171938e2017-03-28 14:44:21 +02004657 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004658
4659 if (arg) {
4660 bargs = memdup_user(arg, sizeof(*bargs));
4661 if (IS_ERR(bargs)) {
4662 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004663 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004664 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004665
4666 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4667 if (!fs_info->balance_ctl) {
4668 ret = -ENOTCONN;
4669 goto out_bargs;
4670 }
4671
4672 bctl = fs_info->balance_ctl;
4673 spin_lock(&fs_info->balance_lock);
4674 bctl->flags |= BTRFS_BALANCE_RESUME;
4675 spin_unlock(&fs_info->balance_lock);
4676
4677 goto do_balance;
4678 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004679 } else {
4680 bargs = NULL;
4681 }
4682
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004683 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004684 ret = -EINPROGRESS;
4685 goto out_bargs;
4686 }
4687
David Sterba8d2db782015-11-04 15:38:29 +01004688 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004689 if (!bctl) {
4690 ret = -ENOMEM;
4691 goto out_bargs;
4692 }
4693
4694 bctl->fs_info = fs_info;
4695 if (arg) {
4696 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4697 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4698 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4699
4700 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004701 } else {
4702 /* balance everything - no filters */
4703 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004704 }
4705
David Sterba8eb93452015-10-12 16:55:54 +02004706 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4707 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004708 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004709 }
4710
Ilya Dryomovde322262012-01-16 22:04:49 +02004711do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004712 /*
David Sterba171938e2017-03-28 14:44:21 +02004713 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004714 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4715 * or, if restriper was paused all the way until unmount, in
David Sterba171938e2017-03-28 14:44:21 +02004716 * free_fs_info. The flag is cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004717 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004718 need_unlock = false;
4719
4720 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004721 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004722
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004723 if (arg) {
4724 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4725 ret = -EFAULT;
4726 }
4727
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004728out_bctl:
4729 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004730out_bargs:
4731 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004732out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004733 mutex_unlock(&fs_info->balance_mutex);
4734 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004735 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004736 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004737out:
Liu Boe54bfa312012-06-29 03:58:48 -06004738 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004739 return ret;
4740}
4741
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004742static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004743{
4744 if (!capable(CAP_SYS_ADMIN))
4745 return -EPERM;
4746
4747 switch (cmd) {
4748 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004749 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004750 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004751 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004752 }
4753
4754 return -EINVAL;
4755}
4756
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004757static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004758 void __user *arg)
4759{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004760 struct btrfs_ioctl_balance_args *bargs;
4761 int ret = 0;
4762
4763 if (!capable(CAP_SYS_ADMIN))
4764 return -EPERM;
4765
4766 mutex_lock(&fs_info->balance_mutex);
4767 if (!fs_info->balance_ctl) {
4768 ret = -ENOTCONN;
4769 goto out;
4770 }
4771
David Sterba8d2db782015-11-04 15:38:29 +01004772 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004773 if (!bargs) {
4774 ret = -ENOMEM;
4775 goto out;
4776 }
4777
4778 update_ioctl_balance_args(fs_info, 1, bargs);
4779
4780 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4781 ret = -EFAULT;
4782
4783 kfree(bargs);
4784out:
4785 mutex_unlock(&fs_info->balance_mutex);
4786 return ret;
4787}
4788
Miao Xie905b0dd2012-11-26 08:50:11 +00004789static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004790{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004791 struct inode *inode = file_inode(file);
4792 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004793 struct btrfs_ioctl_quota_ctl_args *sa;
4794 struct btrfs_trans_handle *trans = NULL;
4795 int ret;
4796 int err;
4797
4798 if (!capable(CAP_SYS_ADMIN))
4799 return -EPERM;
4800
Miao Xie905b0dd2012-11-26 08:50:11 +00004801 ret = mnt_want_write_file(file);
4802 if (ret)
4803 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004804
4805 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004806 if (IS_ERR(sa)) {
4807 ret = PTR_ERR(sa);
4808 goto drop_write;
4809 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004810
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004811 down_write(&fs_info->subvol_sem);
4812 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004813 if (IS_ERR(trans)) {
4814 ret = PTR_ERR(trans);
4815 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004816 }
4817
4818 switch (sa->cmd) {
4819 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004820 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004821 break;
4822 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004823 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004824 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004825 default:
4826 ret = -EINVAL;
4827 break;
4828 }
4829
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004830 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004831 if (err && !ret)
4832 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004833out:
4834 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004835 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004836drop_write:
4837 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004838 return ret;
4839}
4840
Miao Xie905b0dd2012-11-26 08:50:11 +00004841static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004842{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004843 struct inode *inode = file_inode(file);
4844 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4845 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004846 struct btrfs_ioctl_qgroup_assign_args *sa;
4847 struct btrfs_trans_handle *trans;
4848 int ret;
4849 int err;
4850
4851 if (!capable(CAP_SYS_ADMIN))
4852 return -EPERM;
4853
Miao Xie905b0dd2012-11-26 08:50:11 +00004854 ret = mnt_want_write_file(file);
4855 if (ret)
4856 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004857
4858 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004859 if (IS_ERR(sa)) {
4860 ret = PTR_ERR(sa);
4861 goto drop_write;
4862 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004863
4864 trans = btrfs_join_transaction(root);
4865 if (IS_ERR(trans)) {
4866 ret = PTR_ERR(trans);
4867 goto out;
4868 }
4869
Arne Jansen5d13a372011-09-14 15:53:51 +02004870 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004871 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004872 sa->src, sa->dst);
4873 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004874 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004875 sa->src, sa->dst);
4876 }
4877
Qu Wenruoe082f562015-02-27 16:24:28 +08004878 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004880 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004881 btrfs_handle_fs_error(fs_info, err,
4882 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004883 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004884 if (err && !ret)
4885 ret = err;
4886
4887out:
4888 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004889drop_write:
4890 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004891 return ret;
4892}
4893
Miao Xie905b0dd2012-11-26 08:50:11 +00004894static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004895{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004896 struct inode *inode = file_inode(file);
4897 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4898 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004899 struct btrfs_ioctl_qgroup_create_args *sa;
4900 struct btrfs_trans_handle *trans;
4901 int ret;
4902 int err;
4903
4904 if (!capable(CAP_SYS_ADMIN))
4905 return -EPERM;
4906
Miao Xie905b0dd2012-11-26 08:50:11 +00004907 ret = mnt_want_write_file(file);
4908 if (ret)
4909 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004910
4911 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004912 if (IS_ERR(sa)) {
4913 ret = PTR_ERR(sa);
4914 goto drop_write;
4915 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004916
Miao Xied86e56c2012-11-15 11:35:41 +00004917 if (!sa->qgroupid) {
4918 ret = -EINVAL;
4919 goto out;
4920 }
4921
Arne Jansen5d13a372011-09-14 15:53:51 +02004922 trans = btrfs_join_transaction(root);
4923 if (IS_ERR(trans)) {
4924 ret = PTR_ERR(trans);
4925 goto out;
4926 }
4927
Arne Jansen5d13a372011-09-14 15:53:51 +02004928 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004929 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004930 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004931 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004932 }
4933
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004934 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004935 if (err && !ret)
4936 ret = err;
4937
4938out:
4939 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004940drop_write:
4941 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004942 return ret;
4943}
4944
Miao Xie905b0dd2012-11-26 08:50:11 +00004945static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004946{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004947 struct inode *inode = file_inode(file);
4948 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4949 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004950 struct btrfs_ioctl_qgroup_limit_args *sa;
4951 struct btrfs_trans_handle *trans;
4952 int ret;
4953 int err;
4954 u64 qgroupid;
4955
4956 if (!capable(CAP_SYS_ADMIN))
4957 return -EPERM;
4958
Miao Xie905b0dd2012-11-26 08:50:11 +00004959 ret = mnt_want_write_file(file);
4960 if (ret)
4961 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004962
4963 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004964 if (IS_ERR(sa)) {
4965 ret = PTR_ERR(sa);
4966 goto drop_write;
4967 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004968
4969 trans = btrfs_join_transaction(root);
4970 if (IS_ERR(trans)) {
4971 ret = PTR_ERR(trans);
4972 goto out;
4973 }
4974
4975 qgroupid = sa->qgroupid;
4976 if (!qgroupid) {
4977 /* take the current subvol as qgroup */
4978 qgroupid = root->root_key.objectid;
4979 }
4980
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004981 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004982
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004983 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004984 if (err && !ret)
4985 ret = err;
4986
4987out:
4988 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004989drop_write:
4990 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004991 return ret;
4992}
4993
Jan Schmidt2f232032013-04-25 16:04:51 +00004994static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4995{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004996 struct inode *inode = file_inode(file);
4997 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004998 struct btrfs_ioctl_quota_rescan_args *qsa;
4999 int ret;
5000
5001 if (!capable(CAP_SYS_ADMIN))
5002 return -EPERM;
5003
5004 ret = mnt_want_write_file(file);
5005 if (ret)
5006 return ret;
5007
5008 qsa = memdup_user(arg, sizeof(*qsa));
5009 if (IS_ERR(qsa)) {
5010 ret = PTR_ERR(qsa);
5011 goto drop_write;
5012 }
5013
5014 if (qsa->flags) {
5015 ret = -EINVAL;
5016 goto out;
5017 }
5018
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005019 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005020
5021out:
5022 kfree(qsa);
5023drop_write:
5024 mnt_drop_write_file(file);
5025 return ret;
5026}
5027
5028static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5029{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005030 struct inode *inode = file_inode(file);
5031 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005032 struct btrfs_ioctl_quota_rescan_args *qsa;
5033 int ret = 0;
5034
5035 if (!capable(CAP_SYS_ADMIN))
5036 return -EPERM;
5037
David Sterba8d2db782015-11-04 15:38:29 +01005038 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005039 if (!qsa)
5040 return -ENOMEM;
5041
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005042 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005043 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005044 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005045 }
5046
5047 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5048 ret = -EFAULT;
5049
5050 kfree(qsa);
5051 return ret;
5052}
5053
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005054static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5055{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005056 struct inode *inode = file_inode(file);
5057 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005058
5059 if (!capable(CAP_SYS_ADMIN))
5060 return -EPERM;
5061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005062 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005063}
5064
Hugo Millsabccd002014-01-30 20:17:00 +00005065static long _btrfs_ioctl_set_received_subvol(struct file *file,
5066 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005067{
Al Viro496ad9a2013-01-23 17:07:38 -05005068 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005069 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005070 struct btrfs_root *root = BTRFS_I(inode)->root;
5071 struct btrfs_root_item *root_item = &root->root_item;
5072 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005073 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005074 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005075 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005076
David Sterbabd60ea02014-01-16 15:50:22 +01005077 if (!inode_owner_or_capable(inode))
5078 return -EPERM;
5079
Alexander Block8ea05e32012-07-25 17:35:53 +02005080 ret = mnt_want_write_file(file);
5081 if (ret < 0)
5082 return ret;
5083
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005084 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005085
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005086 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005087 ret = -EINVAL;
5088 goto out;
5089 }
5090
5091 if (btrfs_root_readonly(root)) {
5092 ret = -EROFS;
5093 goto out;
5094 }
5095
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005096 /*
5097 * 1 - root item
5098 * 2 - uuid items (received uuid + subvol uuid)
5099 */
5100 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005101 if (IS_ERR(trans)) {
5102 ret = PTR_ERR(trans);
5103 trans = NULL;
5104 goto out;
5105 }
5106
5107 sa->rtransid = trans->transid;
5108 sa->rtime.sec = ct.tv_sec;
5109 sa->rtime.nsec = ct.tv_nsec;
5110
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005111 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5112 BTRFS_UUID_SIZE);
5113 if (received_uuid_changed &&
5114 !btrfs_is_empty_uuid(root_item->received_uuid))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005115 btrfs_uuid_tree_rem(trans, fs_info, root_item->received_uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005116 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5117 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005118 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5119 btrfs_set_root_stransid(root_item, sa->stransid);
5120 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005121 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5122 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5123 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5124 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005125
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005126 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005127 &root->root_key, &root->root_item);
5128 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005129 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005130 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005131 }
5132 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005133 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005134 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5135 root->root_key.objectid);
5136 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005137 btrfs_abort_transaction(trans, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005138 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005139 }
5140 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005141 ret = btrfs_commit_transaction(trans);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005142 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005143 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005144 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005145 }
5146
Hugo Millsabccd002014-01-30 20:17:00 +00005147out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005148 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005149 mnt_drop_write_file(file);
5150 return ret;
5151}
5152
5153#ifdef CONFIG_64BIT
5154static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5155 void __user *arg)
5156{
5157 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5158 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5159 int ret = 0;
5160
5161 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305162 if (IS_ERR(args32))
5163 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005164
David Sterba8d2db782015-11-04 15:38:29 +01005165 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005166 if (!args64) {
5167 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005168 goto out;
5169 }
5170
5171 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5172 args64->stransid = args32->stransid;
5173 args64->rtransid = args32->rtransid;
5174 args64->stime.sec = args32->stime.sec;
5175 args64->stime.nsec = args32->stime.nsec;
5176 args64->rtime.sec = args32->rtime.sec;
5177 args64->rtime.nsec = args32->rtime.nsec;
5178 args64->flags = args32->flags;
5179
5180 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5181 if (ret)
5182 goto out;
5183
5184 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5185 args32->stransid = args64->stransid;
5186 args32->rtransid = args64->rtransid;
5187 args32->stime.sec = args64->stime.sec;
5188 args32->stime.nsec = args64->stime.nsec;
5189 args32->rtime.sec = args64->rtime.sec;
5190 args32->rtime.nsec = args64->rtime.nsec;
5191 args32->flags = args64->flags;
5192
5193 ret = copy_to_user(arg, args32, sizeof(*args32));
5194 if (ret)
5195 ret = -EFAULT;
5196
5197out:
5198 kfree(args32);
5199 kfree(args64);
5200 return ret;
5201}
5202#endif
5203
5204static long btrfs_ioctl_set_received_subvol(struct file *file,
5205 void __user *arg)
5206{
5207 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5208 int ret = 0;
5209
5210 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305211 if (IS_ERR(sa))
5212 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005213
5214 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5215
5216 if (ret)
5217 goto out;
5218
Alexander Block8ea05e32012-07-25 17:35:53 +02005219 ret = copy_to_user(arg, sa, sizeof(*sa));
5220 if (ret)
5221 ret = -EFAULT;
5222
5223out:
5224 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005225 return ret;
5226}
5227
jeff.liu867ab662013-01-05 02:48:01 +00005228static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5229{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005230 struct inode *inode = file_inode(file);
5231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005232 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005233 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005234 char label[BTRFS_LABEL_SIZE];
5235
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005236 spin_lock(&fs_info->super_lock);
5237 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5238 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005239
5240 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005241
5242 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005243 btrfs_warn(fs_info,
5244 "label is too long, return the first %zu bytes",
5245 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005246 }
5247
jeff.liu867ab662013-01-05 02:48:01 +00005248 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005249
5250 return ret ? -EFAULT : 0;
5251}
5252
jeff.liua8bfd4a2013-01-05 02:48:08 +00005253static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5254{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 struct inode *inode = file_inode(file);
5256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5257 struct btrfs_root *root = BTRFS_I(inode)->root;
5258 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005259 struct btrfs_trans_handle *trans;
5260 char label[BTRFS_LABEL_SIZE];
5261 int ret;
5262
5263 if (!capable(CAP_SYS_ADMIN))
5264 return -EPERM;
5265
5266 if (copy_from_user(label, arg, sizeof(label)))
5267 return -EFAULT;
5268
5269 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005270 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005271 "unable to set label with more than %d bytes",
5272 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005273 return -EINVAL;
5274 }
5275
5276 ret = mnt_want_write_file(file);
5277 if (ret)
5278 return ret;
5279
jeff.liua8bfd4a2013-01-05 02:48:08 +00005280 trans = btrfs_start_transaction(root, 0);
5281 if (IS_ERR(trans)) {
5282 ret = PTR_ERR(trans);
5283 goto out_unlock;
5284 }
5285
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005286 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005287 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005288 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005289 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005290
5291out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005292 mnt_drop_write_file(file);
5293 return ret;
5294}
5295
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005296#define INIT_FEATURE_FLAGS(suffix) \
5297 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5298 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5299 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5300
David Sterbad5131b62016-02-17 15:26:27 +01005301int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005302{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005303 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005304 INIT_FEATURE_FLAGS(SUPP),
5305 INIT_FEATURE_FLAGS(SAFE_SET),
5306 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5307 };
5308
5309 if (copy_to_user(arg, &features, sizeof(features)))
5310 return -EFAULT;
5311
5312 return 0;
5313}
5314
5315static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5316{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005317 struct inode *inode = file_inode(file);
5318 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5319 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005320 struct btrfs_ioctl_feature_flags features;
5321
5322 features.compat_flags = btrfs_super_compat_flags(super_block);
5323 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5324 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5325
5326 if (copy_to_user(arg, &features, sizeof(features)))
5327 return -EFAULT;
5328
5329 return 0;
5330}
5331
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005332static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005333 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005334 u64 change_mask, u64 flags, u64 supported_flags,
5335 u64 safe_set, u64 safe_clear)
5336{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005337 const char *type = btrfs_feature_set_names[set];
5338 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005339 u64 disallowed, unsupported;
5340 u64 set_mask = flags & change_mask;
5341 u64 clear_mask = ~flags & change_mask;
5342
5343 unsupported = set_mask & ~supported_flags;
5344 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005345 names = btrfs_printable_features(set, unsupported);
5346 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005347 btrfs_warn(fs_info,
5348 "this kernel does not support the %s feature bit%s",
5349 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005350 kfree(names);
5351 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005352 btrfs_warn(fs_info,
5353 "this kernel does not support %s bits 0x%llx",
5354 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005355 return -EOPNOTSUPP;
5356 }
5357
5358 disallowed = set_mask & ~safe_set;
5359 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005360 names = btrfs_printable_features(set, disallowed);
5361 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005362 btrfs_warn(fs_info,
5363 "can't set the %s feature bit%s while mounted",
5364 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005365 kfree(names);
5366 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005367 btrfs_warn(fs_info,
5368 "can't set %s bits 0x%llx while mounted",
5369 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005370 return -EPERM;
5371 }
5372
5373 disallowed = clear_mask & ~safe_clear;
5374 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005375 names = btrfs_printable_features(set, disallowed);
5376 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005377 btrfs_warn(fs_info,
5378 "can't clear the %s feature bit%s while mounted",
5379 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005380 kfree(names);
5381 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005382 btrfs_warn(fs_info,
5383 "can't clear %s bits 0x%llx while mounted",
5384 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005385 return -EPERM;
5386 }
5387
5388 return 0;
5389}
5390
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005391#define check_feature(fs_info, change_mask, flags, mask_base) \
5392check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005393 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5394 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5395 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5396
5397static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5398{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005399 struct inode *inode = file_inode(file);
5400 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5401 struct btrfs_root *root = BTRFS_I(inode)->root;
5402 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005403 struct btrfs_ioctl_feature_flags flags[2];
5404 struct btrfs_trans_handle *trans;
5405 u64 newflags;
5406 int ret;
5407
5408 if (!capable(CAP_SYS_ADMIN))
5409 return -EPERM;
5410
5411 if (copy_from_user(flags, arg, sizeof(flags)))
5412 return -EFAULT;
5413
5414 /* Nothing to do */
5415 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5416 !flags[0].incompat_flags)
5417 return 0;
5418
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005419 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005420 flags[1].compat_flags, COMPAT);
5421 if (ret)
5422 return ret;
5423
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005424 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005425 flags[1].compat_ro_flags, COMPAT_RO);
5426 if (ret)
5427 return ret;
5428
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005429 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005430 flags[1].incompat_flags, INCOMPAT);
5431 if (ret)
5432 return ret;
5433
David Sterba7ab19622016-05-04 11:32:00 +02005434 ret = mnt_want_write_file(file);
5435 if (ret)
5436 return ret;
5437
David Sterba8051aa12014-02-07 14:34:04 +01005438 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005439 if (IS_ERR(trans)) {
5440 ret = PTR_ERR(trans);
5441 goto out_drop_write;
5442 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005443
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005444 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005445 newflags = btrfs_super_compat_flags(super_block);
5446 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5447 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5448 btrfs_set_super_compat_flags(super_block, newflags);
5449
5450 newflags = btrfs_super_compat_ro_flags(super_block);
5451 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5452 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5453 btrfs_set_super_compat_ro_flags(super_block, newflags);
5454
5455 newflags = btrfs_super_incompat_flags(super_block);
5456 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5457 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5458 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005459 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005460
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005461 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005462out_drop_write:
5463 mnt_drop_write_file(file);
5464
5465 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005466}
5467
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005468long btrfs_ioctl(struct file *file, unsigned int
5469 cmd, unsigned long arg)
5470{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005471 struct inode *inode = file_inode(file);
5472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5473 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005474 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005475
5476 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005477 case FS_IOC_GETFLAGS:
5478 return btrfs_ioctl_getflags(file, argp);
5479 case FS_IOC_SETFLAGS:
5480 return btrfs_ioctl_setflags(file, argp);
5481 case FS_IOC_GETVERSION:
5482 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005483 case FITRIM:
5484 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005485 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005486 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005487 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005488 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005489 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005490 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005491 case BTRFS_IOC_SUBVOL_CREATE_V2:
5492 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005493 case BTRFS_IOC_SNAP_DESTROY:
5494 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005495 case BTRFS_IOC_SUBVOL_GETFLAGS:
5496 return btrfs_ioctl_subvol_getflags(file, argp);
5497 case BTRFS_IOC_SUBVOL_SETFLAGS:
5498 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005499 case BTRFS_IOC_DEFAULT_SUBVOL:
5500 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005501 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005502 return btrfs_ioctl_defrag(file, NULL);
5503 case BTRFS_IOC_DEFRAG_RANGE:
5504 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005505 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005506 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005507 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005508 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005509 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005510 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005511 case BTRFS_IOC_RM_DEV_V2:
5512 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005513 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005514 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005515 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005516 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005517 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005518 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005519 case BTRFS_IOC_TRANS_START:
5520 return btrfs_ioctl_trans_start(file);
5521 case BTRFS_IOC_TRANS_END:
5522 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005523 case BTRFS_IOC_TREE_SEARCH:
5524 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005525 case BTRFS_IOC_TREE_SEARCH_V2:
5526 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005527 case BTRFS_IOC_INO_LOOKUP:
5528 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005529 case BTRFS_IOC_INO_PATHS:
5530 return btrfs_ioctl_ino_to_path(root, argp);
5531 case BTRFS_IOC_LOGICAL_INO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005532 return btrfs_ioctl_logical_to_ino(fs_info, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005533 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005534 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005535 case BTRFS_IOC_SYNC: {
5536 int ret;
5537
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005538 ret = btrfs_start_delalloc_roots(fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005539 if (ret)
5540 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005541 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005542 /*
5543 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005544 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005545 * processing uncleaned subvols.
5546 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005547 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005548 return ret;
5549 }
Sage Weil46204592010-10-29 15:41:32 -04005550 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005551 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005552 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005553 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005554 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005555 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005556 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005557 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005558 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005559 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005560 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005561 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005562 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005563 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005564 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005565 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005566 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5567 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005568#ifdef CONFIG_64BIT
5569 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5570 return btrfs_ioctl_set_received_subvol_32(file, argp);
5571#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005572 case BTRFS_IOC_SEND:
5573 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005574 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005575 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005576 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005577 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005578 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005579 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005580 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005581 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005582 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005583 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005584 case BTRFS_IOC_QUOTA_RESCAN:
5585 return btrfs_ioctl_quota_rescan(file, argp);
5586 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5587 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005588 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5589 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005590 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005591 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005592 case BTRFS_IOC_GET_FSLABEL:
5593 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005594 case BTRFS_IOC_SET_FSLABEL:
5595 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005596 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005597 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005598 case BTRFS_IOC_GET_FEATURES:
5599 return btrfs_ioctl_get_features(file, argp);
5600 case BTRFS_IOC_SET_FEATURES:
5601 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005602 }
5603
5604 return -ENOTTY;
5605}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005606
5607#ifdef CONFIG_COMPAT
5608long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5609{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005610 /*
5611 * These all access 32-bit values anyway so no further
5612 * handling is necessary.
5613 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005614 switch (cmd) {
5615 case FS_IOC32_GETFLAGS:
5616 cmd = FS_IOC_GETFLAGS;
5617 break;
5618 case FS_IOC32_SETFLAGS:
5619 cmd = FS_IOC_SETFLAGS;
5620 break;
5621 case FS_IOC32_GETVERSION:
5622 cmd = FS_IOC_GETVERSION;
5623 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005624 }
5625
5626 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5627}
5628#endif