blob: 242584a0d3b5695074fa5ad2f853d0283becb49a [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
Xiao Guangrong1f781602011-04-20 10:09:16 +0000388 rcu_read_lock();
389 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
390 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000391 if (!device->bdev)
392 continue;
393 q = bdev_get_queue(device->bdev);
394 if (blk_queue_discard(q)) {
395 num_devices++;
396 minlen = min((u64)q->limits.discard_granularity,
397 minlen);
398 }
399 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000400 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200401
Li Dongyangf7039b12011-03-24 10:24:28 +0000402 if (!num_devices)
403 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000404 if (copy_from_user(&range, arg, sizeof(range)))
405 return -EFAULT;
Qu Wenruo4423e072018-11-20 10:26:37 +0800406
407 /*
408 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
409 * block group is in the logical address space, which can be any
410 * sectorsize aligned bytenr in the range [0, U64_MAX].
411 */
412 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200413 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000414
415 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500416 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000417 if (ret < 0)
418 return ret;
419
420 if (copy_to_user(arg, &range, sizeof(range)))
421 return -EFAULT;
422
423 return 0;
424}
425
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200426int btrfs_is_empty_uuid(u8 *uuid)
427{
Chris Mason46e0f662013-11-15 12:14:55 +0100428 int i;
429
430 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
431 if (uuid[i])
432 return 0;
433 }
434 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200435}
436
Miao Xied5c12072013-02-28 10:04:33 +0000437static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400438 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400439 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200440 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000441 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442{
443 struct btrfs_trans_handle *trans;
444 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100445 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400446 struct btrfs_inode_item *inode_item;
447 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000448 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400449 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000450 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700451 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900452 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400453 int ret;
454 int err;
455 u64 objectid;
456 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500457 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000458 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200459 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400460
David Sterba49a3c4d2016-03-24 17:49:22 +0100461 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
462 if (!root_item)
463 return -ENOMEM;
464
Li Zefan581bb052011-04-20 10:06:11 +0800465 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400466 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100467 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000468
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800469 /*
470 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400471 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800472 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100473 if (btrfs_qgroup_level(objectid)) {
474 ret = -ENOSPC;
475 goto fail_free;
476 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800477
Miao Xied5c12072013-02-28 10:04:33 +0000478 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400479 /*
Miao Xied5c12072013-02-28 10:04:33 +0000480 * The same as the snapshot creation, please see the comment
481 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400482 */
Miao Xied5c12072013-02-28 10:04:33 +0000483 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200484 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000485 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100486 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400487
Miao Xied5c12072013-02-28 10:04:33 +0000488 trans = btrfs_start_transaction(root, 0);
489 if (IS_ERR(trans)) {
490 ret = PTR_ERR(trans);
Liu Bode6e8202014-01-09 14:57:06 +0800491 btrfs_subvolume_release_metadata(root, &block_rsv,
492 qgroup_reserved);
David Sterba49a3c4d2016-03-24 17:49:22 +0100493 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000494 }
495 trans->block_rsv = &block_rsv;
496 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400497
Miao Xie8696c532013-02-07 06:02:44 +0000498 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200499 if (ret)
500 goto fail;
501
David Sterba4d75f8a2014-06-15 01:54:12 +0200502 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400503 if (IS_ERR(leaf)) {
504 ret = PTR_ERR(leaf);
505 goto fail;
506 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400507
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400508 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400509 btrfs_set_header_bytenr(leaf, leaf->start);
510 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400511 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400512 btrfs_set_header_owner(leaf, objectid);
513
Ross Kirk0a4e5582013-09-24 10:12:38 +0100514 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200517 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400518 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519 btrfs_mark_buffer_dirty(leaf);
520
David Sterba49a3c4d2016-03-24 17:49:22 +0100521 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800522 btrfs_set_stack_inode_generation(inode_item, 1);
523 btrfs_set_stack_inode_size(inode_item, 3);
524 btrfs_set_stack_inode_nlink(inode_item, 1);
David Sterba707e8a02014-06-04 19:22:26 +0200525 btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800526 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400527
David Sterba49a3c4d2016-03-24 17:49:22 +0100528 btrfs_set_root_flags(root_item, 0);
529 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800530 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000531
David Sterba49a3c4d2016-03-24 17:49:22 +0100532 btrfs_set_root_bytenr(root_item, leaf->start);
533 btrfs_set_root_generation(root_item, trans->transid);
534 btrfs_set_root_level(root_item, 0);
535 btrfs_set_root_refs(root_item, 1);
536 btrfs_set_root_used(root_item, leaf->len);
537 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400538
David Sterba49a3c4d2016-03-24 17:49:22 +0100539 btrfs_set_root_generation_v2(root_item,
540 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200541 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100542 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
543 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
544 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
545 root_item->ctime = root_item->otime;
546 btrfs_set_root_ctransid(root_item, trans->transid);
547 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400548
Chris Mason925baed2008-06-25 16:01:30 -0400549 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400550 free_extent_buffer(leaf);
551 leaf = NULL;
552
David Sterba49a3c4d2016-03-24 17:49:22 +0100553 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400554
555 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400556 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200557 key.type = BTRFS_ROOT_ITEM_KEY;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100559 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 if (ret)
561 goto fail;
562
Yan, Zheng76dda932009-09-21 16:00:26 -0400563 key.offset = (u64)-1;
564 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100565 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100566 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400567 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568 goto fail;
569 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400570
571 btrfs_record_root_in_trans(trans, new_root);
572
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000573 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700574 if (ret) {
575 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400576 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700577 goto fail;
578 }
579
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530580 mutex_lock(&new_root->objectid_mutex);
581 new_root->highest_objectid = new_dirid;
582 mutex_unlock(&new_root->objectid_mutex);
583
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400584 /*
585 * insert the directory item
586 */
Chris Mason3de45862008-11-17 21:02:50 -0500587 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100588 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400589 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590 goto fail;
591 }
Chris Mason3de45862008-11-17 21:02:50 -0500592
593 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800594 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500595 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100596 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400597 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400598 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100599 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500600
Yan Zheng52c26172009-01-05 15:43:43 -0500601 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
602 ret = btrfs_update_inode(trans, root, dir);
603 BUG_ON(ret);
604
Chris Mason0660b5a2008-11-17 20:37:39 -0500605 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400606 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800607 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500608 BUG_ON(ret);
609
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200610 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
David Sterba49a3c4d2016-03-24 17:49:22 +0100611 root_item->uuid, BTRFS_UUID_KEY_SUBVOL,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200612 objectid);
613 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400614 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200615
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400616fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100617 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000618 trans->block_rsv = NULL;
619 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800620 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
621
Sage Weil72fd0322010-10-29 15:41:32 -0400622 if (async_transid) {
623 *async_transid = trans->transid;
624 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000625 if (err)
626 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400627 } else {
628 err = btrfs_commit_transaction(trans, root);
629 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630 if (err && !ret)
631 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500632
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900633 if (!ret) {
634 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800635 if (IS_ERR(inode))
636 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900637 d_instantiate(dentry, inode);
638 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400639 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100640
641fail_free:
642 kfree(root_item);
643 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400644}
645
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000646static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +0800647{
648 s64 writers;
649 DEFINE_WAIT(wait);
650
651 do {
652 prepare_to_wait(&root->subv_writers->wait, &wait,
653 TASK_UNINTERRUPTIBLE);
654
655 writers = percpu_counter_sum(&root->subv_writers->counter);
656 if (writers)
657 schedule();
658
659 finish_wait(&root->subv_writers->wait, &wait);
660 } while (writers);
661}
662
Miao Xiee9662f72013-02-28 10:01:15 +0000663static int create_snapshot(struct btrfs_root *root, struct inode *dir,
664 struct dentry *dentry, char *name, int namelen,
665 u64 *async_transid, bool readonly,
666 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400667{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000668 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400669 struct btrfs_pending_snapshot *pending_snapshot;
670 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000671 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400672
Miao Xie27cdeb72014-04-02 19:51:05 +0800673 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400674 return -EINVAL;
675
David Sterbaa1ee7362015-11-10 18:53:56 +0100676 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
677 if (!pending_snapshot)
678 return -ENOMEM;
679
David Sterbab0c0ea62015-11-10 18:54:00 +0100680 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
681 GFP_NOFS);
David Sterba8546b572015-11-10 18:54:03 +0100682 pending_snapshot->path = btrfs_alloc_path();
683 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100684 ret = -ENOMEM;
685 goto free_pending;
686 }
687
Miao Xie8257b2d2014-03-06 13:38:19 +0800688 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100689 smp_mb__after_atomic();
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000690 btrfs_wait_for_no_snapshoting_writes(root);
Miao Xie8257b2d2014-03-06 13:38:19 +0800691
Miao Xie6a038432013-05-15 07:48:24 +0000692 ret = btrfs_start_delalloc_inodes(root, 0);
693 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100694 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000695
Filipe Manana578def72016-04-26 15:36:38 +0100696 btrfs_wait_ordered_extents(root, -1, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000697
Miao Xie66d8f3d2012-09-06 04:02:28 -0600698 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
699 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000700 /*
701 * 1 - parent dir inode
702 * 2 - dir entries
703 * 1 - root item
704 * 2 - root ref/backref
705 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200706 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000707 */
708 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200709 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400710 &pending_snapshot->qgroup_reserved,
711 false);
Miao Xied5c12072013-02-28 10:04:33 +0000712 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100713 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000714
Yan, Zhenga22285a2010-05-16 10:48:46 -0400715 pending_snapshot->dentry = dentry;
716 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800717 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000718 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000719 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400720
Miao Xied5c12072013-02-28 10:04:33 +0000721 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400722 if (IS_ERR(trans)) {
723 ret = PTR_ERR(trans);
724 goto fail;
725 }
726
Josef Bacik83515832011-06-14 15:16:14 -0400727 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400728 list_add(&pending_snapshot->list,
729 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400730 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400731 if (async_transid) {
732 *async_transid = trans->transid;
733 ret = btrfs_commit_transaction_async(trans,
734 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000735 if (ret)
736 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400737 } else {
738 ret = btrfs_commit_transaction(trans,
739 root->fs_info->extent_root);
740 }
Miao Xieaec80302013-03-04 09:44:29 +0000741 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400742 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400743
744 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400745 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000746 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400747
Chris Masond3797302014-10-15 13:50:56 -0700748 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
749 if (ret)
750 goto fail;
751
David Howells2b0143b2015-03-17 22:25:59 +0000752 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000753 if (IS_ERR(inode)) {
754 ret = PTR_ERR(inode);
755 goto fail;
756 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900757
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000758 d_instantiate(dentry, inode);
759 ret = 0;
760fail:
Miao Xied5c12072013-02-28 10:04:33 +0000761 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
762 &pending_snapshot->block_rsv,
763 pending_snapshot->qgroup_reserved);
David Sterbaa1ee7362015-11-10 18:53:56 +0100764dec_and_free:
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000765 if (atomic_dec_and_test(&root->will_be_snapshoted))
766 wake_up_atomic_t(&root->will_be_snapshoted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100767free_pending:
768 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100769 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100770 kfree(pending_snapshot);
771
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400772 return ret;
773}
774
Sage Weil4260f7c2010-10-29 15:46:43 -0400775/* copy of may_delete in fs/namei.c()
776 * Check whether we can remove a link victim from directory dir, check
777 * whether the type of victim is right.
778 * 1. We can't do it if dir is read-only (done in permission())
779 * 2. We should have write and exec permissions on dir
780 * 3. We can't remove anything from append-only dir
781 * 4. We can't do anything with immutable dir (done in permission())
782 * 5. If the sticky bit on dir is set we should either
783 * a. be owner of dir, or
784 * b. be owner of victim, or
785 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400786 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400787 * links pointing to it.
788 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
789 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
790 * 9. We can't remove a root or mountpoint.
791 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
792 * nfs_async_unlink().
793 */
794
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530795static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400796{
797 int error;
798
David Howells2b0143b2015-03-17 22:25:59 +0000799 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400800 return -ENOENT;
801
David Howells2b0143b2015-03-17 22:25:59 +0000802 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400803 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400804
805 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
806 if (error)
807 return error;
808 if (IS_APPEND(dir))
809 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000810 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
811 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400812 return -EPERM;
813 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000814 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400815 return -ENOTDIR;
816 if (IS_ROOT(victim))
817 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000818 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400819 return -EISDIR;
820 if (IS_DEADDIR(dir))
821 return -ENOENT;
822 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
823 return -EBUSY;
824 return 0;
825}
826
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400827/* copy of may_create in fs/namei.c() */
828static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
829{
David Howells2b0143b2015-03-17 22:25:59 +0000830 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400831 return -EEXIST;
832 if (IS_DEADDIR(dir))
833 return -ENOENT;
834 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
835}
836
837/*
838 * Create a new subvolume below @parent. This is largely modeled after
839 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
840 * inside this filesystem so it's quite a bit simpler.
841 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400842static noinline int btrfs_mksubvol(struct path *parent,
843 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400844 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200845 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000846 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400847{
David Howells2b0143b2015-03-17 22:25:59 +0000848 struct inode *dir = d_inode(parent->dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400849 struct dentry *dentry;
850 int error;
851
Al Viro00235412016-05-26 00:05:12 -0400852 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
853 if (error == -EINTR)
854 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400855
856 dentry = lookup_one_len(name, parent->dentry, namelen);
857 error = PTR_ERR(dentry);
858 if (IS_ERR(dentry))
859 goto out_unlock;
860
Yan, Zheng76dda932009-09-21 16:00:26 -0400861 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400862 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600863 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400864
Chris Mason9c520572012-12-17 14:26:57 -0500865 /*
866 * even if this name doesn't exist, we may get hash collisions.
867 * check for them now when we can safely fail
868 */
869 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
870 dir->i_ino, name,
871 namelen);
872 if (error)
873 goto out_dput;
874
Yan, Zheng76dda932009-09-21 16:00:26 -0400875 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
876
877 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
878 goto out_up_read;
879
Chris Mason3de45862008-11-17 21:02:50 -0500880 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000881 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200882 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500883 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000884 error = create_subvol(dir, dentry, name, namelen,
885 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500886 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400887 if (!error)
888 fsnotify_mkdir(dir, dentry);
889out_up_read:
890 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400891out_dput:
892 dput(dentry);
893out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500894 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400895 return error;
896}
897
Chris Mason4cb53002011-05-24 15:35:30 -0400898/*
899 * When we're defragging a range, we don't want to kick it off again
900 * if it is really just waiting for delalloc to send it down.
901 * If we find a nice big extent or delalloc range for the bytes in the
902 * file you want to defrag, we return 0 to let you know to skip this
903 * part of the file
904 */
David Sterbaaab110a2014-07-29 17:32:10 +0200905static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400906{
907 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
908 struct extent_map *em = NULL;
909 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
910 u64 end;
911
912 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300913 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -0400914 read_unlock(&em_tree->lock);
915
916 if (em) {
917 end = extent_map_end(em);
918 free_extent_map(em);
919 if (end - offset > thresh)
920 return 0;
921 }
922 /* if we already have a nice delalloc here, just stop */
923 thresh /= 2;
924 end = count_range_bits(io_tree, &offset, offset + thresh,
925 thresh, EXTENT_DELALLOC, 1);
926 if (end >= thresh)
927 return 0;
928 return 1;
929}
930
931/*
932 * helper function to walk through a file and find extents
933 * newer than a specific transid, and smaller than thresh.
934 *
935 * This is used by the defragging code to find new and small
936 * extents
937 */
938static int find_new_extents(struct btrfs_root *root,
939 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200940 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400941{
942 struct btrfs_path *path;
943 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400944 struct extent_buffer *leaf;
945 struct btrfs_file_extent_item *extent;
946 int type;
947 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000948 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400949
950 path = btrfs_alloc_path();
951 if (!path)
952 return -ENOMEM;
953
David Sterbaa4689d22011-05-31 17:08:14 +0000954 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400955 min_key.type = BTRFS_EXTENT_DATA_KEY;
956 min_key.offset = *off;
957
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530958 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100959 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400960 if (ret != 0)
961 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000962process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000963 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400964 goto none;
965 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
966 goto none;
967
968 leaf = path->nodes[0];
969 extent = btrfs_item_ptr(leaf, path->slots[0],
970 struct btrfs_file_extent_item);
971
972 type = btrfs_file_extent_type(leaf, extent);
973 if (type == BTRFS_FILE_EXTENT_REG &&
974 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
975 check_defrag_in_cache(inode, min_key.offset, thresh)) {
976 *off = min_key.offset;
977 btrfs_free_path(path);
978 return 0;
979 }
980
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000981 path->slots[0]++;
982 if (path->slots[0] < btrfs_header_nritems(leaf)) {
983 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
984 goto process_slot;
985 }
986
Chris Mason4cb53002011-05-24 15:35:30 -0400987 if (min_key.offset == (u64)-1)
988 goto none;
989
990 min_key.offset++;
991 btrfs_release_path(path);
992 }
993none:
994 btrfs_free_path(path);
995 return -ENOENT;
996}
997
Li Zefan6c282eb2012-06-11 16:03:35 +0800998static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400999{
1000 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001001 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001002 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001003 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001004
1005 /*
1006 * hopefully we have this extent in the tree already, try without
1007 * the full extent lock
1008 */
1009 read_lock(&em_tree->lock);
1010 em = lookup_extent_mapping(em_tree, start, len);
1011 read_unlock(&em_tree->lock);
1012
1013 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001014 struct extent_state *cached = NULL;
1015 u64 end = start + len - 1;
1016
Chris Mason940100a2010-03-10 10:52:59 -05001017 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001018 lock_extent_bits(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001019 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +00001020 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -05001021
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001022 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001023 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001024 }
1025
Li Zefan6c282eb2012-06-11 16:03:35 +08001026 return em;
1027}
1028
1029static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1030{
1031 struct extent_map *next;
1032 bool ret = true;
1033
1034 /* this is the last extent */
1035 if (em->start + em->len >= i_size_read(inode))
1036 return false;
1037
1038 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001039 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1040 ret = false;
1041 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001042 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001043 ret = false;
1044
1045 free_extent_map(next);
1046 return ret;
1047}
1048
David Sterbaaab110a2014-07-29 17:32:10 +02001049static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001050 u64 *last_len, u64 *skip, u64 *defrag_end,
1051 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001052{
1053 struct extent_map *em;
1054 int ret = 1;
1055 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001056 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001057
1058 /*
1059 * make sure that once we start defragging an extent, we keep on
1060 * defragging it
1061 */
1062 if (start < *defrag_end)
1063 return 1;
1064
1065 *skip = 0;
1066
1067 em = defrag_lookup_extent(inode, start);
1068 if (!em)
1069 return 0;
1070
Chris Mason940100a2010-03-10 10:52:59 -05001071 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001072 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001073 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001074 goto out;
1075 }
1076
Liu Bo4a3560c2015-08-07 16:48:41 +08001077 if (!*defrag_end)
1078 prev_mergeable = false;
1079
Li Zefan6c282eb2012-06-11 16:03:35 +08001080 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001081 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001082 * we hit a real extent, if it is big or the next extent is not a
1083 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001084 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001085 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001086 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001087 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001088out:
Chris Mason940100a2010-03-10 10:52:59 -05001089 /*
1090 * last_len ends up being a counter of how many bytes we've defragged.
1091 * every time we choose not to defrag an extent, we reset *last_len
1092 * so that the next tiny extent will force a defrag.
1093 *
1094 * The end result of this is that tiny extents before a single big
1095 * extent will force at least part of that big extent to be defragged.
1096 */
1097 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001098 *defrag_end = extent_map_end(em);
1099 } else {
1100 *last_len = 0;
1101 *skip = extent_map_end(em);
1102 *defrag_end = 0;
1103 }
1104
1105 free_extent_map(em);
1106 return ret;
1107}
1108
Chris Mason4cb53002011-05-24 15:35:30 -04001109/*
1110 * it doesn't do much good to defrag one or two pages
1111 * at a time. This pulls in a nice chunk of pages
1112 * to COW and defrag.
1113 *
1114 * It also makes sure the delalloc code has enough
1115 * dirty data to avoid making new small extents as part
1116 * of the defrag
1117 *
1118 * It's a good idea to start RA on this range
1119 * before calling this.
1120 */
1121static int cluster_pages_for_defrag(struct inode *inode,
1122 struct page **pages,
1123 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001124 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001125{
Chris Mason4cb53002011-05-24 15:35:30 -04001126 unsigned long file_end;
1127 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001128 u64 page_start;
1129 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001130 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001131 int ret;
1132 int i;
1133 int i_done;
1134 struct btrfs_ordered_extent *ordered;
1135 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001136 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001137 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001138
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001139 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001140 if (!isize || start_index > file_end)
1141 return 0;
1142
1143 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001144
1145 ret = btrfs_delalloc_reserve_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001146 start_index << PAGE_SHIFT,
1147 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001148 if (ret)
1149 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001150 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001151 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001152
1153 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001154 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001155 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001156again:
Josef Bacika94733d2011-07-11 10:47:06 -04001157 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001158 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001159 if (!page)
1160 break;
1161
Miao Xie600a45e2012-02-16 15:01:24 +08001162 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001163 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001164 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001165 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001166 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001167 ordered = btrfs_lookup_ordered_extent(inode,
1168 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001169 unlock_extent_cached(tree, page_start, page_end,
1170 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001171 if (!ordered)
1172 break;
1173
1174 unlock_page(page);
1175 btrfs_start_ordered_extent(inode, ordered, 1);
1176 btrfs_put_ordered_extent(ordered);
1177 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001178 /*
1179 * we unlocked the page above, so we need check if
1180 * it was released or not.
1181 */
1182 if (page->mapping != inode->i_mapping) {
1183 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001184 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001185 goto again;
1186 }
Miao Xie600a45e2012-02-16 15:01:24 +08001187 }
1188
Chris Mason4cb53002011-05-24 15:35:30 -04001189 if (!PageUptodate(page)) {
1190 btrfs_readpage(NULL, page);
1191 lock_page(page);
1192 if (!PageUptodate(page)) {
1193 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001194 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001195 ret = -EIO;
1196 break;
1197 }
1198 }
Miao Xie600a45e2012-02-16 15:01:24 +08001199
Miao Xie600a45e2012-02-16 15:01:24 +08001200 if (page->mapping != inode->i_mapping) {
1201 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001202 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001203 goto again;
1204 }
1205
Chris Mason4cb53002011-05-24 15:35:30 -04001206 pages[i] = page;
1207 i_done++;
1208 }
1209 if (!i_done || ret)
1210 goto out;
1211
1212 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1213 goto out;
1214
1215 /*
1216 * so now we have a nice long stream of locked
1217 * and up to date pages, lets wait on them
1218 */
1219 for (i = 0; i < i_done; i++)
1220 wait_on_page_writeback(pages[i]);
1221
1222 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001223 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001224
1225 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001226 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001227 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1228 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001229 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1230 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001231
Liu Bo1f12bd02012-03-29 09:57:44 -04001232 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001233 spin_lock(&BTRFS_I(inode)->lock);
1234 BTRFS_I(inode)->outstanding_extents++;
1235 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001236 btrfs_delalloc_release_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001237 start_index << PAGE_SHIFT,
1238 (page_cnt - i_done) << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001239 }
1240
1241
Liu Bo9e8a4a82012-09-05 19:10:51 -06001242 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001243 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001244
1245 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1246 page_start, page_end - 1, &cached_state,
1247 GFP_NOFS);
1248
1249 for (i = 0; i < i_done; i++) {
1250 clear_page_dirty_for_io(pages[i]);
1251 ClearPageChecked(pages[i]);
1252 set_page_extent_mapped(pages[i]);
1253 set_page_dirty(pages[i]);
1254 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001255 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001256 }
1257 return i_done;
1258out:
1259 for (i = 0; i < i_done; i++) {
1260 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001261 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001262 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001263 btrfs_delalloc_release_space(inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001264 start_index << PAGE_SHIFT,
1265 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001266 return ret;
1267
1268}
1269
1270int btrfs_defrag_file(struct inode *inode, struct file *file,
1271 struct btrfs_ioctl_defrag_range_args *range,
1272 u64 newer_than, unsigned long max_to_defrag)
1273{
1274 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001275 struct file_ra_state *ra = NULL;
1276 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001277 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001278 u64 last_len = 0;
1279 u64 skip = 0;
1280 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001281 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001282 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001283 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001284 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001285 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001286 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001287 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001288 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001289 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001290 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001291 struct page **pages = NULL;
1292
Liu Bo0abd5b12013-04-16 09:20:28 +00001293 if (isize == 0)
1294 return 0;
1295
1296 if (range->start >= isize)
1297 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001298
1299 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1300 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1301 return -EINVAL;
1302 if (range->compress_type)
1303 compress_type = range->compress_type;
1304 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001305
Liu Bo0abd5b12013-04-16 09:20:28 +00001306 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001307 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001308
Chris Mason4cb53002011-05-24 15:35:30 -04001309 /*
1310 * if we were not given a file, allocate a readahead
1311 * context
1312 */
1313 if (!file) {
1314 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1315 if (!ra)
1316 return -ENOMEM;
1317 file_ra_state_init(ra, inode->i_mapping);
1318 } else {
1319 ra = &file->f_ra;
1320 }
1321
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301322 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001323 GFP_NOFS);
1324 if (!pages) {
1325 ret = -ENOMEM;
1326 goto out_ra;
1327 }
1328
1329 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001330 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001331 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001332 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001333 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001334 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001335 }
1336
Chris Mason4cb53002011-05-24 15:35:30 -04001337 if (newer_than) {
1338 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001339 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001340 if (!ret) {
1341 range->start = newer_off;
1342 /*
1343 * we always align our defrag to help keep
1344 * the extents in the file evenly spaced
1345 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001346 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001347 } else
1348 goto out_ra;
1349 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001350 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001351 }
1352 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301353 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001354
Li Zefan2a0f7f52011-10-10 15:43:34 -04001355 /*
1356 * make writeback starts from i, so the defrag range can be
1357 * written sequentially.
1358 */
1359 if (i < inode->i_mapping->writeback_index)
1360 inode->i_mapping->writeback_index = i;
1361
Chris Masonf7f43cc2011-10-11 11:41:40 -04001362 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001363 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001364 /*
1365 * make sure we stop running if someone unmounts
1366 * the FS
1367 */
1368 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1369 break;
1370
David Sterba210549e2013-02-09 23:38:06 +00001371 if (btrfs_defrag_cancelled(root->fs_info)) {
David Sterbaf14d1042015-10-08 11:37:06 +02001372 btrfs_debug(root->fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001373 ret = -EAGAIN;
1374 break;
1375 }
1376
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001377 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001378 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001379 &defrag_end, range->flags &
1380 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001381 unsigned long next;
1382 /*
1383 * the should_defrag function tells us how much to skip
1384 * bump our counter by the suggested amount
1385 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001386 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001387 i = max(i + 1, next);
1388 continue;
1389 }
Li Zefan008873e2011-09-02 15:57:07 +08001390
1391 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001392 cluster = (PAGE_ALIGN(defrag_end) >>
1393 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001394 cluster = min(cluster, max_cluster);
1395 } else {
1396 cluster = max_cluster;
1397 }
1398
Li Zefan008873e2011-09-02 15:57:07 +08001399 if (i + cluster > ra_index) {
1400 ra_index = max(i, ra_index);
1401 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1402 cluster);
chandane4826a52015-06-09 17:38:32 +05301403 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001404 }
Chris Mason940100a2010-03-10 10:52:59 -05001405
Al Viro59551022016-01-22 15:40:57 -05001406 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001407 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1408 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001409 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001410 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001411 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001412 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001413 }
Chris Mason940100a2010-03-10 10:52:59 -05001414
Chris Mason4cb53002011-05-24 15:35:30 -04001415 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001416 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001417 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001418
1419 if (newer_than) {
1420 if (newer_off == (u64)-1)
1421 break;
1422
Liu Boe1f041e2012-03-29 09:57:45 -04001423 if (ret > 0)
1424 i += ret;
1425
Chris Mason4cb53002011-05-24 15:35:30 -04001426 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001427 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001428
Byongho Leeee221842015-12-15 01:42:10 +09001429 ret = find_new_extents(root, inode, newer_than,
1430 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001431 if (!ret) {
1432 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001433 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001434 } else {
1435 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001436 }
Chris Mason4cb53002011-05-24 15:35:30 -04001437 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001438 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001439 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001440 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001441 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001442 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001443 last_len = 0;
1444 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001445 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001446 }
1447
Filipe Mananadec8ef92014-03-01 10:55:54 +00001448 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001449 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001450 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1451 &BTRFS_I(inode)->runtime_flags))
1452 filemap_flush(inode->i_mapping);
1453 }
Chris Mason1e701a32010-03-11 09:42:04 -05001454
1455 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1456 /* the filemap_flush will queue IO into the worker threads, but
1457 * we have to make sure the IO is actually started and that
1458 * ordered extents get created before we return
1459 */
1460 atomic_inc(&root->fs_info->async_submit_draining);
1461 while (atomic_read(&root->fs_info->nr_async_submits) ||
1462 atomic_read(&root->fs_info->async_delalloc_pages)) {
1463 wait_event(root->fs_info->async_submit_wait,
1464 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1465 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1466 }
1467 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001468 }
1469
Li Zefan1a419d82010-10-25 15:12:50 +08001470 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001471 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001472 }
1473
Diego Calleja60ccf822011-09-01 16:33:57 +02001474 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001475
Chris Mason4cb53002011-05-24 15:35:30 -04001476out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001477 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
Al Viro59551022016-01-22 15:40:57 -05001478 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001479 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001480 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001481 }
Chris Mason4cb53002011-05-24 15:35:30 -04001482 if (!file)
1483 kfree(ra);
1484 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001485 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001486}
1487
Miao Xie198605a2012-11-26 08:43:45 +00001488static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001489 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001490{
1491 u64 new_size;
1492 u64 old_size;
1493 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001494 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001495 struct btrfs_ioctl_vol_args *vol_args;
1496 struct btrfs_trans_handle *trans;
1497 struct btrfs_device *device = NULL;
1498 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001499 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001500 char *devstr = NULL;
1501 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001502 int mod = 0;
1503
Chris Masone441d542009-01-05 16:57:23 -05001504 if (!capable(CAP_SYS_ADMIN))
1505 return -EPERM;
1506
Miao Xie198605a2012-11-26 08:43:45 +00001507 ret = mnt_want_write_file(file);
1508 if (ret)
1509 return ret;
1510
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001511 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1512 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001513 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001514 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001515 }
1516
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001517 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001518 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001519 if (IS_ERR(vol_args)) {
1520 ret = PTR_ERR(vol_args);
1521 goto out;
1522 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001523
1524 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001525
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001526 sizestr = vol_args->name;
1527 devstr = strchr(sizestr, ':');
1528 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001529 sizestr = devstr + 1;
1530 *devstr = '\0';
1531 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001532 ret = kstrtoull(devstr, 10, &devid);
1533 if (ret)
1534 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001535 if (!devid) {
1536 ret = -EINVAL;
1537 goto out_free;
1538 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001539 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001540 }
Miao Xiedba60f32012-12-21 09:19:51 +00001541
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001542 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001543 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001544 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001545 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001546 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001547 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001548 }
Miao Xiedba60f32012-12-21 09:19:51 +00001549
1550 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001551 btrfs_info(root->fs_info,
1552 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001553 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001554 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001555 goto out_free;
1556 }
1557
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001558 if (!strcmp(sizestr, "max"))
1559 new_size = device->bdev->bd_inode->i_size;
1560 else {
1561 if (sizestr[0] == '-') {
1562 mod = -1;
1563 sizestr++;
1564 } else if (sizestr[0] == '+') {
1565 mod = 1;
1566 sizestr++;
1567 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001568 new_size = memparse(sizestr, &retptr);
1569 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001570 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001571 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572 }
1573 }
1574
Stefan Behrens63a212a2012-11-05 18:29:28 +01001575 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001576 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001577 goto out_free;
1578 }
1579
Miao Xie7cc8e582014-09-03 21:35:38 +08001580 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001581
1582 if (mod < 0) {
1583 if (new_size > old_size) {
1584 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001585 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001586 }
1587 new_size = old_size - new_size;
1588 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001589 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001590 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001591 goto out_free;
1592 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001593 new_size = old_size + new_size;
1594 }
1595
Byongho Leeee221842015-12-15 01:42:10 +09001596 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001597 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001598 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001599 }
1600 if (new_size > device->bdev->bd_inode->i_size) {
1601 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001602 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001603 }
1604
David Sterbab8b93ad2015-01-16 17:26:13 +01001605 new_size = div_u64(new_size, root->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001606 new_size *= root->sectorsize;
1607
David Sterbaecaeb142015-10-08 09:01:03 +02001608 btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001609 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001610
1611 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001612 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001613 if (IS_ERR(trans)) {
1614 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001615 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001616 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001617 ret = btrfs_grow_device(trans, device, new_size);
1618 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001619 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001620 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001621 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001622
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001623out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001624 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001625out:
1626 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001627 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001628 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001629 return ret;
1630}
1631
Sage Weil72fd0322010-10-29 15:41:32 -04001632static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001633 char *name, unsigned long fd, int subvol,
1634 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001635 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001636{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001637 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001638 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001639
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001640 if (!S_ISDIR(file_inode(file)->i_mode))
1641 return -ENOTDIR;
1642
Liu Boa874a632012-06-29 03:58:46 -06001643 ret = mnt_want_write_file(file);
1644 if (ret)
1645 goto out;
1646
Sage Weil72fd0322010-10-29 15:41:32 -04001647 namelen = strlen(name);
1648 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001649 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001650 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001651 }
1652
Chris Mason16780ca2012-02-20 22:14:55 -05001653 if (name[0] == '.' &&
1654 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1655 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001656 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001657 }
1658
Chris Mason3de45862008-11-17 21:02:50 -05001659 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001660 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001661 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001662 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001663 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001664 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001665 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001666 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001667 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001668 }
1669
Al Viro496ad9a2013-01-23 17:07:38 -05001670 src_inode = file_inode(src.file);
1671 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001672 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001673 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001674 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001675 } else if (!inode_owner_or_capable(src_inode)) {
1676 /*
1677 * Subvolume creation is not restricted, but snapshots
1678 * are limited to own subvolumes only
1679 */
1680 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001681 } else {
1682 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1683 BTRFS_I(src_inode)->root,
1684 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001685 }
Al Viro2903ff02012-08-28 12:52:22 -04001686 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001687 }
Liu Boa874a632012-06-29 03:58:46 -06001688out_drop_write:
1689 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001690out:
Sage Weil72fd0322010-10-29 15:41:32 -04001691 return ret;
1692}
1693
1694static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001695 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001696{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001697 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001698 int ret;
1699
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001700 if (!S_ISDIR(file_inode(file)->i_mode))
1701 return -ENOTDIR;
1702
Li Zefanfa0d2b92010-12-20 15:53:28 +08001703 vol_args = memdup_user(arg, sizeof(*vol_args));
1704 if (IS_ERR(vol_args))
1705 return PTR_ERR(vol_args);
1706 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001707
Li Zefanfa0d2b92010-12-20 15:53:28 +08001708 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001709 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001710 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001711
Li Zefanfa0d2b92010-12-20 15:53:28 +08001712 kfree(vol_args);
1713 return ret;
1714}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001715
Li Zefanfa0d2b92010-12-20 15:53:28 +08001716static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1717 void __user *arg, int subvol)
1718{
1719 struct btrfs_ioctl_vol_args_v2 *vol_args;
1720 int ret;
1721 u64 transid = 0;
1722 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001723 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001724 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001725
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001726 if (!S_ISDIR(file_inode(file)->i_mode))
1727 return -ENOTDIR;
1728
Li Zefanfa0d2b92010-12-20 15:53:28 +08001729 vol_args = memdup_user(arg, sizeof(*vol_args));
1730 if (IS_ERR(vol_args))
1731 return PTR_ERR(vol_args);
1732 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001733
Li Zefanb83cc962010-12-20 16:04:08 +08001734 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001735 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1736 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001737 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001738 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001739 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001740
1741 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1742 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001743 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1744 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001745 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001746 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001747 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001748 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001749 }
1750 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1751 if (IS_ERR(inherit)) {
1752 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001753 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001754 }
1755 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001756
1757 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001758 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001759 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001760 if (ret)
1761 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001762
Dan Carpenterc47ca322014-09-04 14:09:15 +03001763 if (ptr && copy_to_user(arg +
1764 offsetof(struct btrfs_ioctl_vol_args_v2,
1765 transid),
1766 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001767 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001768
1769free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001770 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001771free_args:
1772 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001773 return ret;
1774}
1775
Li Zefan0caa1022010-12-20 16:30:25 +08001776static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1777 void __user *arg)
1778{
Al Viro496ad9a2013-01-23 17:07:38 -05001779 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001780 struct btrfs_root *root = BTRFS_I(inode)->root;
1781 int ret = 0;
1782 u64 flags = 0;
1783
Li Zefan33345d012011-04-20 10:31:50 +08001784 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001785 return -EINVAL;
1786
1787 down_read(&root->fs_info->subvol_sem);
1788 if (btrfs_root_readonly(root))
1789 flags |= BTRFS_SUBVOL_RDONLY;
1790 up_read(&root->fs_info->subvol_sem);
1791
1792 if (copy_to_user(arg, &flags, sizeof(flags)))
1793 ret = -EFAULT;
1794
1795 return ret;
1796}
1797
1798static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1799 void __user *arg)
1800{
Al Viro496ad9a2013-01-23 17:07:38 -05001801 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001802 struct btrfs_root *root = BTRFS_I(inode)->root;
1803 struct btrfs_trans_handle *trans;
1804 u64 root_flags;
1805 u64 flags;
1806 int ret = 0;
1807
David Sterbabd60ea02014-01-16 15:50:22 +01001808 if (!inode_owner_or_capable(inode))
1809 return -EPERM;
1810
Liu Bob9ca0662012-06-29 03:58:49 -06001811 ret = mnt_want_write_file(file);
1812 if (ret)
1813 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001814
Liu Bob9ca0662012-06-29 03:58:49 -06001815 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1816 ret = -EINVAL;
1817 goto out_drop_write;
1818 }
Li Zefan0caa1022010-12-20 16:30:25 +08001819
Liu Bob9ca0662012-06-29 03:58:49 -06001820 if (copy_from_user(&flags, arg, sizeof(flags))) {
1821 ret = -EFAULT;
1822 goto out_drop_write;
1823 }
Li Zefan0caa1022010-12-20 16:30:25 +08001824
Liu Bob9ca0662012-06-29 03:58:49 -06001825 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1826 ret = -EINVAL;
1827 goto out_drop_write;
1828 }
Li Zefan0caa1022010-12-20 16:30:25 +08001829
Liu Bob9ca0662012-06-29 03:58:49 -06001830 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1831 ret = -EOPNOTSUPP;
1832 goto out_drop_write;
1833 }
Li Zefan0caa1022010-12-20 16:30:25 +08001834
1835 down_write(&root->fs_info->subvol_sem);
1836
1837 /* nothing to do */
1838 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001839 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001840
1841 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001842 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001843 btrfs_set_root_flags(&root->root_item,
1844 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001845 } else {
1846 /*
1847 * Block RO -> RW transition if this subvolume is involved in
1848 * send
1849 */
1850 spin_lock(&root->root_item_lock);
1851 if (root->send_in_progress == 0) {
1852 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001853 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001854 spin_unlock(&root->root_item_lock);
1855 } else {
1856 spin_unlock(&root->root_item_lock);
1857 btrfs_warn(root->fs_info,
1858 "Attempt to set subvolume %llu read-write during send",
1859 root->root_key.objectid);
1860 ret = -EPERM;
1861 goto out_drop_sem;
1862 }
1863 }
Li Zefan0caa1022010-12-20 16:30:25 +08001864
1865 trans = btrfs_start_transaction(root, 1);
1866 if (IS_ERR(trans)) {
1867 ret = PTR_ERR(trans);
1868 goto out_reset;
1869 }
1870
Li Zefanb4dc2b82011-02-16 06:06:34 +00001871 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001872 &root->root_key, &root->root_item);
1873
1874 btrfs_commit_transaction(trans, root);
1875out_reset:
1876 if (ret)
1877 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001878out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001879 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001880out_drop_write:
1881 mnt_drop_write_file(file);
1882out:
Li Zefan0caa1022010-12-20 16:30:25 +08001883 return ret;
1884}
1885
Yan, Zheng76dda932009-09-21 16:00:26 -04001886/*
1887 * helper to check if the subvolume references other subvolumes
1888 */
1889static noinline int may_destroy_subvol(struct btrfs_root *root)
1890{
1891 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001892 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001893 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001894 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001895 int ret;
1896
1897 path = btrfs_alloc_path();
1898 if (!path)
1899 return -ENOMEM;
1900
Josef Bacik175a2b82013-08-12 15:36:44 -04001901 /* Make sure this root isn't set as the default subvol */
1902 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1903 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1904 dir_id, "default", 7, 0);
1905 if (di && !IS_ERR(di)) {
1906 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1907 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001908 ret = -EPERM;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001909 btrfs_err(root->fs_info,
1910 "deleting default subvolume %llu is not allowed",
1911 key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001912 goto out;
1913 }
1914 btrfs_release_path(path);
1915 }
1916
Yan, Zheng76dda932009-09-21 16:00:26 -04001917 key.objectid = root->root_key.objectid;
1918 key.type = BTRFS_ROOT_REF_KEY;
1919 key.offset = (u64)-1;
1920
1921 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1922 &key, path, 0, 0);
1923 if (ret < 0)
1924 goto out;
1925 BUG_ON(ret == 0);
1926
1927 ret = 0;
1928 if (path->slots[0] > 0) {
1929 path->slots[0]--;
1930 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1931 if (key.objectid == root->root_key.objectid &&
1932 key.type == BTRFS_ROOT_REF_KEY)
1933 ret = -ENOTEMPTY;
1934 }
1935out:
1936 btrfs_free_path(path);
1937 return ret;
1938}
1939
Chris Masonac8e9812010-02-28 15:39:26 -05001940static noinline int key_in_sk(struct btrfs_key *key,
1941 struct btrfs_ioctl_search_key *sk)
1942{
Chris Masonabc6e132010-03-18 12:10:08 -04001943 struct btrfs_key test;
1944 int ret;
1945
1946 test.objectid = sk->min_objectid;
1947 test.type = sk->min_type;
1948 test.offset = sk->min_offset;
1949
1950 ret = btrfs_comp_cpu_keys(key, &test);
1951 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001952 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001953
1954 test.objectid = sk->max_objectid;
1955 test.type = sk->max_type;
1956 test.offset = sk->max_offset;
1957
1958 ret = btrfs_comp_cpu_keys(key, &test);
1959 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001960 return 0;
1961 return 1;
1962}
1963
Jeff Mahoneydf397562016-06-21 20:18:21 -04001964static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05001965 struct btrfs_key *key,
1966 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001967 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001968 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001969 unsigned long *sk_offset,
1970 int *num_found)
1971{
1972 u64 found_transid;
1973 struct extent_buffer *leaf;
1974 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001975 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001976 unsigned long item_off;
1977 unsigned long item_len;
1978 int nritems;
1979 int i;
1980 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001981 int ret = 0;
1982
1983 leaf = path->nodes[0];
1984 slot = path->slots[0];
1985 nritems = btrfs_header_nritems(leaf);
1986
1987 if (btrfs_header_generation(leaf) > sk->max_transid) {
1988 i = nritems;
1989 goto advance_key;
1990 }
1991 found_transid = btrfs_header_generation(leaf);
1992
1993 for (i = slot; i < nritems; i++) {
1994 item_off = btrfs_item_ptr_offset(leaf, i);
1995 item_len = btrfs_item_size_nr(leaf, i);
1996
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001997 btrfs_item_key_to_cpu(leaf, key, i);
1998 if (!key_in_sk(key, sk))
1999 continue;
2000
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002001 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002002 if (*num_found) {
2003 ret = 1;
2004 goto out;
2005 }
Chris Masonac8e9812010-02-28 15:39:26 -05002006
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002007 /*
2008 * return one empty item back for v1, which does not
2009 * handle -EOVERFLOW
2010 */
2011
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002012 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002013 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002014 ret = -EOVERFLOW;
2015 }
Chris Masonac8e9812010-02-28 15:39:26 -05002016
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002017 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002018 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002019 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002020 }
2021
Chris Masonac8e9812010-02-28 15:39:26 -05002022 sh.objectid = key->objectid;
2023 sh.offset = key->offset;
2024 sh.type = key->type;
2025 sh.len = item_len;
2026 sh.transid = found_transid;
2027
2028 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002029 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2030 ret = -EFAULT;
2031 goto out;
2032 }
2033
Chris Masonac8e9812010-02-28 15:39:26 -05002034 *sk_offset += sizeof(sh);
2035
2036 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002037 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002038 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002039 if (read_extent_buffer_to_user(leaf, up,
2040 item_off, item_len)) {
2041 ret = -EFAULT;
2042 goto out;
2043 }
2044
Chris Masonac8e9812010-02-28 15:39:26 -05002045 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002046 }
Hugo Millse2156862011-05-14 17:43:41 +00002047 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002048
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002049 if (ret) /* -EOVERFLOW from above */
2050 goto out;
2051
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002052 if (*num_found >= sk->nr_items) {
2053 ret = 1;
2054 goto out;
2055 }
Chris Masonac8e9812010-02-28 15:39:26 -05002056 }
2057advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002058 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002059 test.objectid = sk->max_objectid;
2060 test.type = sk->max_type;
2061 test.offset = sk->max_offset;
2062 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2063 ret = 1;
2064 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002065 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002066 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002067 key->offset = 0;
2068 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002069 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002070 key->offset = 0;
2071 key->type = 0;
2072 key->objectid++;
2073 } else
2074 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002075out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002076 /*
2077 * 0: all items from this leaf copied, continue with next
2078 * 1: * more items can be copied, but unused buffer is too small
2079 * * all items were found
2080 * Either way, it will stops the loop which iterates to the next
2081 * leaf
2082 * -EOVERFLOW: item was to large for buffer
2083 * -EFAULT: could not copy extent buffer back to userspace
2084 */
Chris Masonac8e9812010-02-28 15:39:26 -05002085 return ret;
2086}
2087
2088static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002089 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002090 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002091 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002092{
2093 struct btrfs_root *root;
2094 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002095 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002096 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2097 int ret;
2098 int num_found = 0;
2099 unsigned long sk_offset = 0;
2100
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002101 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2102 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002103 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002104 }
Gerhard Heift12544442014-01-30 16:23:58 +01002105
Chris Masonac8e9812010-02-28 15:39:26 -05002106 path = btrfs_alloc_path();
2107 if (!path)
2108 return -ENOMEM;
2109
2110 if (sk->tree_id == 0) {
2111 /* search the root of the inode that was passed */
2112 root = BTRFS_I(inode)->root;
2113 } else {
2114 key.objectid = sk->tree_id;
2115 key.type = BTRFS_ROOT_ITEM_KEY;
2116 key.offset = (u64)-1;
2117 root = btrfs_read_fs_root_no_name(info, &key);
2118 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002119 btrfs_free_path(path);
2120 return -ENOENT;
2121 }
2122 }
2123
2124 key.objectid = sk->min_objectid;
2125 key.type = sk->min_type;
2126 key.offset = sk->min_offset;
2127
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302128 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002129 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002130 if (ret != 0) {
2131 if (ret > 0)
2132 ret = 0;
2133 goto err;
2134 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002135 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002136 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002137 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002138 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002139 break;
2140
2141 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002142 if (ret > 0)
2143 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002144err:
2145 sk->nr_items = num_found;
2146 btrfs_free_path(path);
2147 return ret;
2148}
2149
2150static noinline int btrfs_ioctl_tree_search(struct file *file,
2151 void __user *argp)
2152{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002153 struct btrfs_ioctl_search_args __user *uargs;
2154 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002155 struct inode *inode;
2156 int ret;
2157 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002158
2159 if (!capable(CAP_SYS_ADMIN))
2160 return -EPERM;
2161
Gerhard Heiftba346b32014-01-30 16:24:02 +01002162 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002163
Gerhard Heiftba346b32014-01-30 16:24:02 +01002164 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2165 return -EFAULT;
2166
2167 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002168
Al Viro496ad9a2013-01-23 17:07:38 -05002169 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002170 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002171
2172 /*
2173 * In the origin implementation an overflow is handled by returning a
2174 * search header with a len of zero, so reset ret.
2175 */
2176 if (ret == -EOVERFLOW)
2177 ret = 0;
2178
Gerhard Heiftba346b32014-01-30 16:24:02 +01002179 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002180 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002181 return ret;
2182}
2183
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002184static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2185 void __user *argp)
2186{
2187 struct btrfs_ioctl_search_args_v2 __user *uarg;
2188 struct btrfs_ioctl_search_args_v2 args;
2189 struct inode *inode;
2190 int ret;
2191 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002192 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002193
2194 if (!capable(CAP_SYS_ADMIN))
2195 return -EPERM;
2196
2197 /* copy search header and buffer size */
2198 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2199 if (copy_from_user(&args, uarg, sizeof(args)))
2200 return -EFAULT;
2201
2202 buf_size = args.buf_size;
2203
2204 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2205 return -EOVERFLOW;
2206
2207 /* limit result size to 16MB */
2208 if (buf_size > buf_limit)
2209 buf_size = buf_limit;
2210
2211 inode = file_inode(file);
2212 ret = search_ioctl(inode, &args.key, &buf_size,
2213 (char *)(&uarg->buf[0]));
2214 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2215 ret = -EFAULT;
2216 else if (ret == -EOVERFLOW &&
2217 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2218 ret = -EFAULT;
2219
Yan, Zheng76dda932009-09-21 16:00:26 -04002220 return ret;
2221}
2222
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002223/*
Chris Masonac8e9812010-02-28 15:39:26 -05002224 * Search INODE_REFs to identify path name of 'dirid' directory
2225 * in a 'tree_id' tree. and sets path name to 'name'.
2226 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002227static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2228 u64 tree_id, u64 dirid, char *name)
2229{
2230 struct btrfs_root *root;
2231 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002232 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002233 int ret = -1;
2234 int slot;
2235 int len;
2236 int total_len = 0;
2237 struct btrfs_inode_ref *iref;
2238 struct extent_buffer *l;
2239 struct btrfs_path *path;
2240
2241 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2242 name[0]='\0';
2243 return 0;
2244 }
2245
2246 path = btrfs_alloc_path();
2247 if (!path)
2248 return -ENOMEM;
2249
Nikolay Borisov1c3aae52017-12-01 11:19:42 +02002250 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002251
2252 key.objectid = tree_id;
2253 key.type = BTRFS_ROOT_ITEM_KEY;
2254 key.offset = (u64)-1;
2255 root = btrfs_read_fs_root_no_name(info, &key);
2256 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002257 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002258 ret = -ENOENT;
2259 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002260 }
2261
2262 key.objectid = dirid;
2263 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002264 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002265
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302266 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002267 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2268 if (ret < 0)
2269 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002270 else if (ret > 0) {
2271 ret = btrfs_previous_item(root, path, dirid,
2272 BTRFS_INODE_REF_KEY);
2273 if (ret < 0)
2274 goto out;
2275 else if (ret > 0) {
2276 ret = -ENOENT;
2277 goto out;
2278 }
2279 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002280
2281 l = path->nodes[0];
2282 slot = path->slots[0];
2283 btrfs_item_key_to_cpu(l, &key, slot);
2284
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002285 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2286 len = btrfs_inode_ref_name_len(l, iref);
2287 ptr -= len + 1;
2288 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002289 if (ptr < name) {
2290 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002291 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002292 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002293
2294 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302295 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002296
2297 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2298 break;
2299
David Sterbab3b4aa72011-04-21 01:20:15 +02002300 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002301 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002302 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002303 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002304 }
Li Zefan77906a502011-07-14 03:16:00 +00002305 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302306 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002307 ret = 0;
2308out:
2309 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002310 return ret;
2311}
2312
2313static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2314 void __user *argp)
2315{
2316 struct btrfs_ioctl_ino_lookup_args *args;
2317 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002318 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002319
Julia Lawall2354d082010-10-29 15:14:18 -04002320 args = memdup_user(argp, sizeof(*args));
2321 if (IS_ERR(args))
2322 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002323
Al Viro496ad9a2013-01-23 17:07:38 -05002324 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002325
David Sterba01b810b2015-05-12 19:14:49 +02002326 /*
2327 * Unprivileged query to obtain the containing subvolume root id. The
2328 * path is reset so it's consistent with btrfs_search_path_in_tree.
2329 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002330 if (args->treeid == 0)
2331 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2332
David Sterba01b810b2015-05-12 19:14:49 +02002333 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2334 args->name[0] = 0;
2335 goto out;
2336 }
2337
2338 if (!capable(CAP_SYS_ADMIN)) {
2339 ret = -EPERM;
2340 goto out;
2341 }
2342
Chris Masonac8e9812010-02-28 15:39:26 -05002343 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2344 args->treeid, args->objectid,
2345 args->name);
2346
David Sterba01b810b2015-05-12 19:14:49 +02002347out:
Chris Masonac8e9812010-02-28 15:39:26 -05002348 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2349 ret = -EFAULT;
2350
2351 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002352 return ret;
2353}
2354
Yan, Zheng76dda932009-09-21 16:00:26 -04002355static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2356 void __user *arg)
2357{
Al Viro54563d42013-09-01 15:57:51 -04002358 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002359 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002360 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002361 struct inode *inode;
2362 struct btrfs_root *root = BTRFS_I(dir)->root;
2363 struct btrfs_root *dest = NULL;
2364 struct btrfs_ioctl_vol_args *vol_args;
2365 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002366 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002367 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002368 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002369 int namelen;
2370 int ret;
2371 int err = 0;
2372
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002373 if (!S_ISDIR(dir->i_mode))
2374 return -ENOTDIR;
2375
Yan, Zheng76dda932009-09-21 16:00:26 -04002376 vol_args = memdup_user(arg, sizeof(*vol_args));
2377 if (IS_ERR(vol_args))
2378 return PTR_ERR(vol_args);
2379
2380 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2381 namelen = strlen(vol_args->name);
2382 if (strchr(vol_args->name, '/') ||
2383 strncmp(vol_args->name, "..", namelen) == 0) {
2384 err = -EINVAL;
2385 goto out;
2386 }
2387
Al Viroa561be72011-11-23 11:57:51 -05002388 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002389 if (err)
2390 goto out;
2391
David Sterba521e0542014-04-15 16:41:44 +02002392
Al Viro00235412016-05-26 00:05:12 -04002393 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2394 if (err == -EINTR)
2395 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002396 dentry = lookup_one_len(vol_args->name, parent, namelen);
2397 if (IS_ERR(dentry)) {
2398 err = PTR_ERR(dentry);
2399 goto out_unlock_dir;
2400 }
2401
David Howells2b0143b2015-03-17 22:25:59 +00002402 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002403 err = -ENOENT;
2404 goto out_dput;
2405 }
2406
David Howells2b0143b2015-03-17 22:25:59 +00002407 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002408 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302409 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002410 /*
2411 * Regular user. Only allow this with a special mount
2412 * option, when the user has write+exec access to the
2413 * subvol root, and when rmdir(2) would have been
2414 * allowed.
2415 *
2416 * Note that this is _not_ check that the subvol is
2417 * empty or doesn't contain data that we wouldn't
2418 * otherwise be able to delete.
2419 *
2420 * Users who want to delete empty subvols should try
2421 * rmdir(2).
2422 */
2423 err = -EPERM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002424 if (!btrfs_test_opt(root->fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002425 goto out_dput;
2426
2427 /*
2428 * Do not allow deletion if the parent dir is the same
2429 * as the dir to be deleted. That means the ioctl
2430 * must be called on the dentry referencing the root
2431 * of the subvol, not a random directory contained
2432 * within it.
2433 */
2434 err = -EINVAL;
2435 if (root == dest)
2436 goto out_dput;
2437
2438 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2439 if (err)
2440 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002441 }
2442
Miao Xie5c39da52012-10-22 11:39:53 +00002443 /* check if subvolume may be deleted by a user */
2444 err = btrfs_may_delete(dir, dentry, 1);
2445 if (err)
2446 goto out_dput;
2447
Li Zefan33345d012011-04-20 10:31:50 +08002448 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002449 err = -EINVAL;
2450 goto out_dput;
2451 }
2452
Al Viro59551022016-01-22 15:40:57 -05002453 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002454
2455 /*
2456 * Don't allow to delete a subvolume with send in progress. This is
2457 * inside the i_mutex so the error handling that has to drop the bit
2458 * again is not run concurrently.
2459 */
2460 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002461 root_flags = btrfs_root_flags(&dest->root_item);
2462 if (dest->send_in_progress == 0) {
2463 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002464 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2465 spin_unlock(&dest->root_item_lock);
2466 } else {
2467 spin_unlock(&dest->root_item_lock);
2468 btrfs_warn(root->fs_info,
2469 "Attempt to delete subvolume %llu during send",
Filipe Mananac55bfa62014-05-25 03:55:44 +01002470 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002471 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002472 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002473 }
2474
Yan, Zheng76dda932009-09-21 16:00:26 -04002475 down_write(&root->fs_info->subvol_sem);
2476
2477 err = may_destroy_subvol(dest);
2478 if (err)
2479 goto out_up_write;
2480
Miao Xiec58aaad2013-02-28 10:05:36 +00002481 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2482 /*
2483 * One for dir inode, two for dir entries, two for root
2484 * ref/backref.
2485 */
2486 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002487 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002488 if (err)
2489 goto out_up_write;
2490
Yan, Zhenga22285a2010-05-16 10:48:46 -04002491 trans = btrfs_start_transaction(root, 0);
2492 if (IS_ERR(trans)) {
2493 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002494 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002495 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002496 trans->block_rsv = &block_rsv;
2497 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002498
Filipe Manana2be63d52016-02-12 11:34:23 +00002499 btrfs_record_snapshot_destroy(trans, dir);
2500
Yan, Zheng76dda932009-09-21 16:00:26 -04002501 ret = btrfs_unlink_subvol(trans, root, dir,
2502 dest->root_key.objectid,
2503 dentry->d_name.name,
2504 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002505 if (ret) {
2506 err = ret;
Jeff Mahoney66642832016-06-10 18:19:25 -04002507 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002508 goto out_end_trans;
2509 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002510
2511 btrfs_record_root_in_trans(trans, dest);
2512
2513 memset(&dest->root_item.drop_progress, 0,
2514 sizeof(dest->root_item.drop_progress));
2515 dest->root_item.drop_level = 0;
2516 btrfs_set_root_refs(&dest->root_item, 0);
2517
Miao Xie27cdeb72014-04-02 19:51:05 +08002518 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002519 ret = btrfs_insert_orphan_item(trans,
2520 root->fs_info->tree_root,
2521 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002522 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002523 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002524 err = ret;
2525 goto out_end_trans;
2526 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002527 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002528
2529 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2530 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2531 dest->root_key.objectid);
2532 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002533 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002534 err = ret;
2535 goto out_end_trans;
2536 }
2537 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2538 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2539 dest->root_item.received_uuid,
2540 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2541 dest->root_key.objectid);
2542 if (ret && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002543 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002544 err = ret;
2545 goto out_end_trans;
2546 }
2547 }
2548
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002549out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002550 trans->block_rsv = NULL;
2551 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002552 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002553 if (ret && !err)
2554 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002555 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002556out_release:
2557 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002558out_up_write:
2559 up_write(&root->fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002560 if (err) {
2561 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002562 root_flags = btrfs_root_flags(&dest->root_item);
2563 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002564 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2565 spin_unlock(&dest->root_item_lock);
2566 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002567out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002568 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002569 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002570 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002571 btrfs_invalidate_inodes(dest);
2572 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002573 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002574
2575 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002576 if (dest->ino_cache_inode) {
2577 iput(dest->ino_cache_inode);
2578 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002579 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002580 }
2581out_dput:
2582 dput(dentry);
2583out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002584 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002585out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002586 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002587out:
2588 kfree(vol_args);
2589 return err;
2590}
2591
Chris Mason1e701a32010-03-11 09:42:04 -05002592static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002593{
Al Viro496ad9a2013-01-23 17:07:38 -05002594 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002595 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002596 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002597 int ret;
2598
Ilya Dryomov25122d12013-01-20 15:57:57 +02002599 ret = mnt_want_write_file(file);
2600 if (ret)
2601 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002602
Ilya Dryomov25122d12013-01-20 15:57:57 +02002603 if (btrfs_root_readonly(root)) {
2604 ret = -EROFS;
2605 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002606 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002607
2608 switch (inode->i_mode & S_IFMT) {
2609 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002610 if (!capable(CAP_SYS_ADMIN)) {
2611 ret = -EPERM;
2612 goto out;
2613 }
Eric Sandeende78b512013-01-31 18:21:12 +00002614 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002615 if (ret)
2616 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002617 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002618 break;
2619 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002620 if (!(file->f_mode & FMODE_WRITE)) {
2621 ret = -EINVAL;
2622 goto out;
2623 }
Chris Mason1e701a32010-03-11 09:42:04 -05002624
2625 range = kzalloc(sizeof(*range), GFP_KERNEL);
2626 if (!range) {
2627 ret = -ENOMEM;
2628 goto out;
2629 }
2630
2631 if (argp) {
2632 if (copy_from_user(range, argp,
2633 sizeof(*range))) {
2634 ret = -EFAULT;
2635 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002636 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002637 }
2638 /* compression requires us to start the IO */
2639 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2640 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2641 range->extent_thresh = (u32)-1;
2642 }
2643 } else {
2644 /* the rest are all set to zero by kzalloc */
2645 range->len = (u64)-1;
2646 }
Al Viro496ad9a2013-01-23 17:07:38 -05002647 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002648 range, 0, 0);
2649 if (ret > 0)
2650 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002651 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002652 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002653 default:
2654 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002655 }
Chris Masone441d542009-01-05 16:57:23 -05002656out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002657 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002658 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002659}
2660
Christoph Hellwigb2950862008-12-02 09:54:17 -05002661static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002662{
2663 struct btrfs_ioctl_vol_args *vol_args;
2664 int ret;
2665
Chris Masone441d542009-01-05 16:57:23 -05002666 if (!capable(CAP_SYS_ADMIN))
2667 return -EPERM;
2668
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002669 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2670 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002671 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002672 }
2673
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002674 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002675 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002676 if (IS_ERR(vol_args)) {
2677 ret = PTR_ERR(vol_args);
2678 goto out;
2679 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002680
Mark Fasheh5516e592008-07-24 12:20:14 -04002681 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002682 ret = btrfs_init_new_device(root, vol_args->name);
2683
Anand Jain43d20762014-07-01 00:58:56 +08002684 if (!ret)
2685 btrfs_info(root->fs_info, "disk added %s",vol_args->name);
2686
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002687 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002688out:
2689 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002690 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002691 return ret;
2692}
2693
Anand Jain6b526ed2016-02-13 10:01:39 +08002694static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002695{
Al Viro496ad9a2013-01-23 17:07:38 -05002696 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jain6b526ed2016-02-13 10:01:39 +08002697 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002698 int ret;
2699
Chris Masone441d542009-01-05 16:57:23 -05002700 if (!capable(CAP_SYS_ADMIN))
2701 return -EPERM;
2702
Miao Xieda249272012-11-26 08:44:50 +00002703 ret = mnt_want_write_file(file);
2704 if (ret)
2705 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002706
Li Zefandae7b662009-04-08 15:06:54 +08002707 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002708 if (IS_ERR(vol_args)) {
2709 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002710 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002711 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002712
Anand Jain6b526ed2016-02-13 10:01:39 +08002713 /* Check for compatibility reject unknown flags */
Omar Sandoval9bb94d82018-05-22 15:44:01 -07002714 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
2715 ret = -EOPNOTSUPP;
2716 goto out;
2717 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002718
Anand Jain183860f2013-05-17 10:52:45 +00002719 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2720 1)) {
2721 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2722 goto out;
2723 }
2724
2725 mutex_lock(&root->fs_info->volume_mutex);
David Sterba735654e2016-02-15 18:15:21 +01002726 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Anand Jain6b526ed2016-02-13 10:01:39 +08002727 ret = btrfs_rm_device(root, NULL, vol_args->devid);
2728 } else {
2729 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
2730 ret = btrfs_rm_device(root, vol_args->name, 0);
2731 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002732 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002733 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002734
Anand Jain6b526ed2016-02-13 10:01:39 +08002735 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002736 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Anand Jain6b526ed2016-02-13 10:01:39 +08002737 btrfs_info(root->fs_info, "device deleted: id %llu",
2738 vol_args->devid);
2739 else
2740 btrfs_info(root->fs_info, "device deleted: %s",
2741 vol_args->name);
2742 }
Anand Jain183860f2013-05-17 10:52:45 +00002743out:
2744 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002745err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002746 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002747 return ret;
2748}
2749
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002750static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2751{
2752 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2753 struct btrfs_ioctl_vol_args *vol_args;
2754 int ret;
2755
2756 if (!capable(CAP_SYS_ADMIN))
2757 return -EPERM;
2758
2759 ret = mnt_want_write_file(file);
2760 if (ret)
2761 return ret;
2762
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002763 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2764 1)) {
2765 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002766 goto out_drop_write;
2767 }
2768
2769 vol_args = memdup_user(arg, sizeof(*vol_args));
2770 if (IS_ERR(vol_args)) {
2771 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002772 goto out;
2773 }
2774
David Sterba58d7bbf2016-05-04 14:10:47 +02002775 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002776 mutex_lock(&root->fs_info->volume_mutex);
Anand Jain6b526ed2016-02-13 10:01:39 +08002777 ret = btrfs_rm_device(root, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002778 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002779
2780 if (!ret)
2781 btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002782 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002783out:
2784 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2785out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002786 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002787
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002788 return ret;
2789}
2790
Jan Schmidt475f6382011-03-11 15:41:01 +01002791static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2792{
Li Zefan027ed2f2011-06-08 08:27:56 +00002793 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002794 struct btrfs_device *device;
Jan Schmidt475f6382011-03-11 15:41:01 +01002795 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002796 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002797
Li Zefan027ed2f2011-06-08 08:27:56 +00002798 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2799 if (!fi_args)
2800 return -ENOMEM;
2801
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002802 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002803 fi_args->num_devices = fs_devices->num_devices;
2804 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002805
Byongho Leed7641a42015-09-01 23:10:57 +09002806 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002807 if (device->devid > fi_args->max_id)
2808 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002809 }
2810 mutex_unlock(&fs_devices->device_list_mutex);
2811
David Sterba80a773f2014-05-07 18:17:06 +02002812 fi_args->nodesize = root->fs_info->super_copy->nodesize;
2813 fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
2814 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
2815
Li Zefan027ed2f2011-06-08 08:27:56 +00002816 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2817 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002818
Li Zefan027ed2f2011-06-08 08:27:56 +00002819 kfree(fi_args);
2820 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002821}
2822
2823static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2824{
2825 struct btrfs_ioctl_dev_info_args *di_args;
2826 struct btrfs_device *dev;
2827 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2828 int ret = 0;
2829 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002830
Jan Schmidt475f6382011-03-11 15:41:01 +01002831 di_args = memdup_user(arg, sizeof(*di_args));
2832 if (IS_ERR(di_args))
2833 return PTR_ERR(di_args);
2834
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002835 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002836 s_uuid = di_args->uuid;
2837
2838 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002839 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002840
2841 if (!dev) {
2842 ret = -ENODEV;
2843 goto out;
2844 }
2845
2846 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002847 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2848 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002849 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002850 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002851 struct rcu_string *name;
2852
2853 rcu_read_lock();
2854 name = rcu_dereference(dev->name);
2855 strncpy(di_args->path, name->str, sizeof(di_args->path));
2856 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002857 di_args->path[sizeof(di_args->path) - 1] = 0;
2858 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002859 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002860 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002861
2862out:
David Sterba55793c02013-04-26 15:20:23 +00002863 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002864 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2865 ret = -EFAULT;
2866
2867 kfree(di_args);
2868 return ret;
2869}
2870
Mark Fashehf4414602015-06-30 14:42:05 -07002871static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002872{
2873 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002874
Mark Fasheh416161d2013-08-06 11:42:51 -07002875 page = grab_cache_page(inode->i_mapping, index);
2876 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002877 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002878
2879 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002880 int ret;
2881
2882 ret = btrfs_readpage(NULL, page);
2883 if (ret)
2884 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002885 lock_page(page);
2886 if (!PageUptodate(page)) {
2887 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002888 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002889 return ERR_PTR(-EIO);
2890 }
2891 if (page->mapping != inode->i_mapping) {
2892 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002893 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002894 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002895 }
2896 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002897
2898 return page;
2899}
2900
Mark Fashehf4414602015-06-30 14:42:05 -07002901static int gather_extent_pages(struct inode *inode, struct page **pages,
2902 int num_pages, u64 off)
2903{
2904 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002905 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002906
2907 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002908again:
Mark Fashehf4414602015-06-30 14:42:05 -07002909 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002910 if (IS_ERR(pages[i])) {
2911 int err = PTR_ERR(pages[i]);
2912
2913 if (err == -EAGAIN)
2914 goto again;
2915 pages[i] = NULL;
2916 return err;
2917 }
Mark Fashehf4414602015-06-30 14:42:05 -07002918 }
2919 return 0;
2920}
2921
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002922static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2923 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002924{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002925 /*
2926 * Do any pending delalloc/csum calculations on inode, one way or
2927 * another, and lock file content.
2928 * The locking order is:
2929 *
2930 * 1) pages
2931 * 2) range in the inode's io tree
2932 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002933 while (1) {
2934 struct btrfs_ordered_extent *ordered;
2935 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2936 ordered = btrfs_lookup_first_ordered_extent(inode,
2937 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002938 if ((!ordered ||
2939 ordered->file_offset + ordered->len <= off ||
2940 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002941 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002942 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2943 if (ordered)
2944 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002945 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002946 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002947 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2948 if (ordered)
2949 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002950 if (!retry_range_locking)
2951 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002952 btrfs_wait_ordered_range(inode, off, len);
2953 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002954 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002955}
2956
Mark Fashehf4414602015-06-30 14:42:05 -07002957static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002958{
Al Viro59551022016-01-22 15:40:57 -05002959 inode_unlock(inode1);
2960 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002961}
2962
Mark Fashehf4414602015-06-30 14:42:05 -07002963static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2964{
2965 if (inode1 < inode2)
2966 swap(inode1, inode2);
2967
Al Viro59551022016-01-22 15:40:57 -05002968 inode_lock_nested(inode1, I_MUTEX_PARENT);
2969 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002970}
2971
2972static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2973 struct inode *inode2, u64 loff2, u64 len)
2974{
2975 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2976 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2977}
2978
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002979static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2980 struct inode *inode2, u64 loff2, u64 len,
2981 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002982{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002983 int ret;
2984
Mark Fasheh416161d2013-08-06 11:42:51 -07002985 if (inode1 < inode2) {
2986 swap(inode1, inode2);
2987 swap(loff1, loff2);
2988 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002989 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2990 if (ret)
2991 return ret;
2992 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2993 if (ret)
2994 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2995 loff1 + len - 1);
2996 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002997}
2998
2999struct cmp_pages {
3000 int num_pages;
3001 struct page **src_pages;
3002 struct page **dst_pages;
3003};
3004
3005static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3006{
3007 int i;
3008 struct page *pg;
3009
3010 for (i = 0; i < cmp->num_pages; i++) {
3011 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003012 if (pg) {
3013 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003014 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003015 }
Mark Fashehf4414602015-06-30 14:42:05 -07003016 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003017 if (pg) {
3018 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003019 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003020 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003021 }
Mark Fashehf4414602015-06-30 14:42:05 -07003022 kfree(cmp->src_pages);
3023 kfree(cmp->dst_pages);
3024}
3025
3026static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3027 struct inode *dst, u64 dst_loff,
3028 u64 len, struct cmp_pages *cmp)
3029{
3030 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003031 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003032 struct page **src_pgarr, **dst_pgarr;
3033
3034 /*
3035 * We must gather up all the pages before we initiate our
3036 * extent locking. We use an array for the page pointers. Size
3037 * of the array is bounded by len, which is in turn bounded by
3038 * BTRFS_MAX_DEDUPE_LEN.
3039 */
David Sterba66722f72016-02-11 15:01:38 +01003040 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
3041 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07003042 if (!src_pgarr || !dst_pgarr) {
3043 kfree(src_pgarr);
3044 kfree(dst_pgarr);
3045 return -ENOMEM;
3046 }
3047 cmp->num_pages = num_pages;
3048 cmp->src_pages = src_pgarr;
3049 cmp->dst_pages = dst_pgarr;
3050
3051 ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff);
3052 if (ret)
3053 goto out;
3054
3055 ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff);
3056
3057out:
3058 if (ret)
3059 btrfs_cmp_data_free(cmp);
Naohiro Aotaba44bc42017-09-08 17:48:55 +09003060 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003061}
3062
3063static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
Mark Fashehf4414602015-06-30 14:42:05 -07003064 u64 dst_loff, u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003065{
3066 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003067 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003068 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003069 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003070 void *addr, *dst_addr;
3071
Mark Fashehf4414602015-06-30 14:42:05 -07003072 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003073 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003074 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003075 cmp_len = len;
3076
Mark Fashehf4414602015-06-30 14:42:05 -07003077 BUG_ON(i >= cmp->num_pages);
3078
3079 src_page = cmp->src_pages[i];
3080 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003081 ASSERT(PageLocked(src_page));
3082 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003083
Mark Fasheh416161d2013-08-06 11:42:51 -07003084 addr = kmap_atomic(src_page);
3085 dst_addr = kmap_atomic(dst_page);
3086
3087 flush_dcache_page(src_page);
3088 flush_dcache_page(dst_page);
3089
3090 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003091 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003092
3093 kunmap_atomic(addr);
3094 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003095
3096 if (ret)
3097 break;
3098
Mark Fasheh416161d2013-08-06 11:42:51 -07003099 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003100 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003101 }
3102
3103 return ret;
3104}
3105
Mark Fashehe1d227a2015-06-08 15:05:25 -07003106static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3107 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003108{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003109 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003110 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3111
Mark Fashehe1d227a2015-06-08 15:05:25 -07003112 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003113 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003114
3115 /* if we extend to eof, continue to block boundary */
3116 if (off + len == inode->i_size)
3117 *plen = len = ALIGN(inode->i_size, bs) - off;
3118
Mark Fasheh416161d2013-08-06 11:42:51 -07003119 /* Check that we are block aligned - btrfs_clone() requires this */
3120 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3121 return -EINVAL;
3122
3123 return 0;
3124}
3125
Mark Fashehe1d227a2015-06-08 15:05:25 -07003126static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003127 struct inode *dst, u64 dst_loff)
3128{
3129 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003130 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003131 struct cmp_pages cmp;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003132 int same_inode = 0;
3133 u64 same_lock_start = 0;
3134 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003135
Mark Fasheh416161d2013-08-06 11:42:51 -07003136 if (src == dst)
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003137 same_inode = 1;
Mark Fasheh416161d2013-08-06 11:42:51 -07003138
Filipe Manana113e8282015-03-30 18:26:47 +01003139 if (len == 0)
3140 return 0;
3141
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003142 if (same_inode) {
Al Viro59551022016-01-22 15:40:57 -05003143 inode_lock(src);
Mark Fasheh416161d2013-08-06 11:42:51 -07003144
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003145 ret = extent_same_check_offsets(src, loff, &len, olen);
3146 if (ret)
3147 goto out_unlock;
Filipe Mananaf4dfe682016-02-12 14:44:00 +00003148 ret = extent_same_check_offsets(src, dst_loff, &len, olen);
3149 if (ret)
3150 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003151
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003152 /*
3153 * Single inode case wants the same checks, except we
3154 * don't want our length pushed out past i_size as
3155 * comparing that data range makes no sense.
3156 *
3157 * extent_same_check_offsets() will do this for an
3158 * unaligned length at i_size, so catch it here and
3159 * reject the request.
3160 *
3161 * This effectively means we require aligned extents
3162 * for the single-inode case, whereas the other cases
3163 * allow an unaligned length so long as it ends at
3164 * i_size.
3165 */
3166 if (len != olen) {
3167 ret = -EINVAL;
3168 goto out_unlock;
3169 }
3170
3171 /* Check for overlapping ranges */
3172 if (dst_loff + len > loff && dst_loff < loff + len) {
3173 ret = -EINVAL;
3174 goto out_unlock;
3175 }
3176
3177 same_lock_start = min_t(u64, loff, dst_loff);
3178 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
3179 } else {
3180 btrfs_double_inode_lock(src, dst);
3181
3182 ret = extent_same_check_offsets(src, loff, &len, olen);
3183 if (ret)
3184 goto out_unlock;
3185
3186 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3187 if (ret)
3188 goto out_unlock;
3189 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003190
3191 /* don't make the dst file partly checksummed */
3192 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3193 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3194 ret = -EINVAL;
3195 goto out_unlock;
3196 }
3197
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003198again:
Mark Fashehf4414602015-06-30 14:42:05 -07003199 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3200 if (ret)
3201 goto out_unlock;
3202
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003203 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003204 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3205 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003206 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003207 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3208 false);
3209 /*
3210 * If one of the inodes has dirty pages in the respective range or
3211 * ordered extents, we need to flush dellaloc and wait for all ordered
3212 * extents in the range. We must unlock the pages and the ranges in the
3213 * io trees to avoid deadlocks when flushing delalloc (requires locking
3214 * pages) and when waiting for ordered extents to complete (they require
3215 * range locking).
3216 */
3217 if (ret == -EAGAIN) {
3218 /*
3219 * Ranges in the io trees already unlocked. Now unlock all
3220 * pages before waiting for all IO to complete.
3221 */
3222 btrfs_cmp_data_free(&cmp);
3223 if (same_inode) {
3224 btrfs_wait_ordered_range(src, same_lock_start,
3225 same_lock_len);
3226 } else {
3227 btrfs_wait_ordered_range(src, loff, len);
3228 btrfs_wait_ordered_range(dst, dst_loff, len);
3229 }
3230 goto again;
3231 }
3232 ASSERT(ret == 0);
3233 if (WARN_ON(ret)) {
3234 /* ranges in the io trees already unlocked */
3235 btrfs_cmp_data_free(&cmp);
3236 return ret;
3237 }
Mark Fashehf4414602015-06-30 14:42:05 -07003238
Mark Fasheh207910d2015-06-30 14:42:04 -07003239 /* pass original length for comparison so we stay within i_size */
Mark Fashehf4414602015-06-30 14:42:05 -07003240 ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003241 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003242 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003243
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003244 if (same_inode)
3245 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3246 same_lock_start + same_lock_len - 1);
3247 else
3248 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003249
3250 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003251out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003252 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003253 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003254 else
3255 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003256
3257 return ret;
3258}
3259
Byongho Leeee221842015-12-15 01:42:10 +09003260#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003261
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003262ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3263 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003264{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003265 struct inode *src = file_inode(src_file);
3266 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003267 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003268 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003269
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003270 if (olen > BTRFS_MAX_DEDUPE_LEN)
3271 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003272
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003273 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003274 /*
3275 * Btrfs does not support blocksize < page_size. As a
3276 * result, btrfs_cmp_data() won't correctly handle
3277 * this situation without an update.
3278 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003279 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003280 }
3281
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003282 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3283 if (res)
3284 return res;
3285 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003286}
3287
Filipe Mananaf82a9902014-06-01 01:50:28 +01003288static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3289 struct inode *inode,
3290 u64 endoff,
3291 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003292 const u64 olen,
3293 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003294{
3295 struct btrfs_root *root = BTRFS_I(inode)->root;
3296 int ret;
3297
3298 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003299 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003300 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003301 /*
3302 * We round up to the block size at eof when determining which
3303 * extents to clone above, but shouldn't round up the file size.
3304 */
3305 if (endoff > destoff + olen)
3306 endoff = destoff + olen;
3307 if (endoff > inode->i_size)
3308 btrfs_i_size_write(inode, endoff);
3309
3310 ret = btrfs_update_inode(trans, root, inode);
3311 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003312 btrfs_abort_transaction(trans, ret);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003313 btrfs_end_transaction(trans, root);
3314 goto out;
3315 }
3316 ret = btrfs_end_transaction(trans, root);
3317out:
3318 return ret;
3319}
3320
Filipe Manana7ffbb592014-06-09 03:48:05 +01003321static void clone_update_extent_map(struct inode *inode,
3322 const struct btrfs_trans_handle *trans,
3323 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003324 const u64 hole_offset,
3325 const u64 hole_len)
3326{
3327 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3328 struct extent_map *em;
3329 int ret;
3330
3331 em = alloc_extent_map();
3332 if (!em) {
3333 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3334 &BTRFS_I(inode)->runtime_flags);
3335 return;
3336 }
3337
Filipe Manana14f59792014-06-29 21:45:40 +01003338 if (path) {
3339 struct btrfs_file_extent_item *fi;
3340
3341 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3342 struct btrfs_file_extent_item);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003343 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3344 em->generation = -1;
3345 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3346 BTRFS_FILE_EXTENT_INLINE)
3347 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3348 &BTRFS_I(inode)->runtime_flags);
3349 } else {
3350 em->start = hole_offset;
3351 em->len = hole_len;
3352 em->ram_bytes = em->len;
3353 em->orig_start = hole_offset;
3354 em->block_start = EXTENT_MAP_HOLE;
3355 em->block_len = 0;
3356 em->orig_block_len = 0;
3357 em->compress_type = BTRFS_COMPRESS_NONE;
3358 em->generation = trans->transid;
3359 }
3360
3361 while (1) {
3362 write_lock(&em_tree->lock);
3363 ret = add_extent_mapping(em_tree, em, 1);
3364 write_unlock(&em_tree->lock);
3365 if (ret != -EEXIST) {
3366 free_extent_map(em);
3367 break;
3368 }
3369 btrfs_drop_extent_cache(inode, em->start,
3370 em->start + em->len - 1, 0);
3371 }
3372
David Sterbaee39b432014-09-30 01:33:33 +02003373 if (ret)
Filipe Manana7ffbb592014-06-09 03:48:05 +01003374 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3375 &BTRFS_I(inode)->runtime_flags);
3376}
3377
Filipe Manana8039d872015-10-13 15:15:00 +01003378/*
3379 * Make sure we do not end up inserting an inline extent into a file that has
3380 * already other (non-inline) extents. If a file has an inline extent it can
3381 * not have any other extents and the (single) inline extent must start at the
3382 * file offset 0. Failing to respect these rules will lead to file corruption,
3383 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3384 *
3385 * We can have extents that have been already written to disk or we can have
3386 * dirty ranges still in delalloc, in which case the extent maps and items are
3387 * created only when we run delalloc, and the delalloc ranges might fall outside
3388 * the range we are currently locking in the inode's io tree. So we check the
3389 * inode's i_size because of that (i_size updates are done while holding the
3390 * i_mutex, which we are holding here).
3391 * We also check to see if the inode has a size not greater than "datal" but has
3392 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3393 * protected against such concurrent fallocate calls by the i_mutex).
3394 *
3395 * If the file has no extents but a size greater than datal, do not allow the
3396 * copy because we would need turn the inline extent into a non-inline one (even
3397 * with NO_HOLES enabled). If we find our destination inode only has one inline
3398 * extent, just overwrite it with the source inline extent if its size is less
3399 * than the source extent's size, or we could copy the source inline extent's
3400 * data into the destination inode's inline extent if the later is greater then
3401 * the former.
3402 */
3403static int clone_copy_inline_extent(struct inode *src,
3404 struct inode *dst,
3405 struct btrfs_trans_handle *trans,
3406 struct btrfs_path *path,
3407 struct btrfs_key *new_key,
3408 const u64 drop_start,
3409 const u64 datal,
3410 const u64 skip,
3411 const u64 size,
3412 char *inline_data)
3413{
3414 struct btrfs_root *root = BTRFS_I(dst)->root;
3415 const u64 aligned_end = ALIGN(new_key->offset + datal,
3416 root->sectorsize);
3417 int ret;
3418 struct btrfs_key key;
3419
3420 if (new_key->offset > 0)
3421 return -EOPNOTSUPP;
3422
3423 key.objectid = btrfs_ino(dst);
3424 key.type = BTRFS_EXTENT_DATA_KEY;
3425 key.offset = 0;
3426 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3427 if (ret < 0) {
3428 return ret;
3429 } else if (ret > 0) {
3430 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3431 ret = btrfs_next_leaf(root, path);
3432 if (ret < 0)
3433 return ret;
3434 else if (ret > 0)
3435 goto copy_inline_extent;
3436 }
3437 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3438 if (key.objectid == btrfs_ino(dst) &&
3439 key.type == BTRFS_EXTENT_DATA_KEY) {
3440 ASSERT(key.offset > 0);
3441 return -EOPNOTSUPP;
3442 }
3443 } else if (i_size_read(dst) <= datal) {
3444 struct btrfs_file_extent_item *ei;
3445 u64 ext_len;
3446
3447 /*
3448 * If the file size is <= datal, make sure there are no other
3449 * extents following (can happen do to an fallocate call with
3450 * the flag FALLOC_FL_KEEP_SIZE).
3451 */
3452 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3453 struct btrfs_file_extent_item);
3454 /*
3455 * If it's an inline extent, it can not have other extents
3456 * following it.
3457 */
3458 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3459 BTRFS_FILE_EXTENT_INLINE)
3460 goto copy_inline_extent;
3461
3462 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3463 if (ext_len > aligned_end)
3464 return -EOPNOTSUPP;
3465
3466 ret = btrfs_next_item(root, path);
3467 if (ret < 0) {
3468 return ret;
3469 } else if (ret == 0) {
3470 btrfs_item_key_to_cpu(path->nodes[0], &key,
3471 path->slots[0]);
3472 if (key.objectid == btrfs_ino(dst) &&
3473 key.type == BTRFS_EXTENT_DATA_KEY)
3474 return -EOPNOTSUPP;
3475 }
3476 }
3477
3478copy_inline_extent:
3479 /*
3480 * We have no extent items, or we have an extent at offset 0 which may
3481 * or may not be inlined. All these cases are dealt the same way.
3482 */
3483 if (i_size_read(dst) > datal) {
3484 /*
3485 * If the destination inode has an inline extent...
3486 * This would require copying the data from the source inline
3487 * extent into the beginning of the destination's inline extent.
3488 * But this is really complex, both extents can be compressed
3489 * or just one of them, which would require decompressing and
3490 * re-compressing data (which could increase the new compressed
3491 * size, not allowing the compressed data to fit anymore in an
3492 * inline extent).
3493 * So just don't support this case for now (it should be rare,
3494 * we are not really saving space when cloning inline extents).
3495 */
3496 return -EOPNOTSUPP;
3497 }
3498
3499 btrfs_release_path(path);
3500 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3501 if (ret)
3502 return ret;
3503 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3504 if (ret)
3505 return ret;
3506
3507 if (skip) {
3508 const u32 start = btrfs_file_extent_calc_inline_size(0);
3509
3510 memmove(inline_data + start, inline_data + start + skip, datal);
3511 }
3512
3513 write_extent_buffer(path->nodes[0], inline_data,
3514 btrfs_item_ptr_offset(path->nodes[0],
3515 path->slots[0]),
3516 size);
3517 inode_add_bytes(dst, datal);
3518
3519 return 0;
3520}
3521
Mark Fasheh32b7c682013-08-06 11:42:49 -07003522/**
3523 * btrfs_clone() - clone a range from inode file to another
3524 *
3525 * @src: Inode to clone from
3526 * @inode: Inode to clone to
3527 * @off: Offset within source to start clone from
3528 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003529 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003530 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003531 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003532 */
3533static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003534 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003535 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003536{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003537 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003538 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003539 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003540 struct btrfs_trans_handle *trans;
3541 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003542 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003543 u32 nritems;
3544 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003545 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003546 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003547 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003548
3549 ret = -ENOMEM;
David Sterba15351952016-04-11 18:40:08 +02003550 buf = kmalloc(root->nodesize, GFP_KERNEL | __GFP_NOWARN);
3551 if (!buf) {
3552 buf = vmalloc(root->nodesize);
3553 if (!buf)
3554 return ret;
3555 }
Yan Zhengae01a0a2008-08-04 23:23:47 -04003556
3557 path = btrfs_alloc_path();
3558 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003559 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003560 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003561 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003562
David Sterbae4058b52015-11-27 16:31:35 +01003563 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003564 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08003565 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003566 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003567 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003568
3569 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003570 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003571
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003572 /*
3573 * note the key will change type as we walk through the
3574 * tree.
3575 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003576 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003577 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3578 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003579 if (ret < 0)
3580 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003581 /*
3582 * First search, if no extent item that starts at offset off was
3583 * found but the previous item is an extent item, it's possible
3584 * it might overlap our target range, therefore process it.
3585 */
3586 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3587 btrfs_item_key_to_cpu(path->nodes[0], &key,
3588 path->slots[0] - 1);
3589 if (key.type == BTRFS_EXTENT_DATA_KEY)
3590 path->slots[0]--;
3591 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003592
Yan Zhengae01a0a2008-08-04 23:23:47 -04003593 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003594process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003595 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003596 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003597 if (ret < 0)
3598 goto out;
3599 if (ret > 0)
3600 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003601 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003602 }
3603 leaf = path->nodes[0];
3604 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003605
Yan Zhengae01a0a2008-08-04 23:23:47 -04003606 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003607 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08003608 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003609 break;
3610
David Sterba962a2982014-06-04 18:41:45 +02003611 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003612 struct btrfs_file_extent_item *extent;
3613 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003614 u32 size;
3615 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003616 u64 disko = 0, diskl = 0;
3617 u64 datao = 0, datal = 0;
3618 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003619 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003620
Sage Weilc5c9cd42008-11-12 14:32:25 -05003621 extent = btrfs_item_ptr(leaf, slot,
3622 struct btrfs_file_extent_item);
3623 comp = btrfs_file_extent_compression(leaf, extent);
3624 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003625 if (type == BTRFS_FILE_EXTENT_REG ||
3626 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003627 disko = btrfs_file_extent_disk_bytenr(leaf,
3628 extent);
3629 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3630 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003631 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003632 datal = btrfs_file_extent_num_bytes(leaf,
3633 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003634 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3635 /* take upper bound, may be compressed */
3636 datal = btrfs_file_extent_ram_bytes(leaf,
3637 extent);
3638 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003639
Filipe Manana2c463822014-05-31 02:31:05 +01003640 /*
3641 * The first search might have left us at an extent
3642 * item that ends before our target range's start, can
3643 * happen if we have holes and NO_HOLES feature enabled.
3644 */
3645 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003646 path->slots[0]++;
3647 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003648 } else if (key.offset >= off + len) {
3649 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003650 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003651 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003652 size = btrfs_item_size_nr(leaf, slot);
3653 read_extent_buffer(leaf, buf,
3654 btrfs_item_ptr_offset(leaf, slot),
3655 size);
3656
3657 btrfs_release_path(path);
3658 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003659
Zheng Yan31840ae2008-09-23 13:14:14 -04003660 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08003661 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08003662 if (off <= key.offset)
3663 new_key.offset = key.offset + destoff - off;
3664 else
3665 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003666
Sage Weilb6f34092011-09-20 14:48:51 -04003667 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003668 * Deal with a hole that doesn't have an extent item
3669 * that represents it (NO_HOLES feature enabled).
3670 * This hole is either in the middle of the cloning
3671 * range or at the beginning (fully overlaps it or
3672 * partially overlaps it).
3673 */
3674 if (new_key.offset != last_dest_end)
3675 drop_start = last_dest_end;
3676 else
3677 drop_start = new_key.offset;
3678
3679 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003680 * 1 - adjusting old extent (we may have to split it)
3681 * 1 - add new extent
3682 * 1 - inode update
3683 */
3684 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003685 if (IS_ERR(trans)) {
3686 ret = PTR_ERR(trans);
3687 goto out;
3688 }
3689
Chris Masonc8a894d2009-06-27 21:07:03 -04003690 if (type == BTRFS_FILE_EXTENT_REG ||
3691 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003692 /*
3693 * a | --- range to clone ---| b
3694 * | ------------- extent ------------- |
3695 */
3696
Antonio Ospite93915582014-06-04 14:03:48 +02003697 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003698 if (key.offset + datal > off + len)
3699 datal = off + len - key.offset;
3700
Antonio Ospite93915582014-06-04 14:03:48 +02003701 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 if (off > key.offset) {
3703 datao += off - key.offset;
3704 datal -= off - key.offset;
3705 }
3706
Josef Bacik5dc562c2012-08-17 13:14:17 -04003707 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003708 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003709 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003710 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003711 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003712 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003713 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003714 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003715 btrfs_end_transaction(trans, root);
3716 goto out;
3717 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003718
Sage Weilc5c9cd42008-11-12 14:32:25 -05003719 ret = btrfs_insert_empty_item(trans, root, path,
3720 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003721 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003722 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003723 btrfs_end_transaction(trans, root);
3724 goto out;
3725 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003726
3727 leaf = path->nodes[0];
3728 slot = path->slots[0];
3729 write_extent_buffer(leaf, buf,
3730 btrfs_item_ptr_offset(leaf, slot),
3731 size);
3732
3733 extent = btrfs_item_ptr(leaf, slot,
3734 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003735
Sage Weilc5c9cd42008-11-12 14:32:25 -05003736 /* disko == 0 means it's a hole */
3737 if (!disko)
3738 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003739
3740 btrfs_set_file_extent_offset(leaf, extent,
3741 datao);
3742 btrfs_set_file_extent_num_bytes(leaf, extent,
3743 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003744
Sage Weilc5c9cd42008-11-12 14:32:25 -05003745 if (disko) {
3746 inode_add_bytes(inode, datal);
3747 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003748 disko, diskl, 0,
3749 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003750 btrfs_ino(inode),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003751 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003752 if (ret) {
3753 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 ret);
3755 btrfs_end_transaction(trans,
3756 root);
3757 goto out;
3758
3759 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003760 }
3761 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3762 u64 skip = 0;
3763 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003764
Sage Weilc5c9cd42008-11-12 14:32:25 -05003765 if (off > key.offset) {
3766 skip = off - key.offset;
3767 new_key.offset += skip;
3768 }
Chris Masond3977122009-01-05 21:25:51 -05003769
Liu Boaa42ffd2012-09-18 03:52:23 -06003770 if (key.offset + datal > off + len)
3771 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003772
Sage Weilc5c9cd42008-11-12 14:32:25 -05003773 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003774 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003775 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003776 goto out;
3777 }
3778 size -= skip + trim;
3779 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003780
Filipe Manana8039d872015-10-13 15:15:00 +01003781 ret = clone_copy_inline_extent(src, inode,
3782 trans, path,
3783 &new_key,
3784 drop_start,
3785 datal,
3786 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003787 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003788 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003789 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003790 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003791 btrfs_end_transaction(trans, root);
3792 goto out;
3793 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003794 leaf = path->nodes[0];
3795 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003796 }
3797
Filipe Manana7ffbb592014-06-09 03:48:05 +01003798 /* If we have an implicit hole (NO_HOLES feature). */
3799 if (drop_start < new_key.offset)
3800 clone_update_extent_map(inode, trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003801 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003802 new_key.offset - drop_start);
3803
Filipe Manana14f59792014-06-29 21:45:40 +01003804 clone_update_extent_map(inode, trans, path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003805
Sage Weilc5c9cd42008-11-12 14:32:25 -05003806 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003807 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003808
Filipe Manana62e23902014-08-08 02:47:06 +01003809 last_dest_end = ALIGN(new_key.offset + datal,
3810 root->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003811 ret = clone_finish_inode_update(trans, inode,
3812 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003813 destoff, olen,
3814 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003815 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003816 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003817 if (new_key.offset + datal >= destoff + len)
3818 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003819 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003820 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003821 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003822
3823 if (fatal_signal_pending(current)) {
3824 ret = -EINTR;
3825 goto out;
3826 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003827 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003828 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003829
Filipe Mananaf82a9902014-06-01 01:50:28 +01003830 if (last_dest_end < destoff + len) {
3831 /*
3832 * We have an implicit hole (NO_HOLES feature is enabled) that
3833 * fully or partially overlaps our cloning range at its end.
3834 */
3835 btrfs_release_path(path);
3836
3837 /*
3838 * 1 - remove extent(s)
3839 * 1 - inode update
3840 */
3841 trans = btrfs_start_transaction(root, 2);
3842 if (IS_ERR(trans)) {
3843 ret = PTR_ERR(trans);
3844 goto out;
3845 }
3846 ret = btrfs_drop_extents(trans, root, inode,
3847 last_dest_end, destoff + len, 1);
3848 if (ret) {
3849 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003850 btrfs_abort_transaction(trans, ret);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003851 btrfs_end_transaction(trans, root);
3852 goto out;
3853 }
Filipe Manana14f59792014-06-29 21:45:40 +01003854 clone_update_extent_map(inode, trans, NULL, last_dest_end,
3855 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003856 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003857 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003858 }
3859
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003860out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003861 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003862 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003863 return ret;
3864}
3865
Zach Brown3db11b22015-11-10 16:53:32 -05003866static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3867 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003868{
Al Viro54563d42013-09-01 15:57:51 -04003869 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003870 struct inode *src = file_inode(file_src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003871 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003872 int ret;
3873 u64 len = olen;
3874 u64 bs = root->fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003875 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003876
3877 /*
3878 * TODO:
3879 * - split compressed inline extents. annoying: we need to
3880 * decompress into destination's address_space (the file offset
3881 * may change, so source mapping won't do), then recompress (or
3882 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003883 *
3884 * - split destination inode's inline extents. The inline extents can
3885 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003886 */
3887
Mark Fasheh32b7c682013-08-06 11:42:49 -07003888 if (btrfs_root_readonly(root))
3889 return -EROFS;
3890
Zach Brown3db11b22015-11-10 16:53:32 -05003891 if (file_src->f_path.mnt != file->f_path.mnt ||
3892 src->i_sb != inode->i_sb)
3893 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003894
Mark Fasheh32b7c682013-08-06 11:42:49 -07003895 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003896 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003897
3898 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003899 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003900 } else {
Al Viro59551022016-01-22 15:40:57 -05003901 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003902 }
3903
Omar Sandoval52ea25b2018-05-22 15:02:12 -07003904 /* don't make the dst file partly checksummed */
3905 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3906 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
3907 ret = -EINVAL;
3908 goto out_unlock;
3909 }
3910
Mark Fasheh32b7c682013-08-06 11:42:49 -07003911 /* determine range to clone */
3912 ret = -EINVAL;
3913 if (off + len > src->i_size || off + len < off)
3914 goto out_unlock;
3915 if (len == 0)
3916 olen = len = src->i_size - off;
Filipe Manana800d8112018-11-05 11:14:17 +00003917 /*
3918 * If we extend to eof, continue to block boundary if and only if the
3919 * destination end offset matches the destination file's size, otherwise
3920 * we would be corrupting data by placing the eof block into the middle
3921 * of a file.
3922 */
3923 if (off + len == src->i_size) {
3924 if (!IS_ALIGNED(len, bs) && destoff + len < inode->i_size)
3925 goto out_unlock;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003926 len = ALIGN(src->i_size, bs) - off;
Filipe Manana800d8112018-11-05 11:14:17 +00003927 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003928
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003929 if (len == 0) {
3930 ret = 0;
3931 goto out_unlock;
3932 }
3933
Mark Fasheh32b7c682013-08-06 11:42:49 -07003934 /* verify the end result is block aligned */
3935 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3936 !IS_ALIGNED(destoff, bs))
3937 goto out_unlock;
3938
3939 /* verify if ranges are overlapped within the same file */
3940 if (same_inode) {
3941 if (destoff + len > off && destoff < off + len)
3942 goto out_unlock;
3943 }
3944
3945 if (destoff > inode->i_size) {
3946 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3947 if (ret)
3948 goto out_unlock;
3949 }
3950
Filipe Mananac125b8b2014-05-23 05:03:34 +01003951 /*
3952 * Lock the target range too. Right after we replace the file extent
3953 * items in the fs tree (which now point to the cloned data), we might
3954 * have a worker replace them with extent items relative to a write
3955 * operation that was issued before this clone operation (i.e. confront
3956 * with inode.c:btrfs_finish_ordered_io).
3957 */
3958 if (same_inode) {
3959 u64 lock_start = min_t(u64, off, destoff);
3960 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003961
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003962 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003963 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003964 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3965 true);
3966 }
3967 ASSERT(ret == 0);
3968 if (WARN_ON(ret)) {
3969 /* ranges in the io trees already unlocked */
3970 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003971 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003972
Mark Fasheh1c919a52015-06-30 14:42:08 -07003973 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003974
Filipe Mananac125b8b2014-05-23 05:03:34 +01003975 if (same_inode) {
3976 u64 lock_start = min_t(u64, off, destoff);
3977 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3978
3979 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3980 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003981 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003982 }
3983 /*
3984 * Truncate page cache pages so that future reads will see the cloned
3985 * data immediately and not the previous data.
3986 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303987 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003988 round_down(destoff, PAGE_SIZE),
3989 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003990out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003991 if (!same_inode)
3992 btrfs_double_inode_unlock(src, inode);
3993 else
Al Viro59551022016-01-22 15:40:57 -05003994 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003995 return ret;
3996}
3997
3998ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
3999 struct file *file_out, loff_t pos_out,
4000 size_t len, unsigned int flags)
4001{
4002 ssize_t ret;
4003
4004 ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
4005 if (ret == 0)
4006 ret = len;
4007 return ret;
4008}
4009
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004010int btrfs_clone_file_range(struct file *src_file, loff_t off,
4011 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05004012{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004013 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004014}
4015
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004016/*
4017 * there are many ways the trans_start and trans_end ioctls can lead
4018 * to deadlocks. They should only be used by applications that
4019 * basically own the machine, and have a very in depth understanding
4020 * of all the possible deadlocks and enospc problems.
4021 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05004022static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004023{
Al Viro496ad9a2013-01-23 17:07:38 -05004024 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004025 struct btrfs_root *root = BTRFS_I(inode)->root;
4026 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004027 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004028
Sage Weil1ab86ae2009-09-29 18:38:44 -04004029 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04004030 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004031 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004032
4033 ret = -EINPROGRESS;
4034 if (file->private_data)
4035 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004036
Li Zefanb83cc962010-12-20 16:04:08 +08004037 ret = -EROFS;
4038 if (btrfs_root_readonly(root))
4039 goto out;
4040
Al Viroa561be72011-11-23 11:57:51 -05004041 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05004042 if (ret)
4043 goto out;
4044
Josef Bacika4abeea2011-04-11 17:25:13 -04004045 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004046
Sage Weil1ab86ae2009-09-29 18:38:44 -04004047 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004048 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00004049 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04004050 goto out_drop;
4051
4052 file->private_data = trans;
4053 return 0;
4054
4055out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04004056 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05004057 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004058out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004059 return ret;
4060}
4061
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004062static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4063{
Al Viro496ad9a2013-01-23 17:07:38 -05004064 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004065 struct btrfs_root *root = BTRFS_I(inode)->root;
4066 struct btrfs_root *new_root;
4067 struct btrfs_dir_item *di;
4068 struct btrfs_trans_handle *trans;
4069 struct btrfs_path *path;
4070 struct btrfs_key location;
4071 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004072 u64 objectid = 0;
4073 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004074 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004075
4076 if (!capable(CAP_SYS_ADMIN))
4077 return -EPERM;
4078
Miao Xie3c04ce02012-11-26 08:43:07 +00004079 ret = mnt_want_write_file(file);
4080 if (ret)
4081 return ret;
4082
4083 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4084 ret = -EFAULT;
4085 goto out;
4086 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004087
4088 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304089 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004090
4091 location.objectid = objectid;
4092 location.type = BTRFS_ROOT_ITEM_KEY;
4093 location.offset = (u64)-1;
4094
4095 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004096 if (IS_ERR(new_root)) {
4097 ret = PTR_ERR(new_root);
4098 goto out;
4099 }
satoru takeuchif11525d2017-09-12 22:42:52 +09004100 if (!is_fstree(new_root->objectid)) {
4101 ret = -ENOENT;
4102 goto out;
4103 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004104
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004105 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004106 if (!path) {
4107 ret = -ENOMEM;
4108 goto out;
4109 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004110 path->leave_spinning = 1;
4111
4112 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004113 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004114 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004115 ret = PTR_ERR(trans);
4116 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004117 }
4118
David Sterba6c417612011-04-13 15:41:04 +02004119 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004120 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
4121 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004122 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004123 btrfs_free_path(path);
4124 btrfs_end_transaction(trans, root);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004125 btrfs_err(new_root->fs_info,
4126 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004127 ret = -ENOENT;
4128 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004129 }
4130
4131 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4132 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4133 btrfs_mark_buffer_dirty(path->nodes[0]);
4134 btrfs_free_path(path);
4135
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06004136 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004137 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00004138out:
4139 mnt_drop_write_file(file);
4140 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004141}
4142
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004143void btrfs_get_block_group_info(struct list_head *groups_list,
4144 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004145{
4146 struct btrfs_block_group_cache *block_group;
4147
4148 space->total_bytes = 0;
4149 space->used_bytes = 0;
4150 space->flags = 0;
4151 list_for_each_entry(block_group, groups_list, list) {
4152 space->flags = block_group->flags;
4153 space->total_bytes += block_group->key.offset;
4154 space->used_bytes +=
4155 btrfs_block_group_used(&block_group->item);
4156 }
4157}
4158
Eric Sandeen48a3b632013-04-25 20:41:01 +00004159static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004160{
4161 struct btrfs_ioctl_space_args space_args;
4162 struct btrfs_ioctl_space_info space;
4163 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004164 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004165 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004166 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004167 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4168 BTRFS_BLOCK_GROUP_SYSTEM,
4169 BTRFS_BLOCK_GROUP_METADATA,
4170 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4171 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004172 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004173 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004174 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004175 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004176
4177 if (copy_from_user(&space_args,
4178 (struct btrfs_ioctl_space_args __user *)arg,
4179 sizeof(space_args)))
4180 return -EFAULT;
4181
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004182 for (i = 0; i < num_types; i++) {
4183 struct btrfs_space_info *tmp;
4184
4185 info = NULL;
4186 rcu_read_lock();
4187 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4188 list) {
4189 if (tmp->flags == types[i]) {
4190 info = tmp;
4191 break;
4192 }
4193 }
4194 rcu_read_unlock();
4195
4196 if (!info)
4197 continue;
4198
4199 down_read(&info->groups_sem);
4200 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4201 if (!list_empty(&info->block_groups[c]))
4202 slot_count++;
4203 }
4204 up_read(&info->groups_sem);
4205 }
Josef Bacik1406e432010-01-13 18:19:06 +00004206
David Sterba36523e952014-02-07 14:34:12 +01004207 /*
4208 * Global block reserve, exported as a space_info
4209 */
4210 slot_count++;
4211
Chris Mason7fde62b2010-03-16 15:40:10 -04004212 /* space_slots == 0 means they are asking for a count */
4213 if (space_args.space_slots == 0) {
4214 space_args.total_spaces = slot_count;
4215 goto out;
4216 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004217
Dan Rosenberg51788b12011-02-14 16:04:23 -05004218 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004219
Chris Mason7fde62b2010-03-16 15:40:10 -04004220 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004221
Chris Mason7fde62b2010-03-16 15:40:10 -04004222 /* we generally have at most 6 or so space infos, one for each raid
4223 * level. So, a whole page should be more than enough for everyone
4224 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004225 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004226 return -ENOMEM;
4227
4228 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004229 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004230 if (!dest)
4231 return -ENOMEM;
4232 dest_orig = dest;
4233
4234 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004235 for (i = 0; i < num_types; i++) {
4236 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004237
Dan Rosenberg51788b12011-02-14 16:04:23 -05004238 if (!slot_count)
4239 break;
4240
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004241 info = NULL;
4242 rcu_read_lock();
4243 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4244 list) {
4245 if (tmp->flags == types[i]) {
4246 info = tmp;
4247 break;
4248 }
4249 }
4250 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004251
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004252 if (!info)
4253 continue;
4254 down_read(&info->groups_sem);
4255 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4256 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004257 btrfs_get_block_group_info(
4258 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004259 memcpy(dest, &space, sizeof(space));
4260 dest++;
4261 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004262 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004263 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004264 if (!slot_count)
4265 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004266 }
4267 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004268 }
Josef Bacik1406e432010-01-13 18:19:06 +00004269
David Sterba36523e952014-02-07 14:34:12 +01004270 /*
4271 * Add global block reserve
4272 */
4273 if (slot_count) {
4274 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
4275
4276 spin_lock(&block_rsv->lock);
4277 space.total_bytes = block_rsv->size;
4278 space.used_bytes = block_rsv->size - block_rsv->reserved;
4279 spin_unlock(&block_rsv->lock);
4280 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4281 memcpy(dest, &space, sizeof(space));
4282 space_args.total_spaces++;
4283 }
4284
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004285 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004286 (arg + sizeof(struct btrfs_ioctl_space_args));
4287
4288 if (copy_to_user(user_dest, dest_orig, alloc_size))
4289 ret = -EFAULT;
4290
4291 kfree(dest_orig);
4292out:
4293 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004294 ret = -EFAULT;
4295
4296 return ret;
4297}
4298
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004299/*
4300 * there are many ways the trans_start and trans_end ioctls can lead
4301 * to deadlocks. They should only be used by applications that
4302 * basically own the machine, and have a very in depth understanding
4303 * of all the possible deadlocks and enospc problems.
4304 */
4305long btrfs_ioctl_trans_end(struct file *file)
4306{
Al Viro496ad9a2013-01-23 17:07:38 -05004307 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004308 struct btrfs_root *root = BTRFS_I(inode)->root;
4309 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004310
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004311 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004312 if (!trans)
4313 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004314 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004315
Sage Weil1ab86ae2009-09-29 18:38:44 -04004316 btrfs_end_transaction(trans, root);
4317
Josef Bacika4abeea2011-04-11 17:25:13 -04004318 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004319
Al Viro2a79f172011-12-09 08:06:57 -05004320 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004321 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004322}
4323
Miao Xie9a8c28b2012-11-26 08:40:43 +00004324static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4325 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004326{
Sage Weil46204592010-10-29 15:41:32 -04004327 struct btrfs_trans_handle *trans;
4328 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004329 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004330
Miao Xied4edf392013-02-20 09:17:06 +00004331 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004332 if (IS_ERR(trans)) {
4333 if (PTR_ERR(trans) != -ENOENT)
4334 return PTR_ERR(trans);
4335
4336 /* No running transaction, don't bother */
4337 transid = root->fs_info->last_trans_committed;
4338 goto out;
4339 }
Sage Weil46204592010-10-29 15:41:32 -04004340 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004341 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004342 if (ret) {
4343 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004344 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004345 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004346out:
Sage Weil46204592010-10-29 15:41:32 -04004347 if (argp)
4348 if (copy_to_user(argp, &transid, sizeof(transid)))
4349 return -EFAULT;
4350 return 0;
4351}
4352
Miao Xie9a8c28b2012-11-26 08:40:43 +00004353static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4354 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004355{
Sage Weil46204592010-10-29 15:41:32 -04004356 u64 transid;
4357
4358 if (argp) {
4359 if (copy_from_user(&transid, argp, sizeof(transid)))
4360 return -EFAULT;
4361 } else {
4362 transid = 0; /* current trans */
4363 }
4364 return btrfs_wait_for_commit(root, transid);
4365}
4366
Miao Xieb8e95482012-11-26 08:48:01 +00004367static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004368{
Al Viro496ad9a2013-01-23 17:07:38 -05004369 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01004370 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004371 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004372
4373 if (!capable(CAP_SYS_ADMIN))
4374 return -EPERM;
4375
4376 sa = memdup_user(arg, sizeof(*sa));
4377 if (IS_ERR(sa))
4378 return PTR_ERR(sa);
4379
Miao Xieb8e95482012-11-26 08:48:01 +00004380 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4381 ret = mnt_want_write_file(file);
4382 if (ret)
4383 goto out;
4384 }
4385
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004386 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004387 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4388 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004389
4390 if (copy_to_user(arg, sa, sizeof(*sa)))
4391 ret = -EFAULT;
4392
Miao Xieb8e95482012-11-26 08:48:01 +00004393 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4394 mnt_drop_write_file(file);
4395out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004396 kfree(sa);
4397 return ret;
4398}
4399
4400static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4401{
4402 if (!capable(CAP_SYS_ADMIN))
4403 return -EPERM;
4404
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004405 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004406}
4407
4408static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4409 void __user *arg)
4410{
4411 struct btrfs_ioctl_scrub_args *sa;
4412 int ret;
4413
4414 if (!capable(CAP_SYS_ADMIN))
4415 return -EPERM;
4416
4417 sa = memdup_user(arg, sizeof(*sa));
4418 if (IS_ERR(sa))
4419 return PTR_ERR(sa);
4420
4421 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4422
4423 if (copy_to_user(arg, sa, sizeof(*sa)))
4424 ret = -EFAULT;
4425
4426 kfree(sa);
4427 return ret;
4428}
4429
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004430static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004431 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004432{
4433 struct btrfs_ioctl_get_dev_stats *sa;
4434 int ret;
4435
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004436 sa = memdup_user(arg, sizeof(*sa));
4437 if (IS_ERR(sa))
4438 return PTR_ERR(sa);
4439
David Sterbab27f7c02012-06-22 06:30:39 -06004440 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4441 kfree(sa);
4442 return -EPERM;
4443 }
4444
4445 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004446
4447 if (copy_to_user(arg, sa, sizeof(*sa)))
4448 ret = -EFAULT;
4449
4450 kfree(sa);
4451 return ret;
4452}
4453
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004454static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4455{
4456 struct btrfs_ioctl_dev_replace_args *p;
4457 int ret;
4458
4459 if (!capable(CAP_SYS_ADMIN))
4460 return -EPERM;
4461
4462 p = memdup_user(arg, sizeof(*p));
4463 if (IS_ERR(p))
4464 return PTR_ERR(p);
4465
4466 switch (p->cmd) {
4467 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004468 if (root->fs_info->sb->s_flags & MS_RDONLY) {
4469 ret = -EROFS;
4470 goto out;
4471 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004472 if (atomic_xchg(
4473 &root->fs_info->mutually_exclusive_operation_running,
4474 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004475 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004476 } else {
Anand Jainb5255452016-03-24 18:48:14 +08004477 ret = btrfs_dev_replace_by_ioctl(root, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004478 atomic_set(
4479 &root->fs_info->mutually_exclusive_operation_running,
4480 0);
4481 }
4482 break;
4483 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4484 btrfs_dev_replace_status(root->fs_info, p);
4485 ret = 0;
4486 break;
4487 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4488 ret = btrfs_dev_replace_cancel(root->fs_info, p);
4489 break;
4490 default:
4491 ret = -EINVAL;
4492 break;
4493 }
4494
4495 if (copy_to_user(arg, p, sizeof(*p)))
4496 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004497out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004498 kfree(p);
4499 return ret;
4500}
4501
Jan Schmidtd7728c92011-07-07 16:48:38 +02004502static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4503{
4504 int ret = 0;
4505 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004506 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004507 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004508 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004509 struct inode_fs_paths *ipath = NULL;
4510 struct btrfs_path *path;
4511
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004512 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004513 return -EPERM;
4514
4515 path = btrfs_alloc_path();
4516 if (!path) {
4517 ret = -ENOMEM;
4518 goto out;
4519 }
4520
4521 ipa = memdup_user(arg, sizeof(*ipa));
4522 if (IS_ERR(ipa)) {
4523 ret = PTR_ERR(ipa);
4524 ipa = NULL;
4525 goto out;
4526 }
4527
4528 size = min_t(u32, ipa->size, 4096);
4529 ipath = init_ipath(size, root, path);
4530 if (IS_ERR(ipath)) {
4531 ret = PTR_ERR(ipath);
4532 ipath = NULL;
4533 goto out;
4534 }
4535
4536 ret = paths_from_inode(ipa->inum, ipath);
4537 if (ret < 0)
4538 goto out;
4539
4540 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004541 rel_ptr = ipath->fspath->val[i] -
4542 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004543 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004544 }
4545
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004546 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4547 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004548 if (ret) {
4549 ret = -EFAULT;
4550 goto out;
4551 }
4552
4553out:
4554 btrfs_free_path(path);
4555 free_ipath(ipath);
4556 kfree(ipa);
4557
4558 return ret;
4559}
4560
4561static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4562{
4563 struct btrfs_data_container *inodes = ctx;
4564 const size_t c = 3 * sizeof(u64);
4565
4566 if (inodes->bytes_left >= c) {
4567 inodes->bytes_left -= c;
4568 inodes->val[inodes->elem_cnt] = inum;
4569 inodes->val[inodes->elem_cnt + 1] = offset;
4570 inodes->val[inodes->elem_cnt + 2] = root;
4571 inodes->elem_cnt += 3;
4572 } else {
4573 inodes->bytes_missing += c - inodes->bytes_left;
4574 inodes->bytes_left = 0;
4575 inodes->elem_missed += 3;
4576 }
4577
4578 return 0;
4579}
4580
4581static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4582 void __user *arg)
4583{
4584 int ret = 0;
4585 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004586 struct btrfs_ioctl_logical_ino_args *loi;
4587 struct btrfs_data_container *inodes = NULL;
4588 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004589
4590 if (!capable(CAP_SYS_ADMIN))
4591 return -EPERM;
4592
4593 loi = memdup_user(arg, sizeof(*loi));
4594 if (IS_ERR(loi)) {
4595 ret = PTR_ERR(loi);
4596 loi = NULL;
4597 goto out;
4598 }
4599
4600 path = btrfs_alloc_path();
4601 if (!path) {
4602 ret = -ENOMEM;
4603 goto out;
4604 }
4605
Byongho Leeee221842015-12-15 01:42:10 +09004606 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004607 inodes = init_data_container(size);
4608 if (IS_ERR(inodes)) {
4609 ret = PTR_ERR(inodes);
4610 inodes = NULL;
4611 goto out;
4612 }
4613
Liu Bodf031f02012-09-07 20:01:29 -06004614 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4615 build_ino_list, inodes);
4616 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004617 ret = -ENOENT;
4618 if (ret < 0)
4619 goto out;
4620
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004621 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4622 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004623 if (ret)
4624 ret = -EFAULT;
4625
4626out:
4627 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06004628 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004629 kfree(loi);
4630
4631 return ret;
4632}
4633
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004634void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004635 struct btrfs_ioctl_balance_args *bargs)
4636{
4637 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4638
4639 bargs->flags = bctl->flags;
4640
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004641 if (atomic_read(&fs_info->balance_running))
4642 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4643 if (atomic_read(&fs_info->balance_pause_req))
4644 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004645 if (atomic_read(&fs_info->balance_cancel_req))
4646 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004647
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004648 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4649 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4650 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004651
4652 if (lock) {
4653 spin_lock(&fs_info->balance_lock);
4654 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4655 spin_unlock(&fs_info->balance_lock);
4656 } else {
4657 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4658 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004659}
4660
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004661static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004662{
Al Viro496ad9a2013-01-23 17:07:38 -05004663 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004664 struct btrfs_fs_info *fs_info = root->fs_info;
4665 struct btrfs_ioctl_balance_args *bargs;
4666 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004667 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004668 int ret;
4669
4670 if (!capable(CAP_SYS_ADMIN))
4671 return -EPERM;
4672
Liu Boe54bfa32012-06-29 03:58:48 -06004673 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004674 if (ret)
4675 return ret;
4676
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004677again:
4678 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4679 mutex_lock(&fs_info->volume_mutex);
4680 mutex_lock(&fs_info->balance_mutex);
4681 need_unlock = true;
4682 goto locked;
4683 }
4684
4685 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004686 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004687 * (1) some other op is running
4688 * (2) balance is running
4689 * (3) balance is paused -- special case (think resume)
4690 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004691 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004692 if (fs_info->balance_ctl) {
4693 /* this is either (2) or (3) */
4694 if (!atomic_read(&fs_info->balance_running)) {
4695 mutex_unlock(&fs_info->balance_mutex);
4696 if (!mutex_trylock(&fs_info->volume_mutex))
4697 goto again;
4698 mutex_lock(&fs_info->balance_mutex);
4699
4700 if (fs_info->balance_ctl &&
4701 !atomic_read(&fs_info->balance_running)) {
4702 /* this is (3) */
4703 need_unlock = false;
4704 goto locked;
4705 }
4706
4707 mutex_unlock(&fs_info->balance_mutex);
4708 mutex_unlock(&fs_info->volume_mutex);
4709 goto again;
4710 } else {
4711 /* this is (2) */
4712 mutex_unlock(&fs_info->balance_mutex);
4713 ret = -EINPROGRESS;
4714 goto out;
4715 }
4716 } else {
4717 /* this is (1) */
4718 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004719 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004720 goto out;
4721 }
4722
4723locked:
4724 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004725
4726 if (arg) {
4727 bargs = memdup_user(arg, sizeof(*bargs));
4728 if (IS_ERR(bargs)) {
4729 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004730 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004731 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004732
4733 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4734 if (!fs_info->balance_ctl) {
4735 ret = -ENOTCONN;
4736 goto out_bargs;
4737 }
4738
4739 bctl = fs_info->balance_ctl;
4740 spin_lock(&fs_info->balance_lock);
4741 bctl->flags |= BTRFS_BALANCE_RESUME;
4742 spin_unlock(&fs_info->balance_lock);
4743
4744 goto do_balance;
4745 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004746 } else {
4747 bargs = NULL;
4748 }
4749
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004750 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004751 ret = -EINPROGRESS;
4752 goto out_bargs;
4753 }
4754
David Sterba8d2db782015-11-04 15:38:29 +01004755 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004756 if (!bctl) {
4757 ret = -ENOMEM;
4758 goto out_bargs;
4759 }
4760
4761 bctl->fs_info = fs_info;
4762 if (arg) {
4763 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4764 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4765 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4766
4767 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004768 } else {
4769 /* balance everything - no filters */
4770 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004771 }
4772
David Sterba8eb93452015-10-12 16:55:54 +02004773 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4774 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004775 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004776 }
4777
Ilya Dryomovde322262012-01-16 22:04:49 +02004778do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004779 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004780 * Ownership of bctl and mutually_exclusive_operation_running
4781 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4782 * or, if restriper was paused all the way until unmount, in
4783 * free_fs_info. mutually_exclusive_operation_running is
4784 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004785 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004786 need_unlock = false;
4787
4788 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004789 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004790
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004791 if (arg) {
4792 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4793 ret = -EFAULT;
4794 }
4795
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004796out_bctl:
4797 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004798out_bargs:
4799 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004800out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004801 mutex_unlock(&fs_info->balance_mutex);
4802 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004803 if (need_unlock)
4804 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4805out:
Liu Boe54bfa32012-06-29 03:58:48 -06004806 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004807 return ret;
4808}
4809
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004810static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4811{
4812 if (!capable(CAP_SYS_ADMIN))
4813 return -EPERM;
4814
4815 switch (cmd) {
4816 case BTRFS_BALANCE_CTL_PAUSE:
4817 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004818 case BTRFS_BALANCE_CTL_CANCEL:
4819 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004820 }
4821
4822 return -EINVAL;
4823}
4824
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004825static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4826 void __user *arg)
4827{
4828 struct btrfs_fs_info *fs_info = root->fs_info;
4829 struct btrfs_ioctl_balance_args *bargs;
4830 int ret = 0;
4831
4832 if (!capable(CAP_SYS_ADMIN))
4833 return -EPERM;
4834
4835 mutex_lock(&fs_info->balance_mutex);
4836 if (!fs_info->balance_ctl) {
4837 ret = -ENOTCONN;
4838 goto out;
4839 }
4840
David Sterba8d2db782015-11-04 15:38:29 +01004841 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004842 if (!bargs) {
4843 ret = -ENOMEM;
4844 goto out;
4845 }
4846
4847 update_ioctl_balance_args(fs_info, 1, bargs);
4848
4849 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4850 ret = -EFAULT;
4851
4852 kfree(bargs);
4853out:
4854 mutex_unlock(&fs_info->balance_mutex);
4855 return ret;
4856}
4857
Miao Xie905b0dd2012-11-26 08:50:11 +00004858static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004859{
Al Viro496ad9a2013-01-23 17:07:38 -05004860 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004861 struct btrfs_ioctl_quota_ctl_args *sa;
4862 struct btrfs_trans_handle *trans = NULL;
4863 int ret;
4864 int err;
4865
4866 if (!capable(CAP_SYS_ADMIN))
4867 return -EPERM;
4868
Miao Xie905b0dd2012-11-26 08:50:11 +00004869 ret = mnt_want_write_file(file);
4870 if (ret)
4871 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004872
4873 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004874 if (IS_ERR(sa)) {
4875 ret = PTR_ERR(sa);
4876 goto drop_write;
4877 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004878
Wang Shilong7708f022013-04-07 10:24:57 +00004879 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004880 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4881 if (IS_ERR(trans)) {
4882 ret = PTR_ERR(trans);
4883 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004884 }
4885
4886 switch (sa->cmd) {
4887 case BTRFS_QUOTA_CTL_ENABLE:
4888 ret = btrfs_quota_enable(trans, root->fs_info);
4889 break;
4890 case BTRFS_QUOTA_CTL_DISABLE:
4891 ret = btrfs_quota_disable(trans, root->fs_info);
4892 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004893 default:
4894 ret = -EINVAL;
4895 break;
4896 }
4897
Jan Schmidt2f232032013-04-25 16:04:51 +00004898 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4899 if (err && !ret)
4900 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004901out:
4902 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004903 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004904drop_write:
4905 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004906 return ret;
4907}
4908
Miao Xie905b0dd2012-11-26 08:50:11 +00004909static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004910{
Al Viro496ad9a2013-01-23 17:07:38 -05004911 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004912 struct btrfs_ioctl_qgroup_assign_args *sa;
4913 struct btrfs_trans_handle *trans;
4914 int ret;
4915 int err;
4916
4917 if (!capable(CAP_SYS_ADMIN))
4918 return -EPERM;
4919
Miao Xie905b0dd2012-11-26 08:50:11 +00004920 ret = mnt_want_write_file(file);
4921 if (ret)
4922 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004923
4924 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004925 if (IS_ERR(sa)) {
4926 ret = PTR_ERR(sa);
4927 goto drop_write;
4928 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004929
4930 trans = btrfs_join_transaction(root);
4931 if (IS_ERR(trans)) {
4932 ret = PTR_ERR(trans);
4933 goto out;
4934 }
4935
4936 /* FIXME: check if the IDs really exist */
4937 if (sa->assign) {
4938 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4939 sa->src, sa->dst);
4940 } else {
4941 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4942 sa->src, sa->dst);
4943 }
4944
Qu Wenruoe082f562015-02-27 16:24:28 +08004945 /* update qgroup status and info */
4946 err = btrfs_run_qgroups(trans, root->fs_info);
4947 if (err < 0)
Anand Jainad8403d2016-03-10 12:22:15 +08004948 btrfs_handle_fs_error(root->fs_info, err,
4949 "failed to update qgroup status and info");
Arne Jansen5d13a372011-09-14 15:53:51 +02004950 err = btrfs_end_transaction(trans, root);
4951 if (err && !ret)
4952 ret = err;
4953
4954out:
4955 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004956drop_write:
4957 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004958 return ret;
4959}
4960
Miao Xie905b0dd2012-11-26 08:50:11 +00004961static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004962{
Al Viro496ad9a2013-01-23 17:07:38 -05004963 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004964 struct btrfs_ioctl_qgroup_create_args *sa;
4965 struct btrfs_trans_handle *trans;
4966 int ret;
4967 int err;
4968
4969 if (!capable(CAP_SYS_ADMIN))
4970 return -EPERM;
4971
Miao Xie905b0dd2012-11-26 08:50:11 +00004972 ret = mnt_want_write_file(file);
4973 if (ret)
4974 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004975
4976 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004977 if (IS_ERR(sa)) {
4978 ret = PTR_ERR(sa);
4979 goto drop_write;
4980 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004981
Miao Xied86e56c2012-11-15 11:35:41 +00004982 if (!sa->qgroupid) {
4983 ret = -EINVAL;
4984 goto out;
4985 }
4986
Arne Jansen5d13a372011-09-14 15:53:51 +02004987 trans = btrfs_join_transaction(root);
4988 if (IS_ERR(trans)) {
4989 ret = PTR_ERR(trans);
4990 goto out;
4991 }
4992
4993 /* FIXME: check if the IDs really exist */
4994 if (sa->create) {
Dongsheng Yang4087cf22015-01-18 10:59:23 -05004995 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004996 } else {
4997 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4998 }
4999
5000 err = btrfs_end_transaction(trans, root);
5001 if (err && !ret)
5002 ret = err;
5003
5004out:
5005 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005006drop_write:
5007 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005008 return ret;
5009}
5010
Miao Xie905b0dd2012-11-26 08:50:11 +00005011static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005012{
Al Viro496ad9a2013-01-23 17:07:38 -05005013 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005014 struct btrfs_ioctl_qgroup_limit_args *sa;
5015 struct btrfs_trans_handle *trans;
5016 int ret;
5017 int err;
5018 u64 qgroupid;
5019
5020 if (!capable(CAP_SYS_ADMIN))
5021 return -EPERM;
5022
Miao Xie905b0dd2012-11-26 08:50:11 +00005023 ret = mnt_want_write_file(file);
5024 if (ret)
5025 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005026
5027 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005028 if (IS_ERR(sa)) {
5029 ret = PTR_ERR(sa);
5030 goto drop_write;
5031 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005032
5033 trans = btrfs_join_transaction(root);
5034 if (IS_ERR(trans)) {
5035 ret = PTR_ERR(trans);
5036 goto out;
5037 }
5038
5039 qgroupid = sa->qgroupid;
5040 if (!qgroupid) {
5041 /* take the current subvol as qgroup */
5042 qgroupid = root->root_key.objectid;
5043 }
5044
5045 /* FIXME: check if the IDs really exist */
5046 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
5047
5048 err = btrfs_end_transaction(trans, root);
5049 if (err && !ret)
5050 ret = err;
5051
5052out:
5053 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005054drop_write:
5055 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005056 return ret;
5057}
5058
Jan Schmidt2f232032013-04-25 16:04:51 +00005059static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5060{
Al Viro6d0379e2013-06-16 19:32:35 +04005061 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00005062 struct btrfs_ioctl_quota_rescan_args *qsa;
5063 int ret;
5064
5065 if (!capable(CAP_SYS_ADMIN))
5066 return -EPERM;
5067
5068 ret = mnt_want_write_file(file);
5069 if (ret)
5070 return ret;
5071
5072 qsa = memdup_user(arg, sizeof(*qsa));
5073 if (IS_ERR(qsa)) {
5074 ret = PTR_ERR(qsa);
5075 goto drop_write;
5076 }
5077
5078 if (qsa->flags) {
5079 ret = -EINVAL;
5080 goto out;
5081 }
5082
5083 ret = btrfs_qgroup_rescan(root->fs_info);
5084
5085out:
5086 kfree(qsa);
5087drop_write:
5088 mnt_drop_write_file(file);
5089 return ret;
5090}
5091
5092static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5093{
Al Viro6d0379e2013-06-16 19:32:35 +04005094 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00005095 struct btrfs_ioctl_quota_rescan_args *qsa;
5096 int ret = 0;
5097
5098 if (!capable(CAP_SYS_ADMIN))
5099 return -EPERM;
5100
David Sterba8d2db782015-11-04 15:38:29 +01005101 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005102 if (!qsa)
5103 return -ENOMEM;
5104
5105 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5106 qsa->flags = 1;
5107 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
5108 }
5109
5110 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5111 ret = -EFAULT;
5112
5113 kfree(qsa);
5114 return ret;
5115}
5116
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005117static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5118{
Al Viro54563d42013-09-01 15:57:51 -04005119 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005120
5121 if (!capable(CAP_SYS_ADMIN))
5122 return -EPERM;
5123
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04005124 return btrfs_qgroup_wait_for_completion(root->fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005125}
5126
Hugo Millsabccd002014-01-30 20:17:00 +00005127static long _btrfs_ioctl_set_received_subvol(struct file *file,
5128 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005129{
Al Viro496ad9a2013-01-23 17:07:38 -05005130 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02005131 struct btrfs_root *root = BTRFS_I(inode)->root;
5132 struct btrfs_root_item *root_item = &root->root_item;
5133 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005134 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005135 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005136 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005137
David Sterbabd60ea02014-01-16 15:50:22 +01005138 if (!inode_owner_or_capable(inode))
5139 return -EPERM;
5140
Alexander Block8ea05e32012-07-25 17:35:53 +02005141 ret = mnt_want_write_file(file);
5142 if (ret < 0)
5143 return ret;
5144
5145 down_write(&root->fs_info->subvol_sem);
5146
5147 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
5148 ret = -EINVAL;
5149 goto out;
5150 }
5151
5152 if (btrfs_root_readonly(root)) {
5153 ret = -EROFS;
5154 goto out;
5155 }
5156
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005157 /*
5158 * 1 - root item
5159 * 2 - uuid items (received uuid + subvol uuid)
5160 */
5161 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005162 if (IS_ERR(trans)) {
5163 ret = PTR_ERR(trans);
5164 trans = NULL;
5165 goto out;
5166 }
5167
5168 sa->rtransid = trans->transid;
5169 sa->rtime.sec = ct.tv_sec;
5170 sa->rtime.nsec = ct.tv_nsec;
5171
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005172 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5173 BTRFS_UUID_SIZE);
5174 if (received_uuid_changed &&
5175 !btrfs_is_empty_uuid(root_item->received_uuid))
5176 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
5177 root_item->received_uuid,
5178 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5179 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005180 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5181 btrfs_set_root_stransid(root_item, sa->stransid);
5182 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005183 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5184 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5185 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5186 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005187
5188 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5189 &root->root_key, &root->root_item);
5190 if (ret < 0) {
5191 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02005192 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005193 }
5194 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5195 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
5196 sa->uuid,
5197 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5198 root->root_key.objectid);
5199 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005200 btrfs_abort_transaction(trans, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005201 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005202 }
5203 }
5204 ret = btrfs_commit_transaction(trans, root);
5205 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005206 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005207 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005208 }
5209
Hugo Millsabccd002014-01-30 20:17:00 +00005210out:
5211 up_write(&root->fs_info->subvol_sem);
5212 mnt_drop_write_file(file);
5213 return ret;
5214}
5215
5216#ifdef CONFIG_64BIT
5217static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5218 void __user *arg)
5219{
5220 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5221 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5222 int ret = 0;
5223
5224 args32 = memdup_user(arg, sizeof(*args32));
5225 if (IS_ERR(args32)) {
5226 ret = PTR_ERR(args32);
5227 args32 = NULL;
5228 goto out;
5229 }
5230
David Sterba8d2db782015-11-04 15:38:29 +01005231 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005232 if (!args64) {
5233 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005234 goto out;
5235 }
5236
5237 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5238 args64->stransid = args32->stransid;
5239 args64->rtransid = args32->rtransid;
5240 args64->stime.sec = args32->stime.sec;
5241 args64->stime.nsec = args32->stime.nsec;
5242 args64->rtime.sec = args32->rtime.sec;
5243 args64->rtime.nsec = args32->rtime.nsec;
5244 args64->flags = args32->flags;
5245
5246 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5247 if (ret)
5248 goto out;
5249
5250 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5251 args32->stransid = args64->stransid;
5252 args32->rtransid = args64->rtransid;
5253 args32->stime.sec = args64->stime.sec;
5254 args32->stime.nsec = args64->stime.nsec;
5255 args32->rtime.sec = args64->rtime.sec;
5256 args32->rtime.nsec = args64->rtime.nsec;
5257 args32->flags = args64->flags;
5258
5259 ret = copy_to_user(arg, args32, sizeof(*args32));
5260 if (ret)
5261 ret = -EFAULT;
5262
5263out:
5264 kfree(args32);
5265 kfree(args64);
5266 return ret;
5267}
5268#endif
5269
5270static long btrfs_ioctl_set_received_subvol(struct file *file,
5271 void __user *arg)
5272{
5273 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5274 int ret = 0;
5275
5276 sa = memdup_user(arg, sizeof(*sa));
5277 if (IS_ERR(sa)) {
5278 ret = PTR_ERR(sa);
5279 sa = NULL;
5280 goto out;
5281 }
5282
5283 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5284
5285 if (ret)
5286 goto out;
5287
Alexander Block8ea05e32012-07-25 17:35:53 +02005288 ret = copy_to_user(arg, sa, sizeof(*sa));
5289 if (ret)
5290 ret = -EFAULT;
5291
5292out:
5293 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005294 return ret;
5295}
5296
jeff.liu867ab662013-01-05 02:48:01 +00005297static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5298{
Al Viro6d0379e2013-06-16 19:32:35 +04005299 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005300 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005301 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005302 char label[BTRFS_LABEL_SIZE];
5303
5304 spin_lock(&root->fs_info->super_lock);
5305 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5306 spin_unlock(&root->fs_info->super_lock);
5307
5308 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005309
5310 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005311 btrfs_warn(root->fs_info,
5312 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00005313 }
5314
jeff.liu867ab662013-01-05 02:48:01 +00005315 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005316
5317 return ret ? -EFAULT : 0;
5318}
5319
jeff.liua8bfd4a2013-01-05 02:48:08 +00005320static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5321{
Al Viro6d0379e2013-06-16 19:32:35 +04005322 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005323 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5324 struct btrfs_trans_handle *trans;
5325 char label[BTRFS_LABEL_SIZE];
5326 int ret;
5327
5328 if (!capable(CAP_SYS_ADMIN))
5329 return -EPERM;
5330
5331 if (copy_from_user(label, arg, sizeof(label)))
5332 return -EFAULT;
5333
5334 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005335 btrfs_err(root->fs_info,
5336 "unable to set label with more than %d bytes",
5337 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005338 return -EINVAL;
5339 }
5340
5341 ret = mnt_want_write_file(file);
5342 if (ret)
5343 return ret;
5344
jeff.liua8bfd4a2013-01-05 02:48:08 +00005345 trans = btrfs_start_transaction(root, 0);
5346 if (IS_ERR(trans)) {
5347 ret = PTR_ERR(trans);
5348 goto out_unlock;
5349 }
5350
Anand Jaina1b83ac2013-07-19 17:39:32 +08005351 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005352 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005353 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005354 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005355
5356out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005357 mnt_drop_write_file(file);
5358 return ret;
5359}
5360
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005361#define INIT_FEATURE_FLAGS(suffix) \
5362 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5363 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5364 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5365
David Sterbad5131b62016-02-17 15:26:27 +01005366int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005367{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005368 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005369 INIT_FEATURE_FLAGS(SUPP),
5370 INIT_FEATURE_FLAGS(SAFE_SET),
5371 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5372 };
5373
5374 if (copy_to_user(arg, &features, sizeof(features)))
5375 return -EFAULT;
5376
5377 return 0;
5378}
5379
5380static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5381{
5382 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5383 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5384 struct btrfs_ioctl_feature_flags features;
5385
5386 features.compat_flags = btrfs_super_compat_flags(super_block);
5387 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5388 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5389
5390 if (copy_to_user(arg, &features, sizeof(features)))
5391 return -EFAULT;
5392
5393 return 0;
5394}
5395
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005396static int check_feature_bits(struct btrfs_root *root,
5397 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005398 u64 change_mask, u64 flags, u64 supported_flags,
5399 u64 safe_set, u64 safe_clear)
5400{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005401 const char *type = btrfs_feature_set_names[set];
5402 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005403 u64 disallowed, unsupported;
5404 u64 set_mask = flags & change_mask;
5405 u64 clear_mask = ~flags & change_mask;
5406
5407 unsupported = set_mask & ~supported_flags;
5408 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005409 names = btrfs_printable_features(set, unsupported);
5410 if (names) {
5411 btrfs_warn(root->fs_info,
5412 "this kernel does not support the %s feature bit%s",
5413 names, strchr(names, ',') ? "s" : "");
5414 kfree(names);
5415 } else
5416 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005417 "this kernel does not support %s bits 0x%llx",
5418 type, unsupported);
5419 return -EOPNOTSUPP;
5420 }
5421
5422 disallowed = set_mask & ~safe_set;
5423 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005424 names = btrfs_printable_features(set, disallowed);
5425 if (names) {
5426 btrfs_warn(root->fs_info,
5427 "can't set the %s feature bit%s while mounted",
5428 names, strchr(names, ',') ? "s" : "");
5429 kfree(names);
5430 } else
5431 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005432 "can't set %s bits 0x%llx while mounted",
5433 type, disallowed);
5434 return -EPERM;
5435 }
5436
5437 disallowed = clear_mask & ~safe_clear;
5438 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005439 names = btrfs_printable_features(set, disallowed);
5440 if (names) {
5441 btrfs_warn(root->fs_info,
5442 "can't clear the %s feature bit%s while mounted",
5443 names, strchr(names, ',') ? "s" : "");
5444 kfree(names);
5445 } else
5446 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005447 "can't clear %s bits 0x%llx while mounted",
5448 type, disallowed);
5449 return -EPERM;
5450 }
5451
5452 return 0;
5453}
5454
5455#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005456check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005457 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5458 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5459 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5460
5461static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5462{
5463 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5464 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5465 struct btrfs_ioctl_feature_flags flags[2];
5466 struct btrfs_trans_handle *trans;
5467 u64 newflags;
5468 int ret;
5469
5470 if (!capable(CAP_SYS_ADMIN))
5471 return -EPERM;
5472
5473 if (copy_from_user(flags, arg, sizeof(flags)))
5474 return -EFAULT;
5475
5476 /* Nothing to do */
5477 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5478 !flags[0].incompat_flags)
5479 return 0;
5480
5481 ret = check_feature(root, flags[0].compat_flags,
5482 flags[1].compat_flags, COMPAT);
5483 if (ret)
5484 return ret;
5485
5486 ret = check_feature(root, flags[0].compat_ro_flags,
5487 flags[1].compat_ro_flags, COMPAT_RO);
5488 if (ret)
5489 return ret;
5490
5491 ret = check_feature(root, flags[0].incompat_flags,
5492 flags[1].incompat_flags, INCOMPAT);
5493 if (ret)
5494 return ret;
5495
David Sterba7ab19622016-05-04 11:32:00 +02005496 ret = mnt_want_write_file(file);
5497 if (ret)
5498 return ret;
5499
David Sterba8051aa12014-02-07 14:34:04 +01005500 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005501 if (IS_ERR(trans)) {
5502 ret = PTR_ERR(trans);
5503 goto out_drop_write;
5504 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005505
5506 spin_lock(&root->fs_info->super_lock);
5507 newflags = btrfs_super_compat_flags(super_block);
5508 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5509 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5510 btrfs_set_super_compat_flags(super_block, newflags);
5511
5512 newflags = btrfs_super_compat_ro_flags(super_block);
5513 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5514 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5515 btrfs_set_super_compat_ro_flags(super_block, newflags);
5516
5517 newflags = btrfs_super_incompat_flags(super_block);
5518 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5519 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5520 btrfs_set_super_incompat_flags(super_block, newflags);
5521 spin_unlock(&root->fs_info->super_lock);
5522
David Sterba7ab19622016-05-04 11:32:00 +02005523 ret = btrfs_commit_transaction(trans, root);
5524out_drop_write:
5525 mnt_drop_write_file(file);
5526
5527 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005528}
5529
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005530long btrfs_ioctl(struct file *file, unsigned int
5531 cmd, unsigned long arg)
5532{
Al Viro496ad9a2013-01-23 17:07:38 -05005533 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005534 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005535
5536 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005537 case FS_IOC_GETFLAGS:
5538 return btrfs_ioctl_getflags(file, argp);
5539 case FS_IOC_SETFLAGS:
5540 return btrfs_ioctl_setflags(file, argp);
5541 case FS_IOC_GETVERSION:
5542 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005543 case FITRIM:
5544 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005545 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005546 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005547 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005548 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005549 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005550 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005551 case BTRFS_IOC_SUBVOL_CREATE_V2:
5552 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005553 case BTRFS_IOC_SNAP_DESTROY:
5554 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005555 case BTRFS_IOC_SUBVOL_GETFLAGS:
5556 return btrfs_ioctl_subvol_getflags(file, argp);
5557 case BTRFS_IOC_SUBVOL_SETFLAGS:
5558 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005559 case BTRFS_IOC_DEFAULT_SUBVOL:
5560 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005561 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005562 return btrfs_ioctl_defrag(file, NULL);
5563 case BTRFS_IOC_DEFRAG_RANGE:
5564 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005565 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005566 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005567 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005568 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005569 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005570 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005571 case BTRFS_IOC_RM_DEV_V2:
5572 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005573 case BTRFS_IOC_FS_INFO:
5574 return btrfs_ioctl_fs_info(root, argp);
5575 case BTRFS_IOC_DEV_INFO:
5576 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005577 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005578 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005579 case BTRFS_IOC_TRANS_START:
5580 return btrfs_ioctl_trans_start(file);
5581 case BTRFS_IOC_TRANS_END:
5582 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005583 case BTRFS_IOC_TREE_SEARCH:
5584 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005585 case BTRFS_IOC_TREE_SEARCH_V2:
5586 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005587 case BTRFS_IOC_INO_LOOKUP:
5588 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005589 case BTRFS_IOC_INO_PATHS:
5590 return btrfs_ioctl_ino_to_path(root, argp);
5591 case BTRFS_IOC_LOGICAL_INO:
5592 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005593 case BTRFS_IOC_SPACE_INFO:
5594 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005595 case BTRFS_IOC_SYNC: {
5596 int ret;
5597
Miao Xie6c255e62014-03-06 13:55:01 +08005598 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005599 if (ret)
5600 return ret;
Al Viroddb52f42014-10-21 20:21:18 -04005601 ret = btrfs_sync_fs(file_inode(file)->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005602 /*
5603 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005604 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005605 * processing uncleaned subvols.
5606 */
5607 wake_up_process(root->fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005608 return ret;
5609 }
Sage Weil46204592010-10-29 15:41:32 -04005610 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005611 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005612 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005613 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005614 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005615 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005616 case BTRFS_IOC_SCRUB_CANCEL:
5617 return btrfs_ioctl_scrub_cancel(root, argp);
5618 case BTRFS_IOC_SCRUB_PROGRESS:
5619 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005620 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005621 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005622 case BTRFS_IOC_BALANCE_CTL:
5623 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005624 case BTRFS_IOC_BALANCE_PROGRESS:
5625 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005626 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5627 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005628#ifdef CONFIG_64BIT
5629 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5630 return btrfs_ioctl_set_received_subvol_32(file, argp);
5631#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005632 case BTRFS_IOC_SEND:
5633 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005634 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06005635 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005636 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005637 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005638 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005639 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005640 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005641 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005642 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005643 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005644 case BTRFS_IOC_QUOTA_RESCAN:
5645 return btrfs_ioctl_quota_rescan(file, argp);
5646 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5647 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005648 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5649 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005650 case BTRFS_IOC_DEV_REPLACE:
5651 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005652 case BTRFS_IOC_GET_FSLABEL:
5653 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005654 case BTRFS_IOC_SET_FSLABEL:
5655 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005656 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005657 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005658 case BTRFS_IOC_GET_FEATURES:
5659 return btrfs_ioctl_get_features(file, argp);
5660 case BTRFS_IOC_SET_FEATURES:
5661 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005662 }
5663
5664 return -ENOTTY;
5665}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005666
5667#ifdef CONFIG_COMPAT
5668long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5669{
Jeff Mahoney93fb0432017-02-06 19:39:09 -05005670 /*
5671 * These all access 32-bit values anyway so no further
5672 * handling is necessary.
5673 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005674 switch (cmd) {
5675 case FS_IOC32_GETFLAGS:
5676 cmd = FS_IOC_GETFLAGS;
5677 break;
5678 case FS_IOC32_SETFLAGS:
5679 cmd = FS_IOC_SETFLAGS;
5680 break;
5681 case FS_IOC32_GETVERSION:
5682 cmd = FS_IOC_GETVERSION;
5683 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005684 }
5685
5686 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5687}
5688#endif