blob: 28a13b3c750381074472529dc0661d9b2b908307 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
8#include <linux/buffer_head.h>
9#include <linux/file.h>
10#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040011#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040012#include <linux/pagemap.h>
13#include <linux/highmem.h>
14#include <linux/time.h>
15#include <linux/init.h>
16#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040017#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040018#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/swap.h>
22#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040023#include <linux/compat.h>
24#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040025#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040026#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020027#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000029#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020030#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000031#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070032#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050033#include <linux/iversion.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040038#include "print-tree.h"
39#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040040#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080041#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020042#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040043#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020044#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010045#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000046#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040047#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070048#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000049#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080050#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051
Hugo Millsabccd002014-01-30 20:17:00 +000052#ifdef CONFIG_64BIT
53/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
54 * structures are incorrect, as the timespec structure from userspace
55 * is 4 bytes too small. We define these alternatives here to teach
56 * the kernel about the 32-bit struct packing.
57 */
58struct btrfs_ioctl_timespec_32 {
59 __u64 sec;
60 __u32 nsec;
61} __attribute__ ((__packed__));
62
63struct btrfs_ioctl_received_subvol_args_32 {
64 char uuid[BTRFS_UUID_SIZE]; /* in */
65 __u64 stransid; /* in */
66 __u64 rtransid; /* out */
67 struct btrfs_ioctl_timespec_32 stime; /* in */
68 struct btrfs_ioctl_timespec_32 rtime; /* out */
69 __u64 flags; /* in */
70 __u64 reserved[16]; /* in */
71} __attribute__ ((__packed__));
72
73#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
74 struct btrfs_ioctl_received_subvol_args_32)
75#endif
76
Josef Bacik2351f432017-09-27 10:43:13 -040077#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
78struct btrfs_ioctl_send_args_32 {
79 __s64 send_fd; /* in */
80 __u64 clone_sources_count; /* in */
81 compat_uptr_t clone_sources; /* in */
82 __u64 parent_root; /* in */
83 __u64 flags; /* in */
84 __u64 reserved[4]; /* in */
85} __attribute__ ((__packed__));
86
87#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
88 struct btrfs_ioctl_send_args_32)
89#endif
Hugo Millsabccd002014-01-30 20:17:00 +000090
Mark Fasheh416161d2013-08-06 11:42:51 -070091static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070092 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
93 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070094
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020096static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
97 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020098{
David Sterba1905a0f2018-03-26 18:52:15 +020099 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200100 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +0200101 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102 return flags & ~FS_DIRSYNC_FL;
103 else
104 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
105}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400106
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200108 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
109 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200110 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200111static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200112{
113 unsigned int iflags = 0;
114
115 if (flags & BTRFS_INODE_SYNC)
116 iflags |= FS_SYNC_FL;
117 if (flags & BTRFS_INODE_IMMUTABLE)
118 iflags |= FS_IMMUTABLE_FL;
119 if (flags & BTRFS_INODE_APPEND)
120 iflags |= FS_APPEND_FL;
121 if (flags & BTRFS_INODE_NODUMP)
122 iflags |= FS_NODUMP_FL;
123 if (flags & BTRFS_INODE_NOATIME)
124 iflags |= FS_NOATIME_FL;
125 if (flags & BTRFS_INODE_DIRSYNC)
126 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000127 if (flags & BTRFS_INODE_NODATACOW)
128 iflags |= FS_NOCOW_FL;
129
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900130 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000131 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900132 else if (flags & BTRFS_INODE_COMPRESS)
133 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200134
135 return iflags;
136}
137
138/*
139 * Update inode->i_flags based on the btrfs internal flags.
140 */
David Sterba7b6a2212018-03-26 18:40:21 +0200141void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142{
143 struct btrfs_inode *ip = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145
146 if (ip->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100147 new_fl |= S_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200148 if (ip->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100149 new_fl |= S_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200150 if (ip->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100151 new_fl |= S_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200152 if (ip->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100153 new_fl |= S_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200154 if (ip->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100155 new_fl |= S_DIRSYNC;
156
157 set_mask_bits(&inode->i_flags,
158 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
159 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160}
161
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200162static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
163{
Al Viro496ad9a2013-01-23 17:07:38 -0500164 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
David Sterbaa157d4f2018-03-26 19:12:25 +0200165 unsigned int flags = btrfs_inode_flags_to_fsflags(ip->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200166
167 if (copy_to_user(arg, &flags, sizeof(flags)))
168 return -EFAULT;
169 return 0;
170}
171
David Sterba5ba76ab2018-03-26 18:52:15 +0200172/* Check if @flags are a supported and valid set of FS_*_FL flags */
173static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000174{
175 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
176 FS_NOATIME_FL | FS_NODUMP_FL | \
177 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000178 FS_NOCOMP_FL | FS_COMPR_FL |
179 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return -EOPNOTSUPP;
181
182 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
183 return -EINVAL;
184
Liu Bo75e7cb72011-03-22 10:12:20 +0000185 return 0;
186}
187
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200188static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
189{
Al Viro496ad9a2013-01-23 17:07:38 -0500190 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200192 struct btrfs_inode *ip = BTRFS_I(inode);
193 struct btrfs_root *root = ip->root;
194 struct btrfs_trans_handle *trans;
195 unsigned int flags, oldflags;
196 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800197 u64 ip_oldflags;
198 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600199 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200
David Sterbabd60ea02014-01-16 15:50:22 +0100201 if (!inode_owner_or_capable(inode))
202 return -EPERM;
203
Li Zefanb83cc962010-12-20 16:04:08 +0800204 if (btrfs_root_readonly(root))
205 return -EROFS;
206
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200207 if (copy_from_user(&flags, arg, sizeof(flags)))
208 return -EFAULT;
209
David Sterba5ba76ab2018-03-26 18:52:15 +0200210 ret = check_fsflags(flags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000211 if (ret)
212 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200213
Jan Karae7848682012-06-12 16:20:32 +0200214 ret = mnt_want_write_file(file);
215 if (ret)
216 return ret;
217
Al Viro59551022016-01-22 15:40:57 -0500218 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200219
Li Zefanf062abf02011-12-29 13:36:45 +0800220 ip_oldflags = ip->flags;
221 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600222 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800223
David Sterba1905a0f2018-03-26 18:52:15 +0200224 flags = btrfs_mask_fsflags_for_type(inode, flags);
David Sterbaa157d4f2018-03-26 19:12:25 +0200225 oldflags = btrfs_inode_flags_to_fsflags(ip->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
227 if (!capable(CAP_LINUX_IMMUTABLE)) {
228 ret = -EPERM;
229 goto out_unlock;
230 }
231 }
232
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200233 if (flags & FS_SYNC_FL)
234 ip->flags |= BTRFS_INODE_SYNC;
235 else
236 ip->flags &= ~BTRFS_INODE_SYNC;
237 if (flags & FS_IMMUTABLE_FL)
238 ip->flags |= BTRFS_INODE_IMMUTABLE;
239 else
240 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
241 if (flags & FS_APPEND_FL)
242 ip->flags |= BTRFS_INODE_APPEND;
243 else
244 ip->flags &= ~BTRFS_INODE_APPEND;
245 if (flags & FS_NODUMP_FL)
246 ip->flags |= BTRFS_INODE_NODUMP;
247 else
248 ip->flags &= ~BTRFS_INODE_NODUMP;
249 if (flags & FS_NOATIME_FL)
250 ip->flags |= BTRFS_INODE_NOATIME;
251 else
252 ip->flags &= ~BTRFS_INODE_NOATIME;
253 if (flags & FS_DIRSYNC_FL)
254 ip->flags |= BTRFS_INODE_DIRSYNC;
255 else
256 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600257 if (flags & FS_NOCOW_FL) {
258 if (S_ISREG(mode)) {
259 /*
260 * It's safe to turn csums off here, no extents exist.
261 * Otherwise we want the flag to reflect the real COW
262 * status of the file and will not set it.
263 */
264 if (inode->i_size == 0)
265 ip->flags |= BTRFS_INODE_NODATACOW
266 | BTRFS_INODE_NODATASUM;
267 } else {
268 ip->flags |= BTRFS_INODE_NODATACOW;
269 }
270 } else {
271 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400272 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600273 */
274 if (S_ISREG(mode)) {
275 if (inode->i_size == 0)
276 ip->flags &= ~(BTRFS_INODE_NODATACOW
277 | BTRFS_INODE_NODATASUM);
278 } else {
279 ip->flags &= ~BTRFS_INODE_NODATACOW;
280 }
281 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200282
Liu Bo75e7cb72011-03-22 10:12:20 +0000283 /*
284 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
285 * flag may be changed automatically if compression code won't make
286 * things smaller.
287 */
288 if (flags & FS_NOCOMP_FL) {
289 ip->flags &= ~BTRFS_INODE_COMPRESS;
290 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000291
292 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
293 if (ret && ret != -ENODATA)
294 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000295 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000296 const char *comp;
297
Liu Bo75e7cb72011-03-22 10:12:20 +0000298 ip->flags |= BTRFS_INODE_COMPRESS;
299 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000300
David Sterba93370502017-10-31 17:32:41 +0100301 comp = btrfs_compress_type2str(fs_info->compress_type);
302 if (!comp || comp[0] == 0)
303 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
304
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000305 ret = btrfs_set_prop(inode, "btrfs.compression",
306 comp, strlen(comp), 0);
307 if (ret)
308 goto out_drop;
309
Li Zefanebcb9042011-04-15 03:03:17 +0000310 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100311 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
312 if (ret && ret != -ENODATA)
313 goto out_drop;
Li Zefanebcb9042011-04-15 03:03:17 +0000314 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000315 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200316
Li Zefan4da6f1a2011-12-29 13:39:50 +0800317 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800318 if (IS_ERR(trans)) {
319 ret = PTR_ERR(trans);
320 goto out_drop;
321 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322
David Sterba7b6a2212018-03-26 18:40:21 +0200323 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400324 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700325 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200326 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200327
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400328 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800329 out_drop:
330 if (ret) {
331 ip->flags = ip_oldflags;
332 inode->i_flags = i_oldflags;
333 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200334
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500336 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200337 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000338 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200339}
340
David Sterba19f93b32018-03-26 19:42:05 +0200341/*
342 * Translate btrfs internal inode flags to xflags as expected by the
343 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
344 * silently dropped.
345 */
346static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
347{
348 unsigned int xflags = 0;
349
350 if (flags & BTRFS_INODE_APPEND)
351 xflags |= FS_XFLAG_APPEND;
352 if (flags & BTRFS_INODE_IMMUTABLE)
353 xflags |= FS_XFLAG_IMMUTABLE;
354 if (flags & BTRFS_INODE_NOATIME)
355 xflags |= FS_XFLAG_NOATIME;
356 if (flags & BTRFS_INODE_NODUMP)
357 xflags |= FS_XFLAG_NODUMP;
358 if (flags & BTRFS_INODE_SYNC)
359 xflags |= FS_XFLAG_SYNC;
360
361 return xflags;
362}
363
364/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
365static int check_xflags(unsigned int flags)
366{
367 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
368 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
369 return -EOPNOTSUPP;
370 return 0;
371}
372
David Sterbae4202ac2018-03-26 19:51:16 +0200373/*
374 * Set the xflags from the internal inode flags. The remaining items of fsxattr
375 * are zeroed.
376 */
377static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
378{
379 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
380 struct fsxattr fa;
381
382 memset(&fa, 0, sizeof(fa));
383 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
384
385 if (copy_to_user(arg, &fa, sizeof(fa)))
386 return -EFAULT;
387
388 return 0;
389}
390
David Sterba025f2122018-03-26 19:51:16 +0200391static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
392{
393 struct inode *inode = file_inode(file);
394 struct btrfs_inode *binode = BTRFS_I(inode);
395 struct btrfs_root *root = binode->root;
396 struct btrfs_trans_handle *trans;
397 struct fsxattr fa;
398 unsigned old_flags;
399 unsigned old_i_flags;
400 int ret = 0;
401
402 if (!inode_owner_or_capable(inode))
403 return -EPERM;
404
405 if (btrfs_root_readonly(root))
406 return -EROFS;
407
408 memset(&fa, 0, sizeof(fa));
409 if (copy_from_user(&fa, arg, sizeof(fa)))
410 return -EFAULT;
411
412 ret = check_xflags(fa.fsx_xflags);
413 if (ret)
414 return ret;
415
416 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
417 return -EOPNOTSUPP;
418
419 ret = mnt_want_write_file(file);
420 if (ret)
421 return ret;
422
423 inode_lock(inode);
424
425 old_flags = binode->flags;
426 old_i_flags = inode->i_flags;
427
428 /* We need the capabilities to change append-only or immutable inode */
429 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
430 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
431 !capable(CAP_LINUX_IMMUTABLE)) {
432 ret = -EPERM;
433 goto out_unlock;
434 }
435
436 if (fa.fsx_xflags & FS_XFLAG_SYNC)
437 binode->flags |= BTRFS_INODE_SYNC;
438 else
439 binode->flags &= ~BTRFS_INODE_SYNC;
440 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
441 binode->flags |= BTRFS_INODE_IMMUTABLE;
442 else
443 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
444 if (fa.fsx_xflags & FS_XFLAG_APPEND)
445 binode->flags |= BTRFS_INODE_APPEND;
446 else
447 binode->flags &= ~BTRFS_INODE_APPEND;
448 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
449 binode->flags |= BTRFS_INODE_NODUMP;
450 else
451 binode->flags &= ~BTRFS_INODE_NODUMP;
452 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
453 binode->flags |= BTRFS_INODE_NOATIME;
454 else
455 binode->flags &= ~BTRFS_INODE_NOATIME;
456
457 /* 1 item for the inode */
458 trans = btrfs_start_transaction(root, 1);
459 if (IS_ERR(trans)) {
460 ret = PTR_ERR(trans);
461 goto out_unlock;
462 }
463
464 btrfs_sync_inode_flags_to_i_flags(inode);
465 inode_inc_iversion(inode);
466 inode->i_ctime = current_time(inode);
467 ret = btrfs_update_inode(trans, root, inode);
468
469 btrfs_end_transaction(trans);
470
471out_unlock:
472 if (ret) {
473 binode->flags = old_flags;
474 inode->i_flags = old_i_flags;
475 }
476
477 inode_unlock(inode);
478 mnt_drop_write_file(file);
479
480 return ret;
481}
482
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200483static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
484{
Al Viro496ad9a2013-01-23 17:07:38 -0500485 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200486
487 return put_user(inode->i_generation, arg);
488}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489
Li Dongyangf7039b12011-03-24 10:24:28 +0000490static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
491{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400492 struct inode *inode = file_inode(file);
493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000494 struct btrfs_device *device;
495 struct request_queue *q;
496 struct fstrim_range range;
497 u64 minlen = ULLONG_MAX;
498 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500499 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000500 int ret;
501
502 if (!capable(CAP_SYS_ADMIN))
503 return -EPERM;
504
Xiao Guangrong1f781602011-04-20 10:09:16 +0000505 rcu_read_lock();
506 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
507 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000508 if (!device->bdev)
509 continue;
510 q = bdev_get_queue(device->bdev);
511 if (blk_queue_discard(q)) {
512 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100513 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000514 minlen);
515 }
516 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000517 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200518
Li Dongyangf7039b12011-03-24 10:24:28 +0000519 if (!num_devices)
520 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000521 if (copy_from_user(&range, arg, sizeof(range)))
522 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000523 if (range.start > total_bytes ||
524 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200525 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000526
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200527 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000528 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400529 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000530 if (ret < 0)
531 return ret;
532
533 if (copy_to_user(arg, &range, sizeof(range)))
534 return -EFAULT;
535
536 return 0;
537}
538
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200539int btrfs_is_empty_uuid(u8 *uuid)
540{
Chris Mason46e0f662013-11-15 12:14:55 +0100541 int i;
542
543 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
544 if (uuid[i])
545 return 0;
546 }
547 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200548}
549
Miao Xied5c12072013-02-28 10:04:33 +0000550static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400551 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100552 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200553 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000554 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400556 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557 struct btrfs_trans_handle *trans;
558 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100559 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 struct btrfs_inode_item *inode_item;
561 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000562 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400563 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000564 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700565 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900566 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567 int ret;
568 int err;
569 u64 objectid;
570 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500571 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000572 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200573 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400574
David Sterba49a3c4d2016-03-24 17:49:22 +0100575 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
576 if (!root_item)
577 return -ENOMEM;
578
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400579 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400580 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100581 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000582
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800583 /*
584 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400585 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800586 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100587 if (btrfs_qgroup_level(objectid)) {
588 ret = -ENOSPC;
589 goto fail_free;
590 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800591
Miao Xied5c12072013-02-28 10:04:33 +0000592 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400593 /*
Miao Xied5c12072013-02-28 10:04:33 +0000594 * The same as the snapshot creation, please see the comment
595 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400596 */
Miao Xied5c12072013-02-28 10:04:33 +0000597 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200598 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000599 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100600 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400601
Miao Xied5c12072013-02-28 10:04:33 +0000602 trans = btrfs_start_transaction(root, 0);
603 if (IS_ERR(trans)) {
604 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100605 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100606 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000607 }
608 trans->block_rsv = &block_rsv;
609 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400610
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400611 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200612 if (ret)
613 goto fail;
614
David Sterba4d75f8a2014-06-15 01:54:12 +0200615 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400616 if (IS_ERR(leaf)) {
617 ret = PTR_ERR(leaf);
618 goto fail;
619 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400620
David Sterbab159fa22016-11-08 18:09:03 +0100621 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400622 btrfs_set_header_bytenr(leaf, leaf->start);
623 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400624 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400625 btrfs_set_header_owner(leaf, objectid);
626
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400627 write_extent_buffer_fsid(leaf, fs_info->fsid);
628 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400629 btrfs_mark_buffer_dirty(leaf);
630
David Sterba49a3c4d2016-03-24 17:49:22 +0100631 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800632 btrfs_set_stack_inode_generation(inode_item, 1);
633 btrfs_set_stack_inode_size(inode_item, 3);
634 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400635 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400636 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800637 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400638
David Sterba49a3c4d2016-03-24 17:49:22 +0100639 btrfs_set_root_flags(root_item, 0);
640 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800641 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000642
David Sterba49a3c4d2016-03-24 17:49:22 +0100643 btrfs_set_root_bytenr(root_item, leaf->start);
644 btrfs_set_root_generation(root_item, trans->transid);
645 btrfs_set_root_level(root_item, 0);
646 btrfs_set_root_refs(root_item, 1);
647 btrfs_set_root_used(root_item, leaf->len);
648 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400649
David Sterba49a3c4d2016-03-24 17:49:22 +0100650 btrfs_set_root_generation_v2(root_item,
651 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200652 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100653 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
654 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
655 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
656 root_item->ctime = root_item->otime;
657 btrfs_set_root_ctransid(root_item, trans->transid);
658 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400659
Chris Mason925baed2008-06-25 16:01:30 -0400660 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400661 free_extent_buffer(leaf);
662 leaf = NULL;
663
David Sterba49a3c4d2016-03-24 17:49:22 +0100664 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400665
666 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400667 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200668 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100670 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400671 if (ret)
672 goto fail;
673
Yan, Zheng76dda932009-09-21 16:00:26 -0400674 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400675 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100676 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100677 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400678 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 goto fail;
680 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400681
682 btrfs_record_root_in_trans(trans, new_root);
683
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000684 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700685 if (ret) {
686 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400687 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700688 goto fail;
689 }
690
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530691 mutex_lock(&new_root->objectid_mutex);
692 new_root->highest_objectid = new_dirid;
693 mutex_unlock(&new_root->objectid_mutex);
694
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400695 /*
696 * insert the directory item
697 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200698 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400700 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100701 goto fail;
702 }
Chris Mason3de45862008-11-17 21:02:50 -0500703
704 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200705 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500706 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100707 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400708 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400709 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100710 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500711
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200712 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500713 ret = btrfs_update_inode(trans, root, dir);
714 BUG_ON(ret);
715
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400716 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400717 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200718 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500719 BUG_ON(ret);
720
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400721 ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400722 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200723 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400724 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200725
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100727 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000728 trans->block_rsv = NULL;
729 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100730 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800731
Sage Weil72fd0322010-10-29 15:41:32 -0400732 if (async_transid) {
733 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400734 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000735 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400736 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400737 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400738 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400739 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400740 if (err && !ret)
741 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500742
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900743 if (!ret) {
744 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800745 if (IS_ERR(inode))
746 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900747 d_instantiate(dentry, inode);
748 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100750
751fail_free:
752 kfree(root_item);
753 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400754}
755
Miao Xiee9662f72013-02-28 10:01:15 +0000756static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100757 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000758 u64 *async_transid, bool readonly,
759 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400761 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000762 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763 struct btrfs_pending_snapshot *pending_snapshot;
764 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000765 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400766
Miao Xie27cdeb72014-04-02 19:51:05 +0800767 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400768 return -EINVAL;
769
David Sterba23269bf2017-02-13 11:03:44 +0100770 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100771 if (!pending_snapshot)
772 return -ENOMEM;
773
David Sterbab0c0ea62015-11-10 18:54:00 +0100774 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100775 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100776 pending_snapshot->path = btrfs_alloc_path();
777 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100778 ret = -ENOMEM;
779 goto free_pending;
780 }
781
David Sterbaea14b57f2017-06-22 02:19:11 +0200782 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100783 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600784 /* wait for no snapshot writes */
785 wait_event(root->subv_writers->wait,
786 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800787
Nikolay Borisov76f32e22018-04-23 10:54:14 +0300788 ret = btrfs_start_delalloc_inodes(root);
Miao Xie6a038432013-05-15 07:48:24 +0000789 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100790 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000791
Chris Mason6374e57a2017-06-23 09:48:21 -0700792 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000793
Miao Xie66d8f3d2012-09-06 04:02:28 -0600794 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
795 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000796 /*
797 * 1 - parent dir inode
798 * 2 - dir entries
799 * 1 - root item
800 * 2 - root ref/backref
801 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200802 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000803 */
804 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200805 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400806 &pending_snapshot->qgroup_reserved,
807 false);
Miao Xied5c12072013-02-28 10:04:33 +0000808 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100809 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000810
Yan, Zhenga22285a2010-05-16 10:48:46 -0400811 pending_snapshot->dentry = dentry;
812 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800813 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000814 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000815 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400816
Miao Xied5c12072013-02-28 10:04:33 +0000817 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400818 if (IS_ERR(trans)) {
819 ret = PTR_ERR(trans);
820 goto fail;
821 }
822
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400823 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400824 list_add(&pending_snapshot->list,
825 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400826 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400827 if (async_transid) {
828 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400829 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000830 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400831 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400832 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400833 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400834 }
Miao Xieaec80302013-03-04 09:44:29 +0000835 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400836 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400837
838 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400839 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000840 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400841
Chris Masond3797302014-10-15 13:50:56 -0700842 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
843 if (ret)
844 goto fail;
845
David Howells2b0143b2015-03-17 22:25:59 +0000846 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000847 if (IS_ERR(inode)) {
848 ret = PTR_ERR(inode);
849 goto fail;
850 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900851
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000852 d_instantiate(dentry, inode);
853 ret = 0;
854fail:
David Sterba7775c812017-02-10 19:18:18 +0100855 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100856dec_and_free:
David Sterbaea14b57f2017-06-22 02:19:11 +0200857 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100858 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100859free_pending:
860 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100861 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100862 kfree(pending_snapshot);
863
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400864 return ret;
865}
866
Sage Weil4260f7c2010-10-29 15:46:43 -0400867/* copy of may_delete in fs/namei.c()
868 * Check whether we can remove a link victim from directory dir, check
869 * whether the type of victim is right.
870 * 1. We can't do it if dir is read-only (done in permission())
871 * 2. We should have write and exec permissions on dir
872 * 3. We can't remove anything from append-only dir
873 * 4. We can't do anything with immutable dir (done in permission())
874 * 5. If the sticky bit on dir is set we should either
875 * a. be owner of dir, or
876 * b. be owner of victim, or
877 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400878 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400879 * links pointing to it.
880 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
881 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
882 * 9. We can't remove a root or mountpoint.
883 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
884 * nfs_async_unlink().
885 */
886
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530887static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400888{
889 int error;
890
David Howells2b0143b2015-03-17 22:25:59 +0000891 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400892 return -ENOENT;
893
David Howells2b0143b2015-03-17 22:25:59 +0000894 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400895 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400896
897 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
898 if (error)
899 return error;
900 if (IS_APPEND(dir))
901 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000902 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
903 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400904 return -EPERM;
905 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000906 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400907 return -ENOTDIR;
908 if (IS_ROOT(victim))
909 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000910 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400911 return -EISDIR;
912 if (IS_DEADDIR(dir))
913 return -ENOENT;
914 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
915 return -EBUSY;
916 return 0;
917}
918
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400919/* copy of may_create in fs/namei.c() */
920static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
921{
David Howells2b0143b2015-03-17 22:25:59 +0000922 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400923 return -EEXIST;
924 if (IS_DEADDIR(dir))
925 return -ENOENT;
926 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
927}
928
929/*
930 * Create a new subvolume below @parent. This is largely modeled after
931 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
932 * inside this filesystem so it's quite a bit simpler.
933 */
Al Viro92872092016-11-20 19:34:31 -0500934static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100935 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400936 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200937 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000938 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400939{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400940 struct inode *dir = d_inode(parent->dentry);
941 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400942 struct dentry *dentry;
943 int error;
944
Al Viro00235412016-05-26 00:05:12 -0400945 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
946 if (error == -EINTR)
947 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400948
949 dentry = lookup_one_len(name, parent->dentry, namelen);
950 error = PTR_ERR(dentry);
951 if (IS_ERR(dentry))
952 goto out_unlock;
953
Yan, Zheng76dda932009-09-21 16:00:26 -0400954 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400955 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600956 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400957
Chris Mason9c520572012-12-17 14:26:57 -0500958 /*
959 * even if this name doesn't exist, we may get hash collisions.
960 * check for them now when we can safely fail
961 */
962 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
963 dir->i_ino, name,
964 namelen);
965 if (error)
966 goto out_dput;
967
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400968 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400969
970 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
971 goto out_up_read;
972
Chris Mason3de45862008-11-17 21:02:50 -0500973 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100974 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200975 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500976 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000977 error = create_subvol(dir, dentry, name, namelen,
978 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500979 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400980 if (!error)
981 fsnotify_mkdir(dir, dentry);
982out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400983 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400984out_dput:
985 dput(dentry);
986out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500987 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400988 return error;
989}
990
Chris Mason4cb53002011-05-24 15:35:30 -0400991/*
992 * When we're defragging a range, we don't want to kick it off again
993 * if it is really just waiting for delalloc to send it down.
994 * If we find a nice big extent or delalloc range for the bytes in the
995 * file you want to defrag, we return 0 to let you know to skip this
996 * part of the file
997 */
David Sterbaaab110a2014-07-29 17:32:10 +0200998static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400999{
1000 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1001 struct extent_map *em = NULL;
1002 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1003 u64 end;
1004
1005 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001006 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001007 read_unlock(&em_tree->lock);
1008
1009 if (em) {
1010 end = extent_map_end(em);
1011 free_extent_map(em);
1012 if (end - offset > thresh)
1013 return 0;
1014 }
1015 /* if we already have a nice delalloc here, just stop */
1016 thresh /= 2;
1017 end = count_range_bits(io_tree, &offset, offset + thresh,
1018 thresh, EXTENT_DELALLOC, 1);
1019 if (end >= thresh)
1020 return 0;
1021 return 1;
1022}
1023
1024/*
1025 * helper function to walk through a file and find extents
1026 * newer than a specific transid, and smaller than thresh.
1027 *
1028 * This is used by the defragging code to find new and small
1029 * extents
1030 */
1031static int find_new_extents(struct btrfs_root *root,
1032 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001033 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001034{
1035 struct btrfs_path *path;
1036 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001037 struct extent_buffer *leaf;
1038 struct btrfs_file_extent_item *extent;
1039 int type;
1040 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001041 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001042
1043 path = btrfs_alloc_path();
1044 if (!path)
1045 return -ENOMEM;
1046
David Sterbaa4689d22011-05-31 17:08:14 +00001047 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001048 min_key.type = BTRFS_EXTENT_DATA_KEY;
1049 min_key.offset = *off;
1050
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301051 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001052 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001053 if (ret != 0)
1054 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001055process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001056 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001057 goto none;
1058 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1059 goto none;
1060
1061 leaf = path->nodes[0];
1062 extent = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_file_extent_item);
1064
1065 type = btrfs_file_extent_type(leaf, extent);
1066 if (type == BTRFS_FILE_EXTENT_REG &&
1067 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1068 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1069 *off = min_key.offset;
1070 btrfs_free_path(path);
1071 return 0;
1072 }
1073
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001074 path->slots[0]++;
1075 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1076 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1077 goto process_slot;
1078 }
1079
Chris Mason4cb53002011-05-24 15:35:30 -04001080 if (min_key.offset == (u64)-1)
1081 goto none;
1082
1083 min_key.offset++;
1084 btrfs_release_path(path);
1085 }
1086none:
1087 btrfs_free_path(path);
1088 return -ENOENT;
1089}
1090
Li Zefan6c282eb2012-06-11 16:03:35 +08001091static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001092{
1093 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001094 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001095 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001096 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001097
1098 /*
1099 * hopefully we have this extent in the tree already, try without
1100 * the full extent lock
1101 */
1102 read_lock(&em_tree->lock);
1103 em = lookup_extent_mapping(em_tree, start, len);
1104 read_unlock(&em_tree->lock);
1105
1106 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001107 struct extent_state *cached = NULL;
1108 u64 end = start + len - 1;
1109
Chris Mason940100a2010-03-10 10:52:59 -05001110 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001111 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001112 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001113 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001114
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001115 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001116 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001117 }
1118
Li Zefan6c282eb2012-06-11 16:03:35 +08001119 return em;
1120}
1121
1122static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1123{
1124 struct extent_map *next;
1125 bool ret = true;
1126
1127 /* this is the last extent */
1128 if (em->start + em->len >= i_size_read(inode))
1129 return false;
1130
1131 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001132 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1133 ret = false;
1134 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001135 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001136 ret = false;
1137
1138 free_extent_map(next);
1139 return ret;
1140}
1141
David Sterbaaab110a2014-07-29 17:32:10 +02001142static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001143 u64 *last_len, u64 *skip, u64 *defrag_end,
1144 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001145{
1146 struct extent_map *em;
1147 int ret = 1;
1148 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001149 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001150
1151 /*
1152 * make sure that once we start defragging an extent, we keep on
1153 * defragging it
1154 */
1155 if (start < *defrag_end)
1156 return 1;
1157
1158 *skip = 0;
1159
1160 em = defrag_lookup_extent(inode, start);
1161 if (!em)
1162 return 0;
1163
Chris Mason940100a2010-03-10 10:52:59 -05001164 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001165 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001166 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001167 goto out;
1168 }
1169
Liu Bo4a3560c2015-08-07 16:48:41 +08001170 if (!*defrag_end)
1171 prev_mergeable = false;
1172
Li Zefan6c282eb2012-06-11 16:03:35 +08001173 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001174 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001175 * we hit a real extent, if it is big or the next extent is not a
1176 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001177 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001178 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001179 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001180 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001181out:
Chris Mason940100a2010-03-10 10:52:59 -05001182 /*
1183 * last_len ends up being a counter of how many bytes we've defragged.
1184 * every time we choose not to defrag an extent, we reset *last_len
1185 * so that the next tiny extent will force a defrag.
1186 *
1187 * The end result of this is that tiny extents before a single big
1188 * extent will force at least part of that big extent to be defragged.
1189 */
1190 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001191 *defrag_end = extent_map_end(em);
1192 } else {
1193 *last_len = 0;
1194 *skip = extent_map_end(em);
1195 *defrag_end = 0;
1196 }
1197
1198 free_extent_map(em);
1199 return ret;
1200}
1201
Chris Mason4cb53002011-05-24 15:35:30 -04001202/*
1203 * it doesn't do much good to defrag one or two pages
1204 * at a time. This pulls in a nice chunk of pages
1205 * to COW and defrag.
1206 *
1207 * It also makes sure the delalloc code has enough
1208 * dirty data to avoid making new small extents as part
1209 * of the defrag
1210 *
1211 * It's a good idea to start RA on this range
1212 * before calling this.
1213 */
1214static int cluster_pages_for_defrag(struct inode *inode,
1215 struct page **pages,
1216 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001217 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001218{
Chris Mason4cb53002011-05-24 15:35:30 -04001219 unsigned long file_end;
1220 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001221 u64 page_start;
1222 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001223 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001224 int ret;
1225 int i;
1226 int i_done;
1227 struct btrfs_ordered_extent *ordered;
1228 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001229 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001230 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001231 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001232
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001233 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001234 if (!isize || start_index > file_end)
1235 return 0;
1236
1237 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001238
Qu Wenruo364ecf32017-02-27 15:10:38 +08001239 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001240 start_index << PAGE_SHIFT,
1241 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001242 if (ret)
1243 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001244 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001245 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001246
1247 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001248 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001249 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001250again:
Josef Bacika94733d2011-07-11 10:47:06 -04001251 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001252 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001253 if (!page)
1254 break;
1255
Miao Xie600a45e2012-02-16 15:01:24 +08001256 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001257 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001258 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001259 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001260 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001261 ordered = btrfs_lookup_ordered_extent(inode,
1262 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001263 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001264 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001265 if (!ordered)
1266 break;
1267
1268 unlock_page(page);
1269 btrfs_start_ordered_extent(inode, ordered, 1);
1270 btrfs_put_ordered_extent(ordered);
1271 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001272 /*
1273 * we unlocked the page above, so we need check if
1274 * it was released or not.
1275 */
1276 if (page->mapping != inode->i_mapping) {
1277 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001278 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001279 goto again;
1280 }
Miao Xie600a45e2012-02-16 15:01:24 +08001281 }
1282
Chris Mason4cb53002011-05-24 15:35:30 -04001283 if (!PageUptodate(page)) {
1284 btrfs_readpage(NULL, page);
1285 lock_page(page);
1286 if (!PageUptodate(page)) {
1287 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001288 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001289 ret = -EIO;
1290 break;
1291 }
1292 }
Miao Xie600a45e2012-02-16 15:01:24 +08001293
Miao Xie600a45e2012-02-16 15:01:24 +08001294 if (page->mapping != inode->i_mapping) {
1295 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001296 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001297 goto again;
1298 }
1299
Chris Mason4cb53002011-05-24 15:35:30 -04001300 pages[i] = page;
1301 i_done++;
1302 }
1303 if (!i_done || ret)
1304 goto out;
1305
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001306 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001307 goto out;
1308
1309 /*
1310 * so now we have a nice long stream of locked
1311 * and up to date pages, lets wait on them
1312 */
1313 for (i = 0; i < i_done; i++)
1314 wait_on_page_writeback(pages[i]);
1315
1316 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001317 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001318
1319 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001320 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001321 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1322 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001323 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001324 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001325
Liu Bo1f12bd02012-03-29 09:57:44 -04001326 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001327 spin_lock(&BTRFS_I(inode)->lock);
1328 BTRFS_I(inode)->outstanding_extents++;
1329 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001330 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001331 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001332 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001333 }
1334
1335
Liu Bo9e8a4a82012-09-05 19:10:51 -06001336 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001337 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001338
1339 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001340 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001341
1342 for (i = 0; i < i_done; i++) {
1343 clear_page_dirty_for_io(pages[i]);
1344 ClearPageChecked(pages[i]);
1345 set_page_extent_mapped(pages[i]);
1346 set_page_dirty(pages[i]);
1347 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001348 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001349 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001350 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1351 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001352 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001353 return i_done;
1354out:
1355 for (i = 0; i < i_done; i++) {
1356 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001357 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001358 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001359 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001360 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001361 page_cnt << PAGE_SHIFT, true);
1362 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1363 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001364 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001365 return ret;
1366
1367}
1368
1369int btrfs_defrag_file(struct inode *inode, struct file *file,
1370 struct btrfs_ioctl_defrag_range_args *range,
1371 u64 newer_than, unsigned long max_to_defrag)
1372{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001373 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001374 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001375 struct file_ra_state *ra = NULL;
1376 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001377 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001378 u64 last_len = 0;
1379 u64 skip = 0;
1380 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001381 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001382 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001383 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001384 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001385 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001386 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001387 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001388 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001389 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001390 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001391 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001392 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001393
Liu Bo0abd5b12013-04-16 09:20:28 +00001394 if (isize == 0)
1395 return 0;
1396
1397 if (range->start >= isize)
1398 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001399
David Sterba1e2ef462017-07-17 20:01:59 +02001400 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001401 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1402 return -EINVAL;
1403 if (range->compress_type)
1404 compress_type = range->compress_type;
1405 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001406
Liu Bo0abd5b12013-04-16 09:20:28 +00001407 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001408 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001409
Chris Mason4cb53002011-05-24 15:35:30 -04001410 /*
David Sterba0a52d102017-06-22 03:22:58 +02001411 * If we were not given a file, allocate a readahead context. As
1412 * readahead is just an optimization, defrag will work without it so
1413 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001414 */
1415 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001416 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001417 if (ra)
1418 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001419 } else {
1420 ra = &file->f_ra;
1421 }
1422
David Sterba63e727e2017-06-22 03:13:02 +02001423 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001424 if (!pages) {
1425 ret = -ENOMEM;
1426 goto out_ra;
1427 }
1428
1429 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001430 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001431 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001432 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001433 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001434 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001435 }
1436
Chris Mason4cb53002011-05-24 15:35:30 -04001437 if (newer_than) {
1438 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001439 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001440 if (!ret) {
1441 range->start = newer_off;
1442 /*
1443 * we always align our defrag to help keep
1444 * the extents in the file evenly spaced
1445 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001446 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001447 } else
1448 goto out_ra;
1449 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001450 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001451 }
1452 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301453 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001454
Li Zefan2a0f7f52011-10-10 15:43:34 -04001455 /*
1456 * make writeback starts from i, so the defrag range can be
1457 * written sequentially.
1458 */
1459 if (i < inode->i_mapping->writeback_index)
1460 inode->i_mapping->writeback_index = i;
1461
Chris Masonf7f43cc2011-10-11 11:41:40 -04001462 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001463 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001464 /*
1465 * make sure we stop running if someone unmounts
1466 * the FS
1467 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001468 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001469 break;
1470
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001471 if (btrfs_defrag_cancelled(fs_info)) {
1472 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001473 ret = -EAGAIN;
1474 break;
1475 }
1476
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001477 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001478 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001479 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001480 unsigned long next;
1481 /*
1482 * the should_defrag function tells us how much to skip
1483 * bump our counter by the suggested amount
1484 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001485 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001486 i = max(i + 1, next);
1487 continue;
1488 }
Li Zefan008873e2011-09-02 15:57:07 +08001489
1490 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001491 cluster = (PAGE_ALIGN(defrag_end) >>
1492 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001493 cluster = min(cluster, max_cluster);
1494 } else {
1495 cluster = max_cluster;
1496 }
1497
Li Zefan008873e2011-09-02 15:57:07 +08001498 if (i + cluster > ra_index) {
1499 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001500 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001501 page_cache_sync_readahead(inode->i_mapping, ra,
1502 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301503 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001504 }
Chris Mason940100a2010-03-10 10:52:59 -05001505
Al Viro59551022016-01-22 15:40:57 -05001506 inode_lock(inode);
David Sterba1e2ef462017-07-17 20:01:59 +02001507 if (do_compress)
David Sterbaeec63c62017-07-17 19:41:31 +02001508 BTRFS_I(inode)->defrag_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001509 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001510 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001511 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001512 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001513 }
Chris Mason940100a2010-03-10 10:52:59 -05001514
Chris Mason4cb53002011-05-24 15:35:30 -04001515 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001516 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001517 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001518
1519 if (newer_than) {
1520 if (newer_off == (u64)-1)
1521 break;
1522
Liu Boe1f041e2012-03-29 09:57:45 -04001523 if (ret > 0)
1524 i += ret;
1525
Chris Mason4cb53002011-05-24 15:35:30 -04001526 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001527 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001528
Byongho Leeee221842015-12-15 01:42:10 +09001529 ret = find_new_extents(root, inode, newer_than,
1530 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001531 if (!ret) {
1532 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001533 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001534 } else {
1535 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001536 }
Chris Mason4cb53002011-05-24 15:35:30 -04001537 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001538 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001539 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001540 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001541 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001542 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001543 last_len = 0;
1544 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001545 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001546 }
1547
Filipe Mananadec8ef92014-03-01 10:55:54 +00001548 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001549 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001550 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1551 &BTRFS_I(inode)->runtime_flags))
1552 filemap_flush(inode->i_mapping);
1553 }
Chris Mason1e701a32010-03-11 09:42:04 -05001554
Li Zefan1a419d82010-10-25 15:12:50 +08001555 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001556 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001557 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1558 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001559 }
1560
Diego Calleja60ccf822011-09-01 16:33:57 +02001561 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001562
Chris Mason4cb53002011-05-24 15:35:30 -04001563out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001564 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001565 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001566 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001567 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001568 }
Chris Mason4cb53002011-05-24 15:35:30 -04001569 if (!file)
1570 kfree(ra);
1571 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001572 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573}
1574
Miao Xie198605a2012-11-26 08:43:45 +00001575static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001576 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001578 struct inode *inode = file_inode(file);
1579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001580 u64 new_size;
1581 u64 old_size;
1582 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001583 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001584 struct btrfs_ioctl_vol_args *vol_args;
1585 struct btrfs_trans_handle *trans;
1586 struct btrfs_device *device = NULL;
1587 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001588 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001589 char *devstr = NULL;
1590 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001591 int mod = 0;
1592
Chris Masone441d542009-01-05 16:57:23 -05001593 if (!capable(CAP_SYS_ADMIN))
1594 return -EPERM;
1595
Miao Xie198605a2012-11-26 08:43:45 +00001596 ret = mnt_want_write_file(file);
1597 if (ret)
1598 return ret;
1599
David Sterba171938e2017-03-28 14:44:21 +02001600 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001601 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001602 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001603 }
1604
Li Zefandae7b662009-04-08 15:06:54 +08001605 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001606 if (IS_ERR(vol_args)) {
1607 ret = PTR_ERR(vol_args);
1608 goto out;
1609 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001610
1611 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001612
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001613 sizestr = vol_args->name;
1614 devstr = strchr(sizestr, ':');
1615 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616 sizestr = devstr + 1;
1617 *devstr = '\0';
1618 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001619 ret = kstrtoull(devstr, 10, &devid);
1620 if (ret)
1621 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001622 if (!devid) {
1623 ret = -EINVAL;
1624 goto out_free;
1625 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001626 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001627 }
Miao Xiedba60f32012-12-21 09:19:51 +00001628
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001629 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001630 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001631 btrfs_info(fs_info, "resizer unable to find device %llu",
1632 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001633 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001634 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001635 }
Miao Xiedba60f32012-12-21 09:19:51 +00001636
Anand Jainebbede42017-12-04 12:54:52 +08001637 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001638 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001639 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001640 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001641 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001642 goto out_free;
1643 }
1644
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001645 if (!strcmp(sizestr, "max"))
1646 new_size = device->bdev->bd_inode->i_size;
1647 else {
1648 if (sizestr[0] == '-') {
1649 mod = -1;
1650 sizestr++;
1651 } else if (sizestr[0] == '+') {
1652 mod = 1;
1653 sizestr++;
1654 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001655 new_size = memparse(sizestr, &retptr);
1656 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001657 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001658 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001659 }
1660 }
1661
Anand Jain401e29c2017-12-04 12:54:55 +08001662 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001663 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001664 goto out_free;
1665 }
1666
Miao Xie7cc8e582014-09-03 21:35:38 +08001667 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001668
1669 if (mod < 0) {
1670 if (new_size > old_size) {
1671 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001672 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001673 }
1674 new_size = old_size - new_size;
1675 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001676 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001677 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001678 goto out_free;
1679 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001680 new_size = old_size + new_size;
1681 }
1682
Byongho Leeee221842015-12-15 01:42:10 +09001683 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001684 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001685 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001686 }
1687 if (new_size > device->bdev->bd_inode->i_size) {
1688 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001689 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001690 }
1691
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001692 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001693
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001694 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1695 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001696
1697 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001698 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001699 if (IS_ERR(trans)) {
1700 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001701 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001702 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001703 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001704 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001705 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001707 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001708
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001709out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001710 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001711out:
David Sterba171938e2017-03-28 14:44:21 +02001712 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001713 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001714 return ret;
1715}
1716
Sage Weil72fd0322010-10-29 15:41:32 -04001717static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001718 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001719 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001720 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001721{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001722 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001723 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001724
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001725 if (!S_ISDIR(file_inode(file)->i_mode))
1726 return -ENOTDIR;
1727
Liu Boa874a632012-06-29 03:58:46 -06001728 ret = mnt_want_write_file(file);
1729 if (ret)
1730 goto out;
1731
Sage Weil72fd0322010-10-29 15:41:32 -04001732 namelen = strlen(name);
1733 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001734 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001735 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001736 }
1737
Chris Mason16780ca2012-02-20 22:14:55 -05001738 if (name[0] == '.' &&
1739 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1740 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001741 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001742 }
1743
Chris Mason3de45862008-11-17 21:02:50 -05001744 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001745 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001746 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001747 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001748 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001749 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001750 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001751 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001752 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001753 }
1754
Al Viro496ad9a2013-01-23 17:07:38 -05001755 src_inode = file_inode(src.file);
1756 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001757 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001758 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001759 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001760 } else if (!inode_owner_or_capable(src_inode)) {
1761 /*
1762 * Subvolume creation is not restricted, but snapshots
1763 * are limited to own subvolumes only
1764 */
1765 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001766 } else {
1767 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1768 BTRFS_I(src_inode)->root,
1769 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001770 }
Al Viro2903ff02012-08-28 12:52:22 -04001771 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001772 }
Liu Boa874a632012-06-29 03:58:46 -06001773out_drop_write:
1774 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001775out:
Sage Weil72fd0322010-10-29 15:41:32 -04001776 return ret;
1777}
1778
1779static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001780 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001781{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001782 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001783 int ret;
1784
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001785 if (!S_ISDIR(file_inode(file)->i_mode))
1786 return -ENOTDIR;
1787
Li Zefanfa0d2b92010-12-20 15:53:28 +08001788 vol_args = memdup_user(arg, sizeof(*vol_args));
1789 if (IS_ERR(vol_args))
1790 return PTR_ERR(vol_args);
1791 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001792
Li Zefanfa0d2b92010-12-20 15:53:28 +08001793 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001794 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001795 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001796
Li Zefanfa0d2b92010-12-20 15:53:28 +08001797 kfree(vol_args);
1798 return ret;
1799}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001800
Li Zefanfa0d2b92010-12-20 15:53:28 +08001801static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1802 void __user *arg, int subvol)
1803{
1804 struct btrfs_ioctl_vol_args_v2 *vol_args;
1805 int ret;
1806 u64 transid = 0;
1807 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001808 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001809 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001810
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001811 if (!S_ISDIR(file_inode(file)->i_mode))
1812 return -ENOTDIR;
1813
Li Zefanfa0d2b92010-12-20 15:53:28 +08001814 vol_args = memdup_user(arg, sizeof(*vol_args));
1815 if (IS_ERR(vol_args))
1816 return PTR_ERR(vol_args);
1817 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001818
Li Zefanb83cc962010-12-20 16:04:08 +08001819 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001820 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1821 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001822 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001823 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001824 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001825
1826 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1827 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001828 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1829 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001830 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001831 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001832 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001833 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001834 }
1835 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1836 if (IS_ERR(inherit)) {
1837 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001838 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001839 }
1840 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001841
1842 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001843 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001844 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001845 if (ret)
1846 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001847
Dan Carpenterc47ca322014-09-04 14:09:15 +03001848 if (ptr && copy_to_user(arg +
1849 offsetof(struct btrfs_ioctl_vol_args_v2,
1850 transid),
1851 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001852 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001853
1854free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001855 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001856free_args:
1857 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001858 return ret;
1859}
1860
Li Zefan0caa1022010-12-20 16:30:25 +08001861static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1862 void __user *arg)
1863{
Al Viro496ad9a2013-01-23 17:07:38 -05001864 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001865 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001866 struct btrfs_root *root = BTRFS_I(inode)->root;
1867 int ret = 0;
1868 u64 flags = 0;
1869
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001870 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001871 return -EINVAL;
1872
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001874 if (btrfs_root_readonly(root))
1875 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001876 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001877
1878 if (copy_to_user(arg, &flags, sizeof(flags)))
1879 ret = -EFAULT;
1880
1881 return ret;
1882}
1883
1884static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1885 void __user *arg)
1886{
Al Viro496ad9a2013-01-23 17:07:38 -05001887 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001889 struct btrfs_root *root = BTRFS_I(inode)->root;
1890 struct btrfs_trans_handle *trans;
1891 u64 root_flags;
1892 u64 flags;
1893 int ret = 0;
1894
David Sterbabd60ea02014-01-16 15:50:22 +01001895 if (!inode_owner_or_capable(inode))
1896 return -EPERM;
1897
Liu Bob9ca0662012-06-29 03:58:49 -06001898 ret = mnt_want_write_file(file);
1899 if (ret)
1900 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001901
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001902 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001903 ret = -EINVAL;
1904 goto out_drop_write;
1905 }
Li Zefan0caa1022010-12-20 16:30:25 +08001906
Liu Bob9ca0662012-06-29 03:58:49 -06001907 if (copy_from_user(&flags, arg, sizeof(flags))) {
1908 ret = -EFAULT;
1909 goto out_drop_write;
1910 }
Li Zefan0caa1022010-12-20 16:30:25 +08001911
Liu Bob9ca0662012-06-29 03:58:49 -06001912 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1913 ret = -EINVAL;
1914 goto out_drop_write;
1915 }
Li Zefan0caa1022010-12-20 16:30:25 +08001916
Liu Bob9ca0662012-06-29 03:58:49 -06001917 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1918 ret = -EOPNOTSUPP;
1919 goto out_drop_write;
1920 }
Li Zefan0caa1022010-12-20 16:30:25 +08001921
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001922 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001923
1924 /* nothing to do */
1925 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001926 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001927
1928 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001929 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001930 btrfs_set_root_flags(&root->root_item,
1931 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001932 } else {
1933 /*
1934 * Block RO -> RW transition if this subvolume is involved in
1935 * send
1936 */
1937 spin_lock(&root->root_item_lock);
1938 if (root->send_in_progress == 0) {
1939 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001940 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001941 spin_unlock(&root->root_item_lock);
1942 } else {
1943 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001944 btrfs_warn(fs_info,
1945 "Attempt to set subvolume %llu read-write during send",
1946 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001947 ret = -EPERM;
1948 goto out_drop_sem;
1949 }
1950 }
Li Zefan0caa1022010-12-20 16:30:25 +08001951
1952 trans = btrfs_start_transaction(root, 1);
1953 if (IS_ERR(trans)) {
1954 ret = PTR_ERR(trans);
1955 goto out_reset;
1956 }
1957
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001958 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001959 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001960 if (ret < 0) {
1961 btrfs_end_transaction(trans);
1962 goto out_reset;
1963 }
Li Zefan0caa1022010-12-20 16:30:25 +08001964
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001965 ret = btrfs_commit_transaction(trans);
1966
Li Zefan0caa1022010-12-20 16:30:25 +08001967out_reset:
1968 if (ret)
1969 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001970out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001971 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001972out_drop_write:
1973 mnt_drop_write_file(file);
1974out:
Li Zefan0caa1022010-12-20 16:30:25 +08001975 return ret;
1976}
1977
Chris Masonac8e9812010-02-28 15:39:26 -05001978static noinline int key_in_sk(struct btrfs_key *key,
1979 struct btrfs_ioctl_search_key *sk)
1980{
Chris Masonabc6e132010-03-18 12:10:08 -04001981 struct btrfs_key test;
1982 int ret;
1983
1984 test.objectid = sk->min_objectid;
1985 test.type = sk->min_type;
1986 test.offset = sk->min_offset;
1987
1988 ret = btrfs_comp_cpu_keys(key, &test);
1989 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001990 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001991
1992 test.objectid = sk->max_objectid;
1993 test.type = sk->max_type;
1994 test.offset = sk->max_offset;
1995
1996 ret = btrfs_comp_cpu_keys(key, &test);
1997 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001998 return 0;
1999 return 1;
2000}
2001
Jeff Mahoneydf397562016-06-21 20:18:21 -04002002static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002003 struct btrfs_key *key,
2004 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002005 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002006 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002007 unsigned long *sk_offset,
2008 int *num_found)
2009{
2010 u64 found_transid;
2011 struct extent_buffer *leaf;
2012 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002013 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002014 unsigned long item_off;
2015 unsigned long item_len;
2016 int nritems;
2017 int i;
2018 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002019 int ret = 0;
2020
2021 leaf = path->nodes[0];
2022 slot = path->slots[0];
2023 nritems = btrfs_header_nritems(leaf);
2024
2025 if (btrfs_header_generation(leaf) > sk->max_transid) {
2026 i = nritems;
2027 goto advance_key;
2028 }
2029 found_transid = btrfs_header_generation(leaf);
2030
2031 for (i = slot; i < nritems; i++) {
2032 item_off = btrfs_item_ptr_offset(leaf, i);
2033 item_len = btrfs_item_size_nr(leaf, i);
2034
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002035 btrfs_item_key_to_cpu(leaf, key, i);
2036 if (!key_in_sk(key, sk))
2037 continue;
2038
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002039 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002040 if (*num_found) {
2041 ret = 1;
2042 goto out;
2043 }
Chris Masonac8e9812010-02-28 15:39:26 -05002044
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002045 /*
2046 * return one empty item back for v1, which does not
2047 * handle -EOVERFLOW
2048 */
2049
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002050 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002051 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002052 ret = -EOVERFLOW;
2053 }
Chris Masonac8e9812010-02-28 15:39:26 -05002054
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002055 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002056 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002057 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002058 }
2059
Chris Masonac8e9812010-02-28 15:39:26 -05002060 sh.objectid = key->objectid;
2061 sh.offset = key->offset;
2062 sh.type = key->type;
2063 sh.len = item_len;
2064 sh.transid = found_transid;
2065
2066 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002067 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2068 ret = -EFAULT;
2069 goto out;
2070 }
2071
Chris Masonac8e9812010-02-28 15:39:26 -05002072 *sk_offset += sizeof(sh);
2073
2074 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002075 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002076 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002077 if (read_extent_buffer_to_user(leaf, up,
2078 item_off, item_len)) {
2079 ret = -EFAULT;
2080 goto out;
2081 }
2082
Chris Masonac8e9812010-02-28 15:39:26 -05002083 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002084 }
Hugo Millse2156862011-05-14 17:43:41 +00002085 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002086
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002087 if (ret) /* -EOVERFLOW from above */
2088 goto out;
2089
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002090 if (*num_found >= sk->nr_items) {
2091 ret = 1;
2092 goto out;
2093 }
Chris Masonac8e9812010-02-28 15:39:26 -05002094 }
2095advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002096 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002097 test.objectid = sk->max_objectid;
2098 test.type = sk->max_type;
2099 test.offset = sk->max_offset;
2100 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2101 ret = 1;
2102 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002103 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002104 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002105 key->offset = 0;
2106 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002107 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002108 key->offset = 0;
2109 key->type = 0;
2110 key->objectid++;
2111 } else
2112 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002113out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002114 /*
2115 * 0: all items from this leaf copied, continue with next
2116 * 1: * more items can be copied, but unused buffer is too small
2117 * * all items were found
2118 * Either way, it will stops the loop which iterates to the next
2119 * leaf
2120 * -EOVERFLOW: item was to large for buffer
2121 * -EFAULT: could not copy extent buffer back to userspace
2122 */
Chris Masonac8e9812010-02-28 15:39:26 -05002123 return ret;
2124}
2125
2126static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002127 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002128 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002129 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002130{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002131 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002132 struct btrfs_root *root;
2133 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002134 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002135 int ret;
2136 int num_found = 0;
2137 unsigned long sk_offset = 0;
2138
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002139 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2140 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002141 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002142 }
Gerhard Heift12544442014-01-30 16:23:58 +01002143
Chris Masonac8e9812010-02-28 15:39:26 -05002144 path = btrfs_alloc_path();
2145 if (!path)
2146 return -ENOMEM;
2147
2148 if (sk->tree_id == 0) {
2149 /* search the root of the inode that was passed */
2150 root = BTRFS_I(inode)->root;
2151 } else {
2152 key.objectid = sk->tree_id;
2153 key.type = BTRFS_ROOT_ITEM_KEY;
2154 key.offset = (u64)-1;
2155 root = btrfs_read_fs_root_no_name(info, &key);
2156 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002157 btrfs_free_path(path);
2158 return -ENOENT;
2159 }
2160 }
2161
2162 key.objectid = sk->min_objectid;
2163 key.type = sk->min_type;
2164 key.offset = sk->min_offset;
2165
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302166 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002167 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002168 if (ret != 0) {
2169 if (ret > 0)
2170 ret = 0;
2171 goto err;
2172 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002173 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002174 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002175 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002176 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002177 break;
2178
2179 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002180 if (ret > 0)
2181 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002182err:
2183 sk->nr_items = num_found;
2184 btrfs_free_path(path);
2185 return ret;
2186}
2187
2188static noinline int btrfs_ioctl_tree_search(struct file *file,
2189 void __user *argp)
2190{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002191 struct btrfs_ioctl_search_args __user *uargs;
2192 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002193 struct inode *inode;
2194 int ret;
2195 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002196
2197 if (!capable(CAP_SYS_ADMIN))
2198 return -EPERM;
2199
Gerhard Heiftba346b32014-01-30 16:24:02 +01002200 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002201
Gerhard Heiftba346b32014-01-30 16:24:02 +01002202 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2203 return -EFAULT;
2204
2205 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002206
Al Viro496ad9a2013-01-23 17:07:38 -05002207 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002208 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002209
2210 /*
2211 * In the origin implementation an overflow is handled by returning a
2212 * search header with a len of zero, so reset ret.
2213 */
2214 if (ret == -EOVERFLOW)
2215 ret = 0;
2216
Gerhard Heiftba346b32014-01-30 16:24:02 +01002217 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002218 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002219 return ret;
2220}
2221
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002222static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2223 void __user *argp)
2224{
2225 struct btrfs_ioctl_search_args_v2 __user *uarg;
2226 struct btrfs_ioctl_search_args_v2 args;
2227 struct inode *inode;
2228 int ret;
2229 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002230 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002231
2232 if (!capable(CAP_SYS_ADMIN))
2233 return -EPERM;
2234
2235 /* copy search header and buffer size */
2236 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2237 if (copy_from_user(&args, uarg, sizeof(args)))
2238 return -EFAULT;
2239
2240 buf_size = args.buf_size;
2241
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002242 /* limit result size to 16MB */
2243 if (buf_size > buf_limit)
2244 buf_size = buf_limit;
2245
2246 inode = file_inode(file);
2247 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002248 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002249 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2250 ret = -EFAULT;
2251 else if (ret == -EOVERFLOW &&
2252 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2253 ret = -EFAULT;
2254
Yan, Zheng76dda932009-09-21 16:00:26 -04002255 return ret;
2256}
2257
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002258/*
Chris Masonac8e9812010-02-28 15:39:26 -05002259 * Search INODE_REFs to identify path name of 'dirid' directory
2260 * in a 'tree_id' tree. and sets path name to 'name'.
2261 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002262static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2263 u64 tree_id, u64 dirid, char *name)
2264{
2265 struct btrfs_root *root;
2266 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002267 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002268 int ret = -1;
2269 int slot;
2270 int len;
2271 int total_len = 0;
2272 struct btrfs_inode_ref *iref;
2273 struct extent_buffer *l;
2274 struct btrfs_path *path;
2275
2276 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2277 name[0]='\0';
2278 return 0;
2279 }
2280
2281 path = btrfs_alloc_path();
2282 if (!path)
2283 return -ENOMEM;
2284
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002285 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002286
2287 key.objectid = tree_id;
2288 key.type = BTRFS_ROOT_ITEM_KEY;
2289 key.offset = (u64)-1;
2290 root = btrfs_read_fs_root_no_name(info, &key);
2291 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002292 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002293 ret = -ENOENT;
2294 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002295 }
2296
2297 key.objectid = dirid;
2298 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002299 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002300
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302301 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2303 if (ret < 0)
2304 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002305 else if (ret > 0) {
2306 ret = btrfs_previous_item(root, path, dirid,
2307 BTRFS_INODE_REF_KEY);
2308 if (ret < 0)
2309 goto out;
2310 else if (ret > 0) {
2311 ret = -ENOENT;
2312 goto out;
2313 }
2314 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002315
2316 l = path->nodes[0];
2317 slot = path->slots[0];
2318 btrfs_item_key_to_cpu(l, &key, slot);
2319
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002320 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2321 len = btrfs_inode_ref_name_len(l, iref);
2322 ptr -= len + 1;
2323 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002324 if (ptr < name) {
2325 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002326 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002327 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002328
2329 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302330 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002331
2332 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2333 break;
2334
David Sterbab3b4aa72011-04-21 01:20:15 +02002335 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002336 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002337 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002338 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002339 }
Li Zefan77906a502011-07-14 03:16:00 +00002340 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302341 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002342 ret = 0;
2343out:
2344 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002345 return ret;
2346}
2347
2348static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2349 void __user *argp)
2350{
2351 struct btrfs_ioctl_ino_lookup_args *args;
2352 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002353 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002354
Julia Lawall2354d08f2010-10-29 15:14:18 -04002355 args = memdup_user(argp, sizeof(*args));
2356 if (IS_ERR(args))
2357 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002358
Al Viro496ad9a2013-01-23 17:07:38 -05002359 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002360
David Sterba01b810b2015-05-12 19:14:49 +02002361 /*
2362 * Unprivileged query to obtain the containing subvolume root id. The
2363 * path is reset so it's consistent with btrfs_search_path_in_tree.
2364 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002365 if (args->treeid == 0)
2366 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2367
David Sterba01b810b2015-05-12 19:14:49 +02002368 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2369 args->name[0] = 0;
2370 goto out;
2371 }
2372
2373 if (!capable(CAP_SYS_ADMIN)) {
2374 ret = -EPERM;
2375 goto out;
2376 }
2377
Chris Masonac8e9812010-02-28 15:39:26 -05002378 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2379 args->treeid, args->objectid,
2380 args->name);
2381
David Sterba01b810b2015-05-12 19:14:49 +02002382out:
Chris Masonac8e9812010-02-28 15:39:26 -05002383 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2384 ret = -EFAULT;
2385
2386 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002387 return ret;
2388}
2389
Yan, Zheng76dda932009-09-21 16:00:26 -04002390static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2391 void __user *arg)
2392{
Al Viro54563d42013-09-01 15:57:51 -04002393 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002394 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002395 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002396 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002397 struct inode *inode;
2398 struct btrfs_root *root = BTRFS_I(dir)->root;
2399 struct btrfs_root *dest = NULL;
2400 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002401 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002402 int err = 0;
2403
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002404 if (!S_ISDIR(dir->i_mode))
2405 return -ENOTDIR;
2406
Yan, Zheng76dda932009-09-21 16:00:26 -04002407 vol_args = memdup_user(arg, sizeof(*vol_args));
2408 if (IS_ERR(vol_args))
2409 return PTR_ERR(vol_args);
2410
2411 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2412 namelen = strlen(vol_args->name);
2413 if (strchr(vol_args->name, '/') ||
2414 strncmp(vol_args->name, "..", namelen) == 0) {
2415 err = -EINVAL;
2416 goto out;
2417 }
2418
Al Viroa561be72011-11-23 11:57:51 -05002419 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002420 if (err)
2421 goto out;
2422
David Sterba521e0542014-04-15 16:41:44 +02002423
Al Viro00235412016-05-26 00:05:12 -04002424 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2425 if (err == -EINTR)
2426 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002427 dentry = lookup_one_len(vol_args->name, parent, namelen);
2428 if (IS_ERR(dentry)) {
2429 err = PTR_ERR(dentry);
2430 goto out_unlock_dir;
2431 }
2432
David Howells2b0143b2015-03-17 22:25:59 +00002433 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002434 err = -ENOENT;
2435 goto out_dput;
2436 }
2437
David Howells2b0143b2015-03-17 22:25:59 +00002438 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002439 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302440 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002441 /*
2442 * Regular user. Only allow this with a special mount
2443 * option, when the user has write+exec access to the
2444 * subvol root, and when rmdir(2) would have been
2445 * allowed.
2446 *
2447 * Note that this is _not_ check that the subvol is
2448 * empty or doesn't contain data that we wouldn't
2449 * otherwise be able to delete.
2450 *
2451 * Users who want to delete empty subvols should try
2452 * rmdir(2).
2453 */
2454 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002455 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002456 goto out_dput;
2457
2458 /*
2459 * Do not allow deletion if the parent dir is the same
2460 * as the dir to be deleted. That means the ioctl
2461 * must be called on the dentry referencing the root
2462 * of the subvol, not a random directory contained
2463 * within it.
2464 */
2465 err = -EINVAL;
2466 if (root == dest)
2467 goto out_dput;
2468
2469 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2470 if (err)
2471 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002472 }
2473
Miao Xie5c39da52012-10-22 11:39:53 +00002474 /* check if subvolume may be deleted by a user */
2475 err = btrfs_may_delete(dir, dentry, 1);
2476 if (err)
2477 goto out_dput;
2478
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002479 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002480 err = -EINVAL;
2481 goto out_dput;
2482 }
2483
Al Viro59551022016-01-22 15:40:57 -05002484 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002485 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002486 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002487 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002488 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002489
Yan, Zheng76dda932009-09-21 16:00:26 -04002490out_dput:
2491 dput(dentry);
2492out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002493 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002494out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002495 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002496out:
2497 kfree(vol_args);
2498 return err;
2499}
2500
Chris Mason1e701a32010-03-11 09:42:04 -05002501static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002502{
Al Viro496ad9a2013-01-23 17:07:38 -05002503 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002504 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002505 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002506 int ret;
2507
Ilya Dryomov25122d12013-01-20 15:57:57 +02002508 ret = mnt_want_write_file(file);
2509 if (ret)
2510 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002511
Ilya Dryomov25122d12013-01-20 15:57:57 +02002512 if (btrfs_root_readonly(root)) {
2513 ret = -EROFS;
2514 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002515 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002516
2517 switch (inode->i_mode & S_IFMT) {
2518 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002519 if (!capable(CAP_SYS_ADMIN)) {
2520 ret = -EPERM;
2521 goto out;
2522 }
Eric Sandeende78b512013-01-31 18:21:12 +00002523 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002524 break;
2525 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002526 if (!(file->f_mode & FMODE_WRITE)) {
2527 ret = -EINVAL;
2528 goto out;
2529 }
Chris Mason1e701a32010-03-11 09:42:04 -05002530
2531 range = kzalloc(sizeof(*range), GFP_KERNEL);
2532 if (!range) {
2533 ret = -ENOMEM;
2534 goto out;
2535 }
2536
2537 if (argp) {
2538 if (copy_from_user(range, argp,
2539 sizeof(*range))) {
2540 ret = -EFAULT;
2541 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002542 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002543 }
2544 /* compression requires us to start the IO */
2545 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2546 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2547 range->extent_thresh = (u32)-1;
2548 }
2549 } else {
2550 /* the rest are all set to zero by kzalloc */
2551 range->len = (u64)-1;
2552 }
Al Viro496ad9a2013-01-23 17:07:38 -05002553 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002554 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002555 if (ret > 0)
2556 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002557 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002558 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002559 default:
2560 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002561 }
Chris Masone441d542009-01-05 16:57:23 -05002562out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002563 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002564 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002565}
2566
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002567static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002568{
2569 struct btrfs_ioctl_vol_args *vol_args;
2570 int ret;
2571
Chris Masone441d542009-01-05 16:57:23 -05002572 if (!capable(CAP_SYS_ADMIN))
2573 return -EPERM;
2574
David Sterba171938e2017-03-28 14:44:21 +02002575 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002576 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002577
Li Zefandae7b662009-04-08 15:06:54 +08002578 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002579 if (IS_ERR(vol_args)) {
2580 ret = PTR_ERR(vol_args);
2581 goto out;
2582 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002583
Mark Fasheh5516e592008-07-24 12:20:14 -04002584 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002585 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002586
Anand Jain43d20762014-07-01 00:58:56 +08002587 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002588 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002589
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002590 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002591out:
David Sterba171938e2017-03-28 14:44:21 +02002592 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002593 return ret;
2594}
2595
Anand Jain6b526ed2016-02-13 10:01:39 +08002596static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002597{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002598 struct inode *inode = file_inode(file);
2599 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002600 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002601 int ret;
2602
Chris Masone441d542009-01-05 16:57:23 -05002603 if (!capable(CAP_SYS_ADMIN))
2604 return -EPERM;
2605
Miao Xieda249272012-11-26 08:44:50 +00002606 ret = mnt_want_write_file(file);
2607 if (ret)
2608 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002609
Li Zefandae7b662009-04-08 15:06:54 +08002610 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002611 if (IS_ERR(vol_args)) {
2612 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002613 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002614 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002615
Anand Jain6b526ed2016-02-13 10:01:39 +08002616 /* Check for compatibility reject unknown flags */
David Sterba735654e2016-02-15 18:15:21 +01002617 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2618 return -EOPNOTSUPP;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002619
David Sterba171938e2017-03-28 14:44:21 +02002620 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002621 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2622 goto out;
2623 }
2624
David Sterba735654e2016-02-15 18:15:21 +01002625 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002626 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002627 } else {
2628 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002629 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002630 }
David Sterba171938e2017-03-28 14:44:21 +02002631 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002632
Anand Jain6b526ed2016-02-13 10:01:39 +08002633 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002634 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002635 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002636 vol_args->devid);
2637 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002638 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002639 vol_args->name);
2640 }
Anand Jain183860f2013-05-17 10:52:45 +00002641out:
2642 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002643err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002644 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002645 return ret;
2646}
2647
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002648static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2649{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 struct inode *inode = file_inode(file);
2651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002652 struct btrfs_ioctl_vol_args *vol_args;
2653 int ret;
2654
2655 if (!capable(CAP_SYS_ADMIN))
2656 return -EPERM;
2657
2658 ret = mnt_want_write_file(file);
2659 if (ret)
2660 return ret;
2661
David Sterba171938e2017-03-28 14:44:21 +02002662 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002663 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002664 goto out_drop_write;
2665 }
2666
2667 vol_args = memdup_user(arg, sizeof(*vol_args));
2668 if (IS_ERR(vol_args)) {
2669 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002670 goto out;
2671 }
2672
David Sterba58d7bbf2016-05-04 14:10:47 +02002673 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002674 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002675
2676 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002677 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002678 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002679out:
David Sterba171938e2017-03-28 14:44:21 +02002680 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002681out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002682 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002683
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002684 return ret;
2685}
2686
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002687static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2688 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002689{
Li Zefan027ed2f2011-06-08 08:27:56 +00002690 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002691 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002692 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002693 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002694
Li Zefan027ed2f2011-06-08 08:27:56 +00002695 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2696 if (!fi_args)
2697 return -ENOMEM;
2698
David Sterbad03262c2017-06-16 00:09:21 +02002699 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00002700 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002701
David Sterbad03262c2017-06-16 00:09:21 +02002702 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002703 if (device->devid > fi_args->max_id)
2704 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002705 }
David Sterbad03262c2017-06-16 00:09:21 +02002706 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002707
David Sterbad03262c2017-06-16 00:09:21 +02002708 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07002709 fi_args->nodesize = fs_info->nodesize;
2710 fi_args->sectorsize = fs_info->sectorsize;
2711 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002712
Li Zefan027ed2f2011-06-08 08:27:56 +00002713 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2714 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002715
Li Zefan027ed2f2011-06-08 08:27:56 +00002716 kfree(fi_args);
2717 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002718}
2719
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002720static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2721 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002722{
2723 struct btrfs_ioctl_dev_info_args *di_args;
2724 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01002725 int ret = 0;
2726 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002727
Jan Schmidt475f6382011-03-11 15:41:01 +01002728 di_args = memdup_user(arg, sizeof(*di_args));
2729 if (IS_ERR(di_args))
2730 return PTR_ERR(di_args);
2731
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002732 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002733 s_uuid = di_args->uuid;
2734
David Sterbac5593ca2017-06-16 00:09:21 +02002735 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002736 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002737
2738 if (!dev) {
2739 ret = -ENODEV;
2740 goto out;
2741 }
2742
2743 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002744 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2745 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002746 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002747 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002748 struct rcu_string *name;
2749
Josef Bacik606686e2012-06-04 14:03:51 -04002750 name = rcu_dereference(dev->name);
Xiongfeng Wang6670d4c2018-01-08 19:51:22 +08002751 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02002752 di_args->path[sizeof(di_args->path) - 1] = 0;
2753 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002754 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002755 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002756
2757out:
David Sterbac5593ca2017-06-16 00:09:21 +02002758 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002759 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2760 ret = -EFAULT;
2761
2762 kfree(di_args);
2763 return ret;
2764}
2765
Mark Fashehf4414602015-06-30 14:42:05 -07002766static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002767{
2768 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002769
Mark Fasheh416161d2013-08-06 11:42:51 -07002770 page = grab_cache_page(inode->i_mapping, index);
2771 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002772 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002773
2774 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002775 int ret;
2776
2777 ret = btrfs_readpage(NULL, page);
2778 if (ret)
2779 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002780 lock_page(page);
2781 if (!PageUptodate(page)) {
2782 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002783 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002784 return ERR_PTR(-EIO);
2785 }
2786 if (page->mapping != inode->i_mapping) {
2787 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002788 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002789 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002790 }
2791 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002792
2793 return page;
2794}
2795
Mark Fashehf4414602015-06-30 14:42:05 -07002796static int gather_extent_pages(struct inode *inode, struct page **pages,
2797 int num_pages, u64 off)
2798{
2799 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002800 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002801
2802 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002803again:
Mark Fashehf4414602015-06-30 14:42:05 -07002804 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002805 if (IS_ERR(pages[i])) {
2806 int err = PTR_ERR(pages[i]);
2807
2808 if (err == -EAGAIN)
2809 goto again;
2810 pages[i] = NULL;
2811 return err;
2812 }
Mark Fashehf4414602015-06-30 14:42:05 -07002813 }
2814 return 0;
2815}
2816
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002817static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2818 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002819{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002820 /*
2821 * Do any pending delalloc/csum calculations on inode, one way or
2822 * another, and lock file content.
2823 * The locking order is:
2824 *
2825 * 1) pages
2826 * 2) range in the inode's io tree
2827 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002828 while (1) {
2829 struct btrfs_ordered_extent *ordered;
2830 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2831 ordered = btrfs_lookup_first_ordered_extent(inode,
2832 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002833 if ((!ordered ||
2834 ordered->file_offset + ordered->len <= off ||
2835 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002836 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002837 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2838 if (ordered)
2839 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002840 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002841 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002842 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2843 if (ordered)
2844 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002845 if (!retry_range_locking)
2846 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002847 btrfs_wait_ordered_range(inode, off, len);
2848 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002849 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002850}
2851
Mark Fashehf4414602015-06-30 14:42:05 -07002852static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002853{
Al Viro59551022016-01-22 15:40:57 -05002854 inode_unlock(inode1);
2855 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002856}
2857
Mark Fashehf4414602015-06-30 14:42:05 -07002858static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2859{
2860 if (inode1 < inode2)
2861 swap(inode1, inode2);
2862
Al Viro59551022016-01-22 15:40:57 -05002863 inode_lock_nested(inode1, I_MUTEX_PARENT);
2864 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002865}
2866
2867static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2868 struct inode *inode2, u64 loff2, u64 len)
2869{
2870 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2871 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2872}
2873
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002874static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2875 struct inode *inode2, u64 loff2, u64 len,
2876 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002877{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002878 int ret;
2879
Mark Fasheh416161d2013-08-06 11:42:51 -07002880 if (inode1 < inode2) {
2881 swap(inode1, inode2);
2882 swap(loff1, loff2);
2883 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002884 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2885 if (ret)
2886 return ret;
2887 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2888 if (ret)
2889 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2890 loff1 + len - 1);
2891 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002892}
2893
2894struct cmp_pages {
2895 int num_pages;
2896 struct page **src_pages;
2897 struct page **dst_pages;
2898};
2899
2900static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2901{
2902 int i;
2903 struct page *pg;
2904
2905 for (i = 0; i < cmp->num_pages; i++) {
2906 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002907 if (pg) {
2908 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002909 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002910 }
Mark Fashehf4414602015-06-30 14:42:05 -07002911 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002912 if (pg) {
2913 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002914 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002915 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002916 }
Mark Fashehf4414602015-06-30 14:42:05 -07002917 kfree(cmp->src_pages);
2918 kfree(cmp->dst_pages);
2919}
2920
2921static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2922 struct inode *dst, u64 dst_loff,
2923 u64 len, struct cmp_pages *cmp)
2924{
2925 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002926 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002927 struct page **src_pgarr, **dst_pgarr;
2928
2929 /*
2930 * We must gather up all the pages before we initiate our
2931 * extent locking. We use an array for the page pointers. Size
2932 * of the array is bounded by len, which is in turn bounded by
2933 * BTRFS_MAX_DEDUPE_LEN.
2934 */
David Sterba66722f72016-02-11 15:01:38 +01002935 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
2936 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07002937 if (!src_pgarr || !dst_pgarr) {
2938 kfree(src_pgarr);
2939 kfree(dst_pgarr);
2940 return -ENOMEM;
2941 }
2942 cmp->num_pages = num_pages;
2943 cmp->src_pages = src_pgarr;
2944 cmp->dst_pages = dst_pgarr;
2945
Filipe Mananab1517622017-02-21 17:14:52 +00002946 /*
2947 * If deduping ranges in the same inode, locking rules make it mandatory
2948 * to always lock pages in ascending order to avoid deadlocks with
2949 * concurrent tasks (such as starting writeback/delalloc).
2950 */
2951 if (src == dst && dst_loff < loff) {
2952 swap(src_pgarr, dst_pgarr);
2953 swap(loff, dst_loff);
2954 }
2955
2956 ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002957 if (ret)
2958 goto out;
2959
Filipe Mananab1517622017-02-21 17:14:52 +00002960 ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002961
2962out:
2963 if (ret)
2964 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09002965 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07002966}
2967
David Sterba1a287cf2017-02-10 20:15:10 +01002968static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002969{
2970 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07002971 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07002972 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002973 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002974 void *addr, *dst_addr;
2975
Mark Fashehf4414602015-06-30 14:42:05 -07002976 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002977 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002978 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07002979 cmp_len = len;
2980
Mark Fashehf4414602015-06-30 14:42:05 -07002981 BUG_ON(i >= cmp->num_pages);
2982
2983 src_page = cmp->src_pages[i];
2984 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002985 ASSERT(PageLocked(src_page));
2986 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07002987
Mark Fasheh416161d2013-08-06 11:42:51 -07002988 addr = kmap_atomic(src_page);
2989 dst_addr = kmap_atomic(dst_page);
2990
2991 flush_dcache_page(src_page);
2992 flush_dcache_page(dst_page);
2993
2994 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08002995 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002996
2997 kunmap_atomic(addr);
2998 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07002999
3000 if (ret)
3001 break;
3002
Mark Fasheh416161d2013-08-06 11:42:51 -07003003 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003004 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003005 }
3006
3007 return ret;
3008}
3009
Mark Fashehe1d227a2015-06-08 15:05:25 -07003010static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3011 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003012{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003013 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003014 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3015
Mark Fashehe1d227a2015-06-08 15:05:25 -07003016 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003017 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003018
3019 /* if we extend to eof, continue to block boundary */
3020 if (off + len == inode->i_size)
3021 *plen = len = ALIGN(inode->i_size, bs) - off;
3022
Mark Fasheh416161d2013-08-06 11:42:51 -07003023 /* Check that we are block aligned - btrfs_clone() requires this */
3024 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3025 return -EINVAL;
3026
3027 return 0;
3028}
3029
Mark Fashehe1d227a2015-06-08 15:05:25 -07003030static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003031 struct inode *dst, u64 dst_loff)
3032{
3033 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003034 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003035 struct cmp_pages cmp;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003036 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003037 u64 same_lock_start = 0;
3038 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003039
Filipe Manana113e8282015-03-30 18:26:47 +01003040 if (len == 0)
3041 return 0;
3042
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003043 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003044 inode_lock(src);
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003045 else
3046 btrfs_double_inode_lock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003047
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003048 ret = extent_same_check_offsets(src, loff, &len, olen);
3049 if (ret)
3050 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003051
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003052 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3053 if (ret)
3054 goto out_unlock;
3055
3056 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003057 /*
3058 * Single inode case wants the same checks, except we
3059 * don't want our length pushed out past i_size as
3060 * comparing that data range makes no sense.
3061 *
3062 * extent_same_check_offsets() will do this for an
3063 * unaligned length at i_size, so catch it here and
3064 * reject the request.
3065 *
3066 * This effectively means we require aligned extents
3067 * for the single-inode case, whereas the other cases
3068 * allow an unaligned length so long as it ends at
3069 * i_size.
3070 */
3071 if (len != olen) {
3072 ret = -EINVAL;
3073 goto out_unlock;
3074 }
3075
3076 /* Check for overlapping ranges */
3077 if (dst_loff + len > loff && dst_loff < loff + len) {
3078 ret = -EINVAL;
3079 goto out_unlock;
3080 }
3081
3082 same_lock_start = min_t(u64, loff, dst_loff);
3083 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003084 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003085
3086 /* don't make the dst file partly checksummed */
3087 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3088 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3089 ret = -EINVAL;
3090 goto out_unlock;
3091 }
3092
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003093again:
Mark Fashehf4414602015-06-30 14:42:05 -07003094 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3095 if (ret)
3096 goto out_unlock;
3097
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003098 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003099 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3100 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003101 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003102 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3103 false);
3104 /*
3105 * If one of the inodes has dirty pages in the respective range or
3106 * ordered extents, we need to flush dellaloc and wait for all ordered
3107 * extents in the range. We must unlock the pages and the ranges in the
3108 * io trees to avoid deadlocks when flushing delalloc (requires locking
3109 * pages) and when waiting for ordered extents to complete (they require
3110 * range locking).
3111 */
3112 if (ret == -EAGAIN) {
3113 /*
3114 * Ranges in the io trees already unlocked. Now unlock all
3115 * pages before waiting for all IO to complete.
3116 */
3117 btrfs_cmp_data_free(&cmp);
3118 if (same_inode) {
3119 btrfs_wait_ordered_range(src, same_lock_start,
3120 same_lock_len);
3121 } else {
3122 btrfs_wait_ordered_range(src, loff, len);
3123 btrfs_wait_ordered_range(dst, dst_loff, len);
3124 }
3125 goto again;
3126 }
3127 ASSERT(ret == 0);
3128 if (WARN_ON(ret)) {
3129 /* ranges in the io trees already unlocked */
3130 btrfs_cmp_data_free(&cmp);
3131 return ret;
3132 }
Mark Fashehf4414602015-06-30 14:42:05 -07003133
Mark Fasheh207910d2015-06-30 14:42:04 -07003134 /* pass original length for comparison so we stay within i_size */
David Sterba1a287cf2017-02-10 20:15:10 +01003135 ret = btrfs_cmp_data(olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003136 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003137 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003138
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003139 if (same_inode)
3140 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3141 same_lock_start + same_lock_len - 1);
3142 else
3143 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003144
3145 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003146out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003147 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003148 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003149 else
3150 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003151
3152 return ret;
3153}
3154
Byongho Leeee221842015-12-15 01:42:10 +09003155#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003156
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003157ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3158 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003159{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003160 struct inode *src = file_inode(src_file);
3161 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003162 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003163 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003164
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003165 if (olen > BTRFS_MAX_DEDUPE_LEN)
3166 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003167
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003168 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003169 /*
3170 * Btrfs does not support blocksize < page_size. As a
3171 * result, btrfs_cmp_data() won't correctly handle
3172 * this situation without an update.
3173 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003174 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003175 }
3176
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003177 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3178 if (res)
3179 return res;
3180 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003181}
3182
Filipe Mananaf82a9902014-06-01 01:50:28 +01003183static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3184 struct inode *inode,
3185 u64 endoff,
3186 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003187 const u64 olen,
3188 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003189{
3190 struct btrfs_root *root = BTRFS_I(inode)->root;
3191 int ret;
3192
3193 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003194 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003195 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003196 /*
3197 * We round up to the block size at eof when determining which
3198 * extents to clone above, but shouldn't round up the file size.
3199 */
3200 if (endoff > destoff + olen)
3201 endoff = destoff + olen;
3202 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003203 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003204
3205 ret = btrfs_update_inode(trans, root, inode);
3206 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003207 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003208 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003209 goto out;
3210 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003211 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003212out:
3213 return ret;
3214}
3215
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003216static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003217 const struct btrfs_trans_handle *trans,
3218 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003219 const u64 hole_offset,
3220 const u64 hole_len)
3221{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003222 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003223 struct extent_map *em;
3224 int ret;
3225
3226 em = alloc_extent_map();
3227 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003228 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003229 return;
3230 }
3231
Filipe Manana14f59792014-06-29 21:45:40 +01003232 if (path) {
3233 struct btrfs_file_extent_item *fi;
3234
3235 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3236 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003237 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003238 em->generation = -1;
3239 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3240 BTRFS_FILE_EXTENT_INLINE)
3241 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003242 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003243 } else {
3244 em->start = hole_offset;
3245 em->len = hole_len;
3246 em->ram_bytes = em->len;
3247 em->orig_start = hole_offset;
3248 em->block_start = EXTENT_MAP_HOLE;
3249 em->block_len = 0;
3250 em->orig_block_len = 0;
3251 em->compress_type = BTRFS_COMPRESS_NONE;
3252 em->generation = trans->transid;
3253 }
3254
3255 while (1) {
3256 write_lock(&em_tree->lock);
3257 ret = add_extent_mapping(em_tree, em, 1);
3258 write_unlock(&em_tree->lock);
3259 if (ret != -EEXIST) {
3260 free_extent_map(em);
3261 break;
3262 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003263 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003264 em->start + em->len - 1, 0);
3265 }
3266
David Sterbaee39b432014-09-30 01:33:33 +02003267 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003268 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003269}
3270
Filipe Manana8039d872015-10-13 15:15:00 +01003271/*
3272 * Make sure we do not end up inserting an inline extent into a file that has
3273 * already other (non-inline) extents. If a file has an inline extent it can
3274 * not have any other extents and the (single) inline extent must start at the
3275 * file offset 0. Failing to respect these rules will lead to file corruption,
3276 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3277 *
3278 * We can have extents that have been already written to disk or we can have
3279 * dirty ranges still in delalloc, in which case the extent maps and items are
3280 * created only when we run delalloc, and the delalloc ranges might fall outside
3281 * the range we are currently locking in the inode's io tree. So we check the
3282 * inode's i_size because of that (i_size updates are done while holding the
3283 * i_mutex, which we are holding here).
3284 * We also check to see if the inode has a size not greater than "datal" but has
3285 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3286 * protected against such concurrent fallocate calls by the i_mutex).
3287 *
3288 * If the file has no extents but a size greater than datal, do not allow the
3289 * copy because we would need turn the inline extent into a non-inline one (even
3290 * with NO_HOLES enabled). If we find our destination inode only has one inline
3291 * extent, just overwrite it with the source inline extent if its size is less
3292 * than the source extent's size, or we could copy the source inline extent's
3293 * data into the destination inode's inline extent if the later is greater then
3294 * the former.
3295 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003296static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003297 struct btrfs_trans_handle *trans,
3298 struct btrfs_path *path,
3299 struct btrfs_key *new_key,
3300 const u64 drop_start,
3301 const u64 datal,
3302 const u64 skip,
3303 const u64 size,
3304 char *inline_data)
3305{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003306 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003307 struct btrfs_root *root = BTRFS_I(dst)->root;
3308 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003309 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003310 int ret;
3311 struct btrfs_key key;
3312
3313 if (new_key->offset > 0)
3314 return -EOPNOTSUPP;
3315
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003316 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003317 key.type = BTRFS_EXTENT_DATA_KEY;
3318 key.offset = 0;
3319 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3320 if (ret < 0) {
3321 return ret;
3322 } else if (ret > 0) {
3323 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3324 ret = btrfs_next_leaf(root, path);
3325 if (ret < 0)
3326 return ret;
3327 else if (ret > 0)
3328 goto copy_inline_extent;
3329 }
3330 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003331 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003332 key.type == BTRFS_EXTENT_DATA_KEY) {
3333 ASSERT(key.offset > 0);
3334 return -EOPNOTSUPP;
3335 }
3336 } else if (i_size_read(dst) <= datal) {
3337 struct btrfs_file_extent_item *ei;
3338 u64 ext_len;
3339
3340 /*
3341 * If the file size is <= datal, make sure there are no other
3342 * extents following (can happen do to an fallocate call with
3343 * the flag FALLOC_FL_KEEP_SIZE).
3344 */
3345 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3346 struct btrfs_file_extent_item);
3347 /*
3348 * If it's an inline extent, it can not have other extents
3349 * following it.
3350 */
3351 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3352 BTRFS_FILE_EXTENT_INLINE)
3353 goto copy_inline_extent;
3354
3355 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3356 if (ext_len > aligned_end)
3357 return -EOPNOTSUPP;
3358
3359 ret = btrfs_next_item(root, path);
3360 if (ret < 0) {
3361 return ret;
3362 } else if (ret == 0) {
3363 btrfs_item_key_to_cpu(path->nodes[0], &key,
3364 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003365 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003366 key.type == BTRFS_EXTENT_DATA_KEY)
3367 return -EOPNOTSUPP;
3368 }
3369 }
3370
3371copy_inline_extent:
3372 /*
3373 * We have no extent items, or we have an extent at offset 0 which may
3374 * or may not be inlined. All these cases are dealt the same way.
3375 */
3376 if (i_size_read(dst) > datal) {
3377 /*
3378 * If the destination inode has an inline extent...
3379 * This would require copying the data from the source inline
3380 * extent into the beginning of the destination's inline extent.
3381 * But this is really complex, both extents can be compressed
3382 * or just one of them, which would require decompressing and
3383 * re-compressing data (which could increase the new compressed
3384 * size, not allowing the compressed data to fit anymore in an
3385 * inline extent).
3386 * So just don't support this case for now (it should be rare,
3387 * we are not really saving space when cloning inline extents).
3388 */
3389 return -EOPNOTSUPP;
3390 }
3391
3392 btrfs_release_path(path);
3393 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3394 if (ret)
3395 return ret;
3396 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3397 if (ret)
3398 return ret;
3399
3400 if (skip) {
3401 const u32 start = btrfs_file_extent_calc_inline_size(0);
3402
3403 memmove(inline_data + start, inline_data + start + skip, datal);
3404 }
3405
3406 write_extent_buffer(path->nodes[0], inline_data,
3407 btrfs_item_ptr_offset(path->nodes[0],
3408 path->slots[0]),
3409 size);
3410 inode_add_bytes(dst, datal);
3411
3412 return 0;
3413}
3414
Mark Fasheh32b7c682013-08-06 11:42:49 -07003415/**
3416 * btrfs_clone() - clone a range from inode file to another
3417 *
3418 * @src: Inode to clone from
3419 * @inode: Inode to clone to
3420 * @off: Offset within source to start clone from
3421 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003422 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003423 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003424 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003425 */
3426static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003427 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003428 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003429{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003430 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003431 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003432 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003433 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003434 struct btrfs_trans_handle *trans;
3435 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003436 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003437 u32 nritems;
3438 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003439 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003440 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003441 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003442
3443 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003444 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3445 if (!buf)
3446 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003447
3448 path = btrfs_alloc_path();
3449 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003450 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003451 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003452 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003453
David Sterbae4058b52015-11-27 16:31:35 +01003454 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003455 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003456 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003457 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003458 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003459
3460 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003461 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003462
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003463 /*
3464 * note the key will change type as we walk through the
3465 * tree.
3466 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003467 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003468 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3469 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003470 if (ret < 0)
3471 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003472 /*
3473 * First search, if no extent item that starts at offset off was
3474 * found but the previous item is an extent item, it's possible
3475 * it might overlap our target range, therefore process it.
3476 */
3477 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3478 btrfs_item_key_to_cpu(path->nodes[0], &key,
3479 path->slots[0] - 1);
3480 if (key.type == BTRFS_EXTENT_DATA_KEY)
3481 path->slots[0]--;
3482 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003483
Yan Zhengae01a0a2008-08-04 23:23:47 -04003484 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003485process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003486 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003487 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003488 if (ret < 0)
3489 goto out;
3490 if (ret > 0)
3491 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003492 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003493 }
3494 leaf = path->nodes[0];
3495 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003496
Yan Zhengae01a0a2008-08-04 23:23:47 -04003497 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003498 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003499 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003500 break;
3501
David Sterba962a2982014-06-04 18:41:45 +02003502 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003503 struct btrfs_file_extent_item *extent;
3504 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003505 u32 size;
3506 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003507 u64 disko = 0, diskl = 0;
3508 u64 datao = 0, datal = 0;
3509 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003510 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003511
Sage Weilc5c9cd42008-11-12 14:32:25 -05003512 extent = btrfs_item_ptr(leaf, slot,
3513 struct btrfs_file_extent_item);
3514 comp = btrfs_file_extent_compression(leaf, extent);
3515 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003516 if (type == BTRFS_FILE_EXTENT_REG ||
3517 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003518 disko = btrfs_file_extent_disk_bytenr(leaf,
3519 extent);
3520 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3521 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003522 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003523 datal = btrfs_file_extent_num_bytes(leaf,
3524 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003525 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3526 /* take upper bound, may be compressed */
3527 datal = btrfs_file_extent_ram_bytes(leaf,
3528 extent);
3529 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003530
Filipe Manana2c463822014-05-31 02:31:05 +01003531 /*
3532 * The first search might have left us at an extent
3533 * item that ends before our target range's start, can
3534 * happen if we have holes and NO_HOLES feature enabled.
3535 */
3536 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003537 path->slots[0]++;
3538 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003539 } else if (key.offset >= off + len) {
3540 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003541 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003542 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003543 size = btrfs_item_size_nr(leaf, slot);
3544 read_extent_buffer(leaf, buf,
3545 btrfs_item_ptr_offset(leaf, slot),
3546 size);
3547
3548 btrfs_release_path(path);
3549 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003550
Zheng Yan31840ae2008-09-23 13:14:14 -04003551 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003552 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003553 if (off <= key.offset)
3554 new_key.offset = key.offset + destoff - off;
3555 else
3556 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003557
Sage Weilb6f34092011-09-20 14:48:51 -04003558 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003559 * Deal with a hole that doesn't have an extent item
3560 * that represents it (NO_HOLES feature enabled).
3561 * This hole is either in the middle of the cloning
3562 * range or at the beginning (fully overlaps it or
3563 * partially overlaps it).
3564 */
3565 if (new_key.offset != last_dest_end)
3566 drop_start = last_dest_end;
3567 else
3568 drop_start = new_key.offset;
3569
3570 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003571 * 1 - adjusting old extent (we may have to split it)
3572 * 1 - add new extent
3573 * 1 - inode update
3574 */
3575 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003576 if (IS_ERR(trans)) {
3577 ret = PTR_ERR(trans);
3578 goto out;
3579 }
3580
Chris Masonc8a894d2009-06-27 21:07:03 -04003581 if (type == BTRFS_FILE_EXTENT_REG ||
3582 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003583 /*
3584 * a | --- range to clone ---| b
3585 * | ------------- extent ------------- |
3586 */
3587
Antonio Ospite93915582014-06-04 14:03:48 +02003588 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003589 if (key.offset + datal > off + len)
3590 datal = off + len - key.offset;
3591
Antonio Ospite93915582014-06-04 14:03:48 +02003592 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003593 if (off > key.offset) {
3594 datao += off - key.offset;
3595 datal -= off - key.offset;
3596 }
3597
Josef Bacik5dc562c2012-08-17 13:14:17 -04003598 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003599 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003600 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003601 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003602 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003603 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003604 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003605 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003606 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003607 goto out;
3608 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003609
Sage Weilc5c9cd42008-11-12 14:32:25 -05003610 ret = btrfs_insert_empty_item(trans, root, path,
3611 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003613 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003614 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003615 goto out;
3616 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003617
3618 leaf = path->nodes[0];
3619 slot = path->slots[0];
3620 write_extent_buffer(leaf, buf,
3621 btrfs_item_ptr_offset(leaf, slot),
3622 size);
3623
3624 extent = btrfs_item_ptr(leaf, slot,
3625 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003626
Sage Weilc5c9cd42008-11-12 14:32:25 -05003627 /* disko == 0 means it's a hole */
3628 if (!disko)
3629 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003630
3631 btrfs_set_file_extent_offset(leaf, extent,
3632 datao);
3633 btrfs_set_file_extent_num_bytes(leaf, extent,
3634 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003635
Sage Weilc5c9cd42008-11-12 14:32:25 -05003636 if (disko) {
3637 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003638 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003639 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003640 disko, diskl, 0,
3641 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003642 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003643 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003644 if (ret) {
3645 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003646 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003647 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003648 goto out;
3649
3650 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003651 }
3652 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3653 u64 skip = 0;
3654 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003655
Sage Weilc5c9cd42008-11-12 14:32:25 -05003656 if (off > key.offset) {
3657 skip = off - key.offset;
3658 new_key.offset += skip;
3659 }
Chris Masond3977122009-01-05 21:25:51 -05003660
Liu Boaa42ffd2012-09-18 03:52:23 -06003661 if (key.offset + datal > off + len)
3662 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003663
Sage Weilc5c9cd42008-11-12 14:32:25 -05003664 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003665 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003666 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003667 goto out;
3668 }
3669 size -= skip + trim;
3670 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003671
David Sterba4a0ab9d2017-02-10 20:18:49 +01003672 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003673 trans, path,
3674 &new_key,
3675 drop_start,
3676 datal,
3677 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003678 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003679 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003680 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003681 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003682 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003683 goto out;
3684 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003685 leaf = path->nodes[0];
3686 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003687 }
3688
Filipe Manana7ffbb592014-06-09 03:48:05 +01003689 /* If we have an implicit hole (NO_HOLES feature). */
3690 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003691 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003692 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003693 new_key.offset - drop_start);
3694
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003695 clone_update_extent_map(BTRFS_I(inode), trans,
3696 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003697
Sage Weilc5c9cd42008-11-12 14:32:25 -05003698 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003699 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003700
Filipe Manana62e23902014-08-08 02:47:06 +01003701 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003702 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003703 ret = clone_finish_inode_update(trans, inode,
3704 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003705 destoff, olen,
3706 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003707 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003708 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003709 if (new_key.offset + datal >= destoff + len)
3710 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003712 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003713 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003714
3715 if (fatal_signal_pending(current)) {
3716 ret = -EINTR;
3717 goto out;
3718 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003719 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003720 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003721
Filipe Mananaf82a9902014-06-01 01:50:28 +01003722 if (last_dest_end < destoff + len) {
3723 /*
3724 * We have an implicit hole (NO_HOLES feature is enabled) that
3725 * fully or partially overlaps our cloning range at its end.
3726 */
3727 btrfs_release_path(path);
3728
3729 /*
3730 * 1 - remove extent(s)
3731 * 1 - inode update
3732 */
3733 trans = btrfs_start_transaction(root, 2);
3734 if (IS_ERR(trans)) {
3735 ret = PTR_ERR(trans);
3736 goto out;
3737 }
3738 ret = btrfs_drop_extents(trans, root, inode,
3739 last_dest_end, destoff + len, 1);
3740 if (ret) {
3741 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003742 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003743 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003744 goto out;
3745 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003746 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3747 last_dest_end,
3748 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003749 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003750 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003751 }
3752
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003753out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003754 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003755 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003756 return ret;
3757}
3758
Zach Brown3db11b22015-11-10 16:53:32 -05003759static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3760 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003761{
Al Viro54563d42013-09-01 15:57:51 -04003762 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003763 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003765 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003766 int ret;
3767 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003768 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003769 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003770
3771 /*
3772 * TODO:
3773 * - split compressed inline extents. annoying: we need to
3774 * decompress into destination's address_space (the file offset
3775 * may change, so source mapping won't do), then recompress (or
3776 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003777 *
3778 * - split destination inode's inline extents. The inline extents can
3779 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003780 */
3781
Mark Fasheh32b7c682013-08-06 11:42:49 -07003782 if (btrfs_root_readonly(root))
3783 return -EROFS;
3784
Zach Brown3db11b22015-11-10 16:53:32 -05003785 if (file_src->f_path.mnt != file->f_path.mnt ||
3786 src->i_sb != inode->i_sb)
3787 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003788
3789 /* don't make the dst file partly checksummed */
3790 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3791 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003792 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003793
Mark Fasheh32b7c682013-08-06 11:42:49 -07003794 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003795 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003796
3797 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003798 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003799 } else {
Al Viro59551022016-01-22 15:40:57 -05003800 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003801 }
3802
3803 /* determine range to clone */
3804 ret = -EINVAL;
3805 if (off + len > src->i_size || off + len < off)
3806 goto out_unlock;
3807 if (len == 0)
3808 olen = len = src->i_size - off;
3809 /* if we extend to eof, continue to block boundary */
3810 if (off + len == src->i_size)
3811 len = ALIGN(src->i_size, bs) - off;
3812
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003813 if (len == 0) {
3814 ret = 0;
3815 goto out_unlock;
3816 }
3817
Mark Fasheh32b7c682013-08-06 11:42:49 -07003818 /* verify the end result is block aligned */
3819 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3820 !IS_ALIGNED(destoff, bs))
3821 goto out_unlock;
3822
3823 /* verify if ranges are overlapped within the same file */
3824 if (same_inode) {
3825 if (destoff + len > off && destoff < off + len)
3826 goto out_unlock;
3827 }
3828
3829 if (destoff > inode->i_size) {
3830 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3831 if (ret)
3832 goto out_unlock;
3833 }
3834
Filipe Mananac125b8b2014-05-23 05:03:34 +01003835 /*
3836 * Lock the target range too. Right after we replace the file extent
3837 * items in the fs tree (which now point to the cloned data), we might
3838 * have a worker replace them with extent items relative to a write
3839 * operation that was issued before this clone operation (i.e. confront
3840 * with inode.c:btrfs_finish_ordered_io).
3841 */
3842 if (same_inode) {
3843 u64 lock_start = min_t(u64, off, destoff);
3844 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003845
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003846 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003847 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003848 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3849 true);
3850 }
3851 ASSERT(ret == 0);
3852 if (WARN_ON(ret)) {
3853 /* ranges in the io trees already unlocked */
3854 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003855 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003856
Mark Fasheh1c919a52015-06-30 14:42:08 -07003857 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003858
Filipe Mananac125b8b2014-05-23 05:03:34 +01003859 if (same_inode) {
3860 u64 lock_start = min_t(u64, off, destoff);
3861 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3862
3863 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3864 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003865 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003866 }
3867 /*
3868 * Truncate page cache pages so that future reads will see the cloned
3869 * data immediately and not the previous data.
3870 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303871 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003872 round_down(destoff, PAGE_SIZE),
3873 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003874out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003875 if (!same_inode)
3876 btrfs_double_inode_unlock(src, inode);
3877 else
Al Viro59551022016-01-22 15:40:57 -05003878 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003879 return ret;
3880}
3881
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003882int btrfs_clone_file_range(struct file *src_file, loff_t off,
3883 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003884{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003885 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003886}
3887
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003888static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3889{
Al Viro496ad9a2013-01-23 17:07:38 -05003890 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003891 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003892 struct btrfs_root *root = BTRFS_I(inode)->root;
3893 struct btrfs_root *new_root;
3894 struct btrfs_dir_item *di;
3895 struct btrfs_trans_handle *trans;
3896 struct btrfs_path *path;
3897 struct btrfs_key location;
3898 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003899 u64 objectid = 0;
3900 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003901 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003902
3903 if (!capable(CAP_SYS_ADMIN))
3904 return -EPERM;
3905
Miao Xie3c04ce02012-11-26 08:43:07 +00003906 ret = mnt_want_write_file(file);
3907 if (ret)
3908 return ret;
3909
3910 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3911 ret = -EFAULT;
3912 goto out;
3913 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003914
3915 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303916 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003917
3918 location.objectid = objectid;
3919 location.type = BTRFS_ROOT_ITEM_KEY;
3920 location.offset = (u64)-1;
3921
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003922 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003923 if (IS_ERR(new_root)) {
3924 ret = PTR_ERR(new_root);
3925 goto out;
3926 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09003927 if (!is_fstree(new_root->objectid)) {
3928 ret = -ENOENT;
3929 goto out;
3930 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003931
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003932 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003933 if (!path) {
3934 ret = -ENOMEM;
3935 goto out;
3936 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003937 path->leave_spinning = 1;
3938
3939 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003940 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003941 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003942 ret = PTR_ERR(trans);
3943 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003944 }
3945
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003946 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3947 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003948 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003949 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003950 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003951 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003952 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003953 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003954 ret = -ENOENT;
3955 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003956 }
3957
3958 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3959 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3960 btrfs_mark_buffer_dirty(path->nodes[0]);
3961 btrfs_free_path(path);
3962
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003963 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003964 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00003965out:
3966 mnt_drop_write_file(file);
3967 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003968}
3969
Su Yuec065f5b12018-04-02 17:24:11 +08003970static void get_block_group_info(struct list_head *groups_list,
3971 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003972{
3973 struct btrfs_block_group_cache *block_group;
3974
3975 space->total_bytes = 0;
3976 space->used_bytes = 0;
3977 space->flags = 0;
3978 list_for_each_entry(block_group, groups_list, list) {
3979 space->flags = block_group->flags;
3980 space->total_bytes += block_group->key.offset;
3981 space->used_bytes +=
3982 btrfs_block_group_used(&block_group->item);
3983 }
3984}
3985
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003986static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3987 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003988{
3989 struct btrfs_ioctl_space_args space_args;
3990 struct btrfs_ioctl_space_info space;
3991 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003992 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003993 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003994 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01003995 static const u64 types[] = {
3996 BTRFS_BLOCK_GROUP_DATA,
3997 BTRFS_BLOCK_GROUP_SYSTEM,
3998 BTRFS_BLOCK_GROUP_METADATA,
3999 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4000 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004001 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004002 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004003 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004004 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004005 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004006
4007 if (copy_from_user(&space_args,
4008 (struct btrfs_ioctl_space_args __user *)arg,
4009 sizeof(space_args)))
4010 return -EFAULT;
4011
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004012 for (i = 0; i < num_types; i++) {
4013 struct btrfs_space_info *tmp;
4014
4015 info = NULL;
4016 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004017 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004018 list) {
4019 if (tmp->flags == types[i]) {
4020 info = tmp;
4021 break;
4022 }
4023 }
4024 rcu_read_unlock();
4025
4026 if (!info)
4027 continue;
4028
4029 down_read(&info->groups_sem);
4030 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4031 if (!list_empty(&info->block_groups[c]))
4032 slot_count++;
4033 }
4034 up_read(&info->groups_sem);
4035 }
Josef Bacik1406e432010-01-13 18:19:06 +00004036
David Sterba36523e952014-02-07 14:34:12 +01004037 /*
4038 * Global block reserve, exported as a space_info
4039 */
4040 slot_count++;
4041
Chris Mason7fde62b2010-03-16 15:40:10 -04004042 /* space_slots == 0 means they are asking for a count */
4043 if (space_args.space_slots == 0) {
4044 space_args.total_spaces = slot_count;
4045 goto out;
4046 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004047
Dan Rosenberg51788b12011-02-14 16:04:23 -05004048 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004049
Chris Mason7fde62b2010-03-16 15:40:10 -04004050 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004051
Chris Mason7fde62b2010-03-16 15:40:10 -04004052 /* we generally have at most 6 or so space infos, one for each raid
4053 * level. So, a whole page should be more than enough for everyone
4054 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004055 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004056 return -ENOMEM;
4057
4058 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004059 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004060 if (!dest)
4061 return -ENOMEM;
4062 dest_orig = dest;
4063
4064 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004065 for (i = 0; i < num_types; i++) {
4066 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004067
Dan Rosenberg51788b12011-02-14 16:04:23 -05004068 if (!slot_count)
4069 break;
4070
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004071 info = NULL;
4072 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004073 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004074 list) {
4075 if (tmp->flags == types[i]) {
4076 info = tmp;
4077 break;
4078 }
4079 }
4080 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004081
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004082 if (!info)
4083 continue;
4084 down_read(&info->groups_sem);
4085 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4086 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004087 get_block_group_info(&info->block_groups[c],
4088 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004089 memcpy(dest, &space, sizeof(space));
4090 dest++;
4091 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004092 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004093 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004094 if (!slot_count)
4095 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004096 }
4097 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004098 }
Josef Bacik1406e432010-01-13 18:19:06 +00004099
David Sterba36523e952014-02-07 14:34:12 +01004100 /*
4101 * Add global block reserve
4102 */
4103 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004104 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004105
4106 spin_lock(&block_rsv->lock);
4107 space.total_bytes = block_rsv->size;
4108 space.used_bytes = block_rsv->size - block_rsv->reserved;
4109 spin_unlock(&block_rsv->lock);
4110 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4111 memcpy(dest, &space, sizeof(space));
4112 space_args.total_spaces++;
4113 }
4114
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004115 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004116 (arg + sizeof(struct btrfs_ioctl_space_args));
4117
4118 if (copy_to_user(user_dest, dest_orig, alloc_size))
4119 ret = -EFAULT;
4120
4121 kfree(dest_orig);
4122out:
4123 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004124 ret = -EFAULT;
4125
4126 return ret;
4127}
4128
Miao Xie9a8c28b2012-11-26 08:40:43 +00004129static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4130 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004131{
Sage Weil46204592010-10-29 15:41:32 -04004132 struct btrfs_trans_handle *trans;
4133 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004134 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004135
Miao Xied4edf392013-02-20 09:17:06 +00004136 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004137 if (IS_ERR(trans)) {
4138 if (PTR_ERR(trans) != -ENOENT)
4139 return PTR_ERR(trans);
4140
4141 /* No running transaction, don't bother */
4142 transid = root->fs_info->last_trans_committed;
4143 goto out;
4144 }
Sage Weil46204592010-10-29 15:41:32 -04004145 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004146 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004147 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004148 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004149 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004150 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004151out:
Sage Weil46204592010-10-29 15:41:32 -04004152 if (argp)
4153 if (copy_to_user(argp, &transid, sizeof(transid)))
4154 return -EFAULT;
4155 return 0;
4156}
4157
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004158static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004159 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004160{
Sage Weil46204592010-10-29 15:41:32 -04004161 u64 transid;
4162
4163 if (argp) {
4164 if (copy_from_user(&transid, argp, sizeof(transid)))
4165 return -EFAULT;
4166 } else {
4167 transid = 0; /* current trans */
4168 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004169 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004170}
4171
Miao Xieb8e95482012-11-26 08:48:01 +00004172static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004173{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004174 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004175 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004176 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004177
4178 if (!capable(CAP_SYS_ADMIN))
4179 return -EPERM;
4180
4181 sa = memdup_user(arg, sizeof(*sa));
4182 if (IS_ERR(sa))
4183 return PTR_ERR(sa);
4184
Miao Xieb8e95482012-11-26 08:48:01 +00004185 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4186 ret = mnt_want_write_file(file);
4187 if (ret)
4188 goto out;
4189 }
4190
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004191 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004192 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4193 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004194
4195 if (copy_to_user(arg, sa, sizeof(*sa)))
4196 ret = -EFAULT;
4197
Miao Xieb8e95482012-11-26 08:48:01 +00004198 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4199 mnt_drop_write_file(file);
4200out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004201 kfree(sa);
4202 return ret;
4203}
4204
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004205static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004206{
4207 if (!capable(CAP_SYS_ADMIN))
4208 return -EPERM;
4209
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004210 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004211}
4212
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004213static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004214 void __user *arg)
4215{
4216 struct btrfs_ioctl_scrub_args *sa;
4217 int ret;
4218
4219 if (!capable(CAP_SYS_ADMIN))
4220 return -EPERM;
4221
4222 sa = memdup_user(arg, sizeof(*sa));
4223 if (IS_ERR(sa))
4224 return PTR_ERR(sa);
4225
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004226 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004227
4228 if (copy_to_user(arg, sa, sizeof(*sa)))
4229 ret = -EFAULT;
4230
4231 kfree(sa);
4232 return ret;
4233}
4234
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004235static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004236 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004237{
4238 struct btrfs_ioctl_get_dev_stats *sa;
4239 int ret;
4240
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004241 sa = memdup_user(arg, sizeof(*sa));
4242 if (IS_ERR(sa))
4243 return PTR_ERR(sa);
4244
David Sterbab27f7c02012-06-22 06:30:39 -06004245 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4246 kfree(sa);
4247 return -EPERM;
4248 }
4249
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004250 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004251
4252 if (copy_to_user(arg, sa, sizeof(*sa)))
4253 ret = -EFAULT;
4254
4255 kfree(sa);
4256 return ret;
4257}
4258
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004259static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4260 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004261{
4262 struct btrfs_ioctl_dev_replace_args *p;
4263 int ret;
4264
4265 if (!capable(CAP_SYS_ADMIN))
4266 return -EPERM;
4267
4268 p = memdup_user(arg, sizeof(*p));
4269 if (IS_ERR(p))
4270 return PTR_ERR(p);
4271
4272 switch (p->cmd) {
4273 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004274 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004275 ret = -EROFS;
4276 goto out;
4277 }
David Sterba171938e2017-03-28 14:44:21 +02004278 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004279 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004280 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004281 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004282 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004283 }
4284 break;
4285 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004286 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004287 ret = 0;
4288 break;
4289 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004290 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004291 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004292 break;
4293 default:
4294 ret = -EINVAL;
4295 break;
4296 }
4297
4298 if (copy_to_user(arg, p, sizeof(*p)))
4299 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004300out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004301 kfree(p);
4302 return ret;
4303}
4304
Jan Schmidtd7728c92011-07-07 16:48:38 +02004305static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4306{
4307 int ret = 0;
4308 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004309 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004310 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004311 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004312 struct inode_fs_paths *ipath = NULL;
4313 struct btrfs_path *path;
4314
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004315 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004316 return -EPERM;
4317
4318 path = btrfs_alloc_path();
4319 if (!path) {
4320 ret = -ENOMEM;
4321 goto out;
4322 }
4323
4324 ipa = memdup_user(arg, sizeof(*ipa));
4325 if (IS_ERR(ipa)) {
4326 ret = PTR_ERR(ipa);
4327 ipa = NULL;
4328 goto out;
4329 }
4330
4331 size = min_t(u32, ipa->size, 4096);
4332 ipath = init_ipath(size, root, path);
4333 if (IS_ERR(ipath)) {
4334 ret = PTR_ERR(ipath);
4335 ipath = NULL;
4336 goto out;
4337 }
4338
4339 ret = paths_from_inode(ipa->inum, ipath);
4340 if (ret < 0)
4341 goto out;
4342
4343 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004344 rel_ptr = ipath->fspath->val[i] -
4345 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004346 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004347 }
4348
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004349 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4350 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004351 if (ret) {
4352 ret = -EFAULT;
4353 goto out;
4354 }
4355
4356out:
4357 btrfs_free_path(path);
4358 free_ipath(ipath);
4359 kfree(ipa);
4360
4361 return ret;
4362}
4363
4364static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4365{
4366 struct btrfs_data_container *inodes = ctx;
4367 const size_t c = 3 * sizeof(u64);
4368
4369 if (inodes->bytes_left >= c) {
4370 inodes->bytes_left -= c;
4371 inodes->val[inodes->elem_cnt] = inum;
4372 inodes->val[inodes->elem_cnt + 1] = offset;
4373 inodes->val[inodes->elem_cnt + 2] = root;
4374 inodes->elem_cnt += 3;
4375 } else {
4376 inodes->bytes_missing += c - inodes->bytes_left;
4377 inodes->bytes_left = 0;
4378 inodes->elem_missed += 3;
4379 }
4380
4381 return 0;
4382}
4383
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004384static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004385 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004386{
4387 int ret = 0;
4388 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004389 struct btrfs_ioctl_logical_ino_args *loi;
4390 struct btrfs_data_container *inodes = NULL;
4391 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004392 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004393
4394 if (!capable(CAP_SYS_ADMIN))
4395 return -EPERM;
4396
4397 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304398 if (IS_ERR(loi))
4399 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004400
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004401 if (version == 1) {
4402 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004403 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004404 } else {
4405 /* All reserved bits must be 0 for now */
4406 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4407 ret = -EINVAL;
4408 goto out_loi;
4409 }
4410 /* Only accept flags we have defined so far */
4411 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4412 ret = -EINVAL;
4413 goto out_loi;
4414 }
4415 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004416 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004417 }
4418
Jan Schmidtd7728c92011-07-07 16:48:38 +02004419 path = btrfs_alloc_path();
4420 if (!path) {
4421 ret = -ENOMEM;
4422 goto out;
4423 }
4424
Jan Schmidtd7728c92011-07-07 16:48:38 +02004425 inodes = init_data_container(size);
4426 if (IS_ERR(inodes)) {
4427 ret = PTR_ERR(inodes);
4428 inodes = NULL;
4429 goto out;
4430 }
4431
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004432 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004433 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004434 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004435 ret = -ENOENT;
4436 if (ret < 0)
4437 goto out;
4438
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004439 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4440 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004441 if (ret)
4442 ret = -EFAULT;
4443
4444out:
4445 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004446 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004447out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004448 kfree(loi);
4449
4450 return ret;
4451}
4452
David Sterba008ef092018-03-21 02:05:27 +01004453void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004454 struct btrfs_ioctl_balance_args *bargs)
4455{
4456 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4457
4458 bargs->flags = bctl->flags;
4459
David Sterba3009a622018-03-21 01:31:04 +01004460 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004461 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4462 if (atomic_read(&fs_info->balance_pause_req))
4463 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004464 if (atomic_read(&fs_info->balance_cancel_req))
4465 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004466
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004467 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4468 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4469 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004470
David Sterba008ef092018-03-21 02:05:27 +01004471 spin_lock(&fs_info->balance_lock);
4472 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4473 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004474}
4475
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004476static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004477{
Al Viro496ad9a2013-01-23 17:07:38 -05004478 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004479 struct btrfs_fs_info *fs_info = root->fs_info;
4480 struct btrfs_ioctl_balance_args *bargs;
4481 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004482 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004483 int ret;
4484
4485 if (!capable(CAP_SYS_ADMIN))
4486 return -EPERM;
4487
Liu Boe54bfa312012-06-29 03:58:48 -06004488 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004489 if (ret)
4490 return ret;
4491
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004492again:
David Sterba171938e2017-03-28 14:44:21 +02004493 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004494 mutex_lock(&fs_info->balance_mutex);
4495 need_unlock = true;
4496 goto locked;
4497 }
4498
4499 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004500 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004501 * (1) some other op is running
4502 * (2) balance is running
4503 * (3) balance is paused -- special case (think resume)
4504 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004505 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004506 if (fs_info->balance_ctl) {
4507 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004508 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004509 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004510 /*
4511 * Lock released to allow other waiters to continue,
4512 * we'll reexamine the status again.
4513 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004514 mutex_lock(&fs_info->balance_mutex);
4515
4516 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004517 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004518 /* this is (3) */
4519 need_unlock = false;
4520 goto locked;
4521 }
4522
4523 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004524 goto again;
4525 } else {
4526 /* this is (2) */
4527 mutex_unlock(&fs_info->balance_mutex);
4528 ret = -EINPROGRESS;
4529 goto out;
4530 }
4531 } else {
4532 /* this is (1) */
4533 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004534 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004535 goto out;
4536 }
4537
4538locked:
David Sterba171938e2017-03-28 14:44:21 +02004539 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004540
4541 if (arg) {
4542 bargs = memdup_user(arg, sizeof(*bargs));
4543 if (IS_ERR(bargs)) {
4544 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004545 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004546 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004547
4548 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4549 if (!fs_info->balance_ctl) {
4550 ret = -ENOTCONN;
4551 goto out_bargs;
4552 }
4553
4554 bctl = fs_info->balance_ctl;
4555 spin_lock(&fs_info->balance_lock);
4556 bctl->flags |= BTRFS_BALANCE_RESUME;
4557 spin_unlock(&fs_info->balance_lock);
4558
4559 goto do_balance;
4560 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004561 } else {
4562 bargs = NULL;
4563 }
4564
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004565 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004566 ret = -EINPROGRESS;
4567 goto out_bargs;
4568 }
4569
David Sterba8d2db782015-11-04 15:38:29 +01004570 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004571 if (!bctl) {
4572 ret = -ENOMEM;
4573 goto out_bargs;
4574 }
4575
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004576 if (arg) {
4577 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4578 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4579 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4580
4581 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004582 } else {
4583 /* balance everything - no filters */
4584 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004585 }
4586
David Sterba8eb93452015-10-12 16:55:54 +02004587 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4588 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004589 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004590 }
4591
Ilya Dryomovde322262012-01-16 22:04:49 +02004592do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004593 /*
David Sterba149196a2018-03-20 20:23:09 +01004594 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4595 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4596 * restriper was paused all the way until unmount, in free_fs_info.
4597 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004598 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004599 need_unlock = false;
4600
David Sterba6fcf6e22018-05-07 17:44:03 +02004601 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004602 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004603
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004604 if (arg) {
4605 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4606 ret = -EFAULT;
4607 }
4608
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004609out_bctl:
4610 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004611out_bargs:
4612 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004613out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004614 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004615 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004616 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004617out:
Liu Boe54bfa312012-06-29 03:58:48 -06004618 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004619 return ret;
4620}
4621
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004622static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004623{
4624 if (!capable(CAP_SYS_ADMIN))
4625 return -EPERM;
4626
4627 switch (cmd) {
4628 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004629 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004630 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004631 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004632 }
4633
4634 return -EINVAL;
4635}
4636
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004637static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004638 void __user *arg)
4639{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004640 struct btrfs_ioctl_balance_args *bargs;
4641 int ret = 0;
4642
4643 if (!capable(CAP_SYS_ADMIN))
4644 return -EPERM;
4645
4646 mutex_lock(&fs_info->balance_mutex);
4647 if (!fs_info->balance_ctl) {
4648 ret = -ENOTCONN;
4649 goto out;
4650 }
4651
David Sterba8d2db782015-11-04 15:38:29 +01004652 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004653 if (!bargs) {
4654 ret = -ENOMEM;
4655 goto out;
4656 }
4657
David Sterba008ef092018-03-21 02:05:27 +01004658 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004659
4660 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4661 ret = -EFAULT;
4662
4663 kfree(bargs);
4664out:
4665 mutex_unlock(&fs_info->balance_mutex);
4666 return ret;
4667}
4668
Miao Xie905b0dd2012-11-26 08:50:11 +00004669static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004670{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004671 struct inode *inode = file_inode(file);
4672 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004673 struct btrfs_ioctl_quota_ctl_args *sa;
4674 struct btrfs_trans_handle *trans = NULL;
4675 int ret;
4676 int err;
4677
4678 if (!capable(CAP_SYS_ADMIN))
4679 return -EPERM;
4680
Miao Xie905b0dd2012-11-26 08:50:11 +00004681 ret = mnt_want_write_file(file);
4682 if (ret)
4683 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004684
4685 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004686 if (IS_ERR(sa)) {
4687 ret = PTR_ERR(sa);
4688 goto drop_write;
4689 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004690
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004691 down_write(&fs_info->subvol_sem);
4692 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004693 if (IS_ERR(trans)) {
4694 ret = PTR_ERR(trans);
4695 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004696 }
4697
4698 switch (sa->cmd) {
4699 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004700 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004701 break;
4702 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004703 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004704 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004705 default:
4706 ret = -EINVAL;
4707 break;
4708 }
4709
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004710 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004711 if (err && !ret)
4712 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004713out:
4714 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004715 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004716drop_write:
4717 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004718 return ret;
4719}
4720
Miao Xie905b0dd2012-11-26 08:50:11 +00004721static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004722{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004723 struct inode *inode = file_inode(file);
4724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4725 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004726 struct btrfs_ioctl_qgroup_assign_args *sa;
4727 struct btrfs_trans_handle *trans;
4728 int ret;
4729 int err;
4730
4731 if (!capable(CAP_SYS_ADMIN))
4732 return -EPERM;
4733
Miao Xie905b0dd2012-11-26 08:50:11 +00004734 ret = mnt_want_write_file(file);
4735 if (ret)
4736 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004737
4738 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004739 if (IS_ERR(sa)) {
4740 ret = PTR_ERR(sa);
4741 goto drop_write;
4742 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004743
4744 trans = btrfs_join_transaction(root);
4745 if (IS_ERR(trans)) {
4746 ret = PTR_ERR(trans);
4747 goto out;
4748 }
4749
Arne Jansen5d13a372011-09-14 15:53:51 +02004750 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004751 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004752 sa->src, sa->dst);
4753 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004754 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004755 sa->src, sa->dst);
4756 }
4757
Qu Wenruoe082f562015-02-27 16:24:28 +08004758 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004759 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004760 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004761 btrfs_handle_fs_error(fs_info, err,
4762 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004763 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004764 if (err && !ret)
4765 ret = err;
4766
4767out:
4768 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004769drop_write:
4770 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004771 return ret;
4772}
4773
Miao Xie905b0dd2012-11-26 08:50:11 +00004774static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004775{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004776 struct inode *inode = file_inode(file);
4777 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4778 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004779 struct btrfs_ioctl_qgroup_create_args *sa;
4780 struct btrfs_trans_handle *trans;
4781 int ret;
4782 int err;
4783
4784 if (!capable(CAP_SYS_ADMIN))
4785 return -EPERM;
4786
Miao Xie905b0dd2012-11-26 08:50:11 +00004787 ret = mnt_want_write_file(file);
4788 if (ret)
4789 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004790
4791 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004792 if (IS_ERR(sa)) {
4793 ret = PTR_ERR(sa);
4794 goto drop_write;
4795 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004796
Miao Xied86e56c2012-11-15 11:35:41 +00004797 if (!sa->qgroupid) {
4798 ret = -EINVAL;
4799 goto out;
4800 }
4801
Arne Jansen5d13a372011-09-14 15:53:51 +02004802 trans = btrfs_join_transaction(root);
4803 if (IS_ERR(trans)) {
4804 ret = PTR_ERR(trans);
4805 goto out;
4806 }
4807
Arne Jansen5d13a372011-09-14 15:53:51 +02004808 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004809 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004810 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004811 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004812 }
4813
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004814 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004815 if (err && !ret)
4816 ret = err;
4817
4818out:
4819 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004820drop_write:
4821 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004822 return ret;
4823}
4824
Miao Xie905b0dd2012-11-26 08:50:11 +00004825static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004826{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004827 struct inode *inode = file_inode(file);
4828 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4829 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004830 struct btrfs_ioctl_qgroup_limit_args *sa;
4831 struct btrfs_trans_handle *trans;
4832 int ret;
4833 int err;
4834 u64 qgroupid;
4835
4836 if (!capable(CAP_SYS_ADMIN))
4837 return -EPERM;
4838
Miao Xie905b0dd2012-11-26 08:50:11 +00004839 ret = mnt_want_write_file(file);
4840 if (ret)
4841 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004842
4843 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004844 if (IS_ERR(sa)) {
4845 ret = PTR_ERR(sa);
4846 goto drop_write;
4847 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004848
4849 trans = btrfs_join_transaction(root);
4850 if (IS_ERR(trans)) {
4851 ret = PTR_ERR(trans);
4852 goto out;
4853 }
4854
4855 qgroupid = sa->qgroupid;
4856 if (!qgroupid) {
4857 /* take the current subvol as qgroup */
4858 qgroupid = root->root_key.objectid;
4859 }
4860
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004861 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004862
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004863 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004864 if (err && !ret)
4865 ret = err;
4866
4867out:
4868 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004869drop_write:
4870 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004871 return ret;
4872}
4873
Jan Schmidt2f232032013-04-25 16:04:51 +00004874static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4875{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 struct inode *inode = file_inode(file);
4877 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004878 struct btrfs_ioctl_quota_rescan_args *qsa;
4879 int ret;
4880
4881 if (!capable(CAP_SYS_ADMIN))
4882 return -EPERM;
4883
4884 ret = mnt_want_write_file(file);
4885 if (ret)
4886 return ret;
4887
4888 qsa = memdup_user(arg, sizeof(*qsa));
4889 if (IS_ERR(qsa)) {
4890 ret = PTR_ERR(qsa);
4891 goto drop_write;
4892 }
4893
4894 if (qsa->flags) {
4895 ret = -EINVAL;
4896 goto out;
4897 }
4898
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004899 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004900
4901out:
4902 kfree(qsa);
4903drop_write:
4904 mnt_drop_write_file(file);
4905 return ret;
4906}
4907
4908static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4909{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004910 struct inode *inode = file_inode(file);
4911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004912 struct btrfs_ioctl_quota_rescan_args *qsa;
4913 int ret = 0;
4914
4915 if (!capable(CAP_SYS_ADMIN))
4916 return -EPERM;
4917
David Sterba8d2db782015-11-04 15:38:29 +01004918 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004919 if (!qsa)
4920 return -ENOMEM;
4921
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004922 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00004923 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004924 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004925 }
4926
4927 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4928 ret = -EFAULT;
4929
4930 kfree(qsa);
4931 return ret;
4932}
4933
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004934static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004936 struct inode *inode = file_inode(file);
4937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004938
4939 if (!capable(CAP_SYS_ADMIN))
4940 return -EPERM;
4941
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004942 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004943}
4944
Hugo Millsabccd002014-01-30 20:17:00 +00004945static long _btrfs_ioctl_set_received_subvol(struct file *file,
4946 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004947{
Al Viro496ad9a2013-01-23 17:07:38 -05004948 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004949 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004950 struct btrfs_root *root = BTRFS_I(inode)->root;
4951 struct btrfs_root_item *root_item = &root->root_item;
4952 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004953 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004954 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004955 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004956
David Sterbabd60ea02014-01-16 15:50:22 +01004957 if (!inode_owner_or_capable(inode))
4958 return -EPERM;
4959
Alexander Block8ea05e32012-07-25 17:35:53 +02004960 ret = mnt_want_write_file(file);
4961 if (ret < 0)
4962 return ret;
4963
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004964 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004965
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004966 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004967 ret = -EINVAL;
4968 goto out;
4969 }
4970
4971 if (btrfs_root_readonly(root)) {
4972 ret = -EROFS;
4973 goto out;
4974 }
4975
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004976 /*
4977 * 1 - root item
4978 * 2 - uuid items (received uuid + subvol uuid)
4979 */
4980 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004981 if (IS_ERR(trans)) {
4982 ret = PTR_ERR(trans);
4983 trans = NULL;
4984 goto out;
4985 }
4986
4987 sa->rtransid = trans->transid;
4988 sa->rtime.sec = ct.tv_sec;
4989 sa->rtime.nsec = ct.tv_nsec;
4990
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004991 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4992 BTRFS_UUID_SIZE);
4993 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004994 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4995 ret = btrfs_uuid_tree_rem(trans, fs_info,
4996 root_item->received_uuid,
4997 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4998 root->root_key.objectid);
4999 if (ret && ret != -ENOENT) {
5000 btrfs_abort_transaction(trans, ret);
5001 btrfs_end_transaction(trans);
5002 goto out;
5003 }
5004 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005005 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5006 btrfs_set_root_stransid(root_item, sa->stransid);
5007 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005008 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5009 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5010 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5011 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005012
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005013 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005014 &root->root_key, &root->root_item);
5015 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005016 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005017 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005018 }
5019 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005020 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005021 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5022 root->root_key.objectid);
5023 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005024 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005025 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005026 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005027 }
5028 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005029 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005030out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005031 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005032 mnt_drop_write_file(file);
5033 return ret;
5034}
5035
5036#ifdef CONFIG_64BIT
5037static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5038 void __user *arg)
5039{
5040 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5041 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5042 int ret = 0;
5043
5044 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305045 if (IS_ERR(args32))
5046 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005047
David Sterba8d2db782015-11-04 15:38:29 +01005048 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005049 if (!args64) {
5050 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005051 goto out;
5052 }
5053
5054 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5055 args64->stransid = args32->stransid;
5056 args64->rtransid = args32->rtransid;
5057 args64->stime.sec = args32->stime.sec;
5058 args64->stime.nsec = args32->stime.nsec;
5059 args64->rtime.sec = args32->rtime.sec;
5060 args64->rtime.nsec = args32->rtime.nsec;
5061 args64->flags = args32->flags;
5062
5063 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5064 if (ret)
5065 goto out;
5066
5067 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5068 args32->stransid = args64->stransid;
5069 args32->rtransid = args64->rtransid;
5070 args32->stime.sec = args64->stime.sec;
5071 args32->stime.nsec = args64->stime.nsec;
5072 args32->rtime.sec = args64->rtime.sec;
5073 args32->rtime.nsec = args64->rtime.nsec;
5074 args32->flags = args64->flags;
5075
5076 ret = copy_to_user(arg, args32, sizeof(*args32));
5077 if (ret)
5078 ret = -EFAULT;
5079
5080out:
5081 kfree(args32);
5082 kfree(args64);
5083 return ret;
5084}
5085#endif
5086
5087static long btrfs_ioctl_set_received_subvol(struct file *file,
5088 void __user *arg)
5089{
5090 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5091 int ret = 0;
5092
5093 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305094 if (IS_ERR(sa))
5095 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005096
5097 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5098
5099 if (ret)
5100 goto out;
5101
Alexander Block8ea05e32012-07-25 17:35:53 +02005102 ret = copy_to_user(arg, sa, sizeof(*sa));
5103 if (ret)
5104 ret = -EFAULT;
5105
5106out:
5107 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005108 return ret;
5109}
5110
jeff.liu867ab662013-01-05 02:48:01 +00005111static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5112{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005113 struct inode *inode = file_inode(file);
5114 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005115 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005116 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005117 char label[BTRFS_LABEL_SIZE];
5118
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005119 spin_lock(&fs_info->super_lock);
5120 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5121 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005122
5123 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005124
5125 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005126 btrfs_warn(fs_info,
5127 "label is too long, return the first %zu bytes",
5128 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005129 }
5130
jeff.liu867ab662013-01-05 02:48:01 +00005131 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005132
5133 return ret ? -EFAULT : 0;
5134}
5135
jeff.liua8bfd4a2013-01-05 02:48:08 +00005136static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5137{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005138 struct inode *inode = file_inode(file);
5139 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5140 struct btrfs_root *root = BTRFS_I(inode)->root;
5141 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005142 struct btrfs_trans_handle *trans;
5143 char label[BTRFS_LABEL_SIZE];
5144 int ret;
5145
5146 if (!capable(CAP_SYS_ADMIN))
5147 return -EPERM;
5148
5149 if (copy_from_user(label, arg, sizeof(label)))
5150 return -EFAULT;
5151
5152 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005153 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005154 "unable to set label with more than %d bytes",
5155 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005156 return -EINVAL;
5157 }
5158
5159 ret = mnt_want_write_file(file);
5160 if (ret)
5161 return ret;
5162
jeff.liua8bfd4a2013-01-05 02:48:08 +00005163 trans = btrfs_start_transaction(root, 0);
5164 if (IS_ERR(trans)) {
5165 ret = PTR_ERR(trans);
5166 goto out_unlock;
5167 }
5168
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005169 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005170 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005171 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005172 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005173
5174out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005175 mnt_drop_write_file(file);
5176 return ret;
5177}
5178
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005179#define INIT_FEATURE_FLAGS(suffix) \
5180 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5181 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5182 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5183
David Sterbad5131b62016-02-17 15:26:27 +01005184int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005185{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005186 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005187 INIT_FEATURE_FLAGS(SUPP),
5188 INIT_FEATURE_FLAGS(SAFE_SET),
5189 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5190 };
5191
5192 if (copy_to_user(arg, &features, sizeof(features)))
5193 return -EFAULT;
5194
5195 return 0;
5196}
5197
5198static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5199{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005200 struct inode *inode = file_inode(file);
5201 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5202 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005203 struct btrfs_ioctl_feature_flags features;
5204
5205 features.compat_flags = btrfs_super_compat_flags(super_block);
5206 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5207 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5208
5209 if (copy_to_user(arg, &features, sizeof(features)))
5210 return -EFAULT;
5211
5212 return 0;
5213}
5214
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005215static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005216 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005217 u64 change_mask, u64 flags, u64 supported_flags,
5218 u64 safe_set, u64 safe_clear)
5219{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005220 const char *type = btrfs_feature_set_names[set];
5221 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005222 u64 disallowed, unsupported;
5223 u64 set_mask = flags & change_mask;
5224 u64 clear_mask = ~flags & change_mask;
5225
5226 unsupported = set_mask & ~supported_flags;
5227 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005228 names = btrfs_printable_features(set, unsupported);
5229 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005230 btrfs_warn(fs_info,
5231 "this kernel does not support the %s feature bit%s",
5232 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005233 kfree(names);
5234 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005235 btrfs_warn(fs_info,
5236 "this kernel does not support %s bits 0x%llx",
5237 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005238 return -EOPNOTSUPP;
5239 }
5240
5241 disallowed = set_mask & ~safe_set;
5242 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005243 names = btrfs_printable_features(set, disallowed);
5244 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005245 btrfs_warn(fs_info,
5246 "can't set the %s feature bit%s while mounted",
5247 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005248 kfree(names);
5249 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005250 btrfs_warn(fs_info,
5251 "can't set %s bits 0x%llx while mounted",
5252 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005253 return -EPERM;
5254 }
5255
5256 disallowed = clear_mask & ~safe_clear;
5257 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005258 names = btrfs_printable_features(set, disallowed);
5259 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005260 btrfs_warn(fs_info,
5261 "can't clear the %s feature bit%s while mounted",
5262 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005263 kfree(names);
5264 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005265 btrfs_warn(fs_info,
5266 "can't clear %s bits 0x%llx while mounted",
5267 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005268 return -EPERM;
5269 }
5270
5271 return 0;
5272}
5273
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005274#define check_feature(fs_info, change_mask, flags, mask_base) \
5275check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005276 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5277 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5278 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5279
5280static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005282 struct inode *inode = file_inode(file);
5283 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5284 struct btrfs_root *root = BTRFS_I(inode)->root;
5285 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005286 struct btrfs_ioctl_feature_flags flags[2];
5287 struct btrfs_trans_handle *trans;
5288 u64 newflags;
5289 int ret;
5290
5291 if (!capable(CAP_SYS_ADMIN))
5292 return -EPERM;
5293
5294 if (copy_from_user(flags, arg, sizeof(flags)))
5295 return -EFAULT;
5296
5297 /* Nothing to do */
5298 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5299 !flags[0].incompat_flags)
5300 return 0;
5301
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005302 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005303 flags[1].compat_flags, COMPAT);
5304 if (ret)
5305 return ret;
5306
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005307 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005308 flags[1].compat_ro_flags, COMPAT_RO);
5309 if (ret)
5310 return ret;
5311
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005312 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005313 flags[1].incompat_flags, INCOMPAT);
5314 if (ret)
5315 return ret;
5316
David Sterba7ab19622016-05-04 11:32:00 +02005317 ret = mnt_want_write_file(file);
5318 if (ret)
5319 return ret;
5320
David Sterba8051aa12014-02-07 14:34:04 +01005321 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005322 if (IS_ERR(trans)) {
5323 ret = PTR_ERR(trans);
5324 goto out_drop_write;
5325 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005326
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005327 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005328 newflags = btrfs_super_compat_flags(super_block);
5329 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5330 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5331 btrfs_set_super_compat_flags(super_block, newflags);
5332
5333 newflags = btrfs_super_compat_ro_flags(super_block);
5334 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5335 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5336 btrfs_set_super_compat_ro_flags(super_block, newflags);
5337
5338 newflags = btrfs_super_incompat_flags(super_block);
5339 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5340 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5341 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005342 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005343
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005344 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005345out_drop_write:
5346 mnt_drop_write_file(file);
5347
5348 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005349}
5350
Josef Bacik2351f432017-09-27 10:43:13 -04005351static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5352{
5353 struct btrfs_ioctl_send_args *arg;
5354 int ret;
5355
5356 if (compat) {
5357#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5358 struct btrfs_ioctl_send_args_32 args32;
5359
5360 ret = copy_from_user(&args32, argp, sizeof(args32));
5361 if (ret)
5362 return -EFAULT;
5363 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5364 if (!arg)
5365 return -ENOMEM;
5366 arg->send_fd = args32.send_fd;
5367 arg->clone_sources_count = args32.clone_sources_count;
5368 arg->clone_sources = compat_ptr(args32.clone_sources);
5369 arg->parent_root = args32.parent_root;
5370 arg->flags = args32.flags;
5371 memcpy(arg->reserved, args32.reserved,
5372 sizeof(args32.reserved));
5373#else
5374 return -ENOTTY;
5375#endif
5376 } else {
5377 arg = memdup_user(argp, sizeof(*arg));
5378 if (IS_ERR(arg))
5379 return PTR_ERR(arg);
5380 }
5381 ret = btrfs_ioctl_send(file, arg);
5382 kfree(arg);
5383 return ret;
5384}
5385
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005386long btrfs_ioctl(struct file *file, unsigned int
5387 cmd, unsigned long arg)
5388{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005389 struct inode *inode = file_inode(file);
5390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5391 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005392 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005393
5394 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005395 case FS_IOC_GETFLAGS:
5396 return btrfs_ioctl_getflags(file, argp);
5397 case FS_IOC_SETFLAGS:
5398 return btrfs_ioctl_setflags(file, argp);
5399 case FS_IOC_GETVERSION:
5400 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005401 case FITRIM:
5402 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005403 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005404 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005405 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005406 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005407 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005408 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005409 case BTRFS_IOC_SUBVOL_CREATE_V2:
5410 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005411 case BTRFS_IOC_SNAP_DESTROY:
5412 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005413 case BTRFS_IOC_SUBVOL_GETFLAGS:
5414 return btrfs_ioctl_subvol_getflags(file, argp);
5415 case BTRFS_IOC_SUBVOL_SETFLAGS:
5416 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005417 case BTRFS_IOC_DEFAULT_SUBVOL:
5418 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005419 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005420 return btrfs_ioctl_defrag(file, NULL);
5421 case BTRFS_IOC_DEFRAG_RANGE:
5422 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005423 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005424 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005425 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005426 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005427 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005428 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005429 case BTRFS_IOC_RM_DEV_V2:
5430 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005431 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005432 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005433 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005434 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005435 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005436 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005437 case BTRFS_IOC_TREE_SEARCH:
5438 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005439 case BTRFS_IOC_TREE_SEARCH_V2:
5440 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005441 case BTRFS_IOC_INO_LOOKUP:
5442 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005443 case BTRFS_IOC_INO_PATHS:
5444 return btrfs_ioctl_ino_to_path(root, argp);
5445 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005446 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5447 case BTRFS_IOC_LOGICAL_INO_V2:
5448 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005449 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005450 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005451 case BTRFS_IOC_SYNC: {
5452 int ret;
5453
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005454 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005455 if (ret)
5456 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005457 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005458 /*
5459 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005460 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005461 * processing uncleaned subvols.
5462 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005463 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005464 return ret;
5465 }
Sage Weil46204592010-10-29 15:41:32 -04005466 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005467 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005468 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005469 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005470 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005471 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005472 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005473 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005474 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005475 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005476 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005477 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005478 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005479 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005480 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005481 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005482 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5483 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005484#ifdef CONFIG_64BIT
5485 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5486 return btrfs_ioctl_set_received_subvol_32(file, argp);
5487#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005488 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005489 return _btrfs_ioctl_send(file, argp, false);
5490#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5491 case BTRFS_IOC_SEND_32:
5492 return _btrfs_ioctl_send(file, argp, true);
5493#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005494 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005495 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005496 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005497 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005498 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005499 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005500 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005501 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005502 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005503 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005504 case BTRFS_IOC_QUOTA_RESCAN:
5505 return btrfs_ioctl_quota_rescan(file, argp);
5506 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5507 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005508 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5509 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005510 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005511 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005512 case BTRFS_IOC_GET_FSLABEL:
5513 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005514 case BTRFS_IOC_SET_FSLABEL:
5515 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005516 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005517 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005518 case BTRFS_IOC_GET_FEATURES:
5519 return btrfs_ioctl_get_features(file, argp);
5520 case BTRFS_IOC_SET_FEATURES:
5521 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005522 case FS_IOC_FSGETXATTR:
5523 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005524 case FS_IOC_FSSETXATTR:
5525 return btrfs_ioctl_fssetxattr(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005526 }
5527
5528 return -ENOTTY;
5529}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005530
5531#ifdef CONFIG_COMPAT
5532long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5533{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005534 /*
5535 * These all access 32-bit values anyway so no further
5536 * handling is necessary.
5537 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005538 switch (cmd) {
5539 case FS_IOC32_GETFLAGS:
5540 cmd = FS_IOC_GETFLAGS;
5541 break;
5542 case FS_IOC32_SETFLAGS:
5543 cmd = FS_IOC_SETFLAGS;
5544 break;
5545 case FS_IOC32_GETVERSION:
5546 cmd = FS_IOC_GETVERSION;
5547 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005548 }
5549
5550 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5551}
5552#endif