blob: eefe103c65daaea6226eae1bf60b2e195760e597 [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);
Josef Bacike8da91e2019-12-06 09:37:15 -0500613 if (ret) {
614 btrfs_abort_transaction(trans, ret);
615 goto fail;
616 }
Yan Zheng52c26172009-01-05 15:43:43 -0500617
Chris Mason0660b5a2008-11-17 20:37:39 -0500618 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400619 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800620 btrfs_ino(dir), index, name, namelen);
Josef Bacike8da91e2019-12-06 09:37:15 -0500621 if (ret) {
622 btrfs_abort_transaction(trans, ret);
623 goto fail;
624 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500625
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200626 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
David Sterba49a3c4d2016-03-24 17:49:22 +0100627 root_item->uuid, BTRFS_UUID_KEY_SUBVOL,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200628 objectid);
629 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400630 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200631
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400632fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100633 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000634 trans->block_rsv = NULL;
635 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800636 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
637
Sage Weil72fd0322010-10-29 15:41:32 -0400638 if (async_transid) {
639 *async_transid = trans->transid;
640 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000641 if (err)
642 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400643 } else {
644 err = btrfs_commit_transaction(trans, root);
645 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400646 if (err && !ret)
647 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500648
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900649 if (!ret) {
650 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800651 if (IS_ERR(inode))
652 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900653 d_instantiate(dentry, inode);
654 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400655 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100656
657fail_free:
658 kfree(root_item);
659 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400660}
661
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000662static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +0800663{
664 s64 writers;
665 DEFINE_WAIT(wait);
666
667 do {
668 prepare_to_wait(&root->subv_writers->wait, &wait,
669 TASK_UNINTERRUPTIBLE);
670
671 writers = percpu_counter_sum(&root->subv_writers->counter);
672 if (writers)
673 schedule();
674
675 finish_wait(&root->subv_writers->wait, &wait);
676 } while (writers);
677}
678
Miao Xiee9662f72013-02-28 10:01:15 +0000679static int create_snapshot(struct btrfs_root *root, struct inode *dir,
680 struct dentry *dentry, char *name, int namelen,
681 u64 *async_transid, bool readonly,
682 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400683{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000684 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400685 struct btrfs_pending_snapshot *pending_snapshot;
686 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000687 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400688
Miao Xie27cdeb72014-04-02 19:51:05 +0800689 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400690 return -EINVAL;
691
David Sterbaa1ee7362015-11-10 18:53:56 +0100692 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
693 if (!pending_snapshot)
694 return -ENOMEM;
695
David Sterbab0c0ea62015-11-10 18:54:00 +0100696 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
697 GFP_NOFS);
David Sterba8546b572015-11-10 18:54:03 +0100698 pending_snapshot->path = btrfs_alloc_path();
699 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100700 ret = -ENOMEM;
701 goto free_pending;
702 }
703
Miao Xie8257b2d2014-03-06 13:38:19 +0800704 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100705 smp_mb__after_atomic();
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000706 btrfs_wait_for_no_snapshoting_writes(root);
Miao Xie8257b2d2014-03-06 13:38:19 +0800707
Miao Xie6a038432013-05-15 07:48:24 +0000708 ret = btrfs_start_delalloc_inodes(root, 0);
709 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100710 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000711
Filipe Manana578def72016-04-26 15:36:38 +0100712 btrfs_wait_ordered_extents(root, -1, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000713
Miao Xie66d8f3d2012-09-06 04:02:28 -0600714 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
715 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000716 /*
717 * 1 - parent dir inode
718 * 2 - dir entries
719 * 1 - root item
720 * 2 - root ref/backref
721 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200722 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000723 */
724 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200725 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400726 &pending_snapshot->qgroup_reserved,
727 false);
Miao Xied5c12072013-02-28 10:04:33 +0000728 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100729 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000730
Yan, Zhenga22285a2010-05-16 10:48:46 -0400731 pending_snapshot->dentry = dentry;
732 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800733 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000734 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000735 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400736
Miao Xied5c12072013-02-28 10:04:33 +0000737 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400738 if (IS_ERR(trans)) {
739 ret = PTR_ERR(trans);
740 goto fail;
741 }
742
Josef Bacik83515832011-06-14 15:16:14 -0400743 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400744 list_add(&pending_snapshot->list,
745 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400746 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400747 if (async_transid) {
748 *async_transid = trans->transid;
749 ret = btrfs_commit_transaction_async(trans,
750 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000751 if (ret)
752 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400753 } else {
754 ret = btrfs_commit_transaction(trans,
755 root->fs_info->extent_root);
756 }
Miao Xieaec80302013-03-04 09:44:29 +0000757 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400758 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400759
760 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400761 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000762 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763
Chris Masond3797302014-10-15 13:50:56 -0700764 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
765 if (ret)
766 goto fail;
767
David Howells2b0143b2015-03-17 22:25:59 +0000768 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000769 if (IS_ERR(inode)) {
770 ret = PTR_ERR(inode);
771 goto fail;
772 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900773
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000774 d_instantiate(dentry, inode);
775 ret = 0;
776fail:
Miao Xied5c12072013-02-28 10:04:33 +0000777 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
778 &pending_snapshot->block_rsv,
779 pending_snapshot->qgroup_reserved);
David Sterbaa1ee7362015-11-10 18:53:56 +0100780dec_and_free:
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000781 if (atomic_dec_and_test(&root->will_be_snapshoted))
782 wake_up_atomic_t(&root->will_be_snapshoted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100783free_pending:
784 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100785 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100786 kfree(pending_snapshot);
787
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400788 return ret;
789}
790
Sage Weil4260f7c2010-10-29 15:46:43 -0400791/* copy of may_delete in fs/namei.c()
792 * Check whether we can remove a link victim from directory dir, check
793 * whether the type of victim is right.
794 * 1. We can't do it if dir is read-only (done in permission())
795 * 2. We should have write and exec permissions on dir
796 * 3. We can't remove anything from append-only dir
797 * 4. We can't do anything with immutable dir (done in permission())
798 * 5. If the sticky bit on dir is set we should either
799 * a. be owner of dir, or
800 * b. be owner of victim, or
801 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400802 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400803 * links pointing to it.
804 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
805 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
806 * 9. We can't remove a root or mountpoint.
807 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
808 * nfs_async_unlink().
809 */
810
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530811static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400812{
813 int error;
814
David Howells2b0143b2015-03-17 22:25:59 +0000815 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400816 return -ENOENT;
817
David Howells2b0143b2015-03-17 22:25:59 +0000818 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400819 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400820
821 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
822 if (error)
823 return error;
824 if (IS_APPEND(dir))
825 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000826 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
827 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400828 return -EPERM;
829 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000830 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400831 return -ENOTDIR;
832 if (IS_ROOT(victim))
833 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000834 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400835 return -EISDIR;
836 if (IS_DEADDIR(dir))
837 return -ENOENT;
838 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
839 return -EBUSY;
840 return 0;
841}
842
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400843/* copy of may_create in fs/namei.c() */
844static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
845{
David Howells2b0143b2015-03-17 22:25:59 +0000846 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400847 return -EEXIST;
848 if (IS_DEADDIR(dir))
849 return -ENOENT;
850 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
851}
852
853/*
854 * Create a new subvolume below @parent. This is largely modeled after
855 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
856 * inside this filesystem so it's quite a bit simpler.
857 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400858static noinline int btrfs_mksubvol(struct path *parent,
859 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400860 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200861 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000862 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400863{
David Howells2b0143b2015-03-17 22:25:59 +0000864 struct inode *dir = d_inode(parent->dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400865 struct dentry *dentry;
866 int error;
867
Al Viro00235412016-05-26 00:05:12 -0400868 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
869 if (error == -EINTR)
870 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400871
872 dentry = lookup_one_len(name, parent->dentry, namelen);
873 error = PTR_ERR(dentry);
874 if (IS_ERR(dentry))
875 goto out_unlock;
876
Yan, Zheng76dda932009-09-21 16:00:26 -0400877 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400878 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600879 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400880
Chris Mason9c520572012-12-17 14:26:57 -0500881 /*
882 * even if this name doesn't exist, we may get hash collisions.
883 * check for them now when we can safely fail
884 */
885 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
886 dir->i_ino, name,
887 namelen);
888 if (error)
889 goto out_dput;
890
Yan, Zheng76dda932009-09-21 16:00:26 -0400891 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
892
893 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
894 goto out_up_read;
895
Chris Mason3de45862008-11-17 21:02:50 -0500896 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000897 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200898 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500899 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000900 error = create_subvol(dir, dentry, name, namelen,
901 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500902 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400903 if (!error)
904 fsnotify_mkdir(dir, dentry);
905out_up_read:
906 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400907out_dput:
908 dput(dentry);
909out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500910 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400911 return error;
912}
913
Chris Mason4cb53002011-05-24 15:35:30 -0400914/*
915 * When we're defragging a range, we don't want to kick it off again
916 * if it is really just waiting for delalloc to send it down.
917 * If we find a nice big extent or delalloc range for the bytes in the
918 * file you want to defrag, we return 0 to let you know to skip this
919 * part of the file
920 */
David Sterbaaab110a2014-07-29 17:32:10 +0200921static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400922{
923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
924 struct extent_map *em = NULL;
925 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
926 u64 end;
927
928 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300929 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -0400930 read_unlock(&em_tree->lock);
931
932 if (em) {
933 end = extent_map_end(em);
934 free_extent_map(em);
935 if (end - offset > thresh)
936 return 0;
937 }
938 /* if we already have a nice delalloc here, just stop */
939 thresh /= 2;
940 end = count_range_bits(io_tree, &offset, offset + thresh,
941 thresh, EXTENT_DELALLOC, 1);
942 if (end >= thresh)
943 return 0;
944 return 1;
945}
946
947/*
948 * helper function to walk through a file and find extents
949 * newer than a specific transid, and smaller than thresh.
950 *
951 * This is used by the defragging code to find new and small
952 * extents
953 */
954static int find_new_extents(struct btrfs_root *root,
955 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200956 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400957{
958 struct btrfs_path *path;
959 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400960 struct extent_buffer *leaf;
961 struct btrfs_file_extent_item *extent;
962 int type;
963 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000964 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400965
966 path = btrfs_alloc_path();
967 if (!path)
968 return -ENOMEM;
969
David Sterbaa4689d22011-05-31 17:08:14 +0000970 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400971 min_key.type = BTRFS_EXTENT_DATA_KEY;
972 min_key.offset = *off;
973
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530974 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100975 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400976 if (ret != 0)
977 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000978process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000979 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400980 goto none;
981 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
982 goto none;
983
984 leaf = path->nodes[0];
985 extent = btrfs_item_ptr(leaf, path->slots[0],
986 struct btrfs_file_extent_item);
987
988 type = btrfs_file_extent_type(leaf, extent);
989 if (type == BTRFS_FILE_EXTENT_REG &&
990 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
991 check_defrag_in_cache(inode, min_key.offset, thresh)) {
992 *off = min_key.offset;
993 btrfs_free_path(path);
994 return 0;
995 }
996
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000997 path->slots[0]++;
998 if (path->slots[0] < btrfs_header_nritems(leaf)) {
999 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1000 goto process_slot;
1001 }
1002
Chris Mason4cb53002011-05-24 15:35:30 -04001003 if (min_key.offset == (u64)-1)
1004 goto none;
1005
1006 min_key.offset++;
1007 btrfs_release_path(path);
1008 }
1009none:
1010 btrfs_free_path(path);
1011 return -ENOENT;
1012}
1013
Li Zefan6c282eb2012-06-11 16:03:35 +08001014static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001015{
1016 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001017 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001018 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001019 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001020
1021 /*
1022 * hopefully we have this extent in the tree already, try without
1023 * the full extent lock
1024 */
1025 read_lock(&em_tree->lock);
1026 em = lookup_extent_mapping(em_tree, start, len);
1027 read_unlock(&em_tree->lock);
1028
1029 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001030 struct extent_state *cached = NULL;
1031 u64 end = start + len - 1;
1032
Chris Mason940100a2010-03-10 10:52:59 -05001033 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001034 lock_extent_bits(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001035 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +00001036 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -05001037
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001038 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001039 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001040 }
1041
Li Zefan6c282eb2012-06-11 16:03:35 +08001042 return em;
1043}
1044
1045static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1046{
1047 struct extent_map *next;
1048 bool ret = true;
1049
1050 /* this is the last extent */
1051 if (em->start + em->len >= i_size_read(inode))
1052 return false;
1053
1054 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001055 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1056 ret = false;
1057 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001058 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001059 ret = false;
1060
1061 free_extent_map(next);
1062 return ret;
1063}
1064
David Sterbaaab110a2014-07-29 17:32:10 +02001065static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001066 u64 *last_len, u64 *skip, u64 *defrag_end,
1067 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001068{
1069 struct extent_map *em;
1070 int ret = 1;
1071 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001072 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001073
1074 /*
1075 * make sure that once we start defragging an extent, we keep on
1076 * defragging it
1077 */
1078 if (start < *defrag_end)
1079 return 1;
1080
1081 *skip = 0;
1082
1083 em = defrag_lookup_extent(inode, start);
1084 if (!em)
1085 return 0;
1086
Chris Mason940100a2010-03-10 10:52:59 -05001087 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001088 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001089 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001090 goto out;
1091 }
1092
Liu Bo4a3560c2015-08-07 16:48:41 +08001093 if (!*defrag_end)
1094 prev_mergeable = false;
1095
Li Zefan6c282eb2012-06-11 16:03:35 +08001096 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001097 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001098 * we hit a real extent, if it is big or the next extent is not a
1099 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001100 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001101 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001102 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001103 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001104out:
Chris Mason940100a2010-03-10 10:52:59 -05001105 /*
1106 * last_len ends up being a counter of how many bytes we've defragged.
1107 * every time we choose not to defrag an extent, we reset *last_len
1108 * so that the next tiny extent will force a defrag.
1109 *
1110 * The end result of this is that tiny extents before a single big
1111 * extent will force at least part of that big extent to be defragged.
1112 */
1113 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001114 *defrag_end = extent_map_end(em);
1115 } else {
1116 *last_len = 0;
1117 *skip = extent_map_end(em);
1118 *defrag_end = 0;
1119 }
1120
1121 free_extent_map(em);
1122 return ret;
1123}
1124
Chris Mason4cb53002011-05-24 15:35:30 -04001125/*
1126 * it doesn't do much good to defrag one or two pages
1127 * at a time. This pulls in a nice chunk of pages
1128 * to COW and defrag.
1129 *
1130 * It also makes sure the delalloc code has enough
1131 * dirty data to avoid making new small extents as part
1132 * of the defrag
1133 *
1134 * It's a good idea to start RA on this range
1135 * before calling this.
1136 */
1137static int cluster_pages_for_defrag(struct inode *inode,
1138 struct page **pages,
1139 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001140 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001141{
Chris Mason4cb53002011-05-24 15:35:30 -04001142 unsigned long file_end;
1143 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001144 u64 page_start;
1145 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001146 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001147 int ret;
1148 int i;
1149 int i_done;
1150 struct btrfs_ordered_extent *ordered;
1151 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001152 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001153 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001154
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001155 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001156 if (!isize || start_index > file_end)
1157 return 0;
1158
1159 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001160
1161 ret = btrfs_delalloc_reserve_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001162 start_index << PAGE_SHIFT,
1163 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001164 if (ret)
1165 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001166 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001167 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001168
1169 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001170 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001171 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001172again:
Josef Bacika94733d2011-07-11 10:47:06 -04001173 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001174 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001175 if (!page)
1176 break;
1177
Miao Xie600a45e2012-02-16 15:01:24 +08001178 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001179 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001180 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001181 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001182 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001183 ordered = btrfs_lookup_ordered_extent(inode,
1184 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001185 unlock_extent_cached(tree, page_start, page_end,
1186 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001187 if (!ordered)
1188 break;
1189
1190 unlock_page(page);
1191 btrfs_start_ordered_extent(inode, ordered, 1);
1192 btrfs_put_ordered_extent(ordered);
1193 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001194 /*
1195 * we unlocked the page above, so we need check if
1196 * it was released or not.
1197 */
1198 if (page->mapping != inode->i_mapping) {
1199 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001200 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001201 goto again;
1202 }
Miao Xie600a45e2012-02-16 15:01:24 +08001203 }
1204
Chris Mason4cb53002011-05-24 15:35:30 -04001205 if (!PageUptodate(page)) {
1206 btrfs_readpage(NULL, page);
1207 lock_page(page);
1208 if (!PageUptodate(page)) {
1209 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001210 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001211 ret = -EIO;
1212 break;
1213 }
1214 }
Miao Xie600a45e2012-02-16 15:01:24 +08001215
Miao Xie600a45e2012-02-16 15:01:24 +08001216 if (page->mapping != inode->i_mapping) {
1217 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001218 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001219 goto again;
1220 }
1221
Chris Mason4cb53002011-05-24 15:35:30 -04001222 pages[i] = page;
1223 i_done++;
1224 }
1225 if (!i_done || ret)
1226 goto out;
1227
1228 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1229 goto out;
1230
1231 /*
1232 * so now we have a nice long stream of locked
1233 * and up to date pages, lets wait on them
1234 */
1235 for (i = 0; i < i_done; i++)
1236 wait_on_page_writeback(pages[i]);
1237
1238 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001239 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001240
1241 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001242 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001243 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1244 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001245 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1246 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001247
Liu Bo1f12bd02012-03-29 09:57:44 -04001248 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001249 spin_lock(&BTRFS_I(inode)->lock);
1250 BTRFS_I(inode)->outstanding_extents++;
1251 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001252 btrfs_delalloc_release_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001253 start_index << PAGE_SHIFT,
1254 (page_cnt - i_done) << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001255 }
1256
1257
Liu Bo9e8a4a82012-09-05 19:10:51 -06001258 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001259 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001260
1261 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1262 page_start, page_end - 1, &cached_state,
1263 GFP_NOFS);
1264
1265 for (i = 0; i < i_done; i++) {
1266 clear_page_dirty_for_io(pages[i]);
1267 ClearPageChecked(pages[i]);
1268 set_page_extent_mapped(pages[i]);
1269 set_page_dirty(pages[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 }
1273 return i_done;
1274out:
1275 for (i = 0; i < i_done; i++) {
1276 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001277 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001278 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001279 btrfs_delalloc_release_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001280 start_index << PAGE_SHIFT,
1281 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001282 return ret;
1283
1284}
1285
1286int btrfs_defrag_file(struct inode *inode, struct file *file,
1287 struct btrfs_ioctl_defrag_range_args *range,
1288 u64 newer_than, unsigned long max_to_defrag)
1289{
1290 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001291 struct file_ra_state *ra = NULL;
1292 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001293 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001294 u64 last_len = 0;
1295 u64 skip = 0;
1296 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001297 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001298 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001299 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001300 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001301 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001302 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001303 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001304 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001305 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001306 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001307 struct page **pages = NULL;
1308
Liu Bo0abd5b12013-04-16 09:20:28 +00001309 if (isize == 0)
1310 return 0;
1311
1312 if (range->start >= isize)
1313 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001314
1315 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1316 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1317 return -EINVAL;
1318 if (range->compress_type)
1319 compress_type = range->compress_type;
1320 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001321
Liu Bo0abd5b12013-04-16 09:20:28 +00001322 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001323 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001324
Chris Mason4cb53002011-05-24 15:35:30 -04001325 /*
1326 * if we were not given a file, allocate a readahead
1327 * context
1328 */
1329 if (!file) {
1330 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1331 if (!ra)
1332 return -ENOMEM;
1333 file_ra_state_init(ra, inode->i_mapping);
1334 } else {
1335 ra = &file->f_ra;
1336 }
1337
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301338 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001339 GFP_NOFS);
1340 if (!pages) {
1341 ret = -ENOMEM;
1342 goto out_ra;
1343 }
1344
1345 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001346 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001347 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001348 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001349 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001350 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001351 }
1352
Chris Mason4cb53002011-05-24 15:35:30 -04001353 if (newer_than) {
1354 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001355 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001356 if (!ret) {
1357 range->start = newer_off;
1358 /*
1359 * we always align our defrag to help keep
1360 * the extents in the file evenly spaced
1361 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001362 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001363 } else
1364 goto out_ra;
1365 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001366 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001367 }
1368 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301369 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001370
Li Zefan2a0f7f52011-10-10 15:43:34 -04001371 /*
1372 * make writeback starts from i, so the defrag range can be
1373 * written sequentially.
1374 */
1375 if (i < inode->i_mapping->writeback_index)
1376 inode->i_mapping->writeback_index = i;
1377
Chris Masonf7f43cc2011-10-11 11:41:40 -04001378 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001379 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001380 /*
1381 * make sure we stop running if someone unmounts
1382 * the FS
1383 */
1384 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1385 break;
1386
David Sterba210549e2013-02-09 23:38:06 +00001387 if (btrfs_defrag_cancelled(root->fs_info)) {
David Sterbaf14d1042015-10-08 11:37:06 +02001388 btrfs_debug(root->fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001389 ret = -EAGAIN;
1390 break;
1391 }
1392
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001393 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001394 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001395 &defrag_end, range->flags &
1396 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001397 unsigned long next;
1398 /*
1399 * the should_defrag function tells us how much to skip
1400 * bump our counter by the suggested amount
1401 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001402 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001403 i = max(i + 1, next);
1404 continue;
1405 }
Li Zefan008873e2011-09-02 15:57:07 +08001406
1407 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001408 cluster = (PAGE_ALIGN(defrag_end) >>
1409 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001410 cluster = min(cluster, max_cluster);
1411 } else {
1412 cluster = max_cluster;
1413 }
1414
Li Zefan008873e2011-09-02 15:57:07 +08001415 if (i + cluster > ra_index) {
1416 ra_index = max(i, ra_index);
1417 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1418 cluster);
chandane4826a52015-06-09 17:38:32 +05301419 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001420 }
Chris Mason940100a2010-03-10 10:52:59 -05001421
Al Viro59551022016-01-22 15:40:57 -05001422 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001423 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1424 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001425 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001426 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001427 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001428 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001429 }
Chris Mason940100a2010-03-10 10:52:59 -05001430
Chris Mason4cb53002011-05-24 15:35:30 -04001431 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001432 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001433 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001434
1435 if (newer_than) {
1436 if (newer_off == (u64)-1)
1437 break;
1438
Liu Boe1f041e2012-03-29 09:57:45 -04001439 if (ret > 0)
1440 i += ret;
1441
Chris Mason4cb53002011-05-24 15:35:30 -04001442 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001443 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001444
Byongho Leeee221842015-12-15 01:42:10 +09001445 ret = find_new_extents(root, inode, newer_than,
1446 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001447 if (!ret) {
1448 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001449 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001450 } else {
1451 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001452 }
Chris Mason4cb53002011-05-24 15:35:30 -04001453 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001454 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001455 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001456 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001457 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001458 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001459 last_len = 0;
1460 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001461 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001462 }
1463
Filipe Mananadec8ef92014-03-01 10:55:54 +00001464 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001465 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001466 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1467 &BTRFS_I(inode)->runtime_flags))
1468 filemap_flush(inode->i_mapping);
1469 }
Chris Mason1e701a32010-03-11 09:42:04 -05001470
1471 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1472 /* the filemap_flush will queue IO into the worker threads, but
1473 * we have to make sure the IO is actually started and that
1474 * ordered extents get created before we return
1475 */
1476 atomic_inc(&root->fs_info->async_submit_draining);
1477 while (atomic_read(&root->fs_info->nr_async_submits) ||
1478 atomic_read(&root->fs_info->async_delalloc_pages)) {
1479 wait_event(root->fs_info->async_submit_wait,
1480 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1481 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1482 }
1483 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001484 }
1485
Li Zefan1a419d82010-10-25 15:12:50 +08001486 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001487 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001488 }
1489
Diego Calleja60ccf822011-09-01 16:33:57 +02001490 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001491
Chris Mason4cb53002011-05-24 15:35:30 -04001492out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001493 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
Al Viro59551022016-01-22 15:40:57 -05001494 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001495 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001496 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001497 }
Chris Mason4cb53002011-05-24 15:35:30 -04001498 if (!file)
1499 kfree(ra);
1500 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001501 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001502}
1503
Miao Xie198605a2012-11-26 08:43:45 +00001504static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001505 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001506{
1507 u64 new_size;
1508 u64 old_size;
1509 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001510 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001511 struct btrfs_ioctl_vol_args *vol_args;
1512 struct btrfs_trans_handle *trans;
1513 struct btrfs_device *device = NULL;
1514 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001515 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001516 char *devstr = NULL;
1517 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001518 int mod = 0;
1519
Chris Masone441d542009-01-05 16:57:23 -05001520 if (!capable(CAP_SYS_ADMIN))
1521 return -EPERM;
1522
Miao Xie198605a2012-11-26 08:43:45 +00001523 ret = mnt_want_write_file(file);
1524 if (ret)
1525 return ret;
1526
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001527 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1528 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001529 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001530 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001531 }
1532
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001533 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001534 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001535 if (IS_ERR(vol_args)) {
1536 ret = PTR_ERR(vol_args);
1537 goto out;
1538 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001539
1540 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001541
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001542 sizestr = vol_args->name;
1543 devstr = strchr(sizestr, ':');
1544 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001545 sizestr = devstr + 1;
1546 *devstr = '\0';
1547 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001548 ret = kstrtoull(devstr, 10, &devid);
1549 if (ret)
1550 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001551 if (!devid) {
1552 ret = -EINVAL;
1553 goto out_free;
1554 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001555 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001556 }
Miao Xiedba60f32012-12-21 09:19:51 +00001557
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001558 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001559 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001560 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001561 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001562 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001563 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001564 }
Miao Xiedba60f32012-12-21 09:19:51 +00001565
1566 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001567 btrfs_info(root->fs_info,
1568 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001569 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001570 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001571 goto out_free;
1572 }
1573
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001574 if (!strcmp(sizestr, "max"))
1575 new_size = device->bdev->bd_inode->i_size;
1576 else {
1577 if (sizestr[0] == '-') {
1578 mod = -1;
1579 sizestr++;
1580 } else if (sizestr[0] == '+') {
1581 mod = 1;
1582 sizestr++;
1583 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001584 new_size = memparse(sizestr, &retptr);
1585 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001586 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001587 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001588 }
1589 }
1590
Stefan Behrens63a212a2012-11-05 18:29:28 +01001591 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001592 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001593 goto out_free;
1594 }
1595
Miao Xie7cc8e582014-09-03 21:35:38 +08001596 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001597
1598 if (mod < 0) {
1599 if (new_size > old_size) {
1600 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001601 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001602 }
1603 new_size = old_size - new_size;
1604 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001605 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001606 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001607 goto out_free;
1608 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001609 new_size = old_size + new_size;
1610 }
1611
Byongho Leeee221842015-12-15 01:42:10 +09001612 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001613 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001614 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001615 }
1616 if (new_size > device->bdev->bd_inode->i_size) {
1617 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001618 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001619 }
1620
David Sterbab8b93ad2015-01-16 17:26:13 +01001621 new_size = div_u64(new_size, root->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001622 new_size *= root->sectorsize;
1623
David Sterbaecaeb142015-10-08 09:01:03 +02001624 btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001625 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626
1627 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001628 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001629 if (IS_ERR(trans)) {
1630 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001631 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001632 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 ret = btrfs_grow_device(trans, device, new_size);
1634 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001635 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001636 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001637 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001638
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001639out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001640 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001641out:
1642 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001643 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001644 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001645 return ret;
1646}
1647
Sage Weil72fd0322010-10-29 15:41:32 -04001648static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001649 char *name, unsigned long fd, int subvol,
1650 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001651 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001652{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001653 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001654 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001655
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001656 if (!S_ISDIR(file_inode(file)->i_mode))
1657 return -ENOTDIR;
1658
Liu Boa874a632012-06-29 03:58:46 -06001659 ret = mnt_want_write_file(file);
1660 if (ret)
1661 goto out;
1662
Sage Weil72fd0322010-10-29 15:41:32 -04001663 namelen = strlen(name);
1664 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001665 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001666 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001667 }
1668
Chris Mason16780ca2012-02-20 22:14:55 -05001669 if (name[0] == '.' &&
1670 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1671 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001672 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001673 }
1674
Chris Mason3de45862008-11-17 21:02:50 -05001675 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001676 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001677 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001678 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001679 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001680 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001681 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001682 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001683 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001684 }
1685
Al Viro496ad9a2013-01-23 17:07:38 -05001686 src_inode = file_inode(src.file);
1687 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001688 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001689 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001690 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001691 } else if (!inode_owner_or_capable(src_inode)) {
1692 /*
1693 * Subvolume creation is not restricted, but snapshots
1694 * are limited to own subvolumes only
1695 */
1696 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001697 } else {
1698 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1699 BTRFS_I(src_inode)->root,
1700 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001701 }
Al Viro2903ff02012-08-28 12:52:22 -04001702 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001703 }
Liu Boa874a632012-06-29 03:58:46 -06001704out_drop_write:
1705 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706out:
Sage Weil72fd0322010-10-29 15:41:32 -04001707 return ret;
1708}
1709
1710static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001711 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001712{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001713 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001714 int ret;
1715
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001716 if (!S_ISDIR(file_inode(file)->i_mode))
1717 return -ENOTDIR;
1718
Li Zefanfa0d2b92010-12-20 15:53:28 +08001719 vol_args = memdup_user(arg, sizeof(*vol_args));
1720 if (IS_ERR(vol_args))
1721 return PTR_ERR(vol_args);
1722 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001723
Li Zefanfa0d2b92010-12-20 15:53:28 +08001724 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001725 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001726 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001727
Li Zefanfa0d2b92010-12-20 15:53:28 +08001728 kfree(vol_args);
1729 return ret;
1730}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001731
Li Zefanfa0d2b92010-12-20 15:53:28 +08001732static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1733 void __user *arg, int subvol)
1734{
1735 struct btrfs_ioctl_vol_args_v2 *vol_args;
1736 int ret;
1737 u64 transid = 0;
1738 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001739 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001740 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001741
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001742 if (!S_ISDIR(file_inode(file)->i_mode))
1743 return -ENOTDIR;
1744
Li Zefanfa0d2b92010-12-20 15:53:28 +08001745 vol_args = memdup_user(arg, sizeof(*vol_args));
1746 if (IS_ERR(vol_args))
1747 return PTR_ERR(vol_args);
1748 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001749
Li Zefanb83cc962010-12-20 16:04:08 +08001750 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001751 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1752 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001753 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001754 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001755 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001756
1757 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1758 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001759 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1760 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001761 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001762 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001763 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001764 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001765 }
1766 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1767 if (IS_ERR(inherit)) {
1768 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001769 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001770 }
1771 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001772
1773 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001774 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001775 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001776 if (ret)
1777 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001778
Dan Carpenterc47ca322014-09-04 14:09:15 +03001779 if (ptr && copy_to_user(arg +
1780 offsetof(struct btrfs_ioctl_vol_args_v2,
1781 transid),
1782 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001783 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001784
1785free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001786 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001787free_args:
1788 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001789 return ret;
1790}
1791
Li Zefan0caa1022010-12-20 16:30:25 +08001792static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1793 void __user *arg)
1794{
Al Viro496ad9a2013-01-23 17:07:38 -05001795 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001796 struct btrfs_root *root = BTRFS_I(inode)->root;
1797 int ret = 0;
1798 u64 flags = 0;
1799
Li Zefan33345d012011-04-20 10:31:50 +08001800 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001801 return -EINVAL;
1802
1803 down_read(&root->fs_info->subvol_sem);
1804 if (btrfs_root_readonly(root))
1805 flags |= BTRFS_SUBVOL_RDONLY;
1806 up_read(&root->fs_info->subvol_sem);
1807
1808 if (copy_to_user(arg, &flags, sizeof(flags)))
1809 ret = -EFAULT;
1810
1811 return ret;
1812}
1813
1814static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1815 void __user *arg)
1816{
Al Viro496ad9a2013-01-23 17:07:38 -05001817 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001818 struct btrfs_root *root = BTRFS_I(inode)->root;
1819 struct btrfs_trans_handle *trans;
1820 u64 root_flags;
1821 u64 flags;
1822 int ret = 0;
1823
David Sterbabd60ea02014-01-16 15:50:22 +01001824 if (!inode_owner_or_capable(inode))
1825 return -EPERM;
1826
Liu Bob9ca0662012-06-29 03:58:49 -06001827 ret = mnt_want_write_file(file);
1828 if (ret)
1829 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001830
Liu Bob9ca0662012-06-29 03:58:49 -06001831 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1832 ret = -EINVAL;
1833 goto out_drop_write;
1834 }
Li Zefan0caa1022010-12-20 16:30:25 +08001835
Liu Bob9ca0662012-06-29 03:58:49 -06001836 if (copy_from_user(&flags, arg, sizeof(flags))) {
1837 ret = -EFAULT;
1838 goto out_drop_write;
1839 }
Li Zefan0caa1022010-12-20 16:30:25 +08001840
Liu Bob9ca0662012-06-29 03:58:49 -06001841 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1842 ret = -EINVAL;
1843 goto out_drop_write;
1844 }
Li Zefan0caa1022010-12-20 16:30:25 +08001845
Liu Bob9ca0662012-06-29 03:58:49 -06001846 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1847 ret = -EOPNOTSUPP;
1848 goto out_drop_write;
1849 }
Li Zefan0caa1022010-12-20 16:30:25 +08001850
1851 down_write(&root->fs_info->subvol_sem);
1852
1853 /* nothing to do */
1854 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001855 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001856
1857 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001858 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001859 btrfs_set_root_flags(&root->root_item,
1860 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001861 } else {
1862 /*
1863 * Block RO -> RW transition if this subvolume is involved in
1864 * send
1865 */
1866 spin_lock(&root->root_item_lock);
1867 if (root->send_in_progress == 0) {
1868 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001869 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001870 spin_unlock(&root->root_item_lock);
1871 } else {
1872 spin_unlock(&root->root_item_lock);
1873 btrfs_warn(root->fs_info,
1874 "Attempt to set subvolume %llu read-write during send",
1875 root->root_key.objectid);
1876 ret = -EPERM;
1877 goto out_drop_sem;
1878 }
1879 }
Li Zefan0caa1022010-12-20 16:30:25 +08001880
1881 trans = btrfs_start_transaction(root, 1);
1882 if (IS_ERR(trans)) {
1883 ret = PTR_ERR(trans);
1884 goto out_reset;
1885 }
1886
Li Zefanb4dc2b82011-02-16 06:06:34 +00001887 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001888 &root->root_key, &root->root_item);
1889
1890 btrfs_commit_transaction(trans, root);
1891out_reset:
1892 if (ret)
1893 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001894out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001895 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001896out_drop_write:
1897 mnt_drop_write_file(file);
1898out:
Li Zefan0caa1022010-12-20 16:30:25 +08001899 return ret;
1900}
1901
Yan, Zheng76dda932009-09-21 16:00:26 -04001902/*
1903 * helper to check if the subvolume references other subvolumes
1904 */
1905static noinline int may_destroy_subvol(struct btrfs_root *root)
1906{
1907 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001908 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001909 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001910 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001911 int ret;
1912
1913 path = btrfs_alloc_path();
1914 if (!path)
1915 return -ENOMEM;
1916
Josef Bacik175a2b82013-08-12 15:36:44 -04001917 /* Make sure this root isn't set as the default subvol */
1918 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1919 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1920 dir_id, "default", 7, 0);
1921 if (di && !IS_ERR(di)) {
1922 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1923 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001924 ret = -EPERM;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001925 btrfs_err(root->fs_info,
1926 "deleting default subvolume %llu is not allowed",
1927 key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001928 goto out;
1929 }
1930 btrfs_release_path(path);
1931 }
1932
Yan, Zheng76dda932009-09-21 16:00:26 -04001933 key.objectid = root->root_key.objectid;
1934 key.type = BTRFS_ROOT_REF_KEY;
1935 key.offset = (u64)-1;
1936
1937 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1938 &key, path, 0, 0);
1939 if (ret < 0)
1940 goto out;
1941 BUG_ON(ret == 0);
1942
1943 ret = 0;
1944 if (path->slots[0] > 0) {
1945 path->slots[0]--;
1946 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1947 if (key.objectid == root->root_key.objectid &&
1948 key.type == BTRFS_ROOT_REF_KEY)
1949 ret = -ENOTEMPTY;
1950 }
1951out:
1952 btrfs_free_path(path);
1953 return ret;
1954}
1955
Chris Masonac8e9812010-02-28 15:39:26 -05001956static noinline int key_in_sk(struct btrfs_key *key,
1957 struct btrfs_ioctl_search_key *sk)
1958{
Chris Masonabc6e132010-03-18 12:10:08 -04001959 struct btrfs_key test;
1960 int ret;
1961
1962 test.objectid = sk->min_objectid;
1963 test.type = sk->min_type;
1964 test.offset = sk->min_offset;
1965
1966 ret = btrfs_comp_cpu_keys(key, &test);
1967 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001968 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001969
1970 test.objectid = sk->max_objectid;
1971 test.type = sk->max_type;
1972 test.offset = sk->max_offset;
1973
1974 ret = btrfs_comp_cpu_keys(key, &test);
1975 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001976 return 0;
1977 return 1;
1978}
1979
Jeff Mahoneydf397562016-06-21 20:18:21 -04001980static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001981 struct btrfs_key *key,
1982 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001983 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001984 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001985 unsigned long *sk_offset,
1986 int *num_found)
1987{
1988 u64 found_transid;
1989 struct extent_buffer *leaf;
1990 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001991 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001992 unsigned long item_off;
1993 unsigned long item_len;
1994 int nritems;
1995 int i;
1996 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001997 int ret = 0;
1998
1999 leaf = path->nodes[0];
2000 slot = path->slots[0];
2001 nritems = btrfs_header_nritems(leaf);
2002
2003 if (btrfs_header_generation(leaf) > sk->max_transid) {
2004 i = nritems;
2005 goto advance_key;
2006 }
2007 found_transid = btrfs_header_generation(leaf);
2008
2009 for (i = slot; i < nritems; i++) {
2010 item_off = btrfs_item_ptr_offset(leaf, i);
2011 item_len = btrfs_item_size_nr(leaf, i);
2012
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002013 btrfs_item_key_to_cpu(leaf, key, i);
2014 if (!key_in_sk(key, sk))
2015 continue;
2016
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002017 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002018 if (*num_found) {
2019 ret = 1;
2020 goto out;
2021 }
Chris Masonac8e9812010-02-28 15:39:26 -05002022
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002023 /*
2024 * return one empty item back for v1, which does not
2025 * handle -EOVERFLOW
2026 */
2027
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002028 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002029 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002030 ret = -EOVERFLOW;
2031 }
Chris Masonac8e9812010-02-28 15:39:26 -05002032
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002033 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002034 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002035 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002036 }
2037
Chris Masonac8e9812010-02-28 15:39:26 -05002038 sh.objectid = key->objectid;
2039 sh.offset = key->offset;
2040 sh.type = key->type;
2041 sh.len = item_len;
2042 sh.transid = found_transid;
2043
2044 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002045 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2046 ret = -EFAULT;
2047 goto out;
2048 }
2049
Chris Masonac8e9812010-02-28 15:39:26 -05002050 *sk_offset += sizeof(sh);
2051
2052 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002053 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002054 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002055 if (read_extent_buffer_to_user(leaf, up,
2056 item_off, item_len)) {
2057 ret = -EFAULT;
2058 goto out;
2059 }
2060
Chris Masonac8e9812010-02-28 15:39:26 -05002061 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002062 }
Hugo Millse2156862011-05-14 17:43:41 +00002063 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002064
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002065 if (ret) /* -EOVERFLOW from above */
2066 goto out;
2067
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002068 if (*num_found >= sk->nr_items) {
2069 ret = 1;
2070 goto out;
2071 }
Chris Masonac8e9812010-02-28 15:39:26 -05002072 }
2073advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002074 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002075 test.objectid = sk->max_objectid;
2076 test.type = sk->max_type;
2077 test.offset = sk->max_offset;
2078 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2079 ret = 1;
2080 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002081 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002082 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002083 key->offset = 0;
2084 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002085 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002086 key->offset = 0;
2087 key->type = 0;
2088 key->objectid++;
2089 } else
2090 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002091out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002092 /*
2093 * 0: all items from this leaf copied, continue with next
2094 * 1: * more items can be copied, but unused buffer is too small
2095 * * all items were found
2096 * Either way, it will stops the loop which iterates to the next
2097 * leaf
2098 * -EOVERFLOW: item was to large for buffer
2099 * -EFAULT: could not copy extent buffer back to userspace
2100 */
Chris Masonac8e9812010-02-28 15:39:26 -05002101 return ret;
2102}
2103
2104static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002105 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002106 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002107 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002108{
2109 struct btrfs_root *root;
2110 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002111 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002112 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2113 int ret;
2114 int num_found = 0;
2115 unsigned long sk_offset = 0;
2116
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002117 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2118 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002119 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002120 }
Gerhard Heift12544442014-01-30 16:23:58 +01002121
Chris Masonac8e9812010-02-28 15:39:26 -05002122 path = btrfs_alloc_path();
2123 if (!path)
2124 return -ENOMEM;
2125
2126 if (sk->tree_id == 0) {
2127 /* search the root of the inode that was passed */
2128 root = BTRFS_I(inode)->root;
2129 } else {
2130 key.objectid = sk->tree_id;
2131 key.type = BTRFS_ROOT_ITEM_KEY;
2132 key.offset = (u64)-1;
2133 root = btrfs_read_fs_root_no_name(info, &key);
2134 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002135 btrfs_free_path(path);
2136 return -ENOENT;
2137 }
2138 }
2139
2140 key.objectid = sk->min_objectid;
2141 key.type = sk->min_type;
2142 key.offset = sk->min_offset;
2143
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302144 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002145 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002146 if (ret != 0) {
2147 if (ret > 0)
2148 ret = 0;
2149 goto err;
2150 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002151 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002152 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002153 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002154 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002155 break;
2156
2157 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002158 if (ret > 0)
2159 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002160err:
2161 sk->nr_items = num_found;
2162 btrfs_free_path(path);
2163 return ret;
2164}
2165
2166static noinline int btrfs_ioctl_tree_search(struct file *file,
2167 void __user *argp)
2168{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002169 struct btrfs_ioctl_search_args __user *uargs;
2170 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002171 struct inode *inode;
2172 int ret;
2173 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002174
2175 if (!capable(CAP_SYS_ADMIN))
2176 return -EPERM;
2177
Gerhard Heiftba346b32014-01-30 16:24:02 +01002178 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002179
Gerhard Heiftba346b32014-01-30 16:24:02 +01002180 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2181 return -EFAULT;
2182
2183 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002184
Al Viro496ad9a2013-01-23 17:07:38 -05002185 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002186 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002187
2188 /*
2189 * In the origin implementation an overflow is handled by returning a
2190 * search header with a len of zero, so reset ret.
2191 */
2192 if (ret == -EOVERFLOW)
2193 ret = 0;
2194
Gerhard Heiftba346b32014-01-30 16:24:02 +01002195 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002196 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002197 return ret;
2198}
2199
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002200static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2201 void __user *argp)
2202{
2203 struct btrfs_ioctl_search_args_v2 __user *uarg;
2204 struct btrfs_ioctl_search_args_v2 args;
2205 struct inode *inode;
2206 int ret;
2207 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002208 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002209
2210 if (!capable(CAP_SYS_ADMIN))
2211 return -EPERM;
2212
2213 /* copy search header and buffer size */
2214 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2215 if (copy_from_user(&args, uarg, sizeof(args)))
2216 return -EFAULT;
2217
2218 buf_size = args.buf_size;
2219
2220 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2221 return -EOVERFLOW;
2222
2223 /* limit result size to 16MB */
2224 if (buf_size > buf_limit)
2225 buf_size = buf_limit;
2226
2227 inode = file_inode(file);
2228 ret = search_ioctl(inode, &args.key, &buf_size,
2229 (char *)(&uarg->buf[0]));
2230 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2231 ret = -EFAULT;
2232 else if (ret == -EOVERFLOW &&
2233 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2234 ret = -EFAULT;
2235
Yan, Zheng76dda932009-09-21 16:00:26 -04002236 return ret;
2237}
2238
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002239/*
Chris Masonac8e9812010-02-28 15:39:26 -05002240 * Search INODE_REFs to identify path name of 'dirid' directory
2241 * in a 'tree_id' tree. and sets path name to 'name'.
2242 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002243static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2244 u64 tree_id, u64 dirid, char *name)
2245{
2246 struct btrfs_root *root;
2247 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002248 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002249 int ret = -1;
2250 int slot;
2251 int len;
2252 int total_len = 0;
2253 struct btrfs_inode_ref *iref;
2254 struct extent_buffer *l;
2255 struct btrfs_path *path;
2256
2257 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2258 name[0]='\0';
2259 return 0;
2260 }
2261
2262 path = btrfs_alloc_path();
2263 if (!path)
2264 return -ENOMEM;
2265
Nikolay Borisov1c3aae52017-12-01 11:19:42 +02002266 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002267
2268 key.objectid = tree_id;
2269 key.type = BTRFS_ROOT_ITEM_KEY;
2270 key.offset = (u64)-1;
2271 root = btrfs_read_fs_root_no_name(info, &key);
2272 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002273 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002274 ret = -ENOENT;
2275 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002276 }
2277
2278 key.objectid = dirid;
2279 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002280 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002281
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302282 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002283 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2284 if (ret < 0)
2285 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002286 else if (ret > 0) {
2287 ret = btrfs_previous_item(root, path, dirid,
2288 BTRFS_INODE_REF_KEY);
2289 if (ret < 0)
2290 goto out;
2291 else if (ret > 0) {
2292 ret = -ENOENT;
2293 goto out;
2294 }
2295 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002296
2297 l = path->nodes[0];
2298 slot = path->slots[0];
2299 btrfs_item_key_to_cpu(l, &key, slot);
2300
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002301 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2302 len = btrfs_inode_ref_name_len(l, iref);
2303 ptr -= len + 1;
2304 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002305 if (ptr < name) {
2306 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002307 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002308 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002309
2310 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302311 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002312
2313 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2314 break;
2315
David Sterbab3b4aa72011-04-21 01:20:15 +02002316 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002317 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002318 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002319 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002320 }
Li Zefan77906a502011-07-14 03:16:00 +00002321 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302322 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002323 ret = 0;
2324out:
2325 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002326 return ret;
2327}
2328
2329static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2330 void __user *argp)
2331{
2332 struct btrfs_ioctl_ino_lookup_args *args;
2333 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002334 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002335
Julia Lawall2354d082010-10-29 15:14:18 -04002336 args = memdup_user(argp, sizeof(*args));
2337 if (IS_ERR(args))
2338 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002339
Al Viro496ad9a2013-01-23 17:07:38 -05002340 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002341
David Sterba01b810b2015-05-12 19:14:49 +02002342 /*
2343 * Unprivileged query to obtain the containing subvolume root id. The
2344 * path is reset so it's consistent with btrfs_search_path_in_tree.
2345 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002346 if (args->treeid == 0)
2347 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2348
David Sterba01b810b2015-05-12 19:14:49 +02002349 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2350 args->name[0] = 0;
2351 goto out;
2352 }
2353
2354 if (!capable(CAP_SYS_ADMIN)) {
2355 ret = -EPERM;
2356 goto out;
2357 }
2358
Chris Masonac8e9812010-02-28 15:39:26 -05002359 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2360 args->treeid, args->objectid,
2361 args->name);
2362
David Sterba01b810b2015-05-12 19:14:49 +02002363out:
Chris Masonac8e9812010-02-28 15:39:26 -05002364 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2365 ret = -EFAULT;
2366
2367 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002368 return ret;
2369}
2370
Yan, Zheng76dda932009-09-21 16:00:26 -04002371static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2372 void __user *arg)
2373{
Al Viro54563d42013-09-01 15:57:51 -04002374 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002375 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002376 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002377 struct inode *inode;
2378 struct btrfs_root *root = BTRFS_I(dir)->root;
2379 struct btrfs_root *dest = NULL;
2380 struct btrfs_ioctl_vol_args *vol_args;
2381 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002382 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002383 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002384 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002385 int namelen;
2386 int ret;
2387 int err = 0;
2388
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002389 if (!S_ISDIR(dir->i_mode))
2390 return -ENOTDIR;
2391
Yan, Zheng76dda932009-09-21 16:00:26 -04002392 vol_args = memdup_user(arg, sizeof(*vol_args));
2393 if (IS_ERR(vol_args))
2394 return PTR_ERR(vol_args);
2395
2396 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2397 namelen = strlen(vol_args->name);
2398 if (strchr(vol_args->name, '/') ||
2399 strncmp(vol_args->name, "..", namelen) == 0) {
2400 err = -EINVAL;
2401 goto out;
2402 }
2403
Al Viroa561be72011-11-23 11:57:51 -05002404 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002405 if (err)
2406 goto out;
2407
David Sterba521e0542014-04-15 16:41:44 +02002408
Al Viro00235412016-05-26 00:05:12 -04002409 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2410 if (err == -EINTR)
2411 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002412 dentry = lookup_one_len(vol_args->name, parent, namelen);
2413 if (IS_ERR(dentry)) {
2414 err = PTR_ERR(dentry);
2415 goto out_unlock_dir;
2416 }
2417
David Howells2b0143b2015-03-17 22:25:59 +00002418 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002419 err = -ENOENT;
2420 goto out_dput;
2421 }
2422
David Howells2b0143b2015-03-17 22:25:59 +00002423 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002424 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302425 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002426 /*
2427 * Regular user. Only allow this with a special mount
2428 * option, when the user has write+exec access to the
2429 * subvol root, and when rmdir(2) would have been
2430 * allowed.
2431 *
2432 * Note that this is _not_ check that the subvol is
2433 * empty or doesn't contain data that we wouldn't
2434 * otherwise be able to delete.
2435 *
2436 * Users who want to delete empty subvols should try
2437 * rmdir(2).
2438 */
2439 err = -EPERM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002440 if (!btrfs_test_opt(root->fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002441 goto out_dput;
2442
2443 /*
2444 * Do not allow deletion if the parent dir is the same
2445 * as the dir to be deleted. That means the ioctl
2446 * must be called on the dentry referencing the root
2447 * of the subvol, not a random directory contained
2448 * within it.
2449 */
2450 err = -EINVAL;
2451 if (root == dest)
2452 goto out_dput;
2453
2454 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2455 if (err)
2456 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002457 }
2458
Miao Xie5c39da52012-10-22 11:39:53 +00002459 /* check if subvolume may be deleted by a user */
2460 err = btrfs_may_delete(dir, dentry, 1);
2461 if (err)
2462 goto out_dput;
2463
Li Zefan33345d012011-04-20 10:31:50 +08002464 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002465 err = -EINVAL;
2466 goto out_dput;
2467 }
2468
Al Viro59551022016-01-22 15:40:57 -05002469 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002470
2471 /*
2472 * Don't allow to delete a subvolume with send in progress. This is
2473 * inside the i_mutex so the error handling that has to drop the bit
2474 * again is not run concurrently.
2475 */
2476 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002477 root_flags = btrfs_root_flags(&dest->root_item);
2478 if (dest->send_in_progress == 0) {
2479 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002480 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2481 spin_unlock(&dest->root_item_lock);
2482 } else {
2483 spin_unlock(&dest->root_item_lock);
2484 btrfs_warn(root->fs_info,
2485 "Attempt to delete subvolume %llu during send",
Filipe Mananac55bfa62014-05-25 03:55:44 +01002486 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002487 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002488 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002489 }
2490
Yan, Zheng76dda932009-09-21 16:00:26 -04002491 down_write(&root->fs_info->subvol_sem);
2492
2493 err = may_destroy_subvol(dest);
2494 if (err)
2495 goto out_up_write;
2496
Miao Xiec58aaad2013-02-28 10:05:36 +00002497 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2498 /*
2499 * One for dir inode, two for dir entries, two for root
2500 * ref/backref.
2501 */
2502 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002503 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002504 if (err)
2505 goto out_up_write;
2506
Yan, Zhenga22285a2010-05-16 10:48:46 -04002507 trans = btrfs_start_transaction(root, 0);
2508 if (IS_ERR(trans)) {
2509 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002510 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002511 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002512 trans->block_rsv = &block_rsv;
2513 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002514
Filipe Manana2be63d52016-02-12 11:34:23 +00002515 btrfs_record_snapshot_destroy(trans, dir);
2516
Yan, Zheng76dda932009-09-21 16:00:26 -04002517 ret = btrfs_unlink_subvol(trans, root, dir,
2518 dest->root_key.objectid,
2519 dentry->d_name.name,
2520 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002521 if (ret) {
2522 err = ret;
Jeff Mahoney66642832016-06-10 18:19:25 -04002523 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002524 goto out_end_trans;
2525 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002526
2527 btrfs_record_root_in_trans(trans, dest);
2528
2529 memset(&dest->root_item.drop_progress, 0,
2530 sizeof(dest->root_item.drop_progress));
2531 dest->root_item.drop_level = 0;
2532 btrfs_set_root_refs(&dest->root_item, 0);
2533
Miao Xie27cdeb72014-04-02 19:51:05 +08002534 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002535 ret = btrfs_insert_orphan_item(trans,
2536 root->fs_info->tree_root,
2537 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002538 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002539 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002540 err = ret;
2541 goto out_end_trans;
2542 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002543 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002544
2545 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2546 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2547 dest->root_key.objectid);
2548 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002549 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002550 err = ret;
2551 goto out_end_trans;
2552 }
2553 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2554 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2555 dest->root_item.received_uuid,
2556 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2557 dest->root_key.objectid);
2558 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002559 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002560 err = ret;
2561 goto out_end_trans;
2562 }
2563 }
2564
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002565out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002566 trans->block_rsv = NULL;
2567 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002568 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002569 if (ret && !err)
2570 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002571 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002572out_release:
2573 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002574out_up_write:
2575 up_write(&root->fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002576 if (err) {
2577 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002578 root_flags = btrfs_root_flags(&dest->root_item);
2579 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002580 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2581 spin_unlock(&dest->root_item_lock);
2582 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002583out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002584 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002585 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002586 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002587 btrfs_invalidate_inodes(dest);
2588 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002589 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002590
2591 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002592 if (dest->ino_cache_inode) {
2593 iput(dest->ino_cache_inode);
2594 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002595 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002596 }
2597out_dput:
2598 dput(dentry);
2599out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002600 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002601out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002602 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002603out:
2604 kfree(vol_args);
2605 return err;
2606}
2607
Chris Mason1e701a32010-03-11 09:42:04 -05002608static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002609{
Al Viro496ad9a2013-01-23 17:07:38 -05002610 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002611 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002612 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002613 int ret;
2614
Ilya Dryomov25122d12013-01-20 15:57:57 +02002615 ret = mnt_want_write_file(file);
2616 if (ret)
2617 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002618
Ilya Dryomov25122d12013-01-20 15:57:57 +02002619 if (btrfs_root_readonly(root)) {
2620 ret = -EROFS;
2621 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002622 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002623
2624 switch (inode->i_mode & S_IFMT) {
2625 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002626 if (!capable(CAP_SYS_ADMIN)) {
2627 ret = -EPERM;
2628 goto out;
2629 }
Eric Sandeende78b512013-01-31 18:21:12 +00002630 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002631 if (ret)
2632 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002633 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002634 break;
2635 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002636 if (!(file->f_mode & FMODE_WRITE)) {
2637 ret = -EINVAL;
2638 goto out;
2639 }
Chris Mason1e701a32010-03-11 09:42:04 -05002640
2641 range = kzalloc(sizeof(*range), GFP_KERNEL);
2642 if (!range) {
2643 ret = -ENOMEM;
2644 goto out;
2645 }
2646
2647 if (argp) {
2648 if (copy_from_user(range, argp,
2649 sizeof(*range))) {
2650 ret = -EFAULT;
2651 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002652 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002653 }
2654 /* compression requires us to start the IO */
2655 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2656 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2657 range->extent_thresh = (u32)-1;
2658 }
2659 } else {
2660 /* the rest are all set to zero by kzalloc */
2661 range->len = (u64)-1;
2662 }
Al Viro496ad9a2013-01-23 17:07:38 -05002663 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002664 range, 0, 0);
2665 if (ret > 0)
2666 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002667 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002668 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002669 default:
2670 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002671 }
Chris Masone441d542009-01-05 16:57:23 -05002672out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002673 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002674 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002675}
2676
Christoph Hellwigb2950862008-12-02 09:54:17 -05002677static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002678{
2679 struct btrfs_ioctl_vol_args *vol_args;
2680 int ret;
2681
Chris Masone441d542009-01-05 16:57:23 -05002682 if (!capable(CAP_SYS_ADMIN))
2683 return -EPERM;
2684
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002685 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2686 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002687 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002688 }
2689
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002690 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002691 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002692 if (IS_ERR(vol_args)) {
2693 ret = PTR_ERR(vol_args);
2694 goto out;
2695 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002696
Mark Fasheh5516e592008-07-24 12:20:14 -04002697 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002698 ret = btrfs_init_new_device(root, vol_args->name);
2699
Anand Jain43d20762014-07-01 00:58:56 +08002700 if (!ret)
2701 btrfs_info(root->fs_info, "disk added %s",vol_args->name);
2702
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002703 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002704out:
2705 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002706 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002707 return ret;
2708}
2709
Anand Jain6b526ed2016-02-13 10:01:39 +08002710static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002711{
Al Viro496ad9a2013-01-23 17:07:38 -05002712 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jain6b526ed2016-02-13 10:01:39 +08002713 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002714 int ret;
2715
Chris Masone441d542009-01-05 16:57:23 -05002716 if (!capable(CAP_SYS_ADMIN))
2717 return -EPERM;
2718
Miao Xieda249272012-11-26 08:44:50 +00002719 ret = mnt_want_write_file(file);
2720 if (ret)
2721 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002722
Li Zefandae7b662009-04-08 15:06:54 +08002723 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002724 if (IS_ERR(vol_args)) {
2725 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002726 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002727 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002728
Anand Jain6b526ed2016-02-13 10:01:39 +08002729 /* Check for compatibility reject unknown flags */
Omar Sandoval9bb94d82018-05-22 15:44:01 -07002730 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
2731 ret = -EOPNOTSUPP;
2732 goto out;
2733 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002734
Anand Jain183860f2013-05-17 10:52:45 +00002735 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2736 1)) {
2737 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2738 goto out;
2739 }
2740
2741 mutex_lock(&root->fs_info->volume_mutex);
David Sterba735654e2016-02-15 18:15:21 +01002742 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Anand Jain6b526ed2016-02-13 10:01:39 +08002743 ret = btrfs_rm_device(root, NULL, vol_args->devid);
2744 } else {
2745 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
2746 ret = btrfs_rm_device(root, vol_args->name, 0);
2747 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002748 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002749 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002750
Anand Jain6b526ed2016-02-13 10:01:39 +08002751 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002752 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Anand Jain6b526ed2016-02-13 10:01:39 +08002753 btrfs_info(root->fs_info, "device deleted: id %llu",
2754 vol_args->devid);
2755 else
2756 btrfs_info(root->fs_info, "device deleted: %s",
2757 vol_args->name);
2758 }
Anand Jain183860f2013-05-17 10:52:45 +00002759out:
2760 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002761err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002762 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002763 return ret;
2764}
2765
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002766static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2767{
2768 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2769 struct btrfs_ioctl_vol_args *vol_args;
2770 int ret;
2771
2772 if (!capable(CAP_SYS_ADMIN))
2773 return -EPERM;
2774
2775 ret = mnt_want_write_file(file);
2776 if (ret)
2777 return ret;
2778
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002779 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2780 1)) {
2781 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002782 goto out_drop_write;
2783 }
2784
2785 vol_args = memdup_user(arg, sizeof(*vol_args));
2786 if (IS_ERR(vol_args)) {
2787 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002788 goto out;
2789 }
2790
David Sterba58d7bbf2016-05-04 14:10:47 +02002791 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002792 mutex_lock(&root->fs_info->volume_mutex);
Anand Jain6b526ed2016-02-13 10:01:39 +08002793 ret = btrfs_rm_device(root, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002794 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002795
2796 if (!ret)
2797 btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002798 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002799out:
2800 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2801out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002802 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002803
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002804 return ret;
2805}
2806
Jan Schmidt475f6382011-03-11 15:41:01 +01002807static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2808{
Li Zefan027ed2f2011-06-08 08:27:56 +00002809 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002810 struct btrfs_device *device;
Jan Schmidt475f6382011-03-11 15:41:01 +01002811 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002812 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002813
Li Zefan027ed2f2011-06-08 08:27:56 +00002814 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2815 if (!fi_args)
2816 return -ENOMEM;
2817
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002818 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002819 fi_args->num_devices = fs_devices->num_devices;
2820 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002821
Byongho Leed7641a42015-09-01 23:10:57 +09002822 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002823 if (device->devid > fi_args->max_id)
2824 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002825 }
2826 mutex_unlock(&fs_devices->device_list_mutex);
2827
David Sterba80a773f2014-05-07 18:17:06 +02002828 fi_args->nodesize = root->fs_info->super_copy->nodesize;
2829 fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
2830 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
2831
Li Zefan027ed2f2011-06-08 08:27:56 +00002832 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2833 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002834
Li Zefan027ed2f2011-06-08 08:27:56 +00002835 kfree(fi_args);
2836 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002837}
2838
2839static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2840{
2841 struct btrfs_ioctl_dev_info_args *di_args;
2842 struct btrfs_device *dev;
2843 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2844 int ret = 0;
2845 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002846
Jan Schmidt475f6382011-03-11 15:41:01 +01002847 di_args = memdup_user(arg, sizeof(*di_args));
2848 if (IS_ERR(di_args))
2849 return PTR_ERR(di_args);
2850
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002851 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002852 s_uuid = di_args->uuid;
2853
2854 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002855 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002856
2857 if (!dev) {
2858 ret = -ENODEV;
2859 goto out;
2860 }
2861
2862 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002863 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2864 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002865 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002866 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002867 struct rcu_string *name;
2868
2869 rcu_read_lock();
2870 name = rcu_dereference(dev->name);
2871 strncpy(di_args->path, name->str, sizeof(di_args->path));
2872 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002873 di_args->path[sizeof(di_args->path) - 1] = 0;
2874 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002875 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002876 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002877
2878out:
David Sterba55793c02013-04-26 15:20:23 +00002879 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002880 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2881 ret = -EFAULT;
2882
2883 kfree(di_args);
2884 return ret;
2885}
2886
Mark Fashehf4414602015-06-30 14:42:05 -07002887static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002888{
2889 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002890
Mark Fasheh416161d2013-08-06 11:42:51 -07002891 page = grab_cache_page(inode->i_mapping, index);
2892 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002893 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002894
2895 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002896 int ret;
2897
2898 ret = btrfs_readpage(NULL, page);
2899 if (ret)
2900 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002901 lock_page(page);
2902 if (!PageUptodate(page)) {
2903 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002904 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002905 return ERR_PTR(-EIO);
2906 }
2907 if (page->mapping != inode->i_mapping) {
2908 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002909 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002910 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002911 }
2912 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002913
2914 return page;
2915}
2916
Mark Fashehf4414602015-06-30 14:42:05 -07002917static int gather_extent_pages(struct inode *inode, struct page **pages,
2918 int num_pages, u64 off)
2919{
2920 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002921 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002922
2923 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002924again:
Mark Fashehf4414602015-06-30 14:42:05 -07002925 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002926 if (IS_ERR(pages[i])) {
2927 int err = PTR_ERR(pages[i]);
2928
2929 if (err == -EAGAIN)
2930 goto again;
2931 pages[i] = NULL;
2932 return err;
2933 }
Mark Fashehf4414602015-06-30 14:42:05 -07002934 }
2935 return 0;
2936}
2937
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002938static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2939 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002940{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002941 /*
2942 * Do any pending delalloc/csum calculations on inode, one way or
2943 * another, and lock file content.
2944 * The locking order is:
2945 *
2946 * 1) pages
2947 * 2) range in the inode's io tree
2948 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002949 while (1) {
2950 struct btrfs_ordered_extent *ordered;
2951 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2952 ordered = btrfs_lookup_first_ordered_extent(inode,
2953 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002954 if ((!ordered ||
2955 ordered->file_offset + ordered->len <= off ||
2956 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002957 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002958 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2959 if (ordered)
2960 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002961 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002962 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002963 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2964 if (ordered)
2965 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002966 if (!retry_range_locking)
2967 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002968 btrfs_wait_ordered_range(inode, off, len);
2969 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002970 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002971}
2972
Mark Fashehf4414602015-06-30 14:42:05 -07002973static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002974{
Al Viro59551022016-01-22 15:40:57 -05002975 inode_unlock(inode1);
2976 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002977}
2978
Mark Fashehf4414602015-06-30 14:42:05 -07002979static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2980{
2981 if (inode1 < inode2)
2982 swap(inode1, inode2);
2983
Al Viro59551022016-01-22 15:40:57 -05002984 inode_lock_nested(inode1, I_MUTEX_PARENT);
2985 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002986}
2987
2988static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2989 struct inode *inode2, u64 loff2, u64 len)
2990{
2991 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2992 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2993}
2994
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002995static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2996 struct inode *inode2, u64 loff2, u64 len,
2997 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002998{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002999 int ret;
3000
Mark Fasheh416161d2013-08-06 11:42:51 -07003001 if (inode1 < inode2) {
3002 swap(inode1, inode2);
3003 swap(loff1, loff2);
3004 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003005 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
3006 if (ret)
3007 return ret;
3008 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
3009 if (ret)
3010 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
3011 loff1 + len - 1);
3012 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003013}
3014
3015struct cmp_pages {
3016 int num_pages;
3017 struct page **src_pages;
3018 struct page **dst_pages;
3019};
3020
3021static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3022{
3023 int i;
3024 struct page *pg;
3025
3026 for (i = 0; i < cmp->num_pages; i++) {
3027 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003028 if (pg) {
3029 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003030 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003031 }
Mark Fashehf4414602015-06-30 14:42:05 -07003032 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003033 if (pg) {
3034 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003035 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003036 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003037 }
Mark Fashehf4414602015-06-30 14:42:05 -07003038 kfree(cmp->src_pages);
3039 kfree(cmp->dst_pages);
3040}
3041
3042static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3043 struct inode *dst, u64 dst_loff,
3044 u64 len, struct cmp_pages *cmp)
3045{
3046 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003047 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003048 struct page **src_pgarr, **dst_pgarr;
3049
3050 /*
3051 * We must gather up all the pages before we initiate our
3052 * extent locking. We use an array for the page pointers. Size
3053 * of the array is bounded by len, which is in turn bounded by
3054 * BTRFS_MAX_DEDUPE_LEN.
3055 */
David Sterba66722f72016-02-11 15:01:38 +01003056 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
3057 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07003058 if (!src_pgarr || !dst_pgarr) {
3059 kfree(src_pgarr);
3060 kfree(dst_pgarr);
3061 return -ENOMEM;
3062 }
3063 cmp->num_pages = num_pages;
3064 cmp->src_pages = src_pgarr;
3065 cmp->dst_pages = dst_pgarr;
3066
3067 ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff);
3068 if (ret)
3069 goto out;
3070
3071 ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff);
3072
3073out:
3074 if (ret)
3075 btrfs_cmp_data_free(cmp);
Naohiro Aotaba44bc42017-09-08 17:48:55 +09003076 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003077}
3078
3079static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
Mark Fashehf4414602015-06-30 14:42:05 -07003080 u64 dst_loff, u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003081{
3082 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003083 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003084 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003085 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003086 void *addr, *dst_addr;
3087
Mark Fashehf4414602015-06-30 14:42:05 -07003088 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003089 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003090 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003091 cmp_len = len;
3092
Mark Fashehf4414602015-06-30 14:42:05 -07003093 BUG_ON(i >= cmp->num_pages);
3094
3095 src_page = cmp->src_pages[i];
3096 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003097 ASSERT(PageLocked(src_page));
3098 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003099
Mark Fasheh416161d2013-08-06 11:42:51 -07003100 addr = kmap_atomic(src_page);
3101 dst_addr = kmap_atomic(dst_page);
3102
3103 flush_dcache_page(src_page);
3104 flush_dcache_page(dst_page);
3105
3106 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003107 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003108
3109 kunmap_atomic(addr);
3110 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003111
3112 if (ret)
3113 break;
3114
Mark Fasheh416161d2013-08-06 11:42:51 -07003115 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003116 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003117 }
3118
3119 return ret;
3120}
3121
Mark Fashehe1d227a2015-06-08 15:05:25 -07003122static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3123 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003124{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003125 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003126 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3127
Mark Fashehe1d227a2015-06-08 15:05:25 -07003128 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003129 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003130
3131 /* if we extend to eof, continue to block boundary */
3132 if (off + len == inode->i_size)
3133 *plen = len = ALIGN(inode->i_size, bs) - off;
3134
Mark Fasheh416161d2013-08-06 11:42:51 -07003135 /* Check that we are block aligned - btrfs_clone() requires this */
3136 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3137 return -EINVAL;
3138
3139 return 0;
3140}
3141
Mark Fashehe1d227a2015-06-08 15:05:25 -07003142static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003143 struct inode *dst, u64 dst_loff)
3144{
3145 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003146 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003147 struct cmp_pages cmp;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003148 int same_inode = 0;
3149 u64 same_lock_start = 0;
3150 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003151
Mark Fasheh416161d2013-08-06 11:42:51 -07003152 if (src == dst)
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003153 same_inode = 1;
Mark Fasheh416161d2013-08-06 11:42:51 -07003154
Filipe Manana113e8282015-03-30 18:26:47 +01003155 if (len == 0)
3156 return 0;
3157
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003158 if (same_inode) {
Al Viro59551022016-01-22 15:40:57 -05003159 inode_lock(src);
Mark Fasheh416161d2013-08-06 11:42:51 -07003160
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003161 ret = extent_same_check_offsets(src, loff, &len, olen);
3162 if (ret)
3163 goto out_unlock;
Filipe Mananaf4dfe682016-02-12 14:44:00 +00003164 ret = extent_same_check_offsets(src, dst_loff, &len, olen);
3165 if (ret)
3166 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003167
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003168 /*
3169 * Single inode case wants the same checks, except we
3170 * don't want our length pushed out past i_size as
3171 * comparing that data range makes no sense.
3172 *
3173 * extent_same_check_offsets() will do this for an
3174 * unaligned length at i_size, so catch it here and
3175 * reject the request.
3176 *
3177 * This effectively means we require aligned extents
3178 * for the single-inode case, whereas the other cases
3179 * allow an unaligned length so long as it ends at
3180 * i_size.
3181 */
3182 if (len != olen) {
3183 ret = -EINVAL;
3184 goto out_unlock;
3185 }
3186
3187 /* Check for overlapping ranges */
3188 if (dst_loff + len > loff && dst_loff < loff + len) {
3189 ret = -EINVAL;
3190 goto out_unlock;
3191 }
3192
3193 same_lock_start = min_t(u64, loff, dst_loff);
3194 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
3195 } else {
3196 btrfs_double_inode_lock(src, dst);
3197
3198 ret = extent_same_check_offsets(src, loff, &len, olen);
3199 if (ret)
3200 goto out_unlock;
3201
3202 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3203 if (ret)
3204 goto out_unlock;
3205 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003206
3207 /* don't make the dst file partly checksummed */
3208 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3209 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3210 ret = -EINVAL;
3211 goto out_unlock;
3212 }
3213
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003214again:
Mark Fashehf4414602015-06-30 14:42:05 -07003215 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3216 if (ret)
3217 goto out_unlock;
3218
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003219 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003220 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3221 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003222 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003223 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3224 false);
3225 /*
3226 * If one of the inodes has dirty pages in the respective range or
3227 * ordered extents, we need to flush dellaloc and wait for all ordered
3228 * extents in the range. We must unlock the pages and the ranges in the
3229 * io trees to avoid deadlocks when flushing delalloc (requires locking
3230 * pages) and when waiting for ordered extents to complete (they require
3231 * range locking).
3232 */
3233 if (ret == -EAGAIN) {
3234 /*
3235 * Ranges in the io trees already unlocked. Now unlock all
3236 * pages before waiting for all IO to complete.
3237 */
3238 btrfs_cmp_data_free(&cmp);
3239 if (same_inode) {
3240 btrfs_wait_ordered_range(src, same_lock_start,
3241 same_lock_len);
3242 } else {
3243 btrfs_wait_ordered_range(src, loff, len);
3244 btrfs_wait_ordered_range(dst, dst_loff, len);
3245 }
3246 goto again;
3247 }
3248 ASSERT(ret == 0);
3249 if (WARN_ON(ret)) {
3250 /* ranges in the io trees already unlocked */
3251 btrfs_cmp_data_free(&cmp);
3252 return ret;
3253 }
Mark Fashehf4414602015-06-30 14:42:05 -07003254
Mark Fasheh207910d2015-06-30 14:42:04 -07003255 /* pass original length for comparison so we stay within i_size */
Mark Fashehf4414602015-06-30 14:42:05 -07003256 ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003257 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003258 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003259
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003260 if (same_inode)
3261 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3262 same_lock_start + same_lock_len - 1);
3263 else
3264 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003265
3266 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003267out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003268 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003269 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003270 else
3271 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003272
3273 return ret;
3274}
3275
Byongho Leeee221842015-12-15 01:42:10 +09003276#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003277
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003278ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3279 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003280{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003281 struct inode *src = file_inode(src_file);
3282 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003283 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003284 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003285
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003286 if (olen > BTRFS_MAX_DEDUPE_LEN)
3287 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003288
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003289 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003290 /*
3291 * Btrfs does not support blocksize < page_size. As a
3292 * result, btrfs_cmp_data() won't correctly handle
3293 * this situation without an update.
3294 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003295 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003296 }
3297
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003298 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3299 if (res)
3300 return res;
3301 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003302}
3303
Filipe Mananaf82a9902014-06-01 01:50:28 +01003304static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3305 struct inode *inode,
3306 u64 endoff,
3307 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003308 const u64 olen,
3309 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003310{
3311 struct btrfs_root *root = BTRFS_I(inode)->root;
3312 int ret;
3313
3314 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003315 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003316 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003317 /*
3318 * We round up to the block size at eof when determining which
3319 * extents to clone above, but shouldn't round up the file size.
3320 */
3321 if (endoff > destoff + olen)
3322 endoff = destoff + olen;
3323 if (endoff > inode->i_size)
3324 btrfs_i_size_write(inode, endoff);
3325
3326 ret = btrfs_update_inode(trans, root, inode);
3327 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003328 btrfs_abort_transaction(trans, ret);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003329 btrfs_end_transaction(trans, root);
3330 goto out;
3331 }
3332 ret = btrfs_end_transaction(trans, root);
3333out:
3334 return ret;
3335}
3336
Filipe Manana7ffbb592014-06-09 03:48:05 +01003337static void clone_update_extent_map(struct inode *inode,
3338 const struct btrfs_trans_handle *trans,
3339 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003340 const u64 hole_offset,
3341 const u64 hole_len)
3342{
3343 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3344 struct extent_map *em;
3345 int ret;
3346
3347 em = alloc_extent_map();
3348 if (!em) {
3349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3350 &BTRFS_I(inode)->runtime_flags);
3351 return;
3352 }
3353
Filipe Manana14f59792014-06-29 21:45:40 +01003354 if (path) {
3355 struct btrfs_file_extent_item *fi;
3356
3357 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3358 struct btrfs_file_extent_item);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003359 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3360 em->generation = -1;
3361 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3362 BTRFS_FILE_EXTENT_INLINE)
3363 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3364 &BTRFS_I(inode)->runtime_flags);
3365 } else {
3366 em->start = hole_offset;
3367 em->len = hole_len;
3368 em->ram_bytes = em->len;
3369 em->orig_start = hole_offset;
3370 em->block_start = EXTENT_MAP_HOLE;
3371 em->block_len = 0;
3372 em->orig_block_len = 0;
3373 em->compress_type = BTRFS_COMPRESS_NONE;
3374 em->generation = trans->transid;
3375 }
3376
3377 while (1) {
3378 write_lock(&em_tree->lock);
3379 ret = add_extent_mapping(em_tree, em, 1);
3380 write_unlock(&em_tree->lock);
3381 if (ret != -EEXIST) {
3382 free_extent_map(em);
3383 break;
3384 }
3385 btrfs_drop_extent_cache(inode, em->start,
3386 em->start + em->len - 1, 0);
3387 }
3388
David Sterbaee39b432014-09-30 01:33:33 +02003389 if (ret)
Filipe Manana7ffbb592014-06-09 03:48:05 +01003390 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3391 &BTRFS_I(inode)->runtime_flags);
3392}
3393
Filipe Manana8039d872015-10-13 15:15:00 +01003394/*
3395 * Make sure we do not end up inserting an inline extent into a file that has
3396 * already other (non-inline) extents. If a file has an inline extent it can
3397 * not have any other extents and the (single) inline extent must start at the
3398 * file offset 0. Failing to respect these rules will lead to file corruption,
3399 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3400 *
3401 * We can have extents that have been already written to disk or we can have
3402 * dirty ranges still in delalloc, in which case the extent maps and items are
3403 * created only when we run delalloc, and the delalloc ranges might fall outside
3404 * the range we are currently locking in the inode's io tree. So we check the
3405 * inode's i_size because of that (i_size updates are done while holding the
3406 * i_mutex, which we are holding here).
3407 * We also check to see if the inode has a size not greater than "datal" but has
3408 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3409 * protected against such concurrent fallocate calls by the i_mutex).
3410 *
3411 * If the file has no extents but a size greater than datal, do not allow the
3412 * copy because we would need turn the inline extent into a non-inline one (even
3413 * with NO_HOLES enabled). If we find our destination inode only has one inline
3414 * extent, just overwrite it with the source inline extent if its size is less
3415 * than the source extent's size, or we could copy the source inline extent's
3416 * data into the destination inode's inline extent if the later is greater then
3417 * the former.
3418 */
3419static int clone_copy_inline_extent(struct inode *src,
3420 struct inode *dst,
3421 struct btrfs_trans_handle *trans,
3422 struct btrfs_path *path,
3423 struct btrfs_key *new_key,
3424 const u64 drop_start,
3425 const u64 datal,
3426 const u64 skip,
3427 const u64 size,
3428 char *inline_data)
3429{
3430 struct btrfs_root *root = BTRFS_I(dst)->root;
3431 const u64 aligned_end = ALIGN(new_key->offset + datal,
3432 root->sectorsize);
3433 int ret;
3434 struct btrfs_key key;
3435
3436 if (new_key->offset > 0)
3437 return -EOPNOTSUPP;
3438
3439 key.objectid = btrfs_ino(dst);
3440 key.type = BTRFS_EXTENT_DATA_KEY;
3441 key.offset = 0;
3442 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3443 if (ret < 0) {
3444 return ret;
3445 } else if (ret > 0) {
3446 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3447 ret = btrfs_next_leaf(root, path);
3448 if (ret < 0)
3449 return ret;
3450 else if (ret > 0)
3451 goto copy_inline_extent;
3452 }
3453 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3454 if (key.objectid == btrfs_ino(dst) &&
3455 key.type == BTRFS_EXTENT_DATA_KEY) {
3456 ASSERT(key.offset > 0);
3457 return -EOPNOTSUPP;
3458 }
3459 } else if (i_size_read(dst) <= datal) {
3460 struct btrfs_file_extent_item *ei;
3461 u64 ext_len;
3462
3463 /*
3464 * If the file size is <= datal, make sure there are no other
3465 * extents following (can happen do to an fallocate call with
3466 * the flag FALLOC_FL_KEEP_SIZE).
3467 */
3468 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3469 struct btrfs_file_extent_item);
3470 /*
3471 * If it's an inline extent, it can not have other extents
3472 * following it.
3473 */
3474 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3475 BTRFS_FILE_EXTENT_INLINE)
3476 goto copy_inline_extent;
3477
3478 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3479 if (ext_len > aligned_end)
3480 return -EOPNOTSUPP;
3481
3482 ret = btrfs_next_item(root, path);
3483 if (ret < 0) {
3484 return ret;
3485 } else if (ret == 0) {
3486 btrfs_item_key_to_cpu(path->nodes[0], &key,
3487 path->slots[0]);
3488 if (key.objectid == btrfs_ino(dst) &&
3489 key.type == BTRFS_EXTENT_DATA_KEY)
3490 return -EOPNOTSUPP;
3491 }
3492 }
3493
3494copy_inline_extent:
3495 /*
3496 * We have no extent items, or we have an extent at offset 0 which may
3497 * or may not be inlined. All these cases are dealt the same way.
3498 */
3499 if (i_size_read(dst) > datal) {
3500 /*
3501 * If the destination inode has an inline extent...
3502 * This would require copying the data from the source inline
3503 * extent into the beginning of the destination's inline extent.
3504 * But this is really complex, both extents can be compressed
3505 * or just one of them, which would require decompressing and
3506 * re-compressing data (which could increase the new compressed
3507 * size, not allowing the compressed data to fit anymore in an
3508 * inline extent).
3509 * So just don't support this case for now (it should be rare,
3510 * we are not really saving space when cloning inline extents).
3511 */
3512 return -EOPNOTSUPP;
3513 }
3514
3515 btrfs_release_path(path);
3516 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3517 if (ret)
3518 return ret;
3519 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3520 if (ret)
3521 return ret;
3522
3523 if (skip) {
3524 const u32 start = btrfs_file_extent_calc_inline_size(0);
3525
3526 memmove(inline_data + start, inline_data + start + skip, datal);
3527 }
3528
3529 write_extent_buffer(path->nodes[0], inline_data,
3530 btrfs_item_ptr_offset(path->nodes[0],
3531 path->slots[0]),
3532 size);
3533 inode_add_bytes(dst, datal);
3534
3535 return 0;
3536}
3537
Mark Fasheh32b7c682013-08-06 11:42:49 -07003538/**
3539 * btrfs_clone() - clone a range from inode file to another
3540 *
3541 * @src: Inode to clone from
3542 * @inode: Inode to clone to
3543 * @off: Offset within source to start clone from
3544 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003545 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003546 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003547 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003548 */
3549static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003550 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003551 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003552{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003553 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003554 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003555 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003556 struct btrfs_trans_handle *trans;
3557 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003558 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003559 u32 nritems;
3560 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003561 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003562 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003563 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003564
3565 ret = -ENOMEM;
David Sterba15351952016-04-11 18:40:08 +02003566 buf = kmalloc(root->nodesize, GFP_KERNEL | __GFP_NOWARN);
3567 if (!buf) {
3568 buf = vmalloc(root->nodesize);
3569 if (!buf)
3570 return ret;
3571 }
Yan Zhengae01a0a2008-08-04 23:23:47 -04003572
3573 path = btrfs_alloc_path();
3574 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003575 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003576 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003577 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003578
David Sterbae4058b52015-11-27 16:31:35 +01003579 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003580 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08003581 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003582 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003583 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003584
3585 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003586 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003587
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003588 /*
3589 * note the key will change type as we walk through the
3590 * tree.
3591 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003592 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003593 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3594 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003595 if (ret < 0)
3596 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003597 /*
3598 * First search, if no extent item that starts at offset off was
3599 * found but the previous item is an extent item, it's possible
3600 * it might overlap our target range, therefore process it.
3601 */
3602 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3603 btrfs_item_key_to_cpu(path->nodes[0], &key,
3604 path->slots[0] - 1);
3605 if (key.type == BTRFS_EXTENT_DATA_KEY)
3606 path->slots[0]--;
3607 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003608
Yan Zhengae01a0a2008-08-04 23:23:47 -04003609 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003610process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003611 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003612 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003613 if (ret < 0)
3614 goto out;
3615 if (ret > 0)
3616 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003617 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003618 }
3619 leaf = path->nodes[0];
3620 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003621
Yan Zhengae01a0a2008-08-04 23:23:47 -04003622 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003623 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08003624 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003625 break;
3626
David Sterba962a2982014-06-04 18:41:45 +02003627 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003628 struct btrfs_file_extent_item *extent;
3629 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003630 u32 size;
3631 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003632 u64 disko = 0, diskl = 0;
3633 u64 datao = 0, datal = 0;
3634 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003635 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003636
Sage Weilc5c9cd42008-11-12 14:32:25 -05003637 extent = btrfs_item_ptr(leaf, slot,
3638 struct btrfs_file_extent_item);
3639 comp = btrfs_file_extent_compression(leaf, extent);
3640 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003641 if (type == BTRFS_FILE_EXTENT_REG ||
3642 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003643 disko = btrfs_file_extent_disk_bytenr(leaf,
3644 extent);
3645 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3646 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003647 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003648 datal = btrfs_file_extent_num_bytes(leaf,
3649 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003650 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3651 /* take upper bound, may be compressed */
3652 datal = btrfs_file_extent_ram_bytes(leaf,
3653 extent);
3654 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003655
Filipe Manana2c463822014-05-31 02:31:05 +01003656 /*
3657 * The first search might have left us at an extent
3658 * item that ends before our target range's start, can
3659 * happen if we have holes and NO_HOLES feature enabled.
3660 */
3661 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003662 path->slots[0]++;
3663 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003664 } else if (key.offset >= off + len) {
3665 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003666 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003667 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003668 size = btrfs_item_size_nr(leaf, slot);
3669 read_extent_buffer(leaf, buf,
3670 btrfs_item_ptr_offset(leaf, slot),
3671 size);
3672
3673 btrfs_release_path(path);
3674 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003675
Zheng Yan31840ae2008-09-23 13:14:14 -04003676 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08003677 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08003678 if (off <= key.offset)
3679 new_key.offset = key.offset + destoff - off;
3680 else
3681 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003682
Sage Weilb6f34092011-09-20 14:48:51 -04003683 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003684 * Deal with a hole that doesn't have an extent item
3685 * that represents it (NO_HOLES feature enabled).
3686 * This hole is either in the middle of the cloning
3687 * range or at the beginning (fully overlaps it or
3688 * partially overlaps it).
3689 */
3690 if (new_key.offset != last_dest_end)
3691 drop_start = last_dest_end;
3692 else
3693 drop_start = new_key.offset;
3694
3695 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003696 * 1 - adjusting old extent (we may have to split it)
3697 * 1 - add new extent
3698 * 1 - inode update
3699 */
3700 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003701 if (IS_ERR(trans)) {
3702 ret = PTR_ERR(trans);
3703 goto out;
3704 }
3705
Chris Masonc8a894d2009-06-27 21:07:03 -04003706 if (type == BTRFS_FILE_EXTENT_REG ||
3707 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003708 /*
3709 * a | --- range to clone ---| b
3710 * | ------------- extent ------------- |
3711 */
3712
Antonio Ospite93915582014-06-04 14:03:48 +02003713 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003714 if (key.offset + datal > off + len)
3715 datal = off + len - key.offset;
3716
Antonio Ospite93915582014-06-04 14:03:48 +02003717 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003718 if (off > key.offset) {
3719 datao += off - key.offset;
3720 datal -= off - key.offset;
3721 }
3722
Josef Bacik5dc562c2012-08-17 13:14:17 -04003723 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003724 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003725 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003726 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003727 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003728 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003729 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003730 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003731 btrfs_end_transaction(trans, root);
3732 goto out;
3733 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003734
Sage Weilc5c9cd42008-11-12 14:32:25 -05003735 ret = btrfs_insert_empty_item(trans, root, path,
3736 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003737 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003738 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003739 btrfs_end_transaction(trans, root);
3740 goto out;
3741 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003742
3743 leaf = path->nodes[0];
3744 slot = path->slots[0];
3745 write_extent_buffer(leaf, buf,
3746 btrfs_item_ptr_offset(leaf, slot),
3747 size);
3748
3749 extent = btrfs_item_ptr(leaf, slot,
3750 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003751
Sage Weilc5c9cd42008-11-12 14:32:25 -05003752 /* disko == 0 means it's a hole */
3753 if (!disko)
3754 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003755
3756 btrfs_set_file_extent_offset(leaf, extent,
3757 datao);
3758 btrfs_set_file_extent_num_bytes(leaf, extent,
3759 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003760
Sage Weilc5c9cd42008-11-12 14:32:25 -05003761 if (disko) {
3762 inode_add_bytes(inode, datal);
3763 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003764 disko, diskl, 0,
3765 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003766 btrfs_ino(inode),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003767 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003768 if (ret) {
3769 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003770 ret);
3771 btrfs_end_transaction(trans,
3772 root);
3773 goto out;
3774
3775 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003776 }
3777 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3778 u64 skip = 0;
3779 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003780
Sage Weilc5c9cd42008-11-12 14:32:25 -05003781 if (off > key.offset) {
3782 skip = off - key.offset;
3783 new_key.offset += skip;
3784 }
Chris Masond3977122009-01-05 21:25:51 -05003785
Liu Boaa42ffd2012-09-18 03:52:23 -06003786 if (key.offset + datal > off + len)
3787 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003788
Sage Weilc5c9cd42008-11-12 14:32:25 -05003789 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003790 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003791 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003792 goto out;
3793 }
3794 size -= skip + trim;
3795 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003796
Filipe Manana8039d872015-10-13 15:15:00 +01003797 ret = clone_copy_inline_extent(src, inode,
3798 trans, path,
3799 &new_key,
3800 drop_start,
3801 datal,
3802 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003803 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003804 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003805 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003806 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 btrfs_end_transaction(trans, root);
3808 goto out;
3809 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003810 leaf = path->nodes[0];
3811 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003812 }
3813
Filipe Manana7ffbb592014-06-09 03:48:05 +01003814 /* If we have an implicit hole (NO_HOLES feature). */
3815 if (drop_start < new_key.offset)
3816 clone_update_extent_map(inode, trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003817 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003818 new_key.offset - drop_start);
3819
Filipe Manana14f59792014-06-29 21:45:40 +01003820 clone_update_extent_map(inode, trans, path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003821
Sage Weilc5c9cd42008-11-12 14:32:25 -05003822 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003823 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003824
Filipe Manana62e23902014-08-08 02:47:06 +01003825 last_dest_end = ALIGN(new_key.offset + datal,
3826 root->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003827 ret = clone_finish_inode_update(trans, inode,
3828 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003829 destoff, olen,
3830 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003831 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003832 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003833 if (new_key.offset + datal >= destoff + len)
3834 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003835 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003836 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003837 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003838
3839 if (fatal_signal_pending(current)) {
3840 ret = -EINTR;
3841 goto out;
3842 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003843 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003844 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003845
Filipe Mananaf82a9902014-06-01 01:50:28 +01003846 if (last_dest_end < destoff + len) {
3847 /*
3848 * We have an implicit hole (NO_HOLES feature is enabled) that
3849 * fully or partially overlaps our cloning range at its end.
3850 */
3851 btrfs_release_path(path);
3852
3853 /*
3854 * 1 - remove extent(s)
3855 * 1 - inode update
3856 */
3857 trans = btrfs_start_transaction(root, 2);
3858 if (IS_ERR(trans)) {
3859 ret = PTR_ERR(trans);
3860 goto out;
3861 }
3862 ret = btrfs_drop_extents(trans, root, inode,
3863 last_dest_end, destoff + len, 1);
3864 if (ret) {
3865 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003866 btrfs_abort_transaction(trans, ret);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003867 btrfs_end_transaction(trans, root);
3868 goto out;
3869 }
Filipe Manana14f59792014-06-29 21:45:40 +01003870 clone_update_extent_map(inode, trans, NULL, last_dest_end,
3871 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003872 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003873 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003874 }
3875
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003876out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003877 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003878 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003879 return ret;
3880}
3881
Zach Brown3db11b22015-11-10 16:53:32 -05003882static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3883 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003884{
Al Viro54563d42013-09-01 15:57:51 -04003885 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003886 struct inode *src = file_inode(file_src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003887 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003888 int ret;
3889 u64 len = olen;
3890 u64 bs = root->fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003891 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003892
3893 /*
3894 * TODO:
3895 * - split compressed inline extents. annoying: we need to
3896 * decompress into destination's address_space (the file offset
3897 * may change, so source mapping won't do), then recompress (or
3898 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003899 *
3900 * - split destination inode's inline extents. The inline extents can
3901 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003902 */
3903
Mark Fasheh32b7c682013-08-06 11:42:49 -07003904 if (btrfs_root_readonly(root))
3905 return -EROFS;
3906
Zach Brown3db11b22015-11-10 16:53:32 -05003907 if (file_src->f_path.mnt != file->f_path.mnt ||
3908 src->i_sb != inode->i_sb)
3909 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003910
Mark Fasheh32b7c682013-08-06 11:42:49 -07003911 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003912 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003913
3914 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003915 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003916 } else {
Al Viro59551022016-01-22 15:40:57 -05003917 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003918 }
3919
Omar Sandoval52ea25b2018-05-22 15:02:12 -07003920 /* don't make the dst file partly checksummed */
3921 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3922 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
3923 ret = -EINVAL;
3924 goto out_unlock;
3925 }
3926
Mark Fasheh32b7c682013-08-06 11:42:49 -07003927 /* determine range to clone */
3928 ret = -EINVAL;
3929 if (off + len > src->i_size || off + len < off)
3930 goto out_unlock;
3931 if (len == 0)
3932 olen = len = src->i_size - off;
Filipe Manana800d8112018-11-05 11:14:17 +00003933 /*
3934 * If we extend to eof, continue to block boundary if and only if the
3935 * destination end offset matches the destination file's size, otherwise
3936 * we would be corrupting data by placing the eof block into the middle
3937 * of a file.
3938 */
3939 if (off + len == src->i_size) {
3940 if (!IS_ALIGNED(len, bs) && destoff + len < inode->i_size)
3941 goto out_unlock;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003942 len = ALIGN(src->i_size, bs) - off;
Filipe Manana800d8112018-11-05 11:14:17 +00003943 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003944
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003945 if (len == 0) {
3946 ret = 0;
3947 goto out_unlock;
3948 }
3949
Mark Fasheh32b7c682013-08-06 11:42:49 -07003950 /* verify the end result is block aligned */
3951 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3952 !IS_ALIGNED(destoff, bs))
3953 goto out_unlock;
3954
3955 /* verify if ranges are overlapped within the same file */
3956 if (same_inode) {
3957 if (destoff + len > off && destoff < off + len)
3958 goto out_unlock;
3959 }
3960
3961 if (destoff > inode->i_size) {
3962 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3963 if (ret)
3964 goto out_unlock;
3965 }
3966
Filipe Mananac125b8b2014-05-23 05:03:34 +01003967 /*
3968 * Lock the target range too. Right after we replace the file extent
3969 * items in the fs tree (which now point to the cloned data), we might
3970 * have a worker replace them with extent items relative to a write
3971 * operation that was issued before this clone operation (i.e. confront
3972 * with inode.c:btrfs_finish_ordered_io).
3973 */
3974 if (same_inode) {
3975 u64 lock_start = min_t(u64, off, destoff);
3976 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003977
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003978 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003979 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003980 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3981 true);
3982 }
3983 ASSERT(ret == 0);
3984 if (WARN_ON(ret)) {
3985 /* ranges in the io trees already unlocked */
3986 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003987 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003988
Mark Fasheh1c919a52015-06-30 14:42:08 -07003989 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003990
Filipe Mananac125b8b2014-05-23 05:03:34 +01003991 if (same_inode) {
3992 u64 lock_start = min_t(u64, off, destoff);
3993 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3994
3995 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3996 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003997 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003998 }
3999 /*
4000 * Truncate page cache pages so that future reads will see the cloned
4001 * data immediately and not the previous data.
4002 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05304003 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004004 round_down(destoff, PAGE_SIZE),
4005 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004006out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07004007 if (!same_inode)
4008 btrfs_double_inode_unlock(src, inode);
4009 else
Al Viro59551022016-01-22 15:40:57 -05004010 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05004011 return ret;
4012}
4013
4014ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
4015 struct file *file_out, loff_t pos_out,
4016 size_t len, unsigned int flags)
4017{
4018 ssize_t ret;
4019
4020 ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
4021 if (ret == 0)
4022 ret = len;
4023 return ret;
4024}
4025
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004026int btrfs_clone_file_range(struct file *src_file, loff_t off,
4027 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05004028{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004029 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004030}
4031
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004032/*
4033 * there are many ways the trans_start and trans_end ioctls can lead
4034 * to deadlocks. They should only be used by applications that
4035 * basically own the machine, and have a very in depth understanding
4036 * of all the possible deadlocks and enospc problems.
4037 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05004038static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004039{
Al Viro496ad9a2013-01-23 17:07:38 -05004040 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004041 struct btrfs_root *root = BTRFS_I(inode)->root;
4042 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004043 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004044
Sage Weil1ab86ae2009-09-29 18:38:44 -04004045 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04004046 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004047 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004048
4049 ret = -EINPROGRESS;
4050 if (file->private_data)
4051 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004052
Li Zefanb83cc962010-12-20 16:04:08 +08004053 ret = -EROFS;
4054 if (btrfs_root_readonly(root))
4055 goto out;
4056
Al Viroa561be72011-11-23 11:57:51 -05004057 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05004058 if (ret)
4059 goto out;
4060
Josef Bacika4abeea2011-04-11 17:25:13 -04004061 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004062
Sage Weil1ab86ae2009-09-29 18:38:44 -04004063 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004064 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00004065 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04004066 goto out_drop;
4067
4068 file->private_data = trans;
4069 return 0;
4070
4071out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04004072 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05004073 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004074out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004075 return ret;
4076}
4077
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004078static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4079{
Al Viro496ad9a2013-01-23 17:07:38 -05004080 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004081 struct btrfs_root *root = BTRFS_I(inode)->root;
4082 struct btrfs_root *new_root;
4083 struct btrfs_dir_item *di;
4084 struct btrfs_trans_handle *trans;
4085 struct btrfs_path *path;
4086 struct btrfs_key location;
4087 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004088 u64 objectid = 0;
4089 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004090 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004091
4092 if (!capable(CAP_SYS_ADMIN))
4093 return -EPERM;
4094
Miao Xie3c04ce02012-11-26 08:43:07 +00004095 ret = mnt_want_write_file(file);
4096 if (ret)
4097 return ret;
4098
4099 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4100 ret = -EFAULT;
4101 goto out;
4102 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004103
4104 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304105 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004106
4107 location.objectid = objectid;
4108 location.type = BTRFS_ROOT_ITEM_KEY;
4109 location.offset = (u64)-1;
4110
4111 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004112 if (IS_ERR(new_root)) {
4113 ret = PTR_ERR(new_root);
4114 goto out;
4115 }
satoru takeuchif11525d2017-09-12 22:42:52 +09004116 if (!is_fstree(new_root->objectid)) {
4117 ret = -ENOENT;
4118 goto out;
4119 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004120
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004121 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004122 if (!path) {
4123 ret = -ENOMEM;
4124 goto out;
4125 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004126 path->leave_spinning = 1;
4127
4128 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004129 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004130 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004131 ret = PTR_ERR(trans);
4132 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004133 }
4134
David Sterba6c417612011-04-13 15:41:04 +02004135 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004136 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
4137 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004138 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004139 btrfs_free_path(path);
4140 btrfs_end_transaction(trans, root);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004141 btrfs_err(new_root->fs_info,
4142 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004143 ret = -ENOENT;
4144 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004145 }
4146
4147 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4148 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4149 btrfs_mark_buffer_dirty(path->nodes[0]);
4150 btrfs_free_path(path);
4151
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06004152 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004153 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00004154out:
4155 mnt_drop_write_file(file);
4156 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004157}
4158
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004159void btrfs_get_block_group_info(struct list_head *groups_list,
4160 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004161{
4162 struct btrfs_block_group_cache *block_group;
4163
4164 space->total_bytes = 0;
4165 space->used_bytes = 0;
4166 space->flags = 0;
4167 list_for_each_entry(block_group, groups_list, list) {
4168 space->flags = block_group->flags;
4169 space->total_bytes += block_group->key.offset;
4170 space->used_bytes +=
4171 btrfs_block_group_used(&block_group->item);
4172 }
4173}
4174
Eric Sandeen48a3b632013-04-25 20:41:01 +00004175static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004176{
4177 struct btrfs_ioctl_space_args space_args;
4178 struct btrfs_ioctl_space_info space;
4179 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004180 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004181 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004182 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004183 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4184 BTRFS_BLOCK_GROUP_SYSTEM,
4185 BTRFS_BLOCK_GROUP_METADATA,
4186 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4187 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004188 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004189 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004190 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004191 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004192
4193 if (copy_from_user(&space_args,
4194 (struct btrfs_ioctl_space_args __user *)arg,
4195 sizeof(space_args)))
4196 return -EFAULT;
4197
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004198 for (i = 0; i < num_types; i++) {
4199 struct btrfs_space_info *tmp;
4200
4201 info = NULL;
4202 rcu_read_lock();
4203 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4204 list) {
4205 if (tmp->flags == types[i]) {
4206 info = tmp;
4207 break;
4208 }
4209 }
4210 rcu_read_unlock();
4211
4212 if (!info)
4213 continue;
4214
4215 down_read(&info->groups_sem);
4216 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4217 if (!list_empty(&info->block_groups[c]))
4218 slot_count++;
4219 }
4220 up_read(&info->groups_sem);
4221 }
Josef Bacik1406e432010-01-13 18:19:06 +00004222
David Sterba36523e952014-02-07 14:34:12 +01004223 /*
4224 * Global block reserve, exported as a space_info
4225 */
4226 slot_count++;
4227
Chris Mason7fde62b2010-03-16 15:40:10 -04004228 /* space_slots == 0 means they are asking for a count */
4229 if (space_args.space_slots == 0) {
4230 space_args.total_spaces = slot_count;
4231 goto out;
4232 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004233
Dan Rosenberg51788b12011-02-14 16:04:23 -05004234 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004235
Chris Mason7fde62b2010-03-16 15:40:10 -04004236 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004237
Chris Mason7fde62b2010-03-16 15:40:10 -04004238 /* we generally have at most 6 or so space infos, one for each raid
4239 * level. So, a whole page should be more than enough for everyone
4240 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004241 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004242 return -ENOMEM;
4243
4244 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004245 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004246 if (!dest)
4247 return -ENOMEM;
4248 dest_orig = dest;
4249
4250 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004251 for (i = 0; i < num_types; i++) {
4252 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004253
Dan Rosenberg51788b12011-02-14 16:04:23 -05004254 if (!slot_count)
4255 break;
4256
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004257 info = NULL;
4258 rcu_read_lock();
4259 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4260 list) {
4261 if (tmp->flags == types[i]) {
4262 info = tmp;
4263 break;
4264 }
4265 }
4266 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004267
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004268 if (!info)
4269 continue;
4270 down_read(&info->groups_sem);
4271 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4272 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004273 btrfs_get_block_group_info(
4274 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004275 memcpy(dest, &space, sizeof(space));
4276 dest++;
4277 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004278 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004279 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004280 if (!slot_count)
4281 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004282 }
4283 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004284 }
Josef Bacik1406e432010-01-13 18:19:06 +00004285
David Sterba36523e952014-02-07 14:34:12 +01004286 /*
4287 * Add global block reserve
4288 */
4289 if (slot_count) {
4290 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
4291
4292 spin_lock(&block_rsv->lock);
4293 space.total_bytes = block_rsv->size;
4294 space.used_bytes = block_rsv->size - block_rsv->reserved;
4295 spin_unlock(&block_rsv->lock);
4296 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4297 memcpy(dest, &space, sizeof(space));
4298 space_args.total_spaces++;
4299 }
4300
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004301 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004302 (arg + sizeof(struct btrfs_ioctl_space_args));
4303
4304 if (copy_to_user(user_dest, dest_orig, alloc_size))
4305 ret = -EFAULT;
4306
4307 kfree(dest_orig);
4308out:
4309 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004310 ret = -EFAULT;
4311
4312 return ret;
4313}
4314
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004315/*
4316 * there are many ways the trans_start and trans_end ioctls can lead
4317 * to deadlocks. They should only be used by applications that
4318 * basically own the machine, and have a very in depth understanding
4319 * of all the possible deadlocks and enospc problems.
4320 */
4321long btrfs_ioctl_trans_end(struct file *file)
4322{
Al Viro496ad9a2013-01-23 17:07:38 -05004323 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004324 struct btrfs_root *root = BTRFS_I(inode)->root;
4325 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004326
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004327 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004328 if (!trans)
4329 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004330 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004331
Sage Weil1ab86ae2009-09-29 18:38:44 -04004332 btrfs_end_transaction(trans, root);
4333
Josef Bacika4abeea2011-04-11 17:25:13 -04004334 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004335
Al Viro2a79f172011-12-09 08:06:57 -05004336 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004337 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004338}
4339
Miao Xie9a8c28b2012-11-26 08:40:43 +00004340static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4341 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004342{
Sage Weil46204592010-10-29 15:41:32 -04004343 struct btrfs_trans_handle *trans;
4344 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004345 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004346
Miao Xied4edf392013-02-20 09:17:06 +00004347 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004348 if (IS_ERR(trans)) {
4349 if (PTR_ERR(trans) != -ENOENT)
4350 return PTR_ERR(trans);
4351
4352 /* No running transaction, don't bother */
4353 transid = root->fs_info->last_trans_committed;
4354 goto out;
4355 }
Sage Weil46204592010-10-29 15:41:32 -04004356 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004357 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004358 if (ret) {
4359 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004360 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004361 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004362out:
Sage Weil46204592010-10-29 15:41:32 -04004363 if (argp)
4364 if (copy_to_user(argp, &transid, sizeof(transid)))
4365 return -EFAULT;
4366 return 0;
4367}
4368
Miao Xie9a8c28b2012-11-26 08:40:43 +00004369static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4370 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004371{
Sage Weil46204592010-10-29 15:41:32 -04004372 u64 transid;
4373
4374 if (argp) {
4375 if (copy_from_user(&transid, argp, sizeof(transid)))
4376 return -EFAULT;
4377 } else {
4378 transid = 0; /* current trans */
4379 }
4380 return btrfs_wait_for_commit(root, transid);
4381}
4382
Miao Xieb8e95482012-11-26 08:48:01 +00004383static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004384{
Al Viro496ad9a2013-01-23 17:07:38 -05004385 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01004386 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004387 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004388
4389 if (!capable(CAP_SYS_ADMIN))
4390 return -EPERM;
4391
4392 sa = memdup_user(arg, sizeof(*sa));
4393 if (IS_ERR(sa))
4394 return PTR_ERR(sa);
4395
Miao Xieb8e95482012-11-26 08:48:01 +00004396 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4397 ret = mnt_want_write_file(file);
4398 if (ret)
4399 goto out;
4400 }
4401
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004402 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004403 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4404 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004405
4406 if (copy_to_user(arg, sa, sizeof(*sa)))
4407 ret = -EFAULT;
4408
Miao Xieb8e95482012-11-26 08:48:01 +00004409 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4410 mnt_drop_write_file(file);
4411out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004412 kfree(sa);
4413 return ret;
4414}
4415
4416static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4417{
4418 if (!capable(CAP_SYS_ADMIN))
4419 return -EPERM;
4420
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004421 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004422}
4423
4424static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4425 void __user *arg)
4426{
4427 struct btrfs_ioctl_scrub_args *sa;
4428 int ret;
4429
4430 if (!capable(CAP_SYS_ADMIN))
4431 return -EPERM;
4432
4433 sa = memdup_user(arg, sizeof(*sa));
4434 if (IS_ERR(sa))
4435 return PTR_ERR(sa);
4436
4437 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4438
4439 if (copy_to_user(arg, sa, sizeof(*sa)))
4440 ret = -EFAULT;
4441
4442 kfree(sa);
4443 return ret;
4444}
4445
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004446static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004447 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004448{
4449 struct btrfs_ioctl_get_dev_stats *sa;
4450 int ret;
4451
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004452 sa = memdup_user(arg, sizeof(*sa));
4453 if (IS_ERR(sa))
4454 return PTR_ERR(sa);
4455
David Sterbab27f7c02012-06-22 06:30:39 -06004456 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4457 kfree(sa);
4458 return -EPERM;
4459 }
4460
4461 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004462
4463 if (copy_to_user(arg, sa, sizeof(*sa)))
4464 ret = -EFAULT;
4465
4466 kfree(sa);
4467 return ret;
4468}
4469
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004470static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4471{
4472 struct btrfs_ioctl_dev_replace_args *p;
4473 int ret;
4474
4475 if (!capable(CAP_SYS_ADMIN))
4476 return -EPERM;
4477
4478 p = memdup_user(arg, sizeof(*p));
4479 if (IS_ERR(p))
4480 return PTR_ERR(p);
4481
4482 switch (p->cmd) {
4483 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004484 if (root->fs_info->sb->s_flags & MS_RDONLY) {
4485 ret = -EROFS;
4486 goto out;
4487 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004488 if (atomic_xchg(
4489 &root->fs_info->mutually_exclusive_operation_running,
4490 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004491 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004492 } else {
Anand Jainb5255452016-03-24 18:48:14 +08004493 ret = btrfs_dev_replace_by_ioctl(root, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004494 atomic_set(
4495 &root->fs_info->mutually_exclusive_operation_running,
4496 0);
4497 }
4498 break;
4499 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4500 btrfs_dev_replace_status(root->fs_info, p);
4501 ret = 0;
4502 break;
4503 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4504 ret = btrfs_dev_replace_cancel(root->fs_info, p);
4505 break;
4506 default:
4507 ret = -EINVAL;
4508 break;
4509 }
4510
4511 if (copy_to_user(arg, p, sizeof(*p)))
4512 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004513out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004514 kfree(p);
4515 return ret;
4516}
4517
Jan Schmidtd7728c92011-07-07 16:48:38 +02004518static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4519{
4520 int ret = 0;
4521 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004522 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004523 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004524 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004525 struct inode_fs_paths *ipath = NULL;
4526 struct btrfs_path *path;
4527
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004528 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004529 return -EPERM;
4530
4531 path = btrfs_alloc_path();
4532 if (!path) {
4533 ret = -ENOMEM;
4534 goto out;
4535 }
4536
4537 ipa = memdup_user(arg, sizeof(*ipa));
4538 if (IS_ERR(ipa)) {
4539 ret = PTR_ERR(ipa);
4540 ipa = NULL;
4541 goto out;
4542 }
4543
4544 size = min_t(u32, ipa->size, 4096);
4545 ipath = init_ipath(size, root, path);
4546 if (IS_ERR(ipath)) {
4547 ret = PTR_ERR(ipath);
4548 ipath = NULL;
4549 goto out;
4550 }
4551
4552 ret = paths_from_inode(ipa->inum, ipath);
4553 if (ret < 0)
4554 goto out;
4555
4556 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004557 rel_ptr = ipath->fspath->val[i] -
4558 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004559 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004560 }
4561
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004562 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4563 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004564 if (ret) {
4565 ret = -EFAULT;
4566 goto out;
4567 }
4568
4569out:
4570 btrfs_free_path(path);
4571 free_ipath(ipath);
4572 kfree(ipa);
4573
4574 return ret;
4575}
4576
4577static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4578{
4579 struct btrfs_data_container *inodes = ctx;
4580 const size_t c = 3 * sizeof(u64);
4581
4582 if (inodes->bytes_left >= c) {
4583 inodes->bytes_left -= c;
4584 inodes->val[inodes->elem_cnt] = inum;
4585 inodes->val[inodes->elem_cnt + 1] = offset;
4586 inodes->val[inodes->elem_cnt + 2] = root;
4587 inodes->elem_cnt += 3;
4588 } else {
4589 inodes->bytes_missing += c - inodes->bytes_left;
4590 inodes->bytes_left = 0;
4591 inodes->elem_missed += 3;
4592 }
4593
4594 return 0;
4595}
4596
4597static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4598 void __user *arg)
4599{
4600 int ret = 0;
4601 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004602 struct btrfs_ioctl_logical_ino_args *loi;
4603 struct btrfs_data_container *inodes = NULL;
4604 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004605
4606 if (!capable(CAP_SYS_ADMIN))
4607 return -EPERM;
4608
4609 loi = memdup_user(arg, sizeof(*loi));
4610 if (IS_ERR(loi)) {
4611 ret = PTR_ERR(loi);
4612 loi = NULL;
4613 goto out;
4614 }
4615
4616 path = btrfs_alloc_path();
4617 if (!path) {
4618 ret = -ENOMEM;
4619 goto out;
4620 }
4621
Byongho Leeee221842015-12-15 01:42:10 +09004622 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004623 inodes = init_data_container(size);
4624 if (IS_ERR(inodes)) {
4625 ret = PTR_ERR(inodes);
4626 inodes = NULL;
4627 goto out;
4628 }
4629
Liu Bodf031f02012-09-07 20:01:29 -06004630 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4631 build_ino_list, inodes);
4632 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004633 ret = -ENOENT;
4634 if (ret < 0)
4635 goto out;
4636
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004637 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4638 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004639 if (ret)
4640 ret = -EFAULT;
4641
4642out:
4643 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06004644 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004645 kfree(loi);
4646
4647 return ret;
4648}
4649
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004650void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004651 struct btrfs_ioctl_balance_args *bargs)
4652{
4653 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4654
4655 bargs->flags = bctl->flags;
4656
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004657 if (atomic_read(&fs_info->balance_running))
4658 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4659 if (atomic_read(&fs_info->balance_pause_req))
4660 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004661 if (atomic_read(&fs_info->balance_cancel_req))
4662 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004663
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004664 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4665 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4666 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004667
4668 if (lock) {
4669 spin_lock(&fs_info->balance_lock);
4670 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4671 spin_unlock(&fs_info->balance_lock);
4672 } else {
4673 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4674 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004675}
4676
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004677static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004678{
Al Viro496ad9a2013-01-23 17:07:38 -05004679 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004680 struct btrfs_fs_info *fs_info = root->fs_info;
4681 struct btrfs_ioctl_balance_args *bargs;
4682 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004683 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004684 int ret;
4685
4686 if (!capable(CAP_SYS_ADMIN))
4687 return -EPERM;
4688
Liu Boe54bfa32012-06-29 03:58:48 -06004689 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004690 if (ret)
4691 return ret;
4692
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004693again:
4694 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4695 mutex_lock(&fs_info->volume_mutex);
4696 mutex_lock(&fs_info->balance_mutex);
4697 need_unlock = true;
4698 goto locked;
4699 }
4700
4701 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004702 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004703 * (1) some other op is running
4704 * (2) balance is running
4705 * (3) balance is paused -- special case (think resume)
4706 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004707 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004708 if (fs_info->balance_ctl) {
4709 /* this is either (2) or (3) */
4710 if (!atomic_read(&fs_info->balance_running)) {
4711 mutex_unlock(&fs_info->balance_mutex);
4712 if (!mutex_trylock(&fs_info->volume_mutex))
4713 goto again;
4714 mutex_lock(&fs_info->balance_mutex);
4715
4716 if (fs_info->balance_ctl &&
4717 !atomic_read(&fs_info->balance_running)) {
4718 /* this is (3) */
4719 need_unlock = false;
4720 goto locked;
4721 }
4722
4723 mutex_unlock(&fs_info->balance_mutex);
4724 mutex_unlock(&fs_info->volume_mutex);
4725 goto again;
4726 } else {
4727 /* this is (2) */
4728 mutex_unlock(&fs_info->balance_mutex);
4729 ret = -EINPROGRESS;
4730 goto out;
4731 }
4732 } else {
4733 /* this is (1) */
4734 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004735 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004736 goto out;
4737 }
4738
4739locked:
4740 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004741
4742 if (arg) {
4743 bargs = memdup_user(arg, sizeof(*bargs));
4744 if (IS_ERR(bargs)) {
4745 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004746 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004747 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004748
4749 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4750 if (!fs_info->balance_ctl) {
4751 ret = -ENOTCONN;
4752 goto out_bargs;
4753 }
4754
4755 bctl = fs_info->balance_ctl;
4756 spin_lock(&fs_info->balance_lock);
4757 bctl->flags |= BTRFS_BALANCE_RESUME;
4758 spin_unlock(&fs_info->balance_lock);
4759
4760 goto do_balance;
4761 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004762 } else {
4763 bargs = NULL;
4764 }
4765
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004766 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004767 ret = -EINPROGRESS;
4768 goto out_bargs;
4769 }
4770
David Sterba8d2db782015-11-04 15:38:29 +01004771 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004772 if (!bctl) {
4773 ret = -ENOMEM;
4774 goto out_bargs;
4775 }
4776
4777 bctl->fs_info = fs_info;
4778 if (arg) {
4779 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4780 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4781 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4782
4783 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004784 } else {
4785 /* balance everything - no filters */
4786 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004787 }
4788
David Sterba8eb93452015-10-12 16:55:54 +02004789 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4790 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004791 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004792 }
4793
Ilya Dryomovde322262012-01-16 22:04:49 +02004794do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004795 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004796 * Ownership of bctl and mutually_exclusive_operation_running
4797 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4798 * or, if restriper was paused all the way until unmount, in
4799 * free_fs_info. mutually_exclusive_operation_running is
4800 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004801 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004802 need_unlock = false;
4803
4804 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004805 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004806
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004807 if (arg) {
4808 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4809 ret = -EFAULT;
4810 }
4811
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004812out_bctl:
4813 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004814out_bargs:
4815 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004816out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004817 mutex_unlock(&fs_info->balance_mutex);
4818 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004819 if (need_unlock)
4820 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4821out:
Liu Boe54bfa32012-06-29 03:58:48 -06004822 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004823 return ret;
4824}
4825
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004826static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4827{
4828 if (!capable(CAP_SYS_ADMIN))
4829 return -EPERM;
4830
4831 switch (cmd) {
4832 case BTRFS_BALANCE_CTL_PAUSE:
4833 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004834 case BTRFS_BALANCE_CTL_CANCEL:
4835 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004836 }
4837
4838 return -EINVAL;
4839}
4840
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004841static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4842 void __user *arg)
4843{
4844 struct btrfs_fs_info *fs_info = root->fs_info;
4845 struct btrfs_ioctl_balance_args *bargs;
4846 int ret = 0;
4847
4848 if (!capable(CAP_SYS_ADMIN))
4849 return -EPERM;
4850
4851 mutex_lock(&fs_info->balance_mutex);
4852 if (!fs_info->balance_ctl) {
4853 ret = -ENOTCONN;
4854 goto out;
4855 }
4856
David Sterba8d2db782015-11-04 15:38:29 +01004857 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004858 if (!bargs) {
4859 ret = -ENOMEM;
4860 goto out;
4861 }
4862
4863 update_ioctl_balance_args(fs_info, 1, bargs);
4864
4865 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4866 ret = -EFAULT;
4867
4868 kfree(bargs);
4869out:
4870 mutex_unlock(&fs_info->balance_mutex);
4871 return ret;
4872}
4873
Miao Xie905b0dd2012-11-26 08:50:11 +00004874static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004875{
Al Viro496ad9a2013-01-23 17:07:38 -05004876 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004877 struct btrfs_ioctl_quota_ctl_args *sa;
4878 struct btrfs_trans_handle *trans = NULL;
4879 int ret;
4880 int err;
4881
4882 if (!capable(CAP_SYS_ADMIN))
4883 return -EPERM;
4884
Miao Xie905b0dd2012-11-26 08:50:11 +00004885 ret = mnt_want_write_file(file);
4886 if (ret)
4887 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004888
4889 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004890 if (IS_ERR(sa)) {
4891 ret = PTR_ERR(sa);
4892 goto drop_write;
4893 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004894
Wang Shilong7708f022013-04-07 10:24:57 +00004895 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004896 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4897 if (IS_ERR(trans)) {
4898 ret = PTR_ERR(trans);
4899 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004900 }
4901
4902 switch (sa->cmd) {
4903 case BTRFS_QUOTA_CTL_ENABLE:
4904 ret = btrfs_quota_enable(trans, root->fs_info);
4905 break;
4906 case BTRFS_QUOTA_CTL_DISABLE:
4907 ret = btrfs_quota_disable(trans, root->fs_info);
4908 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004909 default:
4910 ret = -EINVAL;
4911 break;
4912 }
4913
Jan Schmidt2f232032013-04-25 16:04:51 +00004914 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4915 if (err && !ret)
4916 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004917out:
4918 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004919 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004920drop_write:
4921 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004922 return ret;
4923}
4924
Miao Xie905b0dd2012-11-26 08:50:11 +00004925static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004926{
Al Viro496ad9a2013-01-23 17:07:38 -05004927 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004928 struct btrfs_ioctl_qgroup_assign_args *sa;
4929 struct btrfs_trans_handle *trans;
4930 int ret;
4931 int err;
4932
4933 if (!capable(CAP_SYS_ADMIN))
4934 return -EPERM;
4935
Miao Xie905b0dd2012-11-26 08:50:11 +00004936 ret = mnt_want_write_file(file);
4937 if (ret)
4938 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004939
4940 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004941 if (IS_ERR(sa)) {
4942 ret = PTR_ERR(sa);
4943 goto drop_write;
4944 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004945
4946 trans = btrfs_join_transaction(root);
4947 if (IS_ERR(trans)) {
4948 ret = PTR_ERR(trans);
4949 goto out;
4950 }
4951
4952 /* FIXME: check if the IDs really exist */
4953 if (sa->assign) {
4954 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4955 sa->src, sa->dst);
4956 } else {
4957 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4958 sa->src, sa->dst);
4959 }
4960
Qu Wenruoe082f562015-02-27 16:24:28 +08004961 /* update qgroup status and info */
4962 err = btrfs_run_qgroups(trans, root->fs_info);
4963 if (err < 0)
Anand Jainad8403d2016-03-10 12:22:15 +08004964 btrfs_handle_fs_error(root->fs_info, err,
4965 "failed to update qgroup status and info");
Arne Jansen5d13a372011-09-14 15:53:51 +02004966 err = btrfs_end_transaction(trans, root);
4967 if (err && !ret)
4968 ret = err;
4969
4970out:
4971 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004972drop_write:
4973 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004974 return ret;
4975}
4976
Miao Xie905b0dd2012-11-26 08:50:11 +00004977static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004978{
Al Viro496ad9a2013-01-23 17:07:38 -05004979 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004980 struct btrfs_ioctl_qgroup_create_args *sa;
4981 struct btrfs_trans_handle *trans;
4982 int ret;
4983 int err;
4984
4985 if (!capable(CAP_SYS_ADMIN))
4986 return -EPERM;
4987
Miao Xie905b0dd2012-11-26 08:50:11 +00004988 ret = mnt_want_write_file(file);
4989 if (ret)
4990 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004991
4992 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004993 if (IS_ERR(sa)) {
4994 ret = PTR_ERR(sa);
4995 goto drop_write;
4996 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004997
Miao Xied86e56c2012-11-15 11:35:41 +00004998 if (!sa->qgroupid) {
4999 ret = -EINVAL;
5000 goto out;
5001 }
5002
Arne Jansen5d13a372011-09-14 15:53:51 +02005003 trans = btrfs_join_transaction(root);
5004 if (IS_ERR(trans)) {
5005 ret = PTR_ERR(trans);
5006 goto out;
5007 }
5008
5009 /* FIXME: check if the IDs really exist */
5010 if (sa->create) {
Dongsheng Yang4087cf22015-01-18 10:59:23 -05005011 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02005012 } else {
5013 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
5014 }
5015
5016 err = btrfs_end_transaction(trans, root);
5017 if (err && !ret)
5018 ret = err;
5019
5020out:
5021 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005022drop_write:
5023 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005024 return ret;
5025}
5026
Miao Xie905b0dd2012-11-26 08:50:11 +00005027static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005028{
Al Viro496ad9a2013-01-23 17:07:38 -05005029 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005030 struct btrfs_ioctl_qgroup_limit_args *sa;
5031 struct btrfs_trans_handle *trans;
5032 int ret;
5033 int err;
5034 u64 qgroupid;
5035
5036 if (!capable(CAP_SYS_ADMIN))
5037 return -EPERM;
5038
Miao Xie905b0dd2012-11-26 08:50:11 +00005039 ret = mnt_want_write_file(file);
5040 if (ret)
5041 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005042
5043 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005044 if (IS_ERR(sa)) {
5045 ret = PTR_ERR(sa);
5046 goto drop_write;
5047 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005048
5049 trans = btrfs_join_transaction(root);
5050 if (IS_ERR(trans)) {
5051 ret = PTR_ERR(trans);
5052 goto out;
5053 }
5054
5055 qgroupid = sa->qgroupid;
5056 if (!qgroupid) {
5057 /* take the current subvol as qgroup */
5058 qgroupid = root->root_key.objectid;
5059 }
5060
5061 /* FIXME: check if the IDs really exist */
5062 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
5063
5064 err = btrfs_end_transaction(trans, root);
5065 if (err && !ret)
5066 ret = err;
5067
5068out:
5069 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005070drop_write:
5071 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005072 return ret;
5073}
5074
Jan Schmidt2f232032013-04-25 16:04:51 +00005075static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5076{
Al Viro6d0379e2013-06-16 19:32:35 +04005077 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00005078 struct btrfs_ioctl_quota_rescan_args *qsa;
5079 int ret;
5080
5081 if (!capable(CAP_SYS_ADMIN))
5082 return -EPERM;
5083
5084 ret = mnt_want_write_file(file);
5085 if (ret)
5086 return ret;
5087
5088 qsa = memdup_user(arg, sizeof(*qsa));
5089 if (IS_ERR(qsa)) {
5090 ret = PTR_ERR(qsa);
5091 goto drop_write;
5092 }
5093
5094 if (qsa->flags) {
5095 ret = -EINVAL;
5096 goto out;
5097 }
5098
5099 ret = btrfs_qgroup_rescan(root->fs_info);
5100
5101out:
5102 kfree(qsa);
5103drop_write:
5104 mnt_drop_write_file(file);
5105 return ret;
5106}
5107
5108static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5109{
Al Viro6d0379e2013-06-16 19:32:35 +04005110 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00005111 struct btrfs_ioctl_quota_rescan_args *qsa;
5112 int ret = 0;
5113
5114 if (!capable(CAP_SYS_ADMIN))
5115 return -EPERM;
5116
David Sterba8d2db782015-11-04 15:38:29 +01005117 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005118 if (!qsa)
5119 return -ENOMEM;
5120
5121 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5122 qsa->flags = 1;
5123 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
5124 }
5125
5126 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5127 ret = -EFAULT;
5128
5129 kfree(qsa);
5130 return ret;
5131}
5132
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005133static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5134{
Al Viro54563d42013-09-01 15:57:51 -04005135 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005136
5137 if (!capable(CAP_SYS_ADMIN))
5138 return -EPERM;
5139
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04005140 return btrfs_qgroup_wait_for_completion(root->fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005141}
5142
Hugo Millsabccd002014-01-30 20:17:00 +00005143static long _btrfs_ioctl_set_received_subvol(struct file *file,
5144 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005145{
Al Viro496ad9a2013-01-23 17:07:38 -05005146 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02005147 struct btrfs_root *root = BTRFS_I(inode)->root;
5148 struct btrfs_root_item *root_item = &root->root_item;
5149 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005150 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005151 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005152 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005153
David Sterbabd60ea02014-01-16 15:50:22 +01005154 if (!inode_owner_or_capable(inode))
5155 return -EPERM;
5156
Alexander Block8ea05e32012-07-25 17:35:53 +02005157 ret = mnt_want_write_file(file);
5158 if (ret < 0)
5159 return ret;
5160
5161 down_write(&root->fs_info->subvol_sem);
5162
5163 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
5164 ret = -EINVAL;
5165 goto out;
5166 }
5167
5168 if (btrfs_root_readonly(root)) {
5169 ret = -EROFS;
5170 goto out;
5171 }
5172
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005173 /*
5174 * 1 - root item
5175 * 2 - uuid items (received uuid + subvol uuid)
5176 */
5177 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005178 if (IS_ERR(trans)) {
5179 ret = PTR_ERR(trans);
5180 trans = NULL;
5181 goto out;
5182 }
5183
5184 sa->rtransid = trans->transid;
5185 sa->rtime.sec = ct.tv_sec;
5186 sa->rtime.nsec = ct.tv_nsec;
5187
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005188 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5189 BTRFS_UUID_SIZE);
5190 if (received_uuid_changed &&
5191 !btrfs_is_empty_uuid(root_item->received_uuid))
5192 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
5193 root_item->received_uuid,
5194 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5195 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005196 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5197 btrfs_set_root_stransid(root_item, sa->stransid);
5198 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005199 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5200 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5201 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5202 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005203
5204 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5205 &root->root_key, &root->root_item);
5206 if (ret < 0) {
5207 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02005208 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005209 }
5210 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5211 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
5212 sa->uuid,
5213 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5214 root->root_key.objectid);
5215 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005216 btrfs_abort_transaction(trans, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005217 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005218 }
5219 }
5220 ret = btrfs_commit_transaction(trans, root);
5221 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005222 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005223 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005224 }
5225
Hugo Millsabccd002014-01-30 20:17:00 +00005226out:
5227 up_write(&root->fs_info->subvol_sem);
5228 mnt_drop_write_file(file);
5229 return ret;
5230}
5231
5232#ifdef CONFIG_64BIT
5233static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5234 void __user *arg)
5235{
5236 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5237 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5238 int ret = 0;
5239
5240 args32 = memdup_user(arg, sizeof(*args32));
5241 if (IS_ERR(args32)) {
5242 ret = PTR_ERR(args32);
5243 args32 = NULL;
5244 goto out;
5245 }
5246
David Sterba8d2db782015-11-04 15:38:29 +01005247 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005248 if (!args64) {
5249 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005250 goto out;
5251 }
5252
5253 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5254 args64->stransid = args32->stransid;
5255 args64->rtransid = args32->rtransid;
5256 args64->stime.sec = args32->stime.sec;
5257 args64->stime.nsec = args32->stime.nsec;
5258 args64->rtime.sec = args32->rtime.sec;
5259 args64->rtime.nsec = args32->rtime.nsec;
5260 args64->flags = args32->flags;
5261
5262 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5263 if (ret)
5264 goto out;
5265
5266 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5267 args32->stransid = args64->stransid;
5268 args32->rtransid = args64->rtransid;
5269 args32->stime.sec = args64->stime.sec;
5270 args32->stime.nsec = args64->stime.nsec;
5271 args32->rtime.sec = args64->rtime.sec;
5272 args32->rtime.nsec = args64->rtime.nsec;
5273 args32->flags = args64->flags;
5274
5275 ret = copy_to_user(arg, args32, sizeof(*args32));
5276 if (ret)
5277 ret = -EFAULT;
5278
5279out:
5280 kfree(args32);
5281 kfree(args64);
5282 return ret;
5283}
5284#endif
5285
5286static long btrfs_ioctl_set_received_subvol(struct file *file,
5287 void __user *arg)
5288{
5289 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5290 int ret = 0;
5291
5292 sa = memdup_user(arg, sizeof(*sa));
5293 if (IS_ERR(sa)) {
5294 ret = PTR_ERR(sa);
5295 sa = NULL;
5296 goto out;
5297 }
5298
5299 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5300
5301 if (ret)
5302 goto out;
5303
Alexander Block8ea05e32012-07-25 17:35:53 +02005304 ret = copy_to_user(arg, sa, sizeof(*sa));
5305 if (ret)
5306 ret = -EFAULT;
5307
5308out:
5309 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005310 return ret;
5311}
5312
jeff.liu867ab662013-01-05 02:48:01 +00005313static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5314{
Al Viro6d0379e2013-06-16 19:32:35 +04005315 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005316 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005317 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005318 char label[BTRFS_LABEL_SIZE];
5319
5320 spin_lock(&root->fs_info->super_lock);
5321 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5322 spin_unlock(&root->fs_info->super_lock);
5323
5324 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005325
5326 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005327 btrfs_warn(root->fs_info,
5328 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00005329 }
5330
jeff.liu867ab662013-01-05 02:48:01 +00005331 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005332
5333 return ret ? -EFAULT : 0;
5334}
5335
jeff.liua8bfd4a2013-01-05 02:48:08 +00005336static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5337{
Al Viro6d0379e2013-06-16 19:32:35 +04005338 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005339 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5340 struct btrfs_trans_handle *trans;
5341 char label[BTRFS_LABEL_SIZE];
5342 int ret;
5343
5344 if (!capable(CAP_SYS_ADMIN))
5345 return -EPERM;
5346
5347 if (copy_from_user(label, arg, sizeof(label)))
5348 return -EFAULT;
5349
5350 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005351 btrfs_err(root->fs_info,
5352 "unable to set label with more than %d bytes",
5353 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005354 return -EINVAL;
5355 }
5356
5357 ret = mnt_want_write_file(file);
5358 if (ret)
5359 return ret;
5360
jeff.liua8bfd4a2013-01-05 02:48:08 +00005361 trans = btrfs_start_transaction(root, 0);
5362 if (IS_ERR(trans)) {
5363 ret = PTR_ERR(trans);
5364 goto out_unlock;
5365 }
5366
Anand Jaina1b83ac2013-07-19 17:39:32 +08005367 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005368 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005369 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005370 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005371
5372out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005373 mnt_drop_write_file(file);
5374 return ret;
5375}
5376
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005377#define INIT_FEATURE_FLAGS(suffix) \
5378 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5379 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5380 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5381
David Sterbad5131b62016-02-17 15:26:27 +01005382int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005383{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005384 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005385 INIT_FEATURE_FLAGS(SUPP),
5386 INIT_FEATURE_FLAGS(SAFE_SET),
5387 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5388 };
5389
5390 if (copy_to_user(arg, &features, sizeof(features)))
5391 return -EFAULT;
5392
5393 return 0;
5394}
5395
5396static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5397{
5398 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5399 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5400 struct btrfs_ioctl_feature_flags features;
5401
5402 features.compat_flags = btrfs_super_compat_flags(super_block);
5403 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5404 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5405
5406 if (copy_to_user(arg, &features, sizeof(features)))
5407 return -EFAULT;
5408
5409 return 0;
5410}
5411
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005412static int check_feature_bits(struct btrfs_root *root,
5413 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005414 u64 change_mask, u64 flags, u64 supported_flags,
5415 u64 safe_set, u64 safe_clear)
5416{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005417 const char *type = btrfs_feature_set_names[set];
5418 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005419 u64 disallowed, unsupported;
5420 u64 set_mask = flags & change_mask;
5421 u64 clear_mask = ~flags & change_mask;
5422
5423 unsupported = set_mask & ~supported_flags;
5424 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005425 names = btrfs_printable_features(set, unsupported);
5426 if (names) {
5427 btrfs_warn(root->fs_info,
5428 "this kernel does not support the %s feature bit%s",
5429 names, strchr(names, ',') ? "s" : "");
5430 kfree(names);
5431 } else
5432 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005433 "this kernel does not support %s bits 0x%llx",
5434 type, unsupported);
5435 return -EOPNOTSUPP;
5436 }
5437
5438 disallowed = set_mask & ~safe_set;
5439 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005440 names = btrfs_printable_features(set, disallowed);
5441 if (names) {
5442 btrfs_warn(root->fs_info,
5443 "can't set the %s feature bit%s while mounted",
5444 names, strchr(names, ',') ? "s" : "");
5445 kfree(names);
5446 } else
5447 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005448 "can't set %s bits 0x%llx while mounted",
5449 type, disallowed);
5450 return -EPERM;
5451 }
5452
5453 disallowed = clear_mask & ~safe_clear;
5454 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005455 names = btrfs_printable_features(set, disallowed);
5456 if (names) {
5457 btrfs_warn(root->fs_info,
5458 "can't clear the %s feature bit%s while mounted",
5459 names, strchr(names, ',') ? "s" : "");
5460 kfree(names);
5461 } else
5462 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005463 "can't clear %s bits 0x%llx while mounted",
5464 type, disallowed);
5465 return -EPERM;
5466 }
5467
5468 return 0;
5469}
5470
5471#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005472check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005473 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5474 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5475 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5476
5477static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5478{
5479 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5480 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5481 struct btrfs_ioctl_feature_flags flags[2];
5482 struct btrfs_trans_handle *trans;
5483 u64 newflags;
5484 int ret;
5485
5486 if (!capable(CAP_SYS_ADMIN))
5487 return -EPERM;
5488
5489 if (copy_from_user(flags, arg, sizeof(flags)))
5490 return -EFAULT;
5491
5492 /* Nothing to do */
5493 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5494 !flags[0].incompat_flags)
5495 return 0;
5496
5497 ret = check_feature(root, flags[0].compat_flags,
5498 flags[1].compat_flags, COMPAT);
5499 if (ret)
5500 return ret;
5501
5502 ret = check_feature(root, flags[0].compat_ro_flags,
5503 flags[1].compat_ro_flags, COMPAT_RO);
5504 if (ret)
5505 return ret;
5506
5507 ret = check_feature(root, flags[0].incompat_flags,
5508 flags[1].incompat_flags, INCOMPAT);
5509 if (ret)
5510 return ret;
5511
David Sterba7ab19622016-05-04 11:32:00 +02005512 ret = mnt_want_write_file(file);
5513 if (ret)
5514 return ret;
5515
David Sterba8051aa12014-02-07 14:34:04 +01005516 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005517 if (IS_ERR(trans)) {
5518 ret = PTR_ERR(trans);
5519 goto out_drop_write;
5520 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005521
5522 spin_lock(&root->fs_info->super_lock);
5523 newflags = btrfs_super_compat_flags(super_block);
5524 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5525 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5526 btrfs_set_super_compat_flags(super_block, newflags);
5527
5528 newflags = btrfs_super_compat_ro_flags(super_block);
5529 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5530 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5531 btrfs_set_super_compat_ro_flags(super_block, newflags);
5532
5533 newflags = btrfs_super_incompat_flags(super_block);
5534 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5535 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5536 btrfs_set_super_incompat_flags(super_block, newflags);
5537 spin_unlock(&root->fs_info->super_lock);
5538
David Sterba7ab19622016-05-04 11:32:00 +02005539 ret = btrfs_commit_transaction(trans, root);
5540out_drop_write:
5541 mnt_drop_write_file(file);
5542
5543 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005544}
5545
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005546long btrfs_ioctl(struct file *file, unsigned int
5547 cmd, unsigned long arg)
5548{
Al Viro496ad9a2013-01-23 17:07:38 -05005549 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005550 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005551
5552 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005553 case FS_IOC_GETFLAGS:
5554 return btrfs_ioctl_getflags(file, argp);
5555 case FS_IOC_SETFLAGS:
5556 return btrfs_ioctl_setflags(file, argp);
5557 case FS_IOC_GETVERSION:
5558 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005559 case FITRIM:
5560 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005561 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005562 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005563 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005564 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005565 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005566 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005567 case BTRFS_IOC_SUBVOL_CREATE_V2:
5568 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005569 case BTRFS_IOC_SNAP_DESTROY:
5570 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005571 case BTRFS_IOC_SUBVOL_GETFLAGS:
5572 return btrfs_ioctl_subvol_getflags(file, argp);
5573 case BTRFS_IOC_SUBVOL_SETFLAGS:
5574 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005575 case BTRFS_IOC_DEFAULT_SUBVOL:
5576 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005577 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005578 return btrfs_ioctl_defrag(file, NULL);
5579 case BTRFS_IOC_DEFRAG_RANGE:
5580 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005581 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005582 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005583 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005584 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005585 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005586 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005587 case BTRFS_IOC_RM_DEV_V2:
5588 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005589 case BTRFS_IOC_FS_INFO:
5590 return btrfs_ioctl_fs_info(root, argp);
5591 case BTRFS_IOC_DEV_INFO:
5592 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005593 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005594 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005595 case BTRFS_IOC_TRANS_START:
5596 return btrfs_ioctl_trans_start(file);
5597 case BTRFS_IOC_TRANS_END:
5598 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005599 case BTRFS_IOC_TREE_SEARCH:
5600 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005601 case BTRFS_IOC_TREE_SEARCH_V2:
5602 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005603 case BTRFS_IOC_INO_LOOKUP:
5604 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005605 case BTRFS_IOC_INO_PATHS:
5606 return btrfs_ioctl_ino_to_path(root, argp);
5607 case BTRFS_IOC_LOGICAL_INO:
5608 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005609 case BTRFS_IOC_SPACE_INFO:
5610 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005611 case BTRFS_IOC_SYNC: {
5612 int ret;
5613
Miao Xie6c255e62014-03-06 13:55:01 +08005614 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005615 if (ret)
5616 return ret;
Al Viroddb52f42014-10-21 20:21:18 -04005617 ret = btrfs_sync_fs(file_inode(file)->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005618 /*
5619 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005620 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005621 * processing uncleaned subvols.
5622 */
5623 wake_up_process(root->fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005624 return ret;
5625 }
Sage Weil46204592010-10-29 15:41:32 -04005626 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005627 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005628 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005629 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005630 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005631 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005632 case BTRFS_IOC_SCRUB_CANCEL:
5633 return btrfs_ioctl_scrub_cancel(root, argp);
5634 case BTRFS_IOC_SCRUB_PROGRESS:
5635 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005636 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005637 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005638 case BTRFS_IOC_BALANCE_CTL:
5639 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005640 case BTRFS_IOC_BALANCE_PROGRESS:
5641 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005642 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5643 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005644#ifdef CONFIG_64BIT
5645 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5646 return btrfs_ioctl_set_received_subvol_32(file, argp);
5647#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005648 case BTRFS_IOC_SEND:
5649 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005650 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06005651 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005652 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005653 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005654 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005655 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005656 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005657 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005658 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005659 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005660 case BTRFS_IOC_QUOTA_RESCAN:
5661 return btrfs_ioctl_quota_rescan(file, argp);
5662 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5663 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005664 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5665 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005666 case BTRFS_IOC_DEV_REPLACE:
5667 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005668 case BTRFS_IOC_GET_FSLABEL:
5669 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005670 case BTRFS_IOC_SET_FSLABEL:
5671 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005672 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005673 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005674 case BTRFS_IOC_GET_FEATURES:
5675 return btrfs_ioctl_get_features(file, argp);
5676 case BTRFS_IOC_SET_FEATURES:
5677 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005678 }
5679
5680 return -ENOTTY;
5681}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005682
5683#ifdef CONFIG_COMPAT
5684long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5685{
Jeff Mahoney93fb0432017-02-06 19:39:09 -05005686 /*
5687 * These all access 32-bit values anyway so no further
5688 * handling is necessary.
5689 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005690 switch (cmd) {
5691 case FS_IOC32_GETFLAGS:
5692 cmd = FS_IOC_GETFLAGS;
5693 break;
5694 case FS_IOC32_SETFLAGS:
5695 cmd = FS_IOC_SETFLAGS;
5696 break;
5697 case FS_IOC32_GETVERSION:
5698 cmd = FS_IOC_GETVERSION;
5699 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005700 }
5701
5702 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5703}
5704#endif