blob: 27fc660b0ff33e12f1e739af5daa48fa65dcc5da [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"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040062
Hugo Millsabccd002014-01-30 20:17:00 +000063#ifdef CONFIG_64BIT
64/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
65 * structures are incorrect, as the timespec structure from userspace
66 * is 4 bytes too small. We define these alternatives here to teach
67 * the kernel about the 32-bit struct packing.
68 */
69struct btrfs_ioctl_timespec_32 {
70 __u64 sec;
71 __u32 nsec;
72} __attribute__ ((__packed__));
73
74struct btrfs_ioctl_received_subvol_args_32 {
75 char uuid[BTRFS_UUID_SIZE]; /* in */
76 __u64 stransid; /* in */
77 __u64 rtransid; /* out */
78 struct btrfs_ioctl_timespec_32 stime; /* in */
79 struct btrfs_ioctl_timespec_32 rtime; /* out */
80 __u64 flags; /* in */
81 __u64 reserved[16]; /* in */
82} __attribute__ ((__packed__));
83
84#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
85 struct btrfs_ioctl_received_subvol_args_32)
86#endif
87
88
Mark Fasheh416161d2013-08-06 11:42:51 -070089static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070090 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
91 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070092
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093/* Mask out flags that are inappropriate for the given type of inode. */
94static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
95{
96 if (S_ISDIR(mode))
97 return flags;
98 else if (S_ISREG(mode))
99 return flags & ~FS_DIRSYNC_FL;
100 else
101 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
102}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400103
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200104/*
105 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
106 */
107static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
108{
109 unsigned int iflags = 0;
110
111 if (flags & BTRFS_INODE_SYNC)
112 iflags |= FS_SYNC_FL;
113 if (flags & BTRFS_INODE_IMMUTABLE)
114 iflags |= FS_IMMUTABLE_FL;
115 if (flags & BTRFS_INODE_APPEND)
116 iflags |= FS_APPEND_FL;
117 if (flags & BTRFS_INODE_NODUMP)
118 iflags |= FS_NODUMP_FL;
119 if (flags & BTRFS_INODE_NOATIME)
120 iflags |= FS_NOATIME_FL;
121 if (flags & BTRFS_INODE_DIRSYNC)
122 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000123 if (flags & BTRFS_INODE_NODATACOW)
124 iflags |= FS_NOCOW_FL;
125
126 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
127 iflags |= FS_COMPR_FL;
128 else if (flags & BTRFS_INODE_NOCOMPRESS)
129 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200130
131 return iflags;
132}
133
134/*
135 * Update inode->i_flags based on the btrfs internal flags.
136 */
137void btrfs_update_iflags(struct inode *inode)
138{
139 struct btrfs_inode *ip = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100140 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200141
142 if (ip->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100143 new_fl |= S_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200144 if (ip->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100145 new_fl |= S_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200146 if (ip->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100147 new_fl |= S_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200148 if (ip->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100149 new_fl |= S_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200150 if (ip->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100151 new_fl |= S_DIRSYNC;
152
153 set_mask_bits(&inode->i_flags,
154 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
155 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200156}
157
158/*
159 * Inherit flags from the parent inode.
160 *
Josef Bacike27425d2011-09-27 11:01:30 -0400161 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200162 */
163void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
164{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400165 unsigned int flags;
166
167 if (!dir)
168 return;
169
170 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200171
Josef Bacike27425d2011-09-27 11:01:30 -0400172 if (flags & BTRFS_INODE_NOCOMPRESS) {
173 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
174 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
175 } else if (flags & BTRFS_INODE_COMPRESS) {
176 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
177 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
178 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200179
Liu Bo213490b2012-09-11 08:33:50 -0600180 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400181 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600182 if (S_ISREG(inode->i_mode))
183 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
184 }
Josef Bacike27425d2011-09-27 11:01:30 -0400185
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200186 btrfs_update_iflags(inode);
187}
188
189static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
190{
Al Viro496ad9a2013-01-23 17:07:38 -0500191 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200192 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
193
194 if (copy_to_user(arg, &flags, sizeof(flags)))
195 return -EFAULT;
196 return 0;
197}
198
Liu Bo75e7cb72011-03-22 10:12:20 +0000199static int check_flags(unsigned int flags)
200{
201 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
202 FS_NOATIME_FL | FS_NODUMP_FL | \
203 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000204 FS_NOCOMP_FL | FS_COMPR_FL |
205 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000206 return -EOPNOTSUPP;
207
208 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
209 return -EINVAL;
210
Liu Bo75e7cb72011-03-22 10:12:20 +0000211 return 0;
212}
213
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200214static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
215{
Al Viro496ad9a2013-01-23 17:07:38 -0500216 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200217 struct btrfs_inode *ip = BTRFS_I(inode);
218 struct btrfs_root *root = ip->root;
219 struct btrfs_trans_handle *trans;
220 unsigned int flags, oldflags;
221 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800222 u64 ip_oldflags;
223 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600224 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200225
David Sterbabd60ea02014-01-16 15:50:22 +0100226 if (!inode_owner_or_capable(inode))
227 return -EPERM;
228
Li Zefanb83cc962010-12-20 16:04:08 +0800229 if (btrfs_root_readonly(root))
230 return -EROFS;
231
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200232 if (copy_from_user(&flags, arg, sizeof(flags)))
233 return -EFAULT;
234
Liu Bo75e7cb72011-03-22 10:12:20 +0000235 ret = check_flags(flags);
236 if (ret)
237 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238
Jan Karae7848682012-06-12 16:20:32 +0200239 ret = mnt_want_write_file(file);
240 if (ret)
241 return ret;
242
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200243 mutex_lock(&inode->i_mutex);
244
Li Zefanf062abf2011-12-29 13:36:45 +0800245 ip_oldflags = ip->flags;
246 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600247 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800248
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200249 flags = btrfs_mask_flags(inode->i_mode, flags);
250 oldflags = btrfs_flags_to_ioctl(ip->flags);
251 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
252 if (!capable(CAP_LINUX_IMMUTABLE)) {
253 ret = -EPERM;
254 goto out_unlock;
255 }
256 }
257
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200258 if (flags & FS_SYNC_FL)
259 ip->flags |= BTRFS_INODE_SYNC;
260 else
261 ip->flags &= ~BTRFS_INODE_SYNC;
262 if (flags & FS_IMMUTABLE_FL)
263 ip->flags |= BTRFS_INODE_IMMUTABLE;
264 else
265 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
266 if (flags & FS_APPEND_FL)
267 ip->flags |= BTRFS_INODE_APPEND;
268 else
269 ip->flags &= ~BTRFS_INODE_APPEND;
270 if (flags & FS_NODUMP_FL)
271 ip->flags |= BTRFS_INODE_NODUMP;
272 else
273 ip->flags &= ~BTRFS_INODE_NODUMP;
274 if (flags & FS_NOATIME_FL)
275 ip->flags |= BTRFS_INODE_NOATIME;
276 else
277 ip->flags &= ~BTRFS_INODE_NOATIME;
278 if (flags & FS_DIRSYNC_FL)
279 ip->flags |= BTRFS_INODE_DIRSYNC;
280 else
281 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600282 if (flags & FS_NOCOW_FL) {
283 if (S_ISREG(mode)) {
284 /*
285 * It's safe to turn csums off here, no extents exist.
286 * Otherwise we want the flag to reflect the real COW
287 * status of the file and will not set it.
288 */
289 if (inode->i_size == 0)
290 ip->flags |= BTRFS_INODE_NODATACOW
291 | BTRFS_INODE_NODATASUM;
292 } else {
293 ip->flags |= BTRFS_INODE_NODATACOW;
294 }
295 } else {
296 /*
297 * Revert back under same assuptions as above
298 */
299 if (S_ISREG(mode)) {
300 if (inode->i_size == 0)
301 ip->flags &= ~(BTRFS_INODE_NODATACOW
302 | BTRFS_INODE_NODATASUM);
303 } else {
304 ip->flags &= ~BTRFS_INODE_NODATACOW;
305 }
306 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200307
Liu Bo75e7cb72011-03-22 10:12:20 +0000308 /*
309 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
310 * flag may be changed automatically if compression code won't make
311 * things smaller.
312 */
313 if (flags & FS_NOCOMP_FL) {
314 ip->flags &= ~BTRFS_INODE_COMPRESS;
315 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000316
317 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
318 if (ret && ret != -ENODATA)
319 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000320 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000321 const char *comp;
322
Liu Bo75e7cb72011-03-22 10:12:20 +0000323 ip->flags |= BTRFS_INODE_COMPRESS;
324 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000325
326 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
327 comp = "lzo";
328 else
329 comp = "zlib";
330 ret = btrfs_set_prop(inode, "btrfs.compression",
331 comp, strlen(comp), 0);
332 if (ret)
333 goto out_drop;
334
Li Zefanebcb9042011-04-15 03:03:17 +0000335 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100336 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
337 if (ret && ret != -ENODATA)
338 goto out_drop;
Li Zefanebcb9042011-04-15 03:03:17 +0000339 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000340 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200341
Li Zefan4da6f1a2011-12-29 13:39:50 +0800342 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800343 if (IS_ERR(trans)) {
344 ret = PTR_ERR(trans);
345 goto out_drop;
346 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200347
Li Zefan306424cc2011-12-14 20:12:02 -0500348 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400349 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500350 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200351 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200352
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200353 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800354 out_drop:
355 if (ret) {
356 ip->flags = ip_oldflags;
357 inode->i_flags = i_oldflags;
358 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200359
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200360 out_unlock:
361 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200362 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000363 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200364}
365
366static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
367{
Al Viro496ad9a2013-01-23 17:07:38 -0500368 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200369
370 return put_user(inode->i_generation, arg);
371}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400372
Li Dongyangf7039b12011-03-24 10:24:28 +0000373static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
374{
Al Viro54563d42013-09-01 15:57:51 -0400375 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000376 struct btrfs_device *device;
377 struct request_queue *q;
378 struct fstrim_range range;
379 u64 minlen = ULLONG_MAX;
380 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500381 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000382 int ret;
383
384 if (!capable(CAP_SYS_ADMIN))
385 return -EPERM;
386
Xiao Guangrong1f781602011-04-20 10:09:16 +0000387 rcu_read_lock();
388 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
389 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000390 if (!device->bdev)
391 continue;
392 q = bdev_get_queue(device->bdev);
393 if (blk_queue_discard(q)) {
394 num_devices++;
395 minlen = min((u64)q->limits.discard_granularity,
396 minlen);
397 }
398 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000399 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200400
Li Dongyangf7039b12011-03-24 10:24:28 +0000401 if (!num_devices)
402 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000403 if (copy_from_user(&range, arg, sizeof(range)))
404 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000405 if (range.start > total_bytes ||
406 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200407 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000408
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200409 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000410 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500411 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000412 if (ret < 0)
413 return ret;
414
415 if (copy_to_user(arg, &range, sizeof(range)))
416 return -EFAULT;
417
418 return 0;
419}
420
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200421int btrfs_is_empty_uuid(u8 *uuid)
422{
Chris Mason46e0f662013-11-15 12:14:55 +0100423 int i;
424
425 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
426 if (uuid[i])
427 return 0;
428 }
429 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200430}
431
Miao Xied5c12072013-02-28 10:04:33 +0000432static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400433 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400434 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200435 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000436 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400437{
438 struct btrfs_trans_handle *trans;
439 struct btrfs_key key;
440 struct btrfs_root_item root_item;
441 struct btrfs_inode_item *inode_item;
442 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000443 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400444 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000445 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200446 struct timespec cur_time = CURRENT_TIME;
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900447 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400448 int ret;
449 int err;
450 u64 objectid;
451 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500452 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000453 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200454 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400455
Li Zefan581bb052011-04-20 10:06:11 +0800456 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400457 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400458 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000459
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800460 /*
461 * Don't create subvolume whose level is not zero. Or qgroup will be
462 * screwed up since it assume subvolme qgroup's level to be 0.
463 */
464 if (btrfs_qgroup_level(objectid))
465 return -ENOSPC;
466
Miao Xied5c12072013-02-28 10:04:33 +0000467 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400468 /*
Miao Xied5c12072013-02-28 10:04:33 +0000469 * The same as the snapshot creation, please see the comment
470 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400471 */
Miao Xied5c12072013-02-28 10:04:33 +0000472 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200473 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000474 if (ret)
475 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400476
Miao Xied5c12072013-02-28 10:04:33 +0000477 trans = btrfs_start_transaction(root, 0);
478 if (IS_ERR(trans)) {
479 ret = PTR_ERR(trans);
Liu Bode6e8202014-01-09 14:57:06 +0800480 btrfs_subvolume_release_metadata(root, &block_rsv,
481 qgroup_reserved);
482 return ret;
Miao Xied5c12072013-02-28 10:04:33 +0000483 }
484 trans->block_rsv = &block_rsv;
485 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400486
Miao Xie8696c532013-02-07 06:02:44 +0000487 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200488 if (ret)
489 goto fail;
490
David Sterba4d75f8a2014-06-15 01:54:12 +0200491 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400492 if (IS_ERR(leaf)) {
493 ret = PTR_ERR(leaf);
494 goto fail;
495 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400496
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400497 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400498 btrfs_set_header_bytenr(leaf, leaf->start);
499 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400500 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400501 btrfs_set_header_owner(leaf, objectid);
502
Ross Kirk0a4e5582013-09-24 10:12:38 +0100503 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400504 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400505 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200506 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400507 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400508 btrfs_mark_buffer_dirty(leaf);
509
Alexander Block8ea05e32012-07-25 17:35:53 +0200510 memset(&root_item, 0, sizeof(root_item));
511
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400512 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800513 btrfs_set_stack_inode_generation(inode_item, 1);
514 btrfs_set_stack_inode_size(inode_item, 3);
515 btrfs_set_stack_inode_nlink(inode_item, 1);
David Sterba707e8a02014-06-04 19:22:26 +0200516 btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800517 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400518
Qu Wenruo3cae2102013-07-16 11:19:18 +0800519 btrfs_set_root_flags(&root_item, 0);
520 btrfs_set_root_limit(&root_item, 0);
521 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000522
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400523 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400524 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525 btrfs_set_root_level(&root_item, 0);
526 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000527 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400528 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400529
Alexander Block8ea05e32012-07-25 17:35:53 +0200530 btrfs_set_root_generation_v2(&root_item,
531 btrfs_root_generation(&root_item));
532 uuid_le_gen(&new_uuid);
533 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800534 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
535 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200536 root_item.ctime = root_item.otime;
537 btrfs_set_root_ctransid(&root_item, trans->transid);
538 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539
Chris Mason925baed2008-06-25 16:01:30 -0400540 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400541 free_extent_buffer(leaf);
542 leaf = NULL;
543
544 btrfs_set_root_dirid(&root_item, new_dirid);
545
546 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400547 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200548 key.type = BTRFS_ROOT_ITEM_KEY;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400549 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
550 &root_item);
551 if (ret)
552 goto fail;
553
Yan, Zheng76dda932009-09-21 16:00:26 -0400554 key.offset = (u64)-1;
555 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100556 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 ret = PTR_ERR(new_root);
David Sterba6d13f542015-04-24 19:12:01 +0200558 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100559 goto fail;
560 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400561
562 btrfs_record_root_in_trans(trans, new_root);
563
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000564 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700565 if (ret) {
566 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100567 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700568 goto fail;
569 }
570
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400571 /*
572 * insert the directory item
573 */
Chris Mason3de45862008-11-17 21:02:50 -0500574 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100575 if (ret) {
576 btrfs_abort_transaction(trans, root, ret);
577 goto fail;
578 }
Chris Mason3de45862008-11-17 21:02:50 -0500579
580 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800581 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500582 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100583 if (ret) {
584 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400585 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100586 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500587
Yan Zheng52c26172009-01-05 15:43:43 -0500588 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
589 ret = btrfs_update_inode(trans, root, dir);
590 BUG_ON(ret);
591
Chris Mason0660b5a2008-11-17 20:37:39 -0500592 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400593 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800594 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500595 BUG_ON(ret);
596
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200597 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
598 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
599 objectid);
600 if (ret)
601 btrfs_abort_transaction(trans, root, ret);
602
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400603fail:
Miao Xied5c12072013-02-28 10:04:33 +0000604 trans->block_rsv = NULL;
605 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800606 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
607
Sage Weil72fd0322010-10-29 15:41:32 -0400608 if (async_transid) {
609 *async_transid = trans->transid;
610 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000611 if (err)
612 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400613 } else {
614 err = btrfs_commit_transaction(trans, root);
615 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400616 if (err && !ret)
617 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500618
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900619 if (!ret) {
620 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800621 if (IS_ERR(inode))
622 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900623 d_instantiate(dentry, inode);
624 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400625 return ret;
626}
627
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000628static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +0800629{
630 s64 writers;
631 DEFINE_WAIT(wait);
632
633 do {
634 prepare_to_wait(&root->subv_writers->wait, &wait,
635 TASK_UNINTERRUPTIBLE);
636
637 writers = percpu_counter_sum(&root->subv_writers->counter);
638 if (writers)
639 schedule();
640
641 finish_wait(&root->subv_writers->wait, &wait);
642 } while (writers);
643}
644
Miao Xiee9662f72013-02-28 10:01:15 +0000645static int create_snapshot(struct btrfs_root *root, struct inode *dir,
646 struct dentry *dentry, char *name, int namelen,
647 u64 *async_transid, bool readonly,
648 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400649{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000650 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400651 struct btrfs_pending_snapshot *pending_snapshot;
652 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000653 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654
Miao Xie27cdeb72014-04-02 19:51:05 +0800655 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656 return -EINVAL;
657
David Sterbaa1ee7362015-11-10 18:53:56 +0100658 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
659 if (!pending_snapshot)
660 return -ENOMEM;
661
David Sterbab0c0ea62015-11-10 18:54:00 +0100662 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
663 GFP_NOFS);
664 if (!pending_snapshot->root_item) {
665 ret = -ENOMEM;
666 goto free_pending;
667 }
668
Miao Xie8257b2d2014-03-06 13:38:19 +0800669 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100670 smp_mb__after_atomic();
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000671 btrfs_wait_for_no_snapshoting_writes(root);
Miao Xie8257b2d2014-03-06 13:38:19 +0800672
Miao Xie6a038432013-05-15 07:48:24 +0000673 ret = btrfs_start_delalloc_inodes(root, 0);
674 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100675 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000676
Miao Xieb0244192013-11-04 23:13:25 +0800677 btrfs_wait_ordered_extents(root, -1);
Miao Xie6a038432013-05-15 07:48:24 +0000678
Miao Xie66d8f3d2012-09-06 04:02:28 -0600679 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
680 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000681 /*
682 * 1 - parent dir inode
683 * 2 - dir entries
684 * 1 - root item
685 * 2 - root ref/backref
686 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200687 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000688 */
689 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200690 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400691 &pending_snapshot->qgroup_reserved,
692 false);
Miao Xied5c12072013-02-28 10:04:33 +0000693 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100694 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000695
Yan, Zhenga22285a2010-05-16 10:48:46 -0400696 pending_snapshot->dentry = dentry;
697 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800698 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000699 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000700 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400701
Miao Xied5c12072013-02-28 10:04:33 +0000702 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400703 if (IS_ERR(trans)) {
704 ret = PTR_ERR(trans);
705 goto fail;
706 }
707
Josef Bacik83515832011-06-14 15:16:14 -0400708 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400709 list_add(&pending_snapshot->list,
710 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400711 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400712 if (async_transid) {
713 *async_transid = trans->transid;
714 ret = btrfs_commit_transaction_async(trans,
715 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000716 if (ret)
717 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400718 } else {
719 ret = btrfs_commit_transaction(trans,
720 root->fs_info->extent_root);
721 }
Miao Xieaec80302013-03-04 09:44:29 +0000722 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400723 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400724
725 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000727 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400728
Chris Masond3797302014-10-15 13:50:56 -0700729 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
730 if (ret)
731 goto fail;
732
David Howells2b0143b2015-03-17 22:25:59 +0000733 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000734 if (IS_ERR(inode)) {
735 ret = PTR_ERR(inode);
736 goto fail;
737 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900738
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000739 d_instantiate(dentry, inode);
740 ret = 0;
741fail:
Miao Xied5c12072013-02-28 10:04:33 +0000742 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
743 &pending_snapshot->block_rsv,
744 pending_snapshot->qgroup_reserved);
David Sterbaa1ee7362015-11-10 18:53:56 +0100745dec_and_free:
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000746 if (atomic_dec_and_test(&root->will_be_snapshoted))
747 wake_up_atomic_t(&root->will_be_snapshoted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100748free_pending:
749 kfree(pending_snapshot->root_item);
David Sterbaa1ee7362015-11-10 18:53:56 +0100750 kfree(pending_snapshot);
751
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752 return ret;
753}
754
Sage Weil4260f7c2010-10-29 15:46:43 -0400755/* copy of may_delete in fs/namei.c()
756 * Check whether we can remove a link victim from directory dir, check
757 * whether the type of victim is right.
758 * 1. We can't do it if dir is read-only (done in permission())
759 * 2. We should have write and exec permissions on dir
760 * 3. We can't remove anything from append-only dir
761 * 4. We can't do anything with immutable dir (done in permission())
762 * 5. If the sticky bit on dir is set we should either
763 * a. be owner of dir, or
764 * b. be owner of victim, or
765 * c. have CAP_FOWNER capability
766 * 6. If the victim is append-only or immutable we can't do antyhing with
767 * links pointing to it.
768 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
769 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
770 * 9. We can't remove a root or mountpoint.
771 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
772 * nfs_async_unlink().
773 */
774
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530775static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400776{
777 int error;
778
David Howells2b0143b2015-03-17 22:25:59 +0000779 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400780 return -ENOENT;
781
David Howells2b0143b2015-03-17 22:25:59 +0000782 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400783 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400784
785 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
786 if (error)
787 return error;
788 if (IS_APPEND(dir))
789 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000790 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
791 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400792 return -EPERM;
793 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000794 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400795 return -ENOTDIR;
796 if (IS_ROOT(victim))
797 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000798 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400799 return -EISDIR;
800 if (IS_DEADDIR(dir))
801 return -ENOENT;
802 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
803 return -EBUSY;
804 return 0;
805}
806
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400807/* copy of may_create in fs/namei.c() */
808static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
809{
David Howells2b0143b2015-03-17 22:25:59 +0000810 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400811 return -EEXIST;
812 if (IS_DEADDIR(dir))
813 return -ENOENT;
814 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
815}
816
817/*
818 * Create a new subvolume below @parent. This is largely modeled after
819 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
820 * inside this filesystem so it's quite a bit simpler.
821 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400822static noinline int btrfs_mksubvol(struct path *parent,
823 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400824 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200825 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000826 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400827{
David Howells2b0143b2015-03-17 22:25:59 +0000828 struct inode *dir = d_inode(parent->dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400829 struct dentry *dentry;
830 int error;
831
David Sterba5c50c9b2013-03-22 18:12:51 +0000832 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
833 if (error == -EINTR)
834 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400835
836 dentry = lookup_one_len(name, parent->dentry, namelen);
837 error = PTR_ERR(dentry);
838 if (IS_ERR(dentry))
839 goto out_unlock;
840
841 error = -EEXIST;
David Howells2b0143b2015-03-17 22:25:59 +0000842 if (d_really_is_positive(dentry))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400843 goto out_dput;
844
Yan, Zheng76dda932009-09-21 16:00:26 -0400845 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400846 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600847 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400848
Chris Mason9c520572012-12-17 14:26:57 -0500849 /*
850 * even if this name doesn't exist, we may get hash collisions.
851 * check for them now when we can safely fail
852 */
853 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
854 dir->i_ino, name,
855 namelen);
856 if (error)
857 goto out_dput;
858
Yan, Zheng76dda932009-09-21 16:00:26 -0400859 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
860
861 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
862 goto out_up_read;
863
Chris Mason3de45862008-11-17 21:02:50 -0500864 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000865 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200866 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500867 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000868 error = create_subvol(dir, dentry, name, namelen,
869 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500870 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400871 if (!error)
872 fsnotify_mkdir(dir, dentry);
873out_up_read:
874 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400875out_dput:
876 dput(dentry);
877out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400878 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400879 return error;
880}
881
Chris Mason4cb53002011-05-24 15:35:30 -0400882/*
883 * When we're defragging a range, we don't want to kick it off again
884 * if it is really just waiting for delalloc to send it down.
885 * If we find a nice big extent or delalloc range for the bytes in the
886 * file you want to defrag, we return 0 to let you know to skip this
887 * part of the file
888 */
David Sterbaaab110a2014-07-29 17:32:10 +0200889static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400890{
891 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
892 struct extent_map *em = NULL;
893 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
894 u64 end;
895
896 read_lock(&em_tree->lock);
897 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
898 read_unlock(&em_tree->lock);
899
900 if (em) {
901 end = extent_map_end(em);
902 free_extent_map(em);
903 if (end - offset > thresh)
904 return 0;
905 }
906 /* if we already have a nice delalloc here, just stop */
907 thresh /= 2;
908 end = count_range_bits(io_tree, &offset, offset + thresh,
909 thresh, EXTENT_DELALLOC, 1);
910 if (end >= thresh)
911 return 0;
912 return 1;
913}
914
915/*
916 * helper function to walk through a file and find extents
917 * newer than a specific transid, and smaller than thresh.
918 *
919 * This is used by the defragging code to find new and small
920 * extents
921 */
922static int find_new_extents(struct btrfs_root *root,
923 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200924 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400925{
926 struct btrfs_path *path;
927 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400928 struct extent_buffer *leaf;
929 struct btrfs_file_extent_item *extent;
930 int type;
931 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000932 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400933
934 path = btrfs_alloc_path();
935 if (!path)
936 return -ENOMEM;
937
David Sterbaa4689d22011-05-31 17:08:14 +0000938 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400939 min_key.type = BTRFS_EXTENT_DATA_KEY;
940 min_key.offset = *off;
941
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530942 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100943 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400944 if (ret != 0)
945 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000946process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000947 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400948 goto none;
949 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
950 goto none;
951
952 leaf = path->nodes[0];
953 extent = btrfs_item_ptr(leaf, path->slots[0],
954 struct btrfs_file_extent_item);
955
956 type = btrfs_file_extent_type(leaf, extent);
957 if (type == BTRFS_FILE_EXTENT_REG &&
958 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
959 check_defrag_in_cache(inode, min_key.offset, thresh)) {
960 *off = min_key.offset;
961 btrfs_free_path(path);
962 return 0;
963 }
964
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000965 path->slots[0]++;
966 if (path->slots[0] < btrfs_header_nritems(leaf)) {
967 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
968 goto process_slot;
969 }
970
Chris Mason4cb53002011-05-24 15:35:30 -0400971 if (min_key.offset == (u64)-1)
972 goto none;
973
974 min_key.offset++;
975 btrfs_release_path(path);
976 }
977none:
978 btrfs_free_path(path);
979 return -ENOENT;
980}
981
Li Zefan6c282eb2012-06-11 16:03:35 +0800982static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400983{
984 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500985 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800986 struct extent_map *em;
987 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500988
989 /*
990 * hopefully we have this extent in the tree already, try without
991 * the full extent lock
992 */
993 read_lock(&em_tree->lock);
994 em = lookup_extent_mapping(em_tree, start, len);
995 read_unlock(&em_tree->lock);
996
997 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +0000998 struct extent_state *cached = NULL;
999 u64 end = start + len - 1;
1000
Chris Mason940100a2010-03-10 10:52:59 -05001001 /* get the big lock and read metadata off disk */
Filipe Manana308d9802014-03-11 13:56:15 +00001002 lock_extent_bits(io_tree, start, end, 0, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001003 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +00001004 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -05001005
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001006 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001007 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001008 }
1009
Li Zefan6c282eb2012-06-11 16:03:35 +08001010 return em;
1011}
1012
1013static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1014{
1015 struct extent_map *next;
1016 bool ret = true;
1017
1018 /* this is the last extent */
1019 if (em->start + em->len >= i_size_read(inode))
1020 return false;
1021
1022 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001023 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1024 ret = false;
1025 else if ((em->block_start + em->block_len == next->block_start) &&
1026 (em->block_len > 128 * 1024 && next->block_len > 128 * 1024))
Li Zefan6c282eb2012-06-11 16:03:35 +08001027 ret = false;
1028
1029 free_extent_map(next);
1030 return ret;
1031}
1032
David Sterbaaab110a2014-07-29 17:32:10 +02001033static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001034 u64 *last_len, u64 *skip, u64 *defrag_end,
1035 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001036{
1037 struct extent_map *em;
1038 int ret = 1;
1039 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001040 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001041
1042 /*
1043 * make sure that once we start defragging an extent, we keep on
1044 * defragging it
1045 */
1046 if (start < *defrag_end)
1047 return 1;
1048
1049 *skip = 0;
1050
1051 em = defrag_lookup_extent(inode, start);
1052 if (!em)
1053 return 0;
1054
Chris Mason940100a2010-03-10 10:52:59 -05001055 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001056 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001057 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001058 goto out;
1059 }
1060
Liu Bo4a3560c2015-08-07 16:48:41 +08001061 if (!*defrag_end)
1062 prev_mergeable = false;
1063
Li Zefan6c282eb2012-06-11 16:03:35 +08001064 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001065 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001066 * we hit a real extent, if it is big or the next extent is not a
1067 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001068 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001069 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001070 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001071 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001072out:
Chris Mason940100a2010-03-10 10:52:59 -05001073 /*
1074 * last_len ends up being a counter of how many bytes we've defragged.
1075 * every time we choose not to defrag an extent, we reset *last_len
1076 * so that the next tiny extent will force a defrag.
1077 *
1078 * The end result of this is that tiny extents before a single big
1079 * extent will force at least part of that big extent to be defragged.
1080 */
1081 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001082 *defrag_end = extent_map_end(em);
1083 } else {
1084 *last_len = 0;
1085 *skip = extent_map_end(em);
1086 *defrag_end = 0;
1087 }
1088
1089 free_extent_map(em);
1090 return ret;
1091}
1092
Chris Mason4cb53002011-05-24 15:35:30 -04001093/*
1094 * it doesn't do much good to defrag one or two pages
1095 * at a time. This pulls in a nice chunk of pages
1096 * to COW and defrag.
1097 *
1098 * It also makes sure the delalloc code has enough
1099 * dirty data to avoid making new small extents as part
1100 * of the defrag
1101 *
1102 * It's a good idea to start RA on this range
1103 * before calling this.
1104 */
1105static int cluster_pages_for_defrag(struct inode *inode,
1106 struct page **pages,
1107 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001108 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001109{
Chris Mason4cb53002011-05-24 15:35:30 -04001110 unsigned long file_end;
1111 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001112 u64 page_start;
1113 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001114 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001115 int ret;
1116 int i;
1117 int i_done;
1118 struct btrfs_ordered_extent *ordered;
1119 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001120 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001121 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001122
Chris Mason4cb53002011-05-24 15:35:30 -04001123 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001124 if (!isize || start_index > file_end)
1125 return 0;
1126
1127 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001128
1129 ret = btrfs_delalloc_reserve_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001130 start_index << PAGE_CACHE_SHIFT,
1131 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001132 if (ret)
1133 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001134 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001135 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001136
1137 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001138 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001139 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001140again:
Josef Bacika94733d2011-07-11 10:47:06 -04001141 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001142 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001143 if (!page)
1144 break;
1145
Miao Xie600a45e2012-02-16 15:01:24 +08001146 page_start = page_offset(page);
1147 page_end = page_start + PAGE_CACHE_SIZE - 1;
1148 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001149 lock_extent_bits(tree, page_start, page_end,
1150 0, &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001151 ordered = btrfs_lookup_ordered_extent(inode,
1152 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001153 unlock_extent_cached(tree, page_start, page_end,
1154 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001155 if (!ordered)
1156 break;
1157
1158 unlock_page(page);
1159 btrfs_start_ordered_extent(inode, ordered, 1);
1160 btrfs_put_ordered_extent(ordered);
1161 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001162 /*
1163 * we unlocked the page above, so we need check if
1164 * it was released or not.
1165 */
1166 if (page->mapping != inode->i_mapping) {
1167 unlock_page(page);
1168 page_cache_release(page);
1169 goto again;
1170 }
Miao Xie600a45e2012-02-16 15:01:24 +08001171 }
1172
Chris Mason4cb53002011-05-24 15:35:30 -04001173 if (!PageUptodate(page)) {
1174 btrfs_readpage(NULL, page);
1175 lock_page(page);
1176 if (!PageUptodate(page)) {
1177 unlock_page(page);
1178 page_cache_release(page);
1179 ret = -EIO;
1180 break;
1181 }
1182 }
Miao Xie600a45e2012-02-16 15:01:24 +08001183
Miao Xie600a45e2012-02-16 15:01:24 +08001184 if (page->mapping != inode->i_mapping) {
1185 unlock_page(page);
1186 page_cache_release(page);
1187 goto again;
1188 }
1189
Chris Mason4cb53002011-05-24 15:35:30 -04001190 pages[i] = page;
1191 i_done++;
1192 }
1193 if (!i_done || ret)
1194 goto out;
1195
1196 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1197 goto out;
1198
1199 /*
1200 * so now we have a nice long stream of locked
1201 * and up to date pages, lets wait on them
1202 */
1203 for (i = 0; i < i_done; i++)
1204 wait_on_page_writeback(pages[i]);
1205
1206 page_start = page_offset(pages[0]);
1207 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1208
1209 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001210 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001211 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1212 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001213 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1214 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001215
Liu Bo1f12bd02012-03-29 09:57:44 -04001216 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001217 spin_lock(&BTRFS_I(inode)->lock);
1218 BTRFS_I(inode)->outstanding_extents++;
1219 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001220 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001221 start_index << PAGE_CACHE_SHIFT,
1222 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001223 }
1224
1225
Liu Bo9e8a4a82012-09-05 19:10:51 -06001226 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1227 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001228
1229 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1230 page_start, page_end - 1, &cached_state,
1231 GFP_NOFS);
1232
1233 for (i = 0; i < i_done; i++) {
1234 clear_page_dirty_for_io(pages[i]);
1235 ClearPageChecked(pages[i]);
1236 set_page_extent_mapped(pages[i]);
1237 set_page_dirty(pages[i]);
1238 unlock_page(pages[i]);
1239 page_cache_release(pages[i]);
1240 }
1241 return i_done;
1242out:
1243 for (i = 0; i < i_done; i++) {
1244 unlock_page(pages[i]);
1245 page_cache_release(pages[i]);
1246 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001247 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001248 start_index << PAGE_CACHE_SHIFT,
1249 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001250 return ret;
1251
1252}
1253
1254int btrfs_defrag_file(struct inode *inode, struct file *file,
1255 struct btrfs_ioctl_defrag_range_args *range,
1256 u64 newer_than, unsigned long max_to_defrag)
1257{
1258 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001259 struct file_ra_state *ra = NULL;
1260 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001261 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001262 u64 last_len = 0;
1263 u64 skip = 0;
1264 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001265 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001266 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001267 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001268 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001269 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001270 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001271 u32 extent_thresh = range->extent_thresh;
Justin Maggardc41570c2014-01-21 11:18:29 -08001272 unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1273 unsigned long cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001274 u64 new_align = ~((u64)128 * 1024 - 1);
1275 struct page **pages = NULL;
1276
Liu Bo0abd5b12013-04-16 09:20:28 +00001277 if (isize == 0)
1278 return 0;
1279
1280 if (range->start >= isize)
1281 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001282
1283 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1284 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1285 return -EINVAL;
1286 if (range->compress_type)
1287 compress_type = range->compress_type;
1288 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001289
Liu Bo0abd5b12013-04-16 09:20:28 +00001290 if (extent_thresh == 0)
1291 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001292
Chris Mason4cb53002011-05-24 15:35:30 -04001293 /*
1294 * if we were not given a file, allocate a readahead
1295 * context
1296 */
1297 if (!file) {
1298 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1299 if (!ra)
1300 return -ENOMEM;
1301 file_ra_state_init(ra, inode->i_mapping);
1302 } else {
1303 ra = &file->f_ra;
1304 }
1305
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301306 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001307 GFP_NOFS);
1308 if (!pages) {
1309 ret = -ENOMEM;
1310 goto out_ra;
1311 }
1312
1313 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001314 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001315 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001316 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1317 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001318 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001319 }
1320
Chris Mason4cb53002011-05-24 15:35:30 -04001321 if (newer_than) {
1322 ret = find_new_extents(root, inode, newer_than,
1323 &newer_off, 64 * 1024);
1324 if (!ret) {
1325 range->start = newer_off;
1326 /*
1327 * we always align our defrag to help keep
1328 * the extents in the file evenly spaced
1329 */
1330 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001331 } else
1332 goto out_ra;
1333 } else {
1334 i = range->start >> PAGE_CACHE_SHIFT;
1335 }
1336 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301337 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001338
Li Zefan2a0f7f52011-10-10 15:43:34 -04001339 /*
1340 * make writeback starts from i, so the defrag range can be
1341 * written sequentially.
1342 */
1343 if (i < inode->i_mapping->writeback_index)
1344 inode->i_mapping->writeback_index = i;
1345
Chris Masonf7f43cc2011-10-11 11:41:40 -04001346 while (i <= last_index && defrag_count < max_to_defrag &&
David Sterbaed6078f2014-06-05 01:59:57 +02001347 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001348 /*
1349 * make sure we stop running if someone unmounts
1350 * the FS
1351 */
1352 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1353 break;
1354
David Sterba210549e2013-02-09 23:38:06 +00001355 if (btrfs_defrag_cancelled(root->fs_info)) {
David Sterbaf14d1042015-10-08 11:37:06 +02001356 btrfs_debug(root->fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001357 ret = -EAGAIN;
1358 break;
1359 }
1360
Liu Bo66c26892012-03-29 09:57:45 -04001361 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001362 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001363 &defrag_end, range->flags &
1364 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001365 unsigned long next;
1366 /*
1367 * the should_defrag function tells us how much to skip
1368 * bump our counter by the suggested amount
1369 */
David Sterbaed6078f2014-06-05 01:59:57 +02001370 next = DIV_ROUND_UP(skip, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001371 i = max(i + 1, next);
1372 continue;
1373 }
Li Zefan008873e2011-09-02 15:57:07 +08001374
1375 if (!newer_than) {
1376 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1377 PAGE_CACHE_SHIFT) - i;
1378 cluster = min(cluster, max_cluster);
1379 } else {
1380 cluster = max_cluster;
1381 }
1382
Li Zefan008873e2011-09-02 15:57:07 +08001383 if (i + cluster > ra_index) {
1384 ra_index = max(i, ra_index);
1385 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1386 cluster);
chandane4826a52015-06-09 17:38:32 +05301387 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001388 }
Chris Mason940100a2010-03-10 10:52:59 -05001389
Liu Boecb8bea2012-03-29 09:57:44 -04001390 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001391 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1392 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001393 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001394 if (ret < 0) {
1395 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001396 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001397 }
Chris Mason940100a2010-03-10 10:52:59 -05001398
Chris Mason4cb53002011-05-24 15:35:30 -04001399 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001400 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001401 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001402
1403 if (newer_than) {
1404 if (newer_off == (u64)-1)
1405 break;
1406
Liu Boe1f041e2012-03-29 09:57:45 -04001407 if (ret > 0)
1408 i += ret;
1409
Chris Mason4cb53002011-05-24 15:35:30 -04001410 newer_off = max(newer_off + 1,
1411 (u64)i << PAGE_CACHE_SHIFT);
1412
1413 ret = find_new_extents(root, inode,
1414 newer_than, &newer_off,
1415 64 * 1024);
1416 if (!ret) {
1417 range->start = newer_off;
1418 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001419 } else {
1420 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001421 }
Chris Mason4cb53002011-05-24 15:35:30 -04001422 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001423 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001424 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001425 last_len += ret << PAGE_CACHE_SHIFT;
1426 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001427 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001428 last_len = 0;
1429 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001430 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431 }
1432
Filipe Mananadec8ef92014-03-01 10:55:54 +00001433 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001434 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001435 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1436 &BTRFS_I(inode)->runtime_flags))
1437 filemap_flush(inode->i_mapping);
1438 }
Chris Mason1e701a32010-03-11 09:42:04 -05001439
1440 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1441 /* the filemap_flush will queue IO into the worker threads, but
1442 * we have to make sure the IO is actually started and that
1443 * ordered extents get created before we return
1444 */
1445 atomic_inc(&root->fs_info->async_submit_draining);
1446 while (atomic_read(&root->fs_info->nr_async_submits) ||
1447 atomic_read(&root->fs_info->async_delalloc_pages)) {
1448 wait_event(root->fs_info->async_submit_wait,
1449 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1450 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1451 }
1452 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001453 }
1454
Li Zefan1a419d82010-10-25 15:12:50 +08001455 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001456 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001457 }
1458
Diego Calleja60ccf822011-09-01 16:33:57 +02001459 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001460
Chris Mason4cb53002011-05-24 15:35:30 -04001461out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001462 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1463 mutex_lock(&inode->i_mutex);
1464 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1465 mutex_unlock(&inode->i_mutex);
1466 }
Chris Mason4cb53002011-05-24 15:35:30 -04001467 if (!file)
1468 kfree(ra);
1469 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001470 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001471}
1472
Miao Xie198605a2012-11-26 08:43:45 +00001473static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001474 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001475{
1476 u64 new_size;
1477 u64 old_size;
1478 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001479 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001480 struct btrfs_ioctl_vol_args *vol_args;
1481 struct btrfs_trans_handle *trans;
1482 struct btrfs_device *device = NULL;
1483 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001484 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001485 char *devstr = NULL;
1486 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001487 int mod = 0;
1488
Chris Masone441d542009-01-05 16:57:23 -05001489 if (!capable(CAP_SYS_ADMIN))
1490 return -EPERM;
1491
Miao Xie198605a2012-11-26 08:43:45 +00001492 ret = mnt_want_write_file(file);
1493 if (ret)
1494 return ret;
1495
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001496 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1497 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001498 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001499 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001500 }
1501
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001502 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001503 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001504 if (IS_ERR(vol_args)) {
1505 ret = PTR_ERR(vol_args);
1506 goto out;
1507 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001508
1509 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001510
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001511 sizestr = vol_args->name;
1512 devstr = strchr(sizestr, ':');
1513 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001514 sizestr = devstr + 1;
1515 *devstr = '\0';
1516 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001517 ret = kstrtoull(devstr, 10, &devid);
1518 if (ret)
1519 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001520 if (!devid) {
1521 ret = -EINVAL;
1522 goto out_free;
1523 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001524 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001525 }
Miao Xiedba60f32012-12-21 09:19:51 +00001526
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001527 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001528 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001529 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001530 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001531 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001532 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001533 }
Miao Xiedba60f32012-12-21 09:19:51 +00001534
1535 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001536 btrfs_info(root->fs_info,
1537 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001538 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001539 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001540 goto out_free;
1541 }
1542
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001543 if (!strcmp(sizestr, "max"))
1544 new_size = device->bdev->bd_inode->i_size;
1545 else {
1546 if (sizestr[0] == '-') {
1547 mod = -1;
1548 sizestr++;
1549 } else if (sizestr[0] == '+') {
1550 mod = 1;
1551 sizestr++;
1552 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001553 new_size = memparse(sizestr, &retptr);
1554 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001555 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001556 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001557 }
1558 }
1559
Stefan Behrens63a212a2012-11-05 18:29:28 +01001560 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001561 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001562 goto out_free;
1563 }
1564
Miao Xie7cc8e582014-09-03 21:35:38 +08001565 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001566
1567 if (mod < 0) {
1568 if (new_size > old_size) {
1569 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001570 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001571 }
1572 new_size = old_size - new_size;
1573 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001574 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001575 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001576 goto out_free;
1577 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001578 new_size = old_size + new_size;
1579 }
1580
1581 if (new_size < 256 * 1024 * 1024) {
1582 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001583 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001584 }
1585 if (new_size > device->bdev->bd_inode->i_size) {
1586 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001587 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001588 }
1589
David Sterbab8b93ad2015-01-16 17:26:13 +01001590 new_size = div_u64(new_size, root->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001591 new_size *= root->sectorsize;
1592
David Sterbaecaeb142015-10-08 09:01:03 +02001593 btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001594 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001595
1596 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001597 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001598 if (IS_ERR(trans)) {
1599 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001600 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001601 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001602 ret = btrfs_grow_device(trans, device, new_size);
1603 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001604 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001605 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001606 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001607
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001608out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001609 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001610out:
1611 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001612 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001613 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614 return ret;
1615}
1616
Sage Weil72fd0322010-10-29 15:41:32 -04001617static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001618 char *name, unsigned long fd, int subvol,
1619 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001620 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001621{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001622 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001623 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001624
Liu Boa874a632012-06-29 03:58:46 -06001625 ret = mnt_want_write_file(file);
1626 if (ret)
1627 goto out;
1628
Sage Weil72fd0322010-10-29 15:41:32 -04001629 namelen = strlen(name);
1630 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001631 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001632 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 }
1634
Chris Mason16780ca2012-02-20 22:14:55 -05001635 if (name[0] == '.' &&
1636 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1637 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001638 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001639 }
1640
Chris Mason3de45862008-11-17 21:02:50 -05001641 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001642 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001643 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001644 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001645 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001646 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001647 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001648 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001649 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001650 }
1651
Al Viro496ad9a2013-01-23 17:07:38 -05001652 src_inode = file_inode(src.file);
1653 if (src_inode->i_sb != file_inode(file)->i_sb) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001654 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1655 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001656 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001657 } else if (!inode_owner_or_capable(src_inode)) {
1658 /*
1659 * Subvolume creation is not restricted, but snapshots
1660 * are limited to own subvolumes only
1661 */
1662 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001663 } else {
1664 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1665 BTRFS_I(src_inode)->root,
1666 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001667 }
Al Viro2903ff02012-08-28 12:52:22 -04001668 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001669 }
Liu Boa874a632012-06-29 03:58:46 -06001670out_drop_write:
1671 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001672out:
Sage Weil72fd0322010-10-29 15:41:32 -04001673 return ret;
1674}
1675
1676static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001677 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001678{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001679 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001680 int ret;
1681
Li Zefanfa0d2b92010-12-20 15:53:28 +08001682 vol_args = memdup_user(arg, sizeof(*vol_args));
1683 if (IS_ERR(vol_args))
1684 return PTR_ERR(vol_args);
1685 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001686
Li Zefanfa0d2b92010-12-20 15:53:28 +08001687 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001688 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001689 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001690
Li Zefanfa0d2b92010-12-20 15:53:28 +08001691 kfree(vol_args);
1692 return ret;
1693}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001694
Li Zefanfa0d2b92010-12-20 15:53:28 +08001695static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1696 void __user *arg, int subvol)
1697{
1698 struct btrfs_ioctl_vol_args_v2 *vol_args;
1699 int ret;
1700 u64 transid = 0;
1701 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001702 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001703 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001704
Li Zefanfa0d2b92010-12-20 15:53:28 +08001705 vol_args = memdup_user(arg, sizeof(*vol_args));
1706 if (IS_ERR(vol_args))
1707 return PTR_ERR(vol_args);
1708 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001709
Li Zefanb83cc962010-12-20 16:04:08 +08001710 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001711 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1712 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001713 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001714 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001715 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001716
1717 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1718 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001719 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1720 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001721 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1722 if (vol_args->size > PAGE_CACHE_SIZE) {
1723 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001724 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001725 }
1726 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1727 if (IS_ERR(inherit)) {
1728 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001729 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001730 }
1731 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001732
1733 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001734 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001735 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001736 if (ret)
1737 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001738
Dan Carpenterc47ca322014-09-04 14:09:15 +03001739 if (ptr && copy_to_user(arg +
1740 offsetof(struct btrfs_ioctl_vol_args_v2,
1741 transid),
1742 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001743 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001744
1745free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001746 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001747free_args:
1748 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001749 return ret;
1750}
1751
Li Zefan0caa1022010-12-20 16:30:25 +08001752static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1753 void __user *arg)
1754{
Al Viro496ad9a2013-01-23 17:07:38 -05001755 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001756 struct btrfs_root *root = BTRFS_I(inode)->root;
1757 int ret = 0;
1758 u64 flags = 0;
1759
Li Zefan33345d012011-04-20 10:31:50 +08001760 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001761 return -EINVAL;
1762
1763 down_read(&root->fs_info->subvol_sem);
1764 if (btrfs_root_readonly(root))
1765 flags |= BTRFS_SUBVOL_RDONLY;
1766 up_read(&root->fs_info->subvol_sem);
1767
1768 if (copy_to_user(arg, &flags, sizeof(flags)))
1769 ret = -EFAULT;
1770
1771 return ret;
1772}
1773
1774static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1775 void __user *arg)
1776{
Al Viro496ad9a2013-01-23 17:07:38 -05001777 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001778 struct btrfs_root *root = BTRFS_I(inode)->root;
1779 struct btrfs_trans_handle *trans;
1780 u64 root_flags;
1781 u64 flags;
1782 int ret = 0;
1783
David Sterbabd60ea02014-01-16 15:50:22 +01001784 if (!inode_owner_or_capable(inode))
1785 return -EPERM;
1786
Liu Bob9ca0662012-06-29 03:58:49 -06001787 ret = mnt_want_write_file(file);
1788 if (ret)
1789 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001790
Liu Bob9ca0662012-06-29 03:58:49 -06001791 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1792 ret = -EINVAL;
1793 goto out_drop_write;
1794 }
Li Zefan0caa1022010-12-20 16:30:25 +08001795
Liu Bob9ca0662012-06-29 03:58:49 -06001796 if (copy_from_user(&flags, arg, sizeof(flags))) {
1797 ret = -EFAULT;
1798 goto out_drop_write;
1799 }
Li Zefan0caa1022010-12-20 16:30:25 +08001800
Liu Bob9ca0662012-06-29 03:58:49 -06001801 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1802 ret = -EINVAL;
1803 goto out_drop_write;
1804 }
Li Zefan0caa1022010-12-20 16:30:25 +08001805
Liu Bob9ca0662012-06-29 03:58:49 -06001806 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1807 ret = -EOPNOTSUPP;
1808 goto out_drop_write;
1809 }
Li Zefan0caa1022010-12-20 16:30:25 +08001810
1811 down_write(&root->fs_info->subvol_sem);
1812
1813 /* nothing to do */
1814 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001815 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001816
1817 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001818 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001819 btrfs_set_root_flags(&root->root_item,
1820 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001821 } else {
1822 /*
1823 * Block RO -> RW transition if this subvolume is involved in
1824 * send
1825 */
1826 spin_lock(&root->root_item_lock);
1827 if (root->send_in_progress == 0) {
1828 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001829 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001830 spin_unlock(&root->root_item_lock);
1831 } else {
1832 spin_unlock(&root->root_item_lock);
1833 btrfs_warn(root->fs_info,
1834 "Attempt to set subvolume %llu read-write during send",
1835 root->root_key.objectid);
1836 ret = -EPERM;
1837 goto out_drop_sem;
1838 }
1839 }
Li Zefan0caa1022010-12-20 16:30:25 +08001840
1841 trans = btrfs_start_transaction(root, 1);
1842 if (IS_ERR(trans)) {
1843 ret = PTR_ERR(trans);
1844 goto out_reset;
1845 }
1846
Li Zefanb4dc2b82011-02-16 06:06:34 +00001847 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001848 &root->root_key, &root->root_item);
1849
1850 btrfs_commit_transaction(trans, root);
1851out_reset:
1852 if (ret)
1853 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001854out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001855 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001856out_drop_write:
1857 mnt_drop_write_file(file);
1858out:
Li Zefan0caa1022010-12-20 16:30:25 +08001859 return ret;
1860}
1861
Yan, Zheng76dda932009-09-21 16:00:26 -04001862/*
1863 * helper to check if the subvolume references other subvolumes
1864 */
1865static noinline int may_destroy_subvol(struct btrfs_root *root)
1866{
1867 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001868 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001869 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001870 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001871 int ret;
1872
1873 path = btrfs_alloc_path();
1874 if (!path)
1875 return -ENOMEM;
1876
Josef Bacik175a2b82013-08-12 15:36:44 -04001877 /* Make sure this root isn't set as the default subvol */
1878 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1879 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1880 dir_id, "default", 7, 0);
1881 if (di && !IS_ERR(di)) {
1882 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1883 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001884 ret = -EPERM;
1885 btrfs_err(root->fs_info, "deleting default subvolume "
1886 "%llu is not allowed", key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001887 goto out;
1888 }
1889 btrfs_release_path(path);
1890 }
1891
Yan, Zheng76dda932009-09-21 16:00:26 -04001892 key.objectid = root->root_key.objectid;
1893 key.type = BTRFS_ROOT_REF_KEY;
1894 key.offset = (u64)-1;
1895
1896 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1897 &key, path, 0, 0);
1898 if (ret < 0)
1899 goto out;
1900 BUG_ON(ret == 0);
1901
1902 ret = 0;
1903 if (path->slots[0] > 0) {
1904 path->slots[0]--;
1905 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1906 if (key.objectid == root->root_key.objectid &&
1907 key.type == BTRFS_ROOT_REF_KEY)
1908 ret = -ENOTEMPTY;
1909 }
1910out:
1911 btrfs_free_path(path);
1912 return ret;
1913}
1914
Chris Masonac8e9812010-02-28 15:39:26 -05001915static noinline int key_in_sk(struct btrfs_key *key,
1916 struct btrfs_ioctl_search_key *sk)
1917{
Chris Masonabc6e132010-03-18 12:10:08 -04001918 struct btrfs_key test;
1919 int ret;
1920
1921 test.objectid = sk->min_objectid;
1922 test.type = sk->min_type;
1923 test.offset = sk->min_offset;
1924
1925 ret = btrfs_comp_cpu_keys(key, &test);
1926 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001927 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001928
1929 test.objectid = sk->max_objectid;
1930 test.type = sk->max_type;
1931 test.offset = sk->max_offset;
1932
1933 ret = btrfs_comp_cpu_keys(key, &test);
1934 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001935 return 0;
1936 return 1;
1937}
1938
1939static noinline int copy_to_sk(struct btrfs_root *root,
1940 struct btrfs_path *path,
1941 struct btrfs_key *key,
1942 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001943 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001944 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001945 unsigned long *sk_offset,
1946 int *num_found)
1947{
1948 u64 found_transid;
1949 struct extent_buffer *leaf;
1950 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001951 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001952 unsigned long item_off;
1953 unsigned long item_len;
1954 int nritems;
1955 int i;
1956 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001957 int ret = 0;
1958
1959 leaf = path->nodes[0];
1960 slot = path->slots[0];
1961 nritems = btrfs_header_nritems(leaf);
1962
1963 if (btrfs_header_generation(leaf) > sk->max_transid) {
1964 i = nritems;
1965 goto advance_key;
1966 }
1967 found_transid = btrfs_header_generation(leaf);
1968
1969 for (i = slot; i < nritems; i++) {
1970 item_off = btrfs_item_ptr_offset(leaf, i);
1971 item_len = btrfs_item_size_nr(leaf, i);
1972
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001973 btrfs_item_key_to_cpu(leaf, key, i);
1974 if (!key_in_sk(key, sk))
1975 continue;
1976
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001977 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001978 if (*num_found) {
1979 ret = 1;
1980 goto out;
1981 }
Chris Masonac8e9812010-02-28 15:39:26 -05001982
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001983 /*
1984 * return one empty item back for v1, which does not
1985 * handle -EOVERFLOW
1986 */
1987
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001988 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001989 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001990 ret = -EOVERFLOW;
1991 }
Chris Masonac8e9812010-02-28 15:39:26 -05001992
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001993 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05001994 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001995 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001996 }
1997
Chris Masonac8e9812010-02-28 15:39:26 -05001998 sh.objectid = key->objectid;
1999 sh.offset = key->offset;
2000 sh.type = key->type;
2001 sh.len = item_len;
2002 sh.transid = found_transid;
2003
2004 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002005 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2006 ret = -EFAULT;
2007 goto out;
2008 }
2009
Chris Masonac8e9812010-02-28 15:39:26 -05002010 *sk_offset += sizeof(sh);
2011
2012 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002013 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002014 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002015 if (read_extent_buffer_to_user(leaf, up,
2016 item_off, item_len)) {
2017 ret = -EFAULT;
2018 goto out;
2019 }
2020
Chris Masonac8e9812010-02-28 15:39:26 -05002021 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002022 }
Hugo Millse2156862011-05-14 17:43:41 +00002023 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002024
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002025 if (ret) /* -EOVERFLOW from above */
2026 goto out;
2027
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002028 if (*num_found >= sk->nr_items) {
2029 ret = 1;
2030 goto out;
2031 }
Chris Masonac8e9812010-02-28 15:39:26 -05002032 }
2033advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002034 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002035 test.objectid = sk->max_objectid;
2036 test.type = sk->max_type;
2037 test.offset = sk->max_offset;
2038 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2039 ret = 1;
2040 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002041 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002042 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002043 key->offset = 0;
2044 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002045 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002046 key->offset = 0;
2047 key->type = 0;
2048 key->objectid++;
2049 } else
2050 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002051out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002052 /*
2053 * 0: all items from this leaf copied, continue with next
2054 * 1: * more items can be copied, but unused buffer is too small
2055 * * all items were found
2056 * Either way, it will stops the loop which iterates to the next
2057 * leaf
2058 * -EOVERFLOW: item was to large for buffer
2059 * -EFAULT: could not copy extent buffer back to userspace
2060 */
Chris Masonac8e9812010-02-28 15:39:26 -05002061 return ret;
2062}
2063
2064static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002065 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002066 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002067 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002068{
2069 struct btrfs_root *root;
2070 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002071 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002072 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2073 int ret;
2074 int num_found = 0;
2075 unsigned long sk_offset = 0;
2076
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002077 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2078 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002079 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002080 }
Gerhard Heift12544442014-01-30 16:23:58 +01002081
Chris Masonac8e9812010-02-28 15:39:26 -05002082 path = btrfs_alloc_path();
2083 if (!path)
2084 return -ENOMEM;
2085
2086 if (sk->tree_id == 0) {
2087 /* search the root of the inode that was passed */
2088 root = BTRFS_I(inode)->root;
2089 } else {
2090 key.objectid = sk->tree_id;
2091 key.type = BTRFS_ROOT_ITEM_KEY;
2092 key.offset = (u64)-1;
2093 root = btrfs_read_fs_root_no_name(info, &key);
2094 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002095 btrfs_err(info, "could not find root %llu",
Chris Masonac8e9812010-02-28 15:39:26 -05002096 sk->tree_id);
2097 btrfs_free_path(path);
2098 return -ENOENT;
2099 }
2100 }
2101
2102 key.objectid = sk->min_objectid;
2103 key.type = sk->min_type;
2104 key.offset = sk->min_offset;
2105
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302106 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002107 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002108 if (ret != 0) {
2109 if (ret > 0)
2110 ret = 0;
2111 goto err;
2112 }
Gerhard Heiftba346b32014-01-30 16:24:02 +01002113 ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002114 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002115 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002116 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002117 break;
2118
2119 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002120 if (ret > 0)
2121 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002122err:
2123 sk->nr_items = num_found;
2124 btrfs_free_path(path);
2125 return ret;
2126}
2127
2128static noinline int btrfs_ioctl_tree_search(struct file *file,
2129 void __user *argp)
2130{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002131 struct btrfs_ioctl_search_args __user *uargs;
2132 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002133 struct inode *inode;
2134 int ret;
2135 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002136
2137 if (!capable(CAP_SYS_ADMIN))
2138 return -EPERM;
2139
Gerhard Heiftba346b32014-01-30 16:24:02 +01002140 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002141
Gerhard Heiftba346b32014-01-30 16:24:02 +01002142 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2143 return -EFAULT;
2144
2145 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002146
Al Viro496ad9a2013-01-23 17:07:38 -05002147 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002148 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002149
2150 /*
2151 * In the origin implementation an overflow is handled by returning a
2152 * search header with a len of zero, so reset ret.
2153 */
2154 if (ret == -EOVERFLOW)
2155 ret = 0;
2156
Gerhard Heiftba346b32014-01-30 16:24:02 +01002157 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002158 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002159 return ret;
2160}
2161
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002162static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2163 void __user *argp)
2164{
2165 struct btrfs_ioctl_search_args_v2 __user *uarg;
2166 struct btrfs_ioctl_search_args_v2 args;
2167 struct inode *inode;
2168 int ret;
2169 size_t buf_size;
2170 const size_t buf_limit = 16 * 1024 * 1024;
2171
2172 if (!capable(CAP_SYS_ADMIN))
2173 return -EPERM;
2174
2175 /* copy search header and buffer size */
2176 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2177 if (copy_from_user(&args, uarg, sizeof(args)))
2178 return -EFAULT;
2179
2180 buf_size = args.buf_size;
2181
2182 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2183 return -EOVERFLOW;
2184
2185 /* limit result size to 16MB */
2186 if (buf_size > buf_limit)
2187 buf_size = buf_limit;
2188
2189 inode = file_inode(file);
2190 ret = search_ioctl(inode, &args.key, &buf_size,
2191 (char *)(&uarg->buf[0]));
2192 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2193 ret = -EFAULT;
2194 else if (ret == -EOVERFLOW &&
2195 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2196 ret = -EFAULT;
2197
Yan, Zheng76dda932009-09-21 16:00:26 -04002198 return ret;
2199}
2200
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002201/*
Chris Masonac8e9812010-02-28 15:39:26 -05002202 * Search INODE_REFs to identify path name of 'dirid' directory
2203 * in a 'tree_id' tree. and sets path name to 'name'.
2204 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002205static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2206 u64 tree_id, u64 dirid, char *name)
2207{
2208 struct btrfs_root *root;
2209 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002210 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002211 int ret = -1;
2212 int slot;
2213 int len;
2214 int total_len = 0;
2215 struct btrfs_inode_ref *iref;
2216 struct extent_buffer *l;
2217 struct btrfs_path *path;
2218
2219 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2220 name[0]='\0';
2221 return 0;
2222 }
2223
2224 path = btrfs_alloc_path();
2225 if (!path)
2226 return -ENOMEM;
2227
Chris Masonac8e9812010-02-28 15:39:26 -05002228 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002229
2230 key.objectid = tree_id;
2231 key.type = BTRFS_ROOT_ITEM_KEY;
2232 key.offset = (u64)-1;
2233 root = btrfs_read_fs_root_no_name(info, &key);
2234 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002235 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002236 ret = -ENOENT;
2237 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002238 }
2239
2240 key.objectid = dirid;
2241 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002242 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002243
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302244 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002245 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2246 if (ret < 0)
2247 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002248 else if (ret > 0) {
2249 ret = btrfs_previous_item(root, path, dirid,
2250 BTRFS_INODE_REF_KEY);
2251 if (ret < 0)
2252 goto out;
2253 else if (ret > 0) {
2254 ret = -ENOENT;
2255 goto out;
2256 }
2257 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002258
2259 l = path->nodes[0];
2260 slot = path->slots[0];
2261 btrfs_item_key_to_cpu(l, &key, slot);
2262
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002263 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2264 len = btrfs_inode_ref_name_len(l, iref);
2265 ptr -= len + 1;
2266 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002267 if (ptr < name) {
2268 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002269 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002270 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002271
2272 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302273 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002274
2275 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2276 break;
2277
David Sterbab3b4aa72011-04-21 01:20:15 +02002278 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002279 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002280 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002281 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002282 }
Li Zefan77906a502011-07-14 03:16:00 +00002283 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302284 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002285 ret = 0;
2286out:
2287 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002288 return ret;
2289}
2290
2291static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2292 void __user *argp)
2293{
2294 struct btrfs_ioctl_ino_lookup_args *args;
2295 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002296 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002297
Julia Lawall2354d082010-10-29 15:14:18 -04002298 args = memdup_user(argp, sizeof(*args));
2299 if (IS_ERR(args))
2300 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002301
Al Viro496ad9a2013-01-23 17:07:38 -05002302 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002303
David Sterba01b810b2015-05-12 19:14:49 +02002304 /*
2305 * Unprivileged query to obtain the containing subvolume root id. The
2306 * path is reset so it's consistent with btrfs_search_path_in_tree.
2307 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002308 if (args->treeid == 0)
2309 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2310
David Sterba01b810b2015-05-12 19:14:49 +02002311 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2312 args->name[0] = 0;
2313 goto out;
2314 }
2315
2316 if (!capable(CAP_SYS_ADMIN)) {
2317 ret = -EPERM;
2318 goto out;
2319 }
2320
Chris Masonac8e9812010-02-28 15:39:26 -05002321 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2322 args->treeid, args->objectid,
2323 args->name);
2324
David Sterba01b810b2015-05-12 19:14:49 +02002325out:
Chris Masonac8e9812010-02-28 15:39:26 -05002326 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2327 ret = -EFAULT;
2328
2329 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002330 return ret;
2331}
2332
Yan, Zheng76dda932009-09-21 16:00:26 -04002333static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2334 void __user *arg)
2335{
Al Viro54563d42013-09-01 15:57:51 -04002336 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002337 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002338 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002339 struct inode *inode;
2340 struct btrfs_root *root = BTRFS_I(dir)->root;
2341 struct btrfs_root *dest = NULL;
2342 struct btrfs_ioctl_vol_args *vol_args;
2343 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002344 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002345 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002346 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002347 int namelen;
2348 int ret;
2349 int err = 0;
2350
Yan, Zheng76dda932009-09-21 16:00:26 -04002351 vol_args = memdup_user(arg, sizeof(*vol_args));
2352 if (IS_ERR(vol_args))
2353 return PTR_ERR(vol_args);
2354
2355 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2356 namelen = strlen(vol_args->name);
2357 if (strchr(vol_args->name, '/') ||
2358 strncmp(vol_args->name, "..", namelen) == 0) {
2359 err = -EINVAL;
2360 goto out;
2361 }
2362
Al Viroa561be72011-11-23 11:57:51 -05002363 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002364 if (err)
2365 goto out;
2366
David Sterba521e0542014-04-15 16:41:44 +02002367
David Sterba5c50c9b2013-03-22 18:12:51 +00002368 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2369 if (err == -EINTR)
David Sterbae43f9982013-12-06 17:51:32 +01002370 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002371 dentry = lookup_one_len(vol_args->name, parent, namelen);
2372 if (IS_ERR(dentry)) {
2373 err = PTR_ERR(dentry);
2374 goto out_unlock_dir;
2375 }
2376
David Howells2b0143b2015-03-17 22:25:59 +00002377 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002378 err = -ENOENT;
2379 goto out_dput;
2380 }
2381
David Howells2b0143b2015-03-17 22:25:59 +00002382 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002383 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302384 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002385 /*
2386 * Regular user. Only allow this with a special mount
2387 * option, when the user has write+exec access to the
2388 * subvol root, and when rmdir(2) would have been
2389 * allowed.
2390 *
2391 * Note that this is _not_ check that the subvol is
2392 * empty or doesn't contain data that we wouldn't
2393 * otherwise be able to delete.
2394 *
2395 * Users who want to delete empty subvols should try
2396 * rmdir(2).
2397 */
2398 err = -EPERM;
2399 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2400 goto out_dput;
2401
2402 /*
2403 * Do not allow deletion if the parent dir is the same
2404 * as the dir to be deleted. That means the ioctl
2405 * must be called on the dentry referencing the root
2406 * of the subvol, not a random directory contained
2407 * within it.
2408 */
2409 err = -EINVAL;
2410 if (root == dest)
2411 goto out_dput;
2412
2413 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2414 if (err)
2415 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002416 }
2417
Miao Xie5c39da52012-10-22 11:39:53 +00002418 /* check if subvolume may be deleted by a user */
2419 err = btrfs_may_delete(dir, dentry, 1);
2420 if (err)
2421 goto out_dput;
2422
Li Zefan33345d012011-04-20 10:31:50 +08002423 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002424 err = -EINVAL;
2425 goto out_dput;
2426 }
2427
Yan, Zheng76dda932009-09-21 16:00:26 -04002428 mutex_lock(&inode->i_mutex);
David Sterba521e0542014-04-15 16:41:44 +02002429
2430 /*
2431 * Don't allow to delete a subvolume with send in progress. This is
2432 * inside the i_mutex so the error handling that has to drop the bit
2433 * again is not run concurrently.
2434 */
2435 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002436 root_flags = btrfs_root_flags(&dest->root_item);
2437 if (dest->send_in_progress == 0) {
2438 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002439 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2440 spin_unlock(&dest->root_item_lock);
2441 } else {
2442 spin_unlock(&dest->root_item_lock);
2443 btrfs_warn(root->fs_info,
2444 "Attempt to delete subvolume %llu during send",
Filipe Mananac55bfa62014-05-25 03:55:44 +01002445 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002446 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002447 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002448 }
2449
Yan, Zheng76dda932009-09-21 16:00:26 -04002450 down_write(&root->fs_info->subvol_sem);
2451
2452 err = may_destroy_subvol(dest);
2453 if (err)
2454 goto out_up_write;
2455
Miao Xiec58aaad2013-02-28 10:05:36 +00002456 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2457 /*
2458 * One for dir inode, two for dir entries, two for root
2459 * ref/backref.
2460 */
2461 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002462 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002463 if (err)
2464 goto out_up_write;
2465
Yan, Zhenga22285a2010-05-16 10:48:46 -04002466 trans = btrfs_start_transaction(root, 0);
2467 if (IS_ERR(trans)) {
2468 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002469 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002470 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002471 trans->block_rsv = &block_rsv;
2472 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002473
Yan, Zheng76dda932009-09-21 16:00:26 -04002474 ret = btrfs_unlink_subvol(trans, root, dir,
2475 dest->root_key.objectid,
2476 dentry->d_name.name,
2477 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002478 if (ret) {
2479 err = ret;
2480 btrfs_abort_transaction(trans, root, ret);
2481 goto out_end_trans;
2482 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002483
2484 btrfs_record_root_in_trans(trans, dest);
2485
2486 memset(&dest->root_item.drop_progress, 0,
2487 sizeof(dest->root_item.drop_progress));
2488 dest->root_item.drop_level = 0;
2489 btrfs_set_root_refs(&dest->root_item, 0);
2490
Miao Xie27cdeb72014-04-02 19:51:05 +08002491 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002492 ret = btrfs_insert_orphan_item(trans,
2493 root->fs_info->tree_root,
2494 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002495 if (ret) {
2496 btrfs_abort_transaction(trans, root, ret);
2497 err = ret;
2498 goto out_end_trans;
2499 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002500 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002501
2502 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2503 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2504 dest->root_key.objectid);
2505 if (ret && ret != -ENOENT) {
2506 btrfs_abort_transaction(trans, root, ret);
2507 err = ret;
2508 goto out_end_trans;
2509 }
2510 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2511 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2512 dest->root_item.received_uuid,
2513 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2514 dest->root_key.objectid);
2515 if (ret && ret != -ENOENT) {
2516 btrfs_abort_transaction(trans, root, ret);
2517 err = ret;
2518 goto out_end_trans;
2519 }
2520 }
2521
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002522out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002523 trans->block_rsv = NULL;
2524 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002525 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002526 if (ret && !err)
2527 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002528 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002529out_release:
2530 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002531out_up_write:
2532 up_write(&root->fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002533 if (err) {
2534 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002535 root_flags = btrfs_root_flags(&dest->root_item);
2536 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002537 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2538 spin_unlock(&dest->root_item_lock);
2539 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002540out_unlock_inode:
Yan, Zheng76dda932009-09-21 16:00:26 -04002541 mutex_unlock(&inode->i_mutex);
2542 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002543 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002544 btrfs_invalidate_inodes(dest);
2545 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002546 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002547
2548 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002549 if (dest->ino_cache_inode) {
2550 iput(dest->ino_cache_inode);
2551 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002552 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002553 }
2554out_dput:
2555 dput(dentry);
2556out_unlock_dir:
2557 mutex_unlock(&dir->i_mutex);
David Sterbae43f9982013-12-06 17:51:32 +01002558out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002559 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002560out:
2561 kfree(vol_args);
2562 return err;
2563}
2564
Chris Mason1e701a32010-03-11 09:42:04 -05002565static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002566{
Al Viro496ad9a2013-01-23 17:07:38 -05002567 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002568 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002569 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002570 int ret;
2571
Ilya Dryomov25122d12013-01-20 15:57:57 +02002572 ret = mnt_want_write_file(file);
2573 if (ret)
2574 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002575
Ilya Dryomov25122d12013-01-20 15:57:57 +02002576 if (btrfs_root_readonly(root)) {
2577 ret = -EROFS;
2578 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002579 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002580
2581 switch (inode->i_mode & S_IFMT) {
2582 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002583 if (!capable(CAP_SYS_ADMIN)) {
2584 ret = -EPERM;
2585 goto out;
2586 }
Eric Sandeende78b512013-01-31 18:21:12 +00002587 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002588 if (ret)
2589 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002590 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002591 break;
2592 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002593 if (!(file->f_mode & FMODE_WRITE)) {
2594 ret = -EINVAL;
2595 goto out;
2596 }
Chris Mason1e701a32010-03-11 09:42:04 -05002597
2598 range = kzalloc(sizeof(*range), GFP_KERNEL);
2599 if (!range) {
2600 ret = -ENOMEM;
2601 goto out;
2602 }
2603
2604 if (argp) {
2605 if (copy_from_user(range, argp,
2606 sizeof(*range))) {
2607 ret = -EFAULT;
2608 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002609 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002610 }
2611 /* compression requires us to start the IO */
2612 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2613 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2614 range->extent_thresh = (u32)-1;
2615 }
2616 } else {
2617 /* the rest are all set to zero by kzalloc */
2618 range->len = (u64)-1;
2619 }
Al Viro496ad9a2013-01-23 17:07:38 -05002620 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002621 range, 0, 0);
2622 if (ret > 0)
2623 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002624 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002625 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002626 default:
2627 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002628 }
Chris Masone441d542009-01-05 16:57:23 -05002629out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002630 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002631 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002632}
2633
Christoph Hellwigb2950862008-12-02 09:54:17 -05002634static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002635{
2636 struct btrfs_ioctl_vol_args *vol_args;
2637 int ret;
2638
Chris Masone441d542009-01-05 16:57:23 -05002639 if (!capable(CAP_SYS_ADMIN))
2640 return -EPERM;
2641
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002642 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2643 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002644 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002645 }
2646
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002647 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002648 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002649 if (IS_ERR(vol_args)) {
2650 ret = PTR_ERR(vol_args);
2651 goto out;
2652 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002653
Mark Fasheh5516e592008-07-24 12:20:14 -04002654 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002655 ret = btrfs_init_new_device(root, vol_args->name);
2656
Anand Jain43d20762014-07-01 00:58:56 +08002657 if (!ret)
2658 btrfs_info(root->fs_info, "disk added %s",vol_args->name);
2659
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002660 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002661out:
2662 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002663 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002664 return ret;
2665}
2666
Miao Xieda249272012-11-26 08:44:50 +00002667static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002668{
Al Viro496ad9a2013-01-23 17:07:38 -05002669 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002670 struct btrfs_ioctl_vol_args *vol_args;
2671 int ret;
2672
Chris Masone441d542009-01-05 16:57:23 -05002673 if (!capable(CAP_SYS_ADMIN))
2674 return -EPERM;
2675
Miao Xieda249272012-11-26 08:44:50 +00002676 ret = mnt_want_write_file(file);
2677 if (ret)
2678 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002679
Li Zefandae7b662009-04-08 15:06:54 +08002680 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002681 if (IS_ERR(vol_args)) {
2682 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002683 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002684 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002685
Mark Fasheh5516e592008-07-24 12:20:14 -04002686 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002687
Anand Jain183860f2013-05-17 10:52:45 +00002688 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2689 1)) {
2690 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2691 goto out;
2692 }
2693
2694 mutex_lock(&root->fs_info->volume_mutex);
2695 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002696 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002697 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002698
Anand Jainec95d492014-07-01 00:58:57 +08002699 if (!ret)
2700 btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
2701
Anand Jain183860f2013-05-17 10:52:45 +00002702out:
2703 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002704err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002705 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002706 return ret;
2707}
2708
Jan Schmidt475f6382011-03-11 15:41:01 +01002709static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2710{
Li Zefan027ed2f2011-06-08 08:27:56 +00002711 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002712 struct btrfs_device *device;
Jan Schmidt475f6382011-03-11 15:41:01 +01002713 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002714 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002715
Li Zefan027ed2f2011-06-08 08:27:56 +00002716 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2717 if (!fi_args)
2718 return -ENOMEM;
2719
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002720 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002721 fi_args->num_devices = fs_devices->num_devices;
2722 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002723
Byongho Leed7641a42015-09-01 23:10:57 +09002724 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002725 if (device->devid > fi_args->max_id)
2726 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002727 }
2728 mutex_unlock(&fs_devices->device_list_mutex);
2729
David Sterba80a773f2014-05-07 18:17:06 +02002730 fi_args->nodesize = root->fs_info->super_copy->nodesize;
2731 fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
2732 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
2733
Li Zefan027ed2f2011-06-08 08:27:56 +00002734 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2735 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002736
Li Zefan027ed2f2011-06-08 08:27:56 +00002737 kfree(fi_args);
2738 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002739}
2740
2741static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2742{
2743 struct btrfs_ioctl_dev_info_args *di_args;
2744 struct btrfs_device *dev;
2745 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2746 int ret = 0;
2747 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002748
Jan Schmidt475f6382011-03-11 15:41:01 +01002749 di_args = memdup_user(arg, sizeof(*di_args));
2750 if (IS_ERR(di_args))
2751 return PTR_ERR(di_args);
2752
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002753 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002754 s_uuid = di_args->uuid;
2755
2756 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002757 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002758
2759 if (!dev) {
2760 ret = -ENODEV;
2761 goto out;
2762 }
2763
2764 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002765 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2766 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002767 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002768 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002769 struct rcu_string *name;
2770
2771 rcu_read_lock();
2772 name = rcu_dereference(dev->name);
2773 strncpy(di_args->path, name->str, sizeof(di_args->path));
2774 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002775 di_args->path[sizeof(di_args->path) - 1] = 0;
2776 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002777 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002778 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002779
2780out:
David Sterba55793c02013-04-26 15:20:23 +00002781 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002782 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2783 ret = -EFAULT;
2784
2785 kfree(di_args);
2786 return ret;
2787}
2788
Mark Fashehf4414602015-06-30 14:42:05 -07002789static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002790{
2791 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002792 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2793
Mark Fasheh416161d2013-08-06 11:42:51 -07002794 page = grab_cache_page(inode->i_mapping, index);
2795 if (!page)
2796 return NULL;
2797
2798 if (!PageUptodate(page)) {
2799 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2800 0))
2801 return NULL;
2802 lock_page(page);
2803 if (!PageUptodate(page)) {
2804 unlock_page(page);
2805 page_cache_release(page);
2806 return NULL;
2807 }
2808 }
2809 unlock_page(page);
2810
2811 return page;
2812}
2813
Mark Fashehf4414602015-06-30 14:42:05 -07002814static int gather_extent_pages(struct inode *inode, struct page **pages,
2815 int num_pages, u64 off)
2816{
2817 int i;
2818 pgoff_t index = off >> PAGE_CACHE_SHIFT;
2819
2820 for (i = 0; i < num_pages; i++) {
2821 pages[i] = extent_same_get_page(inode, index + i);
2822 if (!pages[i])
2823 return -ENOMEM;
2824 }
2825 return 0;
2826}
2827
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002828static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2829{
2830 /* do any pending delalloc/csum calc on src, one way or
2831 another, and lock file content */
2832 while (1) {
2833 struct btrfs_ordered_extent *ordered;
2834 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2835 ordered = btrfs_lookup_first_ordered_extent(inode,
2836 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002837 if ((!ordered ||
2838 ordered->file_offset + ordered->len <= off ||
2839 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002840 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002841 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2842 if (ordered)
2843 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002844 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002845 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002846 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2847 if (ordered)
2848 btrfs_put_ordered_extent(ordered);
2849 btrfs_wait_ordered_range(inode, off, len);
2850 }
2851}
2852
Mark Fashehf4414602015-06-30 14:42:05 -07002853static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002854{
Mark Fasheh416161d2013-08-06 11:42:51 -07002855 mutex_unlock(&inode1->i_mutex);
2856 mutex_unlock(&inode2->i_mutex);
2857}
2858
Mark Fashehf4414602015-06-30 14:42:05 -07002859static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2860{
2861 if (inode1 < inode2)
2862 swap(inode1, inode2);
2863
2864 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
Mark Fasheh293a8482015-06-30 14:42:06 -07002865 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002866}
2867
2868static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2869 struct inode *inode2, u64 loff2, u64 len)
2870{
2871 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2872 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2873}
2874
2875static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2876 struct inode *inode2, u64 loff2, u64 len)
Mark Fasheh416161d2013-08-06 11:42:51 -07002877{
2878 if (inode1 < inode2) {
2879 swap(inode1, inode2);
2880 swap(loff1, loff2);
2881 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002882 lock_extent_range(inode1, loff1, len);
Mark Fasheh293a8482015-06-30 14:42:06 -07002883 lock_extent_range(inode2, loff2, len);
Mark Fashehf4414602015-06-30 14:42:05 -07002884}
2885
2886struct cmp_pages {
2887 int num_pages;
2888 struct page **src_pages;
2889 struct page **dst_pages;
2890};
2891
2892static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2893{
2894 int i;
2895 struct page *pg;
2896
2897 for (i = 0; i < cmp->num_pages; i++) {
2898 pg = cmp->src_pages[i];
2899 if (pg)
2900 page_cache_release(pg);
2901 pg = cmp->dst_pages[i];
2902 if (pg)
2903 page_cache_release(pg);
Mark Fasheh416161d2013-08-06 11:42:51 -07002904 }
Mark Fashehf4414602015-06-30 14:42:05 -07002905 kfree(cmp->src_pages);
2906 kfree(cmp->dst_pages);
2907}
2908
2909static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2910 struct inode *dst, u64 dst_loff,
2911 u64 len, struct cmp_pages *cmp)
2912{
2913 int ret;
2914 int num_pages = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
2915 struct page **src_pgarr, **dst_pgarr;
2916
2917 /*
2918 * We must gather up all the pages before we initiate our
2919 * extent locking. We use an array for the page pointers. Size
2920 * of the array is bounded by len, which is in turn bounded by
2921 * BTRFS_MAX_DEDUPE_LEN.
2922 */
2923 src_pgarr = kzalloc(num_pages * sizeof(struct page *), GFP_NOFS);
2924 dst_pgarr = kzalloc(num_pages * sizeof(struct page *), GFP_NOFS);
2925 if (!src_pgarr || !dst_pgarr) {
2926 kfree(src_pgarr);
2927 kfree(dst_pgarr);
2928 return -ENOMEM;
2929 }
2930 cmp->num_pages = num_pages;
2931 cmp->src_pages = src_pgarr;
2932 cmp->dst_pages = dst_pgarr;
2933
2934 ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff);
2935 if (ret)
2936 goto out;
2937
2938 ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff);
2939
2940out:
2941 if (ret)
2942 btrfs_cmp_data_free(cmp);
2943 return 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002944}
2945
2946static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
Mark Fashehf4414602015-06-30 14:42:05 -07002947 u64 dst_loff, u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002948{
2949 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07002950 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07002951 struct page *src_page, *dst_page;
2952 unsigned int cmp_len = PAGE_CACHE_SIZE;
2953 void *addr, *dst_addr;
2954
Mark Fashehf4414602015-06-30 14:42:05 -07002955 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002956 while (len) {
2957 if (len < PAGE_CACHE_SIZE)
2958 cmp_len = len;
2959
Mark Fashehf4414602015-06-30 14:42:05 -07002960 BUG_ON(i >= cmp->num_pages);
2961
2962 src_page = cmp->src_pages[i];
2963 dst_page = cmp->dst_pages[i];
2964
Mark Fasheh416161d2013-08-06 11:42:51 -07002965 addr = kmap_atomic(src_page);
2966 dst_addr = kmap_atomic(dst_page);
2967
2968 flush_dcache_page(src_page);
2969 flush_dcache_page(dst_page);
2970
2971 if (memcmp(addr, dst_addr, cmp_len))
2972 ret = BTRFS_SAME_DATA_DIFFERS;
2973
2974 kunmap_atomic(addr);
2975 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07002976
2977 if (ret)
2978 break;
2979
Mark Fasheh416161d2013-08-06 11:42:51 -07002980 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07002981 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07002982 }
2983
2984 return ret;
2985}
2986
Mark Fashehe1d227a2015-06-08 15:05:25 -07002987static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
2988 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07002989{
Mark Fashehe1d227a2015-06-08 15:05:25 -07002990 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07002991 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2992
Mark Fashehe1d227a2015-06-08 15:05:25 -07002993 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07002994 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07002995
2996 /* if we extend to eof, continue to block boundary */
2997 if (off + len == inode->i_size)
2998 *plen = len = ALIGN(inode->i_size, bs) - off;
2999
Mark Fasheh416161d2013-08-06 11:42:51 -07003000 /* Check that we are block aligned - btrfs_clone() requires this */
3001 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3002 return -EINVAL;
3003
3004 return 0;
3005}
3006
Mark Fashehe1d227a2015-06-08 15:05:25 -07003007static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003008 struct inode *dst, u64 dst_loff)
3009{
3010 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003011 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003012 struct cmp_pages cmp;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003013 int same_inode = 0;
3014 u64 same_lock_start = 0;
3015 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003016
Mark Fasheh416161d2013-08-06 11:42:51 -07003017 if (src == dst)
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003018 same_inode = 1;
Mark Fasheh416161d2013-08-06 11:42:51 -07003019
Filipe Manana113e8282015-03-30 18:26:47 +01003020 if (len == 0)
3021 return 0;
3022
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003023 if (same_inode) {
3024 mutex_lock(&src->i_mutex);
Mark Fasheh416161d2013-08-06 11:42:51 -07003025
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003026 ret = extent_same_check_offsets(src, loff, &len, olen);
3027 if (ret)
3028 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003029
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003030 /*
3031 * Single inode case wants the same checks, except we
3032 * don't want our length pushed out past i_size as
3033 * comparing that data range makes no sense.
3034 *
3035 * extent_same_check_offsets() will do this for an
3036 * unaligned length at i_size, so catch it here and
3037 * reject the request.
3038 *
3039 * This effectively means we require aligned extents
3040 * for the single-inode case, whereas the other cases
3041 * allow an unaligned length so long as it ends at
3042 * i_size.
3043 */
3044 if (len != olen) {
3045 ret = -EINVAL;
3046 goto out_unlock;
3047 }
3048
3049 /* Check for overlapping ranges */
3050 if (dst_loff + len > loff && dst_loff < loff + len) {
3051 ret = -EINVAL;
3052 goto out_unlock;
3053 }
3054
3055 same_lock_start = min_t(u64, loff, dst_loff);
3056 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
3057 } else {
3058 btrfs_double_inode_lock(src, dst);
3059
3060 ret = extent_same_check_offsets(src, loff, &len, olen);
3061 if (ret)
3062 goto out_unlock;
3063
3064 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3065 if (ret)
3066 goto out_unlock;
3067 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003068
3069 /* don't make the dst file partly checksummed */
3070 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3071 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3072 ret = -EINVAL;
3073 goto out_unlock;
3074 }
3075
Mark Fashehf4414602015-06-30 14:42:05 -07003076 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3077 if (ret)
3078 goto out_unlock;
3079
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003080 if (same_inode)
3081 lock_extent_range(src, same_lock_start, same_lock_len);
3082 else
3083 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003084
Mark Fasheh207910d2015-06-30 14:42:04 -07003085 /* pass original length for comparison so we stay within i_size */
Mark Fashehf4414602015-06-30 14:42:05 -07003086 ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003087 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003088 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003089
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003090 if (same_inode)
3091 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3092 same_lock_start + same_lock_len - 1);
3093 else
3094 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003095
3096 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003097out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003098 if (same_inode)
3099 mutex_unlock(&src->i_mutex);
3100 else
3101 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003102
3103 return ret;
3104}
3105
3106#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
3107
3108static long btrfs_ioctl_file_extent_same(struct file *file,
Al Viro1c1c8742013-12-11 23:07:51 -05003109 struct btrfs_ioctl_same_args __user *argp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003110{
Filipe Manana497b4052015-07-03 08:36:11 +01003111 struct btrfs_ioctl_same_args *same = NULL;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003112 struct btrfs_ioctl_same_extent_info *info;
Al Viro1c1c8742013-12-11 23:07:51 -05003113 struct inode *src = file_inode(file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003114 u64 off;
3115 u64 len;
3116 int i;
3117 int ret;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003118 unsigned long size;
Mark Fasheh416161d2013-08-06 11:42:51 -07003119 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
3120 bool is_admin = capable(CAP_SYS_ADMIN);
Al Viro1c1c8742013-12-11 23:07:51 -05003121 u16 count;
Mark Fasheh416161d2013-08-06 11:42:51 -07003122
3123 if (!(file->f_mode & FMODE_READ))
3124 return -EINVAL;
3125
3126 ret = mnt_want_write_file(file);
3127 if (ret)
3128 return ret;
3129
Al Viro1c1c8742013-12-11 23:07:51 -05003130 if (get_user(count, &argp->dest_count)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003131 ret = -EFAULT;
3132 goto out;
3133 }
3134
Al Viro1c1c8742013-12-11 23:07:51 -05003135 size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003136
Al Viro1c1c8742013-12-11 23:07:51 -05003137 same = memdup_user(argp, size);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003138
Geyslan G. Bem229eed42013-10-14 12:18:25 -03003139 if (IS_ERR(same)) {
3140 ret = PTR_ERR(same);
Filipe Manana497b4052015-07-03 08:36:11 +01003141 same = NULL;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003142 goto out;
3143 }
3144
3145 off = same->logical_offset;
3146 len = same->length;
Mark Fasheh416161d2013-08-06 11:42:51 -07003147
3148 /*
3149 * Limit the total length we will dedupe for each operation.
3150 * This is intended to bound the total time spent in this
3151 * ioctl to something sane.
3152 */
3153 if (len > BTRFS_MAX_DEDUPE_LEN)
3154 len = BTRFS_MAX_DEDUPE_LEN;
3155
3156 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
3157 /*
3158 * Btrfs does not support blocksize < page_size. As a
3159 * result, btrfs_cmp_data() won't correctly handle
3160 * this situation without an update.
3161 */
3162 ret = -EINVAL;
3163 goto out;
3164 }
3165
3166 ret = -EISDIR;
3167 if (S_ISDIR(src->i_mode))
3168 goto out;
3169
3170 ret = -EACCES;
3171 if (!S_ISREG(src->i_mode))
3172 goto out;
3173
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003174 /* pre-format output fields to sane values */
Al Viro1c1c8742013-12-11 23:07:51 -05003175 for (i = 0; i < count; i++) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003176 same->info[i].bytes_deduped = 0ULL;
3177 same->info[i].status = 0;
3178 }
3179
Al Viro1c1c8742013-12-11 23:07:51 -05003180 for (i = 0, info = same->info; i < count; i++, info++) {
3181 struct inode *dst;
3182 struct fd dst_file = fdget(info->fd);
3183 if (!dst_file.file) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003184 info->status = -EBADF;
Al Viro1c1c8742013-12-11 23:07:51 -05003185 continue;
Mark Fasheh416161d2013-08-06 11:42:51 -07003186 }
Al Viro1c1c8742013-12-11 23:07:51 -05003187 dst = file_inode(dst_file.file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003188
Al Viro1c1c8742013-12-11 23:07:51 -05003189 if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003190 info->status = -EINVAL;
Al Viro1c1c8742013-12-11 23:07:51 -05003191 } else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
3192 info->status = -EXDEV;
3193 } else if (S_ISDIR(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003194 info->status = -EISDIR;
Al Viro1c1c8742013-12-11 23:07:51 -05003195 } else if (!S_ISREG(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003196 info->status = -EACCES;
Al Viro1c1c8742013-12-11 23:07:51 -05003197 } else {
3198 info->status = btrfs_extent_same(src, off, len, dst,
3199 info->logical_offset);
3200 if (info->status == 0)
3201 info->bytes_deduped += len;
Mark Fasheh416161d2013-08-06 11:42:51 -07003202 }
Al Viro1c1c8742013-12-11 23:07:51 -05003203 fdput(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003204 }
3205
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003206 ret = copy_to_user(argp, same, size);
3207 if (ret)
3208 ret = -EFAULT;
3209
Mark Fasheh416161d2013-08-06 11:42:51 -07003210out:
3211 mnt_drop_write_file(file);
Filipe Manana497b4052015-07-03 08:36:11 +01003212 kfree(same);
Mark Fasheh416161d2013-08-06 11:42:51 -07003213 return ret;
3214}
3215
Filipe Mananaf82a9902014-06-01 01:50:28 +01003216static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3217 struct inode *inode,
3218 u64 endoff,
3219 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003220 const u64 olen,
3221 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003222{
3223 struct btrfs_root *root = BTRFS_I(inode)->root;
3224 int ret;
3225
3226 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003227 if (!no_time_update)
3228 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003229 /*
3230 * We round up to the block size at eof when determining which
3231 * extents to clone above, but shouldn't round up the file size.
3232 */
3233 if (endoff > destoff + olen)
3234 endoff = destoff + olen;
3235 if (endoff > inode->i_size)
3236 btrfs_i_size_write(inode, endoff);
3237
3238 ret = btrfs_update_inode(trans, root, inode);
3239 if (ret) {
3240 btrfs_abort_transaction(trans, root, ret);
3241 btrfs_end_transaction(trans, root);
3242 goto out;
3243 }
3244 ret = btrfs_end_transaction(trans, root);
3245out:
3246 return ret;
3247}
3248
Filipe Manana7ffbb592014-06-09 03:48:05 +01003249static void clone_update_extent_map(struct inode *inode,
3250 const struct btrfs_trans_handle *trans,
3251 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003252 const u64 hole_offset,
3253 const u64 hole_len)
3254{
3255 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3256 struct extent_map *em;
3257 int ret;
3258
3259 em = alloc_extent_map();
3260 if (!em) {
3261 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3262 &BTRFS_I(inode)->runtime_flags);
3263 return;
3264 }
3265
Filipe Manana14f59792014-06-29 21:45:40 +01003266 if (path) {
3267 struct btrfs_file_extent_item *fi;
3268
3269 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3270 struct btrfs_file_extent_item);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003271 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3272 em->generation = -1;
3273 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3274 BTRFS_FILE_EXTENT_INLINE)
3275 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3276 &BTRFS_I(inode)->runtime_flags);
3277 } else {
3278 em->start = hole_offset;
3279 em->len = hole_len;
3280 em->ram_bytes = em->len;
3281 em->orig_start = hole_offset;
3282 em->block_start = EXTENT_MAP_HOLE;
3283 em->block_len = 0;
3284 em->orig_block_len = 0;
3285 em->compress_type = BTRFS_COMPRESS_NONE;
3286 em->generation = trans->transid;
3287 }
3288
3289 while (1) {
3290 write_lock(&em_tree->lock);
3291 ret = add_extent_mapping(em_tree, em, 1);
3292 write_unlock(&em_tree->lock);
3293 if (ret != -EEXIST) {
3294 free_extent_map(em);
3295 break;
3296 }
3297 btrfs_drop_extent_cache(inode, em->start,
3298 em->start + em->len - 1, 0);
3299 }
3300
David Sterbaee39b432014-09-30 01:33:33 +02003301 if (ret)
Filipe Manana7ffbb592014-06-09 03:48:05 +01003302 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3303 &BTRFS_I(inode)->runtime_flags);
3304}
3305
Filipe Manana8039d872015-10-13 15:15:00 +01003306/*
3307 * Make sure we do not end up inserting an inline extent into a file that has
3308 * already other (non-inline) extents. If a file has an inline extent it can
3309 * not have any other extents and the (single) inline extent must start at the
3310 * file offset 0. Failing to respect these rules will lead to file corruption,
3311 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3312 *
3313 * We can have extents that have been already written to disk or we can have
3314 * dirty ranges still in delalloc, in which case the extent maps and items are
3315 * created only when we run delalloc, and the delalloc ranges might fall outside
3316 * the range we are currently locking in the inode's io tree. So we check the
3317 * inode's i_size because of that (i_size updates are done while holding the
3318 * i_mutex, which we are holding here).
3319 * We also check to see if the inode has a size not greater than "datal" but has
3320 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3321 * protected against such concurrent fallocate calls by the i_mutex).
3322 *
3323 * If the file has no extents but a size greater than datal, do not allow the
3324 * copy because we would need turn the inline extent into a non-inline one (even
3325 * with NO_HOLES enabled). If we find our destination inode only has one inline
3326 * extent, just overwrite it with the source inline extent if its size is less
3327 * than the source extent's size, or we could copy the source inline extent's
3328 * data into the destination inode's inline extent if the later is greater then
3329 * the former.
3330 */
3331static int clone_copy_inline_extent(struct inode *src,
3332 struct inode *dst,
3333 struct btrfs_trans_handle *trans,
3334 struct btrfs_path *path,
3335 struct btrfs_key *new_key,
3336 const u64 drop_start,
3337 const u64 datal,
3338 const u64 skip,
3339 const u64 size,
3340 char *inline_data)
3341{
3342 struct btrfs_root *root = BTRFS_I(dst)->root;
3343 const u64 aligned_end = ALIGN(new_key->offset + datal,
3344 root->sectorsize);
3345 int ret;
3346 struct btrfs_key key;
3347
3348 if (new_key->offset > 0)
3349 return -EOPNOTSUPP;
3350
3351 key.objectid = btrfs_ino(dst);
3352 key.type = BTRFS_EXTENT_DATA_KEY;
3353 key.offset = 0;
3354 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3355 if (ret < 0) {
3356 return ret;
3357 } else if (ret > 0) {
3358 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3359 ret = btrfs_next_leaf(root, path);
3360 if (ret < 0)
3361 return ret;
3362 else if (ret > 0)
3363 goto copy_inline_extent;
3364 }
3365 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3366 if (key.objectid == btrfs_ino(dst) &&
3367 key.type == BTRFS_EXTENT_DATA_KEY) {
3368 ASSERT(key.offset > 0);
3369 return -EOPNOTSUPP;
3370 }
3371 } else if (i_size_read(dst) <= datal) {
3372 struct btrfs_file_extent_item *ei;
3373 u64 ext_len;
3374
3375 /*
3376 * If the file size is <= datal, make sure there are no other
3377 * extents following (can happen do to an fallocate call with
3378 * the flag FALLOC_FL_KEEP_SIZE).
3379 */
3380 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3381 struct btrfs_file_extent_item);
3382 /*
3383 * If it's an inline extent, it can not have other extents
3384 * following it.
3385 */
3386 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3387 BTRFS_FILE_EXTENT_INLINE)
3388 goto copy_inline_extent;
3389
3390 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3391 if (ext_len > aligned_end)
3392 return -EOPNOTSUPP;
3393
3394 ret = btrfs_next_item(root, path);
3395 if (ret < 0) {
3396 return ret;
3397 } else if (ret == 0) {
3398 btrfs_item_key_to_cpu(path->nodes[0], &key,
3399 path->slots[0]);
3400 if (key.objectid == btrfs_ino(dst) &&
3401 key.type == BTRFS_EXTENT_DATA_KEY)
3402 return -EOPNOTSUPP;
3403 }
3404 }
3405
3406copy_inline_extent:
3407 /*
3408 * We have no extent items, or we have an extent at offset 0 which may
3409 * or may not be inlined. All these cases are dealt the same way.
3410 */
3411 if (i_size_read(dst) > datal) {
3412 /*
3413 * If the destination inode has an inline extent...
3414 * This would require copying the data from the source inline
3415 * extent into the beginning of the destination's inline extent.
3416 * But this is really complex, both extents can be compressed
3417 * or just one of them, which would require decompressing and
3418 * re-compressing data (which could increase the new compressed
3419 * size, not allowing the compressed data to fit anymore in an
3420 * inline extent).
3421 * So just don't support this case for now (it should be rare,
3422 * we are not really saving space when cloning inline extents).
3423 */
3424 return -EOPNOTSUPP;
3425 }
3426
3427 btrfs_release_path(path);
3428 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3429 if (ret)
3430 return ret;
3431 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3432 if (ret)
3433 return ret;
3434
3435 if (skip) {
3436 const u32 start = btrfs_file_extent_calc_inline_size(0);
3437
3438 memmove(inline_data + start, inline_data + start + skip, datal);
3439 }
3440
3441 write_extent_buffer(path->nodes[0], inline_data,
3442 btrfs_item_ptr_offset(path->nodes[0],
3443 path->slots[0]),
3444 size);
3445 inode_add_bytes(dst, datal);
3446
3447 return 0;
3448}
3449
Mark Fasheh32b7c682013-08-06 11:42:49 -07003450/**
3451 * btrfs_clone() - clone a range from inode file to another
3452 *
3453 * @src: Inode to clone from
3454 * @inode: Inode to clone to
3455 * @off: Offset within source to start clone from
3456 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003457 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003458 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003459 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003460 */
3461static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003462 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003463 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003464{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003465 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003466 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003467 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003468 struct btrfs_trans_handle *trans;
3469 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003470 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003471 u32 nritems;
3472 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003473 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003474 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003475 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003476
3477 ret = -ENOMEM;
David Sterba707e8a02014-06-04 19:22:26 +02003478 buf = vmalloc(root->nodesize);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003479 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003480 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003481
3482 path = btrfs_alloc_path();
3483 if (!path) {
3484 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003485 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003486 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003487
David Sterbae4058b52015-11-27 16:31:35 +01003488 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003489 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08003490 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003491 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003492 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003493
3494 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003495 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003496
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003497 /*
3498 * note the key will change type as we walk through the
3499 * tree.
3500 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003501 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003502 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3503 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003504 if (ret < 0)
3505 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003506 /*
3507 * First search, if no extent item that starts at offset off was
3508 * found but the previous item is an extent item, it's possible
3509 * it might overlap our target range, therefore process it.
3510 */
3511 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3512 btrfs_item_key_to_cpu(path->nodes[0], &key,
3513 path->slots[0] - 1);
3514 if (key.type == BTRFS_EXTENT_DATA_KEY)
3515 path->slots[0]--;
3516 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003517
Yan Zhengae01a0a2008-08-04 23:23:47 -04003518 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003519process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003520 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003521 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003522 if (ret < 0)
3523 goto out;
3524 if (ret > 0)
3525 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003526 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003527 }
3528 leaf = path->nodes[0];
3529 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003530
Yan Zhengae01a0a2008-08-04 23:23:47 -04003531 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003532 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08003533 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003534 break;
3535
David Sterba962a2982014-06-04 18:41:45 +02003536 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003537 struct btrfs_file_extent_item *extent;
3538 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003539 u32 size;
3540 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003541 u64 disko = 0, diskl = 0;
3542 u64 datao = 0, datal = 0;
3543 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003544 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003545
Sage Weilc5c9cd42008-11-12 14:32:25 -05003546 extent = btrfs_item_ptr(leaf, slot,
3547 struct btrfs_file_extent_item);
3548 comp = btrfs_file_extent_compression(leaf, extent);
3549 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003550 if (type == BTRFS_FILE_EXTENT_REG ||
3551 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003552 disko = btrfs_file_extent_disk_bytenr(leaf,
3553 extent);
3554 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3555 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003556 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003557 datal = btrfs_file_extent_num_bytes(leaf,
3558 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003559 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3560 /* take upper bound, may be compressed */
3561 datal = btrfs_file_extent_ram_bytes(leaf,
3562 extent);
3563 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003564
Filipe Manana2c463822014-05-31 02:31:05 +01003565 /*
3566 * The first search might have left us at an extent
3567 * item that ends before our target range's start, can
3568 * happen if we have holes and NO_HOLES feature enabled.
3569 */
3570 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003571 path->slots[0]++;
3572 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003573 } else if (key.offset >= off + len) {
3574 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003575 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003576 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003577 size = btrfs_item_size_nr(leaf, slot);
3578 read_extent_buffer(leaf, buf,
3579 btrfs_item_ptr_offset(leaf, slot),
3580 size);
3581
3582 btrfs_release_path(path);
3583 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003584
Zheng Yan31840ae2008-09-23 13:14:14 -04003585 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08003586 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08003587 if (off <= key.offset)
3588 new_key.offset = key.offset + destoff - off;
3589 else
3590 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003591
Sage Weilb6f34092011-09-20 14:48:51 -04003592 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003593 * Deal with a hole that doesn't have an extent item
3594 * that represents it (NO_HOLES feature enabled).
3595 * This hole is either in the middle of the cloning
3596 * range or at the beginning (fully overlaps it or
3597 * partially overlaps it).
3598 */
3599 if (new_key.offset != last_dest_end)
3600 drop_start = last_dest_end;
3601 else
3602 drop_start = new_key.offset;
3603
3604 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003605 * 1 - adjusting old extent (we may have to split it)
3606 * 1 - add new extent
3607 * 1 - inode update
3608 */
3609 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003610 if (IS_ERR(trans)) {
3611 ret = PTR_ERR(trans);
3612 goto out;
3613 }
3614
Chris Masonc8a894d2009-06-27 21:07:03 -04003615 if (type == BTRFS_FILE_EXTENT_REG ||
3616 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003617 /*
3618 * a | --- range to clone ---| b
3619 * | ------------- extent ------------- |
3620 */
3621
Antonio Ospite93915582014-06-04 14:03:48 +02003622 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003623 if (key.offset + datal > off + len)
3624 datal = off + len - key.offset;
3625
Antonio Ospite93915582014-06-04 14:03:48 +02003626 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003627 if (off > key.offset) {
3628 datao += off - key.offset;
3629 datal -= off - key.offset;
3630 }
3631
Josef Bacik5dc562c2012-08-17 13:14:17 -04003632 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003633 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003634 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003635 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003636 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003637 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003638 btrfs_abort_transaction(trans,
3639 root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003640 btrfs_end_transaction(trans, root);
3641 goto out;
3642 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003643
Sage Weilc5c9cd42008-11-12 14:32:25 -05003644 ret = btrfs_insert_empty_item(trans, root, path,
3645 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003646 if (ret) {
3647 btrfs_abort_transaction(trans, root,
3648 ret);
3649 btrfs_end_transaction(trans, root);
3650 goto out;
3651 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003652
3653 leaf = path->nodes[0];
3654 slot = path->slots[0];
3655 write_extent_buffer(leaf, buf,
3656 btrfs_item_ptr_offset(leaf, slot),
3657 size);
3658
3659 extent = btrfs_item_ptr(leaf, slot,
3660 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003661
Sage Weilc5c9cd42008-11-12 14:32:25 -05003662 /* disko == 0 means it's a hole */
3663 if (!disko)
3664 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003665
3666 btrfs_set_file_extent_offset(leaf, extent,
3667 datao);
3668 btrfs_set_file_extent_num_bytes(leaf, extent,
3669 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003670
Sage Weilc5c9cd42008-11-12 14:32:25 -05003671 if (disko) {
3672 inode_add_bytes(inode, datal);
3673 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003674 disko, diskl, 0,
3675 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003676 btrfs_ino(inode),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003677 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003678 if (ret) {
3679 btrfs_abort_transaction(trans,
3680 root,
3681 ret);
3682 btrfs_end_transaction(trans,
3683 root);
3684 goto out;
3685
3686 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003687 }
3688 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3689 u64 skip = 0;
3690 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003691
Sage Weilc5c9cd42008-11-12 14:32:25 -05003692 if (off > key.offset) {
3693 skip = off - key.offset;
3694 new_key.offset += skip;
3695 }
Chris Masond3977122009-01-05 21:25:51 -05003696
Liu Boaa42ffd2012-09-18 03:52:23 -06003697 if (key.offset + datal > off + len)
3698 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003699
Sage Weilc5c9cd42008-11-12 14:32:25 -05003700 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003701 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003703 goto out;
3704 }
3705 size -= skip + trim;
3706 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003707
Filipe Manana8039d872015-10-13 15:15:00 +01003708 ret = clone_copy_inline_extent(src, inode,
3709 trans, path,
3710 &new_key,
3711 drop_start,
3712 datal,
3713 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003714 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003715 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003716 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003717 root,
3718 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003719 btrfs_end_transaction(trans, root);
3720 goto out;
3721 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003722 leaf = path->nodes[0];
3723 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003724 }
3725
Filipe Manana7ffbb592014-06-09 03:48:05 +01003726 /* If we have an implicit hole (NO_HOLES feature). */
3727 if (drop_start < new_key.offset)
3728 clone_update_extent_map(inode, trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003729 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003730 new_key.offset - drop_start);
3731
Filipe Manana14f59792014-06-29 21:45:40 +01003732 clone_update_extent_map(inode, trans, path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003733
Sage Weilc5c9cd42008-11-12 14:32:25 -05003734 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003735 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003736
Filipe Manana62e23902014-08-08 02:47:06 +01003737 last_dest_end = ALIGN(new_key.offset + datal,
3738 root->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003739 ret = clone_finish_inode_update(trans, inode,
3740 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003741 destoff, olen,
3742 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003743 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003744 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003745 if (new_key.offset + datal >= destoff + len)
3746 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003747 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003748 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003749 key.offset = next_key_min_offset;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003750 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003751 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003752
Filipe Mananaf82a9902014-06-01 01:50:28 +01003753 if (last_dest_end < destoff + len) {
3754 /*
3755 * We have an implicit hole (NO_HOLES feature is enabled) that
3756 * fully or partially overlaps our cloning range at its end.
3757 */
3758 btrfs_release_path(path);
3759
3760 /*
3761 * 1 - remove extent(s)
3762 * 1 - inode update
3763 */
3764 trans = btrfs_start_transaction(root, 2);
3765 if (IS_ERR(trans)) {
3766 ret = PTR_ERR(trans);
3767 goto out;
3768 }
3769 ret = btrfs_drop_extents(trans, root, inode,
3770 last_dest_end, destoff + len, 1);
3771 if (ret) {
3772 if (ret != -EOPNOTSUPP)
3773 btrfs_abort_transaction(trans, root, ret);
3774 btrfs_end_transaction(trans, root);
3775 goto out;
3776 }
Filipe Manana14f59792014-06-29 21:45:40 +01003777 clone_update_extent_map(inode, trans, NULL, last_dest_end,
3778 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003779 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003780 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003781 }
3782
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003783out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003784 btrfs_free_path(path);
3785 vfree(buf);
3786 return ret;
3787}
3788
3789static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3790 u64 off, u64 olen, u64 destoff)
3791{
Al Viro54563d42013-09-01 15:57:51 -04003792 struct inode *inode = file_inode(file);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003793 struct btrfs_root *root = BTRFS_I(inode)->root;
3794 struct fd src_file;
3795 struct inode *src;
3796 int ret;
3797 u64 len = olen;
3798 u64 bs = root->fs_info->sb->s_blocksize;
3799 int same_inode = 0;
3800
3801 /*
3802 * TODO:
3803 * - split compressed inline extents. annoying: we need to
3804 * decompress into destination's address_space (the file offset
3805 * may change, so source mapping won't do), then recompress (or
3806 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003807 *
3808 * - split destination inode's inline extents. The inline extents can
3809 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003810 */
3811
3812 /* the destination must be opened for writing */
3813 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3814 return -EINVAL;
3815
3816 if (btrfs_root_readonly(root))
3817 return -EROFS;
3818
3819 ret = mnt_want_write_file(file);
3820 if (ret)
3821 return ret;
3822
3823 src_file = fdget(srcfd);
3824 if (!src_file.file) {
3825 ret = -EBADF;
3826 goto out_drop_write;
3827 }
3828
3829 ret = -EXDEV;
3830 if (src_file.file->f_path.mnt != file->f_path.mnt)
3831 goto out_fput;
3832
3833 src = file_inode(src_file.file);
3834
3835 ret = -EINVAL;
3836 if (src == inode)
3837 same_inode = 1;
3838
3839 /* the src must be open for reading */
3840 if (!(src_file.file->f_mode & FMODE_READ))
3841 goto out_fput;
3842
3843 /* don't make the dst file partly checksummed */
3844 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3845 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3846 goto out_fput;
3847
3848 ret = -EISDIR;
3849 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3850 goto out_fput;
3851
3852 ret = -EXDEV;
3853 if (src->i_sb != inode->i_sb)
3854 goto out_fput;
3855
3856 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003857 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003858 } else {
3859 mutex_lock(&src->i_mutex);
3860 }
3861
3862 /* determine range to clone */
3863 ret = -EINVAL;
3864 if (off + len > src->i_size || off + len < off)
3865 goto out_unlock;
3866 if (len == 0)
3867 olen = len = src->i_size - off;
3868 /* if we extend to eof, continue to block boundary */
3869 if (off + len == src->i_size)
3870 len = ALIGN(src->i_size, bs) - off;
3871
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003872 if (len == 0) {
3873 ret = 0;
3874 goto out_unlock;
3875 }
3876
Mark Fasheh32b7c682013-08-06 11:42:49 -07003877 /* verify the end result is block aligned */
3878 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3879 !IS_ALIGNED(destoff, bs))
3880 goto out_unlock;
3881
3882 /* verify if ranges are overlapped within the same file */
3883 if (same_inode) {
3884 if (destoff + len > off && destoff < off + len)
3885 goto out_unlock;
3886 }
3887
3888 if (destoff > inode->i_size) {
3889 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3890 if (ret)
3891 goto out_unlock;
3892 }
3893
Filipe Mananac125b8b2014-05-23 05:03:34 +01003894 /*
3895 * Lock the target range too. Right after we replace the file extent
3896 * items in the fs tree (which now point to the cloned data), we might
3897 * have a worker replace them with extent items relative to a write
3898 * operation that was issued before this clone operation (i.e. confront
3899 * with inode.c:btrfs_finish_ordered_io).
3900 */
3901 if (same_inode) {
3902 u64 lock_start = min_t(u64, off, destoff);
3903 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003904
Filipe Mananac125b8b2014-05-23 05:03:34 +01003905 lock_extent_range(src, lock_start, lock_len);
3906 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003907 btrfs_double_extent_lock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003908 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003909
Mark Fasheh1c919a52015-06-30 14:42:08 -07003910 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003911
Filipe Mananac125b8b2014-05-23 05:03:34 +01003912 if (same_inode) {
3913 u64 lock_start = min_t(u64, off, destoff);
3914 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3915
3916 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3917 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003918 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003919 }
3920 /*
3921 * Truncate page cache pages so that future reads will see the cloned
3922 * data immediately and not the previous data.
3923 */
3924 truncate_inode_pages_range(&inode->i_data, destoff,
3925 PAGE_CACHE_ALIGN(destoff + len) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003926out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003927 if (!same_inode)
3928 btrfs_double_inode_unlock(src, inode);
3929 else
Filipe David Borba Mananac57c2b32014-01-11 21:31:25 +00003930 mutex_unlock(&src->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003931out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003932 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003933out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003934 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003935 return ret;
3936}
3937
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003938static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003939{
3940 struct btrfs_ioctl_clone_range_args args;
3941
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003942 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003943 return -EFAULT;
3944 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3945 args.src_length, args.dest_offset);
3946}
3947
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003948/*
3949 * there are many ways the trans_start and trans_end ioctls can lead
3950 * to deadlocks. They should only be used by applications that
3951 * basically own the machine, and have a very in depth understanding
3952 * of all the possible deadlocks and enospc problems.
3953 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003954static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003955{
Al Viro496ad9a2013-01-23 17:07:38 -05003956 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003957 struct btrfs_root *root = BTRFS_I(inode)->root;
3958 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003959 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003960
Sage Weil1ab86ae2009-09-29 18:38:44 -04003961 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003962 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003963 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003964
3965 ret = -EINPROGRESS;
3966 if (file->private_data)
3967 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003968
Li Zefanb83cc962010-12-20 16:04:08 +08003969 ret = -EROFS;
3970 if (btrfs_root_readonly(root))
3971 goto out;
3972
Al Viroa561be72011-11-23 11:57:51 -05003973 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003974 if (ret)
3975 goto out;
3976
Josef Bacika4abeea2011-04-11 17:25:13 -04003977 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003978
Sage Weil1ab86ae2009-09-29 18:38:44 -04003979 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003980 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003981 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003982 goto out_drop;
3983
3984 file->private_data = trans;
3985 return 0;
3986
3987out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003988 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003989 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003990out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003991 return ret;
3992}
3993
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003994static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3995{
Al Viro496ad9a2013-01-23 17:07:38 -05003996 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003997 struct btrfs_root *root = BTRFS_I(inode)->root;
3998 struct btrfs_root *new_root;
3999 struct btrfs_dir_item *di;
4000 struct btrfs_trans_handle *trans;
4001 struct btrfs_path *path;
4002 struct btrfs_key location;
4003 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004004 u64 objectid = 0;
4005 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004006 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004007
4008 if (!capable(CAP_SYS_ADMIN))
4009 return -EPERM;
4010
Miao Xie3c04ce02012-11-26 08:43:07 +00004011 ret = mnt_want_write_file(file);
4012 if (ret)
4013 return ret;
4014
4015 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4016 ret = -EFAULT;
4017 goto out;
4018 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004019
4020 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304021 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004022
4023 location.objectid = objectid;
4024 location.type = BTRFS_ROOT_ITEM_KEY;
4025 location.offset = (u64)-1;
4026
4027 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004028 if (IS_ERR(new_root)) {
4029 ret = PTR_ERR(new_root);
4030 goto out;
4031 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004032
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004033 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004034 if (!path) {
4035 ret = -ENOMEM;
4036 goto out;
4037 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004038 path->leave_spinning = 1;
4039
4040 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004041 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004042 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004043 ret = PTR_ERR(trans);
4044 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004045 }
4046
David Sterba6c417612011-04-13 15:41:04 +02004047 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004048 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
4049 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004050 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004051 btrfs_free_path(path);
4052 btrfs_end_transaction(trans, root);
Frank Holtonefe120a2013-12-20 11:37:06 -05004053 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
4054 "item, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004055 ret = -ENOENT;
4056 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004057 }
4058
4059 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4060 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4061 btrfs_mark_buffer_dirty(path->nodes[0]);
4062 btrfs_free_path(path);
4063
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06004064 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004065 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00004066out:
4067 mnt_drop_write_file(file);
4068 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004069}
4070
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004071void btrfs_get_block_group_info(struct list_head *groups_list,
4072 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004073{
4074 struct btrfs_block_group_cache *block_group;
4075
4076 space->total_bytes = 0;
4077 space->used_bytes = 0;
4078 space->flags = 0;
4079 list_for_each_entry(block_group, groups_list, list) {
4080 space->flags = block_group->flags;
4081 space->total_bytes += block_group->key.offset;
4082 space->used_bytes +=
4083 btrfs_block_group_used(&block_group->item);
4084 }
4085}
4086
Eric Sandeen48a3b632013-04-25 20:41:01 +00004087static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004088{
4089 struct btrfs_ioctl_space_args space_args;
4090 struct btrfs_ioctl_space_info space;
4091 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004092 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004093 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004094 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004095 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4096 BTRFS_BLOCK_GROUP_SYSTEM,
4097 BTRFS_BLOCK_GROUP_METADATA,
4098 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4099 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004100 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004101 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004102 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004103 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004104
4105 if (copy_from_user(&space_args,
4106 (struct btrfs_ioctl_space_args __user *)arg,
4107 sizeof(space_args)))
4108 return -EFAULT;
4109
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004110 for (i = 0; i < num_types; i++) {
4111 struct btrfs_space_info *tmp;
4112
4113 info = NULL;
4114 rcu_read_lock();
4115 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4116 list) {
4117 if (tmp->flags == types[i]) {
4118 info = tmp;
4119 break;
4120 }
4121 }
4122 rcu_read_unlock();
4123
4124 if (!info)
4125 continue;
4126
4127 down_read(&info->groups_sem);
4128 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4129 if (!list_empty(&info->block_groups[c]))
4130 slot_count++;
4131 }
4132 up_read(&info->groups_sem);
4133 }
Josef Bacik1406e432010-01-13 18:19:06 +00004134
David Sterba36523e952014-02-07 14:34:12 +01004135 /*
4136 * Global block reserve, exported as a space_info
4137 */
4138 slot_count++;
4139
Chris Mason7fde62b2010-03-16 15:40:10 -04004140 /* space_slots == 0 means they are asking for a count */
4141 if (space_args.space_slots == 0) {
4142 space_args.total_spaces = slot_count;
4143 goto out;
4144 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004145
Dan Rosenberg51788b12011-02-14 16:04:23 -05004146 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004147
Chris Mason7fde62b2010-03-16 15:40:10 -04004148 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004149
Chris Mason7fde62b2010-03-16 15:40:10 -04004150 /* we generally have at most 6 or so space infos, one for each raid
4151 * level. So, a whole page should be more than enough for everyone
4152 */
4153 if (alloc_size > PAGE_CACHE_SIZE)
4154 return -ENOMEM;
4155
4156 space_args.total_spaces = 0;
4157 dest = kmalloc(alloc_size, GFP_NOFS);
4158 if (!dest)
4159 return -ENOMEM;
4160 dest_orig = dest;
4161
4162 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004163 for (i = 0; i < num_types; i++) {
4164 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004165
Dan Rosenberg51788b12011-02-14 16:04:23 -05004166 if (!slot_count)
4167 break;
4168
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004169 info = NULL;
4170 rcu_read_lock();
4171 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4172 list) {
4173 if (tmp->flags == types[i]) {
4174 info = tmp;
4175 break;
4176 }
4177 }
4178 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004179
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004180 if (!info)
4181 continue;
4182 down_read(&info->groups_sem);
4183 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4184 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004185 btrfs_get_block_group_info(
4186 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004187 memcpy(dest, &space, sizeof(space));
4188 dest++;
4189 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004190 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004191 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004192 if (!slot_count)
4193 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004194 }
4195 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004196 }
Josef Bacik1406e432010-01-13 18:19:06 +00004197
David Sterba36523e952014-02-07 14:34:12 +01004198 /*
4199 * Add global block reserve
4200 */
4201 if (slot_count) {
4202 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
4203
4204 spin_lock(&block_rsv->lock);
4205 space.total_bytes = block_rsv->size;
4206 space.used_bytes = block_rsv->size - block_rsv->reserved;
4207 spin_unlock(&block_rsv->lock);
4208 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4209 memcpy(dest, &space, sizeof(space));
4210 space_args.total_spaces++;
4211 }
4212
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004213 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004214 (arg + sizeof(struct btrfs_ioctl_space_args));
4215
4216 if (copy_to_user(user_dest, dest_orig, alloc_size))
4217 ret = -EFAULT;
4218
4219 kfree(dest_orig);
4220out:
4221 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004222 ret = -EFAULT;
4223
4224 return ret;
4225}
4226
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004227/*
4228 * there are many ways the trans_start and trans_end ioctls can lead
4229 * to deadlocks. They should only be used by applications that
4230 * basically own the machine, and have a very in depth understanding
4231 * of all the possible deadlocks and enospc problems.
4232 */
4233long btrfs_ioctl_trans_end(struct file *file)
4234{
Al Viro496ad9a2013-01-23 17:07:38 -05004235 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004236 struct btrfs_root *root = BTRFS_I(inode)->root;
4237 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004238
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004239 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004240 if (!trans)
4241 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004242 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004243
Sage Weil1ab86ae2009-09-29 18:38:44 -04004244 btrfs_end_transaction(trans, root);
4245
Josef Bacika4abeea2011-04-11 17:25:13 -04004246 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004247
Al Viro2a79f172011-12-09 08:06:57 -05004248 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004249 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004250}
4251
Miao Xie9a8c28b2012-11-26 08:40:43 +00004252static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4253 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004254{
Sage Weil46204592010-10-29 15:41:32 -04004255 struct btrfs_trans_handle *trans;
4256 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004257 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004258
Miao Xied4edf392013-02-20 09:17:06 +00004259 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004260 if (IS_ERR(trans)) {
4261 if (PTR_ERR(trans) != -ENOENT)
4262 return PTR_ERR(trans);
4263
4264 /* No running transaction, don't bother */
4265 transid = root->fs_info->last_trans_committed;
4266 goto out;
4267 }
Sage Weil46204592010-10-29 15:41:32 -04004268 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004269 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004270 if (ret) {
4271 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004272 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004273 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004274out:
Sage Weil46204592010-10-29 15:41:32 -04004275 if (argp)
4276 if (copy_to_user(argp, &transid, sizeof(transid)))
4277 return -EFAULT;
4278 return 0;
4279}
4280
Miao Xie9a8c28b2012-11-26 08:40:43 +00004281static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4282 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004283{
Sage Weil46204592010-10-29 15:41:32 -04004284 u64 transid;
4285
4286 if (argp) {
4287 if (copy_from_user(&transid, argp, sizeof(transid)))
4288 return -EFAULT;
4289 } else {
4290 transid = 0; /* current trans */
4291 }
4292 return btrfs_wait_for_commit(root, transid);
4293}
4294
Miao Xieb8e95482012-11-26 08:48:01 +00004295static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004296{
Al Viro496ad9a2013-01-23 17:07:38 -05004297 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01004298 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004299 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004300
4301 if (!capable(CAP_SYS_ADMIN))
4302 return -EPERM;
4303
4304 sa = memdup_user(arg, sizeof(*sa));
4305 if (IS_ERR(sa))
4306 return PTR_ERR(sa);
4307
Miao Xieb8e95482012-11-26 08:48:01 +00004308 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4309 ret = mnt_want_write_file(file);
4310 if (ret)
4311 goto out;
4312 }
4313
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004314 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004315 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4316 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004317
4318 if (copy_to_user(arg, sa, sizeof(*sa)))
4319 ret = -EFAULT;
4320
Miao Xieb8e95482012-11-26 08:48:01 +00004321 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4322 mnt_drop_write_file(file);
4323out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004324 kfree(sa);
4325 return ret;
4326}
4327
4328static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4329{
4330 if (!capable(CAP_SYS_ADMIN))
4331 return -EPERM;
4332
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004333 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004334}
4335
4336static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4337 void __user *arg)
4338{
4339 struct btrfs_ioctl_scrub_args *sa;
4340 int ret;
4341
4342 if (!capable(CAP_SYS_ADMIN))
4343 return -EPERM;
4344
4345 sa = memdup_user(arg, sizeof(*sa));
4346 if (IS_ERR(sa))
4347 return PTR_ERR(sa);
4348
4349 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4350
4351 if (copy_to_user(arg, sa, sizeof(*sa)))
4352 ret = -EFAULT;
4353
4354 kfree(sa);
4355 return ret;
4356}
4357
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004358static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004359 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004360{
4361 struct btrfs_ioctl_get_dev_stats *sa;
4362 int ret;
4363
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004364 sa = memdup_user(arg, sizeof(*sa));
4365 if (IS_ERR(sa))
4366 return PTR_ERR(sa);
4367
David Sterbab27f7c02012-06-22 06:30:39 -06004368 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4369 kfree(sa);
4370 return -EPERM;
4371 }
4372
4373 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004374
4375 if (copy_to_user(arg, sa, sizeof(*sa)))
4376 ret = -EFAULT;
4377
4378 kfree(sa);
4379 return ret;
4380}
4381
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004382static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4383{
4384 struct btrfs_ioctl_dev_replace_args *p;
4385 int ret;
4386
4387 if (!capable(CAP_SYS_ADMIN))
4388 return -EPERM;
4389
4390 p = memdup_user(arg, sizeof(*p));
4391 if (IS_ERR(p))
4392 return PTR_ERR(p);
4393
4394 switch (p->cmd) {
4395 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004396 if (root->fs_info->sb->s_flags & MS_RDONLY) {
4397 ret = -EROFS;
4398 goto out;
4399 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004400 if (atomic_xchg(
4401 &root->fs_info->mutually_exclusive_operation_running,
4402 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004403 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004404 } else {
4405 ret = btrfs_dev_replace_start(root, p);
4406 atomic_set(
4407 &root->fs_info->mutually_exclusive_operation_running,
4408 0);
4409 }
4410 break;
4411 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4412 btrfs_dev_replace_status(root->fs_info, p);
4413 ret = 0;
4414 break;
4415 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4416 ret = btrfs_dev_replace_cancel(root->fs_info, p);
4417 break;
4418 default:
4419 ret = -EINVAL;
4420 break;
4421 }
4422
4423 if (copy_to_user(arg, p, sizeof(*p)))
4424 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004425out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004426 kfree(p);
4427 return ret;
4428}
4429
Jan Schmidtd7728c92011-07-07 16:48:38 +02004430static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4431{
4432 int ret = 0;
4433 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004434 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004435 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004436 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004437 struct inode_fs_paths *ipath = NULL;
4438 struct btrfs_path *path;
4439
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004440 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004441 return -EPERM;
4442
4443 path = btrfs_alloc_path();
4444 if (!path) {
4445 ret = -ENOMEM;
4446 goto out;
4447 }
4448
4449 ipa = memdup_user(arg, sizeof(*ipa));
4450 if (IS_ERR(ipa)) {
4451 ret = PTR_ERR(ipa);
4452 ipa = NULL;
4453 goto out;
4454 }
4455
4456 size = min_t(u32, ipa->size, 4096);
4457 ipath = init_ipath(size, root, path);
4458 if (IS_ERR(ipath)) {
4459 ret = PTR_ERR(ipath);
4460 ipath = NULL;
4461 goto out;
4462 }
4463
4464 ret = paths_from_inode(ipa->inum, ipath);
4465 if (ret < 0)
4466 goto out;
4467
4468 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004469 rel_ptr = ipath->fspath->val[i] -
4470 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004471 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004472 }
4473
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004474 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4475 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004476 if (ret) {
4477 ret = -EFAULT;
4478 goto out;
4479 }
4480
4481out:
4482 btrfs_free_path(path);
4483 free_ipath(ipath);
4484 kfree(ipa);
4485
4486 return ret;
4487}
4488
4489static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4490{
4491 struct btrfs_data_container *inodes = ctx;
4492 const size_t c = 3 * sizeof(u64);
4493
4494 if (inodes->bytes_left >= c) {
4495 inodes->bytes_left -= c;
4496 inodes->val[inodes->elem_cnt] = inum;
4497 inodes->val[inodes->elem_cnt + 1] = offset;
4498 inodes->val[inodes->elem_cnt + 2] = root;
4499 inodes->elem_cnt += 3;
4500 } else {
4501 inodes->bytes_missing += c - inodes->bytes_left;
4502 inodes->bytes_left = 0;
4503 inodes->elem_missed += 3;
4504 }
4505
4506 return 0;
4507}
4508
4509static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4510 void __user *arg)
4511{
4512 int ret = 0;
4513 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004514 struct btrfs_ioctl_logical_ino_args *loi;
4515 struct btrfs_data_container *inodes = NULL;
4516 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004517
4518 if (!capable(CAP_SYS_ADMIN))
4519 return -EPERM;
4520
4521 loi = memdup_user(arg, sizeof(*loi));
4522 if (IS_ERR(loi)) {
4523 ret = PTR_ERR(loi);
4524 loi = NULL;
4525 goto out;
4526 }
4527
4528 path = btrfs_alloc_path();
4529 if (!path) {
4530 ret = -ENOMEM;
4531 goto out;
4532 }
4533
Liu Bo425d17a2012-09-07 20:01:30 -06004534 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004535 inodes = init_data_container(size);
4536 if (IS_ERR(inodes)) {
4537 ret = PTR_ERR(inodes);
4538 inodes = NULL;
4539 goto out;
4540 }
4541
Liu Bodf031f02012-09-07 20:01:29 -06004542 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4543 build_ino_list, inodes);
4544 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004545 ret = -ENOENT;
4546 if (ret < 0)
4547 goto out;
4548
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004549 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4550 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004551 if (ret)
4552 ret = -EFAULT;
4553
4554out:
4555 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06004556 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004557 kfree(loi);
4558
4559 return ret;
4560}
4561
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004562void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004563 struct btrfs_ioctl_balance_args *bargs)
4564{
4565 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4566
4567 bargs->flags = bctl->flags;
4568
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004569 if (atomic_read(&fs_info->balance_running))
4570 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4571 if (atomic_read(&fs_info->balance_pause_req))
4572 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004573 if (atomic_read(&fs_info->balance_cancel_req))
4574 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004575
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004576 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4577 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4578 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004579
4580 if (lock) {
4581 spin_lock(&fs_info->balance_lock);
4582 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4583 spin_unlock(&fs_info->balance_lock);
4584 } else {
4585 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4586 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004587}
4588
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004589static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004590{
Al Viro496ad9a2013-01-23 17:07:38 -05004591 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004592 struct btrfs_fs_info *fs_info = root->fs_info;
4593 struct btrfs_ioctl_balance_args *bargs;
4594 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004595 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004596 int ret;
4597
4598 if (!capable(CAP_SYS_ADMIN))
4599 return -EPERM;
4600
Liu Boe54bfa32012-06-29 03:58:48 -06004601 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004602 if (ret)
4603 return ret;
4604
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004605again:
4606 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4607 mutex_lock(&fs_info->volume_mutex);
4608 mutex_lock(&fs_info->balance_mutex);
4609 need_unlock = true;
4610 goto locked;
4611 }
4612
4613 /*
4614 * mut. excl. ops lock is locked. Three possibilites:
4615 * (1) some other op is running
4616 * (2) balance is running
4617 * (3) balance is paused -- special case (think resume)
4618 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004619 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004620 if (fs_info->balance_ctl) {
4621 /* this is either (2) or (3) */
4622 if (!atomic_read(&fs_info->balance_running)) {
4623 mutex_unlock(&fs_info->balance_mutex);
4624 if (!mutex_trylock(&fs_info->volume_mutex))
4625 goto again;
4626 mutex_lock(&fs_info->balance_mutex);
4627
4628 if (fs_info->balance_ctl &&
4629 !atomic_read(&fs_info->balance_running)) {
4630 /* this is (3) */
4631 need_unlock = false;
4632 goto locked;
4633 }
4634
4635 mutex_unlock(&fs_info->balance_mutex);
4636 mutex_unlock(&fs_info->volume_mutex);
4637 goto again;
4638 } else {
4639 /* this is (2) */
4640 mutex_unlock(&fs_info->balance_mutex);
4641 ret = -EINPROGRESS;
4642 goto out;
4643 }
4644 } else {
4645 /* this is (1) */
4646 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004647 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004648 goto out;
4649 }
4650
4651locked:
4652 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004653
4654 if (arg) {
4655 bargs = memdup_user(arg, sizeof(*bargs));
4656 if (IS_ERR(bargs)) {
4657 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004658 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004659 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004660
4661 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4662 if (!fs_info->balance_ctl) {
4663 ret = -ENOTCONN;
4664 goto out_bargs;
4665 }
4666
4667 bctl = fs_info->balance_ctl;
4668 spin_lock(&fs_info->balance_lock);
4669 bctl->flags |= BTRFS_BALANCE_RESUME;
4670 spin_unlock(&fs_info->balance_lock);
4671
4672 goto do_balance;
4673 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004674 } else {
4675 bargs = NULL;
4676 }
4677
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004678 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004679 ret = -EINPROGRESS;
4680 goto out_bargs;
4681 }
4682
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004683 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4684 if (!bctl) {
4685 ret = -ENOMEM;
4686 goto out_bargs;
4687 }
4688
4689 bctl->fs_info = fs_info;
4690 if (arg) {
4691 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4692 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4693 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4694
4695 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004696 } else {
4697 /* balance everything - no filters */
4698 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004699 }
4700
David Sterba8eb93452015-10-12 16:55:54 +02004701 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4702 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004703 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004704 }
4705
Ilya Dryomovde322262012-01-16 22:04:49 +02004706do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004707 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004708 * Ownership of bctl and mutually_exclusive_operation_running
4709 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4710 * or, if restriper was paused all the way until unmount, in
4711 * free_fs_info. mutually_exclusive_operation_running is
4712 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004713 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004714 need_unlock = false;
4715
4716 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004717 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004718
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004719 if (arg) {
4720 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4721 ret = -EFAULT;
4722 }
4723
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004724out_bctl:
4725 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004726out_bargs:
4727 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004728out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004729 mutex_unlock(&fs_info->balance_mutex);
4730 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004731 if (need_unlock)
4732 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4733out:
Liu Boe54bfa32012-06-29 03:58:48 -06004734 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004735 return ret;
4736}
4737
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004738static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4739{
4740 if (!capable(CAP_SYS_ADMIN))
4741 return -EPERM;
4742
4743 switch (cmd) {
4744 case BTRFS_BALANCE_CTL_PAUSE:
4745 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004746 case BTRFS_BALANCE_CTL_CANCEL:
4747 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004748 }
4749
4750 return -EINVAL;
4751}
4752
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004753static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4754 void __user *arg)
4755{
4756 struct btrfs_fs_info *fs_info = root->fs_info;
4757 struct btrfs_ioctl_balance_args *bargs;
4758 int ret = 0;
4759
4760 if (!capable(CAP_SYS_ADMIN))
4761 return -EPERM;
4762
4763 mutex_lock(&fs_info->balance_mutex);
4764 if (!fs_info->balance_ctl) {
4765 ret = -ENOTCONN;
4766 goto out;
4767 }
4768
4769 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4770 if (!bargs) {
4771 ret = -ENOMEM;
4772 goto out;
4773 }
4774
4775 update_ioctl_balance_args(fs_info, 1, bargs);
4776
4777 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4778 ret = -EFAULT;
4779
4780 kfree(bargs);
4781out:
4782 mutex_unlock(&fs_info->balance_mutex);
4783 return ret;
4784}
4785
Miao Xie905b0dd2012-11-26 08:50:11 +00004786static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004787{
Al Viro496ad9a2013-01-23 17:07:38 -05004788 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004789 struct btrfs_ioctl_quota_ctl_args *sa;
4790 struct btrfs_trans_handle *trans = NULL;
4791 int ret;
4792 int err;
4793
4794 if (!capable(CAP_SYS_ADMIN))
4795 return -EPERM;
4796
Miao Xie905b0dd2012-11-26 08:50:11 +00004797 ret = mnt_want_write_file(file);
4798 if (ret)
4799 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004800
4801 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004802 if (IS_ERR(sa)) {
4803 ret = PTR_ERR(sa);
4804 goto drop_write;
4805 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004806
Wang Shilong7708f022013-04-07 10:24:57 +00004807 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004808 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4809 if (IS_ERR(trans)) {
4810 ret = PTR_ERR(trans);
4811 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004812 }
4813
4814 switch (sa->cmd) {
4815 case BTRFS_QUOTA_CTL_ENABLE:
4816 ret = btrfs_quota_enable(trans, root->fs_info);
4817 break;
4818 case BTRFS_QUOTA_CTL_DISABLE:
4819 ret = btrfs_quota_disable(trans, root->fs_info);
4820 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004821 default:
4822 ret = -EINVAL;
4823 break;
4824 }
4825
Jan Schmidt2f232032013-04-25 16:04:51 +00004826 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4827 if (err && !ret)
4828 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004829out:
4830 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004831 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004832drop_write:
4833 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004834 return ret;
4835}
4836
Miao Xie905b0dd2012-11-26 08:50:11 +00004837static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004838{
Al Viro496ad9a2013-01-23 17:07:38 -05004839 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004840 struct btrfs_ioctl_qgroup_assign_args *sa;
4841 struct btrfs_trans_handle *trans;
4842 int ret;
4843 int err;
4844
4845 if (!capable(CAP_SYS_ADMIN))
4846 return -EPERM;
4847
Miao Xie905b0dd2012-11-26 08:50:11 +00004848 ret = mnt_want_write_file(file);
4849 if (ret)
4850 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004851
4852 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004853 if (IS_ERR(sa)) {
4854 ret = PTR_ERR(sa);
4855 goto drop_write;
4856 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004857
4858 trans = btrfs_join_transaction(root);
4859 if (IS_ERR(trans)) {
4860 ret = PTR_ERR(trans);
4861 goto out;
4862 }
4863
4864 /* FIXME: check if the IDs really exist */
4865 if (sa->assign) {
4866 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4867 sa->src, sa->dst);
4868 } else {
4869 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4870 sa->src, sa->dst);
4871 }
4872
Qu Wenruoe082f562015-02-27 16:24:28 +08004873 /* update qgroup status and info */
4874 err = btrfs_run_qgroups(trans, root->fs_info);
4875 if (err < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08004876 btrfs_std_error(root->fs_info, ret,
Qu Wenruoe082f562015-02-27 16:24:28 +08004877 "failed to update qgroup status and info\n");
Arne Jansen5d13a372011-09-14 15:53:51 +02004878 err = btrfs_end_transaction(trans, root);
4879 if (err && !ret)
4880 ret = err;
4881
4882out:
4883 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004884drop_write:
4885 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004886 return ret;
4887}
4888
Miao Xie905b0dd2012-11-26 08:50:11 +00004889static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004890{
Al Viro496ad9a2013-01-23 17:07:38 -05004891 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004892 struct btrfs_ioctl_qgroup_create_args *sa;
4893 struct btrfs_trans_handle *trans;
4894 int ret;
4895 int err;
4896
4897 if (!capable(CAP_SYS_ADMIN))
4898 return -EPERM;
4899
Miao Xie905b0dd2012-11-26 08:50:11 +00004900 ret = mnt_want_write_file(file);
4901 if (ret)
4902 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004903
4904 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004905 if (IS_ERR(sa)) {
4906 ret = PTR_ERR(sa);
4907 goto drop_write;
4908 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004909
Miao Xied86e56c2012-11-15 11:35:41 +00004910 if (!sa->qgroupid) {
4911 ret = -EINVAL;
4912 goto out;
4913 }
4914
Arne Jansen5d13a372011-09-14 15:53:51 +02004915 trans = btrfs_join_transaction(root);
4916 if (IS_ERR(trans)) {
4917 ret = PTR_ERR(trans);
4918 goto out;
4919 }
4920
4921 /* FIXME: check if the IDs really exist */
4922 if (sa->create) {
Dongsheng Yang4087cf22015-01-18 10:59:23 -05004923 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004924 } else {
4925 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4926 }
4927
4928 err = btrfs_end_transaction(trans, root);
4929 if (err && !ret)
4930 ret = err;
4931
4932out:
4933 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004934drop_write:
4935 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004936 return ret;
4937}
4938
Miao Xie905b0dd2012-11-26 08:50:11 +00004939static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004940{
Al Viro496ad9a2013-01-23 17:07:38 -05004941 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004942 struct btrfs_ioctl_qgroup_limit_args *sa;
4943 struct btrfs_trans_handle *trans;
4944 int ret;
4945 int err;
4946 u64 qgroupid;
4947
4948 if (!capable(CAP_SYS_ADMIN))
4949 return -EPERM;
4950
Miao Xie905b0dd2012-11-26 08:50:11 +00004951 ret = mnt_want_write_file(file);
4952 if (ret)
4953 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004954
4955 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004956 if (IS_ERR(sa)) {
4957 ret = PTR_ERR(sa);
4958 goto drop_write;
4959 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004960
4961 trans = btrfs_join_transaction(root);
4962 if (IS_ERR(trans)) {
4963 ret = PTR_ERR(trans);
4964 goto out;
4965 }
4966
4967 qgroupid = sa->qgroupid;
4968 if (!qgroupid) {
4969 /* take the current subvol as qgroup */
4970 qgroupid = root->root_key.objectid;
4971 }
4972
4973 /* FIXME: check if the IDs really exist */
4974 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4975
4976 err = btrfs_end_transaction(trans, root);
4977 if (err && !ret)
4978 ret = err;
4979
4980out:
4981 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004982drop_write:
4983 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004984 return ret;
4985}
4986
Jan Schmidt2f232032013-04-25 16:04:51 +00004987static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4988{
Al Viro6d0379e2013-06-16 19:32:35 +04004989 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004990 struct btrfs_ioctl_quota_rescan_args *qsa;
4991 int ret;
4992
4993 if (!capable(CAP_SYS_ADMIN))
4994 return -EPERM;
4995
4996 ret = mnt_want_write_file(file);
4997 if (ret)
4998 return ret;
4999
5000 qsa = memdup_user(arg, sizeof(*qsa));
5001 if (IS_ERR(qsa)) {
5002 ret = PTR_ERR(qsa);
5003 goto drop_write;
5004 }
5005
5006 if (qsa->flags) {
5007 ret = -EINVAL;
5008 goto out;
5009 }
5010
5011 ret = btrfs_qgroup_rescan(root->fs_info);
5012
5013out:
5014 kfree(qsa);
5015drop_write:
5016 mnt_drop_write_file(file);
5017 return ret;
5018}
5019
5020static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5021{
Al Viro6d0379e2013-06-16 19:32:35 +04005022 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00005023 struct btrfs_ioctl_quota_rescan_args *qsa;
5024 int ret = 0;
5025
5026 if (!capable(CAP_SYS_ADMIN))
5027 return -EPERM;
5028
5029 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
5030 if (!qsa)
5031 return -ENOMEM;
5032
5033 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5034 qsa->flags = 1;
5035 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
5036 }
5037
5038 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5039 ret = -EFAULT;
5040
5041 kfree(qsa);
5042 return ret;
5043}
5044
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005045static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5046{
Al Viro54563d42013-09-01 15:57:51 -04005047 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005048
5049 if (!capable(CAP_SYS_ADMIN))
5050 return -EPERM;
5051
5052 return btrfs_qgroup_wait_for_completion(root->fs_info);
5053}
5054
Hugo Millsabccd002014-01-30 20:17:00 +00005055static long _btrfs_ioctl_set_received_subvol(struct file *file,
5056 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005057{
Al Viro496ad9a2013-01-23 17:07:38 -05005058 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02005059 struct btrfs_root *root = BTRFS_I(inode)->root;
5060 struct btrfs_root_item *root_item = &root->root_item;
5061 struct btrfs_trans_handle *trans;
5062 struct timespec ct = CURRENT_TIME;
5063 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005064 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005065
David Sterbabd60ea02014-01-16 15:50:22 +01005066 if (!inode_owner_or_capable(inode))
5067 return -EPERM;
5068
Alexander Block8ea05e32012-07-25 17:35:53 +02005069 ret = mnt_want_write_file(file);
5070 if (ret < 0)
5071 return ret;
5072
5073 down_write(&root->fs_info->subvol_sem);
5074
5075 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
5076 ret = -EINVAL;
5077 goto out;
5078 }
5079
5080 if (btrfs_root_readonly(root)) {
5081 ret = -EROFS;
5082 goto out;
5083 }
5084
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005085 /*
5086 * 1 - root item
5087 * 2 - uuid items (received uuid + subvol uuid)
5088 */
5089 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005090 if (IS_ERR(trans)) {
5091 ret = PTR_ERR(trans);
5092 trans = NULL;
5093 goto out;
5094 }
5095
5096 sa->rtransid = trans->transid;
5097 sa->rtime.sec = ct.tv_sec;
5098 sa->rtime.nsec = ct.tv_nsec;
5099
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005100 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5101 BTRFS_UUID_SIZE);
5102 if (received_uuid_changed &&
5103 !btrfs_is_empty_uuid(root_item->received_uuid))
5104 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
5105 root_item->received_uuid,
5106 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5107 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005108 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5109 btrfs_set_root_stransid(root_item, sa->stransid);
5110 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005111 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5112 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5113 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5114 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005115
5116 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5117 &root->root_key, &root->root_item);
5118 if (ret < 0) {
5119 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02005120 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005121 }
5122 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5123 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
5124 sa->uuid,
5125 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5126 root->root_key.objectid);
5127 if (ret < 0 && ret != -EEXIST) {
5128 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005129 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005130 }
5131 }
5132 ret = btrfs_commit_transaction(trans, root);
5133 if (ret < 0) {
5134 btrfs_abort_transaction(trans, root, ret);
5135 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005136 }
5137
Hugo Millsabccd002014-01-30 20:17:00 +00005138out:
5139 up_write(&root->fs_info->subvol_sem);
5140 mnt_drop_write_file(file);
5141 return ret;
5142}
5143
5144#ifdef CONFIG_64BIT
5145static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5146 void __user *arg)
5147{
5148 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5149 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5150 int ret = 0;
5151
5152 args32 = memdup_user(arg, sizeof(*args32));
5153 if (IS_ERR(args32)) {
5154 ret = PTR_ERR(args32);
5155 args32 = NULL;
5156 goto out;
5157 }
5158
5159 args64 = kmalloc(sizeof(*args64), GFP_NOFS);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005160 if (!args64) {
5161 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005162 goto out;
5163 }
5164
5165 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5166 args64->stransid = args32->stransid;
5167 args64->rtransid = args32->rtransid;
5168 args64->stime.sec = args32->stime.sec;
5169 args64->stime.nsec = args32->stime.nsec;
5170 args64->rtime.sec = args32->rtime.sec;
5171 args64->rtime.nsec = args32->rtime.nsec;
5172 args64->flags = args32->flags;
5173
5174 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5175 if (ret)
5176 goto out;
5177
5178 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5179 args32->stransid = args64->stransid;
5180 args32->rtransid = args64->rtransid;
5181 args32->stime.sec = args64->stime.sec;
5182 args32->stime.nsec = args64->stime.nsec;
5183 args32->rtime.sec = args64->rtime.sec;
5184 args32->rtime.nsec = args64->rtime.nsec;
5185 args32->flags = args64->flags;
5186
5187 ret = copy_to_user(arg, args32, sizeof(*args32));
5188 if (ret)
5189 ret = -EFAULT;
5190
5191out:
5192 kfree(args32);
5193 kfree(args64);
5194 return ret;
5195}
5196#endif
5197
5198static long btrfs_ioctl_set_received_subvol(struct file *file,
5199 void __user *arg)
5200{
5201 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5202 int ret = 0;
5203
5204 sa = memdup_user(arg, sizeof(*sa));
5205 if (IS_ERR(sa)) {
5206 ret = PTR_ERR(sa);
5207 sa = NULL;
5208 goto out;
5209 }
5210
5211 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5212
5213 if (ret)
5214 goto out;
5215
Alexander Block8ea05e32012-07-25 17:35:53 +02005216 ret = copy_to_user(arg, sa, sizeof(*sa));
5217 if (ret)
5218 ret = -EFAULT;
5219
5220out:
5221 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005222 return ret;
5223}
5224
jeff.liu867ab662013-01-05 02:48:01 +00005225static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5226{
Al Viro6d0379e2013-06-16 19:32:35 +04005227 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005228 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005229 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005230 char label[BTRFS_LABEL_SIZE];
5231
5232 spin_lock(&root->fs_info->super_lock);
5233 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5234 spin_unlock(&root->fs_info->super_lock);
5235
5236 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005237
5238 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005239 btrfs_warn(root->fs_info,
5240 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00005241 }
5242
jeff.liu867ab662013-01-05 02:48:01 +00005243 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005244
5245 return ret ? -EFAULT : 0;
5246}
5247
jeff.liua8bfd4a2013-01-05 02:48:08 +00005248static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5249{
Al Viro6d0379e2013-06-16 19:32:35 +04005250 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005251 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5252 struct btrfs_trans_handle *trans;
5253 char label[BTRFS_LABEL_SIZE];
5254 int ret;
5255
5256 if (!capable(CAP_SYS_ADMIN))
5257 return -EPERM;
5258
5259 if (copy_from_user(label, arg, sizeof(label)))
5260 return -EFAULT;
5261
5262 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005263 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
jeff.liua8bfd4a2013-01-05 02:48:08 +00005264 BTRFS_LABEL_SIZE - 1);
5265 return -EINVAL;
5266 }
5267
5268 ret = mnt_want_write_file(file);
5269 if (ret)
5270 return ret;
5271
jeff.liua8bfd4a2013-01-05 02:48:08 +00005272 trans = btrfs_start_transaction(root, 0);
5273 if (IS_ERR(trans)) {
5274 ret = PTR_ERR(trans);
5275 goto out_unlock;
5276 }
5277
Anand Jaina1b83ac2013-07-19 17:39:32 +08005278 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005279 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005280 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005281 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005282
5283out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005284 mnt_drop_write_file(file);
5285 return ret;
5286}
5287
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005288#define INIT_FEATURE_FLAGS(suffix) \
5289 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5290 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5291 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5292
5293static int btrfs_ioctl_get_supported_features(struct file *file,
5294 void __user *arg)
5295{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005296 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005297 INIT_FEATURE_FLAGS(SUPP),
5298 INIT_FEATURE_FLAGS(SAFE_SET),
5299 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5300 };
5301
5302 if (copy_to_user(arg, &features, sizeof(features)))
5303 return -EFAULT;
5304
5305 return 0;
5306}
5307
5308static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5309{
5310 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5311 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5312 struct btrfs_ioctl_feature_flags features;
5313
5314 features.compat_flags = btrfs_super_compat_flags(super_block);
5315 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5316 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5317
5318 if (copy_to_user(arg, &features, sizeof(features)))
5319 return -EFAULT;
5320
5321 return 0;
5322}
5323
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005324static int check_feature_bits(struct btrfs_root *root,
5325 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005326 u64 change_mask, u64 flags, u64 supported_flags,
5327 u64 safe_set, u64 safe_clear)
5328{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005329 const char *type = btrfs_feature_set_names[set];
5330 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005331 u64 disallowed, unsupported;
5332 u64 set_mask = flags & change_mask;
5333 u64 clear_mask = ~flags & change_mask;
5334
5335 unsupported = set_mask & ~supported_flags;
5336 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005337 names = btrfs_printable_features(set, unsupported);
5338 if (names) {
5339 btrfs_warn(root->fs_info,
5340 "this kernel does not support the %s feature bit%s",
5341 names, strchr(names, ',') ? "s" : "");
5342 kfree(names);
5343 } else
5344 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005345 "this kernel does not support %s bits 0x%llx",
5346 type, unsupported);
5347 return -EOPNOTSUPP;
5348 }
5349
5350 disallowed = set_mask & ~safe_set;
5351 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005352 names = btrfs_printable_features(set, disallowed);
5353 if (names) {
5354 btrfs_warn(root->fs_info,
5355 "can't set the %s feature bit%s while mounted",
5356 names, strchr(names, ',') ? "s" : "");
5357 kfree(names);
5358 } else
5359 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005360 "can't set %s bits 0x%llx while mounted",
5361 type, disallowed);
5362 return -EPERM;
5363 }
5364
5365 disallowed = clear_mask & ~safe_clear;
5366 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005367 names = btrfs_printable_features(set, disallowed);
5368 if (names) {
5369 btrfs_warn(root->fs_info,
5370 "can't clear the %s feature bit%s while mounted",
5371 names, strchr(names, ',') ? "s" : "");
5372 kfree(names);
5373 } else
5374 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005375 "can't clear %s bits 0x%llx while mounted",
5376 type, disallowed);
5377 return -EPERM;
5378 }
5379
5380 return 0;
5381}
5382
5383#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005384check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005385 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5386 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5387 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5388
5389static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5390{
5391 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5392 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5393 struct btrfs_ioctl_feature_flags flags[2];
5394 struct btrfs_trans_handle *trans;
5395 u64 newflags;
5396 int ret;
5397
5398 if (!capable(CAP_SYS_ADMIN))
5399 return -EPERM;
5400
5401 if (copy_from_user(flags, arg, sizeof(flags)))
5402 return -EFAULT;
5403
5404 /* Nothing to do */
5405 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5406 !flags[0].incompat_flags)
5407 return 0;
5408
5409 ret = check_feature(root, flags[0].compat_flags,
5410 flags[1].compat_flags, COMPAT);
5411 if (ret)
5412 return ret;
5413
5414 ret = check_feature(root, flags[0].compat_ro_flags,
5415 flags[1].compat_ro_flags, COMPAT_RO);
5416 if (ret)
5417 return ret;
5418
5419 ret = check_feature(root, flags[0].incompat_flags,
5420 flags[1].incompat_flags, INCOMPAT);
5421 if (ret)
5422 return ret;
5423
David Sterba8051aa12014-02-07 14:34:04 +01005424 trans = btrfs_start_transaction(root, 0);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005425 if (IS_ERR(trans))
5426 return PTR_ERR(trans);
5427
5428 spin_lock(&root->fs_info->super_lock);
5429 newflags = btrfs_super_compat_flags(super_block);
5430 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5431 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5432 btrfs_set_super_compat_flags(super_block, newflags);
5433
5434 newflags = btrfs_super_compat_ro_flags(super_block);
5435 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5436 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5437 btrfs_set_super_compat_ro_flags(super_block, newflags);
5438
5439 newflags = btrfs_super_incompat_flags(super_block);
5440 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5441 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5442 btrfs_set_super_incompat_flags(super_block, newflags);
5443 spin_unlock(&root->fs_info->super_lock);
5444
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005445 return btrfs_commit_transaction(trans, root);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005446}
5447
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005448long btrfs_ioctl(struct file *file, unsigned int
5449 cmd, unsigned long arg)
5450{
Al Viro496ad9a2013-01-23 17:07:38 -05005451 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005452 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005453
5454 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005455 case FS_IOC_GETFLAGS:
5456 return btrfs_ioctl_getflags(file, argp);
5457 case FS_IOC_SETFLAGS:
5458 return btrfs_ioctl_setflags(file, argp);
5459 case FS_IOC_GETVERSION:
5460 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005461 case FITRIM:
5462 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005463 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005464 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005465 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005466 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005467 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005468 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005469 case BTRFS_IOC_SUBVOL_CREATE_V2:
5470 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005471 case BTRFS_IOC_SNAP_DESTROY:
5472 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005473 case BTRFS_IOC_SUBVOL_GETFLAGS:
5474 return btrfs_ioctl_subvol_getflags(file, argp);
5475 case BTRFS_IOC_SUBVOL_SETFLAGS:
5476 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005477 case BTRFS_IOC_DEFAULT_SUBVOL:
5478 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005479 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005480 return btrfs_ioctl_defrag(file, NULL);
5481 case BTRFS_IOC_DEFRAG_RANGE:
5482 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005483 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005484 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005485 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005486 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005487 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005488 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005489 case BTRFS_IOC_FS_INFO:
5490 return btrfs_ioctl_fs_info(root, argp);
5491 case BTRFS_IOC_DEV_INFO:
5492 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005493 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005494 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005495 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05005496 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
5497 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05005498 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005499 case BTRFS_IOC_TRANS_START:
5500 return btrfs_ioctl_trans_start(file);
5501 case BTRFS_IOC_TRANS_END:
5502 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005503 case BTRFS_IOC_TREE_SEARCH:
5504 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005505 case BTRFS_IOC_TREE_SEARCH_V2:
5506 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005507 case BTRFS_IOC_INO_LOOKUP:
5508 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005509 case BTRFS_IOC_INO_PATHS:
5510 return btrfs_ioctl_ino_to_path(root, argp);
5511 case BTRFS_IOC_LOGICAL_INO:
5512 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005513 case BTRFS_IOC_SPACE_INFO:
5514 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005515 case BTRFS_IOC_SYNC: {
5516 int ret;
5517
Miao Xie6c255e62014-03-06 13:55:01 +08005518 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005519 if (ret)
5520 return ret;
Al Viroddb52f42014-10-21 20:21:18 -04005521 ret = btrfs_sync_fs(file_inode(file)->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005522 /*
5523 * The transaction thread may want to do more work,
5524 * namely it pokes the cleaner ktread that will start
5525 * processing uncleaned subvols.
5526 */
5527 wake_up_process(root->fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005528 return ret;
5529 }
Sage Weil46204592010-10-29 15:41:32 -04005530 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005531 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005532 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005533 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005534 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005535 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005536 case BTRFS_IOC_SCRUB_CANCEL:
5537 return btrfs_ioctl_scrub_cancel(root, argp);
5538 case BTRFS_IOC_SCRUB_PROGRESS:
5539 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005540 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005541 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005542 case BTRFS_IOC_BALANCE_CTL:
5543 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005544 case BTRFS_IOC_BALANCE_PROGRESS:
5545 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005546 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5547 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005548#ifdef CONFIG_64BIT
5549 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5550 return btrfs_ioctl_set_received_subvol_32(file, argp);
5551#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005552 case BTRFS_IOC_SEND:
5553 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005554 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06005555 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005556 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005557 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005558 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005559 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005560 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005561 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005562 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005563 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005564 case BTRFS_IOC_QUOTA_RESCAN:
5565 return btrfs_ioctl_quota_rescan(file, argp);
5566 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5567 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005568 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5569 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005570 case BTRFS_IOC_DEV_REPLACE:
5571 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005572 case BTRFS_IOC_GET_FSLABEL:
5573 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005574 case BTRFS_IOC_SET_FSLABEL:
5575 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07005576 case BTRFS_IOC_FILE_EXTENT_SAME:
5577 return btrfs_ioctl_file_extent_same(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005578 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5579 return btrfs_ioctl_get_supported_features(file, argp);
5580 case BTRFS_IOC_GET_FEATURES:
5581 return btrfs_ioctl_get_features(file, argp);
5582 case BTRFS_IOC_SET_FEATURES:
5583 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005584 }
5585
5586 return -ENOTTY;
5587}