blob: a67143c579aa3f8b5eb3dd9630f1610fba29dfb4 [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000043#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020044#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000045#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070046#include <linux/uaccess.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051#include "print-tree.h"
52#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040053#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020055#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040056#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020057#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010058#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000059#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040060#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070061#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000062#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080063#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040064
Hugo Millsabccd002014-01-30 20:17:00 +000065#ifdef CONFIG_64BIT
66/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
67 * structures are incorrect, as the timespec structure from userspace
68 * is 4 bytes too small. We define these alternatives here to teach
69 * the kernel about the 32-bit struct packing.
70 */
71struct btrfs_ioctl_timespec_32 {
72 __u64 sec;
73 __u32 nsec;
74} __attribute__ ((__packed__));
75
76struct btrfs_ioctl_received_subvol_args_32 {
77 char uuid[BTRFS_UUID_SIZE]; /* in */
78 __u64 stransid; /* in */
79 __u64 rtransid; /* out */
80 struct btrfs_ioctl_timespec_32 stime; /* in */
81 struct btrfs_ioctl_timespec_32 rtime; /* out */
82 __u64 flags; /* in */
83 __u64 reserved[16]; /* in */
84} __attribute__ ((__packed__));
85
86#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
87 struct btrfs_ioctl_received_subvol_args_32)
88#endif
89
90
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. */
96static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
97{
98 if (S_ISDIR(mode))
99 return flags;
100 else if (S_ISREG(mode))
101 return flags & ~FS_DIRSYNC_FL;
102 else
103 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
104}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400105
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200106/*
107 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
108 */
109static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
110{
111 unsigned int iflags = 0;
112
113 if (flags & BTRFS_INODE_SYNC)
114 iflags |= FS_SYNC_FL;
115 if (flags & BTRFS_INODE_IMMUTABLE)
116 iflags |= FS_IMMUTABLE_FL;
117 if (flags & BTRFS_INODE_APPEND)
118 iflags |= FS_APPEND_FL;
119 if (flags & BTRFS_INODE_NODUMP)
120 iflags |= FS_NODUMP_FL;
121 if (flags & BTRFS_INODE_NOATIME)
122 iflags |= FS_NOATIME_FL;
123 if (flags & BTRFS_INODE_DIRSYNC)
124 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000125 if (flags & BTRFS_INODE_NODATACOW)
126 iflags |= FS_NOCOW_FL;
127
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900128 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000129 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900130 else if (flags & BTRFS_INODE_COMPRESS)
131 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200132
133 return iflags;
134}
135
136/*
137 * Update inode->i_flags based on the btrfs internal flags.
138 */
139void btrfs_update_iflags(struct inode *inode)
140{
141 struct btrfs_inode *ip = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100142 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143
144 if (ip->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100145 new_fl |= S_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200146 if (ip->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100147 new_fl |= S_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200148 if (ip->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100149 new_fl |= S_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200150 if (ip->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100151 new_fl |= S_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200152 if (ip->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100153 new_fl |= S_DIRSYNC;
154
155 set_mask_bits(&inode->i_flags,
156 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
157 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200158}
159
160/*
161 * Inherit flags from the parent inode.
162 *
Josef Bacike27425d2011-09-27 11:01:30 -0400163 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200164 */
165void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
166{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400167 unsigned int flags;
168
169 if (!dir)
170 return;
171
172 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200173
Josef Bacike27425d2011-09-27 11:01:30 -0400174 if (flags & BTRFS_INODE_NOCOMPRESS) {
175 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
176 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
177 } else if (flags & BTRFS_INODE_COMPRESS) {
178 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
179 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
180 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181
Liu Bo213490b2012-09-11 08:33:50 -0600182 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400183 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600184 if (S_ISREG(inode->i_mode))
185 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
186 }
Josef Bacike27425d2011-09-27 11:01:30 -0400187
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200188 btrfs_update_iflags(inode);
189}
190
191static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
192{
Al Viro496ad9a2013-01-23 17:07:38 -0500193 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
195
196 if (copy_to_user(arg, &flags, sizeof(flags)))
197 return -EFAULT;
198 return 0;
199}
200
Liu Bo75e7cb72011-03-22 10:12:20 +0000201static int check_flags(unsigned int flags)
202{
203 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
204 FS_NOATIME_FL | FS_NODUMP_FL | \
205 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000206 FS_NOCOMP_FL | FS_COMPR_FL |
207 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000208 return -EOPNOTSUPP;
209
210 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
211 return -EINVAL;
212
Liu Bo75e7cb72011-03-22 10:12:20 +0000213 return 0;
214}
215
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200216static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
217{
Al Viro496ad9a2013-01-23 17:07:38 -0500218 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200219 struct btrfs_inode *ip = BTRFS_I(inode);
220 struct btrfs_root *root = ip->root;
221 struct btrfs_trans_handle *trans;
222 unsigned int flags, oldflags;
223 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800224 u64 ip_oldflags;
225 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600226 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200227
David Sterbabd60ea02014-01-16 15:50:22 +0100228 if (!inode_owner_or_capable(inode))
229 return -EPERM;
230
Li Zefanb83cc962010-12-20 16:04:08 +0800231 if (btrfs_root_readonly(root))
232 return -EROFS;
233
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200234 if (copy_from_user(&flags, arg, sizeof(flags)))
235 return -EFAULT;
236
Liu Bo75e7cb72011-03-22 10:12:20 +0000237 ret = check_flags(flags);
238 if (ret)
239 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200240
Jan Karae7848682012-06-12 16:20:32 +0200241 ret = mnt_want_write_file(file);
242 if (ret)
243 return ret;
244
Al Viro59551022016-01-22 15:40:57 -0500245 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200246
Li Zefanf062abf2011-12-29 13:36:45 +0800247 ip_oldflags = ip->flags;
248 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600249 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800250
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200251 flags = btrfs_mask_flags(inode->i_mode, flags);
252 oldflags = btrfs_flags_to_ioctl(ip->flags);
253 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
254 if (!capable(CAP_LINUX_IMMUTABLE)) {
255 ret = -EPERM;
256 goto out_unlock;
257 }
258 }
259
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200260 if (flags & FS_SYNC_FL)
261 ip->flags |= BTRFS_INODE_SYNC;
262 else
263 ip->flags &= ~BTRFS_INODE_SYNC;
264 if (flags & FS_IMMUTABLE_FL)
265 ip->flags |= BTRFS_INODE_IMMUTABLE;
266 else
267 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
268 if (flags & FS_APPEND_FL)
269 ip->flags |= BTRFS_INODE_APPEND;
270 else
271 ip->flags &= ~BTRFS_INODE_APPEND;
272 if (flags & FS_NODUMP_FL)
273 ip->flags |= BTRFS_INODE_NODUMP;
274 else
275 ip->flags &= ~BTRFS_INODE_NODUMP;
276 if (flags & FS_NOATIME_FL)
277 ip->flags |= BTRFS_INODE_NOATIME;
278 else
279 ip->flags &= ~BTRFS_INODE_NOATIME;
280 if (flags & FS_DIRSYNC_FL)
281 ip->flags |= BTRFS_INODE_DIRSYNC;
282 else
283 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600284 if (flags & FS_NOCOW_FL) {
285 if (S_ISREG(mode)) {
286 /*
287 * It's safe to turn csums off here, no extents exist.
288 * Otherwise we want the flag to reflect the real COW
289 * status of the file and will not set it.
290 */
291 if (inode->i_size == 0)
292 ip->flags |= BTRFS_INODE_NODATACOW
293 | BTRFS_INODE_NODATASUM;
294 } else {
295 ip->flags |= BTRFS_INODE_NODATACOW;
296 }
297 } else {
298 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400299 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600300 */
301 if (S_ISREG(mode)) {
302 if (inode->i_size == 0)
303 ip->flags &= ~(BTRFS_INODE_NODATACOW
304 | BTRFS_INODE_NODATASUM);
305 } else {
306 ip->flags &= ~BTRFS_INODE_NODATACOW;
307 }
308 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200309
Liu Bo75e7cb72011-03-22 10:12:20 +0000310 /*
311 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
312 * flag may be changed automatically if compression code won't make
313 * things smaller.
314 */
315 if (flags & FS_NOCOMP_FL) {
316 ip->flags &= ~BTRFS_INODE_COMPRESS;
317 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000318
319 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
320 if (ret && ret != -ENODATA)
321 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000322 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000323 const char *comp;
324
Liu Bo75e7cb72011-03-22 10:12:20 +0000325 ip->flags |= BTRFS_INODE_COMPRESS;
326 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000327
328 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
329 comp = "lzo";
330 else
331 comp = "zlib";
332 ret = btrfs_set_prop(inode, "btrfs.compression",
333 comp, strlen(comp), 0);
334 if (ret)
335 goto out_drop;
336
Li Zefanebcb9042011-04-15 03:03:17 +0000337 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100338 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
339 if (ret && ret != -ENODATA)
340 goto out_drop;
Li Zefanebcb9042011-04-15 03:03:17 +0000341 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000342 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200343
Li Zefan4da6f1a2011-12-29 13:39:50 +0800344 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800345 if (IS_ERR(trans)) {
346 ret = PTR_ERR(trans);
347 goto out_drop;
348 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200349
Li Zefan306424cc2011-12-14 20:12:02 -0500350 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400351 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700352 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200353 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200354
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200355 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800356 out_drop:
357 if (ret) {
358 ip->flags = ip_oldflags;
359 inode->i_flags = i_oldflags;
360 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200361
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200362 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500363 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200364 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000365 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200366}
367
368static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
369{
Al Viro496ad9a2013-01-23 17:07:38 -0500370 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200371
372 return put_user(inode->i_generation, arg);
373}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400374
Li Dongyangf7039b12011-03-24 10:24:28 +0000375static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
376{
Al Viro54563d42013-09-01 15:57:51 -0400377 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000378 struct btrfs_device *device;
379 struct request_queue *q;
380 struct fstrim_range range;
381 u64 minlen = ULLONG_MAX;
382 u64 num_devices = 0;
383 int ret;
384
385 if (!capable(CAP_SYS_ADMIN))
386 return -EPERM;
387
Filipe Manana1ac411b2019-03-26 10:49:56 +0000388 /*
389 * If the fs is mounted with nologreplay, which requires it to be
390 * mounted in RO mode as well, we can not allow discard on free space
391 * inside block groups, because log trees refer to extents that are not
392 * pinned in a block group's free space cache (pinning the extents is
393 * precisely the first phase of replaying a log tree).
394 */
395 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
396 return -EROFS;
397
Xiao Guangrong1f781602011-04-20 10:09:16 +0000398 rcu_read_lock();
399 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
400 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000401 if (!device->bdev)
402 continue;
403 q = bdev_get_queue(device->bdev);
404 if (blk_queue_discard(q)) {
405 num_devices++;
406 minlen = min((u64)q->limits.discard_granularity,
407 minlen);
408 }
409 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000410 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200411
Li Dongyangf7039b12011-03-24 10:24:28 +0000412 if (!num_devices)
413 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000414 if (copy_from_user(&range, arg, sizeof(range)))
415 return -EFAULT;
Qu Wenruo4423e072018-11-20 10:26:37 +0800416
417 /*
418 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
419 * block group is in the logical address space, which can be any
420 * sectorsize aligned bytenr in the range [0, U64_MAX].
421 */
422 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200423 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000424
425 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500426 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000427 if (ret < 0)
428 return ret;
429
430 if (copy_to_user(arg, &range, sizeof(range)))
431 return -EFAULT;
432
433 return 0;
434}
435
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200436int btrfs_is_empty_uuid(u8 *uuid)
437{
Chris Mason46e0f662013-11-15 12:14:55 +0100438 int i;
439
440 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
441 if (uuid[i])
442 return 0;
443 }
444 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200445}
446
Miao Xied5c12072013-02-28 10:04:33 +0000447static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400448 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400449 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200450 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000451 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400452{
453 struct btrfs_trans_handle *trans;
454 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100455 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400456 struct btrfs_inode_item *inode_item;
457 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000458 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400459 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000460 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700461 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900462 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400463 int ret;
464 int err;
465 u64 objectid;
466 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500467 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000468 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200469 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400470
David Sterba49a3c4d2016-03-24 17:49:22 +0100471 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
472 if (!root_item)
473 return -ENOMEM;
474
Li Zefan581bb052011-04-20 10:06:11 +0800475 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400476 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100477 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000478
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800479 /*
480 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400481 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800482 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100483 if (btrfs_qgroup_level(objectid)) {
484 ret = -ENOSPC;
485 goto fail_free;
486 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800487
Miao Xied5c12072013-02-28 10:04:33 +0000488 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400489 /*
Miao Xied5c12072013-02-28 10:04:33 +0000490 * The same as the snapshot creation, please see the comment
491 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400492 */
Miao Xied5c12072013-02-28 10:04:33 +0000493 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200494 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000495 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100496 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400497
Miao Xied5c12072013-02-28 10:04:33 +0000498 trans = btrfs_start_transaction(root, 0);
499 if (IS_ERR(trans)) {
500 ret = PTR_ERR(trans);
Liu Bode6e8202014-01-09 14:57:06 +0800501 btrfs_subvolume_release_metadata(root, &block_rsv,
502 qgroup_reserved);
David Sterba49a3c4d2016-03-24 17:49:22 +0100503 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000504 }
505 trans->block_rsv = &block_rsv;
506 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400507
Miao Xie8696c532013-02-07 06:02:44 +0000508 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200509 if (ret)
510 goto fail;
511
David Sterba4d75f8a2014-06-15 01:54:12 +0200512 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400513 if (IS_ERR(leaf)) {
514 ret = PTR_ERR(leaf);
515 goto fail;
516 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400517
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400518 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519 btrfs_set_header_bytenr(leaf, leaf->start);
520 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400521 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400522 btrfs_set_header_owner(leaf, objectid);
523
Ross Kirk0a4e5582013-09-24 10:12:38 +0100524 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400526 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200527 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400528 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400529 btrfs_mark_buffer_dirty(leaf);
530
David Sterba49a3c4d2016-03-24 17:49:22 +0100531 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800532 btrfs_set_stack_inode_generation(inode_item, 1);
533 btrfs_set_stack_inode_size(inode_item, 3);
534 btrfs_set_stack_inode_nlink(inode_item, 1);
David Sterba707e8a02014-06-04 19:22:26 +0200535 btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800536 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400537
David Sterba49a3c4d2016-03-24 17:49:22 +0100538 btrfs_set_root_flags(root_item, 0);
539 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800540 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000541
David Sterba49a3c4d2016-03-24 17:49:22 +0100542 btrfs_set_root_bytenr(root_item, leaf->start);
543 btrfs_set_root_generation(root_item, trans->transid);
544 btrfs_set_root_level(root_item, 0);
545 btrfs_set_root_refs(root_item, 1);
546 btrfs_set_root_used(root_item, leaf->len);
547 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400548
David Sterba49a3c4d2016-03-24 17:49:22 +0100549 btrfs_set_root_generation_v2(root_item,
550 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200551 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100552 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
553 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
554 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
555 root_item->ctime = root_item->otime;
556 btrfs_set_root_ctransid(root_item, trans->transid);
557 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558
Chris Mason925baed2008-06-25 16:01:30 -0400559 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 free_extent_buffer(leaf);
561 leaf = NULL;
562
David Sterba49a3c4d2016-03-24 17:49:22 +0100563 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400564
565 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400566 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200567 key.type = BTRFS_ROOT_ITEM_KEY;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400568 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100569 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570 if (ret)
571 goto fail;
572
Yan, Zheng76dda932009-09-21 16:00:26 -0400573 key.offset = (u64)-1;
574 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100575 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100576 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400577 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100578 goto fail;
579 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400580
581 btrfs_record_root_in_trans(trans, new_root);
582
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000583 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700584 if (ret) {
585 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400586 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700587 goto fail;
588 }
589
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530590 mutex_lock(&new_root->objectid_mutex);
591 new_root->highest_objectid = new_dirid;
592 mutex_unlock(&new_root->objectid_mutex);
593
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594 /*
595 * insert the directory item
596 */
Chris Mason3de45862008-11-17 21:02:50 -0500597 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100598 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400599 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100600 goto fail;
601 }
Chris Mason3de45862008-11-17 21:02:50 -0500602
603 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800604 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500605 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100606 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400607 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100609 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500610
Yan Zheng52c26172009-01-05 15:43:43 -0500611 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
612 ret = btrfs_update_inode(trans, root, dir);
613 BUG_ON(ret);
614
Chris Mason0660b5a2008-11-17 20:37:39 -0500615 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400616 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800617 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500618 BUG_ON(ret);
619
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200620 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
David Sterba49a3c4d2016-03-24 17:49:22 +0100621 root_item->uuid, BTRFS_UUID_KEY_SUBVOL,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200622 objectid);
623 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400624 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200625
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400626fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100627 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000628 trans->block_rsv = NULL;
629 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800630 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
631
Sage Weil72fd0322010-10-29 15:41:32 -0400632 if (async_transid) {
633 *async_transid = trans->transid;
634 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000635 if (err)
636 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400637 } else {
638 err = btrfs_commit_transaction(trans, root);
639 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400640 if (err && !ret)
641 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500642
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900643 if (!ret) {
644 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800645 if (IS_ERR(inode))
646 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900647 d_instantiate(dentry, inode);
648 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400649 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100650
651fail_free:
652 kfree(root_item);
653 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654}
655
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000656static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +0800657{
658 s64 writers;
659 DEFINE_WAIT(wait);
660
661 do {
662 prepare_to_wait(&root->subv_writers->wait, &wait,
663 TASK_UNINTERRUPTIBLE);
664
665 writers = percpu_counter_sum(&root->subv_writers->counter);
666 if (writers)
667 schedule();
668
669 finish_wait(&root->subv_writers->wait, &wait);
670 } while (writers);
671}
672
Miao Xiee9662f72013-02-28 10:01:15 +0000673static int create_snapshot(struct btrfs_root *root, struct inode *dir,
674 struct dentry *dentry, char *name, int namelen,
675 u64 *async_transid, bool readonly,
676 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400677{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000678 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400679 struct btrfs_pending_snapshot *pending_snapshot;
680 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000681 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400682
Miao Xie27cdeb72014-04-02 19:51:05 +0800683 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400684 return -EINVAL;
685
David Sterbaa1ee7362015-11-10 18:53:56 +0100686 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
687 if (!pending_snapshot)
688 return -ENOMEM;
689
David Sterbab0c0ea62015-11-10 18:54:00 +0100690 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
691 GFP_NOFS);
David Sterba8546b572015-11-10 18:54:03 +0100692 pending_snapshot->path = btrfs_alloc_path();
693 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100694 ret = -ENOMEM;
695 goto free_pending;
696 }
697
Miao Xie8257b2d2014-03-06 13:38:19 +0800698 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100699 smp_mb__after_atomic();
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000700 btrfs_wait_for_no_snapshoting_writes(root);
Miao Xie8257b2d2014-03-06 13:38:19 +0800701
Miao Xie6a038432013-05-15 07:48:24 +0000702 ret = btrfs_start_delalloc_inodes(root, 0);
703 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100704 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000705
Filipe Manana578def72016-04-26 15:36:38 +0100706 btrfs_wait_ordered_extents(root, -1, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000707
Miao Xie66d8f3d2012-09-06 04:02:28 -0600708 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
709 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000710 /*
711 * 1 - parent dir inode
712 * 2 - dir entries
713 * 1 - root item
714 * 2 - root ref/backref
715 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200716 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000717 */
718 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200719 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400720 &pending_snapshot->qgroup_reserved,
721 false);
Miao Xied5c12072013-02-28 10:04:33 +0000722 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100723 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000724
Yan, Zhenga22285a2010-05-16 10:48:46 -0400725 pending_snapshot->dentry = dentry;
726 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800727 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000728 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000729 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400730
Miao Xied5c12072013-02-28 10:04:33 +0000731 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400732 if (IS_ERR(trans)) {
733 ret = PTR_ERR(trans);
734 goto fail;
735 }
736
Josef Bacik83515832011-06-14 15:16:14 -0400737 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400738 list_add(&pending_snapshot->list,
739 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400740 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400741 if (async_transid) {
742 *async_transid = trans->transid;
743 ret = btrfs_commit_transaction_async(trans,
744 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000745 if (ret)
746 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400747 } else {
748 ret = btrfs_commit_transaction(trans,
749 root->fs_info->extent_root);
750 }
Miao Xieaec80302013-03-04 09:44:29 +0000751 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400752 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400753
754 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400755 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000756 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400757
Chris Masond3797302014-10-15 13:50:56 -0700758 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
759 if (ret)
760 goto fail;
761
David Howells2b0143b2015-03-17 22:25:59 +0000762 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000763 if (IS_ERR(inode)) {
764 ret = PTR_ERR(inode);
765 goto fail;
766 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900767
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000768 d_instantiate(dentry, inode);
769 ret = 0;
770fail:
Miao Xied5c12072013-02-28 10:04:33 +0000771 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
772 &pending_snapshot->block_rsv,
773 pending_snapshot->qgroup_reserved);
David Sterbaa1ee7362015-11-10 18:53:56 +0100774dec_and_free:
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000775 if (atomic_dec_and_test(&root->will_be_snapshoted))
776 wake_up_atomic_t(&root->will_be_snapshoted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100777free_pending:
778 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100779 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100780 kfree(pending_snapshot);
781
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400782 return ret;
783}
784
Sage Weil4260f7c2010-10-29 15:46:43 -0400785/* copy of may_delete in fs/namei.c()
786 * Check whether we can remove a link victim from directory dir, check
787 * whether the type of victim is right.
788 * 1. We can't do it if dir is read-only (done in permission())
789 * 2. We should have write and exec permissions on dir
790 * 3. We can't remove anything from append-only dir
791 * 4. We can't do anything with immutable dir (done in permission())
792 * 5. If the sticky bit on dir is set we should either
793 * a. be owner of dir, or
794 * b. be owner of victim, or
795 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400796 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400797 * links pointing to it.
798 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
799 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
800 * 9. We can't remove a root or mountpoint.
801 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
802 * nfs_async_unlink().
803 */
804
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530805static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400806{
807 int error;
808
David Howells2b0143b2015-03-17 22:25:59 +0000809 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400810 return -ENOENT;
811
David Howells2b0143b2015-03-17 22:25:59 +0000812 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400813 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400814
815 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
816 if (error)
817 return error;
818 if (IS_APPEND(dir))
819 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000820 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
821 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400822 return -EPERM;
823 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000824 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400825 return -ENOTDIR;
826 if (IS_ROOT(victim))
827 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000828 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400829 return -EISDIR;
830 if (IS_DEADDIR(dir))
831 return -ENOENT;
832 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
833 return -EBUSY;
834 return 0;
835}
836
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400837/* copy of may_create in fs/namei.c() */
838static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
839{
David Howells2b0143b2015-03-17 22:25:59 +0000840 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400841 return -EEXIST;
842 if (IS_DEADDIR(dir))
843 return -ENOENT;
844 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
845}
846
847/*
848 * Create a new subvolume below @parent. This is largely modeled after
849 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
850 * inside this filesystem so it's quite a bit simpler.
851 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400852static noinline int btrfs_mksubvol(struct path *parent,
853 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400854 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200855 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000856 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400857{
David Howells2b0143b2015-03-17 22:25:59 +0000858 struct inode *dir = d_inode(parent->dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400859 struct dentry *dentry;
860 int error;
861
Al Viro00235412016-05-26 00:05:12 -0400862 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
863 if (error == -EINTR)
864 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400865
866 dentry = lookup_one_len(name, parent->dentry, namelen);
867 error = PTR_ERR(dentry);
868 if (IS_ERR(dentry))
869 goto out_unlock;
870
Yan, Zheng76dda932009-09-21 16:00:26 -0400871 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400872 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600873 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400874
Chris Mason9c520572012-12-17 14:26:57 -0500875 /*
876 * even if this name doesn't exist, we may get hash collisions.
877 * check for them now when we can safely fail
878 */
879 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
880 dir->i_ino, name,
881 namelen);
882 if (error)
883 goto out_dput;
884
Yan, Zheng76dda932009-09-21 16:00:26 -0400885 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
886
887 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
888 goto out_up_read;
889
Chris Mason3de45862008-11-17 21:02:50 -0500890 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000891 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200892 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500893 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000894 error = create_subvol(dir, dentry, name, namelen,
895 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500896 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400897 if (!error)
898 fsnotify_mkdir(dir, dentry);
899out_up_read:
900 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400901out_dput:
902 dput(dentry);
903out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500904 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400905 return error;
906}
907
Chris Mason4cb53002011-05-24 15:35:30 -0400908/*
909 * When we're defragging a range, we don't want to kick it off again
910 * if it is really just waiting for delalloc to send it down.
911 * If we find a nice big extent or delalloc range for the bytes in the
912 * file you want to defrag, we return 0 to let you know to skip this
913 * part of the file
914 */
David Sterbaaab110a2014-07-29 17:32:10 +0200915static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400916{
917 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
918 struct extent_map *em = NULL;
919 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
920 u64 end;
921
922 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300923 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -0400924 read_unlock(&em_tree->lock);
925
926 if (em) {
927 end = extent_map_end(em);
928 free_extent_map(em);
929 if (end - offset > thresh)
930 return 0;
931 }
932 /* if we already have a nice delalloc here, just stop */
933 thresh /= 2;
934 end = count_range_bits(io_tree, &offset, offset + thresh,
935 thresh, EXTENT_DELALLOC, 1);
936 if (end >= thresh)
937 return 0;
938 return 1;
939}
940
941/*
942 * helper function to walk through a file and find extents
943 * newer than a specific transid, and smaller than thresh.
944 *
945 * This is used by the defragging code to find new and small
946 * extents
947 */
948static int find_new_extents(struct btrfs_root *root,
949 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200950 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400951{
952 struct btrfs_path *path;
953 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400954 struct extent_buffer *leaf;
955 struct btrfs_file_extent_item *extent;
956 int type;
957 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000958 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400959
960 path = btrfs_alloc_path();
961 if (!path)
962 return -ENOMEM;
963
David Sterbaa4689d22011-05-31 17:08:14 +0000964 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400965 min_key.type = BTRFS_EXTENT_DATA_KEY;
966 min_key.offset = *off;
967
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530968 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100969 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400970 if (ret != 0)
971 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000972process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000973 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400974 goto none;
975 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
976 goto none;
977
978 leaf = path->nodes[0];
979 extent = btrfs_item_ptr(leaf, path->slots[0],
980 struct btrfs_file_extent_item);
981
982 type = btrfs_file_extent_type(leaf, extent);
983 if (type == BTRFS_FILE_EXTENT_REG &&
984 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
985 check_defrag_in_cache(inode, min_key.offset, thresh)) {
986 *off = min_key.offset;
987 btrfs_free_path(path);
988 return 0;
989 }
990
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000991 path->slots[0]++;
992 if (path->slots[0] < btrfs_header_nritems(leaf)) {
993 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
994 goto process_slot;
995 }
996
Chris Mason4cb53002011-05-24 15:35:30 -0400997 if (min_key.offset == (u64)-1)
998 goto none;
999
1000 min_key.offset++;
1001 btrfs_release_path(path);
1002 }
1003none:
1004 btrfs_free_path(path);
1005 return -ENOENT;
1006}
1007
Li Zefan6c282eb2012-06-11 16:03:35 +08001008static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001009{
1010 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001011 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001012 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001013 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001014
1015 /*
1016 * hopefully we have this extent in the tree already, try without
1017 * the full extent lock
1018 */
1019 read_lock(&em_tree->lock);
1020 em = lookup_extent_mapping(em_tree, start, len);
1021 read_unlock(&em_tree->lock);
1022
1023 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001024 struct extent_state *cached = NULL;
1025 u64 end = start + len - 1;
1026
Chris Mason940100a2010-03-10 10:52:59 -05001027 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001028 lock_extent_bits(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001029 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +00001030 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -05001031
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001032 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001033 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001034 }
1035
Li Zefan6c282eb2012-06-11 16:03:35 +08001036 return em;
1037}
1038
1039static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1040{
1041 struct extent_map *next;
1042 bool ret = true;
1043
1044 /* this is the last extent */
1045 if (em->start + em->len >= i_size_read(inode))
1046 return false;
1047
1048 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001049 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1050 ret = false;
1051 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001052 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001053 ret = false;
1054
1055 free_extent_map(next);
1056 return ret;
1057}
1058
David Sterbaaab110a2014-07-29 17:32:10 +02001059static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001060 u64 *last_len, u64 *skip, u64 *defrag_end,
1061 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001062{
1063 struct extent_map *em;
1064 int ret = 1;
1065 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001066 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001067
1068 /*
1069 * make sure that once we start defragging an extent, we keep on
1070 * defragging it
1071 */
1072 if (start < *defrag_end)
1073 return 1;
1074
1075 *skip = 0;
1076
1077 em = defrag_lookup_extent(inode, start);
1078 if (!em)
1079 return 0;
1080
Chris Mason940100a2010-03-10 10:52:59 -05001081 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001082 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001083 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001084 goto out;
1085 }
1086
Liu Bo4a3560c2015-08-07 16:48:41 +08001087 if (!*defrag_end)
1088 prev_mergeable = false;
1089
Li Zefan6c282eb2012-06-11 16:03:35 +08001090 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001091 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001092 * we hit a real extent, if it is big or the next extent is not a
1093 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001094 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001095 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001096 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001097 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001098out:
Chris Mason940100a2010-03-10 10:52:59 -05001099 /*
1100 * last_len ends up being a counter of how many bytes we've defragged.
1101 * every time we choose not to defrag an extent, we reset *last_len
1102 * so that the next tiny extent will force a defrag.
1103 *
1104 * The end result of this is that tiny extents before a single big
1105 * extent will force at least part of that big extent to be defragged.
1106 */
1107 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001108 *defrag_end = extent_map_end(em);
1109 } else {
1110 *last_len = 0;
1111 *skip = extent_map_end(em);
1112 *defrag_end = 0;
1113 }
1114
1115 free_extent_map(em);
1116 return ret;
1117}
1118
Chris Mason4cb53002011-05-24 15:35:30 -04001119/*
1120 * it doesn't do much good to defrag one or two pages
1121 * at a time. This pulls in a nice chunk of pages
1122 * to COW and defrag.
1123 *
1124 * It also makes sure the delalloc code has enough
1125 * dirty data to avoid making new small extents as part
1126 * of the defrag
1127 *
1128 * It's a good idea to start RA on this range
1129 * before calling this.
1130 */
1131static int cluster_pages_for_defrag(struct inode *inode,
1132 struct page **pages,
1133 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001134 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001135{
Chris Mason4cb53002011-05-24 15:35:30 -04001136 unsigned long file_end;
1137 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001138 u64 page_start;
1139 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001140 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001141 int ret;
1142 int i;
1143 int i_done;
1144 struct btrfs_ordered_extent *ordered;
1145 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001146 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001147 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001148
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001149 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001150 if (!isize || start_index > file_end)
1151 return 0;
1152
1153 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001154
1155 ret = btrfs_delalloc_reserve_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001156 start_index << PAGE_SHIFT,
1157 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001158 if (ret)
1159 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001160 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001161 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001162
1163 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001164 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001165 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001166again:
Josef Bacika94733d2011-07-11 10:47:06 -04001167 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001168 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001169 if (!page)
1170 break;
1171
Miao Xie600a45e2012-02-16 15:01:24 +08001172 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001173 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001174 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001175 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001176 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001177 ordered = btrfs_lookup_ordered_extent(inode,
1178 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001179 unlock_extent_cached(tree, page_start, page_end,
1180 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001181 if (!ordered)
1182 break;
1183
1184 unlock_page(page);
1185 btrfs_start_ordered_extent(inode, ordered, 1);
1186 btrfs_put_ordered_extent(ordered);
1187 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001188 /*
1189 * we unlocked the page above, so we need check if
1190 * it was released or not.
1191 */
1192 if (page->mapping != inode->i_mapping) {
1193 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001194 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001195 goto again;
1196 }
Miao Xie600a45e2012-02-16 15:01:24 +08001197 }
1198
Chris Mason4cb53002011-05-24 15:35:30 -04001199 if (!PageUptodate(page)) {
1200 btrfs_readpage(NULL, page);
1201 lock_page(page);
1202 if (!PageUptodate(page)) {
1203 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001204 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001205 ret = -EIO;
1206 break;
1207 }
1208 }
Miao Xie600a45e2012-02-16 15:01:24 +08001209
Miao Xie600a45e2012-02-16 15:01:24 +08001210 if (page->mapping != inode->i_mapping) {
1211 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001212 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001213 goto again;
1214 }
1215
Chris Mason4cb53002011-05-24 15:35:30 -04001216 pages[i] = page;
1217 i_done++;
1218 }
1219 if (!i_done || ret)
1220 goto out;
1221
1222 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1223 goto out;
1224
1225 /*
1226 * so now we have a nice long stream of locked
1227 * and up to date pages, lets wait on them
1228 */
1229 for (i = 0; i < i_done; i++)
1230 wait_on_page_writeback(pages[i]);
1231
1232 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001233 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001234
1235 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001236 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001237 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1238 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001239 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1240 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001241
Liu Bo1f12bd02012-03-29 09:57:44 -04001242 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001243 spin_lock(&BTRFS_I(inode)->lock);
1244 BTRFS_I(inode)->outstanding_extents++;
1245 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001246 btrfs_delalloc_release_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001247 start_index << PAGE_SHIFT,
1248 (page_cnt - i_done) << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001249 }
1250
1251
Liu Bo9e8a4a82012-09-05 19:10:51 -06001252 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001253 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001254
1255 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1256 page_start, page_end - 1, &cached_state,
1257 GFP_NOFS);
1258
1259 for (i = 0; i < i_done; i++) {
1260 clear_page_dirty_for_io(pages[i]);
1261 ClearPageChecked(pages[i]);
1262 set_page_extent_mapped(pages[i]);
1263 set_page_dirty(pages[i]);
1264 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001265 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001266 }
1267 return i_done;
1268out:
1269 for (i = 0; i < i_done; i++) {
1270 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001271 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001272 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001273 btrfs_delalloc_release_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001274 start_index << PAGE_SHIFT,
1275 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001276 return ret;
1277
1278}
1279
1280int btrfs_defrag_file(struct inode *inode, struct file *file,
1281 struct btrfs_ioctl_defrag_range_args *range,
1282 u64 newer_than, unsigned long max_to_defrag)
1283{
1284 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001285 struct file_ra_state *ra = NULL;
1286 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001287 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001288 u64 last_len = 0;
1289 u64 skip = 0;
1290 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001291 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001292 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001293 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001294 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001295 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001296 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001297 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001298 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001299 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001300 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001301 struct page **pages = NULL;
1302
Liu Bo0abd5b12013-04-16 09:20:28 +00001303 if (isize == 0)
1304 return 0;
1305
1306 if (range->start >= isize)
1307 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001308
1309 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1310 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1311 return -EINVAL;
1312 if (range->compress_type)
1313 compress_type = range->compress_type;
1314 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001315
Liu Bo0abd5b12013-04-16 09:20:28 +00001316 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001317 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001318
Chris Mason4cb53002011-05-24 15:35:30 -04001319 /*
1320 * if we were not given a file, allocate a readahead
1321 * context
1322 */
1323 if (!file) {
1324 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1325 if (!ra)
1326 return -ENOMEM;
1327 file_ra_state_init(ra, inode->i_mapping);
1328 } else {
1329 ra = &file->f_ra;
1330 }
1331
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301332 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001333 GFP_NOFS);
1334 if (!pages) {
1335 ret = -ENOMEM;
1336 goto out_ra;
1337 }
1338
1339 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001340 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001341 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001342 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001343 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001344 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001345 }
1346
Chris Mason4cb53002011-05-24 15:35:30 -04001347 if (newer_than) {
1348 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001349 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001350 if (!ret) {
1351 range->start = newer_off;
1352 /*
1353 * we always align our defrag to help keep
1354 * the extents in the file evenly spaced
1355 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001356 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001357 } else
1358 goto out_ra;
1359 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001360 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001361 }
1362 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301363 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001364
Li Zefan2a0f7f52011-10-10 15:43:34 -04001365 /*
1366 * make writeback starts from i, so the defrag range can be
1367 * written sequentially.
1368 */
1369 if (i < inode->i_mapping->writeback_index)
1370 inode->i_mapping->writeback_index = i;
1371
Chris Masonf7f43cc2011-10-11 11:41:40 -04001372 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001373 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001374 /*
1375 * make sure we stop running if someone unmounts
1376 * the FS
1377 */
1378 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1379 break;
1380
David Sterba210549e2013-02-09 23:38:06 +00001381 if (btrfs_defrag_cancelled(root->fs_info)) {
David Sterbaf14d1042015-10-08 11:37:06 +02001382 btrfs_debug(root->fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001383 ret = -EAGAIN;
1384 break;
1385 }
1386
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001387 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001388 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001389 &defrag_end, range->flags &
1390 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001391 unsigned long next;
1392 /*
1393 * the should_defrag function tells us how much to skip
1394 * bump our counter by the suggested amount
1395 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001396 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001397 i = max(i + 1, next);
1398 continue;
1399 }
Li Zefan008873e2011-09-02 15:57:07 +08001400
1401 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001402 cluster = (PAGE_ALIGN(defrag_end) >>
1403 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001404 cluster = min(cluster, max_cluster);
1405 } else {
1406 cluster = max_cluster;
1407 }
1408
Li Zefan008873e2011-09-02 15:57:07 +08001409 if (i + cluster > ra_index) {
1410 ra_index = max(i, ra_index);
1411 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1412 cluster);
chandane4826a52015-06-09 17:38:32 +05301413 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001414 }
Chris Mason940100a2010-03-10 10:52:59 -05001415
Al Viro59551022016-01-22 15:40:57 -05001416 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001417 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1418 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001419 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001420 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001421 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001422 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001423 }
Chris Mason940100a2010-03-10 10:52:59 -05001424
Chris Mason4cb53002011-05-24 15:35:30 -04001425 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001426 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001427 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001428
1429 if (newer_than) {
1430 if (newer_off == (u64)-1)
1431 break;
1432
Liu Boe1f041e2012-03-29 09:57:45 -04001433 if (ret > 0)
1434 i += ret;
1435
Chris Mason4cb53002011-05-24 15:35:30 -04001436 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001437 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001438
Byongho Leeee221842015-12-15 01:42:10 +09001439 ret = find_new_extents(root, inode, newer_than,
1440 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001441 if (!ret) {
1442 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001443 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001444 } else {
1445 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001446 }
Chris Mason4cb53002011-05-24 15:35:30 -04001447 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001448 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001449 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001450 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001451 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001452 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001453 last_len = 0;
1454 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001455 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001456 }
1457
Filipe Mananadec8ef92014-03-01 10:55:54 +00001458 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001459 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001460 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1461 &BTRFS_I(inode)->runtime_flags))
1462 filemap_flush(inode->i_mapping);
1463 }
Chris Mason1e701a32010-03-11 09:42:04 -05001464
1465 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1466 /* the filemap_flush will queue IO into the worker threads, but
1467 * we have to make sure the IO is actually started and that
1468 * ordered extents get created before we return
1469 */
1470 atomic_inc(&root->fs_info->async_submit_draining);
1471 while (atomic_read(&root->fs_info->nr_async_submits) ||
1472 atomic_read(&root->fs_info->async_delalloc_pages)) {
1473 wait_event(root->fs_info->async_submit_wait,
1474 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1475 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1476 }
1477 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001478 }
1479
Li Zefan1a419d82010-10-25 15:12:50 +08001480 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001481 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001482 }
1483
Diego Calleja60ccf822011-09-01 16:33:57 +02001484 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001485
Chris Mason4cb53002011-05-24 15:35:30 -04001486out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001487 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
Al Viro59551022016-01-22 15:40:57 -05001488 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001489 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001490 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001491 }
Chris Mason4cb53002011-05-24 15:35:30 -04001492 if (!file)
1493 kfree(ra);
1494 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001495 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001496}
1497
Miao Xie198605a2012-11-26 08:43:45 +00001498static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001499 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001500{
1501 u64 new_size;
1502 u64 old_size;
1503 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001504 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001505 struct btrfs_ioctl_vol_args *vol_args;
1506 struct btrfs_trans_handle *trans;
1507 struct btrfs_device *device = NULL;
1508 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001509 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001510 char *devstr = NULL;
1511 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001512 int mod = 0;
1513
Chris Masone441d542009-01-05 16:57:23 -05001514 if (!capable(CAP_SYS_ADMIN))
1515 return -EPERM;
1516
Miao Xie198605a2012-11-26 08:43:45 +00001517 ret = mnt_want_write_file(file);
1518 if (ret)
1519 return ret;
1520
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001521 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1522 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001523 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001524 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001525 }
1526
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001527 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001528 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001529 if (IS_ERR(vol_args)) {
1530 ret = PTR_ERR(vol_args);
1531 goto out;
1532 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001533
1534 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001535
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001536 sizestr = vol_args->name;
1537 devstr = strchr(sizestr, ':');
1538 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001539 sizestr = devstr + 1;
1540 *devstr = '\0';
1541 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001542 ret = kstrtoull(devstr, 10, &devid);
1543 if (ret)
1544 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001545 if (!devid) {
1546 ret = -EINVAL;
1547 goto out_free;
1548 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001549 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001550 }
Miao Xiedba60f32012-12-21 09:19:51 +00001551
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001552 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001553 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001554 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001555 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001556 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001557 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001558 }
Miao Xiedba60f32012-12-21 09:19:51 +00001559
1560 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001561 btrfs_info(root->fs_info,
1562 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001563 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001564 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001565 goto out_free;
1566 }
1567
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001568 if (!strcmp(sizestr, "max"))
1569 new_size = device->bdev->bd_inode->i_size;
1570 else {
1571 if (sizestr[0] == '-') {
1572 mod = -1;
1573 sizestr++;
1574 } else if (sizestr[0] == '+') {
1575 mod = 1;
1576 sizestr++;
1577 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001578 new_size = memparse(sizestr, &retptr);
1579 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001580 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001581 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001582 }
1583 }
1584
Stefan Behrens63a212a2012-11-05 18:29:28 +01001585 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001586 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001587 goto out_free;
1588 }
1589
Miao Xie7cc8e582014-09-03 21:35:38 +08001590 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001591
1592 if (mod < 0) {
1593 if (new_size > old_size) {
1594 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001595 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001596 }
1597 new_size = old_size - new_size;
1598 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001599 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001600 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001601 goto out_free;
1602 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001603 new_size = old_size + new_size;
1604 }
1605
Byongho Leeee221842015-12-15 01:42:10 +09001606 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001607 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001608 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001609 }
1610 if (new_size > device->bdev->bd_inode->i_size) {
1611 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001612 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001613 }
1614
David Sterbab8b93ad2015-01-16 17:26:13 +01001615 new_size = div_u64(new_size, root->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616 new_size *= root->sectorsize;
1617
David Sterbaecaeb142015-10-08 09:01:03 +02001618 btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001619 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001620
1621 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001622 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001623 if (IS_ERR(trans)) {
1624 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001625 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001626 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001627 ret = btrfs_grow_device(trans, device, new_size);
1628 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001629 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001630 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001631 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001632
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001633out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001634 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001635out:
1636 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001637 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001638 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001639 return ret;
1640}
1641
Sage Weil72fd0322010-10-29 15:41:32 -04001642static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001643 char *name, unsigned long fd, int subvol,
1644 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001645 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001646{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001647 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001648 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001649
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001650 if (!S_ISDIR(file_inode(file)->i_mode))
1651 return -ENOTDIR;
1652
Liu Boa874a632012-06-29 03:58:46 -06001653 ret = mnt_want_write_file(file);
1654 if (ret)
1655 goto out;
1656
Sage Weil72fd0322010-10-29 15:41:32 -04001657 namelen = strlen(name);
1658 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001659 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001660 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001661 }
1662
Chris Mason16780ca2012-02-20 22:14:55 -05001663 if (name[0] == '.' &&
1664 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1665 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001666 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001667 }
1668
Chris Mason3de45862008-11-17 21:02:50 -05001669 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001670 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001671 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001672 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001673 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001674 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001675 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001676 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001677 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001678 }
1679
Al Viro496ad9a2013-01-23 17:07:38 -05001680 src_inode = file_inode(src.file);
1681 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001682 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001683 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001684 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001685 } else if (!inode_owner_or_capable(src_inode)) {
1686 /*
1687 * Subvolume creation is not restricted, but snapshots
1688 * are limited to own subvolumes only
1689 */
1690 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001691 } else {
1692 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1693 BTRFS_I(src_inode)->root,
1694 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001695 }
Al Viro2903ff02012-08-28 12:52:22 -04001696 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001697 }
Liu Boa874a632012-06-29 03:58:46 -06001698out_drop_write:
1699 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001700out:
Sage Weil72fd0322010-10-29 15:41:32 -04001701 return ret;
1702}
1703
1704static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001705 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001706{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001707 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001708 int ret;
1709
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001710 if (!S_ISDIR(file_inode(file)->i_mode))
1711 return -ENOTDIR;
1712
Li Zefanfa0d2b92010-12-20 15:53:28 +08001713 vol_args = memdup_user(arg, sizeof(*vol_args));
1714 if (IS_ERR(vol_args))
1715 return PTR_ERR(vol_args);
1716 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001717
Li Zefanfa0d2b92010-12-20 15:53:28 +08001718 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001719 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001720 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001721
Li Zefanfa0d2b92010-12-20 15:53:28 +08001722 kfree(vol_args);
1723 return ret;
1724}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001725
Li Zefanfa0d2b92010-12-20 15:53:28 +08001726static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1727 void __user *arg, int subvol)
1728{
1729 struct btrfs_ioctl_vol_args_v2 *vol_args;
1730 int ret;
1731 u64 transid = 0;
1732 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001733 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001734 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001735
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001736 if (!S_ISDIR(file_inode(file)->i_mode))
1737 return -ENOTDIR;
1738
Li Zefanfa0d2b92010-12-20 15:53:28 +08001739 vol_args = memdup_user(arg, sizeof(*vol_args));
1740 if (IS_ERR(vol_args))
1741 return PTR_ERR(vol_args);
1742 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001743
Li Zefanb83cc962010-12-20 16:04:08 +08001744 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001745 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1746 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001747 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001748 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001749 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001750
1751 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1752 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001753 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1754 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001755 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001756 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001757 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001758 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001759 }
1760 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1761 if (IS_ERR(inherit)) {
1762 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001763 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001764 }
1765 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001766
1767 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001768 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001769 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001770 if (ret)
1771 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001772
Dan Carpenterc47ca322014-09-04 14:09:15 +03001773 if (ptr && copy_to_user(arg +
1774 offsetof(struct btrfs_ioctl_vol_args_v2,
1775 transid),
1776 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001777 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001778
1779free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001780 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001781free_args:
1782 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001783 return ret;
1784}
1785
Li Zefan0caa1022010-12-20 16:30:25 +08001786static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1787 void __user *arg)
1788{
Al Viro496ad9a2013-01-23 17:07:38 -05001789 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001790 struct btrfs_root *root = BTRFS_I(inode)->root;
1791 int ret = 0;
1792 u64 flags = 0;
1793
Li Zefan33345d012011-04-20 10:31:50 +08001794 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001795 return -EINVAL;
1796
1797 down_read(&root->fs_info->subvol_sem);
1798 if (btrfs_root_readonly(root))
1799 flags |= BTRFS_SUBVOL_RDONLY;
1800 up_read(&root->fs_info->subvol_sem);
1801
1802 if (copy_to_user(arg, &flags, sizeof(flags)))
1803 ret = -EFAULT;
1804
1805 return ret;
1806}
1807
1808static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1809 void __user *arg)
1810{
Al Viro496ad9a2013-01-23 17:07:38 -05001811 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001812 struct btrfs_root *root = BTRFS_I(inode)->root;
1813 struct btrfs_trans_handle *trans;
1814 u64 root_flags;
1815 u64 flags;
1816 int ret = 0;
1817
David Sterbabd60ea02014-01-16 15:50:22 +01001818 if (!inode_owner_or_capable(inode))
1819 return -EPERM;
1820
Liu Bob9ca0662012-06-29 03:58:49 -06001821 ret = mnt_want_write_file(file);
1822 if (ret)
1823 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001824
Liu Bob9ca0662012-06-29 03:58:49 -06001825 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1826 ret = -EINVAL;
1827 goto out_drop_write;
1828 }
Li Zefan0caa1022010-12-20 16:30:25 +08001829
Liu Bob9ca0662012-06-29 03:58:49 -06001830 if (copy_from_user(&flags, arg, sizeof(flags))) {
1831 ret = -EFAULT;
1832 goto out_drop_write;
1833 }
Li Zefan0caa1022010-12-20 16:30:25 +08001834
Liu Bob9ca0662012-06-29 03:58:49 -06001835 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1836 ret = -EINVAL;
1837 goto out_drop_write;
1838 }
Li Zefan0caa1022010-12-20 16:30:25 +08001839
Liu Bob9ca0662012-06-29 03:58:49 -06001840 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1841 ret = -EOPNOTSUPP;
1842 goto out_drop_write;
1843 }
Li Zefan0caa1022010-12-20 16:30:25 +08001844
1845 down_write(&root->fs_info->subvol_sem);
1846
1847 /* nothing to do */
1848 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001849 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001850
1851 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001852 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001853 btrfs_set_root_flags(&root->root_item,
1854 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001855 } else {
1856 /*
1857 * Block RO -> RW transition if this subvolume is involved in
1858 * send
1859 */
1860 spin_lock(&root->root_item_lock);
1861 if (root->send_in_progress == 0) {
1862 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001863 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001864 spin_unlock(&root->root_item_lock);
1865 } else {
1866 spin_unlock(&root->root_item_lock);
1867 btrfs_warn(root->fs_info,
1868 "Attempt to set subvolume %llu read-write during send",
1869 root->root_key.objectid);
1870 ret = -EPERM;
1871 goto out_drop_sem;
1872 }
1873 }
Li Zefan0caa1022010-12-20 16:30:25 +08001874
1875 trans = btrfs_start_transaction(root, 1);
1876 if (IS_ERR(trans)) {
1877 ret = PTR_ERR(trans);
1878 goto out_reset;
1879 }
1880
Li Zefanb4dc2b82011-02-16 06:06:34 +00001881 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001882 &root->root_key, &root->root_item);
1883
1884 btrfs_commit_transaction(trans, root);
1885out_reset:
1886 if (ret)
1887 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001888out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001889 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001890out_drop_write:
1891 mnt_drop_write_file(file);
1892out:
Li Zefan0caa1022010-12-20 16:30:25 +08001893 return ret;
1894}
1895
Yan, Zheng76dda932009-09-21 16:00:26 -04001896/*
1897 * helper to check if the subvolume references other subvolumes
1898 */
1899static noinline int may_destroy_subvol(struct btrfs_root *root)
1900{
1901 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001902 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001903 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001904 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001905 int ret;
1906
1907 path = btrfs_alloc_path();
1908 if (!path)
1909 return -ENOMEM;
1910
Josef Bacik175a2b82013-08-12 15:36:44 -04001911 /* Make sure this root isn't set as the default subvol */
1912 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1913 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1914 dir_id, "default", 7, 0);
1915 if (di && !IS_ERR(di)) {
1916 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1917 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001918 ret = -EPERM;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001919 btrfs_err(root->fs_info,
1920 "deleting default subvolume %llu is not allowed",
1921 key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001922 goto out;
1923 }
1924 btrfs_release_path(path);
1925 }
1926
Yan, Zheng76dda932009-09-21 16:00:26 -04001927 key.objectid = root->root_key.objectid;
1928 key.type = BTRFS_ROOT_REF_KEY;
1929 key.offset = (u64)-1;
1930
1931 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1932 &key, path, 0, 0);
1933 if (ret < 0)
1934 goto out;
1935 BUG_ON(ret == 0);
1936
1937 ret = 0;
1938 if (path->slots[0] > 0) {
1939 path->slots[0]--;
1940 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1941 if (key.objectid == root->root_key.objectid &&
1942 key.type == BTRFS_ROOT_REF_KEY)
1943 ret = -ENOTEMPTY;
1944 }
1945out:
1946 btrfs_free_path(path);
1947 return ret;
1948}
1949
Chris Masonac8e9812010-02-28 15:39:26 -05001950static noinline int key_in_sk(struct btrfs_key *key,
1951 struct btrfs_ioctl_search_key *sk)
1952{
Chris Masonabc6e132010-03-18 12:10:08 -04001953 struct btrfs_key test;
1954 int ret;
1955
1956 test.objectid = sk->min_objectid;
1957 test.type = sk->min_type;
1958 test.offset = sk->min_offset;
1959
1960 ret = btrfs_comp_cpu_keys(key, &test);
1961 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001962 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001963
1964 test.objectid = sk->max_objectid;
1965 test.type = sk->max_type;
1966 test.offset = sk->max_offset;
1967
1968 ret = btrfs_comp_cpu_keys(key, &test);
1969 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001970 return 0;
1971 return 1;
1972}
1973
Jeff Mahoneydf397562016-06-21 20:18:21 -04001974static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001975 struct btrfs_key *key,
1976 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001977 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001978 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001979 unsigned long *sk_offset,
1980 int *num_found)
1981{
1982 u64 found_transid;
1983 struct extent_buffer *leaf;
1984 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001985 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001986 unsigned long item_off;
1987 unsigned long item_len;
1988 int nritems;
1989 int i;
1990 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001991 int ret = 0;
1992
1993 leaf = path->nodes[0];
1994 slot = path->slots[0];
1995 nritems = btrfs_header_nritems(leaf);
1996
1997 if (btrfs_header_generation(leaf) > sk->max_transid) {
1998 i = nritems;
1999 goto advance_key;
2000 }
2001 found_transid = btrfs_header_generation(leaf);
2002
2003 for (i = slot; i < nritems; i++) {
2004 item_off = btrfs_item_ptr_offset(leaf, i);
2005 item_len = btrfs_item_size_nr(leaf, i);
2006
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002007 btrfs_item_key_to_cpu(leaf, key, i);
2008 if (!key_in_sk(key, sk))
2009 continue;
2010
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002011 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002012 if (*num_found) {
2013 ret = 1;
2014 goto out;
2015 }
Chris Masonac8e9812010-02-28 15:39:26 -05002016
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002017 /*
2018 * return one empty item back for v1, which does not
2019 * handle -EOVERFLOW
2020 */
2021
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002022 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002023 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002024 ret = -EOVERFLOW;
2025 }
Chris Masonac8e9812010-02-28 15:39:26 -05002026
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002027 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002028 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002029 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002030 }
2031
Chris Masonac8e9812010-02-28 15:39:26 -05002032 sh.objectid = key->objectid;
2033 sh.offset = key->offset;
2034 sh.type = key->type;
2035 sh.len = item_len;
2036 sh.transid = found_transid;
2037
2038 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002039 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2040 ret = -EFAULT;
2041 goto out;
2042 }
2043
Chris Masonac8e9812010-02-28 15:39:26 -05002044 *sk_offset += sizeof(sh);
2045
2046 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002047 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002048 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002049 if (read_extent_buffer_to_user(leaf, up,
2050 item_off, item_len)) {
2051 ret = -EFAULT;
2052 goto out;
2053 }
2054
Chris Masonac8e9812010-02-28 15:39:26 -05002055 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002056 }
Hugo Millse2156862011-05-14 17:43:41 +00002057 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002058
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002059 if (ret) /* -EOVERFLOW from above */
2060 goto out;
2061
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002062 if (*num_found >= sk->nr_items) {
2063 ret = 1;
2064 goto out;
2065 }
Chris Masonac8e9812010-02-28 15:39:26 -05002066 }
2067advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002068 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002069 test.objectid = sk->max_objectid;
2070 test.type = sk->max_type;
2071 test.offset = sk->max_offset;
2072 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2073 ret = 1;
2074 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002075 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002076 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002077 key->offset = 0;
2078 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002079 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002080 key->offset = 0;
2081 key->type = 0;
2082 key->objectid++;
2083 } else
2084 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002085out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002086 /*
2087 * 0: all items from this leaf copied, continue with next
2088 * 1: * more items can be copied, but unused buffer is too small
2089 * * all items were found
2090 * Either way, it will stops the loop which iterates to the next
2091 * leaf
2092 * -EOVERFLOW: item was to large for buffer
2093 * -EFAULT: could not copy extent buffer back to userspace
2094 */
Chris Masonac8e9812010-02-28 15:39:26 -05002095 return ret;
2096}
2097
2098static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002099 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002100 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002101 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002102{
2103 struct btrfs_root *root;
2104 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002105 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002106 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2107 int ret;
2108 int num_found = 0;
2109 unsigned long sk_offset = 0;
2110
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002111 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2112 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002113 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002114 }
Gerhard Heift12544442014-01-30 16:23:58 +01002115
Chris Masonac8e9812010-02-28 15:39:26 -05002116 path = btrfs_alloc_path();
2117 if (!path)
2118 return -ENOMEM;
2119
2120 if (sk->tree_id == 0) {
2121 /* search the root of the inode that was passed */
2122 root = BTRFS_I(inode)->root;
2123 } else {
2124 key.objectid = sk->tree_id;
2125 key.type = BTRFS_ROOT_ITEM_KEY;
2126 key.offset = (u64)-1;
2127 root = btrfs_read_fs_root_no_name(info, &key);
2128 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002129 btrfs_free_path(path);
2130 return -ENOENT;
2131 }
2132 }
2133
2134 key.objectid = sk->min_objectid;
2135 key.type = sk->min_type;
2136 key.offset = sk->min_offset;
2137
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302138 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002139 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002140 if (ret != 0) {
2141 if (ret > 0)
2142 ret = 0;
2143 goto err;
2144 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002145 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002146 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002147 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002148 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002149 break;
2150
2151 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002152 if (ret > 0)
2153 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002154err:
2155 sk->nr_items = num_found;
2156 btrfs_free_path(path);
2157 return ret;
2158}
2159
2160static noinline int btrfs_ioctl_tree_search(struct file *file,
2161 void __user *argp)
2162{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002163 struct btrfs_ioctl_search_args __user *uargs;
2164 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002165 struct inode *inode;
2166 int ret;
2167 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002168
2169 if (!capable(CAP_SYS_ADMIN))
2170 return -EPERM;
2171
Gerhard Heiftba346b32014-01-30 16:24:02 +01002172 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002173
Gerhard Heiftba346b32014-01-30 16:24:02 +01002174 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2175 return -EFAULT;
2176
2177 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002178
Al Viro496ad9a2013-01-23 17:07:38 -05002179 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002180 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002181
2182 /*
2183 * In the origin implementation an overflow is handled by returning a
2184 * search header with a len of zero, so reset ret.
2185 */
2186 if (ret == -EOVERFLOW)
2187 ret = 0;
2188
Gerhard Heiftba346b32014-01-30 16:24:02 +01002189 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002190 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002191 return ret;
2192}
2193
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002194static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2195 void __user *argp)
2196{
2197 struct btrfs_ioctl_search_args_v2 __user *uarg;
2198 struct btrfs_ioctl_search_args_v2 args;
2199 struct inode *inode;
2200 int ret;
2201 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002202 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002203
2204 if (!capable(CAP_SYS_ADMIN))
2205 return -EPERM;
2206
2207 /* copy search header and buffer size */
2208 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2209 if (copy_from_user(&args, uarg, sizeof(args)))
2210 return -EFAULT;
2211
2212 buf_size = args.buf_size;
2213
2214 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2215 return -EOVERFLOW;
2216
2217 /* limit result size to 16MB */
2218 if (buf_size > buf_limit)
2219 buf_size = buf_limit;
2220
2221 inode = file_inode(file);
2222 ret = search_ioctl(inode, &args.key, &buf_size,
2223 (char *)(&uarg->buf[0]));
2224 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2225 ret = -EFAULT;
2226 else if (ret == -EOVERFLOW &&
2227 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2228 ret = -EFAULT;
2229
Yan, Zheng76dda932009-09-21 16:00:26 -04002230 return ret;
2231}
2232
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002233/*
Chris Masonac8e9812010-02-28 15:39:26 -05002234 * Search INODE_REFs to identify path name of 'dirid' directory
2235 * in a 'tree_id' tree. and sets path name to 'name'.
2236 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002237static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2238 u64 tree_id, u64 dirid, char *name)
2239{
2240 struct btrfs_root *root;
2241 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002242 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002243 int ret = -1;
2244 int slot;
2245 int len;
2246 int total_len = 0;
2247 struct btrfs_inode_ref *iref;
2248 struct extent_buffer *l;
2249 struct btrfs_path *path;
2250
2251 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2252 name[0]='\0';
2253 return 0;
2254 }
2255
2256 path = btrfs_alloc_path();
2257 if (!path)
2258 return -ENOMEM;
2259
Nikolay Borisov1c3aae52017-12-01 11:19:42 +02002260 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002261
2262 key.objectid = tree_id;
2263 key.type = BTRFS_ROOT_ITEM_KEY;
2264 key.offset = (u64)-1;
2265 root = btrfs_read_fs_root_no_name(info, &key);
2266 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002267 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002268 ret = -ENOENT;
2269 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002270 }
2271
2272 key.objectid = dirid;
2273 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002274 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002275
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302276 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002277 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2278 if (ret < 0)
2279 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002280 else if (ret > 0) {
2281 ret = btrfs_previous_item(root, path, dirid,
2282 BTRFS_INODE_REF_KEY);
2283 if (ret < 0)
2284 goto out;
2285 else if (ret > 0) {
2286 ret = -ENOENT;
2287 goto out;
2288 }
2289 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002290
2291 l = path->nodes[0];
2292 slot = path->slots[0];
2293 btrfs_item_key_to_cpu(l, &key, slot);
2294
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002295 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2296 len = btrfs_inode_ref_name_len(l, iref);
2297 ptr -= len + 1;
2298 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002299 if (ptr < name) {
2300 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002301 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002302 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002303
2304 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302305 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002306
2307 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2308 break;
2309
David Sterbab3b4aa72011-04-21 01:20:15 +02002310 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002311 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002312 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002313 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002314 }
Li Zefan77906a502011-07-14 03:16:00 +00002315 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302316 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002317 ret = 0;
2318out:
2319 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002320 return ret;
2321}
2322
2323static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2324 void __user *argp)
2325{
2326 struct btrfs_ioctl_ino_lookup_args *args;
2327 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002328 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002329
Julia Lawall2354d082010-10-29 15:14:18 -04002330 args = memdup_user(argp, sizeof(*args));
2331 if (IS_ERR(args))
2332 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002333
Al Viro496ad9a2013-01-23 17:07:38 -05002334 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002335
David Sterba01b810b2015-05-12 19:14:49 +02002336 /*
2337 * Unprivileged query to obtain the containing subvolume root id. The
2338 * path is reset so it's consistent with btrfs_search_path_in_tree.
2339 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002340 if (args->treeid == 0)
2341 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2342
David Sterba01b810b2015-05-12 19:14:49 +02002343 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2344 args->name[0] = 0;
2345 goto out;
2346 }
2347
2348 if (!capable(CAP_SYS_ADMIN)) {
2349 ret = -EPERM;
2350 goto out;
2351 }
2352
Chris Masonac8e9812010-02-28 15:39:26 -05002353 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2354 args->treeid, args->objectid,
2355 args->name);
2356
David Sterba01b810b2015-05-12 19:14:49 +02002357out:
Chris Masonac8e9812010-02-28 15:39:26 -05002358 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2359 ret = -EFAULT;
2360
2361 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002362 return ret;
2363}
2364
Yan, Zheng76dda932009-09-21 16:00:26 -04002365static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2366 void __user *arg)
2367{
Al Viro54563d42013-09-01 15:57:51 -04002368 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002369 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002370 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002371 struct inode *inode;
2372 struct btrfs_root *root = BTRFS_I(dir)->root;
2373 struct btrfs_root *dest = NULL;
2374 struct btrfs_ioctl_vol_args *vol_args;
2375 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002376 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002377 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002378 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002379 int namelen;
2380 int ret;
2381 int err = 0;
2382
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002383 if (!S_ISDIR(dir->i_mode))
2384 return -ENOTDIR;
2385
Yan, Zheng76dda932009-09-21 16:00:26 -04002386 vol_args = memdup_user(arg, sizeof(*vol_args));
2387 if (IS_ERR(vol_args))
2388 return PTR_ERR(vol_args);
2389
2390 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2391 namelen = strlen(vol_args->name);
2392 if (strchr(vol_args->name, '/') ||
2393 strncmp(vol_args->name, "..", namelen) == 0) {
2394 err = -EINVAL;
2395 goto out;
2396 }
2397
Al Viroa561be72011-11-23 11:57:51 -05002398 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002399 if (err)
2400 goto out;
2401
David Sterba521e0542014-04-15 16:41:44 +02002402
Al Viro00235412016-05-26 00:05:12 -04002403 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2404 if (err == -EINTR)
2405 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002406 dentry = lookup_one_len(vol_args->name, parent, namelen);
2407 if (IS_ERR(dentry)) {
2408 err = PTR_ERR(dentry);
2409 goto out_unlock_dir;
2410 }
2411
David Howells2b0143b2015-03-17 22:25:59 +00002412 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002413 err = -ENOENT;
2414 goto out_dput;
2415 }
2416
David Howells2b0143b2015-03-17 22:25:59 +00002417 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002418 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302419 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002420 /*
2421 * Regular user. Only allow this with a special mount
2422 * option, when the user has write+exec access to the
2423 * subvol root, and when rmdir(2) would have been
2424 * allowed.
2425 *
2426 * Note that this is _not_ check that the subvol is
2427 * empty or doesn't contain data that we wouldn't
2428 * otherwise be able to delete.
2429 *
2430 * Users who want to delete empty subvols should try
2431 * rmdir(2).
2432 */
2433 err = -EPERM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002434 if (!btrfs_test_opt(root->fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002435 goto out_dput;
2436
2437 /*
2438 * Do not allow deletion if the parent dir is the same
2439 * as the dir to be deleted. That means the ioctl
2440 * must be called on the dentry referencing the root
2441 * of the subvol, not a random directory contained
2442 * within it.
2443 */
2444 err = -EINVAL;
2445 if (root == dest)
2446 goto out_dput;
2447
2448 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2449 if (err)
2450 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002451 }
2452
Miao Xie5c39da52012-10-22 11:39:53 +00002453 /* check if subvolume may be deleted by a user */
2454 err = btrfs_may_delete(dir, dentry, 1);
2455 if (err)
2456 goto out_dput;
2457
Li Zefan33345d012011-04-20 10:31:50 +08002458 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002459 err = -EINVAL;
2460 goto out_dput;
2461 }
2462
Al Viro59551022016-01-22 15:40:57 -05002463 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002464
2465 /*
2466 * Don't allow to delete a subvolume with send in progress. This is
2467 * inside the i_mutex so the error handling that has to drop the bit
2468 * again is not run concurrently.
2469 */
2470 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002471 root_flags = btrfs_root_flags(&dest->root_item);
2472 if (dest->send_in_progress == 0) {
2473 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002474 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2475 spin_unlock(&dest->root_item_lock);
2476 } else {
2477 spin_unlock(&dest->root_item_lock);
2478 btrfs_warn(root->fs_info,
2479 "Attempt to delete subvolume %llu during send",
Filipe Mananac55bfa62014-05-25 03:55:44 +01002480 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002481 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002482 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002483 }
2484
Yan, Zheng76dda932009-09-21 16:00:26 -04002485 down_write(&root->fs_info->subvol_sem);
2486
2487 err = may_destroy_subvol(dest);
2488 if (err)
2489 goto out_up_write;
2490
Miao Xiec58aaad2013-02-28 10:05:36 +00002491 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2492 /*
2493 * One for dir inode, two for dir entries, two for root
2494 * ref/backref.
2495 */
2496 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002497 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002498 if (err)
2499 goto out_up_write;
2500
Yan, Zhenga22285a2010-05-16 10:48:46 -04002501 trans = btrfs_start_transaction(root, 0);
2502 if (IS_ERR(trans)) {
2503 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002504 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002505 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002506 trans->block_rsv = &block_rsv;
2507 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002508
Filipe Manana2be63d52016-02-12 11:34:23 +00002509 btrfs_record_snapshot_destroy(trans, dir);
2510
Yan, Zheng76dda932009-09-21 16:00:26 -04002511 ret = btrfs_unlink_subvol(trans, root, dir,
2512 dest->root_key.objectid,
2513 dentry->d_name.name,
2514 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002515 if (ret) {
2516 err = ret;
Jeff Mahoney66642832016-06-10 18:19:25 -04002517 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002518 goto out_end_trans;
2519 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002520
2521 btrfs_record_root_in_trans(trans, dest);
2522
2523 memset(&dest->root_item.drop_progress, 0,
2524 sizeof(dest->root_item.drop_progress));
2525 dest->root_item.drop_level = 0;
2526 btrfs_set_root_refs(&dest->root_item, 0);
2527
Miao Xie27cdeb72014-04-02 19:51:05 +08002528 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002529 ret = btrfs_insert_orphan_item(trans,
2530 root->fs_info->tree_root,
2531 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002532 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002533 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002534 err = ret;
2535 goto out_end_trans;
2536 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002537 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002538
2539 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2540 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2541 dest->root_key.objectid);
2542 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002543 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002544 err = ret;
2545 goto out_end_trans;
2546 }
2547 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2548 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2549 dest->root_item.received_uuid,
2550 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2551 dest->root_key.objectid);
2552 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002553 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002554 err = ret;
2555 goto out_end_trans;
2556 }
2557 }
2558
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002559out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002560 trans->block_rsv = NULL;
2561 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002562 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002563 if (ret && !err)
2564 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002565 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002566out_release:
2567 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002568out_up_write:
2569 up_write(&root->fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002570 if (err) {
2571 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002572 root_flags = btrfs_root_flags(&dest->root_item);
2573 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002574 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2575 spin_unlock(&dest->root_item_lock);
2576 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002577out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002578 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002579 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002580 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002581 btrfs_invalidate_inodes(dest);
2582 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002583 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002584
2585 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002586 if (dest->ino_cache_inode) {
2587 iput(dest->ino_cache_inode);
2588 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002589 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002590 }
2591out_dput:
2592 dput(dentry);
2593out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002594 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002595out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002596 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002597out:
2598 kfree(vol_args);
2599 return err;
2600}
2601
Chris Mason1e701a32010-03-11 09:42:04 -05002602static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002603{
Al Viro496ad9a2013-01-23 17:07:38 -05002604 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002605 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002606 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002607 int ret;
2608
Ilya Dryomov25122d12013-01-20 15:57:57 +02002609 ret = mnt_want_write_file(file);
2610 if (ret)
2611 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002612
Ilya Dryomov25122d12013-01-20 15:57:57 +02002613 if (btrfs_root_readonly(root)) {
2614 ret = -EROFS;
2615 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002616 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002617
2618 switch (inode->i_mode & S_IFMT) {
2619 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002620 if (!capable(CAP_SYS_ADMIN)) {
2621 ret = -EPERM;
2622 goto out;
2623 }
Eric Sandeende78b512013-01-31 18:21:12 +00002624 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002625 if (ret)
2626 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002627 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002628 break;
2629 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002630 if (!(file->f_mode & FMODE_WRITE)) {
2631 ret = -EINVAL;
2632 goto out;
2633 }
Chris Mason1e701a32010-03-11 09:42:04 -05002634
2635 range = kzalloc(sizeof(*range), GFP_KERNEL);
2636 if (!range) {
2637 ret = -ENOMEM;
2638 goto out;
2639 }
2640
2641 if (argp) {
2642 if (copy_from_user(range, argp,
2643 sizeof(*range))) {
2644 ret = -EFAULT;
2645 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002646 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002647 }
2648 /* compression requires us to start the IO */
2649 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2650 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2651 range->extent_thresh = (u32)-1;
2652 }
2653 } else {
2654 /* the rest are all set to zero by kzalloc */
2655 range->len = (u64)-1;
2656 }
Al Viro496ad9a2013-01-23 17:07:38 -05002657 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002658 range, 0, 0);
2659 if (ret > 0)
2660 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002661 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002662 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002663 default:
2664 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002665 }
Chris Masone441d542009-01-05 16:57:23 -05002666out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002667 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002668 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002669}
2670
Christoph Hellwigb2950862008-12-02 09:54:17 -05002671static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002672{
2673 struct btrfs_ioctl_vol_args *vol_args;
2674 int ret;
2675
Chris Masone441d542009-01-05 16:57:23 -05002676 if (!capable(CAP_SYS_ADMIN))
2677 return -EPERM;
2678
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002679 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2680 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002681 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002682 }
2683
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002684 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002685 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002686 if (IS_ERR(vol_args)) {
2687 ret = PTR_ERR(vol_args);
2688 goto out;
2689 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002690
Mark Fasheh5516e592008-07-24 12:20:14 -04002691 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002692 ret = btrfs_init_new_device(root, vol_args->name);
2693
Anand Jain43d20762014-07-01 00:58:56 +08002694 if (!ret)
2695 btrfs_info(root->fs_info, "disk added %s",vol_args->name);
2696
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002697 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002698out:
2699 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002700 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002701 return ret;
2702}
2703
Anand Jain6b526ed2016-02-13 10:01:39 +08002704static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002705{
Al Viro496ad9a2013-01-23 17:07:38 -05002706 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jain6b526ed2016-02-13 10:01:39 +08002707 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002708 int ret;
2709
Chris Masone441d542009-01-05 16:57:23 -05002710 if (!capable(CAP_SYS_ADMIN))
2711 return -EPERM;
2712
Miao Xieda249272012-11-26 08:44:50 +00002713 ret = mnt_want_write_file(file);
2714 if (ret)
2715 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002716
Li Zefandae7b662009-04-08 15:06:54 +08002717 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002718 if (IS_ERR(vol_args)) {
2719 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002720 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002721 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002722
Anand Jain6b526ed2016-02-13 10:01:39 +08002723 /* Check for compatibility reject unknown flags */
Omar Sandoval9bb94d82018-05-22 15:44:01 -07002724 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
2725 ret = -EOPNOTSUPP;
2726 goto out;
2727 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002728
Anand Jain183860f2013-05-17 10:52:45 +00002729 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2730 1)) {
2731 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2732 goto out;
2733 }
2734
2735 mutex_lock(&root->fs_info->volume_mutex);
David Sterba735654e2016-02-15 18:15:21 +01002736 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Anand Jain6b526ed2016-02-13 10:01:39 +08002737 ret = btrfs_rm_device(root, NULL, vol_args->devid);
2738 } else {
2739 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
2740 ret = btrfs_rm_device(root, vol_args->name, 0);
2741 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002742 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002743 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002744
Anand Jain6b526ed2016-02-13 10:01:39 +08002745 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002746 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Anand Jain6b526ed2016-02-13 10:01:39 +08002747 btrfs_info(root->fs_info, "device deleted: id %llu",
2748 vol_args->devid);
2749 else
2750 btrfs_info(root->fs_info, "device deleted: %s",
2751 vol_args->name);
2752 }
Anand Jain183860f2013-05-17 10:52:45 +00002753out:
2754 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002755err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002756 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002757 return ret;
2758}
2759
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002760static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2761{
2762 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2763 struct btrfs_ioctl_vol_args *vol_args;
2764 int ret;
2765
2766 if (!capable(CAP_SYS_ADMIN))
2767 return -EPERM;
2768
2769 ret = mnt_want_write_file(file);
2770 if (ret)
2771 return ret;
2772
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002773 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2774 1)) {
2775 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002776 goto out_drop_write;
2777 }
2778
2779 vol_args = memdup_user(arg, sizeof(*vol_args));
2780 if (IS_ERR(vol_args)) {
2781 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002782 goto out;
2783 }
2784
David Sterba58d7bbf2016-05-04 14:10:47 +02002785 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002786 mutex_lock(&root->fs_info->volume_mutex);
Anand Jain6b526ed2016-02-13 10:01:39 +08002787 ret = btrfs_rm_device(root, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002788 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002789
2790 if (!ret)
2791 btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002792 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002793out:
2794 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2795out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002796 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002797
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002798 return ret;
2799}
2800
Jan Schmidt475f6382011-03-11 15:41:01 +01002801static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2802{
Li Zefan027ed2f2011-06-08 08:27:56 +00002803 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002804 struct btrfs_device *device;
Jan Schmidt475f6382011-03-11 15:41:01 +01002805 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002806 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002807
Li Zefan027ed2f2011-06-08 08:27:56 +00002808 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2809 if (!fi_args)
2810 return -ENOMEM;
2811
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002812 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002813 fi_args->num_devices = fs_devices->num_devices;
2814 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002815
Byongho Leed7641a42015-09-01 23:10:57 +09002816 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002817 if (device->devid > fi_args->max_id)
2818 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002819 }
2820 mutex_unlock(&fs_devices->device_list_mutex);
2821
David Sterba80a773f2014-05-07 18:17:06 +02002822 fi_args->nodesize = root->fs_info->super_copy->nodesize;
2823 fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
2824 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
2825
Li Zefan027ed2f2011-06-08 08:27:56 +00002826 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2827 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002828
Li Zefan027ed2f2011-06-08 08:27:56 +00002829 kfree(fi_args);
2830 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002831}
2832
2833static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2834{
2835 struct btrfs_ioctl_dev_info_args *di_args;
2836 struct btrfs_device *dev;
2837 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2838 int ret = 0;
2839 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002840
Jan Schmidt475f6382011-03-11 15:41:01 +01002841 di_args = memdup_user(arg, sizeof(*di_args));
2842 if (IS_ERR(di_args))
2843 return PTR_ERR(di_args);
2844
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002845 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002846 s_uuid = di_args->uuid;
2847
2848 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002849 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002850
2851 if (!dev) {
2852 ret = -ENODEV;
2853 goto out;
2854 }
2855
2856 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002857 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2858 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002859 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002860 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002861 struct rcu_string *name;
2862
2863 rcu_read_lock();
2864 name = rcu_dereference(dev->name);
2865 strncpy(di_args->path, name->str, sizeof(di_args->path));
2866 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002867 di_args->path[sizeof(di_args->path) - 1] = 0;
2868 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002869 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002870 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002871
2872out:
David Sterba55793c02013-04-26 15:20:23 +00002873 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002874 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2875 ret = -EFAULT;
2876
2877 kfree(di_args);
2878 return ret;
2879}
2880
Mark Fashehf4414602015-06-30 14:42:05 -07002881static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002882{
2883 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002884
Mark Fasheh416161d2013-08-06 11:42:51 -07002885 page = grab_cache_page(inode->i_mapping, index);
2886 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002887 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002888
2889 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002890 int ret;
2891
2892 ret = btrfs_readpage(NULL, page);
2893 if (ret)
2894 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002895 lock_page(page);
2896 if (!PageUptodate(page)) {
2897 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002898 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002899 return ERR_PTR(-EIO);
2900 }
2901 if (page->mapping != inode->i_mapping) {
2902 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002903 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002904 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002905 }
2906 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002907
2908 return page;
2909}
2910
Mark Fashehf4414602015-06-30 14:42:05 -07002911static int gather_extent_pages(struct inode *inode, struct page **pages,
2912 int num_pages, u64 off)
2913{
2914 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002915 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002916
2917 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002918again:
Mark Fashehf4414602015-06-30 14:42:05 -07002919 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002920 if (IS_ERR(pages[i])) {
2921 int err = PTR_ERR(pages[i]);
2922
2923 if (err == -EAGAIN)
2924 goto again;
2925 pages[i] = NULL;
2926 return err;
2927 }
Mark Fashehf4414602015-06-30 14:42:05 -07002928 }
2929 return 0;
2930}
2931
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002932static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2933 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002934{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002935 /*
2936 * Do any pending delalloc/csum calculations on inode, one way or
2937 * another, and lock file content.
2938 * The locking order is:
2939 *
2940 * 1) pages
2941 * 2) range in the inode's io tree
2942 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002943 while (1) {
2944 struct btrfs_ordered_extent *ordered;
2945 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2946 ordered = btrfs_lookup_first_ordered_extent(inode,
2947 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002948 if ((!ordered ||
2949 ordered->file_offset + ordered->len <= off ||
2950 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002951 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002952 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2953 if (ordered)
2954 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002955 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002956 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002957 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2958 if (ordered)
2959 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002960 if (!retry_range_locking)
2961 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002962 btrfs_wait_ordered_range(inode, off, len);
2963 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002964 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002965}
2966
Mark Fashehf4414602015-06-30 14:42:05 -07002967static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002968{
Al Viro59551022016-01-22 15:40:57 -05002969 inode_unlock(inode1);
2970 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002971}
2972
Mark Fashehf4414602015-06-30 14:42:05 -07002973static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2974{
2975 if (inode1 < inode2)
2976 swap(inode1, inode2);
2977
Al Viro59551022016-01-22 15:40:57 -05002978 inode_lock_nested(inode1, I_MUTEX_PARENT);
2979 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002980}
2981
2982static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2983 struct inode *inode2, u64 loff2, u64 len)
2984{
2985 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2986 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2987}
2988
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002989static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2990 struct inode *inode2, u64 loff2, u64 len,
2991 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002992{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002993 int ret;
2994
Mark Fasheh416161d2013-08-06 11:42:51 -07002995 if (inode1 < inode2) {
2996 swap(inode1, inode2);
2997 swap(loff1, loff2);
2998 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002999 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
3000 if (ret)
3001 return ret;
3002 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
3003 if (ret)
3004 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
3005 loff1 + len - 1);
3006 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003007}
3008
3009struct cmp_pages {
3010 int num_pages;
3011 struct page **src_pages;
3012 struct page **dst_pages;
3013};
3014
3015static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3016{
3017 int i;
3018 struct page *pg;
3019
3020 for (i = 0; i < cmp->num_pages; i++) {
3021 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003022 if (pg) {
3023 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003024 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003025 }
Mark Fashehf4414602015-06-30 14:42:05 -07003026 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003027 if (pg) {
3028 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003029 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003030 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003031 }
Mark Fashehf4414602015-06-30 14:42:05 -07003032 kfree(cmp->src_pages);
3033 kfree(cmp->dst_pages);
3034}
3035
3036static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3037 struct inode *dst, u64 dst_loff,
3038 u64 len, struct cmp_pages *cmp)
3039{
3040 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003041 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003042 struct page **src_pgarr, **dst_pgarr;
3043
3044 /*
3045 * We must gather up all the pages before we initiate our
3046 * extent locking. We use an array for the page pointers. Size
3047 * of the array is bounded by len, which is in turn bounded by
3048 * BTRFS_MAX_DEDUPE_LEN.
3049 */
David Sterba66722f72016-02-11 15:01:38 +01003050 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
3051 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07003052 if (!src_pgarr || !dst_pgarr) {
3053 kfree(src_pgarr);
3054 kfree(dst_pgarr);
3055 return -ENOMEM;
3056 }
3057 cmp->num_pages = num_pages;
3058 cmp->src_pages = src_pgarr;
3059 cmp->dst_pages = dst_pgarr;
3060
3061 ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff);
3062 if (ret)
3063 goto out;
3064
3065 ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff);
3066
3067out:
3068 if (ret)
3069 btrfs_cmp_data_free(cmp);
Naohiro Aotaba44bc42017-09-08 17:48:55 +09003070 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003071}
3072
3073static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
Mark Fashehf4414602015-06-30 14:42:05 -07003074 u64 dst_loff, u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003075{
3076 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003077 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003078 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003079 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003080 void *addr, *dst_addr;
3081
Mark Fashehf4414602015-06-30 14:42:05 -07003082 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003083 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003084 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003085 cmp_len = len;
3086
Mark Fashehf4414602015-06-30 14:42:05 -07003087 BUG_ON(i >= cmp->num_pages);
3088
3089 src_page = cmp->src_pages[i];
3090 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003091 ASSERT(PageLocked(src_page));
3092 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003093
Mark Fasheh416161d2013-08-06 11:42:51 -07003094 addr = kmap_atomic(src_page);
3095 dst_addr = kmap_atomic(dst_page);
3096
3097 flush_dcache_page(src_page);
3098 flush_dcache_page(dst_page);
3099
3100 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003101 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003102
3103 kunmap_atomic(addr);
3104 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003105
3106 if (ret)
3107 break;
3108
Mark Fasheh416161d2013-08-06 11:42:51 -07003109 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003110 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003111 }
3112
3113 return ret;
3114}
3115
Mark Fashehe1d227a2015-06-08 15:05:25 -07003116static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3117 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003118{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003119 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003120 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3121
Mark Fashehe1d227a2015-06-08 15:05:25 -07003122 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003123 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003124
3125 /* if we extend to eof, continue to block boundary */
3126 if (off + len == inode->i_size)
3127 *plen = len = ALIGN(inode->i_size, bs) - off;
3128
Mark Fasheh416161d2013-08-06 11:42:51 -07003129 /* Check that we are block aligned - btrfs_clone() requires this */
3130 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3131 return -EINVAL;
3132
3133 return 0;
3134}
3135
Mark Fashehe1d227a2015-06-08 15:05:25 -07003136static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003137 struct inode *dst, u64 dst_loff)
3138{
3139 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003140 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003141 struct cmp_pages cmp;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003142 int same_inode = 0;
3143 u64 same_lock_start = 0;
3144 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003145
Mark Fasheh416161d2013-08-06 11:42:51 -07003146 if (src == dst)
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003147 same_inode = 1;
Mark Fasheh416161d2013-08-06 11:42:51 -07003148
Filipe Manana113e8282015-03-30 18:26:47 +01003149 if (len == 0)
3150 return 0;
3151
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003152 if (same_inode) {
Al Viro59551022016-01-22 15:40:57 -05003153 inode_lock(src);
Mark Fasheh416161d2013-08-06 11:42:51 -07003154
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003155 ret = extent_same_check_offsets(src, loff, &len, olen);
3156 if (ret)
3157 goto out_unlock;
Filipe Mananaf4dfe682016-02-12 14:44:00 +00003158 ret = extent_same_check_offsets(src, dst_loff, &len, olen);
3159 if (ret)
3160 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003161
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003162 /*
3163 * Single inode case wants the same checks, except we
3164 * don't want our length pushed out past i_size as
3165 * comparing that data range makes no sense.
3166 *
3167 * extent_same_check_offsets() will do this for an
3168 * unaligned length at i_size, so catch it here and
3169 * reject the request.
3170 *
3171 * This effectively means we require aligned extents
3172 * for the single-inode case, whereas the other cases
3173 * allow an unaligned length so long as it ends at
3174 * i_size.
3175 */
3176 if (len != olen) {
3177 ret = -EINVAL;
3178 goto out_unlock;
3179 }
3180
3181 /* Check for overlapping ranges */
3182 if (dst_loff + len > loff && dst_loff < loff + len) {
3183 ret = -EINVAL;
3184 goto out_unlock;
3185 }
3186
3187 same_lock_start = min_t(u64, loff, dst_loff);
3188 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
3189 } else {
3190 btrfs_double_inode_lock(src, dst);
3191
3192 ret = extent_same_check_offsets(src, loff, &len, olen);
3193 if (ret)
3194 goto out_unlock;
3195
3196 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3197 if (ret)
3198 goto out_unlock;
3199 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003200
3201 /* don't make the dst file partly checksummed */
3202 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3203 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3204 ret = -EINVAL;
3205 goto out_unlock;
3206 }
3207
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003208again:
Mark Fashehf4414602015-06-30 14:42:05 -07003209 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3210 if (ret)
3211 goto out_unlock;
3212
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003213 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003214 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3215 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003216 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003217 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3218 false);
3219 /*
3220 * If one of the inodes has dirty pages in the respective range or
3221 * ordered extents, we need to flush dellaloc and wait for all ordered
3222 * extents in the range. We must unlock the pages and the ranges in the
3223 * io trees to avoid deadlocks when flushing delalloc (requires locking
3224 * pages) and when waiting for ordered extents to complete (they require
3225 * range locking).
3226 */
3227 if (ret == -EAGAIN) {
3228 /*
3229 * Ranges in the io trees already unlocked. Now unlock all
3230 * pages before waiting for all IO to complete.
3231 */
3232 btrfs_cmp_data_free(&cmp);
3233 if (same_inode) {
3234 btrfs_wait_ordered_range(src, same_lock_start,
3235 same_lock_len);
3236 } else {
3237 btrfs_wait_ordered_range(src, loff, len);
3238 btrfs_wait_ordered_range(dst, dst_loff, len);
3239 }
3240 goto again;
3241 }
3242 ASSERT(ret == 0);
3243 if (WARN_ON(ret)) {
3244 /* ranges in the io trees already unlocked */
3245 btrfs_cmp_data_free(&cmp);
3246 return ret;
3247 }
Mark Fashehf4414602015-06-30 14:42:05 -07003248
Mark Fasheh207910d2015-06-30 14:42:04 -07003249 /* pass original length for comparison so we stay within i_size */
Mark Fashehf4414602015-06-30 14:42:05 -07003250 ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003251 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003252 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003253
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003254 if (same_inode)
3255 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3256 same_lock_start + same_lock_len - 1);
3257 else
3258 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003259
3260 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003261out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003262 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003263 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003264 else
3265 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003266
3267 return ret;
3268}
3269
Byongho Leeee221842015-12-15 01:42:10 +09003270#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003271
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003272ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3273 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003274{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003275 struct inode *src = file_inode(src_file);
3276 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003277 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003278 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003279
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003280 if (olen > BTRFS_MAX_DEDUPE_LEN)
3281 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003282
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003283 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003284 /*
3285 * Btrfs does not support blocksize < page_size. As a
3286 * result, btrfs_cmp_data() won't correctly handle
3287 * this situation without an update.
3288 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003289 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003290 }
3291
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003292 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3293 if (res)
3294 return res;
3295 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003296}
3297
Filipe Mananaf82a9902014-06-01 01:50:28 +01003298static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3299 struct inode *inode,
3300 u64 endoff,
3301 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003302 const u64 olen,
3303 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003304{
3305 struct btrfs_root *root = BTRFS_I(inode)->root;
3306 int ret;
3307
3308 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003309 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003310 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003311 /*
3312 * We round up to the block size at eof when determining which
3313 * extents to clone above, but shouldn't round up the file size.
3314 */
3315 if (endoff > destoff + olen)
3316 endoff = destoff + olen;
3317 if (endoff > inode->i_size)
3318 btrfs_i_size_write(inode, endoff);
3319
3320 ret = btrfs_update_inode(trans, root, inode);
3321 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003322 btrfs_abort_transaction(trans, ret);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003323 btrfs_end_transaction(trans, root);
3324 goto out;
3325 }
3326 ret = btrfs_end_transaction(trans, root);
3327out:
3328 return ret;
3329}
3330
Filipe Manana7ffbb592014-06-09 03:48:05 +01003331static void clone_update_extent_map(struct inode *inode,
3332 const struct btrfs_trans_handle *trans,
3333 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003334 const u64 hole_offset,
3335 const u64 hole_len)
3336{
3337 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3338 struct extent_map *em;
3339 int ret;
3340
3341 em = alloc_extent_map();
3342 if (!em) {
3343 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3344 &BTRFS_I(inode)->runtime_flags);
3345 return;
3346 }
3347
Filipe Manana14f59792014-06-29 21:45:40 +01003348 if (path) {
3349 struct btrfs_file_extent_item *fi;
3350
3351 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3352 struct btrfs_file_extent_item);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003353 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3354 em->generation = -1;
3355 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3356 BTRFS_FILE_EXTENT_INLINE)
3357 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3358 &BTRFS_I(inode)->runtime_flags);
3359 } else {
3360 em->start = hole_offset;
3361 em->len = hole_len;
3362 em->ram_bytes = em->len;
3363 em->orig_start = hole_offset;
3364 em->block_start = EXTENT_MAP_HOLE;
3365 em->block_len = 0;
3366 em->orig_block_len = 0;
3367 em->compress_type = BTRFS_COMPRESS_NONE;
3368 em->generation = trans->transid;
3369 }
3370
3371 while (1) {
3372 write_lock(&em_tree->lock);
3373 ret = add_extent_mapping(em_tree, em, 1);
3374 write_unlock(&em_tree->lock);
3375 if (ret != -EEXIST) {
3376 free_extent_map(em);
3377 break;
3378 }
3379 btrfs_drop_extent_cache(inode, em->start,
3380 em->start + em->len - 1, 0);
3381 }
3382
David Sterbaee39b432014-09-30 01:33:33 +02003383 if (ret)
Filipe Manana7ffbb592014-06-09 03:48:05 +01003384 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3385 &BTRFS_I(inode)->runtime_flags);
3386}
3387
Filipe Manana8039d872015-10-13 15:15:00 +01003388/*
3389 * Make sure we do not end up inserting an inline extent into a file that has
3390 * already other (non-inline) extents. If a file has an inline extent it can
3391 * not have any other extents and the (single) inline extent must start at the
3392 * file offset 0. Failing to respect these rules will lead to file corruption,
3393 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3394 *
3395 * We can have extents that have been already written to disk or we can have
3396 * dirty ranges still in delalloc, in which case the extent maps and items are
3397 * created only when we run delalloc, and the delalloc ranges might fall outside
3398 * the range we are currently locking in the inode's io tree. So we check the
3399 * inode's i_size because of that (i_size updates are done while holding the
3400 * i_mutex, which we are holding here).
3401 * We also check to see if the inode has a size not greater than "datal" but has
3402 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3403 * protected against such concurrent fallocate calls by the i_mutex).
3404 *
3405 * If the file has no extents but a size greater than datal, do not allow the
3406 * copy because we would need turn the inline extent into a non-inline one (even
3407 * with NO_HOLES enabled). If we find our destination inode only has one inline
3408 * extent, just overwrite it with the source inline extent if its size is less
3409 * than the source extent's size, or we could copy the source inline extent's
3410 * data into the destination inode's inline extent if the later is greater then
3411 * the former.
3412 */
3413static int clone_copy_inline_extent(struct inode *src,
3414 struct inode *dst,
3415 struct btrfs_trans_handle *trans,
3416 struct btrfs_path *path,
3417 struct btrfs_key *new_key,
3418 const u64 drop_start,
3419 const u64 datal,
3420 const u64 skip,
3421 const u64 size,
3422 char *inline_data)
3423{
3424 struct btrfs_root *root = BTRFS_I(dst)->root;
3425 const u64 aligned_end = ALIGN(new_key->offset + datal,
3426 root->sectorsize);
3427 int ret;
3428 struct btrfs_key key;
3429
3430 if (new_key->offset > 0)
3431 return -EOPNOTSUPP;
3432
3433 key.objectid = btrfs_ino(dst);
3434 key.type = BTRFS_EXTENT_DATA_KEY;
3435 key.offset = 0;
3436 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3437 if (ret < 0) {
3438 return ret;
3439 } else if (ret > 0) {
3440 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3441 ret = btrfs_next_leaf(root, path);
3442 if (ret < 0)
3443 return ret;
3444 else if (ret > 0)
3445 goto copy_inline_extent;
3446 }
3447 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3448 if (key.objectid == btrfs_ino(dst) &&
3449 key.type == BTRFS_EXTENT_DATA_KEY) {
3450 ASSERT(key.offset > 0);
3451 return -EOPNOTSUPP;
3452 }
3453 } else if (i_size_read(dst) <= datal) {
3454 struct btrfs_file_extent_item *ei;
3455 u64 ext_len;
3456
3457 /*
3458 * If the file size is <= datal, make sure there are no other
3459 * extents following (can happen do to an fallocate call with
3460 * the flag FALLOC_FL_KEEP_SIZE).
3461 */
3462 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3463 struct btrfs_file_extent_item);
3464 /*
3465 * If it's an inline extent, it can not have other extents
3466 * following it.
3467 */
3468 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3469 BTRFS_FILE_EXTENT_INLINE)
3470 goto copy_inline_extent;
3471
3472 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3473 if (ext_len > aligned_end)
3474 return -EOPNOTSUPP;
3475
3476 ret = btrfs_next_item(root, path);
3477 if (ret < 0) {
3478 return ret;
3479 } else if (ret == 0) {
3480 btrfs_item_key_to_cpu(path->nodes[0], &key,
3481 path->slots[0]);
3482 if (key.objectid == btrfs_ino(dst) &&
3483 key.type == BTRFS_EXTENT_DATA_KEY)
3484 return -EOPNOTSUPP;
3485 }
3486 }
3487
3488copy_inline_extent:
3489 /*
3490 * We have no extent items, or we have an extent at offset 0 which may
3491 * or may not be inlined. All these cases are dealt the same way.
3492 */
3493 if (i_size_read(dst) > datal) {
3494 /*
3495 * If the destination inode has an inline extent...
3496 * This would require copying the data from the source inline
3497 * extent into the beginning of the destination's inline extent.
3498 * But this is really complex, both extents can be compressed
3499 * or just one of them, which would require decompressing and
3500 * re-compressing data (which could increase the new compressed
3501 * size, not allowing the compressed data to fit anymore in an
3502 * inline extent).
3503 * So just don't support this case for now (it should be rare,
3504 * we are not really saving space when cloning inline extents).
3505 */
3506 return -EOPNOTSUPP;
3507 }
3508
3509 btrfs_release_path(path);
3510 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3511 if (ret)
3512 return ret;
3513 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3514 if (ret)
3515 return ret;
3516
3517 if (skip) {
3518 const u32 start = btrfs_file_extent_calc_inline_size(0);
3519
3520 memmove(inline_data + start, inline_data + start + skip, datal);
3521 }
3522
3523 write_extent_buffer(path->nodes[0], inline_data,
3524 btrfs_item_ptr_offset(path->nodes[0],
3525 path->slots[0]),
3526 size);
3527 inode_add_bytes(dst, datal);
3528
3529 return 0;
3530}
3531
Mark Fasheh32b7c682013-08-06 11:42:49 -07003532/**
3533 * btrfs_clone() - clone a range from inode file to another
3534 *
3535 * @src: Inode to clone from
3536 * @inode: Inode to clone to
3537 * @off: Offset within source to start clone from
3538 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003539 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003540 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003541 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003542 */
3543static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003544 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003545 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003546{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003547 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003548 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003549 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003550 struct btrfs_trans_handle *trans;
3551 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003552 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003553 u32 nritems;
3554 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003555 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003556 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003557 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003558
3559 ret = -ENOMEM;
David Sterba15351952016-04-11 18:40:08 +02003560 buf = kmalloc(root->nodesize, GFP_KERNEL | __GFP_NOWARN);
3561 if (!buf) {
3562 buf = vmalloc(root->nodesize);
3563 if (!buf)
3564 return ret;
3565 }
Yan Zhengae01a0a2008-08-04 23:23:47 -04003566
3567 path = btrfs_alloc_path();
3568 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003569 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003570 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003571 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003572
David Sterbae4058b52015-11-27 16:31:35 +01003573 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003574 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08003575 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003576 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003577 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003578
3579 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003580 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003581
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003582 /*
3583 * note the key will change type as we walk through the
3584 * tree.
3585 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003586 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003587 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3588 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003589 if (ret < 0)
3590 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003591 /*
3592 * First search, if no extent item that starts at offset off was
3593 * found but the previous item is an extent item, it's possible
3594 * it might overlap our target range, therefore process it.
3595 */
3596 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3597 btrfs_item_key_to_cpu(path->nodes[0], &key,
3598 path->slots[0] - 1);
3599 if (key.type == BTRFS_EXTENT_DATA_KEY)
3600 path->slots[0]--;
3601 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003602
Yan Zhengae01a0a2008-08-04 23:23:47 -04003603 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003604process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003605 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003606 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003607 if (ret < 0)
3608 goto out;
3609 if (ret > 0)
3610 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003611 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003612 }
3613 leaf = path->nodes[0];
3614 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003615
Yan Zhengae01a0a2008-08-04 23:23:47 -04003616 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003617 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08003618 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003619 break;
3620
David Sterba962a2982014-06-04 18:41:45 +02003621 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003622 struct btrfs_file_extent_item *extent;
3623 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003624 u32 size;
3625 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003626 u64 disko = 0, diskl = 0;
3627 u64 datao = 0, datal = 0;
3628 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003629 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003630
Sage Weilc5c9cd42008-11-12 14:32:25 -05003631 extent = btrfs_item_ptr(leaf, slot,
3632 struct btrfs_file_extent_item);
3633 comp = btrfs_file_extent_compression(leaf, extent);
3634 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003635 if (type == BTRFS_FILE_EXTENT_REG ||
3636 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003637 disko = btrfs_file_extent_disk_bytenr(leaf,
3638 extent);
3639 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3640 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003641 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003642 datal = btrfs_file_extent_num_bytes(leaf,
3643 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003644 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3645 /* take upper bound, may be compressed */
3646 datal = btrfs_file_extent_ram_bytes(leaf,
3647 extent);
3648 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003649
Filipe Manana2c463822014-05-31 02:31:05 +01003650 /*
3651 * The first search might have left us at an extent
3652 * item that ends before our target range's start, can
3653 * happen if we have holes and NO_HOLES feature enabled.
3654 */
3655 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003656 path->slots[0]++;
3657 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003658 } else if (key.offset >= off + len) {
3659 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003660 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003661 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003662 size = btrfs_item_size_nr(leaf, slot);
3663 read_extent_buffer(leaf, buf,
3664 btrfs_item_ptr_offset(leaf, slot),
3665 size);
3666
3667 btrfs_release_path(path);
3668 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003669
Zheng Yan31840ae2008-09-23 13:14:14 -04003670 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08003671 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08003672 if (off <= key.offset)
3673 new_key.offset = key.offset + destoff - off;
3674 else
3675 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003676
Sage Weilb6f34092011-09-20 14:48:51 -04003677 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003678 * Deal with a hole that doesn't have an extent item
3679 * that represents it (NO_HOLES feature enabled).
3680 * This hole is either in the middle of the cloning
3681 * range or at the beginning (fully overlaps it or
3682 * partially overlaps it).
3683 */
3684 if (new_key.offset != last_dest_end)
3685 drop_start = last_dest_end;
3686 else
3687 drop_start = new_key.offset;
3688
3689 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003690 * 1 - adjusting old extent (we may have to split it)
3691 * 1 - add new extent
3692 * 1 - inode update
3693 */
3694 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695 if (IS_ERR(trans)) {
3696 ret = PTR_ERR(trans);
3697 goto out;
3698 }
3699
Chris Masonc8a894d2009-06-27 21:07:03 -04003700 if (type == BTRFS_FILE_EXTENT_REG ||
3701 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003702 /*
3703 * a | --- range to clone ---| b
3704 * | ------------- extent ------------- |
3705 */
3706
Antonio Ospite93915582014-06-04 14:03:48 +02003707 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003708 if (key.offset + datal > off + len)
3709 datal = off + len - key.offset;
3710
Antonio Ospite93915582014-06-04 14:03:48 +02003711 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003712 if (off > key.offset) {
3713 datao += off - key.offset;
3714 datal -= off - key.offset;
3715 }
3716
Josef Bacik5dc562c2012-08-17 13:14:17 -04003717 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003718 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003720 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003721 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003722 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003723 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003724 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003725 btrfs_end_transaction(trans, root);
3726 goto out;
3727 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003728
Sage Weilc5c9cd42008-11-12 14:32:25 -05003729 ret = btrfs_insert_empty_item(trans, root, path,
3730 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003731 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003732 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003733 btrfs_end_transaction(trans, root);
3734 goto out;
3735 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003736
3737 leaf = path->nodes[0];
3738 slot = path->slots[0];
3739 write_extent_buffer(leaf, buf,
3740 btrfs_item_ptr_offset(leaf, slot),
3741 size);
3742
3743 extent = btrfs_item_ptr(leaf, slot,
3744 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003745
Sage Weilc5c9cd42008-11-12 14:32:25 -05003746 /* disko == 0 means it's a hole */
3747 if (!disko)
3748 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003749
3750 btrfs_set_file_extent_offset(leaf, extent,
3751 datao);
3752 btrfs_set_file_extent_num_bytes(leaf, extent,
3753 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003754
Sage Weilc5c9cd42008-11-12 14:32:25 -05003755 if (disko) {
3756 inode_add_bytes(inode, datal);
3757 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003758 disko, diskl, 0,
3759 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003760 btrfs_ino(inode),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003761 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003762 if (ret) {
3763 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003764 ret);
3765 btrfs_end_transaction(trans,
3766 root);
3767 goto out;
3768
3769 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003770 }
3771 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3772 u64 skip = 0;
3773 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003774
Sage Weilc5c9cd42008-11-12 14:32:25 -05003775 if (off > key.offset) {
3776 skip = off - key.offset;
3777 new_key.offset += skip;
3778 }
Chris Masond3977122009-01-05 21:25:51 -05003779
Liu Boaa42ffd2012-09-18 03:52:23 -06003780 if (key.offset + datal > off + len)
3781 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003782
Sage Weilc5c9cd42008-11-12 14:32:25 -05003783 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003784 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003785 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003786 goto out;
3787 }
3788 size -= skip + trim;
3789 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003790
Filipe Manana8039d872015-10-13 15:15:00 +01003791 ret = clone_copy_inline_extent(src, inode,
3792 trans, path,
3793 &new_key,
3794 drop_start,
3795 datal,
3796 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003797 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003798 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003799 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003800 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003801 btrfs_end_transaction(trans, root);
3802 goto out;
3803 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003804 leaf = path->nodes[0];
3805 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003806 }
3807
Filipe Manana7ffbb592014-06-09 03:48:05 +01003808 /* If we have an implicit hole (NO_HOLES feature). */
3809 if (drop_start < new_key.offset)
3810 clone_update_extent_map(inode, trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003811 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003812 new_key.offset - drop_start);
3813
Filipe Manana14f59792014-06-29 21:45:40 +01003814 clone_update_extent_map(inode, trans, path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003815
Sage Weilc5c9cd42008-11-12 14:32:25 -05003816 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003817 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003818
Filipe Manana62e23902014-08-08 02:47:06 +01003819 last_dest_end = ALIGN(new_key.offset + datal,
3820 root->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003821 ret = clone_finish_inode_update(trans, inode,
3822 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003823 destoff, olen,
3824 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003825 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003826 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003827 if (new_key.offset + datal >= destoff + len)
3828 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003829 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003830 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003831 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003832
3833 if (fatal_signal_pending(current)) {
3834 ret = -EINTR;
3835 goto out;
3836 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003837 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003838 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003839
Filipe Mananaf82a9902014-06-01 01:50:28 +01003840 if (last_dest_end < destoff + len) {
3841 /*
3842 * We have an implicit hole (NO_HOLES feature is enabled) that
3843 * fully or partially overlaps our cloning range at its end.
3844 */
3845 btrfs_release_path(path);
3846
3847 /*
3848 * 1 - remove extent(s)
3849 * 1 - inode update
3850 */
3851 trans = btrfs_start_transaction(root, 2);
3852 if (IS_ERR(trans)) {
3853 ret = PTR_ERR(trans);
3854 goto out;
3855 }
3856 ret = btrfs_drop_extents(trans, root, inode,
3857 last_dest_end, destoff + len, 1);
3858 if (ret) {
3859 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003860 btrfs_abort_transaction(trans, ret);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003861 btrfs_end_transaction(trans, root);
3862 goto out;
3863 }
Filipe Manana14f59792014-06-29 21:45:40 +01003864 clone_update_extent_map(inode, trans, NULL, last_dest_end,
3865 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003866 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003867 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003868 }
3869
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003870out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003871 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003872 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003873 return ret;
3874}
3875
Zach Brown3db11b22015-11-10 16:53:32 -05003876static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3877 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003878{
Al Viro54563d42013-09-01 15:57:51 -04003879 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003880 struct inode *src = file_inode(file_src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003881 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003882 int ret;
3883 u64 len = olen;
3884 u64 bs = root->fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003885 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003886
3887 /*
3888 * TODO:
3889 * - split compressed inline extents. annoying: we need to
3890 * decompress into destination's address_space (the file offset
3891 * may change, so source mapping won't do), then recompress (or
3892 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003893 *
3894 * - split destination inode's inline extents. The inline extents can
3895 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003896 */
3897
Mark Fasheh32b7c682013-08-06 11:42:49 -07003898 if (btrfs_root_readonly(root))
3899 return -EROFS;
3900
Zach Brown3db11b22015-11-10 16:53:32 -05003901 if (file_src->f_path.mnt != file->f_path.mnt ||
3902 src->i_sb != inode->i_sb)
3903 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003904
Mark Fasheh32b7c682013-08-06 11:42:49 -07003905 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003906 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003907
3908 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003909 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003910 } else {
Al Viro59551022016-01-22 15:40:57 -05003911 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003912 }
3913
Omar Sandoval52ea25b2018-05-22 15:02:12 -07003914 /* don't make the dst file partly checksummed */
3915 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3916 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
3917 ret = -EINVAL;
3918 goto out_unlock;
3919 }
3920
Mark Fasheh32b7c682013-08-06 11:42:49 -07003921 /* determine range to clone */
3922 ret = -EINVAL;
3923 if (off + len > src->i_size || off + len < off)
3924 goto out_unlock;
3925 if (len == 0)
3926 olen = len = src->i_size - off;
Filipe Manana800d8112018-11-05 11:14:17 +00003927 /*
3928 * If we extend to eof, continue to block boundary if and only if the
3929 * destination end offset matches the destination file's size, otherwise
3930 * we would be corrupting data by placing the eof block into the middle
3931 * of a file.
3932 */
3933 if (off + len == src->i_size) {
3934 if (!IS_ALIGNED(len, bs) && destoff + len < inode->i_size)
3935 goto out_unlock;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003936 len = ALIGN(src->i_size, bs) - off;
Filipe Manana800d8112018-11-05 11:14:17 +00003937 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003938
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003939 if (len == 0) {
3940 ret = 0;
3941 goto out_unlock;
3942 }
3943
Mark Fasheh32b7c682013-08-06 11:42:49 -07003944 /* verify the end result is block aligned */
3945 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3946 !IS_ALIGNED(destoff, bs))
3947 goto out_unlock;
3948
3949 /* verify if ranges are overlapped within the same file */
3950 if (same_inode) {
3951 if (destoff + len > off && destoff < off + len)
3952 goto out_unlock;
3953 }
3954
3955 if (destoff > inode->i_size) {
3956 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3957 if (ret)
3958 goto out_unlock;
3959 }
3960
Filipe Mananac125b8b2014-05-23 05:03:34 +01003961 /*
3962 * Lock the target range too. Right after we replace the file extent
3963 * items in the fs tree (which now point to the cloned data), we might
3964 * have a worker replace them with extent items relative to a write
3965 * operation that was issued before this clone operation (i.e. confront
3966 * with inode.c:btrfs_finish_ordered_io).
3967 */
3968 if (same_inode) {
3969 u64 lock_start = min_t(u64, off, destoff);
3970 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003971
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003972 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003973 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003974 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3975 true);
3976 }
3977 ASSERT(ret == 0);
3978 if (WARN_ON(ret)) {
3979 /* ranges in the io trees already unlocked */
3980 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003981 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003982
Mark Fasheh1c919a52015-06-30 14:42:08 -07003983 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003984
Filipe Mananac125b8b2014-05-23 05:03:34 +01003985 if (same_inode) {
3986 u64 lock_start = min_t(u64, off, destoff);
3987 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3988
3989 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3990 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003991 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003992 }
3993 /*
3994 * Truncate page cache pages so that future reads will see the cloned
3995 * data immediately and not the previous data.
3996 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303997 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003998 round_down(destoff, PAGE_SIZE),
3999 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004000out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07004001 if (!same_inode)
4002 btrfs_double_inode_unlock(src, inode);
4003 else
Al Viro59551022016-01-22 15:40:57 -05004004 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05004005 return ret;
4006}
4007
4008ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
4009 struct file *file_out, loff_t pos_out,
4010 size_t len, unsigned int flags)
4011{
4012 ssize_t ret;
4013
4014 ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
4015 if (ret == 0)
4016 ret = len;
4017 return ret;
4018}
4019
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004020int btrfs_clone_file_range(struct file *src_file, loff_t off,
4021 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05004022{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004023 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004024}
4025
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004026/*
4027 * there are many ways the trans_start and trans_end ioctls can lead
4028 * to deadlocks. They should only be used by applications that
4029 * basically own the machine, and have a very in depth understanding
4030 * of all the possible deadlocks and enospc problems.
4031 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05004032static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004033{
Al Viro496ad9a2013-01-23 17:07:38 -05004034 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004035 struct btrfs_root *root = BTRFS_I(inode)->root;
4036 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004037 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004038
Sage Weil1ab86ae2009-09-29 18:38:44 -04004039 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04004040 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004041 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004042
4043 ret = -EINPROGRESS;
4044 if (file->private_data)
4045 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004046
Li Zefanb83cc962010-12-20 16:04:08 +08004047 ret = -EROFS;
4048 if (btrfs_root_readonly(root))
4049 goto out;
4050
Al Viroa561be72011-11-23 11:57:51 -05004051 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05004052 if (ret)
4053 goto out;
4054
Josef Bacika4abeea2011-04-11 17:25:13 -04004055 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004056
Sage Weil1ab86ae2009-09-29 18:38:44 -04004057 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004058 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00004059 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04004060 goto out_drop;
4061
4062 file->private_data = trans;
4063 return 0;
4064
4065out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04004066 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05004067 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004068out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004069 return ret;
4070}
4071
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004072static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4073{
Al Viro496ad9a2013-01-23 17:07:38 -05004074 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004075 struct btrfs_root *root = BTRFS_I(inode)->root;
4076 struct btrfs_root *new_root;
4077 struct btrfs_dir_item *di;
4078 struct btrfs_trans_handle *trans;
4079 struct btrfs_path *path;
4080 struct btrfs_key location;
4081 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004082 u64 objectid = 0;
4083 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004084 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004085
4086 if (!capable(CAP_SYS_ADMIN))
4087 return -EPERM;
4088
Miao Xie3c04ce02012-11-26 08:43:07 +00004089 ret = mnt_want_write_file(file);
4090 if (ret)
4091 return ret;
4092
4093 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4094 ret = -EFAULT;
4095 goto out;
4096 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004097
4098 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304099 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004100
4101 location.objectid = objectid;
4102 location.type = BTRFS_ROOT_ITEM_KEY;
4103 location.offset = (u64)-1;
4104
4105 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004106 if (IS_ERR(new_root)) {
4107 ret = PTR_ERR(new_root);
4108 goto out;
4109 }
satoru takeuchif11525d2017-09-12 22:42:52 +09004110 if (!is_fstree(new_root->objectid)) {
4111 ret = -ENOENT;
4112 goto out;
4113 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004114
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004115 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004116 if (!path) {
4117 ret = -ENOMEM;
4118 goto out;
4119 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004120 path->leave_spinning = 1;
4121
4122 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004123 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004124 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004125 ret = PTR_ERR(trans);
4126 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004127 }
4128
David Sterba6c417612011-04-13 15:41:04 +02004129 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004130 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
4131 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004132 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004133 btrfs_free_path(path);
4134 btrfs_end_transaction(trans, root);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004135 btrfs_err(new_root->fs_info,
4136 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004137 ret = -ENOENT;
4138 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004139 }
4140
4141 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4142 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4143 btrfs_mark_buffer_dirty(path->nodes[0]);
4144 btrfs_free_path(path);
4145
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06004146 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004147 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00004148out:
4149 mnt_drop_write_file(file);
4150 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004151}
4152
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004153void btrfs_get_block_group_info(struct list_head *groups_list,
4154 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004155{
4156 struct btrfs_block_group_cache *block_group;
4157
4158 space->total_bytes = 0;
4159 space->used_bytes = 0;
4160 space->flags = 0;
4161 list_for_each_entry(block_group, groups_list, list) {
4162 space->flags = block_group->flags;
4163 space->total_bytes += block_group->key.offset;
4164 space->used_bytes +=
4165 btrfs_block_group_used(&block_group->item);
4166 }
4167}
4168
Eric Sandeen48a3b632013-04-25 20:41:01 +00004169static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004170{
4171 struct btrfs_ioctl_space_args space_args;
4172 struct btrfs_ioctl_space_info space;
4173 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004174 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004175 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004176 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004177 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4178 BTRFS_BLOCK_GROUP_SYSTEM,
4179 BTRFS_BLOCK_GROUP_METADATA,
4180 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4181 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004182 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004183 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004184 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004185 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004186
4187 if (copy_from_user(&space_args,
4188 (struct btrfs_ioctl_space_args __user *)arg,
4189 sizeof(space_args)))
4190 return -EFAULT;
4191
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004192 for (i = 0; i < num_types; i++) {
4193 struct btrfs_space_info *tmp;
4194
4195 info = NULL;
4196 rcu_read_lock();
4197 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4198 list) {
4199 if (tmp->flags == types[i]) {
4200 info = tmp;
4201 break;
4202 }
4203 }
4204 rcu_read_unlock();
4205
4206 if (!info)
4207 continue;
4208
4209 down_read(&info->groups_sem);
4210 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4211 if (!list_empty(&info->block_groups[c]))
4212 slot_count++;
4213 }
4214 up_read(&info->groups_sem);
4215 }
Josef Bacik1406e432010-01-13 18:19:06 +00004216
David Sterba36523e952014-02-07 14:34:12 +01004217 /*
4218 * Global block reserve, exported as a space_info
4219 */
4220 slot_count++;
4221
Chris Mason7fde62b2010-03-16 15:40:10 -04004222 /* space_slots == 0 means they are asking for a count */
4223 if (space_args.space_slots == 0) {
4224 space_args.total_spaces = slot_count;
4225 goto out;
4226 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004227
Dan Rosenberg51788b12011-02-14 16:04:23 -05004228 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004229
Chris Mason7fde62b2010-03-16 15:40:10 -04004230 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004231
Chris Mason7fde62b2010-03-16 15:40:10 -04004232 /* we generally have at most 6 or so space infos, one for each raid
4233 * level. So, a whole page should be more than enough for everyone
4234 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004235 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004236 return -ENOMEM;
4237
4238 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004239 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004240 if (!dest)
4241 return -ENOMEM;
4242 dest_orig = dest;
4243
4244 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004245 for (i = 0; i < num_types; i++) {
4246 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004247
Dan Rosenberg51788b12011-02-14 16:04:23 -05004248 if (!slot_count)
4249 break;
4250
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004251 info = NULL;
4252 rcu_read_lock();
4253 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4254 list) {
4255 if (tmp->flags == types[i]) {
4256 info = tmp;
4257 break;
4258 }
4259 }
4260 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004261
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004262 if (!info)
4263 continue;
4264 down_read(&info->groups_sem);
4265 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4266 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004267 btrfs_get_block_group_info(
4268 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004269 memcpy(dest, &space, sizeof(space));
4270 dest++;
4271 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004272 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004273 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004274 if (!slot_count)
4275 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004276 }
4277 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004278 }
Josef Bacik1406e432010-01-13 18:19:06 +00004279
David Sterba36523e952014-02-07 14:34:12 +01004280 /*
4281 * Add global block reserve
4282 */
4283 if (slot_count) {
4284 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
4285
4286 spin_lock(&block_rsv->lock);
4287 space.total_bytes = block_rsv->size;
4288 space.used_bytes = block_rsv->size - block_rsv->reserved;
4289 spin_unlock(&block_rsv->lock);
4290 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4291 memcpy(dest, &space, sizeof(space));
4292 space_args.total_spaces++;
4293 }
4294
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004295 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004296 (arg + sizeof(struct btrfs_ioctl_space_args));
4297
4298 if (copy_to_user(user_dest, dest_orig, alloc_size))
4299 ret = -EFAULT;
4300
4301 kfree(dest_orig);
4302out:
4303 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004304 ret = -EFAULT;
4305
4306 return ret;
4307}
4308
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004309/*
4310 * there are many ways the trans_start and trans_end ioctls can lead
4311 * to deadlocks. They should only be used by applications that
4312 * basically own the machine, and have a very in depth understanding
4313 * of all the possible deadlocks and enospc problems.
4314 */
4315long btrfs_ioctl_trans_end(struct file *file)
4316{
Al Viro496ad9a2013-01-23 17:07:38 -05004317 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004318 struct btrfs_root *root = BTRFS_I(inode)->root;
4319 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004320
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004321 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004322 if (!trans)
4323 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004324 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004325
Sage Weil1ab86ae2009-09-29 18:38:44 -04004326 btrfs_end_transaction(trans, root);
4327
Josef Bacika4abeea2011-04-11 17:25:13 -04004328 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004329
Al Viro2a79f172011-12-09 08:06:57 -05004330 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004331 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004332}
4333
Miao Xie9a8c28b2012-11-26 08:40:43 +00004334static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4335 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004336{
Sage Weil46204592010-10-29 15:41:32 -04004337 struct btrfs_trans_handle *trans;
4338 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004339 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004340
Miao Xied4edf392013-02-20 09:17:06 +00004341 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004342 if (IS_ERR(trans)) {
4343 if (PTR_ERR(trans) != -ENOENT)
4344 return PTR_ERR(trans);
4345
4346 /* No running transaction, don't bother */
4347 transid = root->fs_info->last_trans_committed;
4348 goto out;
4349 }
Sage Weil46204592010-10-29 15:41:32 -04004350 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004351 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004352 if (ret) {
4353 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004354 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004355 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004356out:
Sage Weil46204592010-10-29 15:41:32 -04004357 if (argp)
4358 if (copy_to_user(argp, &transid, sizeof(transid)))
4359 return -EFAULT;
4360 return 0;
4361}
4362
Miao Xie9a8c28b2012-11-26 08:40:43 +00004363static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4364 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004365{
Sage Weil46204592010-10-29 15:41:32 -04004366 u64 transid;
4367
4368 if (argp) {
4369 if (copy_from_user(&transid, argp, sizeof(transid)))
4370 return -EFAULT;
4371 } else {
4372 transid = 0; /* current trans */
4373 }
4374 return btrfs_wait_for_commit(root, transid);
4375}
4376
Miao Xieb8e95482012-11-26 08:48:01 +00004377static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004378{
Al Viro496ad9a2013-01-23 17:07:38 -05004379 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01004380 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004381 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004382
4383 if (!capable(CAP_SYS_ADMIN))
4384 return -EPERM;
4385
4386 sa = memdup_user(arg, sizeof(*sa));
4387 if (IS_ERR(sa))
4388 return PTR_ERR(sa);
4389
Miao Xieb8e95482012-11-26 08:48:01 +00004390 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4391 ret = mnt_want_write_file(file);
4392 if (ret)
4393 goto out;
4394 }
4395
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004396 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004397 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4398 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004399
4400 if (copy_to_user(arg, sa, sizeof(*sa)))
4401 ret = -EFAULT;
4402
Miao Xieb8e95482012-11-26 08:48:01 +00004403 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4404 mnt_drop_write_file(file);
4405out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004406 kfree(sa);
4407 return ret;
4408}
4409
4410static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4411{
4412 if (!capable(CAP_SYS_ADMIN))
4413 return -EPERM;
4414
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004415 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004416}
4417
4418static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4419 void __user *arg)
4420{
4421 struct btrfs_ioctl_scrub_args *sa;
4422 int ret;
4423
4424 if (!capable(CAP_SYS_ADMIN))
4425 return -EPERM;
4426
4427 sa = memdup_user(arg, sizeof(*sa));
4428 if (IS_ERR(sa))
4429 return PTR_ERR(sa);
4430
4431 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4432
4433 if (copy_to_user(arg, sa, sizeof(*sa)))
4434 ret = -EFAULT;
4435
4436 kfree(sa);
4437 return ret;
4438}
4439
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004440static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004441 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004442{
4443 struct btrfs_ioctl_get_dev_stats *sa;
4444 int ret;
4445
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004446 sa = memdup_user(arg, sizeof(*sa));
4447 if (IS_ERR(sa))
4448 return PTR_ERR(sa);
4449
David Sterbab27f7c02012-06-22 06:30:39 -06004450 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4451 kfree(sa);
4452 return -EPERM;
4453 }
4454
4455 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004456
4457 if (copy_to_user(arg, sa, sizeof(*sa)))
4458 ret = -EFAULT;
4459
4460 kfree(sa);
4461 return ret;
4462}
4463
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004464static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4465{
4466 struct btrfs_ioctl_dev_replace_args *p;
4467 int ret;
4468
4469 if (!capable(CAP_SYS_ADMIN))
4470 return -EPERM;
4471
4472 p = memdup_user(arg, sizeof(*p));
4473 if (IS_ERR(p))
4474 return PTR_ERR(p);
4475
4476 switch (p->cmd) {
4477 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004478 if (root->fs_info->sb->s_flags & MS_RDONLY) {
4479 ret = -EROFS;
4480 goto out;
4481 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004482 if (atomic_xchg(
4483 &root->fs_info->mutually_exclusive_operation_running,
4484 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004485 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004486 } else {
Anand Jainb5255452016-03-24 18:48:14 +08004487 ret = btrfs_dev_replace_by_ioctl(root, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004488 atomic_set(
4489 &root->fs_info->mutually_exclusive_operation_running,
4490 0);
4491 }
4492 break;
4493 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4494 btrfs_dev_replace_status(root->fs_info, p);
4495 ret = 0;
4496 break;
4497 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4498 ret = btrfs_dev_replace_cancel(root->fs_info, p);
4499 break;
4500 default:
4501 ret = -EINVAL;
4502 break;
4503 }
4504
4505 if (copy_to_user(arg, p, sizeof(*p)))
4506 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004507out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004508 kfree(p);
4509 return ret;
4510}
4511
Jan Schmidtd7728c92011-07-07 16:48:38 +02004512static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4513{
4514 int ret = 0;
4515 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004516 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004517 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004518 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004519 struct inode_fs_paths *ipath = NULL;
4520 struct btrfs_path *path;
4521
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004522 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004523 return -EPERM;
4524
4525 path = btrfs_alloc_path();
4526 if (!path) {
4527 ret = -ENOMEM;
4528 goto out;
4529 }
4530
4531 ipa = memdup_user(arg, sizeof(*ipa));
4532 if (IS_ERR(ipa)) {
4533 ret = PTR_ERR(ipa);
4534 ipa = NULL;
4535 goto out;
4536 }
4537
4538 size = min_t(u32, ipa->size, 4096);
4539 ipath = init_ipath(size, root, path);
4540 if (IS_ERR(ipath)) {
4541 ret = PTR_ERR(ipath);
4542 ipath = NULL;
4543 goto out;
4544 }
4545
4546 ret = paths_from_inode(ipa->inum, ipath);
4547 if (ret < 0)
4548 goto out;
4549
4550 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004551 rel_ptr = ipath->fspath->val[i] -
4552 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004553 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004554 }
4555
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004556 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4557 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004558 if (ret) {
4559 ret = -EFAULT;
4560 goto out;
4561 }
4562
4563out:
4564 btrfs_free_path(path);
4565 free_ipath(ipath);
4566 kfree(ipa);
4567
4568 return ret;
4569}
4570
4571static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4572{
4573 struct btrfs_data_container *inodes = ctx;
4574 const size_t c = 3 * sizeof(u64);
4575
4576 if (inodes->bytes_left >= c) {
4577 inodes->bytes_left -= c;
4578 inodes->val[inodes->elem_cnt] = inum;
4579 inodes->val[inodes->elem_cnt + 1] = offset;
4580 inodes->val[inodes->elem_cnt + 2] = root;
4581 inodes->elem_cnt += 3;
4582 } else {
4583 inodes->bytes_missing += c - inodes->bytes_left;
4584 inodes->bytes_left = 0;
4585 inodes->elem_missed += 3;
4586 }
4587
4588 return 0;
4589}
4590
4591static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4592 void __user *arg)
4593{
4594 int ret = 0;
4595 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004596 struct btrfs_ioctl_logical_ino_args *loi;
4597 struct btrfs_data_container *inodes = NULL;
4598 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004599
4600 if (!capable(CAP_SYS_ADMIN))
4601 return -EPERM;
4602
4603 loi = memdup_user(arg, sizeof(*loi));
4604 if (IS_ERR(loi)) {
4605 ret = PTR_ERR(loi);
4606 loi = NULL;
4607 goto out;
4608 }
4609
4610 path = btrfs_alloc_path();
4611 if (!path) {
4612 ret = -ENOMEM;
4613 goto out;
4614 }
4615
Byongho Leeee221842015-12-15 01:42:10 +09004616 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004617 inodes = init_data_container(size);
4618 if (IS_ERR(inodes)) {
4619 ret = PTR_ERR(inodes);
4620 inodes = NULL;
4621 goto out;
4622 }
4623
Liu Bodf031f02012-09-07 20:01:29 -06004624 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4625 build_ino_list, inodes);
4626 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004627 ret = -ENOENT;
4628 if (ret < 0)
4629 goto out;
4630
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004631 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4632 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004633 if (ret)
4634 ret = -EFAULT;
4635
4636out:
4637 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06004638 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004639 kfree(loi);
4640
4641 return ret;
4642}
4643
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004644void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004645 struct btrfs_ioctl_balance_args *bargs)
4646{
4647 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4648
4649 bargs->flags = bctl->flags;
4650
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004651 if (atomic_read(&fs_info->balance_running))
4652 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4653 if (atomic_read(&fs_info->balance_pause_req))
4654 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004655 if (atomic_read(&fs_info->balance_cancel_req))
4656 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004657
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004658 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4659 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4660 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004661
4662 if (lock) {
4663 spin_lock(&fs_info->balance_lock);
4664 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4665 spin_unlock(&fs_info->balance_lock);
4666 } else {
4667 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4668 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004669}
4670
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004671static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004672{
Al Viro496ad9a2013-01-23 17:07:38 -05004673 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004674 struct btrfs_fs_info *fs_info = root->fs_info;
4675 struct btrfs_ioctl_balance_args *bargs;
4676 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004677 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004678 int ret;
4679
4680 if (!capable(CAP_SYS_ADMIN))
4681 return -EPERM;
4682
Liu Boe54bfa32012-06-29 03:58:48 -06004683 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004684 if (ret)
4685 return ret;
4686
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004687again:
4688 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4689 mutex_lock(&fs_info->volume_mutex);
4690 mutex_lock(&fs_info->balance_mutex);
4691 need_unlock = true;
4692 goto locked;
4693 }
4694
4695 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004696 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004697 * (1) some other op is running
4698 * (2) balance is running
4699 * (3) balance is paused -- special case (think resume)
4700 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004701 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004702 if (fs_info->balance_ctl) {
4703 /* this is either (2) or (3) */
4704 if (!atomic_read(&fs_info->balance_running)) {
4705 mutex_unlock(&fs_info->balance_mutex);
4706 if (!mutex_trylock(&fs_info->volume_mutex))
4707 goto again;
4708 mutex_lock(&fs_info->balance_mutex);
4709
4710 if (fs_info->balance_ctl &&
4711 !atomic_read(&fs_info->balance_running)) {
4712 /* this is (3) */
4713 need_unlock = false;
4714 goto locked;
4715 }
4716
4717 mutex_unlock(&fs_info->balance_mutex);
4718 mutex_unlock(&fs_info->volume_mutex);
4719 goto again;
4720 } else {
4721 /* this is (2) */
4722 mutex_unlock(&fs_info->balance_mutex);
4723 ret = -EINPROGRESS;
4724 goto out;
4725 }
4726 } else {
4727 /* this is (1) */
4728 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004729 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004730 goto out;
4731 }
4732
4733locked:
4734 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004735
4736 if (arg) {
4737 bargs = memdup_user(arg, sizeof(*bargs));
4738 if (IS_ERR(bargs)) {
4739 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004740 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004741 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004742
4743 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4744 if (!fs_info->balance_ctl) {
4745 ret = -ENOTCONN;
4746 goto out_bargs;
4747 }
4748
4749 bctl = fs_info->balance_ctl;
4750 spin_lock(&fs_info->balance_lock);
4751 bctl->flags |= BTRFS_BALANCE_RESUME;
4752 spin_unlock(&fs_info->balance_lock);
4753
4754 goto do_balance;
4755 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004756 } else {
4757 bargs = NULL;
4758 }
4759
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004760 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004761 ret = -EINPROGRESS;
4762 goto out_bargs;
4763 }
4764
David Sterba8d2db782015-11-04 15:38:29 +01004765 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004766 if (!bctl) {
4767 ret = -ENOMEM;
4768 goto out_bargs;
4769 }
4770
4771 bctl->fs_info = fs_info;
4772 if (arg) {
4773 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4774 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4775 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4776
4777 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004778 } else {
4779 /* balance everything - no filters */
4780 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004781 }
4782
David Sterba8eb93452015-10-12 16:55:54 +02004783 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4784 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004785 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004786 }
4787
Ilya Dryomovde322262012-01-16 22:04:49 +02004788do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004789 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004790 * Ownership of bctl and mutually_exclusive_operation_running
4791 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4792 * or, if restriper was paused all the way until unmount, in
4793 * free_fs_info. mutually_exclusive_operation_running is
4794 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004795 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004796 need_unlock = false;
4797
4798 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004799 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004800
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004801 if (arg) {
4802 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4803 ret = -EFAULT;
4804 }
4805
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004806out_bctl:
4807 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004808out_bargs:
4809 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004810out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004811 mutex_unlock(&fs_info->balance_mutex);
4812 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004813 if (need_unlock)
4814 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4815out:
Liu Boe54bfa32012-06-29 03:58:48 -06004816 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004817 return ret;
4818}
4819
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004820static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4821{
4822 if (!capable(CAP_SYS_ADMIN))
4823 return -EPERM;
4824
4825 switch (cmd) {
4826 case BTRFS_BALANCE_CTL_PAUSE:
4827 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004828 case BTRFS_BALANCE_CTL_CANCEL:
4829 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004830 }
4831
4832 return -EINVAL;
4833}
4834
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004835static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4836 void __user *arg)
4837{
4838 struct btrfs_fs_info *fs_info = root->fs_info;
4839 struct btrfs_ioctl_balance_args *bargs;
4840 int ret = 0;
4841
4842 if (!capable(CAP_SYS_ADMIN))
4843 return -EPERM;
4844
4845 mutex_lock(&fs_info->balance_mutex);
4846 if (!fs_info->balance_ctl) {
4847 ret = -ENOTCONN;
4848 goto out;
4849 }
4850
David Sterba8d2db782015-11-04 15:38:29 +01004851 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004852 if (!bargs) {
4853 ret = -ENOMEM;
4854 goto out;
4855 }
4856
4857 update_ioctl_balance_args(fs_info, 1, bargs);
4858
4859 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4860 ret = -EFAULT;
4861
4862 kfree(bargs);
4863out:
4864 mutex_unlock(&fs_info->balance_mutex);
4865 return ret;
4866}
4867
Miao Xie905b0dd2012-11-26 08:50:11 +00004868static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004869{
Al Viro496ad9a2013-01-23 17:07:38 -05004870 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004871 struct btrfs_ioctl_quota_ctl_args *sa;
4872 struct btrfs_trans_handle *trans = NULL;
4873 int ret;
4874 int err;
4875
4876 if (!capable(CAP_SYS_ADMIN))
4877 return -EPERM;
4878
Miao Xie905b0dd2012-11-26 08:50:11 +00004879 ret = mnt_want_write_file(file);
4880 if (ret)
4881 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004882
4883 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004884 if (IS_ERR(sa)) {
4885 ret = PTR_ERR(sa);
4886 goto drop_write;
4887 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004888
Wang Shilong7708f022013-04-07 10:24:57 +00004889 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004890 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4891 if (IS_ERR(trans)) {
4892 ret = PTR_ERR(trans);
4893 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004894 }
4895
4896 switch (sa->cmd) {
4897 case BTRFS_QUOTA_CTL_ENABLE:
4898 ret = btrfs_quota_enable(trans, root->fs_info);
4899 break;
4900 case BTRFS_QUOTA_CTL_DISABLE:
4901 ret = btrfs_quota_disable(trans, root->fs_info);
4902 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004903 default:
4904 ret = -EINVAL;
4905 break;
4906 }
4907
Jan Schmidt2f232032013-04-25 16:04:51 +00004908 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4909 if (err && !ret)
4910 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004911out:
4912 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004913 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004914drop_write:
4915 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004916 return ret;
4917}
4918
Miao Xie905b0dd2012-11-26 08:50:11 +00004919static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004920{
Al Viro496ad9a2013-01-23 17:07:38 -05004921 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004922 struct btrfs_ioctl_qgroup_assign_args *sa;
4923 struct btrfs_trans_handle *trans;
4924 int ret;
4925 int err;
4926
4927 if (!capable(CAP_SYS_ADMIN))
4928 return -EPERM;
4929
Miao Xie905b0dd2012-11-26 08:50:11 +00004930 ret = mnt_want_write_file(file);
4931 if (ret)
4932 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004933
4934 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004935 if (IS_ERR(sa)) {
4936 ret = PTR_ERR(sa);
4937 goto drop_write;
4938 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004939
4940 trans = btrfs_join_transaction(root);
4941 if (IS_ERR(trans)) {
4942 ret = PTR_ERR(trans);
4943 goto out;
4944 }
4945
4946 /* FIXME: check if the IDs really exist */
4947 if (sa->assign) {
4948 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4949 sa->src, sa->dst);
4950 } else {
4951 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4952 sa->src, sa->dst);
4953 }
4954
Qu Wenruoe082f562015-02-27 16:24:28 +08004955 /* update qgroup status and info */
4956 err = btrfs_run_qgroups(trans, root->fs_info);
4957 if (err < 0)
Anand Jainad8403d2016-03-10 12:22:15 +08004958 btrfs_handle_fs_error(root->fs_info, err,
4959 "failed to update qgroup status and info");
Arne Jansen5d13a372011-09-14 15:53:51 +02004960 err = btrfs_end_transaction(trans, root);
4961 if (err && !ret)
4962 ret = err;
4963
4964out:
4965 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004966drop_write:
4967 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004968 return ret;
4969}
4970
Miao Xie905b0dd2012-11-26 08:50:11 +00004971static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004972{
Al Viro496ad9a2013-01-23 17:07:38 -05004973 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004974 struct btrfs_ioctl_qgroup_create_args *sa;
4975 struct btrfs_trans_handle *trans;
4976 int ret;
4977 int err;
4978
4979 if (!capable(CAP_SYS_ADMIN))
4980 return -EPERM;
4981
Miao Xie905b0dd2012-11-26 08:50:11 +00004982 ret = mnt_want_write_file(file);
4983 if (ret)
4984 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004985
4986 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004987 if (IS_ERR(sa)) {
4988 ret = PTR_ERR(sa);
4989 goto drop_write;
4990 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004991
Miao Xied86e56c2012-11-15 11:35:41 +00004992 if (!sa->qgroupid) {
4993 ret = -EINVAL;
4994 goto out;
4995 }
4996
Arne Jansen5d13a372011-09-14 15:53:51 +02004997 trans = btrfs_join_transaction(root);
4998 if (IS_ERR(trans)) {
4999 ret = PTR_ERR(trans);
5000 goto out;
5001 }
5002
5003 /* FIXME: check if the IDs really exist */
5004 if (sa->create) {
Dongsheng Yang4087cf22015-01-18 10:59:23 -05005005 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02005006 } else {
5007 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
5008 }
5009
5010 err = btrfs_end_transaction(trans, root);
5011 if (err && !ret)
5012 ret = err;
5013
5014out:
5015 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005016drop_write:
5017 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005018 return ret;
5019}
5020
Miao Xie905b0dd2012-11-26 08:50:11 +00005021static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005022{
Al Viro496ad9a2013-01-23 17:07:38 -05005023 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005024 struct btrfs_ioctl_qgroup_limit_args *sa;
5025 struct btrfs_trans_handle *trans;
5026 int ret;
5027 int err;
5028 u64 qgroupid;
5029
5030 if (!capable(CAP_SYS_ADMIN))
5031 return -EPERM;
5032
Miao Xie905b0dd2012-11-26 08:50:11 +00005033 ret = mnt_want_write_file(file);
5034 if (ret)
5035 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005036
5037 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005038 if (IS_ERR(sa)) {
5039 ret = PTR_ERR(sa);
5040 goto drop_write;
5041 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005042
5043 trans = btrfs_join_transaction(root);
5044 if (IS_ERR(trans)) {
5045 ret = PTR_ERR(trans);
5046 goto out;
5047 }
5048
5049 qgroupid = sa->qgroupid;
5050 if (!qgroupid) {
5051 /* take the current subvol as qgroup */
5052 qgroupid = root->root_key.objectid;
5053 }
5054
5055 /* FIXME: check if the IDs really exist */
5056 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
5057
5058 err = btrfs_end_transaction(trans, root);
5059 if (err && !ret)
5060 ret = err;
5061
5062out:
5063 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005064drop_write:
5065 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005066 return ret;
5067}
5068
Jan Schmidt2f232032013-04-25 16:04:51 +00005069static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5070{
Al Viro6d0379e2013-06-16 19:32:35 +04005071 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00005072 struct btrfs_ioctl_quota_rescan_args *qsa;
5073 int ret;
5074
5075 if (!capable(CAP_SYS_ADMIN))
5076 return -EPERM;
5077
5078 ret = mnt_want_write_file(file);
5079 if (ret)
5080 return ret;
5081
5082 qsa = memdup_user(arg, sizeof(*qsa));
5083 if (IS_ERR(qsa)) {
5084 ret = PTR_ERR(qsa);
5085 goto drop_write;
5086 }
5087
5088 if (qsa->flags) {
5089 ret = -EINVAL;
5090 goto out;
5091 }
5092
5093 ret = btrfs_qgroup_rescan(root->fs_info);
5094
5095out:
5096 kfree(qsa);
5097drop_write:
5098 mnt_drop_write_file(file);
5099 return ret;
5100}
5101
5102static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5103{
Al Viro6d0379e2013-06-16 19:32:35 +04005104 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00005105 struct btrfs_ioctl_quota_rescan_args *qsa;
5106 int ret = 0;
5107
5108 if (!capable(CAP_SYS_ADMIN))
5109 return -EPERM;
5110
David Sterba8d2db782015-11-04 15:38:29 +01005111 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005112 if (!qsa)
5113 return -ENOMEM;
5114
5115 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5116 qsa->flags = 1;
5117 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
5118 }
5119
5120 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5121 ret = -EFAULT;
5122
5123 kfree(qsa);
5124 return ret;
5125}
5126
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005127static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5128{
Al Viro54563d42013-09-01 15:57:51 -04005129 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005130
5131 if (!capable(CAP_SYS_ADMIN))
5132 return -EPERM;
5133
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04005134 return btrfs_qgroup_wait_for_completion(root->fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005135}
5136
Hugo Millsabccd002014-01-30 20:17:00 +00005137static long _btrfs_ioctl_set_received_subvol(struct file *file,
5138 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005139{
Al Viro496ad9a2013-01-23 17:07:38 -05005140 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02005141 struct btrfs_root *root = BTRFS_I(inode)->root;
5142 struct btrfs_root_item *root_item = &root->root_item;
5143 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005144 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005145 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005146 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005147
David Sterbabd60ea02014-01-16 15:50:22 +01005148 if (!inode_owner_or_capable(inode))
5149 return -EPERM;
5150
Alexander Block8ea05e32012-07-25 17:35:53 +02005151 ret = mnt_want_write_file(file);
5152 if (ret < 0)
5153 return ret;
5154
5155 down_write(&root->fs_info->subvol_sem);
5156
5157 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
5158 ret = -EINVAL;
5159 goto out;
5160 }
5161
5162 if (btrfs_root_readonly(root)) {
5163 ret = -EROFS;
5164 goto out;
5165 }
5166
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005167 /*
5168 * 1 - root item
5169 * 2 - uuid items (received uuid + subvol uuid)
5170 */
5171 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005172 if (IS_ERR(trans)) {
5173 ret = PTR_ERR(trans);
5174 trans = NULL;
5175 goto out;
5176 }
5177
5178 sa->rtransid = trans->transid;
5179 sa->rtime.sec = ct.tv_sec;
5180 sa->rtime.nsec = ct.tv_nsec;
5181
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005182 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5183 BTRFS_UUID_SIZE);
5184 if (received_uuid_changed &&
5185 !btrfs_is_empty_uuid(root_item->received_uuid))
5186 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
5187 root_item->received_uuid,
5188 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5189 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005190 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5191 btrfs_set_root_stransid(root_item, sa->stransid);
5192 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005193 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5194 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5195 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5196 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005197
5198 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5199 &root->root_key, &root->root_item);
5200 if (ret < 0) {
5201 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02005202 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005203 }
5204 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5205 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
5206 sa->uuid,
5207 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5208 root->root_key.objectid);
5209 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005210 btrfs_abort_transaction(trans, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005211 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005212 }
5213 }
5214 ret = btrfs_commit_transaction(trans, root);
5215 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005216 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005217 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005218 }
5219
Hugo Millsabccd002014-01-30 20:17:00 +00005220out:
5221 up_write(&root->fs_info->subvol_sem);
5222 mnt_drop_write_file(file);
5223 return ret;
5224}
5225
5226#ifdef CONFIG_64BIT
5227static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5228 void __user *arg)
5229{
5230 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5231 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5232 int ret = 0;
5233
5234 args32 = memdup_user(arg, sizeof(*args32));
5235 if (IS_ERR(args32)) {
5236 ret = PTR_ERR(args32);
5237 args32 = NULL;
5238 goto out;
5239 }
5240
David Sterba8d2db782015-11-04 15:38:29 +01005241 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005242 if (!args64) {
5243 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005244 goto out;
5245 }
5246
5247 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5248 args64->stransid = args32->stransid;
5249 args64->rtransid = args32->rtransid;
5250 args64->stime.sec = args32->stime.sec;
5251 args64->stime.nsec = args32->stime.nsec;
5252 args64->rtime.sec = args32->rtime.sec;
5253 args64->rtime.nsec = args32->rtime.nsec;
5254 args64->flags = args32->flags;
5255
5256 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5257 if (ret)
5258 goto out;
5259
5260 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5261 args32->stransid = args64->stransid;
5262 args32->rtransid = args64->rtransid;
5263 args32->stime.sec = args64->stime.sec;
5264 args32->stime.nsec = args64->stime.nsec;
5265 args32->rtime.sec = args64->rtime.sec;
5266 args32->rtime.nsec = args64->rtime.nsec;
5267 args32->flags = args64->flags;
5268
5269 ret = copy_to_user(arg, args32, sizeof(*args32));
5270 if (ret)
5271 ret = -EFAULT;
5272
5273out:
5274 kfree(args32);
5275 kfree(args64);
5276 return ret;
5277}
5278#endif
5279
5280static long btrfs_ioctl_set_received_subvol(struct file *file,
5281 void __user *arg)
5282{
5283 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5284 int ret = 0;
5285
5286 sa = memdup_user(arg, sizeof(*sa));
5287 if (IS_ERR(sa)) {
5288 ret = PTR_ERR(sa);
5289 sa = NULL;
5290 goto out;
5291 }
5292
5293 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5294
5295 if (ret)
5296 goto out;
5297
Alexander Block8ea05e32012-07-25 17:35:53 +02005298 ret = copy_to_user(arg, sa, sizeof(*sa));
5299 if (ret)
5300 ret = -EFAULT;
5301
5302out:
5303 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005304 return ret;
5305}
5306
jeff.liu867ab662013-01-05 02:48:01 +00005307static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5308{
Al Viro6d0379e2013-06-16 19:32:35 +04005309 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005310 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005311 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005312 char label[BTRFS_LABEL_SIZE];
5313
5314 spin_lock(&root->fs_info->super_lock);
5315 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5316 spin_unlock(&root->fs_info->super_lock);
5317
5318 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005319
5320 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005321 btrfs_warn(root->fs_info,
5322 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00005323 }
5324
jeff.liu867ab662013-01-05 02:48:01 +00005325 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005326
5327 return ret ? -EFAULT : 0;
5328}
5329
jeff.liua8bfd4a2013-01-05 02:48:08 +00005330static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5331{
Al Viro6d0379e2013-06-16 19:32:35 +04005332 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005333 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5334 struct btrfs_trans_handle *trans;
5335 char label[BTRFS_LABEL_SIZE];
5336 int ret;
5337
5338 if (!capable(CAP_SYS_ADMIN))
5339 return -EPERM;
5340
5341 if (copy_from_user(label, arg, sizeof(label)))
5342 return -EFAULT;
5343
5344 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005345 btrfs_err(root->fs_info,
5346 "unable to set label with more than %d bytes",
5347 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005348 return -EINVAL;
5349 }
5350
5351 ret = mnt_want_write_file(file);
5352 if (ret)
5353 return ret;
5354
jeff.liua8bfd4a2013-01-05 02:48:08 +00005355 trans = btrfs_start_transaction(root, 0);
5356 if (IS_ERR(trans)) {
5357 ret = PTR_ERR(trans);
5358 goto out_unlock;
5359 }
5360
Anand Jaina1b83ac2013-07-19 17:39:32 +08005361 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005362 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005363 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005364 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005365
5366out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005367 mnt_drop_write_file(file);
5368 return ret;
5369}
5370
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005371#define INIT_FEATURE_FLAGS(suffix) \
5372 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5373 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5374 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5375
David Sterbad5131b62016-02-17 15:26:27 +01005376int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005377{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005378 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005379 INIT_FEATURE_FLAGS(SUPP),
5380 INIT_FEATURE_FLAGS(SAFE_SET),
5381 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5382 };
5383
5384 if (copy_to_user(arg, &features, sizeof(features)))
5385 return -EFAULT;
5386
5387 return 0;
5388}
5389
5390static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5391{
5392 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5393 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5394 struct btrfs_ioctl_feature_flags features;
5395
5396 features.compat_flags = btrfs_super_compat_flags(super_block);
5397 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5398 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5399
5400 if (copy_to_user(arg, &features, sizeof(features)))
5401 return -EFAULT;
5402
5403 return 0;
5404}
5405
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005406static int check_feature_bits(struct btrfs_root *root,
5407 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005408 u64 change_mask, u64 flags, u64 supported_flags,
5409 u64 safe_set, u64 safe_clear)
5410{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005411 const char *type = btrfs_feature_set_names[set];
5412 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005413 u64 disallowed, unsupported;
5414 u64 set_mask = flags & change_mask;
5415 u64 clear_mask = ~flags & change_mask;
5416
5417 unsupported = set_mask & ~supported_flags;
5418 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005419 names = btrfs_printable_features(set, unsupported);
5420 if (names) {
5421 btrfs_warn(root->fs_info,
5422 "this kernel does not support the %s feature bit%s",
5423 names, strchr(names, ',') ? "s" : "");
5424 kfree(names);
5425 } else
5426 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005427 "this kernel does not support %s bits 0x%llx",
5428 type, unsupported);
5429 return -EOPNOTSUPP;
5430 }
5431
5432 disallowed = set_mask & ~safe_set;
5433 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005434 names = btrfs_printable_features(set, disallowed);
5435 if (names) {
5436 btrfs_warn(root->fs_info,
5437 "can't set the %s feature bit%s while mounted",
5438 names, strchr(names, ',') ? "s" : "");
5439 kfree(names);
5440 } else
5441 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005442 "can't set %s bits 0x%llx while mounted",
5443 type, disallowed);
5444 return -EPERM;
5445 }
5446
5447 disallowed = clear_mask & ~safe_clear;
5448 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005449 names = btrfs_printable_features(set, disallowed);
5450 if (names) {
5451 btrfs_warn(root->fs_info,
5452 "can't clear the %s feature bit%s while mounted",
5453 names, strchr(names, ',') ? "s" : "");
5454 kfree(names);
5455 } else
5456 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005457 "can't clear %s bits 0x%llx while mounted",
5458 type, disallowed);
5459 return -EPERM;
5460 }
5461
5462 return 0;
5463}
5464
5465#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005466check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005467 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5468 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5469 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5470
5471static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5472{
5473 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5474 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5475 struct btrfs_ioctl_feature_flags flags[2];
5476 struct btrfs_trans_handle *trans;
5477 u64 newflags;
5478 int ret;
5479
5480 if (!capable(CAP_SYS_ADMIN))
5481 return -EPERM;
5482
5483 if (copy_from_user(flags, arg, sizeof(flags)))
5484 return -EFAULT;
5485
5486 /* Nothing to do */
5487 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5488 !flags[0].incompat_flags)
5489 return 0;
5490
5491 ret = check_feature(root, flags[0].compat_flags,
5492 flags[1].compat_flags, COMPAT);
5493 if (ret)
5494 return ret;
5495
5496 ret = check_feature(root, flags[0].compat_ro_flags,
5497 flags[1].compat_ro_flags, COMPAT_RO);
5498 if (ret)
5499 return ret;
5500
5501 ret = check_feature(root, flags[0].incompat_flags,
5502 flags[1].incompat_flags, INCOMPAT);
5503 if (ret)
5504 return ret;
5505
David Sterba7ab19622016-05-04 11:32:00 +02005506 ret = mnt_want_write_file(file);
5507 if (ret)
5508 return ret;
5509
David Sterba8051aa12014-02-07 14:34:04 +01005510 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005511 if (IS_ERR(trans)) {
5512 ret = PTR_ERR(trans);
5513 goto out_drop_write;
5514 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005515
5516 spin_lock(&root->fs_info->super_lock);
5517 newflags = btrfs_super_compat_flags(super_block);
5518 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5519 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5520 btrfs_set_super_compat_flags(super_block, newflags);
5521
5522 newflags = btrfs_super_compat_ro_flags(super_block);
5523 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5524 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5525 btrfs_set_super_compat_ro_flags(super_block, newflags);
5526
5527 newflags = btrfs_super_incompat_flags(super_block);
5528 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5529 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5530 btrfs_set_super_incompat_flags(super_block, newflags);
5531 spin_unlock(&root->fs_info->super_lock);
5532
David Sterba7ab19622016-05-04 11:32:00 +02005533 ret = btrfs_commit_transaction(trans, root);
5534out_drop_write:
5535 mnt_drop_write_file(file);
5536
5537 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005538}
5539
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005540long btrfs_ioctl(struct file *file, unsigned int
5541 cmd, unsigned long arg)
5542{
Al Viro496ad9a2013-01-23 17:07:38 -05005543 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005544 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005545
5546 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005547 case FS_IOC_GETFLAGS:
5548 return btrfs_ioctl_getflags(file, argp);
5549 case FS_IOC_SETFLAGS:
5550 return btrfs_ioctl_setflags(file, argp);
5551 case FS_IOC_GETVERSION:
5552 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005553 case FITRIM:
5554 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005555 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005556 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005557 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005558 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005559 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005560 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005561 case BTRFS_IOC_SUBVOL_CREATE_V2:
5562 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005563 case BTRFS_IOC_SNAP_DESTROY:
5564 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005565 case BTRFS_IOC_SUBVOL_GETFLAGS:
5566 return btrfs_ioctl_subvol_getflags(file, argp);
5567 case BTRFS_IOC_SUBVOL_SETFLAGS:
5568 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005569 case BTRFS_IOC_DEFAULT_SUBVOL:
5570 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005571 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005572 return btrfs_ioctl_defrag(file, NULL);
5573 case BTRFS_IOC_DEFRAG_RANGE:
5574 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005575 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005576 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005577 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005578 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005579 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005580 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005581 case BTRFS_IOC_RM_DEV_V2:
5582 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005583 case BTRFS_IOC_FS_INFO:
5584 return btrfs_ioctl_fs_info(root, argp);
5585 case BTRFS_IOC_DEV_INFO:
5586 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005587 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005588 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005589 case BTRFS_IOC_TRANS_START:
5590 return btrfs_ioctl_trans_start(file);
5591 case BTRFS_IOC_TRANS_END:
5592 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005593 case BTRFS_IOC_TREE_SEARCH:
5594 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005595 case BTRFS_IOC_TREE_SEARCH_V2:
5596 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005597 case BTRFS_IOC_INO_LOOKUP:
5598 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005599 case BTRFS_IOC_INO_PATHS:
5600 return btrfs_ioctl_ino_to_path(root, argp);
5601 case BTRFS_IOC_LOGICAL_INO:
5602 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005603 case BTRFS_IOC_SPACE_INFO:
5604 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005605 case BTRFS_IOC_SYNC: {
5606 int ret;
5607
Miao Xie6c255e62014-03-06 13:55:01 +08005608 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005609 if (ret)
5610 return ret;
Al Viroddb52f42014-10-21 20:21:18 -04005611 ret = btrfs_sync_fs(file_inode(file)->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005612 /*
5613 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005614 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005615 * processing uncleaned subvols.
5616 */
5617 wake_up_process(root->fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005618 return ret;
5619 }
Sage Weil46204592010-10-29 15:41:32 -04005620 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005621 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005622 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005623 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005624 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005625 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005626 case BTRFS_IOC_SCRUB_CANCEL:
5627 return btrfs_ioctl_scrub_cancel(root, argp);
5628 case BTRFS_IOC_SCRUB_PROGRESS:
5629 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005630 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005631 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005632 case BTRFS_IOC_BALANCE_CTL:
5633 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005634 case BTRFS_IOC_BALANCE_PROGRESS:
5635 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005636 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5637 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005638#ifdef CONFIG_64BIT
5639 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5640 return btrfs_ioctl_set_received_subvol_32(file, argp);
5641#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005642 case BTRFS_IOC_SEND:
5643 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005644 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06005645 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005646 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005647 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005648 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005649 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005650 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005651 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005652 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005653 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005654 case BTRFS_IOC_QUOTA_RESCAN:
5655 return btrfs_ioctl_quota_rescan(file, argp);
5656 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5657 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005658 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5659 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005660 case BTRFS_IOC_DEV_REPLACE:
5661 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005662 case BTRFS_IOC_GET_FSLABEL:
5663 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005664 case BTRFS_IOC_SET_FSLABEL:
5665 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005666 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005667 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005668 case BTRFS_IOC_GET_FEATURES:
5669 return btrfs_ioctl_get_features(file, argp);
5670 case BTRFS_IOC_SET_FEATURES:
5671 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005672 }
5673
5674 return -ENOTTY;
5675}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005676
5677#ifdef CONFIG_COMPAT
5678long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5679{
Jeff Mahoney93fb0432017-02-06 19:39:09 -05005680 /*
5681 * These all access 32-bit values anyway so no further
5682 * handling is necessary.
5683 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005684 switch (cmd) {
5685 case FS_IOC32_GETFLAGS:
5686 cmd = FS_IOC_GETFLAGS;
5687 break;
5688 case FS_IOC32_SETFLAGS:
5689 cmd = FS_IOC_SETFLAGS;
5690 break;
5691 case FS_IOC32_GETVERSION:
5692 cmd = FS_IOC_GETVERSION;
5693 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005694 }
5695
5696 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5697}
5698#endif