blob: 8189cb328d4ad5f5d709861f2e1469c14460e0e7 [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040036#include <linux/compat.h>
37#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040038#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040039#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020040#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000042#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020043#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000044#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070045#include <linux/uaccess.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040050#include "print-tree.h"
51#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040052#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080053#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020054#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040055#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020056#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010057#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000058#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040059#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070060#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000061#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080062#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040063
Hugo Millsabccd002014-01-30 20:17:00 +000064#ifdef CONFIG_64BIT
65/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
66 * structures are incorrect, as the timespec structure from userspace
67 * is 4 bytes too small. We define these alternatives here to teach
68 * the kernel about the 32-bit struct packing.
69 */
70struct btrfs_ioctl_timespec_32 {
71 __u64 sec;
72 __u32 nsec;
73} __attribute__ ((__packed__));
74
75struct btrfs_ioctl_received_subvol_args_32 {
76 char uuid[BTRFS_UUID_SIZE]; /* in */
77 __u64 stransid; /* in */
78 __u64 rtransid; /* out */
79 struct btrfs_ioctl_timespec_32 stime; /* in */
80 struct btrfs_ioctl_timespec_32 rtime; /* out */
81 __u64 flags; /* in */
82 __u64 reserved[16]; /* in */
83} __attribute__ ((__packed__));
84
85#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
86 struct btrfs_ioctl_received_subvol_args_32)
87#endif
88
89
Mark Fasheh416161d2013-08-06 11:42:51 -070090static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070091 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
92 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070093
Christoph Hellwig6cbff002009-04-17 10:37:41 +020094/* Mask out flags that are inappropriate for the given type of inode. */
95static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
96{
97 if (S_ISDIR(mode))
98 return flags;
99 else if (S_ISREG(mode))
100 return flags & ~FS_DIRSYNC_FL;
101 else
102 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
103}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400104
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105/*
106 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
107 */
108static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
109{
110 unsigned int iflags = 0;
111
112 if (flags & BTRFS_INODE_SYNC)
113 iflags |= FS_SYNC_FL;
114 if (flags & BTRFS_INODE_IMMUTABLE)
115 iflags |= FS_IMMUTABLE_FL;
116 if (flags & BTRFS_INODE_APPEND)
117 iflags |= FS_APPEND_FL;
118 if (flags & BTRFS_INODE_NODUMP)
119 iflags |= FS_NODUMP_FL;
120 if (flags & BTRFS_INODE_NOATIME)
121 iflags |= FS_NOATIME_FL;
122 if (flags & BTRFS_INODE_DIRSYNC)
123 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000124 if (flags & BTRFS_INODE_NODATACOW)
125 iflags |= FS_NOCOW_FL;
126
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900127 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000128 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900129 else if (flags & BTRFS_INODE_COMPRESS)
130 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200131
132 return iflags;
133}
134
135/*
136 * Update inode->i_flags based on the btrfs internal flags.
137 */
138void btrfs_update_iflags(struct inode *inode)
139{
140 struct btrfs_inode *ip = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100141 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142
143 if (ip->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145 if (ip->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147 if (ip->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200149 if (ip->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200151 if (ip->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100152 new_fl |= S_DIRSYNC;
153
154 set_mask_bits(&inode->i_flags,
155 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
156 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157}
158
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200159static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
160{
Al Viro496ad9a2013-01-23 17:07:38 -0500161 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200162 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
163
164 if (copy_to_user(arg, &flags, sizeof(flags)))
165 return -EFAULT;
166 return 0;
167}
168
Liu Bo75e7cb72011-03-22 10:12:20 +0000169static int check_flags(unsigned int flags)
170{
171 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
172 FS_NOATIME_FL | FS_NODUMP_FL | \
173 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000174 FS_NOCOMP_FL | FS_COMPR_FL |
175 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000176 return -EOPNOTSUPP;
177
178 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
179 return -EINVAL;
180
Liu Bo75e7cb72011-03-22 10:12:20 +0000181 return 0;
182}
183
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200184static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
185{
Al Viro496ad9a2013-01-23 17:07:38 -0500186 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400187 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200188 struct btrfs_inode *ip = BTRFS_I(inode);
189 struct btrfs_root *root = ip->root;
190 struct btrfs_trans_handle *trans;
191 unsigned int flags, oldflags;
192 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800193 u64 ip_oldflags;
194 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600195 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200196
David Sterbabd60ea02014-01-16 15:50:22 +0100197 if (!inode_owner_or_capable(inode))
198 return -EPERM;
199
Li Zefanb83cc962010-12-20 16:04:08 +0800200 if (btrfs_root_readonly(root))
201 return -EROFS;
202
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203 if (copy_from_user(&flags, arg, sizeof(flags)))
204 return -EFAULT;
205
Liu Bo75e7cb72011-03-22 10:12:20 +0000206 ret = check_flags(flags);
207 if (ret)
208 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200209
Jan Karae7848682012-06-12 16:20:32 +0200210 ret = mnt_want_write_file(file);
211 if (ret)
212 return ret;
213
Al Viro59551022016-01-22 15:40:57 -0500214 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200215
Li Zefanf062abf02011-12-29 13:36:45 +0800216 ip_oldflags = ip->flags;
217 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600218 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800219
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200220 flags = btrfs_mask_flags(inode->i_mode, flags);
221 oldflags = btrfs_flags_to_ioctl(ip->flags);
222 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
223 if (!capable(CAP_LINUX_IMMUTABLE)) {
224 ret = -EPERM;
225 goto out_unlock;
226 }
227 }
228
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200229 if (flags & FS_SYNC_FL)
230 ip->flags |= BTRFS_INODE_SYNC;
231 else
232 ip->flags &= ~BTRFS_INODE_SYNC;
233 if (flags & FS_IMMUTABLE_FL)
234 ip->flags |= BTRFS_INODE_IMMUTABLE;
235 else
236 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
237 if (flags & FS_APPEND_FL)
238 ip->flags |= BTRFS_INODE_APPEND;
239 else
240 ip->flags &= ~BTRFS_INODE_APPEND;
241 if (flags & FS_NODUMP_FL)
242 ip->flags |= BTRFS_INODE_NODUMP;
243 else
244 ip->flags &= ~BTRFS_INODE_NODUMP;
245 if (flags & FS_NOATIME_FL)
246 ip->flags |= BTRFS_INODE_NOATIME;
247 else
248 ip->flags &= ~BTRFS_INODE_NOATIME;
249 if (flags & FS_DIRSYNC_FL)
250 ip->flags |= BTRFS_INODE_DIRSYNC;
251 else
252 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600253 if (flags & FS_NOCOW_FL) {
254 if (S_ISREG(mode)) {
255 /*
256 * It's safe to turn csums off here, no extents exist.
257 * Otherwise we want the flag to reflect the real COW
258 * status of the file and will not set it.
259 */
260 if (inode->i_size == 0)
261 ip->flags |= BTRFS_INODE_NODATACOW
262 | BTRFS_INODE_NODATASUM;
263 } else {
264 ip->flags |= BTRFS_INODE_NODATACOW;
265 }
266 } else {
267 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400268 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600269 */
270 if (S_ISREG(mode)) {
271 if (inode->i_size == 0)
272 ip->flags &= ~(BTRFS_INODE_NODATACOW
273 | BTRFS_INODE_NODATASUM);
274 } else {
275 ip->flags &= ~BTRFS_INODE_NODATACOW;
276 }
277 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200278
Liu Bo75e7cb72011-03-22 10:12:20 +0000279 /*
280 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
281 * flag may be changed automatically if compression code won't make
282 * things smaller.
283 */
284 if (flags & FS_NOCOMP_FL) {
285 ip->flags &= ~BTRFS_INODE_COMPRESS;
286 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000287
288 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
289 if (ret && ret != -ENODATA)
290 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000291 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000292 const char *comp;
293
Liu Bo75e7cb72011-03-22 10:12:20 +0000294 ip->flags |= BTRFS_INODE_COMPRESS;
295 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000296
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400297 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000298 comp = "lzo";
299 else
300 comp = "zlib";
301 ret = btrfs_set_prop(inode, "btrfs.compression",
302 comp, strlen(comp), 0);
303 if (ret)
304 goto out_drop;
305
Li Zefanebcb9042011-04-15 03:03:17 +0000306 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100307 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
308 if (ret && ret != -ENODATA)
309 goto out_drop;
Li Zefanebcb9042011-04-15 03:03:17 +0000310 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000311 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200312
Li Zefan4da6f1a2011-12-29 13:39:50 +0800313 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800314 if (IS_ERR(trans)) {
315 ret = PTR_ERR(trans);
316 goto out_drop;
317 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200318
Li Zefan306424cc2011-12-14 20:12:02 -0500319 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400320 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700321 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200323
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400324 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800325 out_drop:
326 if (ret) {
327 ip->flags = ip_oldflags;
328 inode->i_flags = i_oldflags;
329 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200330
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200331 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500332 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200333 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000334 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335}
336
337static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
338{
Al Viro496ad9a2013-01-23 17:07:38 -0500339 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200340
341 return put_user(inode->i_generation, arg);
342}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400343
Li Dongyangf7039b12011-03-24 10:24:28 +0000344static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
345{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400346 struct inode *inode = file_inode(file);
347 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000348 struct btrfs_device *device;
349 struct request_queue *q;
350 struct fstrim_range range;
351 u64 minlen = ULLONG_MAX;
352 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500353 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000354 int ret;
355
356 if (!capable(CAP_SYS_ADMIN))
357 return -EPERM;
358
Xiao Guangrong1f781602011-04-20 10:09:16 +0000359 rcu_read_lock();
360 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
361 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000362 if (!device->bdev)
363 continue;
364 q = bdev_get_queue(device->bdev);
365 if (blk_queue_discard(q)) {
366 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100367 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000368 minlen);
369 }
370 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000371 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200372
Li Dongyangf7039b12011-03-24 10:24:28 +0000373 if (!num_devices)
374 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000375 if (copy_from_user(&range, arg, sizeof(range)))
376 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000377 if (range.start > total_bytes ||
378 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200379 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000380
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200381 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000382 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400383 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000384 if (ret < 0)
385 return ret;
386
387 if (copy_to_user(arg, &range, sizeof(range)))
388 return -EFAULT;
389
390 return 0;
391}
392
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200393int btrfs_is_empty_uuid(u8 *uuid)
394{
Chris Mason46e0f662013-11-15 12:14:55 +0100395 int i;
396
397 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
398 if (uuid[i])
399 return 0;
400 }
401 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200402}
403
Miao Xied5c12072013-02-28 10:04:33 +0000404static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400405 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100406 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200407 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000408 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400410 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400411 struct btrfs_trans_handle *trans;
412 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100413 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414 struct btrfs_inode_item *inode_item;
415 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000416 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400417 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000418 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700419 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900420 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421 int ret;
422 int err;
423 u64 objectid;
424 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500425 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000426 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200427 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400428
David Sterba49a3c4d2016-03-24 17:49:22 +0100429 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
430 if (!root_item)
431 return -ENOMEM;
432
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400433 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400434 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100435 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000436
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800437 /*
438 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400439 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800440 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100441 if (btrfs_qgroup_level(objectid)) {
442 ret = -ENOSPC;
443 goto fail_free;
444 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800445
Miao Xied5c12072013-02-28 10:04:33 +0000446 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400447 /*
Miao Xied5c12072013-02-28 10:04:33 +0000448 * The same as the snapshot creation, please see the comment
449 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400450 */
Miao Xied5c12072013-02-28 10:04:33 +0000451 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200452 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000453 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100454 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400455
Miao Xied5c12072013-02-28 10:04:33 +0000456 trans = btrfs_start_transaction(root, 0);
457 if (IS_ERR(trans)) {
458 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100459 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100460 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000461 }
462 trans->block_rsv = &block_rsv;
463 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400465 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200466 if (ret)
467 goto fail;
468
David Sterba4d75f8a2014-06-15 01:54:12 +0200469 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400470 if (IS_ERR(leaf)) {
471 ret = PTR_ERR(leaf);
472 goto fail;
473 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400474
David Sterbab159fa22016-11-08 18:09:03 +0100475 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400476 btrfs_set_header_bytenr(leaf, leaf->start);
477 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400478 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400479 btrfs_set_header_owner(leaf, objectid);
480
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400481 write_extent_buffer_fsid(leaf, fs_info->fsid);
482 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400483 btrfs_mark_buffer_dirty(leaf);
484
David Sterba49a3c4d2016-03-24 17:49:22 +0100485 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800486 btrfs_set_stack_inode_generation(inode_item, 1);
487 btrfs_set_stack_inode_size(inode_item, 3);
488 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400489 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400490 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800491 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400492
David Sterba49a3c4d2016-03-24 17:49:22 +0100493 btrfs_set_root_flags(root_item, 0);
494 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800495 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000496
David Sterba49a3c4d2016-03-24 17:49:22 +0100497 btrfs_set_root_bytenr(root_item, leaf->start);
498 btrfs_set_root_generation(root_item, trans->transid);
499 btrfs_set_root_level(root_item, 0);
500 btrfs_set_root_refs(root_item, 1);
501 btrfs_set_root_used(root_item, leaf->len);
502 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400503
David Sterba49a3c4d2016-03-24 17:49:22 +0100504 btrfs_set_root_generation_v2(root_item,
505 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200506 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100507 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
508 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
509 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
510 root_item->ctime = root_item->otime;
511 btrfs_set_root_ctransid(root_item, trans->transid);
512 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400513
Chris Mason925baed2008-06-25 16:01:30 -0400514 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515 free_extent_buffer(leaf);
516 leaf = NULL;
517
David Sterba49a3c4d2016-03-24 17:49:22 +0100518 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519
520 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400521 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200522 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400523 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100524 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525 if (ret)
526 goto fail;
527
Yan, Zheng76dda932009-09-21 16:00:26 -0400528 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400529 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100530 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100531 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400532 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100533 goto fail;
534 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400535
536 btrfs_record_root_in_trans(trans, new_root);
537
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000538 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700539 if (ret) {
540 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400541 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700542 goto fail;
543 }
544
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530545 mutex_lock(&new_root->objectid_mutex);
546 new_root->highest_objectid = new_dirid;
547 mutex_unlock(&new_root->objectid_mutex);
548
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400549 /*
550 * insert the directory item
551 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200552 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100553 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400554 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100555 goto fail;
556 }
Chris Mason3de45862008-11-17 21:02:50 -0500557
558 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200559 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500560 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100561 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400562 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400563 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100564 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500565
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200566 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500567 ret = btrfs_update_inode(trans, root, dir);
568 BUG_ON(ret);
569
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400570 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400571 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200572 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500573 BUG_ON(ret);
574
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400575 ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400576 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200577 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400578 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200579
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400580fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100581 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000582 trans->block_rsv = NULL;
583 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100584 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800585
Sage Weil72fd0322010-10-29 15:41:32 -0400586 if (async_transid) {
587 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400588 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000589 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400590 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400591 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400592 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400593 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594 if (err && !ret)
595 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500596
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900597 if (!ret) {
598 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800599 if (IS_ERR(inode))
600 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900601 d_instantiate(dentry, inode);
602 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400603 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100604
605fail_free:
606 kfree(root_item);
607 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608}
609
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 Borisovfc4f21b12017-02-20 13:51:06 +0200981 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +0000982 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500983
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000984 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800985 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500986 }
987
Li Zefan6c282eb2012-06-11 16:03:35 +0800988 return em;
989}
990
991static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
992{
993 struct extent_map *next;
994 bool ret = true;
995
996 /* this is the last extent */
997 if (em->start + em->len >= i_size_read(inode))
998 return false;
999
1000 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001001 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1002 ret = false;
1003 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001004 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001005 ret = false;
1006
1007 free_extent_map(next);
1008 return ret;
1009}
1010
David Sterbaaab110a2014-07-29 17:32:10 +02001011static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001012 u64 *last_len, u64 *skip, u64 *defrag_end,
1013 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001014{
1015 struct extent_map *em;
1016 int ret = 1;
1017 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001018 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001019
1020 /*
1021 * make sure that once we start defragging an extent, we keep on
1022 * defragging it
1023 */
1024 if (start < *defrag_end)
1025 return 1;
1026
1027 *skip = 0;
1028
1029 em = defrag_lookup_extent(inode, start);
1030 if (!em)
1031 return 0;
1032
Chris Mason940100a2010-03-10 10:52:59 -05001033 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001034 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001035 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001036 goto out;
1037 }
1038
Liu Bo4a3560c2015-08-07 16:48:41 +08001039 if (!*defrag_end)
1040 prev_mergeable = false;
1041
Li Zefan6c282eb2012-06-11 16:03:35 +08001042 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001043 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001044 * we hit a real extent, if it is big or the next extent is not a
1045 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001046 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001047 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001048 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001049 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001050out:
Chris Mason940100a2010-03-10 10:52:59 -05001051 /*
1052 * last_len ends up being a counter of how many bytes we've defragged.
1053 * every time we choose not to defrag an extent, we reset *last_len
1054 * so that the next tiny extent will force a defrag.
1055 *
1056 * The end result of this is that tiny extents before a single big
1057 * extent will force at least part of that big extent to be defragged.
1058 */
1059 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001060 *defrag_end = extent_map_end(em);
1061 } else {
1062 *last_len = 0;
1063 *skip = extent_map_end(em);
1064 *defrag_end = 0;
1065 }
1066
1067 free_extent_map(em);
1068 return ret;
1069}
1070
Chris Mason4cb53002011-05-24 15:35:30 -04001071/*
1072 * it doesn't do much good to defrag one or two pages
1073 * at a time. This pulls in a nice chunk of pages
1074 * to COW and defrag.
1075 *
1076 * It also makes sure the delalloc code has enough
1077 * dirty data to avoid making new small extents as part
1078 * of the defrag
1079 *
1080 * It's a good idea to start RA on this range
1081 * before calling this.
1082 */
1083static int cluster_pages_for_defrag(struct inode *inode,
1084 struct page **pages,
1085 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001086 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001087{
Chris Mason4cb53002011-05-24 15:35:30 -04001088 unsigned long file_end;
1089 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001090 u64 page_start;
1091 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001092 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001093 int ret;
1094 int i;
1095 int i_done;
1096 struct btrfs_ordered_extent *ordered;
1097 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001098 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001099 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001100 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001101
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001102 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001103 if (!isize || start_index > file_end)
1104 return 0;
1105
1106 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001107
Qu Wenruo364ecf32017-02-27 15:10:38 +08001108 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001109 start_index << PAGE_SHIFT,
1110 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001111 if (ret)
1112 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001113 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001114 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001115
1116 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001117 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001118 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001119again:
Josef Bacika94733d2011-07-11 10:47:06 -04001120 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001121 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001122 if (!page)
1123 break;
1124
Miao Xie600a45e2012-02-16 15:01:24 +08001125 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001126 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001127 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001128 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001129 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001130 ordered = btrfs_lookup_ordered_extent(inode,
1131 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001132 unlock_extent_cached(tree, page_start, page_end,
1133 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001134 if (!ordered)
1135 break;
1136
1137 unlock_page(page);
1138 btrfs_start_ordered_extent(inode, ordered, 1);
1139 btrfs_put_ordered_extent(ordered);
1140 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001141 /*
1142 * we unlocked the page above, so we need check if
1143 * it was released or not.
1144 */
1145 if (page->mapping != inode->i_mapping) {
1146 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001147 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001148 goto again;
1149 }
Miao Xie600a45e2012-02-16 15:01:24 +08001150 }
1151
Chris Mason4cb53002011-05-24 15:35:30 -04001152 if (!PageUptodate(page)) {
1153 btrfs_readpage(NULL, page);
1154 lock_page(page);
1155 if (!PageUptodate(page)) {
1156 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001157 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001158 ret = -EIO;
1159 break;
1160 }
1161 }
Miao Xie600a45e2012-02-16 15:01:24 +08001162
Miao Xie600a45e2012-02-16 15:01:24 +08001163 if (page->mapping != inode->i_mapping) {
1164 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001165 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001166 goto again;
1167 }
1168
Chris Mason4cb53002011-05-24 15:35:30 -04001169 pages[i] = page;
1170 i_done++;
1171 }
1172 if (!i_done || ret)
1173 goto out;
1174
1175 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1176 goto out;
1177
1178 /*
1179 * so now we have a nice long stream of locked
1180 * and up to date pages, lets wait on them
1181 */
1182 for (i = 0; i < i_done; i++)
1183 wait_on_page_writeback(pages[i]);
1184
1185 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001186 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001187
1188 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001189 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001190 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1191 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001192 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1193 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001194
Liu Bo1f12bd02012-03-29 09:57:44 -04001195 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001196 spin_lock(&BTRFS_I(inode)->lock);
1197 BTRFS_I(inode)->outstanding_extents++;
1198 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001199 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001200 start_index << PAGE_SHIFT,
1201 (page_cnt - i_done) << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001202 }
1203
1204
Liu Bo9e8a4a82012-09-05 19:10:51 -06001205 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001206 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001207
1208 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1209 page_start, page_end - 1, &cached_state,
1210 GFP_NOFS);
1211
1212 for (i = 0; i < i_done; i++) {
1213 clear_page_dirty_for_io(pages[i]);
1214 ClearPageChecked(pages[i]);
1215 set_page_extent_mapped(pages[i]);
1216 set_page_dirty(pages[i]);
1217 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001218 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001219 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08001220 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001221 return i_done;
1222out:
1223 for (i = 0; i < i_done; i++) {
1224 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001225 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001226 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001227 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 start_index << PAGE_SHIFT,
1229 page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001230 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001231 return ret;
1232
1233}
1234
1235int btrfs_defrag_file(struct inode *inode, struct file *file,
1236 struct btrfs_ioctl_defrag_range_args *range,
1237 u64 newer_than, unsigned long max_to_defrag)
1238{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001239 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001240 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001241 struct file_ra_state *ra = NULL;
1242 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001243 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001244 u64 last_len = 0;
1245 u64 skip = 0;
1246 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001247 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001248 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001249 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001250 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001251 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001252 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001253 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001254 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001255 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001256 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001257 struct page **pages = NULL;
1258
Liu Bo0abd5b12013-04-16 09:20:28 +00001259 if (isize == 0)
1260 return 0;
1261
1262 if (range->start >= isize)
1263 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001264
1265 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1266 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1267 return -EINVAL;
1268 if (range->compress_type)
1269 compress_type = range->compress_type;
1270 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001271
Liu Bo0abd5b12013-04-16 09:20:28 +00001272 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001273 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001274
Chris Mason4cb53002011-05-24 15:35:30 -04001275 /*
David Sterba0a52d102017-06-22 03:22:58 +02001276 * If we were not given a file, allocate a readahead context. As
1277 * readahead is just an optimization, defrag will work without it so
1278 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001279 */
1280 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001281 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001282 if (ra)
1283 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001284 } else {
1285 ra = &file->f_ra;
1286 }
1287
David Sterba63e727e2017-06-22 03:13:02 +02001288 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001289 if (!pages) {
1290 ret = -ENOMEM;
1291 goto out_ra;
1292 }
1293
1294 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001295 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001296 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001297 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001298 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001299 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001300 }
1301
Chris Mason4cb53002011-05-24 15:35:30 -04001302 if (newer_than) {
1303 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001304 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001305 if (!ret) {
1306 range->start = newer_off;
1307 /*
1308 * we always align our defrag to help keep
1309 * the extents in the file evenly spaced
1310 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001311 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001312 } else
1313 goto out_ra;
1314 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001315 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001316 }
1317 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301318 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001319
Li Zefan2a0f7f52011-10-10 15:43:34 -04001320 /*
1321 * make writeback starts from i, so the defrag range can be
1322 * written sequentially.
1323 */
1324 if (i < inode->i_mapping->writeback_index)
1325 inode->i_mapping->writeback_index = i;
1326
Chris Masonf7f43cc2011-10-11 11:41:40 -04001327 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001328 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001329 /*
1330 * make sure we stop running if someone unmounts
1331 * the FS
1332 */
1333 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1334 break;
1335
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001336 if (btrfs_defrag_cancelled(fs_info)) {
1337 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001338 ret = -EAGAIN;
1339 break;
1340 }
1341
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001342 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001343 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001344 &defrag_end, range->flags &
1345 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001346 unsigned long next;
1347 /*
1348 * the should_defrag function tells us how much to skip
1349 * bump our counter by the suggested amount
1350 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001351 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001352 i = max(i + 1, next);
1353 continue;
1354 }
Li Zefan008873e2011-09-02 15:57:07 +08001355
1356 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001357 cluster = (PAGE_ALIGN(defrag_end) >>
1358 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001359 cluster = min(cluster, max_cluster);
1360 } else {
1361 cluster = max_cluster;
1362 }
1363
Li Zefan008873e2011-09-02 15:57:07 +08001364 if (i + cluster > ra_index) {
1365 ra_index = max(i, ra_index);
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);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001373 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1374 BTRFS_I(inode)->force_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 Boe1f041e2012-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
1421 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1422 /* 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:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001443 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
Al Viro59551022016-01-22 15:40:57 -05001444 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001445 BTRFS_I(inode)->force_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
2172 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2173 return -EOVERFLOW;
2174
2175 /* limit result size to 16MB */
2176 if (buf_size > buf_limit)
2177 buf_size = buf_limit;
2178
2179 inode = file_inode(file);
2180 ret = search_ioctl(inode, &args.key, &buf_size,
2181 (char *)(&uarg->buf[0]));
2182 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2183 ret = -EFAULT;
2184 else if (ret == -EOVERFLOW &&
2185 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2186 ret = -EFAULT;
2187
Yan, Zheng76dda932009-09-21 16:00:26 -04002188 return ret;
2189}
2190
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002191/*
Chris Masonac8e9812010-02-28 15:39:26 -05002192 * Search INODE_REFs to identify path name of 'dirid' directory
2193 * in a 'tree_id' tree. and sets path name to 'name'.
2194 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002195static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2196 u64 tree_id, u64 dirid, char *name)
2197{
2198 struct btrfs_root *root;
2199 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002200 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002201 int ret = -1;
2202 int slot;
2203 int len;
2204 int total_len = 0;
2205 struct btrfs_inode_ref *iref;
2206 struct extent_buffer *l;
2207 struct btrfs_path *path;
2208
2209 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2210 name[0]='\0';
2211 return 0;
2212 }
2213
2214 path = btrfs_alloc_path();
2215 if (!path)
2216 return -ENOMEM;
2217
Chris Masonac8e9812010-02-28 15:39:26 -05002218 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002219
2220 key.objectid = tree_id;
2221 key.type = BTRFS_ROOT_ITEM_KEY;
2222 key.offset = (u64)-1;
2223 root = btrfs_read_fs_root_no_name(info, &key);
2224 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002225 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002226 ret = -ENOENT;
2227 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002228 }
2229
2230 key.objectid = dirid;
2231 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002232 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002233
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302234 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002235 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2236 if (ret < 0)
2237 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002238 else if (ret > 0) {
2239 ret = btrfs_previous_item(root, path, dirid,
2240 BTRFS_INODE_REF_KEY);
2241 if (ret < 0)
2242 goto out;
2243 else if (ret > 0) {
2244 ret = -ENOENT;
2245 goto out;
2246 }
2247 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002248
2249 l = path->nodes[0];
2250 slot = path->slots[0];
2251 btrfs_item_key_to_cpu(l, &key, slot);
2252
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002253 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2254 len = btrfs_inode_ref_name_len(l, iref);
2255 ptr -= len + 1;
2256 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002257 if (ptr < name) {
2258 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002259 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002260 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002261
2262 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302263 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002264
2265 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2266 break;
2267
David Sterbab3b4aa72011-04-21 01:20:15 +02002268 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002269 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002270 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002271 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002272 }
Li Zefan77906a502011-07-14 03:16:00 +00002273 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302274 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002275 ret = 0;
2276out:
2277 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002278 return ret;
2279}
2280
2281static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2282 void __user *argp)
2283{
2284 struct btrfs_ioctl_ino_lookup_args *args;
2285 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002286 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002287
Julia Lawall2354d08f2010-10-29 15:14:18 -04002288 args = memdup_user(argp, sizeof(*args));
2289 if (IS_ERR(args))
2290 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002291
Al Viro496ad9a2013-01-23 17:07:38 -05002292 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002293
David Sterba01b810b2015-05-12 19:14:49 +02002294 /*
2295 * Unprivileged query to obtain the containing subvolume root id. The
2296 * path is reset so it's consistent with btrfs_search_path_in_tree.
2297 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002298 if (args->treeid == 0)
2299 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2300
David Sterba01b810b2015-05-12 19:14:49 +02002301 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2302 args->name[0] = 0;
2303 goto out;
2304 }
2305
2306 if (!capable(CAP_SYS_ADMIN)) {
2307 ret = -EPERM;
2308 goto out;
2309 }
2310
Chris Masonac8e9812010-02-28 15:39:26 -05002311 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2312 args->treeid, args->objectid,
2313 args->name);
2314
David Sterba01b810b2015-05-12 19:14:49 +02002315out:
Chris Masonac8e9812010-02-28 15:39:26 -05002316 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2317 ret = -EFAULT;
2318
2319 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002320 return ret;
2321}
2322
Yan, Zheng76dda932009-09-21 16:00:26 -04002323static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2324 void __user *arg)
2325{
Al Viro54563d42013-09-01 15:57:51 -04002326 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002327 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002328 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002329 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002330 struct inode *inode;
2331 struct btrfs_root *root = BTRFS_I(dir)->root;
2332 struct btrfs_root *dest = NULL;
2333 struct btrfs_ioctl_vol_args *vol_args;
2334 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002335 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002336 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002337 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002338 int namelen;
2339 int ret;
2340 int err = 0;
2341
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002342 if (!S_ISDIR(dir->i_mode))
2343 return -ENOTDIR;
2344
Yan, Zheng76dda932009-09-21 16:00:26 -04002345 vol_args = memdup_user(arg, sizeof(*vol_args));
2346 if (IS_ERR(vol_args))
2347 return PTR_ERR(vol_args);
2348
2349 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2350 namelen = strlen(vol_args->name);
2351 if (strchr(vol_args->name, '/') ||
2352 strncmp(vol_args->name, "..", namelen) == 0) {
2353 err = -EINVAL;
2354 goto out;
2355 }
2356
Al Viroa561be72011-11-23 11:57:51 -05002357 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002358 if (err)
2359 goto out;
2360
David Sterba521e0542014-04-15 16:41:44 +02002361
Al Viro00235412016-05-26 00:05:12 -04002362 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2363 if (err == -EINTR)
2364 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002365 dentry = lookup_one_len(vol_args->name, parent, namelen);
2366 if (IS_ERR(dentry)) {
2367 err = PTR_ERR(dentry);
2368 goto out_unlock_dir;
2369 }
2370
David Howells2b0143b2015-03-17 22:25:59 +00002371 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002372 err = -ENOENT;
2373 goto out_dput;
2374 }
2375
David Howells2b0143b2015-03-17 22:25:59 +00002376 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002377 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302378 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002379 /*
2380 * Regular user. Only allow this with a special mount
2381 * option, when the user has write+exec access to the
2382 * subvol root, and when rmdir(2) would have been
2383 * allowed.
2384 *
2385 * Note that this is _not_ check that the subvol is
2386 * empty or doesn't contain data that we wouldn't
2387 * otherwise be able to delete.
2388 *
2389 * Users who want to delete empty subvols should try
2390 * rmdir(2).
2391 */
2392 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002393 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002394 goto out_dput;
2395
2396 /*
2397 * Do not allow deletion if the parent dir is the same
2398 * as the dir to be deleted. That means the ioctl
2399 * must be called on the dentry referencing the root
2400 * of the subvol, not a random directory contained
2401 * within it.
2402 */
2403 err = -EINVAL;
2404 if (root == dest)
2405 goto out_dput;
2406
2407 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2408 if (err)
2409 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002410 }
2411
Miao Xie5c39da52012-10-22 11:39:53 +00002412 /* check if subvolume may be deleted by a user */
2413 err = btrfs_may_delete(dir, dentry, 1);
2414 if (err)
2415 goto out_dput;
2416
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002417 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002418 err = -EINVAL;
2419 goto out_dput;
2420 }
2421
Al Viro59551022016-01-22 15:40:57 -05002422 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002423
2424 /*
2425 * Don't allow to delete a subvolume with send in progress. This is
2426 * inside the i_mutex so the error handling that has to drop the bit
2427 * again is not run concurrently.
2428 */
2429 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002430 root_flags = btrfs_root_flags(&dest->root_item);
2431 if (dest->send_in_progress == 0) {
2432 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002433 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2434 spin_unlock(&dest->root_item_lock);
2435 } else {
2436 spin_unlock(&dest->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002437 btrfs_warn(fs_info,
2438 "Attempt to delete subvolume %llu during send",
2439 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002440 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002441 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002442 }
2443
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002444 down_write(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -04002445
2446 err = may_destroy_subvol(dest);
2447 if (err)
2448 goto out_up_write;
2449
Miao Xiec58aaad2013-02-28 10:05:36 +00002450 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2451 /*
2452 * One for dir inode, two for dir entries, two for root
2453 * ref/backref.
2454 */
2455 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002456 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002457 if (err)
2458 goto out_up_write;
2459
Yan, Zhenga22285a2010-05-16 10:48:46 -04002460 trans = btrfs_start_transaction(root, 0);
2461 if (IS_ERR(trans)) {
2462 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002463 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002464 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002465 trans->block_rsv = &block_rsv;
2466 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002467
Nikolay Borisov43663552017-01-18 00:31:29 +02002468 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
Filipe Manana2be63d52016-02-12 11:34:23 +00002469
Yan, Zheng76dda932009-09-21 16:00:26 -04002470 ret = btrfs_unlink_subvol(trans, root, dir,
2471 dest->root_key.objectid,
2472 dentry->d_name.name,
2473 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002474 if (ret) {
2475 err = ret;
Jeff Mahoney66642832016-06-10 18:19:25 -04002476 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002477 goto out_end_trans;
2478 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002479
2480 btrfs_record_root_in_trans(trans, dest);
2481
2482 memset(&dest->root_item.drop_progress, 0,
2483 sizeof(dest->root_item.drop_progress));
2484 dest->root_item.drop_level = 0;
2485 btrfs_set_root_refs(&dest->root_item, 0);
2486
Miao Xie27cdeb72014-04-02 19:51:05 +08002487 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002488 ret = btrfs_insert_orphan_item(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002489 fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04002490 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002491 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002492 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002493 err = ret;
2494 goto out_end_trans;
2495 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002496 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002497
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002498 ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002499 BTRFS_UUID_KEY_SUBVOL,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002500 dest->root_key.objectid);
2501 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002502 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002503 err = ret;
2504 goto out_end_trans;
2505 }
2506 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 ret = btrfs_uuid_tree_rem(trans, fs_info,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002508 dest->root_item.received_uuid,
2509 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2510 dest->root_key.objectid);
2511 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002512 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002513 err = ret;
2514 goto out_end_trans;
2515 }
2516 }
2517
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002518out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002519 trans->block_rsv = NULL;
2520 trans->bytes_reserved = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002521 ret = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002522 if (ret && !err)
2523 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002524 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002525out_release:
David Sterba7775c812017-02-10 19:18:18 +01002526 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Yan, Zheng76dda932009-09-21 16:00:26 -04002527out_up_write:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002528 up_write(&fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002529 if (err) {
2530 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002531 root_flags = btrfs_root_flags(&dest->root_item);
2532 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002533 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2534 spin_unlock(&dest->root_item_lock);
2535 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002536out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002537 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002538 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002539 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002540 btrfs_invalidate_inodes(dest);
2541 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002542 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002543
2544 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002545 if (dest->ino_cache_inode) {
2546 iput(dest->ino_cache_inode);
2547 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002548 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002549 }
2550out_dput:
2551 dput(dentry);
2552out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002553 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002554out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002555 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002556out:
2557 kfree(vol_args);
2558 return err;
2559}
2560
Chris Mason1e701a32010-03-11 09:42:04 -05002561static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002562{
Al Viro496ad9a2013-01-23 17:07:38 -05002563 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002564 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002565 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002566 int ret;
2567
Ilya Dryomov25122d12013-01-20 15:57:57 +02002568 ret = mnt_want_write_file(file);
2569 if (ret)
2570 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002571
Ilya Dryomov25122d12013-01-20 15:57:57 +02002572 if (btrfs_root_readonly(root)) {
2573 ret = -EROFS;
2574 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002575 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002576
2577 switch (inode->i_mode & S_IFMT) {
2578 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002579 if (!capable(CAP_SYS_ADMIN)) {
2580 ret = -EPERM;
2581 goto out;
2582 }
Eric Sandeende78b512013-01-31 18:21:12 +00002583 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002584 break;
2585 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002586 if (!(file->f_mode & FMODE_WRITE)) {
2587 ret = -EINVAL;
2588 goto out;
2589 }
Chris Mason1e701a32010-03-11 09:42:04 -05002590
2591 range = kzalloc(sizeof(*range), GFP_KERNEL);
2592 if (!range) {
2593 ret = -ENOMEM;
2594 goto out;
2595 }
2596
2597 if (argp) {
2598 if (copy_from_user(range, argp,
2599 sizeof(*range))) {
2600 ret = -EFAULT;
2601 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002602 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002603 }
2604 /* compression requires us to start the IO */
2605 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2606 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2607 range->extent_thresh = (u32)-1;
2608 }
2609 } else {
2610 /* the rest are all set to zero by kzalloc */
2611 range->len = (u64)-1;
2612 }
Al Viro496ad9a2013-01-23 17:07:38 -05002613 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002614 range, 0, 0);
2615 if (ret > 0)
2616 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002617 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002618 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002619 default:
2620 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002621 }
Chris Masone441d542009-01-05 16:57:23 -05002622out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002623 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002624 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002625}
2626
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002627static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002628{
2629 struct btrfs_ioctl_vol_args *vol_args;
2630 int ret;
2631
Chris Masone441d542009-01-05 16:57:23 -05002632 if (!capable(CAP_SYS_ADMIN))
2633 return -EPERM;
2634
David Sterba171938e2017-03-28 14:44:21 +02002635 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002636 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002637
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002638 mutex_lock(&fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002639 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002640 if (IS_ERR(vol_args)) {
2641 ret = PTR_ERR(vol_args);
2642 goto out;
2643 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002644
Mark Fasheh5516e592008-07-24 12:20:14 -04002645 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002646 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002647
Anand Jain43d20762014-07-01 00:58:56 +08002648 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002649 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002650
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002651 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002652out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002653 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02002654 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002655 return ret;
2656}
2657
Anand Jain6b526ed2016-02-13 10:01:39 +08002658static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002659{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002660 struct inode *inode = file_inode(file);
2661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002662 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002663 int ret;
2664
Chris Masone441d542009-01-05 16:57:23 -05002665 if (!capable(CAP_SYS_ADMIN))
2666 return -EPERM;
2667
Miao Xieda249272012-11-26 08:44:50 +00002668 ret = mnt_want_write_file(file);
2669 if (ret)
2670 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002671
Li Zefandae7b662009-04-08 15:06:54 +08002672 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002673 if (IS_ERR(vol_args)) {
2674 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002675 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002676 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002677
Anand Jain6b526ed2016-02-13 10:01:39 +08002678 /* Check for compatibility reject unknown flags */
David Sterba735654e2016-02-15 18:15:21 +01002679 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2680 return -EOPNOTSUPP;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002681
David Sterba171938e2017-03-28 14:44:21 +02002682 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002683 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2684 goto out;
2685 }
2686
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002687 mutex_lock(&fs_info->volume_mutex);
David Sterba735654e2016-02-15 18:15:21 +01002688 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002689 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002690 } else {
2691 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002692 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002693 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002694 mutex_unlock(&fs_info->volume_mutex);
David Sterba171938e2017-03-28 14:44:21 +02002695 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002696
Anand Jain6b526ed2016-02-13 10:01:39 +08002697 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002698 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002699 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002700 vol_args->devid);
2701 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002702 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002703 vol_args->name);
2704 }
Anand Jain183860f2013-05-17 10:52:45 +00002705out:
2706 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002707err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002708 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002709 return ret;
2710}
2711
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002712static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2713{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002714 struct inode *inode = file_inode(file);
2715 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002716 struct btrfs_ioctl_vol_args *vol_args;
2717 int ret;
2718
2719 if (!capable(CAP_SYS_ADMIN))
2720 return -EPERM;
2721
2722 ret = mnt_want_write_file(file);
2723 if (ret)
2724 return ret;
2725
David Sterba171938e2017-03-28 14:44:21 +02002726 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002727 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002728 goto out_drop_write;
2729 }
2730
2731 vol_args = memdup_user(arg, sizeof(*vol_args));
2732 if (IS_ERR(vol_args)) {
2733 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002734 goto out;
2735 }
2736
David Sterba58d7bbf2016-05-04 14:10:47 +02002737 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002738 mutex_lock(&fs_info->volume_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002739 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002740 mutex_unlock(&fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002741
2742 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002743 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002744 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002745out:
David Sterba171938e2017-03-28 14:44:21 +02002746 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002747out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002748 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002749
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002750 return ret;
2751}
2752
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002753static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2754 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002755{
Li Zefan027ed2f2011-06-08 08:27:56 +00002756 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002757 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002758 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002759 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002760
Li Zefan027ed2f2011-06-08 08:27:56 +00002761 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2762 if (!fi_args)
2763 return -ENOMEM;
2764
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002765 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002766 fi_args->num_devices = fs_devices->num_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002767 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002768
Byongho Leed7641a42015-09-01 23:10:57 +09002769 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002770 if (device->devid > fi_args->max_id)
2771 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002772 }
2773 mutex_unlock(&fs_devices->device_list_mutex);
2774
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002775 fi_args->nodesize = fs_info->super_copy->nodesize;
2776 fi_args->sectorsize = fs_info->super_copy->sectorsize;
2777 fi_args->clone_alignment = fs_info->super_copy->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002778
Li Zefan027ed2f2011-06-08 08:27:56 +00002779 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2780 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002781
Li Zefan027ed2f2011-06-08 08:27:56 +00002782 kfree(fi_args);
2783 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002784}
2785
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002786static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2787 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002788{
2789 struct btrfs_ioctl_dev_info_args *di_args;
2790 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002791 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002792 int ret = 0;
2793 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002794
Jan Schmidt475f6382011-03-11 15:41:01 +01002795 di_args = memdup_user(arg, sizeof(*di_args));
2796 if (IS_ERR(di_args))
2797 return PTR_ERR(di_args);
2798
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002799 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002800 s_uuid = di_args->uuid;
2801
2802 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002803 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002804
2805 if (!dev) {
2806 ret = -ENODEV;
2807 goto out;
2808 }
2809
2810 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002811 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2812 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002813 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002814 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002815 struct rcu_string *name;
2816
2817 rcu_read_lock();
2818 name = rcu_dereference(dev->name);
2819 strncpy(di_args->path, name->str, sizeof(di_args->path));
2820 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002821 di_args->path[sizeof(di_args->path) - 1] = 0;
2822 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002823 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002824 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002825
2826out:
David Sterba55793c02013-04-26 15:20:23 +00002827 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002828 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2829 ret = -EFAULT;
2830
2831 kfree(di_args);
2832 return ret;
2833}
2834
Mark Fashehf4414602015-06-30 14:42:05 -07002835static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002836{
2837 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002838
Mark Fasheh416161d2013-08-06 11:42:51 -07002839 page = grab_cache_page(inode->i_mapping, index);
2840 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002841 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002842
2843 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002844 int ret;
2845
2846 ret = btrfs_readpage(NULL, page);
2847 if (ret)
2848 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002849 lock_page(page);
2850 if (!PageUptodate(page)) {
2851 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002852 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002853 return ERR_PTR(-EIO);
2854 }
2855 if (page->mapping != inode->i_mapping) {
2856 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002857 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002858 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002859 }
2860 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002861
2862 return page;
2863}
2864
Mark Fashehf4414602015-06-30 14:42:05 -07002865static int gather_extent_pages(struct inode *inode, struct page **pages,
2866 int num_pages, u64 off)
2867{
2868 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002869 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002870
2871 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002872again:
Mark Fashehf4414602015-06-30 14:42:05 -07002873 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002874 if (IS_ERR(pages[i])) {
2875 int err = PTR_ERR(pages[i]);
2876
2877 if (err == -EAGAIN)
2878 goto again;
2879 pages[i] = NULL;
2880 return err;
2881 }
Mark Fashehf4414602015-06-30 14:42:05 -07002882 }
2883 return 0;
2884}
2885
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002886static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2887 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002888{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002889 /*
2890 * Do any pending delalloc/csum calculations on inode, one way or
2891 * another, and lock file content.
2892 * The locking order is:
2893 *
2894 * 1) pages
2895 * 2) range in the inode's io tree
2896 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002897 while (1) {
2898 struct btrfs_ordered_extent *ordered;
2899 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2900 ordered = btrfs_lookup_first_ordered_extent(inode,
2901 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002902 if ((!ordered ||
2903 ordered->file_offset + ordered->len <= off ||
2904 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002905 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002906 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2907 if (ordered)
2908 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002909 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002910 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002911 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2912 if (ordered)
2913 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002914 if (!retry_range_locking)
2915 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002916 btrfs_wait_ordered_range(inode, off, len);
2917 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002918 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002919}
2920
Mark Fashehf4414602015-06-30 14:42:05 -07002921static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002922{
Al Viro59551022016-01-22 15:40:57 -05002923 inode_unlock(inode1);
2924 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002925}
2926
Mark Fashehf4414602015-06-30 14:42:05 -07002927static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2928{
2929 if (inode1 < inode2)
2930 swap(inode1, inode2);
2931
Al Viro59551022016-01-22 15:40:57 -05002932 inode_lock_nested(inode1, I_MUTEX_PARENT);
2933 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002934}
2935
2936static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2937 struct inode *inode2, u64 loff2, u64 len)
2938{
2939 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2940 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2941}
2942
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002943static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2944 struct inode *inode2, u64 loff2, u64 len,
2945 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002946{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002947 int ret;
2948
Mark Fasheh416161d2013-08-06 11:42:51 -07002949 if (inode1 < inode2) {
2950 swap(inode1, inode2);
2951 swap(loff1, loff2);
2952 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002953 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2954 if (ret)
2955 return ret;
2956 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2957 if (ret)
2958 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2959 loff1 + len - 1);
2960 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002961}
2962
2963struct cmp_pages {
2964 int num_pages;
2965 struct page **src_pages;
2966 struct page **dst_pages;
2967};
2968
2969static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2970{
2971 int i;
2972 struct page *pg;
2973
2974 for (i = 0; i < cmp->num_pages; i++) {
2975 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002976 if (pg) {
2977 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002978 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002979 }
Mark Fashehf4414602015-06-30 14:42:05 -07002980 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002981 if (pg) {
2982 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002983 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002984 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002985 }
Mark Fashehf4414602015-06-30 14:42:05 -07002986 kfree(cmp->src_pages);
2987 kfree(cmp->dst_pages);
2988}
2989
2990static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2991 struct inode *dst, u64 dst_loff,
2992 u64 len, struct cmp_pages *cmp)
2993{
2994 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002995 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002996 struct page **src_pgarr, **dst_pgarr;
2997
2998 /*
2999 * We must gather up all the pages before we initiate our
3000 * extent locking. We use an array for the page pointers. Size
3001 * of the array is bounded by len, which is in turn bounded by
3002 * BTRFS_MAX_DEDUPE_LEN.
3003 */
David Sterba66722f72016-02-11 15:01:38 +01003004 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
3005 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07003006 if (!src_pgarr || !dst_pgarr) {
3007 kfree(src_pgarr);
3008 kfree(dst_pgarr);
3009 return -ENOMEM;
3010 }
3011 cmp->num_pages = num_pages;
3012 cmp->src_pages = src_pgarr;
3013 cmp->dst_pages = dst_pgarr;
3014
Filipe Mananab1517622017-02-21 17:14:52 +00003015 /*
3016 * If deduping ranges in the same inode, locking rules make it mandatory
3017 * to always lock pages in ascending order to avoid deadlocks with
3018 * concurrent tasks (such as starting writeback/delalloc).
3019 */
3020 if (src == dst && dst_loff < loff) {
3021 swap(src_pgarr, dst_pgarr);
3022 swap(loff, dst_loff);
3023 }
3024
3025 ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003026 if (ret)
3027 goto out;
3028
Filipe Mananab1517622017-02-21 17:14:52 +00003029 ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003030
3031out:
3032 if (ret)
3033 btrfs_cmp_data_free(cmp);
3034 return 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003035}
3036
David Sterba1a287cf2017-02-10 20:15:10 +01003037static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003038{
3039 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003040 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003041 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003042 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003043 void *addr, *dst_addr;
3044
Mark Fashehf4414602015-06-30 14:42:05 -07003045 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003046 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003047 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003048 cmp_len = len;
3049
Mark Fashehf4414602015-06-30 14:42:05 -07003050 BUG_ON(i >= cmp->num_pages);
3051
3052 src_page = cmp->src_pages[i];
3053 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003054 ASSERT(PageLocked(src_page));
3055 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003056
Mark Fasheh416161d2013-08-06 11:42:51 -07003057 addr = kmap_atomic(src_page);
3058 dst_addr = kmap_atomic(dst_page);
3059
3060 flush_dcache_page(src_page);
3061 flush_dcache_page(dst_page);
3062
3063 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003064 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003065
3066 kunmap_atomic(addr);
3067 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003068
3069 if (ret)
3070 break;
3071
Mark Fasheh416161d2013-08-06 11:42:51 -07003072 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003073 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003074 }
3075
3076 return ret;
3077}
3078
Mark Fashehe1d227a2015-06-08 15:05:25 -07003079static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3080 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003081{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003082 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003083 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3084
Mark Fashehe1d227a2015-06-08 15:05:25 -07003085 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003086 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003087
3088 /* if we extend to eof, continue to block boundary */
3089 if (off + len == inode->i_size)
3090 *plen = len = ALIGN(inode->i_size, bs) - off;
3091
Mark Fasheh416161d2013-08-06 11:42:51 -07003092 /* Check that we are block aligned - btrfs_clone() requires this */
3093 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3094 return -EINVAL;
3095
3096 return 0;
3097}
3098
Mark Fashehe1d227a2015-06-08 15:05:25 -07003099static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003100 struct inode *dst, u64 dst_loff)
3101{
3102 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003103 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003104 struct cmp_pages cmp;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003105 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003106 u64 same_lock_start = 0;
3107 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003108
Filipe Manana113e8282015-03-30 18:26:47 +01003109 if (len == 0)
3110 return 0;
3111
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003112 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003113 inode_lock(src);
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003114 else
3115 btrfs_double_inode_lock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003116
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003117 ret = extent_same_check_offsets(src, loff, &len, olen);
3118 if (ret)
3119 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003120
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003121 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3122 if (ret)
3123 goto out_unlock;
3124
3125 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003126 /*
3127 * Single inode case wants the same checks, except we
3128 * don't want our length pushed out past i_size as
3129 * comparing that data range makes no sense.
3130 *
3131 * extent_same_check_offsets() will do this for an
3132 * unaligned length at i_size, so catch it here and
3133 * reject the request.
3134 *
3135 * This effectively means we require aligned extents
3136 * for the single-inode case, whereas the other cases
3137 * allow an unaligned length so long as it ends at
3138 * i_size.
3139 */
3140 if (len != olen) {
3141 ret = -EINVAL;
3142 goto out_unlock;
3143 }
3144
3145 /* Check for overlapping ranges */
3146 if (dst_loff + len > loff && dst_loff < loff + len) {
3147 ret = -EINVAL;
3148 goto out_unlock;
3149 }
3150
3151 same_lock_start = min_t(u64, loff, dst_loff);
3152 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003153 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003154
3155 /* don't make the dst file partly checksummed */
3156 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3157 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3158 ret = -EINVAL;
3159 goto out_unlock;
3160 }
3161
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003162again:
Mark Fashehf4414602015-06-30 14:42:05 -07003163 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3164 if (ret)
3165 goto out_unlock;
3166
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003167 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003168 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3169 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003170 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003171 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3172 false);
3173 /*
3174 * If one of the inodes has dirty pages in the respective range or
3175 * ordered extents, we need to flush dellaloc and wait for all ordered
3176 * extents in the range. We must unlock the pages and the ranges in the
3177 * io trees to avoid deadlocks when flushing delalloc (requires locking
3178 * pages) and when waiting for ordered extents to complete (they require
3179 * range locking).
3180 */
3181 if (ret == -EAGAIN) {
3182 /*
3183 * Ranges in the io trees already unlocked. Now unlock all
3184 * pages before waiting for all IO to complete.
3185 */
3186 btrfs_cmp_data_free(&cmp);
3187 if (same_inode) {
3188 btrfs_wait_ordered_range(src, same_lock_start,
3189 same_lock_len);
3190 } else {
3191 btrfs_wait_ordered_range(src, loff, len);
3192 btrfs_wait_ordered_range(dst, dst_loff, len);
3193 }
3194 goto again;
3195 }
3196 ASSERT(ret == 0);
3197 if (WARN_ON(ret)) {
3198 /* ranges in the io trees already unlocked */
3199 btrfs_cmp_data_free(&cmp);
3200 return ret;
3201 }
Mark Fashehf4414602015-06-30 14:42:05 -07003202
Mark Fasheh207910d2015-06-30 14:42:04 -07003203 /* pass original length for comparison so we stay within i_size */
David Sterba1a287cf2017-02-10 20:15:10 +01003204 ret = btrfs_cmp_data(olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003205 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003206 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003207
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003208 if (same_inode)
3209 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3210 same_lock_start + same_lock_len - 1);
3211 else
3212 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003213
3214 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003215out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003216 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003217 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003218 else
3219 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003220
3221 return ret;
3222}
3223
Byongho Leeee221842015-12-15 01:42:10 +09003224#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003225
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003226ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3227 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003228{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003229 struct inode *src = file_inode(src_file);
3230 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003231 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003232 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003233
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003234 if (olen > BTRFS_MAX_DEDUPE_LEN)
3235 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003236
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003237 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003238 /*
3239 * Btrfs does not support blocksize < page_size. As a
3240 * result, btrfs_cmp_data() won't correctly handle
3241 * this situation without an update.
3242 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003243 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003244 }
3245
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003246 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3247 if (res)
3248 return res;
3249 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003250}
3251
Filipe Mananaf82a9902014-06-01 01:50:28 +01003252static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3253 struct inode *inode,
3254 u64 endoff,
3255 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003256 const u64 olen,
3257 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003258{
3259 struct btrfs_root *root = BTRFS_I(inode)->root;
3260 int ret;
3261
3262 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003263 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003264 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003265 /*
3266 * We round up to the block size at eof when determining which
3267 * extents to clone above, but shouldn't round up the file size.
3268 */
3269 if (endoff > destoff + olen)
3270 endoff = destoff + olen;
3271 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003272 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003273
3274 ret = btrfs_update_inode(trans, root, inode);
3275 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003276 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003277 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003278 goto out;
3279 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003280 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003281out:
3282 return ret;
3283}
3284
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003285static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003286 const struct btrfs_trans_handle *trans,
3287 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003288 const u64 hole_offset,
3289 const u64 hole_len)
3290{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003291 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003292 struct extent_map *em;
3293 int ret;
3294
3295 em = alloc_extent_map();
3296 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003297 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003298 return;
3299 }
3300
Filipe Manana14f59792014-06-29 21:45:40 +01003301 if (path) {
3302 struct btrfs_file_extent_item *fi;
3303
3304 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3305 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003306 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003307 em->generation = -1;
3308 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3309 BTRFS_FILE_EXTENT_INLINE)
3310 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003311 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003312 } else {
3313 em->start = hole_offset;
3314 em->len = hole_len;
3315 em->ram_bytes = em->len;
3316 em->orig_start = hole_offset;
3317 em->block_start = EXTENT_MAP_HOLE;
3318 em->block_len = 0;
3319 em->orig_block_len = 0;
3320 em->compress_type = BTRFS_COMPRESS_NONE;
3321 em->generation = trans->transid;
3322 }
3323
3324 while (1) {
3325 write_lock(&em_tree->lock);
3326 ret = add_extent_mapping(em_tree, em, 1);
3327 write_unlock(&em_tree->lock);
3328 if (ret != -EEXIST) {
3329 free_extent_map(em);
3330 break;
3331 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003332 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003333 em->start + em->len - 1, 0);
3334 }
3335
David Sterbaee39b432014-09-30 01:33:33 +02003336 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003337 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003338}
3339
Filipe Manana8039d872015-10-13 15:15:00 +01003340/*
3341 * Make sure we do not end up inserting an inline extent into a file that has
3342 * already other (non-inline) extents. If a file has an inline extent it can
3343 * not have any other extents and the (single) inline extent must start at the
3344 * file offset 0. Failing to respect these rules will lead to file corruption,
3345 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3346 *
3347 * We can have extents that have been already written to disk or we can have
3348 * dirty ranges still in delalloc, in which case the extent maps and items are
3349 * created only when we run delalloc, and the delalloc ranges might fall outside
3350 * the range we are currently locking in the inode's io tree. So we check the
3351 * inode's i_size because of that (i_size updates are done while holding the
3352 * i_mutex, which we are holding here).
3353 * We also check to see if the inode has a size not greater than "datal" but has
3354 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3355 * protected against such concurrent fallocate calls by the i_mutex).
3356 *
3357 * If the file has no extents but a size greater than datal, do not allow the
3358 * copy because we would need turn the inline extent into a non-inline one (even
3359 * with NO_HOLES enabled). If we find our destination inode only has one inline
3360 * extent, just overwrite it with the source inline extent if its size is less
3361 * than the source extent's size, or we could copy the source inline extent's
3362 * data into the destination inode's inline extent if the later is greater then
3363 * the former.
3364 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003365static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003366 struct btrfs_trans_handle *trans,
3367 struct btrfs_path *path,
3368 struct btrfs_key *new_key,
3369 const u64 drop_start,
3370 const u64 datal,
3371 const u64 skip,
3372 const u64 size,
3373 char *inline_data)
3374{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003376 struct btrfs_root *root = BTRFS_I(dst)->root;
3377 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003378 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003379 int ret;
3380 struct btrfs_key key;
3381
3382 if (new_key->offset > 0)
3383 return -EOPNOTSUPP;
3384
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003385 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003386 key.type = BTRFS_EXTENT_DATA_KEY;
3387 key.offset = 0;
3388 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3389 if (ret < 0) {
3390 return ret;
3391 } else if (ret > 0) {
3392 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3393 ret = btrfs_next_leaf(root, path);
3394 if (ret < 0)
3395 return ret;
3396 else if (ret > 0)
3397 goto copy_inline_extent;
3398 }
3399 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003400 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003401 key.type == BTRFS_EXTENT_DATA_KEY) {
3402 ASSERT(key.offset > 0);
3403 return -EOPNOTSUPP;
3404 }
3405 } else if (i_size_read(dst) <= datal) {
3406 struct btrfs_file_extent_item *ei;
3407 u64 ext_len;
3408
3409 /*
3410 * If the file size is <= datal, make sure there are no other
3411 * extents following (can happen do to an fallocate call with
3412 * the flag FALLOC_FL_KEEP_SIZE).
3413 */
3414 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3415 struct btrfs_file_extent_item);
3416 /*
3417 * If it's an inline extent, it can not have other extents
3418 * following it.
3419 */
3420 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3421 BTRFS_FILE_EXTENT_INLINE)
3422 goto copy_inline_extent;
3423
3424 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3425 if (ext_len > aligned_end)
3426 return -EOPNOTSUPP;
3427
3428 ret = btrfs_next_item(root, path);
3429 if (ret < 0) {
3430 return ret;
3431 } else if (ret == 0) {
3432 btrfs_item_key_to_cpu(path->nodes[0], &key,
3433 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003434 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003435 key.type == BTRFS_EXTENT_DATA_KEY)
3436 return -EOPNOTSUPP;
3437 }
3438 }
3439
3440copy_inline_extent:
3441 /*
3442 * We have no extent items, or we have an extent at offset 0 which may
3443 * or may not be inlined. All these cases are dealt the same way.
3444 */
3445 if (i_size_read(dst) > datal) {
3446 /*
3447 * If the destination inode has an inline extent...
3448 * This would require copying the data from the source inline
3449 * extent into the beginning of the destination's inline extent.
3450 * But this is really complex, both extents can be compressed
3451 * or just one of them, which would require decompressing and
3452 * re-compressing data (which could increase the new compressed
3453 * size, not allowing the compressed data to fit anymore in an
3454 * inline extent).
3455 * So just don't support this case for now (it should be rare,
3456 * we are not really saving space when cloning inline extents).
3457 */
3458 return -EOPNOTSUPP;
3459 }
3460
3461 btrfs_release_path(path);
3462 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3463 if (ret)
3464 return ret;
3465 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3466 if (ret)
3467 return ret;
3468
3469 if (skip) {
3470 const u32 start = btrfs_file_extent_calc_inline_size(0);
3471
3472 memmove(inline_data + start, inline_data + start + skip, datal);
3473 }
3474
3475 write_extent_buffer(path->nodes[0], inline_data,
3476 btrfs_item_ptr_offset(path->nodes[0],
3477 path->slots[0]),
3478 size);
3479 inode_add_bytes(dst, datal);
3480
3481 return 0;
3482}
3483
Mark Fasheh32b7c682013-08-06 11:42:49 -07003484/**
3485 * btrfs_clone() - clone a range from inode file to another
3486 *
3487 * @src: Inode to clone from
3488 * @inode: Inode to clone to
3489 * @off: Offset within source to start clone from
3490 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003491 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003492 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003493 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003494 */
3495static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003496 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003497 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003498{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003499 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003500 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003501 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003502 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003503 struct btrfs_trans_handle *trans;
3504 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003505 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003506 u32 nritems;
3507 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003508 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003509 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003510 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003511
3512 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003513 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3514 if (!buf)
3515 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003516
3517 path = btrfs_alloc_path();
3518 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003519 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003520 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003521 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003522
David Sterbae4058b52015-11-27 16:31:35 +01003523 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003524 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003525 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003526 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003527 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003528
3529 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003530 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003531
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003532 /*
3533 * note the key will change type as we walk through the
3534 * tree.
3535 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003536 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003537 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3538 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003539 if (ret < 0)
3540 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003541 /*
3542 * First search, if no extent item that starts at offset off was
3543 * found but the previous item is an extent item, it's possible
3544 * it might overlap our target range, therefore process it.
3545 */
3546 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3547 btrfs_item_key_to_cpu(path->nodes[0], &key,
3548 path->slots[0] - 1);
3549 if (key.type == BTRFS_EXTENT_DATA_KEY)
3550 path->slots[0]--;
3551 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003552
Yan Zhengae01a0a2008-08-04 23:23:47 -04003553 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003554process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003555 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003556 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003557 if (ret < 0)
3558 goto out;
3559 if (ret > 0)
3560 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003561 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003562 }
3563 leaf = path->nodes[0];
3564 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003565
Yan Zhengae01a0a2008-08-04 23:23:47 -04003566 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003567 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003568 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003569 break;
3570
David Sterba962a2982014-06-04 18:41:45 +02003571 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003572 struct btrfs_file_extent_item *extent;
3573 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003574 u32 size;
3575 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003576 u64 disko = 0, diskl = 0;
3577 u64 datao = 0, datal = 0;
3578 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003579 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003580
Sage Weilc5c9cd42008-11-12 14:32:25 -05003581 extent = btrfs_item_ptr(leaf, slot,
3582 struct btrfs_file_extent_item);
3583 comp = btrfs_file_extent_compression(leaf, extent);
3584 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003585 if (type == BTRFS_FILE_EXTENT_REG ||
3586 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003587 disko = btrfs_file_extent_disk_bytenr(leaf,
3588 extent);
3589 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3590 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003591 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003592 datal = btrfs_file_extent_num_bytes(leaf,
3593 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003594 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3595 /* take upper bound, may be compressed */
3596 datal = btrfs_file_extent_ram_bytes(leaf,
3597 extent);
3598 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003599
Filipe Manana2c463822014-05-31 02:31:05 +01003600 /*
3601 * The first search might have left us at an extent
3602 * item that ends before our target range's start, can
3603 * happen if we have holes and NO_HOLES feature enabled.
3604 */
3605 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003606 path->slots[0]++;
3607 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003608 } else if (key.offset >= off + len) {
3609 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003610 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003611 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003612 size = btrfs_item_size_nr(leaf, slot);
3613 read_extent_buffer(leaf, buf,
3614 btrfs_item_ptr_offset(leaf, slot),
3615 size);
3616
3617 btrfs_release_path(path);
3618 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003619
Zheng Yan31840ae2008-09-23 13:14:14 -04003620 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003621 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003622 if (off <= key.offset)
3623 new_key.offset = key.offset + destoff - off;
3624 else
3625 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003626
Sage Weilb6f34092011-09-20 14:48:51 -04003627 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003628 * Deal with a hole that doesn't have an extent item
3629 * that represents it (NO_HOLES feature enabled).
3630 * This hole is either in the middle of the cloning
3631 * range or at the beginning (fully overlaps it or
3632 * partially overlaps it).
3633 */
3634 if (new_key.offset != last_dest_end)
3635 drop_start = last_dest_end;
3636 else
3637 drop_start = new_key.offset;
3638
3639 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003640 * 1 - adjusting old extent (we may have to split it)
3641 * 1 - add new extent
3642 * 1 - inode update
3643 */
3644 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003645 if (IS_ERR(trans)) {
3646 ret = PTR_ERR(trans);
3647 goto out;
3648 }
3649
Chris Masonc8a894d2009-06-27 21:07:03 -04003650 if (type == BTRFS_FILE_EXTENT_REG ||
3651 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003652 /*
3653 * a | --- range to clone ---| b
3654 * | ------------- extent ------------- |
3655 */
3656
Antonio Ospite93915582014-06-04 14:03:48 +02003657 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003658 if (key.offset + datal > off + len)
3659 datal = off + len - key.offset;
3660
Antonio Ospite93915582014-06-04 14:03:48 +02003661 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003662 if (off > key.offset) {
3663 datao += off - key.offset;
3664 datal -= off - key.offset;
3665 }
3666
Josef Bacik5dc562c2012-08-17 13:14:17 -04003667 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003668 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003669 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003670 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003671 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003672 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003673 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003674 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003675 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003676 goto out;
3677 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678
Sage Weilc5c9cd42008-11-12 14:32:25 -05003679 ret = btrfs_insert_empty_item(trans, root, path,
3680 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003681 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003682 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003683 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003684 goto out;
3685 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003686
3687 leaf = path->nodes[0];
3688 slot = path->slots[0];
3689 write_extent_buffer(leaf, buf,
3690 btrfs_item_ptr_offset(leaf, slot),
3691 size);
3692
3693 extent = btrfs_item_ptr(leaf, slot,
3694 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003695
Sage Weilc5c9cd42008-11-12 14:32:25 -05003696 /* disko == 0 means it's a hole */
3697 if (!disko)
3698 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003699
3700 btrfs_set_file_extent_offset(leaf, extent,
3701 datao);
3702 btrfs_set_file_extent_num_bytes(leaf, extent,
3703 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003704
Sage Weilc5c9cd42008-11-12 14:32:25 -05003705 if (disko) {
3706 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003707 ret = btrfs_inc_extent_ref(trans,
3708 fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003709 disko, diskl, 0,
3710 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003711 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003712 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003713 if (ret) {
3714 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003715 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003716 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003717 goto out;
3718
3719 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003720 }
3721 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3722 u64 skip = 0;
3723 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003724
Sage Weilc5c9cd42008-11-12 14:32:25 -05003725 if (off > key.offset) {
3726 skip = off - key.offset;
3727 new_key.offset += skip;
3728 }
Chris Masond3977122009-01-05 21:25:51 -05003729
Liu Boaa42ffd2012-09-18 03:52:23 -06003730 if (key.offset + datal > off + len)
3731 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003732
Sage Weilc5c9cd42008-11-12 14:32:25 -05003733 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003734 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003735 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003736 goto out;
3737 }
3738 size -= skip + trim;
3739 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003740
David Sterba4a0ab9d2017-02-10 20:18:49 +01003741 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003742 trans, path,
3743 &new_key,
3744 drop_start,
3745 datal,
3746 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003747 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003748 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003749 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003750 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003751 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003752 goto out;
3753 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003754 leaf = path->nodes[0];
3755 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003756 }
3757
Filipe Manana7ffbb592014-06-09 03:48:05 +01003758 /* If we have an implicit hole (NO_HOLES feature). */
3759 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003760 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003761 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003762 new_key.offset - drop_start);
3763
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003764 clone_update_extent_map(BTRFS_I(inode), trans,
3765 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003766
Sage Weilc5c9cd42008-11-12 14:32:25 -05003767 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003768 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003769
Filipe Manana62e23902014-08-08 02:47:06 +01003770 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003771 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003772 ret = clone_finish_inode_update(trans, inode,
3773 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003774 destoff, olen,
3775 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003776 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003777 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003778 if (new_key.offset + datal >= destoff + len)
3779 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003780 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003781 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003782 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003783
3784 if (fatal_signal_pending(current)) {
3785 ret = -EINTR;
3786 goto out;
3787 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003788 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003789 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003790
Filipe Mananaf82a9902014-06-01 01:50:28 +01003791 if (last_dest_end < destoff + len) {
3792 /*
3793 * We have an implicit hole (NO_HOLES feature is enabled) that
3794 * fully or partially overlaps our cloning range at its end.
3795 */
3796 btrfs_release_path(path);
3797
3798 /*
3799 * 1 - remove extent(s)
3800 * 1 - inode update
3801 */
3802 trans = btrfs_start_transaction(root, 2);
3803 if (IS_ERR(trans)) {
3804 ret = PTR_ERR(trans);
3805 goto out;
3806 }
3807 ret = btrfs_drop_extents(trans, root, inode,
3808 last_dest_end, destoff + len, 1);
3809 if (ret) {
3810 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003811 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003812 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003813 goto out;
3814 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003815 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3816 last_dest_end,
3817 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003818 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003819 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003820 }
3821
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003822out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003823 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003824 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003825 return ret;
3826}
3827
Zach Brown3db11b22015-11-10 16:53:32 -05003828static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3829 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003830{
Al Viro54563d42013-09-01 15:57:51 -04003831 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003832 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003833 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003834 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003835 int ret;
3836 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003837 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003838 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003839
3840 /*
3841 * TODO:
3842 * - split compressed inline extents. annoying: we need to
3843 * decompress into destination's address_space (the file offset
3844 * may change, so source mapping won't do), then recompress (or
3845 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003846 *
3847 * - split destination inode's inline extents. The inline extents can
3848 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003849 */
3850
Mark Fasheh32b7c682013-08-06 11:42:49 -07003851 if (btrfs_root_readonly(root))
3852 return -EROFS;
3853
Zach Brown3db11b22015-11-10 16:53:32 -05003854 if (file_src->f_path.mnt != file->f_path.mnt ||
3855 src->i_sb != inode->i_sb)
3856 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003857
3858 /* don't make the dst file partly checksummed */
3859 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3860 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003861 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003862
Mark Fasheh32b7c682013-08-06 11:42:49 -07003863 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003864 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003865
3866 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003867 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003868 } else {
Al Viro59551022016-01-22 15:40:57 -05003869 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003870 }
3871
3872 /* determine range to clone */
3873 ret = -EINVAL;
3874 if (off + len > src->i_size || off + len < off)
3875 goto out_unlock;
3876 if (len == 0)
3877 olen = len = src->i_size - off;
3878 /* if we extend to eof, continue to block boundary */
3879 if (off + len == src->i_size)
3880 len = ALIGN(src->i_size, bs) - off;
3881
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003882 if (len == 0) {
3883 ret = 0;
3884 goto out_unlock;
3885 }
3886
Mark Fasheh32b7c682013-08-06 11:42:49 -07003887 /* verify the end result is block aligned */
3888 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3889 !IS_ALIGNED(destoff, bs))
3890 goto out_unlock;
3891
3892 /* verify if ranges are overlapped within the same file */
3893 if (same_inode) {
3894 if (destoff + len > off && destoff < off + len)
3895 goto out_unlock;
3896 }
3897
3898 if (destoff > inode->i_size) {
3899 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3900 if (ret)
3901 goto out_unlock;
3902 }
3903
Filipe Mananac125b8b2014-05-23 05:03:34 +01003904 /*
3905 * Lock the target range too. Right after we replace the file extent
3906 * items in the fs tree (which now point to the cloned data), we might
3907 * have a worker replace them with extent items relative to a write
3908 * operation that was issued before this clone operation (i.e. confront
3909 * with inode.c:btrfs_finish_ordered_io).
3910 */
3911 if (same_inode) {
3912 u64 lock_start = min_t(u64, off, destoff);
3913 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003914
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003915 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003916 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003917 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3918 true);
3919 }
3920 ASSERT(ret == 0);
3921 if (WARN_ON(ret)) {
3922 /* ranges in the io trees already unlocked */
3923 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003924 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003925
Mark Fasheh1c919a52015-06-30 14:42:08 -07003926 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003927
Filipe Mananac125b8b2014-05-23 05:03:34 +01003928 if (same_inode) {
3929 u64 lock_start = min_t(u64, off, destoff);
3930 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3931
3932 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3933 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003934 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003935 }
3936 /*
3937 * Truncate page cache pages so that future reads will see the cloned
3938 * data immediately and not the previous data.
3939 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303940 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003941 round_down(destoff, PAGE_SIZE),
3942 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003943out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003944 if (!same_inode)
3945 btrfs_double_inode_unlock(src, inode);
3946 else
Al Viro59551022016-01-22 15:40:57 -05003947 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003948 return ret;
3949}
3950
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003951int btrfs_clone_file_range(struct file *src_file, loff_t off,
3952 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003953{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003954 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003955}
3956
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003957/*
3958 * there are many ways the trans_start and trans_end ioctls can lead
3959 * to deadlocks. They should only be used by applications that
3960 * basically own the machine, and have a very in depth understanding
3961 * of all the possible deadlocks and enospc problems.
3962 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003963static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003964{
Al Viro496ad9a2013-01-23 17:07:38 -05003965 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003966 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003967 struct btrfs_root *root = BTRFS_I(inode)->root;
3968 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003969 int ret;
Nikolay Borisov3558d4f2017-07-26 11:26:28 +03003970 static bool warned = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003971
Sage Weil1ab86ae2009-09-29 18:38:44 -04003972 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003973 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003974 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003975
Nikolay Borisov3558d4f2017-07-26 11:26:28 +03003976 if (!warned) {
3977 btrfs_warn(fs_info,
3978 "Userspace transaction mechanism is considered "
3979 "deprecated and slated to be removed in 4.17. "
3980 "If you have a valid use case please "
3981 "speak up on the mailing list");
3982 WARN_ON(1);
3983 warned = true;
3984 }
3985
Sage Weil1ab86ae2009-09-29 18:38:44 -04003986 ret = -EINPROGRESS;
3987 if (file->private_data)
3988 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003989
Li Zefanb83cc962010-12-20 16:04:08 +08003990 ret = -EROFS;
3991 if (btrfs_root_readonly(root))
3992 goto out;
3993
Al Viroa561be72011-11-23 11:57:51 -05003994 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003995 if (ret)
3996 goto out;
3997
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003998 atomic_inc(&fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003999
Sage Weil1ab86ae2009-09-29 18:38:44 -04004000 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004001 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00004002 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04004003 goto out_drop;
4004
4005 file->private_data = trans;
4006 return 0;
4007
4008out_drop:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004009 atomic_dec(&fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05004010 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004011out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004012 return ret;
4013}
4014
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004015static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4016{
Al Viro496ad9a2013-01-23 17:07:38 -05004017 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004018 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004019 struct btrfs_root *root = BTRFS_I(inode)->root;
4020 struct btrfs_root *new_root;
4021 struct btrfs_dir_item *di;
4022 struct btrfs_trans_handle *trans;
4023 struct btrfs_path *path;
4024 struct btrfs_key location;
4025 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004026 u64 objectid = 0;
4027 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004028 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004029
4030 if (!capable(CAP_SYS_ADMIN))
4031 return -EPERM;
4032
Miao Xie3c04ce02012-11-26 08:43:07 +00004033 ret = mnt_want_write_file(file);
4034 if (ret)
4035 return ret;
4036
4037 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4038 ret = -EFAULT;
4039 goto out;
4040 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004041
4042 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304043 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004044
4045 location.objectid = objectid;
4046 location.type = BTRFS_ROOT_ITEM_KEY;
4047 location.offset = (u64)-1;
4048
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004049 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004050 if (IS_ERR(new_root)) {
4051 ret = PTR_ERR(new_root);
4052 goto out;
4053 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004054
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004055 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004056 if (!path) {
4057 ret = -ENOMEM;
4058 goto out;
4059 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004060 path->leave_spinning = 1;
4061
4062 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004063 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004064 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004065 ret = PTR_ERR(trans);
4066 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004067 }
4068
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004069 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4070 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004071 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004072 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004073 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004074 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004075 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004076 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004077 ret = -ENOENT;
4078 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004079 }
4080
4081 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4082 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4083 btrfs_mark_buffer_dirty(path->nodes[0]);
4084 btrfs_free_path(path);
4085
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004086 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004087 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004088out:
4089 mnt_drop_write_file(file);
4090 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004091}
4092
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004093void btrfs_get_block_group_info(struct list_head *groups_list,
4094 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004095{
4096 struct btrfs_block_group_cache *block_group;
4097
4098 space->total_bytes = 0;
4099 space->used_bytes = 0;
4100 space->flags = 0;
4101 list_for_each_entry(block_group, groups_list, list) {
4102 space->flags = block_group->flags;
4103 space->total_bytes += block_group->key.offset;
4104 space->used_bytes +=
4105 btrfs_block_group_used(&block_group->item);
4106 }
4107}
4108
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004109static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4110 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004111{
4112 struct btrfs_ioctl_space_args space_args;
4113 struct btrfs_ioctl_space_info space;
4114 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004115 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004116 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004117 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004118 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4119 BTRFS_BLOCK_GROUP_SYSTEM,
4120 BTRFS_BLOCK_GROUP_METADATA,
4121 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4122 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004123 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004124 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004125 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004126 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004127
4128 if (copy_from_user(&space_args,
4129 (struct btrfs_ioctl_space_args __user *)arg,
4130 sizeof(space_args)))
4131 return -EFAULT;
4132
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004133 for (i = 0; i < num_types; i++) {
4134 struct btrfs_space_info *tmp;
4135
4136 info = NULL;
4137 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004138 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004139 list) {
4140 if (tmp->flags == types[i]) {
4141 info = tmp;
4142 break;
4143 }
4144 }
4145 rcu_read_unlock();
4146
4147 if (!info)
4148 continue;
4149
4150 down_read(&info->groups_sem);
4151 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4152 if (!list_empty(&info->block_groups[c]))
4153 slot_count++;
4154 }
4155 up_read(&info->groups_sem);
4156 }
Josef Bacik1406e432010-01-13 18:19:06 +00004157
David Sterba36523e952014-02-07 14:34:12 +01004158 /*
4159 * Global block reserve, exported as a space_info
4160 */
4161 slot_count++;
4162
Chris Mason7fde62b2010-03-16 15:40:10 -04004163 /* space_slots == 0 means they are asking for a count */
4164 if (space_args.space_slots == 0) {
4165 space_args.total_spaces = slot_count;
4166 goto out;
4167 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004168
Dan Rosenberg51788b12011-02-14 16:04:23 -05004169 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004170
Chris Mason7fde62b2010-03-16 15:40:10 -04004171 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004172
Chris Mason7fde62b2010-03-16 15:40:10 -04004173 /* we generally have at most 6 or so space infos, one for each raid
4174 * level. So, a whole page should be more than enough for everyone
4175 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004176 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004177 return -ENOMEM;
4178
4179 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004180 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004181 if (!dest)
4182 return -ENOMEM;
4183 dest_orig = dest;
4184
4185 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004186 for (i = 0; i < num_types; i++) {
4187 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004188
Dan Rosenberg51788b12011-02-14 16:04:23 -05004189 if (!slot_count)
4190 break;
4191
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004192 info = NULL;
4193 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004194 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004195 list) {
4196 if (tmp->flags == types[i]) {
4197 info = tmp;
4198 break;
4199 }
4200 }
4201 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004202
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004203 if (!info)
4204 continue;
4205 down_read(&info->groups_sem);
4206 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4207 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004208 btrfs_get_block_group_info(
4209 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004210 memcpy(dest, &space, sizeof(space));
4211 dest++;
4212 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004213 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004214 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004215 if (!slot_count)
4216 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004217 }
4218 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004219 }
Josef Bacik1406e432010-01-13 18:19:06 +00004220
David Sterba36523e952014-02-07 14:34:12 +01004221 /*
4222 * Add global block reserve
4223 */
4224 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004225 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004226
4227 spin_lock(&block_rsv->lock);
4228 space.total_bytes = block_rsv->size;
4229 space.used_bytes = block_rsv->size - block_rsv->reserved;
4230 spin_unlock(&block_rsv->lock);
4231 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4232 memcpy(dest, &space, sizeof(space));
4233 space_args.total_spaces++;
4234 }
4235
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004236 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004237 (arg + sizeof(struct btrfs_ioctl_space_args));
4238
4239 if (copy_to_user(user_dest, dest_orig, alloc_size))
4240 ret = -EFAULT;
4241
4242 kfree(dest_orig);
4243out:
4244 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004245 ret = -EFAULT;
4246
4247 return ret;
4248}
4249
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004250/*
4251 * there are many ways the trans_start and trans_end ioctls can lead
4252 * to deadlocks. They should only be used by applications that
4253 * basically own the machine, and have a very in depth understanding
4254 * of all the possible deadlocks and enospc problems.
4255 */
4256long btrfs_ioctl_trans_end(struct file *file)
4257{
Al Viro496ad9a2013-01-23 17:07:38 -05004258 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004259 struct btrfs_root *root = BTRFS_I(inode)->root;
4260 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004261
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004262 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004263 if (!trans)
4264 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004265 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004266
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004267 btrfs_end_transaction(trans);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004268
Josef Bacika4abeea2011-04-11 17:25:13 -04004269 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004270
Al Viro2a79f172011-12-09 08:06:57 -05004271 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004272 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004273}
4274
Miao Xie9a8c28b2012-11-26 08:40:43 +00004275static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4276 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004277{
Sage Weil46204592010-10-29 15:41:32 -04004278 struct btrfs_trans_handle *trans;
4279 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004280 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004281
Miao Xied4edf392013-02-20 09:17:06 +00004282 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004283 if (IS_ERR(trans)) {
4284 if (PTR_ERR(trans) != -ENOENT)
4285 return PTR_ERR(trans);
4286
4287 /* No running transaction, don't bother */
4288 transid = root->fs_info->last_trans_committed;
4289 goto out;
4290 }
Sage Weil46204592010-10-29 15:41:32 -04004291 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004292 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004293 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004294 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004295 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004296 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004297out:
Sage Weil46204592010-10-29 15:41:32 -04004298 if (argp)
4299 if (copy_to_user(argp, &transid, sizeof(transid)))
4300 return -EFAULT;
4301 return 0;
4302}
4303
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004304static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004305 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004306{
Sage Weil46204592010-10-29 15:41:32 -04004307 u64 transid;
4308
4309 if (argp) {
4310 if (copy_from_user(&transid, argp, sizeof(transid)))
4311 return -EFAULT;
4312 } else {
4313 transid = 0; /* current trans */
4314 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004315 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004316}
4317
Miao Xieb8e95482012-11-26 08:48:01 +00004318static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004320 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004321 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004322 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004323
4324 if (!capable(CAP_SYS_ADMIN))
4325 return -EPERM;
4326
4327 sa = memdup_user(arg, sizeof(*sa));
4328 if (IS_ERR(sa))
4329 return PTR_ERR(sa);
4330
Miao Xieb8e95482012-11-26 08:48:01 +00004331 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4332 ret = mnt_want_write_file(file);
4333 if (ret)
4334 goto out;
4335 }
4336
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004337 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004338 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4339 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004340
4341 if (copy_to_user(arg, sa, sizeof(*sa)))
4342 ret = -EFAULT;
4343
Miao Xieb8e95482012-11-26 08:48:01 +00004344 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4345 mnt_drop_write_file(file);
4346out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004347 kfree(sa);
4348 return ret;
4349}
4350
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004351static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004352{
4353 if (!capable(CAP_SYS_ADMIN))
4354 return -EPERM;
4355
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004356 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004357}
4358
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004359static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004360 void __user *arg)
4361{
4362 struct btrfs_ioctl_scrub_args *sa;
4363 int ret;
4364
4365 if (!capable(CAP_SYS_ADMIN))
4366 return -EPERM;
4367
4368 sa = memdup_user(arg, sizeof(*sa));
4369 if (IS_ERR(sa))
4370 return PTR_ERR(sa);
4371
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004372 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004373
4374 if (copy_to_user(arg, sa, sizeof(*sa)))
4375 ret = -EFAULT;
4376
4377 kfree(sa);
4378 return ret;
4379}
4380
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004381static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004382 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004383{
4384 struct btrfs_ioctl_get_dev_stats *sa;
4385 int ret;
4386
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004387 sa = memdup_user(arg, sizeof(*sa));
4388 if (IS_ERR(sa))
4389 return PTR_ERR(sa);
4390
David Sterbab27f7c02012-06-22 06:30:39 -06004391 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4392 kfree(sa);
4393 return -EPERM;
4394 }
4395
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004396 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004397
4398 if (copy_to_user(arg, sa, sizeof(*sa)))
4399 ret = -EFAULT;
4400
4401 kfree(sa);
4402 return ret;
4403}
4404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004405static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4406 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004407{
4408 struct btrfs_ioctl_dev_replace_args *p;
4409 int ret;
4410
4411 if (!capable(CAP_SYS_ADMIN))
4412 return -EPERM;
4413
4414 p = memdup_user(arg, sizeof(*p));
4415 if (IS_ERR(p))
4416 return PTR_ERR(p);
4417
4418 switch (p->cmd) {
4419 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 if (fs_info->sb->s_flags & MS_RDONLY) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004421 ret = -EROFS;
4422 goto out;
4423 }
David Sterba171938e2017-03-28 14:44:21 +02004424 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004425 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004426 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004427 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004428 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004429 }
4430 break;
4431 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004432 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004433 ret = 0;
4434 break;
4435 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004436 ret = btrfs_dev_replace_cancel(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004437 break;
4438 default:
4439 ret = -EINVAL;
4440 break;
4441 }
4442
4443 if (copy_to_user(arg, p, sizeof(*p)))
4444 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004445out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004446 kfree(p);
4447 return ret;
4448}
4449
Jan Schmidtd7728c92011-07-07 16:48:38 +02004450static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4451{
4452 int ret = 0;
4453 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004454 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004455 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004456 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004457 struct inode_fs_paths *ipath = NULL;
4458 struct btrfs_path *path;
4459
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004460 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004461 return -EPERM;
4462
4463 path = btrfs_alloc_path();
4464 if (!path) {
4465 ret = -ENOMEM;
4466 goto out;
4467 }
4468
4469 ipa = memdup_user(arg, sizeof(*ipa));
4470 if (IS_ERR(ipa)) {
4471 ret = PTR_ERR(ipa);
4472 ipa = NULL;
4473 goto out;
4474 }
4475
4476 size = min_t(u32, ipa->size, 4096);
4477 ipath = init_ipath(size, root, path);
4478 if (IS_ERR(ipath)) {
4479 ret = PTR_ERR(ipath);
4480 ipath = NULL;
4481 goto out;
4482 }
4483
4484 ret = paths_from_inode(ipa->inum, ipath);
4485 if (ret < 0)
4486 goto out;
4487
4488 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004489 rel_ptr = ipath->fspath->val[i] -
4490 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004491 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004492 }
4493
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004494 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4495 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004496 if (ret) {
4497 ret = -EFAULT;
4498 goto out;
4499 }
4500
4501out:
4502 btrfs_free_path(path);
4503 free_ipath(ipath);
4504 kfree(ipa);
4505
4506 return ret;
4507}
4508
4509static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4510{
4511 struct btrfs_data_container *inodes = ctx;
4512 const size_t c = 3 * sizeof(u64);
4513
4514 if (inodes->bytes_left >= c) {
4515 inodes->bytes_left -= c;
4516 inodes->val[inodes->elem_cnt] = inum;
4517 inodes->val[inodes->elem_cnt + 1] = offset;
4518 inodes->val[inodes->elem_cnt + 2] = root;
4519 inodes->elem_cnt += 3;
4520 } else {
4521 inodes->bytes_missing += c - inodes->bytes_left;
4522 inodes->bytes_left = 0;
4523 inodes->elem_missed += 3;
4524 }
4525
4526 return 0;
4527}
4528
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004529static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Jan Schmidtd7728c92011-07-07 16:48:38 +02004530 void __user *arg)
4531{
4532 int ret = 0;
4533 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004534 struct btrfs_ioctl_logical_ino_args *loi;
4535 struct btrfs_data_container *inodes = NULL;
4536 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004537
4538 if (!capable(CAP_SYS_ADMIN))
4539 return -EPERM;
4540
4541 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304542 if (IS_ERR(loi))
4543 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004544
4545 path = btrfs_alloc_path();
4546 if (!path) {
4547 ret = -ENOMEM;
4548 goto out;
4549 }
4550
Byongho Leeee221842015-12-15 01:42:10 +09004551 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004552 inodes = init_data_container(size);
4553 if (IS_ERR(inodes)) {
4554 ret = PTR_ERR(inodes);
4555 inodes = NULL;
4556 goto out;
4557 }
4558
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004559 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Liu Bodf031f02012-09-07 20:01:29 -06004560 build_ino_list, inodes);
4561 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004562 ret = -ENOENT;
4563 if (ret < 0)
4564 goto out;
4565
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004566 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4567 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004568 if (ret)
4569 ret = -EFAULT;
4570
4571out:
4572 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004573 kvfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004574 kfree(loi);
4575
4576 return ret;
4577}
4578
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004579void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004580 struct btrfs_ioctl_balance_args *bargs)
4581{
4582 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4583
4584 bargs->flags = bctl->flags;
4585
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004586 if (atomic_read(&fs_info->balance_running))
4587 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4588 if (atomic_read(&fs_info->balance_pause_req))
4589 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004590 if (atomic_read(&fs_info->balance_cancel_req))
4591 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004592
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004593 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4594 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4595 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004596
4597 if (lock) {
4598 spin_lock(&fs_info->balance_lock);
4599 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4600 spin_unlock(&fs_info->balance_lock);
4601 } else {
4602 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4603 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004604}
4605
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004606static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004607{
Al Viro496ad9a2013-01-23 17:07:38 -05004608 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004609 struct btrfs_fs_info *fs_info = root->fs_info;
4610 struct btrfs_ioctl_balance_args *bargs;
4611 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004612 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004613 int ret;
4614
4615 if (!capable(CAP_SYS_ADMIN))
4616 return -EPERM;
4617
Liu Boe54bfa312012-06-29 03:58:48 -06004618 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004619 if (ret)
4620 return ret;
4621
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004622again:
David Sterba171938e2017-03-28 14:44:21 +02004623 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004624 mutex_lock(&fs_info->volume_mutex);
4625 mutex_lock(&fs_info->balance_mutex);
4626 need_unlock = true;
4627 goto locked;
4628 }
4629
4630 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004631 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004632 * (1) some other op is running
4633 * (2) balance is running
4634 * (3) balance is paused -- special case (think resume)
4635 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004636 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004637 if (fs_info->balance_ctl) {
4638 /* this is either (2) or (3) */
4639 if (!atomic_read(&fs_info->balance_running)) {
4640 mutex_unlock(&fs_info->balance_mutex);
4641 if (!mutex_trylock(&fs_info->volume_mutex))
4642 goto again;
4643 mutex_lock(&fs_info->balance_mutex);
4644
4645 if (fs_info->balance_ctl &&
4646 !atomic_read(&fs_info->balance_running)) {
4647 /* this is (3) */
4648 need_unlock = false;
4649 goto locked;
4650 }
4651
4652 mutex_unlock(&fs_info->balance_mutex);
4653 mutex_unlock(&fs_info->volume_mutex);
4654 goto again;
4655 } else {
4656 /* this is (2) */
4657 mutex_unlock(&fs_info->balance_mutex);
4658 ret = -EINPROGRESS;
4659 goto out;
4660 }
4661 } else {
4662 /* this is (1) */
4663 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004664 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004665 goto out;
4666 }
4667
4668locked:
David Sterba171938e2017-03-28 14:44:21 +02004669 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004670
4671 if (arg) {
4672 bargs = memdup_user(arg, sizeof(*bargs));
4673 if (IS_ERR(bargs)) {
4674 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004675 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004676 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004677
4678 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4679 if (!fs_info->balance_ctl) {
4680 ret = -ENOTCONN;
4681 goto out_bargs;
4682 }
4683
4684 bctl = fs_info->balance_ctl;
4685 spin_lock(&fs_info->balance_lock);
4686 bctl->flags |= BTRFS_BALANCE_RESUME;
4687 spin_unlock(&fs_info->balance_lock);
4688
4689 goto do_balance;
4690 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004691 } else {
4692 bargs = NULL;
4693 }
4694
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004695 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004696 ret = -EINPROGRESS;
4697 goto out_bargs;
4698 }
4699
David Sterba8d2db782015-11-04 15:38:29 +01004700 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004701 if (!bctl) {
4702 ret = -ENOMEM;
4703 goto out_bargs;
4704 }
4705
4706 bctl->fs_info = fs_info;
4707 if (arg) {
4708 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4709 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4710 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4711
4712 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004713 } else {
4714 /* balance everything - no filters */
4715 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004716 }
4717
David Sterba8eb93452015-10-12 16:55:54 +02004718 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4719 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004720 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004721 }
4722
Ilya Dryomovde322262012-01-16 22:04:49 +02004723do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004724 /*
David Sterba171938e2017-03-28 14:44:21 +02004725 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004726 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4727 * or, if restriper was paused all the way until unmount, in
David Sterba171938e2017-03-28 14:44:21 +02004728 * free_fs_info. The flag is cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004729 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004730 need_unlock = false;
4731
4732 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004733 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004734
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004735 if (arg) {
4736 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4737 ret = -EFAULT;
4738 }
4739
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004740out_bctl:
4741 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004742out_bargs:
4743 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004744out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004745 mutex_unlock(&fs_info->balance_mutex);
4746 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004747 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004748 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004749out:
Liu Boe54bfa312012-06-29 03:58:48 -06004750 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004751 return ret;
4752}
4753
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004754static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004755{
4756 if (!capable(CAP_SYS_ADMIN))
4757 return -EPERM;
4758
4759 switch (cmd) {
4760 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004761 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004762 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004763 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004764 }
4765
4766 return -EINVAL;
4767}
4768
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004769static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004770 void __user *arg)
4771{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004772 struct btrfs_ioctl_balance_args *bargs;
4773 int ret = 0;
4774
4775 if (!capable(CAP_SYS_ADMIN))
4776 return -EPERM;
4777
4778 mutex_lock(&fs_info->balance_mutex);
4779 if (!fs_info->balance_ctl) {
4780 ret = -ENOTCONN;
4781 goto out;
4782 }
4783
David Sterba8d2db782015-11-04 15:38:29 +01004784 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004785 if (!bargs) {
4786 ret = -ENOMEM;
4787 goto out;
4788 }
4789
4790 update_ioctl_balance_args(fs_info, 1, bargs);
4791
4792 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4793 ret = -EFAULT;
4794
4795 kfree(bargs);
4796out:
4797 mutex_unlock(&fs_info->balance_mutex);
4798 return ret;
4799}
4800
Miao Xie905b0dd2012-11-26 08:50:11 +00004801static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004802{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004803 struct inode *inode = file_inode(file);
4804 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004805 struct btrfs_ioctl_quota_ctl_args *sa;
4806 struct btrfs_trans_handle *trans = NULL;
4807 int ret;
4808 int err;
4809
4810 if (!capable(CAP_SYS_ADMIN))
4811 return -EPERM;
4812
Miao Xie905b0dd2012-11-26 08:50:11 +00004813 ret = mnt_want_write_file(file);
4814 if (ret)
4815 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004816
4817 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004818 if (IS_ERR(sa)) {
4819 ret = PTR_ERR(sa);
4820 goto drop_write;
4821 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004822
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004823 down_write(&fs_info->subvol_sem);
4824 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004825 if (IS_ERR(trans)) {
4826 ret = PTR_ERR(trans);
4827 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004828 }
4829
4830 switch (sa->cmd) {
4831 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004832 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004833 break;
4834 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004835 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004836 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004837 default:
4838 ret = -EINVAL;
4839 break;
4840 }
4841
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004842 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004843 if (err && !ret)
4844 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004845out:
4846 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004847 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004848drop_write:
4849 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004850 return ret;
4851}
4852
Miao Xie905b0dd2012-11-26 08:50:11 +00004853static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004854{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004855 struct inode *inode = file_inode(file);
4856 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4857 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004858 struct btrfs_ioctl_qgroup_assign_args *sa;
4859 struct btrfs_trans_handle *trans;
4860 int ret;
4861 int err;
4862
4863 if (!capable(CAP_SYS_ADMIN))
4864 return -EPERM;
4865
Miao Xie905b0dd2012-11-26 08:50:11 +00004866 ret = mnt_want_write_file(file);
4867 if (ret)
4868 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004869
4870 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004871 if (IS_ERR(sa)) {
4872 ret = PTR_ERR(sa);
4873 goto drop_write;
4874 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004875
4876 trans = btrfs_join_transaction(root);
4877 if (IS_ERR(trans)) {
4878 ret = PTR_ERR(trans);
4879 goto out;
4880 }
4881
Arne Jansen5d13a372011-09-14 15:53:51 +02004882 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004883 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004884 sa->src, sa->dst);
4885 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004886 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004887 sa->src, sa->dst);
4888 }
4889
Qu Wenruoe082f562015-02-27 16:24:28 +08004890 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004891 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004892 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004893 btrfs_handle_fs_error(fs_info, err,
4894 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004895 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004896 if (err && !ret)
4897 ret = err;
4898
4899out:
4900 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004901drop_write:
4902 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004903 return ret;
4904}
4905
Miao Xie905b0dd2012-11-26 08:50:11 +00004906static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004907{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004908 struct inode *inode = file_inode(file);
4909 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4910 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004911 struct btrfs_ioctl_qgroup_create_args *sa;
4912 struct btrfs_trans_handle *trans;
4913 int ret;
4914 int err;
4915
4916 if (!capable(CAP_SYS_ADMIN))
4917 return -EPERM;
4918
Miao Xie905b0dd2012-11-26 08:50:11 +00004919 ret = mnt_want_write_file(file);
4920 if (ret)
4921 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004922
4923 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004924 if (IS_ERR(sa)) {
4925 ret = PTR_ERR(sa);
4926 goto drop_write;
4927 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004928
Miao Xied86e56c2012-11-15 11:35:41 +00004929 if (!sa->qgroupid) {
4930 ret = -EINVAL;
4931 goto out;
4932 }
4933
Arne Jansen5d13a372011-09-14 15:53:51 +02004934 trans = btrfs_join_transaction(root);
4935 if (IS_ERR(trans)) {
4936 ret = PTR_ERR(trans);
4937 goto out;
4938 }
4939
Arne Jansen5d13a372011-09-14 15:53:51 +02004940 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004941 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004942 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004943 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004944 }
4945
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004946 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004947 if (err && !ret)
4948 ret = err;
4949
4950out:
4951 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004952drop_write:
4953 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004954 return ret;
4955}
4956
Miao Xie905b0dd2012-11-26 08:50:11 +00004957static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004958{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004959 struct inode *inode = file_inode(file);
4960 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4961 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004962 struct btrfs_ioctl_qgroup_limit_args *sa;
4963 struct btrfs_trans_handle *trans;
4964 int ret;
4965 int err;
4966 u64 qgroupid;
4967
4968 if (!capable(CAP_SYS_ADMIN))
4969 return -EPERM;
4970
Miao Xie905b0dd2012-11-26 08:50:11 +00004971 ret = mnt_want_write_file(file);
4972 if (ret)
4973 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004974
4975 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004976 if (IS_ERR(sa)) {
4977 ret = PTR_ERR(sa);
4978 goto drop_write;
4979 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004980
4981 trans = btrfs_join_transaction(root);
4982 if (IS_ERR(trans)) {
4983 ret = PTR_ERR(trans);
4984 goto out;
4985 }
4986
4987 qgroupid = sa->qgroupid;
4988 if (!qgroupid) {
4989 /* take the current subvol as qgroup */
4990 qgroupid = root->root_key.objectid;
4991 }
4992
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004993 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004994
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004995 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004996 if (err && !ret)
4997 ret = err;
4998
4999out:
5000 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005001drop_write:
5002 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005003 return ret;
5004}
5005
Jan Schmidt2f232032013-04-25 16:04:51 +00005006static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5007{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005008 struct inode *inode = file_inode(file);
5009 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005010 struct btrfs_ioctl_quota_rescan_args *qsa;
5011 int ret;
5012
5013 if (!capable(CAP_SYS_ADMIN))
5014 return -EPERM;
5015
5016 ret = mnt_want_write_file(file);
5017 if (ret)
5018 return ret;
5019
5020 qsa = memdup_user(arg, sizeof(*qsa));
5021 if (IS_ERR(qsa)) {
5022 ret = PTR_ERR(qsa);
5023 goto drop_write;
5024 }
5025
5026 if (qsa->flags) {
5027 ret = -EINVAL;
5028 goto out;
5029 }
5030
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005031 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005032
5033out:
5034 kfree(qsa);
5035drop_write:
5036 mnt_drop_write_file(file);
5037 return ret;
5038}
5039
5040static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5041{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005042 struct inode *inode = file_inode(file);
5043 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005044 struct btrfs_ioctl_quota_rescan_args *qsa;
5045 int ret = 0;
5046
5047 if (!capable(CAP_SYS_ADMIN))
5048 return -EPERM;
5049
David Sterba8d2db782015-11-04 15:38:29 +01005050 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005051 if (!qsa)
5052 return -ENOMEM;
5053
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005054 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005055 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005056 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005057 }
5058
5059 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5060 ret = -EFAULT;
5061
5062 kfree(qsa);
5063 return ret;
5064}
5065
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005066static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5067{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005068 struct inode *inode = file_inode(file);
5069 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005070
5071 if (!capable(CAP_SYS_ADMIN))
5072 return -EPERM;
5073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005074 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005075}
5076
Hugo Millsabccd002014-01-30 20:17:00 +00005077static long _btrfs_ioctl_set_received_subvol(struct file *file,
5078 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005079{
Al Viro496ad9a2013-01-23 17:07:38 -05005080 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005081 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005082 struct btrfs_root *root = BTRFS_I(inode)->root;
5083 struct btrfs_root_item *root_item = &root->root_item;
5084 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005085 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005086 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005087 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005088
David Sterbabd60ea02014-01-16 15:50:22 +01005089 if (!inode_owner_or_capable(inode))
5090 return -EPERM;
5091
Alexander Block8ea05e32012-07-25 17:35:53 +02005092 ret = mnt_want_write_file(file);
5093 if (ret < 0)
5094 return ret;
5095
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005096 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005097
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005098 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005099 ret = -EINVAL;
5100 goto out;
5101 }
5102
5103 if (btrfs_root_readonly(root)) {
5104 ret = -EROFS;
5105 goto out;
5106 }
5107
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005108 /*
5109 * 1 - root item
5110 * 2 - uuid items (received uuid + subvol uuid)
5111 */
5112 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005113 if (IS_ERR(trans)) {
5114 ret = PTR_ERR(trans);
5115 trans = NULL;
5116 goto out;
5117 }
5118
5119 sa->rtransid = trans->transid;
5120 sa->rtime.sec = ct.tv_sec;
5121 sa->rtime.nsec = ct.tv_nsec;
5122
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005123 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5124 BTRFS_UUID_SIZE);
5125 if (received_uuid_changed &&
5126 !btrfs_is_empty_uuid(root_item->received_uuid))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005127 btrfs_uuid_tree_rem(trans, fs_info, root_item->received_uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005128 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5129 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005130 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5131 btrfs_set_root_stransid(root_item, sa->stransid);
5132 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005133 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5134 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5135 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5136 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005137
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005138 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005139 &root->root_key, &root->root_item);
5140 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005141 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005142 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005143 }
5144 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005145 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005146 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5147 root->root_key.objectid);
5148 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005149 btrfs_abort_transaction(trans, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005150 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005151 }
5152 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005153 ret = btrfs_commit_transaction(trans);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005154 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005155 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005156 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005157 }
5158
Hugo Millsabccd002014-01-30 20:17:00 +00005159out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005160 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005161 mnt_drop_write_file(file);
5162 return ret;
5163}
5164
5165#ifdef CONFIG_64BIT
5166static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5167 void __user *arg)
5168{
5169 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5170 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5171 int ret = 0;
5172
5173 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305174 if (IS_ERR(args32))
5175 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005176
David Sterba8d2db782015-11-04 15:38:29 +01005177 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005178 if (!args64) {
5179 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005180 goto out;
5181 }
5182
5183 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5184 args64->stransid = args32->stransid;
5185 args64->rtransid = args32->rtransid;
5186 args64->stime.sec = args32->stime.sec;
5187 args64->stime.nsec = args32->stime.nsec;
5188 args64->rtime.sec = args32->rtime.sec;
5189 args64->rtime.nsec = args32->rtime.nsec;
5190 args64->flags = args32->flags;
5191
5192 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5193 if (ret)
5194 goto out;
5195
5196 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5197 args32->stransid = args64->stransid;
5198 args32->rtransid = args64->rtransid;
5199 args32->stime.sec = args64->stime.sec;
5200 args32->stime.nsec = args64->stime.nsec;
5201 args32->rtime.sec = args64->rtime.sec;
5202 args32->rtime.nsec = args64->rtime.nsec;
5203 args32->flags = args64->flags;
5204
5205 ret = copy_to_user(arg, args32, sizeof(*args32));
5206 if (ret)
5207 ret = -EFAULT;
5208
5209out:
5210 kfree(args32);
5211 kfree(args64);
5212 return ret;
5213}
5214#endif
5215
5216static long btrfs_ioctl_set_received_subvol(struct file *file,
5217 void __user *arg)
5218{
5219 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5220 int ret = 0;
5221
5222 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305223 if (IS_ERR(sa))
5224 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005225
5226 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5227
5228 if (ret)
5229 goto out;
5230
Alexander Block8ea05e32012-07-25 17:35:53 +02005231 ret = copy_to_user(arg, sa, sizeof(*sa));
5232 if (ret)
5233 ret = -EFAULT;
5234
5235out:
5236 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005237 return ret;
5238}
5239
jeff.liu867ab662013-01-05 02:48:01 +00005240static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5241{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005242 struct inode *inode = file_inode(file);
5243 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005244 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005245 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005246 char label[BTRFS_LABEL_SIZE];
5247
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005248 spin_lock(&fs_info->super_lock);
5249 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5250 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005251
5252 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005253
5254 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 btrfs_warn(fs_info,
5256 "label is too long, return the first %zu bytes",
5257 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005258 }
5259
jeff.liu867ab662013-01-05 02:48:01 +00005260 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005261
5262 return ret ? -EFAULT : 0;
5263}
5264
jeff.liua8bfd4a2013-01-05 02:48:08 +00005265static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5266{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005267 struct inode *inode = file_inode(file);
5268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5269 struct btrfs_root *root = BTRFS_I(inode)->root;
5270 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005271 struct btrfs_trans_handle *trans;
5272 char label[BTRFS_LABEL_SIZE];
5273 int ret;
5274
5275 if (!capable(CAP_SYS_ADMIN))
5276 return -EPERM;
5277
5278 if (copy_from_user(label, arg, sizeof(label)))
5279 return -EFAULT;
5280
5281 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005282 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005283 "unable to set label with more than %d bytes",
5284 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005285 return -EINVAL;
5286 }
5287
5288 ret = mnt_want_write_file(file);
5289 if (ret)
5290 return ret;
5291
jeff.liua8bfd4a2013-01-05 02:48:08 +00005292 trans = btrfs_start_transaction(root, 0);
5293 if (IS_ERR(trans)) {
5294 ret = PTR_ERR(trans);
5295 goto out_unlock;
5296 }
5297
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005298 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005299 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005300 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005301 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005302
5303out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005304 mnt_drop_write_file(file);
5305 return ret;
5306}
5307
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005308#define INIT_FEATURE_FLAGS(suffix) \
5309 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5310 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5311 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5312
David Sterbad5131b62016-02-17 15:26:27 +01005313int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005314{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005315 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005316 INIT_FEATURE_FLAGS(SUPP),
5317 INIT_FEATURE_FLAGS(SAFE_SET),
5318 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5319 };
5320
5321 if (copy_to_user(arg, &features, sizeof(features)))
5322 return -EFAULT;
5323
5324 return 0;
5325}
5326
5327static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005329 struct inode *inode = file_inode(file);
5330 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5331 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005332 struct btrfs_ioctl_feature_flags features;
5333
5334 features.compat_flags = btrfs_super_compat_flags(super_block);
5335 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5336 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5337
5338 if (copy_to_user(arg, &features, sizeof(features)))
5339 return -EFAULT;
5340
5341 return 0;
5342}
5343
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005344static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005345 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005346 u64 change_mask, u64 flags, u64 supported_flags,
5347 u64 safe_set, u64 safe_clear)
5348{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005349 const char *type = btrfs_feature_set_names[set];
5350 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005351 u64 disallowed, unsupported;
5352 u64 set_mask = flags & change_mask;
5353 u64 clear_mask = ~flags & change_mask;
5354
5355 unsupported = set_mask & ~supported_flags;
5356 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005357 names = btrfs_printable_features(set, unsupported);
5358 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005359 btrfs_warn(fs_info,
5360 "this kernel does not support the %s feature bit%s",
5361 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005362 kfree(names);
5363 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005364 btrfs_warn(fs_info,
5365 "this kernel does not support %s bits 0x%llx",
5366 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005367 return -EOPNOTSUPP;
5368 }
5369
5370 disallowed = set_mask & ~safe_set;
5371 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005372 names = btrfs_printable_features(set, disallowed);
5373 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005374 btrfs_warn(fs_info,
5375 "can't set the %s feature bit%s while mounted",
5376 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005377 kfree(names);
5378 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005379 btrfs_warn(fs_info,
5380 "can't set %s bits 0x%llx while mounted",
5381 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005382 return -EPERM;
5383 }
5384
5385 disallowed = clear_mask & ~safe_clear;
5386 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005387 names = btrfs_printable_features(set, disallowed);
5388 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005389 btrfs_warn(fs_info,
5390 "can't clear the %s feature bit%s while mounted",
5391 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005392 kfree(names);
5393 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005394 btrfs_warn(fs_info,
5395 "can't clear %s bits 0x%llx while mounted",
5396 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005397 return -EPERM;
5398 }
5399
5400 return 0;
5401}
5402
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005403#define check_feature(fs_info, change_mask, flags, mask_base) \
5404check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005405 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5406 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5407 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5408
5409static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5410{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005411 struct inode *inode = file_inode(file);
5412 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5413 struct btrfs_root *root = BTRFS_I(inode)->root;
5414 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005415 struct btrfs_ioctl_feature_flags flags[2];
5416 struct btrfs_trans_handle *trans;
5417 u64 newflags;
5418 int ret;
5419
5420 if (!capable(CAP_SYS_ADMIN))
5421 return -EPERM;
5422
5423 if (copy_from_user(flags, arg, sizeof(flags)))
5424 return -EFAULT;
5425
5426 /* Nothing to do */
5427 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5428 !flags[0].incompat_flags)
5429 return 0;
5430
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005431 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005432 flags[1].compat_flags, COMPAT);
5433 if (ret)
5434 return ret;
5435
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005436 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005437 flags[1].compat_ro_flags, COMPAT_RO);
5438 if (ret)
5439 return ret;
5440
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005441 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005442 flags[1].incompat_flags, INCOMPAT);
5443 if (ret)
5444 return ret;
5445
David Sterba7ab19622016-05-04 11:32:00 +02005446 ret = mnt_want_write_file(file);
5447 if (ret)
5448 return ret;
5449
David Sterba8051aa12014-02-07 14:34:04 +01005450 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005451 if (IS_ERR(trans)) {
5452 ret = PTR_ERR(trans);
5453 goto out_drop_write;
5454 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005455
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005456 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005457 newflags = btrfs_super_compat_flags(super_block);
5458 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5459 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5460 btrfs_set_super_compat_flags(super_block, newflags);
5461
5462 newflags = btrfs_super_compat_ro_flags(super_block);
5463 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5464 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5465 btrfs_set_super_compat_ro_flags(super_block, newflags);
5466
5467 newflags = btrfs_super_incompat_flags(super_block);
5468 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5469 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5470 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005471 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005472
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005473 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005474out_drop_write:
5475 mnt_drop_write_file(file);
5476
5477 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005478}
5479
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005480long btrfs_ioctl(struct file *file, unsigned int
5481 cmd, unsigned long arg)
5482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005483 struct inode *inode = file_inode(file);
5484 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5485 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005486 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005487
5488 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005489 case FS_IOC_GETFLAGS:
5490 return btrfs_ioctl_getflags(file, argp);
5491 case FS_IOC_SETFLAGS:
5492 return btrfs_ioctl_setflags(file, argp);
5493 case FS_IOC_GETVERSION:
5494 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005495 case FITRIM:
5496 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005497 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005498 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005499 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005500 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005501 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005502 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005503 case BTRFS_IOC_SUBVOL_CREATE_V2:
5504 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005505 case BTRFS_IOC_SNAP_DESTROY:
5506 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005507 case BTRFS_IOC_SUBVOL_GETFLAGS:
5508 return btrfs_ioctl_subvol_getflags(file, argp);
5509 case BTRFS_IOC_SUBVOL_SETFLAGS:
5510 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005511 case BTRFS_IOC_DEFAULT_SUBVOL:
5512 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005513 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005514 return btrfs_ioctl_defrag(file, NULL);
5515 case BTRFS_IOC_DEFRAG_RANGE:
5516 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005517 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005518 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005519 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005520 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005521 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005522 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005523 case BTRFS_IOC_RM_DEV_V2:
5524 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005525 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005526 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005527 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005528 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005529 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005530 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005531 case BTRFS_IOC_TRANS_START:
5532 return btrfs_ioctl_trans_start(file);
5533 case BTRFS_IOC_TRANS_END:
5534 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005535 case BTRFS_IOC_TREE_SEARCH:
5536 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005537 case BTRFS_IOC_TREE_SEARCH_V2:
5538 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005539 case BTRFS_IOC_INO_LOOKUP:
5540 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005541 case BTRFS_IOC_INO_PATHS:
5542 return btrfs_ioctl_ino_to_path(root, argp);
5543 case BTRFS_IOC_LOGICAL_INO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005544 return btrfs_ioctl_logical_to_ino(fs_info, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005545 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005546 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005547 case BTRFS_IOC_SYNC: {
5548 int ret;
5549
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005550 ret = btrfs_start_delalloc_roots(fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005551 if (ret)
5552 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005553 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005554 /*
5555 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005556 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005557 * processing uncleaned subvols.
5558 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005559 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005560 return ret;
5561 }
Sage Weil46204592010-10-29 15:41:32 -04005562 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005563 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005564 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005565 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005566 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005567 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005568 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005569 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005570 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005571 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005572 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005573 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005574 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005575 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005576 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005577 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005578 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5579 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005580#ifdef CONFIG_64BIT
5581 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5582 return btrfs_ioctl_set_received_subvol_32(file, argp);
5583#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005584 case BTRFS_IOC_SEND:
5585 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005586 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005587 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005588 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005589 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005590 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005591 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005592 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005593 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005594 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005595 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005596 case BTRFS_IOC_QUOTA_RESCAN:
5597 return btrfs_ioctl_quota_rescan(file, argp);
5598 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5599 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005600 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5601 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005602 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005603 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005604 case BTRFS_IOC_GET_FSLABEL:
5605 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005606 case BTRFS_IOC_SET_FSLABEL:
5607 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005608 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005609 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005610 case BTRFS_IOC_GET_FEATURES:
5611 return btrfs_ioctl_get_features(file, argp);
5612 case BTRFS_IOC_SET_FEATURES:
5613 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005614 }
5615
5616 return -ENOTTY;
5617}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005618
5619#ifdef CONFIG_COMPAT
5620long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5621{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005622 /*
5623 * These all access 32-bit values anyway so no further
5624 * handling is necessary.
5625 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005626 switch (cmd) {
5627 case FS_IOC32_GETFLAGS:
5628 cmd = FS_IOC_GETFLAGS;
5629 break;
5630 case FS_IOC32_SETFLAGS:
5631 cmd = FS_IOC_SETFLAGS;
5632 break;
5633 case FS_IOC32_GETVERSION:
5634 cmd = FS_IOC_GETVERSION;
5635 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005636 }
5637
5638 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5639}
5640#endif