blob: 802df5755cd3b8f3411db9961d0a59054a498fbf [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 Zefanf062abf2011-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 Zefanf062abf2011-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 Zefanf062abf2011-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 Zefanf062abf2011-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 Zefanf062abf2011-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);
liubo2d4e6f62011-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
David Sterbaea14b57f2017-06-22 02:19:11 +0200610static void btrfs_wait_for_no_snapshotting_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
David Sterbaea14b57f2017-06-22 02:19:11 +0200653 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100654 smp_mb__after_atomic();
David Sterbaea14b57f2017-06-22 02:19:11 +0200655 btrfs_wait_for_no_snapshotting_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:
David Sterbaea14b57f2017-06-22 02:19:11 +0200726 if (atomic_dec_and_test(&root->will_be_snapshotted))
727 wake_up_atomic_t(&root->will_be_snapshotted);
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;
David Sterba1e2ef462017-07-17 20:01:59 +02001258 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001259
Liu Bo0abd5b12013-04-16 09:20:28 +00001260 if (isize == 0)
1261 return 0;
1262
1263 if (range->start >= isize)
1264 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001265
David Sterba1e2ef462017-07-17 20:01:59 +02001266 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001267 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1268 return -EINVAL;
1269 if (range->compress_type)
1270 compress_type = range->compress_type;
1271 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001272
Liu Bo0abd5b12013-04-16 09:20:28 +00001273 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001274 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001275
Chris Mason4cb53002011-05-24 15:35:30 -04001276 /*
David Sterba0a52d102017-06-22 03:22:58 +02001277 * If we were not given a file, allocate a readahead context. As
1278 * readahead is just an optimization, defrag will work without it so
1279 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001280 */
1281 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001282 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001283 if (ra)
1284 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001285 } else {
1286 ra = &file->f_ra;
1287 }
1288
David Sterba63e727e2017-06-22 03:13:02 +02001289 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001290 if (!pages) {
1291 ret = -ENOMEM;
1292 goto out_ra;
1293 }
1294
1295 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001296 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001297 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001298 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001299 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001300 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001301 }
1302
Chris Mason4cb53002011-05-24 15:35:30 -04001303 if (newer_than) {
1304 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001305 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001306 if (!ret) {
1307 range->start = newer_off;
1308 /*
1309 * we always align our defrag to help keep
1310 * the extents in the file evenly spaced
1311 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001312 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001313 } else
1314 goto out_ra;
1315 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001316 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001317 }
1318 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301319 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001320
Li Zefan2a0f7f52011-10-10 15:43:34 -04001321 /*
1322 * make writeback starts from i, so the defrag range can be
1323 * written sequentially.
1324 */
1325 if (i < inode->i_mapping->writeback_index)
1326 inode->i_mapping->writeback_index = i;
1327
Chris Masonf7f43cc2011-10-11 11:41:40 -04001328 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001329 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001330 /*
1331 * make sure we stop running if someone unmounts
1332 * the FS
1333 */
1334 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1335 break;
1336
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001337 if (btrfs_defrag_cancelled(fs_info)) {
1338 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001339 ret = -EAGAIN;
1340 break;
1341 }
1342
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001343 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001344 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001345 &defrag_end, do_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);
David Sterba0a52d102017-06-22 03:22:58 +02001366 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001367 page_cache_sync_readahead(inode->i_mapping, ra,
1368 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301369 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001370 }
Chris Mason940100a2010-03-10 10:52:59 -05001371
Al Viro59551022016-01-22 15:40:57 -05001372 inode_lock(inode);
David Sterba1e2ef462017-07-17 20:01:59 +02001373 if (do_compress)
David Sterbaeec63c62017-07-17 19:41:31 +02001374 BTRFS_I(inode)->defrag_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001375 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001376 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001377 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001378 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001379 }
Chris Mason940100a2010-03-10 10:52:59 -05001380
Chris Mason4cb53002011-05-24 15:35:30 -04001381 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001382 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001383 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001384
1385 if (newer_than) {
1386 if (newer_off == (u64)-1)
1387 break;
1388
Liu Boe1f041e12012-03-29 09:57:45 -04001389 if (ret > 0)
1390 i += ret;
1391
Chris Mason4cb53002011-05-24 15:35:30 -04001392 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001393 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001394
Byongho Leeee221842015-12-15 01:42:10 +09001395 ret = find_new_extents(root, inode, newer_than,
1396 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001397 if (!ret) {
1398 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001399 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001400 } else {
1401 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001402 }
Chris Mason4cb53002011-05-24 15:35:30 -04001403 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001404 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001405 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001406 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001407 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001408 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001409 last_len = 0;
1410 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001411 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001412 }
1413
Filipe Mananadec8ef92014-03-01 10:55:54 +00001414 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001415 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001416 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1417 &BTRFS_I(inode)->runtime_flags))
1418 filemap_flush(inode->i_mapping);
1419 }
Chris Mason1e701a32010-03-11 09:42:04 -05001420
David Sterba1e2ef462017-07-17 20:01:59 +02001421 if (do_compress) {
Chris Mason1e701a32010-03-11 09:42:04 -05001422 /* the filemap_flush will queue IO into the worker threads, but
1423 * we have to make sure the IO is actually started and that
1424 * ordered extents get created before we return
1425 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001426 atomic_inc(&fs_info->async_submit_draining);
1427 while (atomic_read(&fs_info->nr_async_submits) ||
1428 atomic_read(&fs_info->async_delalloc_pages)) {
1429 wait_event(fs_info->async_submit_wait,
1430 (atomic_read(&fs_info->nr_async_submits) == 0 &&
1431 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason1e701a32010-03-11 09:42:04 -05001432 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001433 atomic_dec(&fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001434 }
1435
Li Zefan1a419d82010-10-25 15:12:50 +08001436 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001437 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001438 }
1439
Diego Calleja60ccf822011-09-01 16:33:57 +02001440 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001441
Chris Mason4cb53002011-05-24 15:35:30 -04001442out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001443 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001444 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001445 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001446 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001447 }
Chris Mason4cb53002011-05-24 15:35:30 -04001448 if (!file)
1449 kfree(ra);
1450 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001451 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001452}
1453
Miao Xie198605a2012-11-26 08:43:45 +00001454static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001455 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001456{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001457 struct inode *inode = file_inode(file);
1458 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001459 u64 new_size;
1460 u64 old_size;
1461 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001462 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001463 struct btrfs_ioctl_vol_args *vol_args;
1464 struct btrfs_trans_handle *trans;
1465 struct btrfs_device *device = NULL;
1466 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001467 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001468 char *devstr = NULL;
1469 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001470 int mod = 0;
1471
Chris Masone441d542009-01-05 16:57:23 -05001472 if (!capable(CAP_SYS_ADMIN))
1473 return -EPERM;
1474
Miao Xie198605a2012-11-26 08:43:45 +00001475 ret = mnt_want_write_file(file);
1476 if (ret)
1477 return ret;
1478
David Sterba171938e2017-03-28 14:44:21 +02001479 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001480 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001481 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001482 }
1483
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001484 mutex_lock(&fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001485 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001486 if (IS_ERR(vol_args)) {
1487 ret = PTR_ERR(vol_args);
1488 goto out;
1489 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001490
1491 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001492
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001493 sizestr = vol_args->name;
1494 devstr = strchr(sizestr, ':');
1495 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001496 sizestr = devstr + 1;
1497 *devstr = '\0';
1498 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001499 ret = kstrtoull(devstr, 10, &devid);
1500 if (ret)
1501 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001502 if (!devid) {
1503 ret = -EINVAL;
1504 goto out_free;
1505 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001506 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001507 }
Miao Xiedba60f32012-12-21 09:19:51 +00001508
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001509 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001510 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001511 btrfs_info(fs_info, "resizer unable to find device %llu",
1512 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001513 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001514 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001515 }
Miao Xiedba60f32012-12-21 09:19:51 +00001516
1517 if (!device->writeable) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001518 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001519 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001520 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001521 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001522 goto out_free;
1523 }
1524
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001525 if (!strcmp(sizestr, "max"))
1526 new_size = device->bdev->bd_inode->i_size;
1527 else {
1528 if (sizestr[0] == '-') {
1529 mod = -1;
1530 sizestr++;
1531 } else if (sizestr[0] == '+') {
1532 mod = 1;
1533 sizestr++;
1534 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001535 new_size = memparse(sizestr, &retptr);
1536 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001537 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001538 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001539 }
1540 }
1541
Stefan Behrens63a212a2012-11-05 18:29:28 +01001542 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001543 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001544 goto out_free;
1545 }
1546
Miao Xie7cc8e582014-09-03 21:35:38 +08001547 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001548
1549 if (mod < 0) {
1550 if (new_size > old_size) {
1551 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001552 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001553 }
1554 new_size = old_size - new_size;
1555 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001556 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001557 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001558 goto out_free;
1559 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001560 new_size = old_size + new_size;
1561 }
1562
Byongho Leeee221842015-12-15 01:42:10 +09001563 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001564 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001565 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001566 }
1567 if (new_size > device->bdev->bd_inode->i_size) {
1568 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001569 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001570 }
1571
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001572 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001574 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1575 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001576
1577 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001578 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001579 if (IS_ERR(trans)) {
1580 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001581 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001582 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001583 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001584 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001585 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001586 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001587 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001588
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001589out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001590 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001591out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001592 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02001593 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001594 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001595 return ret;
1596}
1597
Sage Weil72fd0322010-10-29 15:41:32 -04001598static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001599 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001600 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001601 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001602{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001603 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001604 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001605
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001606 if (!S_ISDIR(file_inode(file)->i_mode))
1607 return -ENOTDIR;
1608
Liu Boa874a632012-06-29 03:58:46 -06001609 ret = mnt_want_write_file(file);
1610 if (ret)
1611 goto out;
1612
Sage Weil72fd0322010-10-29 15:41:32 -04001613 namelen = strlen(name);
1614 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001615 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001616 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001617 }
1618
Chris Mason16780ca2012-02-20 22:14:55 -05001619 if (name[0] == '.' &&
1620 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1621 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001622 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001623 }
1624
Chris Mason3de45862008-11-17 21:02:50 -05001625 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001626 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001627 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001628 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001629 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001630 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001631 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001632 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001633 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001634 }
1635
Al Viro496ad9a2013-01-23 17:07:38 -05001636 src_inode = file_inode(src.file);
1637 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001638 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001639 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001640 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001641 } else if (!inode_owner_or_capable(src_inode)) {
1642 /*
1643 * Subvolume creation is not restricted, but snapshots
1644 * are limited to own subvolumes only
1645 */
1646 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001647 } else {
1648 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1649 BTRFS_I(src_inode)->root,
1650 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001651 }
Al Viro2903ff02012-08-28 12:52:22 -04001652 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001653 }
Liu Boa874a632012-06-29 03:58:46 -06001654out_drop_write:
1655 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001656out:
Sage Weil72fd0322010-10-29 15:41:32 -04001657 return ret;
1658}
1659
1660static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001661 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001662{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001663 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001664 int ret;
1665
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001666 if (!S_ISDIR(file_inode(file)->i_mode))
1667 return -ENOTDIR;
1668
Li Zefanfa0d2b92010-12-20 15:53:28 +08001669 vol_args = memdup_user(arg, sizeof(*vol_args));
1670 if (IS_ERR(vol_args))
1671 return PTR_ERR(vol_args);
1672 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001673
Li Zefanfa0d2b92010-12-20 15:53:28 +08001674 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001675 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001676 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001677
Li Zefanfa0d2b92010-12-20 15:53:28 +08001678 kfree(vol_args);
1679 return ret;
1680}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001681
Li Zefanfa0d2b92010-12-20 15:53:28 +08001682static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1683 void __user *arg, int subvol)
1684{
1685 struct btrfs_ioctl_vol_args_v2 *vol_args;
1686 int ret;
1687 u64 transid = 0;
1688 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001689 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001690 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001691
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001692 if (!S_ISDIR(file_inode(file)->i_mode))
1693 return -ENOTDIR;
1694
Li Zefanfa0d2b92010-12-20 15:53:28 +08001695 vol_args = memdup_user(arg, sizeof(*vol_args));
1696 if (IS_ERR(vol_args))
1697 return PTR_ERR(vol_args);
1698 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001699
Li Zefanb83cc962010-12-20 16:04:08 +08001700 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001701 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1702 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001703 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001704 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001705 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001706
1707 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1708 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001709 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1710 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001711 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001712 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001713 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001714 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001715 }
1716 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1717 if (IS_ERR(inherit)) {
1718 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001719 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001720 }
1721 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001722
1723 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001724 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001725 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001726 if (ret)
1727 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001728
Dan Carpenterc47ca322014-09-04 14:09:15 +03001729 if (ptr && copy_to_user(arg +
1730 offsetof(struct btrfs_ioctl_vol_args_v2,
1731 transid),
1732 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001733 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001734
1735free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001736 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001737free_args:
1738 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001739 return ret;
1740}
1741
Li Zefan0caa1022010-12-20 16:30:25 +08001742static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1743 void __user *arg)
1744{
Al Viro496ad9a2013-01-23 17:07:38 -05001745 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001746 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001747 struct btrfs_root *root = BTRFS_I(inode)->root;
1748 int ret = 0;
1749 u64 flags = 0;
1750
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001751 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001752 return -EINVAL;
1753
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001754 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001755 if (btrfs_root_readonly(root))
1756 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001757 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001758
1759 if (copy_to_user(arg, &flags, sizeof(flags)))
1760 ret = -EFAULT;
1761
1762 return ret;
1763}
1764
1765static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1766 void __user *arg)
1767{
Al Viro496ad9a2013-01-23 17:07:38 -05001768 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001769 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001770 struct btrfs_root *root = BTRFS_I(inode)->root;
1771 struct btrfs_trans_handle *trans;
1772 u64 root_flags;
1773 u64 flags;
1774 int ret = 0;
1775
David Sterbabd60ea02014-01-16 15:50:22 +01001776 if (!inode_owner_or_capable(inode))
1777 return -EPERM;
1778
Liu Bob9ca0662012-06-29 03:58:49 -06001779 ret = mnt_want_write_file(file);
1780 if (ret)
1781 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001782
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001783 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001784 ret = -EINVAL;
1785 goto out_drop_write;
1786 }
Li Zefan0caa1022010-12-20 16:30:25 +08001787
Liu Bob9ca0662012-06-29 03:58:49 -06001788 if (copy_from_user(&flags, arg, sizeof(flags))) {
1789 ret = -EFAULT;
1790 goto out_drop_write;
1791 }
Li Zefan0caa1022010-12-20 16:30:25 +08001792
Liu Bob9ca0662012-06-29 03:58:49 -06001793 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1794 ret = -EINVAL;
1795 goto out_drop_write;
1796 }
Li Zefan0caa1022010-12-20 16:30:25 +08001797
Liu Bob9ca0662012-06-29 03:58:49 -06001798 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1799 ret = -EOPNOTSUPP;
1800 goto out_drop_write;
1801 }
Li Zefan0caa1022010-12-20 16:30:25 +08001802
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001803 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001804
1805 /* nothing to do */
1806 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001807 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001808
1809 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001810 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001811 btrfs_set_root_flags(&root->root_item,
1812 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001813 } else {
1814 /*
1815 * Block RO -> RW transition if this subvolume is involved in
1816 * send
1817 */
1818 spin_lock(&root->root_item_lock);
1819 if (root->send_in_progress == 0) {
1820 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001821 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001822 spin_unlock(&root->root_item_lock);
1823 } else {
1824 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001825 btrfs_warn(fs_info,
1826 "Attempt to set subvolume %llu read-write during send",
1827 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001828 ret = -EPERM;
1829 goto out_drop_sem;
1830 }
1831 }
Li Zefan0caa1022010-12-20 16:30:25 +08001832
1833 trans = btrfs_start_transaction(root, 1);
1834 if (IS_ERR(trans)) {
1835 ret = PTR_ERR(trans);
1836 goto out_reset;
1837 }
1838
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001839 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001840 &root->root_key, &root->root_item);
1841
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001842 btrfs_commit_transaction(trans);
Li Zefan0caa1022010-12-20 16:30:25 +08001843out_reset:
1844 if (ret)
1845 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001846out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001847 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001848out_drop_write:
1849 mnt_drop_write_file(file);
1850out:
Li Zefan0caa1022010-12-20 16:30:25 +08001851 return ret;
1852}
1853
Yan, Zheng76dda932009-09-21 16:00:26 -04001854/*
1855 * helper to check if the subvolume references other subvolumes
1856 */
1857static noinline int may_destroy_subvol(struct btrfs_root *root)
1858{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001859 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04001860 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001861 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001862 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001863 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001864 int ret;
1865
1866 path = btrfs_alloc_path();
1867 if (!path)
1868 return -ENOMEM;
1869
Josef Bacik175a2b82013-08-12 15:36:44 -04001870 /* Make sure this root isn't set as the default subvol */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001871 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1872 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
Josef Bacik175a2b82013-08-12 15:36:44 -04001873 dir_id, "default", 7, 0);
1874 if (di && !IS_ERR(di)) {
1875 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1876 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001877 ret = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001878 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001879 "deleting default subvolume %llu is not allowed",
1880 key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001881 goto out;
1882 }
1883 btrfs_release_path(path);
1884 }
1885
Yan, Zheng76dda932009-09-21 16:00:26 -04001886 key.objectid = root->root_key.objectid;
1887 key.type = BTRFS_ROOT_REF_KEY;
1888 key.offset = (u64)-1;
1889
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04001891 if (ret < 0)
1892 goto out;
1893 BUG_ON(ret == 0);
1894
1895 ret = 0;
1896 if (path->slots[0] > 0) {
1897 path->slots[0]--;
1898 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1899 if (key.objectid == root->root_key.objectid &&
1900 key.type == BTRFS_ROOT_REF_KEY)
1901 ret = -ENOTEMPTY;
1902 }
1903out:
1904 btrfs_free_path(path);
1905 return ret;
1906}
1907
Chris Masonac8e9812010-02-28 15:39:26 -05001908static noinline int key_in_sk(struct btrfs_key *key,
1909 struct btrfs_ioctl_search_key *sk)
1910{
Chris Masonabc6e132010-03-18 12:10:08 -04001911 struct btrfs_key test;
1912 int ret;
1913
1914 test.objectid = sk->min_objectid;
1915 test.type = sk->min_type;
1916 test.offset = sk->min_offset;
1917
1918 ret = btrfs_comp_cpu_keys(key, &test);
1919 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001920 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001921
1922 test.objectid = sk->max_objectid;
1923 test.type = sk->max_type;
1924 test.offset = sk->max_offset;
1925
1926 ret = btrfs_comp_cpu_keys(key, &test);
1927 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001928 return 0;
1929 return 1;
1930}
1931
Jeff Mahoneydf397562016-06-21 20:18:21 -04001932static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001933 struct btrfs_key *key,
1934 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001935 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001936 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001937 unsigned long *sk_offset,
1938 int *num_found)
1939{
1940 u64 found_transid;
1941 struct extent_buffer *leaf;
1942 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001943 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001944 unsigned long item_off;
1945 unsigned long item_len;
1946 int nritems;
1947 int i;
1948 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001949 int ret = 0;
1950
1951 leaf = path->nodes[0];
1952 slot = path->slots[0];
1953 nritems = btrfs_header_nritems(leaf);
1954
1955 if (btrfs_header_generation(leaf) > sk->max_transid) {
1956 i = nritems;
1957 goto advance_key;
1958 }
1959 found_transid = btrfs_header_generation(leaf);
1960
1961 for (i = slot; i < nritems; i++) {
1962 item_off = btrfs_item_ptr_offset(leaf, i);
1963 item_len = btrfs_item_size_nr(leaf, i);
1964
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001965 btrfs_item_key_to_cpu(leaf, key, i);
1966 if (!key_in_sk(key, sk))
1967 continue;
1968
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001969 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001970 if (*num_found) {
1971 ret = 1;
1972 goto out;
1973 }
Chris Masonac8e9812010-02-28 15:39:26 -05001974
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001975 /*
1976 * return one empty item back for v1, which does not
1977 * handle -EOVERFLOW
1978 */
1979
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001980 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001981 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001982 ret = -EOVERFLOW;
1983 }
Chris Masonac8e9812010-02-28 15:39:26 -05001984
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001985 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05001986 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001987 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001988 }
1989
Chris Masonac8e9812010-02-28 15:39:26 -05001990 sh.objectid = key->objectid;
1991 sh.offset = key->offset;
1992 sh.type = key->type;
1993 sh.len = item_len;
1994 sh.transid = found_transid;
1995
1996 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01001997 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
1998 ret = -EFAULT;
1999 goto out;
2000 }
2001
Chris Masonac8e9812010-02-28 15:39:26 -05002002 *sk_offset += sizeof(sh);
2003
2004 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002005 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002006 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002007 if (read_extent_buffer_to_user(leaf, up,
2008 item_off, item_len)) {
2009 ret = -EFAULT;
2010 goto out;
2011 }
2012
Chris Masonac8e9812010-02-28 15:39:26 -05002013 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002014 }
Hugo Millse2156862011-05-14 17:43:41 +00002015 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002016
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002017 if (ret) /* -EOVERFLOW from above */
2018 goto out;
2019
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002020 if (*num_found >= sk->nr_items) {
2021 ret = 1;
2022 goto out;
2023 }
Chris Masonac8e9812010-02-28 15:39:26 -05002024 }
2025advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002026 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002027 test.objectid = sk->max_objectid;
2028 test.type = sk->max_type;
2029 test.offset = sk->max_offset;
2030 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2031 ret = 1;
2032 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002033 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002034 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002035 key->offset = 0;
2036 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002037 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002038 key->offset = 0;
2039 key->type = 0;
2040 key->objectid++;
2041 } else
2042 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002043out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002044 /*
2045 * 0: all items from this leaf copied, continue with next
2046 * 1: * more items can be copied, but unused buffer is too small
2047 * * all items were found
2048 * Either way, it will stops the loop which iterates to the next
2049 * leaf
2050 * -EOVERFLOW: item was to large for buffer
2051 * -EFAULT: could not copy extent buffer back to userspace
2052 */
Chris Masonac8e9812010-02-28 15:39:26 -05002053 return ret;
2054}
2055
2056static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002057 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002058 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002059 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002060{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002061 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002062 struct btrfs_root *root;
2063 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002064 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002065 int ret;
2066 int num_found = 0;
2067 unsigned long sk_offset = 0;
2068
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002069 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2070 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002071 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002072 }
Gerhard Heift12544442014-01-30 16:23:58 +01002073
Chris Masonac8e9812010-02-28 15:39:26 -05002074 path = btrfs_alloc_path();
2075 if (!path)
2076 return -ENOMEM;
2077
2078 if (sk->tree_id == 0) {
2079 /* search the root of the inode that was passed */
2080 root = BTRFS_I(inode)->root;
2081 } else {
2082 key.objectid = sk->tree_id;
2083 key.type = BTRFS_ROOT_ITEM_KEY;
2084 key.offset = (u64)-1;
2085 root = btrfs_read_fs_root_no_name(info, &key);
2086 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002087 btrfs_free_path(path);
2088 return -ENOENT;
2089 }
2090 }
2091
2092 key.objectid = sk->min_objectid;
2093 key.type = sk->min_type;
2094 key.offset = sk->min_offset;
2095
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302096 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002097 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002098 if (ret != 0) {
2099 if (ret > 0)
2100 ret = 0;
2101 goto err;
2102 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002103 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002104 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002105 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002106 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002107 break;
2108
2109 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002110 if (ret > 0)
2111 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002112err:
2113 sk->nr_items = num_found;
2114 btrfs_free_path(path);
2115 return ret;
2116}
2117
2118static noinline int btrfs_ioctl_tree_search(struct file *file,
2119 void __user *argp)
2120{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002121 struct btrfs_ioctl_search_args __user *uargs;
2122 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002123 struct inode *inode;
2124 int ret;
2125 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002126
2127 if (!capable(CAP_SYS_ADMIN))
2128 return -EPERM;
2129
Gerhard Heiftba346b32014-01-30 16:24:02 +01002130 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002131
Gerhard Heiftba346b32014-01-30 16:24:02 +01002132 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2133 return -EFAULT;
2134
2135 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002136
Al Viro496ad9a2013-01-23 17:07:38 -05002137 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002138 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002139
2140 /*
2141 * In the origin implementation an overflow is handled by returning a
2142 * search header with a len of zero, so reset ret.
2143 */
2144 if (ret == -EOVERFLOW)
2145 ret = 0;
2146
Gerhard Heiftba346b32014-01-30 16:24:02 +01002147 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002148 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002149 return ret;
2150}
2151
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002152static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2153 void __user *argp)
2154{
2155 struct btrfs_ioctl_search_args_v2 __user *uarg;
2156 struct btrfs_ioctl_search_args_v2 args;
2157 struct inode *inode;
2158 int ret;
2159 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002160 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002161
2162 if (!capable(CAP_SYS_ADMIN))
2163 return -EPERM;
2164
2165 /* copy search header and buffer size */
2166 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2167 if (copy_from_user(&args, uarg, sizeof(args)))
2168 return -EFAULT;
2169
2170 buf_size = args.buf_size;
2171
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002172 /* limit result size to 16MB */
2173 if (buf_size > buf_limit)
2174 buf_size = buf_limit;
2175
2176 inode = file_inode(file);
2177 ret = search_ioctl(inode, &args.key, &buf_size,
2178 (char *)(&uarg->buf[0]));
2179 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2180 ret = -EFAULT;
2181 else if (ret == -EOVERFLOW &&
2182 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2183 ret = -EFAULT;
2184
Yan, Zheng76dda932009-09-21 16:00:26 -04002185 return ret;
2186}
2187
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002188/*
Chris Masonac8e9812010-02-28 15:39:26 -05002189 * Search INODE_REFs to identify path name of 'dirid' directory
2190 * in a 'tree_id' tree. and sets path name to 'name'.
2191 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002192static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2193 u64 tree_id, u64 dirid, char *name)
2194{
2195 struct btrfs_root *root;
2196 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002197 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002198 int ret = -1;
2199 int slot;
2200 int len;
2201 int total_len = 0;
2202 struct btrfs_inode_ref *iref;
2203 struct extent_buffer *l;
2204 struct btrfs_path *path;
2205
2206 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2207 name[0]='\0';
2208 return 0;
2209 }
2210
2211 path = btrfs_alloc_path();
2212 if (!path)
2213 return -ENOMEM;
2214
Chris Masonac8e9812010-02-28 15:39:26 -05002215 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002216
2217 key.objectid = tree_id;
2218 key.type = BTRFS_ROOT_ITEM_KEY;
2219 key.offset = (u64)-1;
2220 root = btrfs_read_fs_root_no_name(info, &key);
2221 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002222 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002223 ret = -ENOENT;
2224 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002225 }
2226
2227 key.objectid = dirid;
2228 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002229 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002230
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302231 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002232 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2233 if (ret < 0)
2234 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002235 else if (ret > 0) {
2236 ret = btrfs_previous_item(root, path, dirid,
2237 BTRFS_INODE_REF_KEY);
2238 if (ret < 0)
2239 goto out;
2240 else if (ret > 0) {
2241 ret = -ENOENT;
2242 goto out;
2243 }
2244 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002245
2246 l = path->nodes[0];
2247 slot = path->slots[0];
2248 btrfs_item_key_to_cpu(l, &key, slot);
2249
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002250 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2251 len = btrfs_inode_ref_name_len(l, iref);
2252 ptr -= len + 1;
2253 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002254 if (ptr < name) {
2255 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002256 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002257 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002258
2259 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302260 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002261
2262 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2263 break;
2264
David Sterbab3b4aa72011-04-21 01:20:15 +02002265 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002266 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002267 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002268 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002269 }
Li Zefan77906a502011-07-14 03:16:00 +00002270 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302271 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002272 ret = 0;
2273out:
2274 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002275 return ret;
2276}
2277
2278static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2279 void __user *argp)
2280{
2281 struct btrfs_ioctl_ino_lookup_args *args;
2282 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002283 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002284
Julia Lawall2354d082010-10-29 15:14:18 -04002285 args = memdup_user(argp, sizeof(*args));
2286 if (IS_ERR(args))
2287 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002288
Al Viro496ad9a2013-01-23 17:07:38 -05002289 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002290
David Sterba01b810b2015-05-12 19:14:49 +02002291 /*
2292 * Unprivileged query to obtain the containing subvolume root id. The
2293 * path is reset so it's consistent with btrfs_search_path_in_tree.
2294 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002295 if (args->treeid == 0)
2296 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2297
David Sterba01b810b2015-05-12 19:14:49 +02002298 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2299 args->name[0] = 0;
2300 goto out;
2301 }
2302
2303 if (!capable(CAP_SYS_ADMIN)) {
2304 ret = -EPERM;
2305 goto out;
2306 }
2307
Chris Masonac8e9812010-02-28 15:39:26 -05002308 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2309 args->treeid, args->objectid,
2310 args->name);
2311
David Sterba01b810b2015-05-12 19:14:49 +02002312out:
Chris Masonac8e9812010-02-28 15:39:26 -05002313 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2314 ret = -EFAULT;
2315
2316 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002317 return ret;
2318}
2319
Yan, Zheng76dda932009-09-21 16:00:26 -04002320static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2321 void __user *arg)
2322{
Al Viro54563d42013-09-01 15:57:51 -04002323 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002324 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002325 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002326 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002327 struct inode *inode;
2328 struct btrfs_root *root = BTRFS_I(dir)->root;
2329 struct btrfs_root *dest = NULL;
2330 struct btrfs_ioctl_vol_args *vol_args;
2331 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002332 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002333 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002334 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002335 int namelen;
2336 int ret;
2337 int err = 0;
2338
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002339 if (!S_ISDIR(dir->i_mode))
2340 return -ENOTDIR;
2341
Yan, Zheng76dda932009-09-21 16:00:26 -04002342 vol_args = memdup_user(arg, sizeof(*vol_args));
2343 if (IS_ERR(vol_args))
2344 return PTR_ERR(vol_args);
2345
2346 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2347 namelen = strlen(vol_args->name);
2348 if (strchr(vol_args->name, '/') ||
2349 strncmp(vol_args->name, "..", namelen) == 0) {
2350 err = -EINVAL;
2351 goto out;
2352 }
2353
Al Viroa561be72011-11-23 11:57:51 -05002354 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002355 if (err)
2356 goto out;
2357
David Sterba521e0542014-04-15 16:41:44 +02002358
Al Viro00235412016-05-26 00:05:12 -04002359 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2360 if (err == -EINTR)
2361 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002362 dentry = lookup_one_len(vol_args->name, parent, namelen);
2363 if (IS_ERR(dentry)) {
2364 err = PTR_ERR(dentry);
2365 goto out_unlock_dir;
2366 }
2367
David Howells2b0143b2015-03-17 22:25:59 +00002368 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002369 err = -ENOENT;
2370 goto out_dput;
2371 }
2372
David Howells2b0143b2015-03-17 22:25:59 +00002373 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002374 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302375 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002376 /*
2377 * Regular user. Only allow this with a special mount
2378 * option, when the user has write+exec access to the
2379 * subvol root, and when rmdir(2) would have been
2380 * allowed.
2381 *
2382 * Note that this is _not_ check that the subvol is
2383 * empty or doesn't contain data that we wouldn't
2384 * otherwise be able to delete.
2385 *
2386 * Users who want to delete empty subvols should try
2387 * rmdir(2).
2388 */
2389 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002390 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002391 goto out_dput;
2392
2393 /*
2394 * Do not allow deletion if the parent dir is the same
2395 * as the dir to be deleted. That means the ioctl
2396 * must be called on the dentry referencing the root
2397 * of the subvol, not a random directory contained
2398 * within it.
2399 */
2400 err = -EINVAL;
2401 if (root == dest)
2402 goto out_dput;
2403
2404 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2405 if (err)
2406 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002407 }
2408
Miao Xie5c39da52012-10-22 11:39:53 +00002409 /* check if subvolume may be deleted by a user */
2410 err = btrfs_may_delete(dir, dentry, 1);
2411 if (err)
2412 goto out_dput;
2413
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002414 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002415 err = -EINVAL;
2416 goto out_dput;
2417 }
2418
Al Viro59551022016-01-22 15:40:57 -05002419 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002420
2421 /*
2422 * Don't allow to delete a subvolume with send in progress. This is
2423 * inside the i_mutex so the error handling that has to drop the bit
2424 * again is not run concurrently.
2425 */
2426 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002427 root_flags = btrfs_root_flags(&dest->root_item);
2428 if (dest->send_in_progress == 0) {
2429 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002430 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2431 spin_unlock(&dest->root_item_lock);
2432 } else {
2433 spin_unlock(&dest->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002434 btrfs_warn(fs_info,
2435 "Attempt to delete subvolume %llu during send",
2436 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002437 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002438 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002439 }
2440
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002441 down_write(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -04002442
2443 err = may_destroy_subvol(dest);
2444 if (err)
2445 goto out_up_write;
2446
Miao Xiec58aaad2013-02-28 10:05:36 +00002447 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2448 /*
2449 * One for dir inode, two for dir entries, two for root
2450 * ref/backref.
2451 */
2452 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002453 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002454 if (err)
2455 goto out_up_write;
2456
Yan, Zhenga22285a2010-05-16 10:48:46 -04002457 trans = btrfs_start_transaction(root, 0);
2458 if (IS_ERR(trans)) {
2459 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002460 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002461 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002462 trans->block_rsv = &block_rsv;
2463 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002464
Nikolay Borisov43663552017-01-18 00:31:29 +02002465 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
Filipe Manana2be63d52016-02-12 11:34:23 +00002466
Yan, Zheng76dda932009-09-21 16:00:26 -04002467 ret = btrfs_unlink_subvol(trans, root, dir,
2468 dest->root_key.objectid,
2469 dentry->d_name.name,
2470 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002471 if (ret) {
2472 err = ret;
Jeff Mahoney66642832016-06-10 18:19:25 -04002473 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002474 goto out_end_trans;
2475 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002476
2477 btrfs_record_root_in_trans(trans, dest);
2478
2479 memset(&dest->root_item.drop_progress, 0,
2480 sizeof(dest->root_item.drop_progress));
2481 dest->root_item.drop_level = 0;
2482 btrfs_set_root_refs(&dest->root_item, 0);
2483
Miao Xie27cdeb72014-04-02 19:51:05 +08002484 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002485 ret = btrfs_insert_orphan_item(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04002487 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002488 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002489 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002490 err = ret;
2491 goto out_end_trans;
2492 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002493 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002494
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002495 ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002496 BTRFS_UUID_KEY_SUBVOL,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002497 dest->root_key.objectid);
2498 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002499 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002500 err = ret;
2501 goto out_end_trans;
2502 }
2503 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002504 ret = btrfs_uuid_tree_rem(trans, fs_info,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002505 dest->root_item.received_uuid,
2506 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2507 dest->root_key.objectid);
2508 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002509 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002510 err = ret;
2511 goto out_end_trans;
2512 }
2513 }
2514
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002515out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002516 trans->block_rsv = NULL;
2517 trans->bytes_reserved = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002518 ret = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002519 if (ret && !err)
2520 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002521 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002522out_release:
David Sterba7775c812017-02-10 19:18:18 +01002523 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Yan, Zheng76dda932009-09-21 16:00:26 -04002524out_up_write:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002525 up_write(&fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002526 if (err) {
2527 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002528 root_flags = btrfs_root_flags(&dest->root_item);
2529 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002530 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2531 spin_unlock(&dest->root_item_lock);
2532 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002533out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002534 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002535 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002536 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002537 btrfs_invalidate_inodes(dest);
2538 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002539 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002540
2541 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002542 if (dest->ino_cache_inode) {
2543 iput(dest->ino_cache_inode);
2544 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002545 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002546 }
2547out_dput:
2548 dput(dentry);
2549out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002550 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002551out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002552 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002553out:
2554 kfree(vol_args);
2555 return err;
2556}
2557
Chris Mason1e701a32010-03-11 09:42:04 -05002558static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002559{
Al Viro496ad9a2013-01-23 17:07:38 -05002560 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002561 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002562 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002563 int ret;
2564
Ilya Dryomov25122d12013-01-20 15:57:57 +02002565 ret = mnt_want_write_file(file);
2566 if (ret)
2567 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002568
Ilya Dryomov25122d12013-01-20 15:57:57 +02002569 if (btrfs_root_readonly(root)) {
2570 ret = -EROFS;
2571 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002572 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002573
2574 switch (inode->i_mode & S_IFMT) {
2575 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002576 if (!capable(CAP_SYS_ADMIN)) {
2577 ret = -EPERM;
2578 goto out;
2579 }
Eric Sandeende78b512013-01-31 18:21:12 +00002580 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002581 break;
2582 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002583 if (!(file->f_mode & FMODE_WRITE)) {
2584 ret = -EINVAL;
2585 goto out;
2586 }
Chris Mason1e701a32010-03-11 09:42:04 -05002587
2588 range = kzalloc(sizeof(*range), GFP_KERNEL);
2589 if (!range) {
2590 ret = -ENOMEM;
2591 goto out;
2592 }
2593
2594 if (argp) {
2595 if (copy_from_user(range, argp,
2596 sizeof(*range))) {
2597 ret = -EFAULT;
2598 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002599 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002600 }
2601 /* compression requires us to start the IO */
2602 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2603 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2604 range->extent_thresh = (u32)-1;
2605 }
2606 } else {
2607 /* the rest are all set to zero by kzalloc */
2608 range->len = (u64)-1;
2609 }
Al Viro496ad9a2013-01-23 17:07:38 -05002610 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002611 range, 0, 0);
2612 if (ret > 0)
2613 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002614 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002615 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002616 default:
2617 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002618 }
Chris Masone441d542009-01-05 16:57:23 -05002619out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002620 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002621 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002622}
2623
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002624static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002625{
2626 struct btrfs_ioctl_vol_args *vol_args;
2627 int ret;
2628
Chris Masone441d542009-01-05 16:57:23 -05002629 if (!capable(CAP_SYS_ADMIN))
2630 return -EPERM;
2631
David Sterba171938e2017-03-28 14:44:21 +02002632 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002633 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002634
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002635 mutex_lock(&fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002636 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002637 if (IS_ERR(vol_args)) {
2638 ret = PTR_ERR(vol_args);
2639 goto out;
2640 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002641
Mark Fasheh5516e592008-07-24 12:20:14 -04002642 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002643 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002644
Anand Jain43d20762014-07-01 00:58:56 +08002645 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002646 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002647
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002648 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002649out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02002651 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002652 return ret;
2653}
2654
Anand Jain6b526ed2016-02-13 10:01:39 +08002655static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002656{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002657 struct inode *inode = file_inode(file);
2658 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002659 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002660 int ret;
2661
Chris Masone441d542009-01-05 16:57:23 -05002662 if (!capable(CAP_SYS_ADMIN))
2663 return -EPERM;
2664
Miao Xieda249272012-11-26 08:44:50 +00002665 ret = mnt_want_write_file(file);
2666 if (ret)
2667 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002668
Li Zefandae7b662009-04-08 15:06:54 +08002669 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002670 if (IS_ERR(vol_args)) {
2671 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002672 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002673 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002674
Anand Jain6b526ed2016-02-13 10:01:39 +08002675 /* Check for compatibility reject unknown flags */
David Sterba735654e2016-02-15 18:15:21 +01002676 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2677 return -EOPNOTSUPP;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002678
David Sterba171938e2017-03-28 14:44:21 +02002679 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002680 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2681 goto out;
2682 }
2683
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002684 mutex_lock(&fs_info->volume_mutex);
David Sterba735654e2016-02-15 18:15:21 +01002685 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002686 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002687 } else {
2688 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002689 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002690 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002691 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02002692 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002693
Anand Jain6b526ed2016-02-13 10:01:39 +08002694 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002695 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002696 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002697 vol_args->devid);
2698 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002699 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002700 vol_args->name);
2701 }
Anand Jain183860f2013-05-17 10:52:45 +00002702out:
2703 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002704err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002705 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002706 return ret;
2707}
2708
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002709static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2710{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002711 struct inode *inode = file_inode(file);
2712 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002713 struct btrfs_ioctl_vol_args *vol_args;
2714 int ret;
2715
2716 if (!capable(CAP_SYS_ADMIN))
2717 return -EPERM;
2718
2719 ret = mnt_want_write_file(file);
2720 if (ret)
2721 return ret;
2722
David Sterba171938e2017-03-28 14:44:21 +02002723 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002724 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002725 goto out_drop_write;
2726 }
2727
2728 vol_args = memdup_user(arg, sizeof(*vol_args));
2729 if (IS_ERR(vol_args)) {
2730 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002731 goto out;
2732 }
2733
David Sterba58d7bbf2016-05-04 14:10:47 +02002734 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002735 mutex_lock(&fs_info->volume_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002736 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002737 mutex_unlock(&fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002738
2739 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002740 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002741 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002742out:
David Sterba171938e2017-03-28 14:44:21 +02002743 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002744out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002745 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002746
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002747 return ret;
2748}
2749
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002750static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2751 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002752{
Li Zefan027ed2f2011-06-08 08:27:56 +00002753 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002754 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002755 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002756 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002757
Li Zefan027ed2f2011-06-08 08:27:56 +00002758 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2759 if (!fi_args)
2760 return -ENOMEM;
2761
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002762 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002763 fi_args->num_devices = fs_devices->num_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002764 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002765
Byongho Leed7641a42015-09-01 23:10:57 +09002766 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002767 if (device->devid > fi_args->max_id)
2768 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002769 }
2770 mutex_unlock(&fs_devices->device_list_mutex);
2771
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07002772 fi_args->nodesize = fs_info->nodesize;
2773 fi_args->sectorsize = fs_info->sectorsize;
2774 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002775
Li Zefan027ed2f2011-06-08 08:27:56 +00002776 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2777 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002778
Li Zefan027ed2f2011-06-08 08:27:56 +00002779 kfree(fi_args);
2780 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002781}
2782
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002783static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2784 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002785{
2786 struct btrfs_ioctl_dev_info_args *di_args;
2787 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002788 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002789 int ret = 0;
2790 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002791
Jan Schmidt475f6382011-03-11 15:41:01 +01002792 di_args = memdup_user(arg, sizeof(*di_args));
2793 if (IS_ERR(di_args))
2794 return PTR_ERR(di_args);
2795
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002796 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002797 s_uuid = di_args->uuid;
2798
2799 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002800 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002801
2802 if (!dev) {
2803 ret = -ENODEV;
2804 goto out;
2805 }
2806
2807 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002808 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2809 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002810 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002811 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002812 struct rcu_string *name;
2813
2814 rcu_read_lock();
2815 name = rcu_dereference(dev->name);
2816 strncpy(di_args->path, name->str, sizeof(di_args->path));
2817 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002818 di_args->path[sizeof(di_args->path) - 1] = 0;
2819 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002820 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002821 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002822
2823out:
David Sterba55793c02013-04-26 15:20:23 +00002824 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002825 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2826 ret = -EFAULT;
2827
2828 kfree(di_args);
2829 return ret;
2830}
2831
Mark Fashehf4414602015-06-30 14:42:05 -07002832static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002833{
2834 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002835
Mark Fasheh416161d2013-08-06 11:42:51 -07002836 page = grab_cache_page(inode->i_mapping, index);
2837 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002838 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002839
2840 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002841 int ret;
2842
2843 ret = btrfs_readpage(NULL, page);
2844 if (ret)
2845 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002846 lock_page(page);
2847 if (!PageUptodate(page)) {
2848 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002849 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002850 return ERR_PTR(-EIO);
2851 }
2852 if (page->mapping != inode->i_mapping) {
2853 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002854 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002855 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002856 }
2857 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002858
2859 return page;
2860}
2861
Mark Fashehf4414602015-06-30 14:42:05 -07002862static int gather_extent_pages(struct inode *inode, struct page **pages,
2863 int num_pages, u64 off)
2864{
2865 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002866 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002867
2868 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002869again:
Mark Fashehf4414602015-06-30 14:42:05 -07002870 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002871 if (IS_ERR(pages[i])) {
2872 int err = PTR_ERR(pages[i]);
2873
2874 if (err == -EAGAIN)
2875 goto again;
2876 pages[i] = NULL;
2877 return err;
2878 }
Mark Fashehf4414602015-06-30 14:42:05 -07002879 }
2880 return 0;
2881}
2882
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002883static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2884 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002885{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002886 /*
2887 * Do any pending delalloc/csum calculations on inode, one way or
2888 * another, and lock file content.
2889 * The locking order is:
2890 *
2891 * 1) pages
2892 * 2) range in the inode's io tree
2893 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002894 while (1) {
2895 struct btrfs_ordered_extent *ordered;
2896 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2897 ordered = btrfs_lookup_first_ordered_extent(inode,
2898 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002899 if ((!ordered ||
2900 ordered->file_offset + ordered->len <= off ||
2901 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002902 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002903 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2904 if (ordered)
2905 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002906 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002907 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002908 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2909 if (ordered)
2910 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002911 if (!retry_range_locking)
2912 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002913 btrfs_wait_ordered_range(inode, off, len);
2914 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002915 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002916}
2917
Mark Fashehf4414602015-06-30 14:42:05 -07002918static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002919{
Al Viro59551022016-01-22 15:40:57 -05002920 inode_unlock(inode1);
2921 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002922}
2923
Mark Fashehf4414602015-06-30 14:42:05 -07002924static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2925{
2926 if (inode1 < inode2)
2927 swap(inode1, inode2);
2928
Al Viro59551022016-01-22 15:40:57 -05002929 inode_lock_nested(inode1, I_MUTEX_PARENT);
2930 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002931}
2932
2933static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2934 struct inode *inode2, u64 loff2, u64 len)
2935{
2936 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2937 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2938}
2939
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002940static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2941 struct inode *inode2, u64 loff2, u64 len,
2942 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002943{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002944 int ret;
2945
Mark Fasheh416161d2013-08-06 11:42:51 -07002946 if (inode1 < inode2) {
2947 swap(inode1, inode2);
2948 swap(loff1, loff2);
2949 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002950 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2951 if (ret)
2952 return ret;
2953 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2954 if (ret)
2955 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2956 loff1 + len - 1);
2957 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002958}
2959
2960struct cmp_pages {
2961 int num_pages;
2962 struct page **src_pages;
2963 struct page **dst_pages;
2964};
2965
2966static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2967{
2968 int i;
2969 struct page *pg;
2970
2971 for (i = 0; i < cmp->num_pages; i++) {
2972 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002973 if (pg) {
2974 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002975 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002976 }
Mark Fashehf4414602015-06-30 14:42:05 -07002977 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002978 if (pg) {
2979 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002980 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002981 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002982 }
Mark Fashehf4414602015-06-30 14:42:05 -07002983 kfree(cmp->src_pages);
2984 kfree(cmp->dst_pages);
2985}
2986
2987static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2988 struct inode *dst, u64 dst_loff,
2989 u64 len, struct cmp_pages *cmp)
2990{
2991 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002992 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002993 struct page **src_pgarr, **dst_pgarr;
2994
2995 /*
2996 * We must gather up all the pages before we initiate our
2997 * extent locking. We use an array for the page pointers. Size
2998 * of the array is bounded by len, which is in turn bounded by
2999 * BTRFS_MAX_DEDUPE_LEN.
3000 */
David Sterba66722f72016-02-11 15:01:38 +01003001 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
3002 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07003003 if (!src_pgarr || !dst_pgarr) {
3004 kfree(src_pgarr);
3005 kfree(dst_pgarr);
3006 return -ENOMEM;
3007 }
3008 cmp->num_pages = num_pages;
3009 cmp->src_pages = src_pgarr;
3010 cmp->dst_pages = dst_pgarr;
3011
Filipe Mananab1517622017-02-21 17:14:52 +00003012 /*
3013 * If deduping ranges in the same inode, locking rules make it mandatory
3014 * to always lock pages in ascending order to avoid deadlocks with
3015 * concurrent tasks (such as starting writeback/delalloc).
3016 */
3017 if (src == dst && dst_loff < loff) {
3018 swap(src_pgarr, dst_pgarr);
3019 swap(loff, dst_loff);
3020 }
3021
3022 ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003023 if (ret)
3024 goto out;
3025
Filipe Mananab1517622017-02-21 17:14:52 +00003026 ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003027
3028out:
3029 if (ret)
3030 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09003031 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003032}
3033
David Sterba1a287cf2017-02-10 20:15:10 +01003034static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003035{
3036 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003037 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003038 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003039 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003040 void *addr, *dst_addr;
3041
Mark Fashehf4414602015-06-30 14:42:05 -07003042 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003043 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003044 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003045 cmp_len = len;
3046
Mark Fashehf4414602015-06-30 14:42:05 -07003047 BUG_ON(i >= cmp->num_pages);
3048
3049 src_page = cmp->src_pages[i];
3050 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003051 ASSERT(PageLocked(src_page));
3052 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003053
Mark Fasheh416161d2013-08-06 11:42:51 -07003054 addr = kmap_atomic(src_page);
3055 dst_addr = kmap_atomic(dst_page);
3056
3057 flush_dcache_page(src_page);
3058 flush_dcache_page(dst_page);
3059
3060 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003061 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003062
3063 kunmap_atomic(addr);
3064 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003065
3066 if (ret)
3067 break;
3068
Mark Fasheh416161d2013-08-06 11:42:51 -07003069 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003070 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003071 }
3072
3073 return ret;
3074}
3075
Mark Fashehe1d227a2015-06-08 15:05:25 -07003076static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3077 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003078{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003079 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003080 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3081
Mark Fashehe1d227a2015-06-08 15:05:25 -07003082 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003083 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003084
3085 /* if we extend to eof, continue to block boundary */
3086 if (off + len == inode->i_size)
3087 *plen = len = ALIGN(inode->i_size, bs) - off;
3088
Mark Fasheh416161d2013-08-06 11:42:51 -07003089 /* Check that we are block aligned - btrfs_clone() requires this */
3090 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3091 return -EINVAL;
3092
3093 return 0;
3094}
3095
Mark Fashehe1d227a2015-06-08 15:05:25 -07003096static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003097 struct inode *dst, u64 dst_loff)
3098{
3099 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003100 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003101 struct cmp_pages cmp;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003102 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003103 u64 same_lock_start = 0;
3104 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003105
Filipe Manana113e8282015-03-30 18:26:47 +01003106 if (len == 0)
3107 return 0;
3108
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003109 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003110 inode_lock(src);
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003111 else
3112 btrfs_double_inode_lock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003113
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003114 ret = extent_same_check_offsets(src, loff, &len, olen);
3115 if (ret)
3116 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003117
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003118 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3119 if (ret)
3120 goto out_unlock;
3121
3122 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003123 /*
3124 * Single inode case wants the same checks, except we
3125 * don't want our length pushed out past i_size as
3126 * comparing that data range makes no sense.
3127 *
3128 * extent_same_check_offsets() will do this for an
3129 * unaligned length at i_size, so catch it here and
3130 * reject the request.
3131 *
3132 * This effectively means we require aligned extents
3133 * for the single-inode case, whereas the other cases
3134 * allow an unaligned length so long as it ends at
3135 * i_size.
3136 */
3137 if (len != olen) {
3138 ret = -EINVAL;
3139 goto out_unlock;
3140 }
3141
3142 /* Check for overlapping ranges */
3143 if (dst_loff + len > loff && dst_loff < loff + len) {
3144 ret = -EINVAL;
3145 goto out_unlock;
3146 }
3147
3148 same_lock_start = min_t(u64, loff, dst_loff);
3149 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003150 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003151
3152 /* don't make the dst file partly checksummed */
3153 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3154 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3155 ret = -EINVAL;
3156 goto out_unlock;
3157 }
3158
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003159again:
Mark Fashehf4414602015-06-30 14:42:05 -07003160 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3161 if (ret)
3162 goto out_unlock;
3163
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003164 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003165 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3166 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003167 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003168 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3169 false);
3170 /*
3171 * If one of the inodes has dirty pages in the respective range or
3172 * ordered extents, we need to flush dellaloc and wait for all ordered
3173 * extents in the range. We must unlock the pages and the ranges in the
3174 * io trees to avoid deadlocks when flushing delalloc (requires locking
3175 * pages) and when waiting for ordered extents to complete (they require
3176 * range locking).
3177 */
3178 if (ret == -EAGAIN) {
3179 /*
3180 * Ranges in the io trees already unlocked. Now unlock all
3181 * pages before waiting for all IO to complete.
3182 */
3183 btrfs_cmp_data_free(&cmp);
3184 if (same_inode) {
3185 btrfs_wait_ordered_range(src, same_lock_start,
3186 same_lock_len);
3187 } else {
3188 btrfs_wait_ordered_range(src, loff, len);
3189 btrfs_wait_ordered_range(dst, dst_loff, len);
3190 }
3191 goto again;
3192 }
3193 ASSERT(ret == 0);
3194 if (WARN_ON(ret)) {
3195 /* ranges in the io trees already unlocked */
3196 btrfs_cmp_data_free(&cmp);
3197 return ret;
3198 }
Mark Fashehf4414602015-06-30 14:42:05 -07003199
Mark Fasheh207910d2015-06-30 14:42:04 -07003200 /* pass original length for comparison so we stay within i_size */
David Sterba1a287cf2017-02-10 20:15:10 +01003201 ret = btrfs_cmp_data(olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003202 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003203 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003204
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003205 if (same_inode)
3206 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3207 same_lock_start + same_lock_len - 1);
3208 else
3209 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003210
3211 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003212out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003213 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003214 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003215 else
3216 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003217
3218 return ret;
3219}
3220
Byongho Leeee221842015-12-15 01:42:10 +09003221#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003222
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003223ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3224 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003225{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003226 struct inode *src = file_inode(src_file);
3227 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003228 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003229 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003230
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003231 if (olen > BTRFS_MAX_DEDUPE_LEN)
3232 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003233
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003234 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003235 /*
3236 * Btrfs does not support blocksize < page_size. As a
3237 * result, btrfs_cmp_data() won't correctly handle
3238 * this situation without an update.
3239 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003240 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003241 }
3242
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003243 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3244 if (res)
3245 return res;
3246 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003247}
3248
Filipe Mananaf82a9902014-06-01 01:50:28 +01003249static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3250 struct inode *inode,
3251 u64 endoff,
3252 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003253 const u64 olen,
3254 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003255{
3256 struct btrfs_root *root = BTRFS_I(inode)->root;
3257 int ret;
3258
3259 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003260 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003261 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003262 /*
3263 * We round up to the block size at eof when determining which
3264 * extents to clone above, but shouldn't round up the file size.
3265 */
3266 if (endoff > destoff + olen)
3267 endoff = destoff + olen;
3268 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003269 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003270
3271 ret = btrfs_update_inode(trans, root, inode);
3272 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003273 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003274 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003275 goto out;
3276 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003277 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003278out:
3279 return ret;
3280}
3281
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003282static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003283 const struct btrfs_trans_handle *trans,
3284 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003285 const u64 hole_offset,
3286 const u64 hole_len)
3287{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003288 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003289 struct extent_map *em;
3290 int ret;
3291
3292 em = alloc_extent_map();
3293 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003294 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003295 return;
3296 }
3297
Filipe Manana14f59792014-06-29 21:45:40 +01003298 if (path) {
3299 struct btrfs_file_extent_item *fi;
3300
3301 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3302 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003303 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003304 em->generation = -1;
3305 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3306 BTRFS_FILE_EXTENT_INLINE)
3307 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003308 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003309 } else {
3310 em->start = hole_offset;
3311 em->len = hole_len;
3312 em->ram_bytes = em->len;
3313 em->orig_start = hole_offset;
3314 em->block_start = EXTENT_MAP_HOLE;
3315 em->block_len = 0;
3316 em->orig_block_len = 0;
3317 em->compress_type = BTRFS_COMPRESS_NONE;
3318 em->generation = trans->transid;
3319 }
3320
3321 while (1) {
3322 write_lock(&em_tree->lock);
3323 ret = add_extent_mapping(em_tree, em, 1);
3324 write_unlock(&em_tree->lock);
3325 if (ret != -EEXIST) {
3326 free_extent_map(em);
3327 break;
3328 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003329 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003330 em->start + em->len - 1, 0);
3331 }
3332
David Sterbaee39b432014-09-30 01:33:33 +02003333 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003334 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003335}
3336
Filipe Manana8039d872015-10-13 15:15:00 +01003337/*
3338 * Make sure we do not end up inserting an inline extent into a file that has
3339 * already other (non-inline) extents. If a file has an inline extent it can
3340 * not have any other extents and the (single) inline extent must start at the
3341 * file offset 0. Failing to respect these rules will lead to file corruption,
3342 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3343 *
3344 * We can have extents that have been already written to disk or we can have
3345 * dirty ranges still in delalloc, in which case the extent maps and items are
3346 * created only when we run delalloc, and the delalloc ranges might fall outside
3347 * the range we are currently locking in the inode's io tree. So we check the
3348 * inode's i_size because of that (i_size updates are done while holding the
3349 * i_mutex, which we are holding here).
3350 * We also check to see if the inode has a size not greater than "datal" but has
3351 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3352 * protected against such concurrent fallocate calls by the i_mutex).
3353 *
3354 * If the file has no extents but a size greater than datal, do not allow the
3355 * copy because we would need turn the inline extent into a non-inline one (even
3356 * with NO_HOLES enabled). If we find our destination inode only has one inline
3357 * extent, just overwrite it with the source inline extent if its size is less
3358 * than the source extent's size, or we could copy the source inline extent's
3359 * data into the destination inode's inline extent if the later is greater then
3360 * the former.
3361 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003362static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003363 struct btrfs_trans_handle *trans,
3364 struct btrfs_path *path,
3365 struct btrfs_key *new_key,
3366 const u64 drop_start,
3367 const u64 datal,
3368 const u64 skip,
3369 const u64 size,
3370 char *inline_data)
3371{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003372 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003373 struct btrfs_root *root = BTRFS_I(dst)->root;
3374 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003376 int ret;
3377 struct btrfs_key key;
3378
3379 if (new_key->offset > 0)
3380 return -EOPNOTSUPP;
3381
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003382 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003383 key.type = BTRFS_EXTENT_DATA_KEY;
3384 key.offset = 0;
3385 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3386 if (ret < 0) {
3387 return ret;
3388 } else if (ret > 0) {
3389 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3390 ret = btrfs_next_leaf(root, path);
3391 if (ret < 0)
3392 return ret;
3393 else if (ret > 0)
3394 goto copy_inline_extent;
3395 }
3396 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003397 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003398 key.type == BTRFS_EXTENT_DATA_KEY) {
3399 ASSERT(key.offset > 0);
3400 return -EOPNOTSUPP;
3401 }
3402 } else if (i_size_read(dst) <= datal) {
3403 struct btrfs_file_extent_item *ei;
3404 u64 ext_len;
3405
3406 /*
3407 * If the file size is <= datal, make sure there are no other
3408 * extents following (can happen do to an fallocate call with
3409 * the flag FALLOC_FL_KEEP_SIZE).
3410 */
3411 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3412 struct btrfs_file_extent_item);
3413 /*
3414 * If it's an inline extent, it can not have other extents
3415 * following it.
3416 */
3417 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3418 BTRFS_FILE_EXTENT_INLINE)
3419 goto copy_inline_extent;
3420
3421 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3422 if (ext_len > aligned_end)
3423 return -EOPNOTSUPP;
3424
3425 ret = btrfs_next_item(root, path);
3426 if (ret < 0) {
3427 return ret;
3428 } else if (ret == 0) {
3429 btrfs_item_key_to_cpu(path->nodes[0], &key,
3430 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003431 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003432 key.type == BTRFS_EXTENT_DATA_KEY)
3433 return -EOPNOTSUPP;
3434 }
3435 }
3436
3437copy_inline_extent:
3438 /*
3439 * We have no extent items, or we have an extent at offset 0 which may
3440 * or may not be inlined. All these cases are dealt the same way.
3441 */
3442 if (i_size_read(dst) > datal) {
3443 /*
3444 * If the destination inode has an inline extent...
3445 * This would require copying the data from the source inline
3446 * extent into the beginning of the destination's inline extent.
3447 * But this is really complex, both extents can be compressed
3448 * or just one of them, which would require decompressing and
3449 * re-compressing data (which could increase the new compressed
3450 * size, not allowing the compressed data to fit anymore in an
3451 * inline extent).
3452 * So just don't support this case for now (it should be rare,
3453 * we are not really saving space when cloning inline extents).
3454 */
3455 return -EOPNOTSUPP;
3456 }
3457
3458 btrfs_release_path(path);
3459 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3460 if (ret)
3461 return ret;
3462 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3463 if (ret)
3464 return ret;
3465
3466 if (skip) {
3467 const u32 start = btrfs_file_extent_calc_inline_size(0);
3468
3469 memmove(inline_data + start, inline_data + start + skip, datal);
3470 }
3471
3472 write_extent_buffer(path->nodes[0], inline_data,
3473 btrfs_item_ptr_offset(path->nodes[0],
3474 path->slots[0]),
3475 size);
3476 inode_add_bytes(dst, datal);
3477
3478 return 0;
3479}
3480
Mark Fasheh32b7c682013-08-06 11:42:49 -07003481/**
3482 * btrfs_clone() - clone a range from inode file to another
3483 *
3484 * @src: Inode to clone from
3485 * @inode: Inode to clone to
3486 * @off: Offset within source to start clone from
3487 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003488 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003489 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003490 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003491 */
3492static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003493 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003494 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003495{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003496 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003497 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003498 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003499 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003500 struct btrfs_trans_handle *trans;
3501 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003502 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003503 u32 nritems;
3504 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003505 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003506 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003507 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003508
3509 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003510 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3511 if (!buf)
3512 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003513
3514 path = btrfs_alloc_path();
3515 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003516 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003517 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003518 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003519
David Sterbae4058b52015-11-27 16:31:35 +01003520 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003521 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003522 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003523 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003524 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003525
3526 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003527 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003528
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003529 /*
3530 * note the key will change type as we walk through the
3531 * tree.
3532 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003533 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003534 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3535 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003536 if (ret < 0)
3537 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003538 /*
3539 * First search, if no extent item that starts at offset off was
3540 * found but the previous item is an extent item, it's possible
3541 * it might overlap our target range, therefore process it.
3542 */
3543 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3544 btrfs_item_key_to_cpu(path->nodes[0], &key,
3545 path->slots[0] - 1);
3546 if (key.type == BTRFS_EXTENT_DATA_KEY)
3547 path->slots[0]--;
3548 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003549
Yan Zhengae01a0a2008-08-04 23:23:47 -04003550 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003551process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003552 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003553 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003554 if (ret < 0)
3555 goto out;
3556 if (ret > 0)
3557 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003558 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003559 }
3560 leaf = path->nodes[0];
3561 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003562
Yan Zhengae01a0a2008-08-04 23:23:47 -04003563 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003564 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003565 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003566 break;
3567
David Sterba962a2982014-06-04 18:41:45 +02003568 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003569 struct btrfs_file_extent_item *extent;
3570 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003571 u32 size;
3572 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003573 u64 disko = 0, diskl = 0;
3574 u64 datao = 0, datal = 0;
3575 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003576 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003577
Sage Weilc5c9cd42008-11-12 14:32:25 -05003578 extent = btrfs_item_ptr(leaf, slot,
3579 struct btrfs_file_extent_item);
3580 comp = btrfs_file_extent_compression(leaf, extent);
3581 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003582 if (type == BTRFS_FILE_EXTENT_REG ||
3583 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003584 disko = btrfs_file_extent_disk_bytenr(leaf,
3585 extent);
3586 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3587 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003588 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003589 datal = btrfs_file_extent_num_bytes(leaf,
3590 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003591 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3592 /* take upper bound, may be compressed */
3593 datal = btrfs_file_extent_ram_bytes(leaf,
3594 extent);
3595 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003596
Filipe Manana2c463822014-05-31 02:31:05 +01003597 /*
3598 * The first search might have left us at an extent
3599 * item that ends before our target range's start, can
3600 * happen if we have holes and NO_HOLES feature enabled.
3601 */
3602 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003603 path->slots[0]++;
3604 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003605 } else if (key.offset >= off + len) {
3606 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003607 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003608 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003609 size = btrfs_item_size_nr(leaf, slot);
3610 read_extent_buffer(leaf, buf,
3611 btrfs_item_ptr_offset(leaf, slot),
3612 size);
3613
3614 btrfs_release_path(path);
3615 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003616
Zheng Yan31840ae2008-09-23 13:14:14 -04003617 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003618 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003619 if (off <= key.offset)
3620 new_key.offset = key.offset + destoff - off;
3621 else
3622 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003623
Sage Weilb6f34092011-09-20 14:48:51 -04003624 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003625 * Deal with a hole that doesn't have an extent item
3626 * that represents it (NO_HOLES feature enabled).
3627 * This hole is either in the middle of the cloning
3628 * range or at the beginning (fully overlaps it or
3629 * partially overlaps it).
3630 */
3631 if (new_key.offset != last_dest_end)
3632 drop_start = last_dest_end;
3633 else
3634 drop_start = new_key.offset;
3635
3636 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003637 * 1 - adjusting old extent (we may have to split it)
3638 * 1 - add new extent
3639 * 1 - inode update
3640 */
3641 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003642 if (IS_ERR(trans)) {
3643 ret = PTR_ERR(trans);
3644 goto out;
3645 }
3646
Chris Masonc8a894d2009-06-27 21:07:03 -04003647 if (type == BTRFS_FILE_EXTENT_REG ||
3648 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003649 /*
3650 * a | --- range to clone ---| b
3651 * | ------------- extent ------------- |
3652 */
3653
Antonio Ospite93915582014-06-04 14:03:48 +02003654 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003655 if (key.offset + datal > off + len)
3656 datal = off + len - key.offset;
3657
Antonio Ospite93915582014-06-04 14:03:48 +02003658 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003659 if (off > key.offset) {
3660 datao += off - key.offset;
3661 datal -= off - key.offset;
3662 }
3663
Josef Bacik5dc562c2012-08-17 13:14:17 -04003664 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003665 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003666 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003667 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003668 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003669 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003670 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003671 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003672 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003673 goto out;
3674 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003675
Sage Weilc5c9cd42008-11-12 14:32:25 -05003676 ret = btrfs_insert_empty_item(trans, root, path,
3677 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003678 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003679 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003680 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003681 goto out;
3682 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003683
3684 leaf = path->nodes[0];
3685 slot = path->slots[0];
3686 write_extent_buffer(leaf, buf,
3687 btrfs_item_ptr_offset(leaf, slot),
3688 size);
3689
3690 extent = btrfs_item_ptr(leaf, slot,
3691 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003692
Sage Weilc5c9cd42008-11-12 14:32:25 -05003693 /* disko == 0 means it's a hole */
3694 if (!disko)
3695 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003696
3697 btrfs_set_file_extent_offset(leaf, extent,
3698 datao);
3699 btrfs_set_file_extent_num_bytes(leaf, extent,
3700 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003701
Sage Weilc5c9cd42008-11-12 14:32:25 -05003702 if (disko) {
3703 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003704 ret = btrfs_inc_extent_ref(trans,
3705 fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003706 disko, diskl, 0,
3707 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003708 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003709 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003710 if (ret) {
3711 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003712 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003713 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003714 goto out;
3715
3716 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003717 }
3718 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3719 u64 skip = 0;
3720 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003721
Sage Weilc5c9cd42008-11-12 14:32:25 -05003722 if (off > key.offset) {
3723 skip = off - key.offset;
3724 new_key.offset += skip;
3725 }
Chris Masond3977122009-01-05 21:25:51 -05003726
Liu Boaa42ffd2012-09-18 03:52:23 -06003727 if (key.offset + datal > off + len)
3728 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003729
Sage Weilc5c9cd42008-11-12 14:32:25 -05003730 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003731 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003732 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003733 goto out;
3734 }
3735 size -= skip + trim;
3736 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003737
David Sterba4a0ab9d2017-02-10 20:18:49 +01003738 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003739 trans, path,
3740 &new_key,
3741 drop_start,
3742 datal,
3743 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003744 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003745 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003746 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003747 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003748 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003749 goto out;
3750 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003751 leaf = path->nodes[0];
3752 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003753 }
3754
Filipe Manana7ffbb592014-06-09 03:48:05 +01003755 /* If we have an implicit hole (NO_HOLES feature). */
3756 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003757 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003758 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003759 new_key.offset - drop_start);
3760
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003761 clone_update_extent_map(BTRFS_I(inode), trans,
3762 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003763
Sage Weilc5c9cd42008-11-12 14:32:25 -05003764 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003765 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003766
Filipe Manana62e23902014-08-08 02:47:06 +01003767 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003768 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003769 ret = clone_finish_inode_update(trans, inode,
3770 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003771 destoff, olen,
3772 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003773 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003774 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003775 if (new_key.offset + datal >= destoff + len)
3776 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003777 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003778 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003779 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003780
3781 if (fatal_signal_pending(current)) {
3782 ret = -EINTR;
3783 goto out;
3784 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003785 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003786 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003787
Filipe Mananaf82a9902014-06-01 01:50:28 +01003788 if (last_dest_end < destoff + len) {
3789 /*
3790 * We have an implicit hole (NO_HOLES feature is enabled) that
3791 * fully or partially overlaps our cloning range at its end.
3792 */
3793 btrfs_release_path(path);
3794
3795 /*
3796 * 1 - remove extent(s)
3797 * 1 - inode update
3798 */
3799 trans = btrfs_start_transaction(root, 2);
3800 if (IS_ERR(trans)) {
3801 ret = PTR_ERR(trans);
3802 goto out;
3803 }
3804 ret = btrfs_drop_extents(trans, root, inode,
3805 last_dest_end, destoff + len, 1);
3806 if (ret) {
3807 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003808 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003809 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003810 goto out;
3811 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003812 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3813 last_dest_end,
3814 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003815 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003816 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003817 }
3818
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003819out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003820 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003821 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003822 return ret;
3823}
3824
Zach Brown3db11b22015-11-10 16:53:32 -05003825static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3826 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003827{
Al Viro54563d42013-09-01 15:57:51 -04003828 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003829 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003830 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003831 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003832 int ret;
3833 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003834 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003835 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003836
3837 /*
3838 * TODO:
3839 * - split compressed inline extents. annoying: we need to
3840 * decompress into destination's address_space (the file offset
3841 * may change, so source mapping won't do), then recompress (or
3842 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003843 *
3844 * - split destination inode's inline extents. The inline extents can
3845 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003846 */
3847
Mark Fasheh32b7c682013-08-06 11:42:49 -07003848 if (btrfs_root_readonly(root))
3849 return -EROFS;
3850
Zach Brown3db11b22015-11-10 16:53:32 -05003851 if (file_src->f_path.mnt != file->f_path.mnt ||
3852 src->i_sb != inode->i_sb)
3853 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003854
3855 /* don't make the dst file partly checksummed */
3856 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3857 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003858 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003859
Mark Fasheh32b7c682013-08-06 11:42:49 -07003860 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003861 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003862
3863 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003864 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003865 } else {
Al Viro59551022016-01-22 15:40:57 -05003866 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003867 }
3868
3869 /* determine range to clone */
3870 ret = -EINVAL;
3871 if (off + len > src->i_size || off + len < off)
3872 goto out_unlock;
3873 if (len == 0)
3874 olen = len = src->i_size - off;
3875 /* if we extend to eof, continue to block boundary */
3876 if (off + len == src->i_size)
3877 len = ALIGN(src->i_size, bs) - off;
3878
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003879 if (len == 0) {
3880 ret = 0;
3881 goto out_unlock;
3882 }
3883
Mark Fasheh32b7c682013-08-06 11:42:49 -07003884 /* verify the end result is block aligned */
3885 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3886 !IS_ALIGNED(destoff, bs))
3887 goto out_unlock;
3888
3889 /* verify if ranges are overlapped within the same file */
3890 if (same_inode) {
3891 if (destoff + len > off && destoff < off + len)
3892 goto out_unlock;
3893 }
3894
3895 if (destoff > inode->i_size) {
3896 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3897 if (ret)
3898 goto out_unlock;
3899 }
3900
Filipe Mananac125b8b2014-05-23 05:03:34 +01003901 /*
3902 * Lock the target range too. Right after we replace the file extent
3903 * items in the fs tree (which now point to the cloned data), we might
3904 * have a worker replace them with extent items relative to a write
3905 * operation that was issued before this clone operation (i.e. confront
3906 * with inode.c:btrfs_finish_ordered_io).
3907 */
3908 if (same_inode) {
3909 u64 lock_start = min_t(u64, off, destoff);
3910 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003911
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003912 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003913 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003914 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3915 true);
3916 }
3917 ASSERT(ret == 0);
3918 if (WARN_ON(ret)) {
3919 /* ranges in the io trees already unlocked */
3920 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003921 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003922
Mark Fasheh1c919a52015-06-30 14:42:08 -07003923 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003924
Filipe Mananac125b8b2014-05-23 05:03:34 +01003925 if (same_inode) {
3926 u64 lock_start = min_t(u64, off, destoff);
3927 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3928
3929 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3930 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003931 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003932 }
3933 /*
3934 * Truncate page cache pages so that future reads will see the cloned
3935 * data immediately and not the previous data.
3936 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303937 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003938 round_down(destoff, PAGE_SIZE),
3939 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003940out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003941 if (!same_inode)
3942 btrfs_double_inode_unlock(src, inode);
3943 else
Al Viro59551022016-01-22 15:40:57 -05003944 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003945 return ret;
3946}
3947
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003948int btrfs_clone_file_range(struct file *src_file, loff_t off,
3949 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003950{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003951 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003952}
3953
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003954/*
3955 * there are many ways the trans_start and trans_end ioctls can lead
3956 * to deadlocks. They should only be used by applications that
3957 * basically own the machine, and have a very in depth understanding
3958 * of all the possible deadlocks and enospc problems.
3959 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003960static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003961{
Al Viro496ad9a2013-01-23 17:07:38 -05003962 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003963 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003964 struct btrfs_root *root = BTRFS_I(inode)->root;
3965 struct btrfs_trans_handle *trans;
Josef Bacik23b5ec72017-07-24 15:14:25 -04003966 struct btrfs_file_private *private;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003967 int ret;
Nikolay Borisov3558d4f2017-07-26 11:26:28 +03003968 static bool warned = false;
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
Nikolay Borisov3558d4f2017-07-26 11:26:28 +03003974 if (!warned) {
3975 btrfs_warn(fs_info,
3976 "Userspace transaction mechanism is considered "
3977 "deprecated and slated to be removed in 4.17. "
3978 "If you have a valid use case please "
3979 "speak up on the mailing list");
3980 WARN_ON(1);
3981 warned = true;
3982 }
3983
Sage Weil1ab86ae2009-09-29 18:38:44 -04003984 ret = -EINPROGRESS;
Josef Bacik23b5ec72017-07-24 15:14:25 -04003985 private = file->private_data;
3986 if (private && private->trans)
Sage Weil1ab86ae2009-09-29 18:38:44 -04003987 goto out;
Josef Bacik23b5ec72017-07-24 15:14:25 -04003988 if (!private) {
3989 private = kzalloc(sizeof(struct btrfs_file_private),
3990 GFP_KERNEL);
3991 if (!private)
3992 return -ENOMEM;
3993 file->private_data = private;
3994 }
Sage Weil9ca9ee02008-08-04 10:41:27 -04003995
Li Zefanb83cc962010-12-20 16:04:08 +08003996 ret = -EROFS;
3997 if (btrfs_root_readonly(root))
3998 goto out;
3999
Al Viroa561be72011-11-23 11:57:51 -05004000 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05004001 if (ret)
4002 goto out;
4003
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004004 atomic_inc(&fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004005
Sage Weil1ab86ae2009-09-29 18:38:44 -04004006 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004007 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00004008 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04004009 goto out_drop;
4010
Josef Bacik23b5ec72017-07-24 15:14:25 -04004011 private->trans = trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004012 return 0;
4013
4014out_drop:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004015 atomic_dec(&fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05004016 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004017out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004018 return ret;
4019}
4020
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004021static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4022{
Al Viro496ad9a2013-01-23 17:07:38 -05004023 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004024 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004025 struct btrfs_root *root = BTRFS_I(inode)->root;
4026 struct btrfs_root *new_root;
4027 struct btrfs_dir_item *di;
4028 struct btrfs_trans_handle *trans;
4029 struct btrfs_path *path;
4030 struct btrfs_key location;
4031 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004032 u64 objectid = 0;
4033 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004034 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004035
4036 if (!capable(CAP_SYS_ADMIN))
4037 return -EPERM;
4038
Miao Xie3c04ce02012-11-26 08:43:07 +00004039 ret = mnt_want_write_file(file);
4040 if (ret)
4041 return ret;
4042
4043 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4044 ret = -EFAULT;
4045 goto out;
4046 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004047
4048 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304049 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004050
4051 location.objectid = objectid;
4052 location.type = BTRFS_ROOT_ITEM_KEY;
4053 location.offset = (u64)-1;
4054
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004055 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004056 if (IS_ERR(new_root)) {
4057 ret = PTR_ERR(new_root);
4058 goto out;
4059 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004060 if (!is_fstree(new_root->objectid)) {
4061 ret = -ENOENT;
4062 goto out;
4063 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004064
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004065 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004066 if (!path) {
4067 ret = -ENOMEM;
4068 goto out;
4069 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004070 path->leave_spinning = 1;
4071
4072 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004073 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004074 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004075 ret = PTR_ERR(trans);
4076 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004077 }
4078
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004079 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4080 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004081 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004082 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004083 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004084 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004085 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004086 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004087 ret = -ENOENT;
4088 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004089 }
4090
4091 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4092 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4093 btrfs_mark_buffer_dirty(path->nodes[0]);
4094 btrfs_free_path(path);
4095
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004096 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004097 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004098out:
4099 mnt_drop_write_file(file);
4100 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004101}
4102
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004103void btrfs_get_block_group_info(struct list_head *groups_list,
4104 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004105{
4106 struct btrfs_block_group_cache *block_group;
4107
4108 space->total_bytes = 0;
4109 space->used_bytes = 0;
4110 space->flags = 0;
4111 list_for_each_entry(block_group, groups_list, list) {
4112 space->flags = block_group->flags;
4113 space->total_bytes += block_group->key.offset;
4114 space->used_bytes +=
4115 btrfs_block_group_used(&block_group->item);
4116 }
4117}
4118
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004119static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4120 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004121{
4122 struct btrfs_ioctl_space_args space_args;
4123 struct btrfs_ioctl_space_info space;
4124 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004125 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004126 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004127 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004128 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4129 BTRFS_BLOCK_GROUP_SYSTEM,
4130 BTRFS_BLOCK_GROUP_METADATA,
4131 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4132 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004133 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004134 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004135 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004136 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004137
4138 if (copy_from_user(&space_args,
4139 (struct btrfs_ioctl_space_args __user *)arg,
4140 sizeof(space_args)))
4141 return -EFAULT;
4142
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004143 for (i = 0; i < num_types; i++) {
4144 struct btrfs_space_info *tmp;
4145
4146 info = NULL;
4147 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004148 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004149 list) {
4150 if (tmp->flags == types[i]) {
4151 info = tmp;
4152 break;
4153 }
4154 }
4155 rcu_read_unlock();
4156
4157 if (!info)
4158 continue;
4159
4160 down_read(&info->groups_sem);
4161 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4162 if (!list_empty(&info->block_groups[c]))
4163 slot_count++;
4164 }
4165 up_read(&info->groups_sem);
4166 }
Josef Bacik1406e432010-01-13 18:19:06 +00004167
David Sterba36523e952014-02-07 14:34:12 +01004168 /*
4169 * Global block reserve, exported as a space_info
4170 */
4171 slot_count++;
4172
Chris Mason7fde62b2010-03-16 15:40:10 -04004173 /* space_slots == 0 means they are asking for a count */
4174 if (space_args.space_slots == 0) {
4175 space_args.total_spaces = slot_count;
4176 goto out;
4177 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004178
Dan Rosenberg51788b12011-02-14 16:04:23 -05004179 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004180
Chris Mason7fde62b2010-03-16 15:40:10 -04004181 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004182
Chris Mason7fde62b2010-03-16 15:40:10 -04004183 /* we generally have at most 6 or so space infos, one for each raid
4184 * level. So, a whole page should be more than enough for everyone
4185 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004186 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004187 return -ENOMEM;
4188
4189 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004190 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004191 if (!dest)
4192 return -ENOMEM;
4193 dest_orig = dest;
4194
4195 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004196 for (i = 0; i < num_types; i++) {
4197 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004198
Dan Rosenberg51788b12011-02-14 16:04:23 -05004199 if (!slot_count)
4200 break;
4201
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004202 info = NULL;
4203 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004204 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004205 list) {
4206 if (tmp->flags == types[i]) {
4207 info = tmp;
4208 break;
4209 }
4210 }
4211 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004212
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004213 if (!info)
4214 continue;
4215 down_read(&info->groups_sem);
4216 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4217 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004218 btrfs_get_block_group_info(
4219 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004220 memcpy(dest, &space, sizeof(space));
4221 dest++;
4222 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004223 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004224 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004225 if (!slot_count)
4226 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004227 }
4228 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004229 }
Josef Bacik1406e432010-01-13 18:19:06 +00004230
David Sterba36523e952014-02-07 14:34:12 +01004231 /*
4232 * Add global block reserve
4233 */
4234 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004235 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004236
4237 spin_lock(&block_rsv->lock);
4238 space.total_bytes = block_rsv->size;
4239 space.used_bytes = block_rsv->size - block_rsv->reserved;
4240 spin_unlock(&block_rsv->lock);
4241 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4242 memcpy(dest, &space, sizeof(space));
4243 space_args.total_spaces++;
4244 }
4245
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004246 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004247 (arg + sizeof(struct btrfs_ioctl_space_args));
4248
4249 if (copy_to_user(user_dest, dest_orig, alloc_size))
4250 ret = -EFAULT;
4251
4252 kfree(dest_orig);
4253out:
4254 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004255 ret = -EFAULT;
4256
4257 return ret;
4258}
4259
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004260/*
4261 * there are many ways the trans_start and trans_end ioctls can lead
4262 * to deadlocks. They should only be used by applications that
4263 * basically own the machine, and have a very in depth understanding
4264 * of all the possible deadlocks and enospc problems.
4265 */
4266long btrfs_ioctl_trans_end(struct file *file)
4267{
Al Viro496ad9a2013-01-23 17:07:38 -05004268 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004269 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04004270 struct btrfs_file_private *private = file->private_data;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004271
Josef Bacik23b5ec72017-07-24 15:14:25 -04004272 if (!private || !private->trans)
Sage Weil1ab86ae2009-09-29 18:38:44 -04004273 return -EINVAL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004274
Josef Bacik23b5ec72017-07-24 15:14:25 -04004275 btrfs_end_transaction(private->trans);
4276 private->trans = NULL;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004277
Josef Bacika4abeea2011-04-11 17:25:13 -04004278 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004279
Al Viro2a79f172011-12-09 08:06:57 -05004280 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004281 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004282}
4283
Miao Xie9a8c28b2012-11-26 08:40:43 +00004284static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4285 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004286{
Sage Weil46204592010-10-29 15:41:32 -04004287 struct btrfs_trans_handle *trans;
4288 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004289 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004290
Miao Xied4edf392013-02-20 09:17:06 +00004291 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004292 if (IS_ERR(trans)) {
4293 if (PTR_ERR(trans) != -ENOENT)
4294 return PTR_ERR(trans);
4295
4296 /* No running transaction, don't bother */
4297 transid = root->fs_info->last_trans_committed;
4298 goto out;
4299 }
Sage Weil46204592010-10-29 15:41:32 -04004300 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004301 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004302 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004303 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004304 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004305 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004306out:
Sage Weil46204592010-10-29 15:41:32 -04004307 if (argp)
4308 if (copy_to_user(argp, &transid, sizeof(transid)))
4309 return -EFAULT;
4310 return 0;
4311}
4312
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004313static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004314 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004315{
Sage Weil46204592010-10-29 15:41:32 -04004316 u64 transid;
4317
4318 if (argp) {
4319 if (copy_from_user(&transid, argp, sizeof(transid)))
4320 return -EFAULT;
4321 } else {
4322 transid = 0; /* current trans */
4323 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004324 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004325}
4326
Miao Xieb8e95482012-11-26 08:48:01 +00004327static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004329 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004330 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004331 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004332
4333 if (!capable(CAP_SYS_ADMIN))
4334 return -EPERM;
4335
4336 sa = memdup_user(arg, sizeof(*sa));
4337 if (IS_ERR(sa))
4338 return PTR_ERR(sa);
4339
Miao Xieb8e95482012-11-26 08:48:01 +00004340 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4341 ret = mnt_want_write_file(file);
4342 if (ret)
4343 goto out;
4344 }
4345
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004346 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004347 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4348 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004349
4350 if (copy_to_user(arg, sa, sizeof(*sa)))
4351 ret = -EFAULT;
4352
Miao Xieb8e95482012-11-26 08:48:01 +00004353 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4354 mnt_drop_write_file(file);
4355out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004356 kfree(sa);
4357 return ret;
4358}
4359
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004360static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004361{
4362 if (!capable(CAP_SYS_ADMIN))
4363 return -EPERM;
4364
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004365 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004366}
4367
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004368static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004369 void __user *arg)
4370{
4371 struct btrfs_ioctl_scrub_args *sa;
4372 int ret;
4373
4374 if (!capable(CAP_SYS_ADMIN))
4375 return -EPERM;
4376
4377 sa = memdup_user(arg, sizeof(*sa));
4378 if (IS_ERR(sa))
4379 return PTR_ERR(sa);
4380
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004381 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004382
4383 if (copy_to_user(arg, sa, sizeof(*sa)))
4384 ret = -EFAULT;
4385
4386 kfree(sa);
4387 return ret;
4388}
4389
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004390static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004391 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004392{
4393 struct btrfs_ioctl_get_dev_stats *sa;
4394 int ret;
4395
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004396 sa = memdup_user(arg, sizeof(*sa));
4397 if (IS_ERR(sa))
4398 return PTR_ERR(sa);
4399
David Sterbab27f7c02012-06-22 06:30:39 -06004400 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4401 kfree(sa);
4402 return -EPERM;
4403 }
4404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004405 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004406
4407 if (copy_to_user(arg, sa, sizeof(*sa)))
4408 ret = -EFAULT;
4409
4410 kfree(sa);
4411 return ret;
4412}
4413
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004414static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4415 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004416{
4417 struct btrfs_ioctl_dev_replace_args *p;
4418 int ret;
4419
4420 if (!capable(CAP_SYS_ADMIN))
4421 return -EPERM;
4422
4423 p = memdup_user(arg, sizeof(*p));
4424 if (IS_ERR(p))
4425 return PTR_ERR(p);
4426
4427 switch (p->cmd) {
4428 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004429 if (fs_info->sb->s_flags & MS_RDONLY) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004430 ret = -EROFS;
4431 goto out;
4432 }
David Sterba171938e2017-03-28 14:44:21 +02004433 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004434 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004435 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004436 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004437 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004438 }
4439 break;
4440 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004441 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004442 ret = 0;
4443 break;
4444 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004445 ret = btrfs_dev_replace_cancel(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004446 break;
4447 default:
4448 ret = -EINVAL;
4449 break;
4450 }
4451
4452 if (copy_to_user(arg, p, sizeof(*p)))
4453 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004454out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004455 kfree(p);
4456 return ret;
4457}
4458
Jan Schmidtd7728c92011-07-07 16:48:38 +02004459static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4460{
4461 int ret = 0;
4462 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004463 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004464 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004465 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004466 struct inode_fs_paths *ipath = NULL;
4467 struct btrfs_path *path;
4468
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004469 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004470 return -EPERM;
4471
4472 path = btrfs_alloc_path();
4473 if (!path) {
4474 ret = -ENOMEM;
4475 goto out;
4476 }
4477
4478 ipa = memdup_user(arg, sizeof(*ipa));
4479 if (IS_ERR(ipa)) {
4480 ret = PTR_ERR(ipa);
4481 ipa = NULL;
4482 goto out;
4483 }
4484
4485 size = min_t(u32, ipa->size, 4096);
4486 ipath = init_ipath(size, root, path);
4487 if (IS_ERR(ipath)) {
4488 ret = PTR_ERR(ipath);
4489 ipath = NULL;
4490 goto out;
4491 }
4492
4493 ret = paths_from_inode(ipa->inum, ipath);
4494 if (ret < 0)
4495 goto out;
4496
4497 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004498 rel_ptr = ipath->fspath->val[i] -
4499 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004500 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004501 }
4502
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004503 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4504 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004505 if (ret) {
4506 ret = -EFAULT;
4507 goto out;
4508 }
4509
4510out:
4511 btrfs_free_path(path);
4512 free_ipath(ipath);
4513 kfree(ipa);
4514
4515 return ret;
4516}
4517
4518static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4519{
4520 struct btrfs_data_container *inodes = ctx;
4521 const size_t c = 3 * sizeof(u64);
4522
4523 if (inodes->bytes_left >= c) {
4524 inodes->bytes_left -= c;
4525 inodes->val[inodes->elem_cnt] = inum;
4526 inodes->val[inodes->elem_cnt + 1] = offset;
4527 inodes->val[inodes->elem_cnt + 2] = root;
4528 inodes->elem_cnt += 3;
4529 } else {
4530 inodes->bytes_missing += c - inodes->bytes_left;
4531 inodes->bytes_left = 0;
4532 inodes->elem_missed += 3;
4533 }
4534
4535 return 0;
4536}
4537
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004538static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Jan Schmidtd7728c92011-07-07 16:48:38 +02004539 void __user *arg)
4540{
4541 int ret = 0;
4542 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004543 struct btrfs_ioctl_logical_ino_args *loi;
4544 struct btrfs_data_container *inodes = NULL;
4545 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004546
4547 if (!capable(CAP_SYS_ADMIN))
4548 return -EPERM;
4549
4550 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304551 if (IS_ERR(loi))
4552 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004553
4554 path = btrfs_alloc_path();
4555 if (!path) {
4556 ret = -ENOMEM;
4557 goto out;
4558 }
4559
Byongho Leeee221842015-12-15 01:42:10 +09004560 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004561 inodes = init_data_container(size);
4562 if (IS_ERR(inodes)) {
4563 ret = PTR_ERR(inodes);
4564 inodes = NULL;
4565 goto out;
4566 }
4567
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004568 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Liu Bodf031f02012-09-07 20:01:29 -06004569 build_ino_list, inodes);
4570 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004571 ret = -ENOENT;
4572 if (ret < 0)
4573 goto out;
4574
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004575 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4576 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004577 if (ret)
4578 ret = -EFAULT;
4579
4580out:
4581 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004582 kvfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004583 kfree(loi);
4584
4585 return ret;
4586}
4587
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004588void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004589 struct btrfs_ioctl_balance_args *bargs)
4590{
4591 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4592
4593 bargs->flags = bctl->flags;
4594
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004595 if (atomic_read(&fs_info->balance_running))
4596 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4597 if (atomic_read(&fs_info->balance_pause_req))
4598 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004599 if (atomic_read(&fs_info->balance_cancel_req))
4600 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004601
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004602 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4603 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4604 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004605
4606 if (lock) {
4607 spin_lock(&fs_info->balance_lock);
4608 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4609 spin_unlock(&fs_info->balance_lock);
4610 } else {
4611 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4612 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004613}
4614
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004615static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004616{
Al Viro496ad9a2013-01-23 17:07:38 -05004617 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004618 struct btrfs_fs_info *fs_info = root->fs_info;
4619 struct btrfs_ioctl_balance_args *bargs;
4620 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004621 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004622 int ret;
4623
4624 if (!capable(CAP_SYS_ADMIN))
4625 return -EPERM;
4626
Liu Boe54bfa32012-06-29 03:58:48 -06004627 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004628 if (ret)
4629 return ret;
4630
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004631again:
David Sterba171938e2017-03-28 14:44:21 +02004632 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004633 mutex_lock(&fs_info->volume_mutex);
4634 mutex_lock(&fs_info->balance_mutex);
4635 need_unlock = true;
4636 goto locked;
4637 }
4638
4639 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004640 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004641 * (1) some other op is running
4642 * (2) balance is running
4643 * (3) balance is paused -- special case (think resume)
4644 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004645 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004646 if (fs_info->balance_ctl) {
4647 /* this is either (2) or (3) */
4648 if (!atomic_read(&fs_info->balance_running)) {
4649 mutex_unlock(&fs_info->balance_mutex);
4650 if (!mutex_trylock(&fs_info->volume_mutex))
4651 goto again;
4652 mutex_lock(&fs_info->balance_mutex);
4653
4654 if (fs_info->balance_ctl &&
4655 !atomic_read(&fs_info->balance_running)) {
4656 /* this is (3) */
4657 need_unlock = false;
4658 goto locked;
4659 }
4660
4661 mutex_unlock(&fs_info->balance_mutex);
4662 mutex_unlock(&fs_info->volume_mutex);
4663 goto again;
4664 } else {
4665 /* this is (2) */
4666 mutex_unlock(&fs_info->balance_mutex);
4667 ret = -EINPROGRESS;
4668 goto out;
4669 }
4670 } else {
4671 /* this is (1) */
4672 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004673 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004674 goto out;
4675 }
4676
4677locked:
David Sterba171938e2017-03-28 14:44:21 +02004678 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004679
4680 if (arg) {
4681 bargs = memdup_user(arg, sizeof(*bargs));
4682 if (IS_ERR(bargs)) {
4683 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004684 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004685 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004686
4687 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4688 if (!fs_info->balance_ctl) {
4689 ret = -ENOTCONN;
4690 goto out_bargs;
4691 }
4692
4693 bctl = fs_info->balance_ctl;
4694 spin_lock(&fs_info->balance_lock);
4695 bctl->flags |= BTRFS_BALANCE_RESUME;
4696 spin_unlock(&fs_info->balance_lock);
4697
4698 goto do_balance;
4699 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004700 } else {
4701 bargs = NULL;
4702 }
4703
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004704 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004705 ret = -EINPROGRESS;
4706 goto out_bargs;
4707 }
4708
David Sterba8d2db782015-11-04 15:38:29 +01004709 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004710 if (!bctl) {
4711 ret = -ENOMEM;
4712 goto out_bargs;
4713 }
4714
4715 bctl->fs_info = fs_info;
4716 if (arg) {
4717 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4718 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4719 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4720
4721 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004722 } else {
4723 /* balance everything - no filters */
4724 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004725 }
4726
David Sterba8eb93452015-10-12 16:55:54 +02004727 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4728 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004729 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004730 }
4731
Ilya Dryomovde322262012-01-16 22:04:49 +02004732do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004733 /*
David Sterba171938e2017-03-28 14:44:21 +02004734 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004735 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4736 * or, if restriper was paused all the way until unmount, in
David Sterba171938e2017-03-28 14:44:21 +02004737 * free_fs_info. The flag is cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004738 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004739 need_unlock = false;
4740
4741 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004742 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004743
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004744 if (arg) {
4745 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4746 ret = -EFAULT;
4747 }
4748
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004749out_bctl:
4750 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004751out_bargs:
4752 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004753out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004754 mutex_unlock(&fs_info->balance_mutex);
4755 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004756 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004757 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004758out:
Liu Boe54bfa32012-06-29 03:58:48 -06004759 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004760 return ret;
4761}
4762
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004763static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004764{
4765 if (!capable(CAP_SYS_ADMIN))
4766 return -EPERM;
4767
4768 switch (cmd) {
4769 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004770 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004771 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004772 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004773 }
4774
4775 return -EINVAL;
4776}
4777
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004778static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004779 void __user *arg)
4780{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004781 struct btrfs_ioctl_balance_args *bargs;
4782 int ret = 0;
4783
4784 if (!capable(CAP_SYS_ADMIN))
4785 return -EPERM;
4786
4787 mutex_lock(&fs_info->balance_mutex);
4788 if (!fs_info->balance_ctl) {
4789 ret = -ENOTCONN;
4790 goto out;
4791 }
4792
David Sterba8d2db782015-11-04 15:38:29 +01004793 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004794 if (!bargs) {
4795 ret = -ENOMEM;
4796 goto out;
4797 }
4798
4799 update_ioctl_balance_args(fs_info, 1, bargs);
4800
4801 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4802 ret = -EFAULT;
4803
4804 kfree(bargs);
4805out:
4806 mutex_unlock(&fs_info->balance_mutex);
4807 return ret;
4808}
4809
Miao Xie905b0dd2012-11-26 08:50:11 +00004810static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004811{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004812 struct inode *inode = file_inode(file);
4813 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004814 struct btrfs_ioctl_quota_ctl_args *sa;
4815 struct btrfs_trans_handle *trans = NULL;
4816 int ret;
4817 int err;
4818
4819 if (!capable(CAP_SYS_ADMIN))
4820 return -EPERM;
4821
Miao Xie905b0dd2012-11-26 08:50:11 +00004822 ret = mnt_want_write_file(file);
4823 if (ret)
4824 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004825
4826 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004827 if (IS_ERR(sa)) {
4828 ret = PTR_ERR(sa);
4829 goto drop_write;
4830 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004831
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004832 down_write(&fs_info->subvol_sem);
4833 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004834 if (IS_ERR(trans)) {
4835 ret = PTR_ERR(trans);
4836 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004837 }
4838
4839 switch (sa->cmd) {
4840 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004841 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004842 break;
4843 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004844 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004845 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004846 default:
4847 ret = -EINVAL;
4848 break;
4849 }
4850
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004851 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004852 if (err && !ret)
4853 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004854out:
4855 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004856 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004857drop_write:
4858 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004859 return ret;
4860}
4861
Miao Xie905b0dd2012-11-26 08:50:11 +00004862static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004863{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004864 struct inode *inode = file_inode(file);
4865 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4866 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004867 struct btrfs_ioctl_qgroup_assign_args *sa;
4868 struct btrfs_trans_handle *trans;
4869 int ret;
4870 int err;
4871
4872 if (!capable(CAP_SYS_ADMIN))
4873 return -EPERM;
4874
Miao Xie905b0dd2012-11-26 08:50:11 +00004875 ret = mnt_want_write_file(file);
4876 if (ret)
4877 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004878
4879 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004880 if (IS_ERR(sa)) {
4881 ret = PTR_ERR(sa);
4882 goto drop_write;
4883 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004884
4885 trans = btrfs_join_transaction(root);
4886 if (IS_ERR(trans)) {
4887 ret = PTR_ERR(trans);
4888 goto out;
4889 }
4890
Arne Jansen5d13a372011-09-14 15:53:51 +02004891 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004892 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004893 sa->src, sa->dst);
4894 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004895 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004896 sa->src, sa->dst);
4897 }
4898
Qu Wenruoe082f562015-02-27 16:24:28 +08004899 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004900 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004901 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004902 btrfs_handle_fs_error(fs_info, err,
4903 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004904 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004905 if (err && !ret)
4906 ret = err;
4907
4908out:
4909 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004910drop_write:
4911 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004912 return ret;
4913}
4914
Miao Xie905b0dd2012-11-26 08:50:11 +00004915static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004916{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004917 struct inode *inode = file_inode(file);
4918 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4919 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004920 struct btrfs_ioctl_qgroup_create_args *sa;
4921 struct btrfs_trans_handle *trans;
4922 int ret;
4923 int err;
4924
4925 if (!capable(CAP_SYS_ADMIN))
4926 return -EPERM;
4927
Miao Xie905b0dd2012-11-26 08:50:11 +00004928 ret = mnt_want_write_file(file);
4929 if (ret)
4930 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004931
4932 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004933 if (IS_ERR(sa)) {
4934 ret = PTR_ERR(sa);
4935 goto drop_write;
4936 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004937
Miao Xied86e56c2012-11-15 11:35:41 +00004938 if (!sa->qgroupid) {
4939 ret = -EINVAL;
4940 goto out;
4941 }
4942
Arne Jansen5d13a372011-09-14 15:53:51 +02004943 trans = btrfs_join_transaction(root);
4944 if (IS_ERR(trans)) {
4945 ret = PTR_ERR(trans);
4946 goto out;
4947 }
4948
Arne Jansen5d13a372011-09-14 15:53:51 +02004949 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004950 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004951 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004952 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004953 }
4954
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004955 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004956 if (err && !ret)
4957 ret = err;
4958
4959out:
4960 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004961drop_write:
4962 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004963 return ret;
4964}
4965
Miao Xie905b0dd2012-11-26 08:50:11 +00004966static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004967{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 struct inode *inode = file_inode(file);
4969 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4970 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004971 struct btrfs_ioctl_qgroup_limit_args *sa;
4972 struct btrfs_trans_handle *trans;
4973 int ret;
4974 int err;
4975 u64 qgroupid;
4976
4977 if (!capable(CAP_SYS_ADMIN))
4978 return -EPERM;
4979
Miao Xie905b0dd2012-11-26 08:50:11 +00004980 ret = mnt_want_write_file(file);
4981 if (ret)
4982 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004983
4984 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004985 if (IS_ERR(sa)) {
4986 ret = PTR_ERR(sa);
4987 goto drop_write;
4988 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004989
4990 trans = btrfs_join_transaction(root);
4991 if (IS_ERR(trans)) {
4992 ret = PTR_ERR(trans);
4993 goto out;
4994 }
4995
4996 qgroupid = sa->qgroupid;
4997 if (!qgroupid) {
4998 /* take the current subvol as qgroup */
4999 qgroupid = root->root_key.objectid;
5000 }
5001
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005002 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005003
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005004 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005005 if (err && !ret)
5006 ret = err;
5007
5008out:
5009 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005010drop_write:
5011 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005012 return ret;
5013}
5014
Jan Schmidt2f232032013-04-25 16:04:51 +00005015static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5016{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005017 struct inode *inode = file_inode(file);
5018 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005019 struct btrfs_ioctl_quota_rescan_args *qsa;
5020 int ret;
5021
5022 if (!capable(CAP_SYS_ADMIN))
5023 return -EPERM;
5024
5025 ret = mnt_want_write_file(file);
5026 if (ret)
5027 return ret;
5028
5029 qsa = memdup_user(arg, sizeof(*qsa));
5030 if (IS_ERR(qsa)) {
5031 ret = PTR_ERR(qsa);
5032 goto drop_write;
5033 }
5034
5035 if (qsa->flags) {
5036 ret = -EINVAL;
5037 goto out;
5038 }
5039
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005040 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005041
5042out:
5043 kfree(qsa);
5044drop_write:
5045 mnt_drop_write_file(file);
5046 return ret;
5047}
5048
5049static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5050{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005051 struct inode *inode = file_inode(file);
5052 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005053 struct btrfs_ioctl_quota_rescan_args *qsa;
5054 int ret = 0;
5055
5056 if (!capable(CAP_SYS_ADMIN))
5057 return -EPERM;
5058
David Sterba8d2db782015-11-04 15:38:29 +01005059 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005060 if (!qsa)
5061 return -ENOMEM;
5062
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005063 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005064 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005065 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005066 }
5067
5068 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5069 ret = -EFAULT;
5070
5071 kfree(qsa);
5072 return ret;
5073}
5074
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005075static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5076{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005077 struct inode *inode = file_inode(file);
5078 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005079
5080 if (!capable(CAP_SYS_ADMIN))
5081 return -EPERM;
5082
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005083 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005084}
5085
Hugo Millsabccd002014-01-30 20:17:00 +00005086static long _btrfs_ioctl_set_received_subvol(struct file *file,
5087 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005088{
Al Viro496ad9a2013-01-23 17:07:38 -05005089 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005090 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005091 struct btrfs_root *root = BTRFS_I(inode)->root;
5092 struct btrfs_root_item *root_item = &root->root_item;
5093 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005094 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005095 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005096 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005097
David Sterbabd60ea02014-01-16 15:50:22 +01005098 if (!inode_owner_or_capable(inode))
5099 return -EPERM;
5100
Alexander Block8ea05e32012-07-25 17:35:53 +02005101 ret = mnt_want_write_file(file);
5102 if (ret < 0)
5103 return ret;
5104
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005105 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005106
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005107 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005108 ret = -EINVAL;
5109 goto out;
5110 }
5111
5112 if (btrfs_root_readonly(root)) {
5113 ret = -EROFS;
5114 goto out;
5115 }
5116
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005117 /*
5118 * 1 - root item
5119 * 2 - uuid items (received uuid + subvol uuid)
5120 */
5121 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005122 if (IS_ERR(trans)) {
5123 ret = PTR_ERR(trans);
5124 trans = NULL;
5125 goto out;
5126 }
5127
5128 sa->rtransid = trans->transid;
5129 sa->rtime.sec = ct.tv_sec;
5130 sa->rtime.nsec = ct.tv_nsec;
5131
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005132 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5133 BTRFS_UUID_SIZE);
5134 if (received_uuid_changed &&
5135 !btrfs_is_empty_uuid(root_item->received_uuid))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005136 btrfs_uuid_tree_rem(trans, fs_info, root_item->received_uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005137 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5138 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005139 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5140 btrfs_set_root_stransid(root_item, sa->stransid);
5141 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005142 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5143 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5144 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5145 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005146
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005147 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005148 &root->root_key, &root->root_item);
5149 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005150 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005151 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005152 }
5153 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005154 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005155 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5156 root->root_key.objectid);
5157 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005158 btrfs_abort_transaction(trans, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005159 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005160 }
5161 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005162 ret = btrfs_commit_transaction(trans);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005163 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005164 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005165 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005166 }
5167
Hugo Millsabccd002014-01-30 20:17:00 +00005168out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005169 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005170 mnt_drop_write_file(file);
5171 return ret;
5172}
5173
5174#ifdef CONFIG_64BIT
5175static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5176 void __user *arg)
5177{
5178 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5179 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5180 int ret = 0;
5181
5182 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305183 if (IS_ERR(args32))
5184 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005185
David Sterba8d2db782015-11-04 15:38:29 +01005186 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005187 if (!args64) {
5188 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005189 goto out;
5190 }
5191
5192 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5193 args64->stransid = args32->stransid;
5194 args64->rtransid = args32->rtransid;
5195 args64->stime.sec = args32->stime.sec;
5196 args64->stime.nsec = args32->stime.nsec;
5197 args64->rtime.sec = args32->rtime.sec;
5198 args64->rtime.nsec = args32->rtime.nsec;
5199 args64->flags = args32->flags;
5200
5201 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5202 if (ret)
5203 goto out;
5204
5205 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5206 args32->stransid = args64->stransid;
5207 args32->rtransid = args64->rtransid;
5208 args32->stime.sec = args64->stime.sec;
5209 args32->stime.nsec = args64->stime.nsec;
5210 args32->rtime.sec = args64->rtime.sec;
5211 args32->rtime.nsec = args64->rtime.nsec;
5212 args32->flags = args64->flags;
5213
5214 ret = copy_to_user(arg, args32, sizeof(*args32));
5215 if (ret)
5216 ret = -EFAULT;
5217
5218out:
5219 kfree(args32);
5220 kfree(args64);
5221 return ret;
5222}
5223#endif
5224
5225static long btrfs_ioctl_set_received_subvol(struct file *file,
5226 void __user *arg)
5227{
5228 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5229 int ret = 0;
5230
5231 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305232 if (IS_ERR(sa))
5233 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005234
5235 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5236
5237 if (ret)
5238 goto out;
5239
Alexander Block8ea05e32012-07-25 17:35:53 +02005240 ret = copy_to_user(arg, sa, sizeof(*sa));
5241 if (ret)
5242 ret = -EFAULT;
5243
5244out:
5245 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005246 return ret;
5247}
5248
jeff.liu867ab662013-01-05 02:48:01 +00005249static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005251 struct inode *inode = file_inode(file);
5252 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005253 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005254 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005255 char label[BTRFS_LABEL_SIZE];
5256
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005257 spin_lock(&fs_info->super_lock);
5258 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5259 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005260
5261 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005262
5263 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005264 btrfs_warn(fs_info,
5265 "label is too long, return the first %zu bytes",
5266 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005267 }
5268
jeff.liu867ab662013-01-05 02:48:01 +00005269 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005270
5271 return ret ? -EFAULT : 0;
5272}
5273
jeff.liua8bfd4a2013-01-05 02:48:08 +00005274static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005276 struct inode *inode = file_inode(file);
5277 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5278 struct btrfs_root *root = BTRFS_I(inode)->root;
5279 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005280 struct btrfs_trans_handle *trans;
5281 char label[BTRFS_LABEL_SIZE];
5282 int ret;
5283
5284 if (!capable(CAP_SYS_ADMIN))
5285 return -EPERM;
5286
5287 if (copy_from_user(label, arg, sizeof(label)))
5288 return -EFAULT;
5289
5290 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005291 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005292 "unable to set label with more than %d bytes",
5293 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005294 return -EINVAL;
5295 }
5296
5297 ret = mnt_want_write_file(file);
5298 if (ret)
5299 return ret;
5300
jeff.liua8bfd4a2013-01-05 02:48:08 +00005301 trans = btrfs_start_transaction(root, 0);
5302 if (IS_ERR(trans)) {
5303 ret = PTR_ERR(trans);
5304 goto out_unlock;
5305 }
5306
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005307 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005308 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005309 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005310 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005311
5312out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005313 mnt_drop_write_file(file);
5314 return ret;
5315}
5316
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005317#define INIT_FEATURE_FLAGS(suffix) \
5318 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5319 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5320 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5321
David Sterbad5131b62016-02-17 15:26:27 +01005322int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005323{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005324 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005325 INIT_FEATURE_FLAGS(SUPP),
5326 INIT_FEATURE_FLAGS(SAFE_SET),
5327 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5328 };
5329
5330 if (copy_to_user(arg, &features, sizeof(features)))
5331 return -EFAULT;
5332
5333 return 0;
5334}
5335
5336static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005338 struct inode *inode = file_inode(file);
5339 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5340 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005341 struct btrfs_ioctl_feature_flags features;
5342
5343 features.compat_flags = btrfs_super_compat_flags(super_block);
5344 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5345 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5346
5347 if (copy_to_user(arg, &features, sizeof(features)))
5348 return -EFAULT;
5349
5350 return 0;
5351}
5352
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005353static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005354 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005355 u64 change_mask, u64 flags, u64 supported_flags,
5356 u64 safe_set, u64 safe_clear)
5357{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005358 const char *type = btrfs_feature_set_names[set];
5359 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005360 u64 disallowed, unsupported;
5361 u64 set_mask = flags & change_mask;
5362 u64 clear_mask = ~flags & change_mask;
5363
5364 unsupported = set_mask & ~supported_flags;
5365 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005366 names = btrfs_printable_features(set, unsupported);
5367 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005368 btrfs_warn(fs_info,
5369 "this kernel does not support the %s feature bit%s",
5370 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005371 kfree(names);
5372 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005373 btrfs_warn(fs_info,
5374 "this kernel does not support %s bits 0x%llx",
5375 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005376 return -EOPNOTSUPP;
5377 }
5378
5379 disallowed = set_mask & ~safe_set;
5380 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005381 names = btrfs_printable_features(set, disallowed);
5382 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005383 btrfs_warn(fs_info,
5384 "can't set the %s feature bit%s while mounted",
5385 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005386 kfree(names);
5387 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005388 btrfs_warn(fs_info,
5389 "can't set %s bits 0x%llx while mounted",
5390 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005391 return -EPERM;
5392 }
5393
5394 disallowed = clear_mask & ~safe_clear;
5395 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005396 names = btrfs_printable_features(set, disallowed);
5397 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005398 btrfs_warn(fs_info,
5399 "can't clear the %s feature bit%s while mounted",
5400 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005401 kfree(names);
5402 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005403 btrfs_warn(fs_info,
5404 "can't clear %s bits 0x%llx while mounted",
5405 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005406 return -EPERM;
5407 }
5408
5409 return 0;
5410}
5411
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005412#define check_feature(fs_info, change_mask, flags, mask_base) \
5413check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005414 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5415 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5416 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5417
5418static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5419{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005420 struct inode *inode = file_inode(file);
5421 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5422 struct btrfs_root *root = BTRFS_I(inode)->root;
5423 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005424 struct btrfs_ioctl_feature_flags flags[2];
5425 struct btrfs_trans_handle *trans;
5426 u64 newflags;
5427 int ret;
5428
5429 if (!capable(CAP_SYS_ADMIN))
5430 return -EPERM;
5431
5432 if (copy_from_user(flags, arg, sizeof(flags)))
5433 return -EFAULT;
5434
5435 /* Nothing to do */
5436 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5437 !flags[0].incompat_flags)
5438 return 0;
5439
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005440 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005441 flags[1].compat_flags, COMPAT);
5442 if (ret)
5443 return ret;
5444
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005445 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005446 flags[1].compat_ro_flags, COMPAT_RO);
5447 if (ret)
5448 return ret;
5449
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005450 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005451 flags[1].incompat_flags, INCOMPAT);
5452 if (ret)
5453 return ret;
5454
David Sterba7ab19622016-05-04 11:32:00 +02005455 ret = mnt_want_write_file(file);
5456 if (ret)
5457 return ret;
5458
David Sterba8051aa12014-02-07 14:34:04 +01005459 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005460 if (IS_ERR(trans)) {
5461 ret = PTR_ERR(trans);
5462 goto out_drop_write;
5463 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005464
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005465 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005466 newflags = btrfs_super_compat_flags(super_block);
5467 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5468 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5469 btrfs_set_super_compat_flags(super_block, newflags);
5470
5471 newflags = btrfs_super_compat_ro_flags(super_block);
5472 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5473 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5474 btrfs_set_super_compat_ro_flags(super_block, newflags);
5475
5476 newflags = btrfs_super_incompat_flags(super_block);
5477 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5478 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5479 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005480 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005481
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005482 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005483out_drop_write:
5484 mnt_drop_write_file(file);
5485
5486 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005487}
5488
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005489long btrfs_ioctl(struct file *file, unsigned int
5490 cmd, unsigned long arg)
5491{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005492 struct inode *inode = file_inode(file);
5493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5494 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005495 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005496
5497 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005498 case FS_IOC_GETFLAGS:
5499 return btrfs_ioctl_getflags(file, argp);
5500 case FS_IOC_SETFLAGS:
5501 return btrfs_ioctl_setflags(file, argp);
5502 case FS_IOC_GETVERSION:
5503 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005504 case FITRIM:
5505 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005506 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005507 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005508 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005509 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005510 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005511 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005512 case BTRFS_IOC_SUBVOL_CREATE_V2:
5513 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005514 case BTRFS_IOC_SNAP_DESTROY:
5515 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005516 case BTRFS_IOC_SUBVOL_GETFLAGS:
5517 return btrfs_ioctl_subvol_getflags(file, argp);
5518 case BTRFS_IOC_SUBVOL_SETFLAGS:
5519 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005520 case BTRFS_IOC_DEFAULT_SUBVOL:
5521 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005522 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005523 return btrfs_ioctl_defrag(file, NULL);
5524 case BTRFS_IOC_DEFRAG_RANGE:
5525 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005526 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005527 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005528 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005529 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005530 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005531 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005532 case BTRFS_IOC_RM_DEV_V2:
5533 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005534 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005535 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005536 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005537 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005538 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005539 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005540 case BTRFS_IOC_TRANS_START:
5541 return btrfs_ioctl_trans_start(file);
5542 case BTRFS_IOC_TRANS_END:
5543 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005544 case BTRFS_IOC_TREE_SEARCH:
5545 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005546 case BTRFS_IOC_TREE_SEARCH_V2:
5547 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005548 case BTRFS_IOC_INO_LOOKUP:
5549 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005550 case BTRFS_IOC_INO_PATHS:
5551 return btrfs_ioctl_ino_to_path(root, argp);
5552 case BTRFS_IOC_LOGICAL_INO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005553 return btrfs_ioctl_logical_to_ino(fs_info, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005554 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005555 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005556 case BTRFS_IOC_SYNC: {
5557 int ret;
5558
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005559 ret = btrfs_start_delalloc_roots(fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005560 if (ret)
5561 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005562 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005563 /*
5564 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005565 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005566 * processing uncleaned subvols.
5567 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005568 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005569 return ret;
5570 }
Sage Weil46204592010-10-29 15:41:32 -04005571 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005572 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005573 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005574 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005575 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005576 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005577 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005578 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005579 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005580 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005581 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005582 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005583 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005584 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005585 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005586 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005587 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5588 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005589#ifdef CONFIG_64BIT
5590 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5591 return btrfs_ioctl_set_received_subvol_32(file, argp);
5592#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005593 case BTRFS_IOC_SEND:
5594 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005595 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005596 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005597 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005598 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005599 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005600 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005601 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005602 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005603 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005604 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005605 case BTRFS_IOC_QUOTA_RESCAN:
5606 return btrfs_ioctl_quota_rescan(file, argp);
5607 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5608 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005609 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5610 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005611 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005612 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005613 case BTRFS_IOC_GET_FSLABEL:
5614 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005615 case BTRFS_IOC_SET_FSLABEL:
5616 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005617 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005618 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005619 case BTRFS_IOC_GET_FEATURES:
5620 return btrfs_ioctl_get_features(file, argp);
5621 case BTRFS_IOC_SET_FEATURES:
5622 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005623 }
5624
5625 return -ENOTTY;
5626}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005627
5628#ifdef CONFIG_COMPAT
5629long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5630{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005631 /*
5632 * These all access 32-bit values anyway so no further
5633 * handling is necessary.
5634 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005635 switch (cmd) {
5636 case FS_IOC32_GETFLAGS:
5637 cmd = FS_IOC_GETFLAGS;
5638 break;
5639 case FS_IOC32_SETFLAGS:
5640 cmd = FS_IOC_SETFLAGS;
5641 break;
5642 case FS_IOC32_GETVERSION:
5643 cmd = FS_IOC_GETVERSION;
5644 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005645 }
5646
5647 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5648}
5649#endif