blob: 0cdcdf216a1bb2935e9faf3ec831f27fdfe42104 [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
Al Viro59551022016-01-22 15:40:57 -0500243 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200244
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);
Deepa Dinamani04b285f2016-02-06 23:57:21 -0800350 inode->i_ctime = current_fs_time(inode->i_sb);
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:
Al Viro59551022016-01-22 15:40:57 -0500361 inode_unlock(inode);
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;
Deepa Dinamani04b285f2016-02-06 23:57:21 -0800446 struct timespec cur_time = current_fs_time(dir->i_sb);
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
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530571 mutex_lock(&new_root->objectid_mutex);
572 new_root->highest_objectid = new_dirid;
573 mutex_unlock(&new_root->objectid_mutex);
574
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400575 /*
576 * insert the directory item
577 */
Chris Mason3de45862008-11-17 21:02:50 -0500578 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100579 if (ret) {
580 btrfs_abort_transaction(trans, root, ret);
581 goto fail;
582 }
Chris Mason3de45862008-11-17 21:02:50 -0500583
584 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800585 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500586 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100587 if (ret) {
588 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400589 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500591
Yan Zheng52c26172009-01-05 15:43:43 -0500592 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
593 ret = btrfs_update_inode(trans, root, dir);
594 BUG_ON(ret);
595
Chris Mason0660b5a2008-11-17 20:37:39 -0500596 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400597 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800598 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500599 BUG_ON(ret);
600
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200601 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
602 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
603 objectid);
604 if (ret)
605 btrfs_abort_transaction(trans, root, ret);
606
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400607fail:
Miao Xied5c12072013-02-28 10:04:33 +0000608 trans->block_rsv = NULL;
609 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800610 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
611
Sage Weil72fd0322010-10-29 15:41:32 -0400612 if (async_transid) {
613 *async_transid = trans->transid;
614 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000615 if (err)
616 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400617 } else {
618 err = btrfs_commit_transaction(trans, root);
619 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400620 if (err && !ret)
621 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500622
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900623 if (!ret) {
624 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800625 if (IS_ERR(inode))
626 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900627 d_instantiate(dentry, inode);
628 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400629 return ret;
630}
631
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000632static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +0800633{
634 s64 writers;
635 DEFINE_WAIT(wait);
636
637 do {
638 prepare_to_wait(&root->subv_writers->wait, &wait,
639 TASK_UNINTERRUPTIBLE);
640
641 writers = percpu_counter_sum(&root->subv_writers->counter);
642 if (writers)
643 schedule();
644
645 finish_wait(&root->subv_writers->wait, &wait);
646 } while (writers);
647}
648
Miao Xiee9662f72013-02-28 10:01:15 +0000649static int create_snapshot(struct btrfs_root *root, struct inode *dir,
650 struct dentry *dentry, char *name, int namelen,
651 u64 *async_transid, bool readonly,
652 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400653{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000654 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400655 struct btrfs_pending_snapshot *pending_snapshot;
656 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000657 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400658
Miao Xie27cdeb72014-04-02 19:51:05 +0800659 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400660 return -EINVAL;
661
David Sterbaa1ee7362015-11-10 18:53:56 +0100662 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
663 if (!pending_snapshot)
664 return -ENOMEM;
665
David Sterbab0c0ea62015-11-10 18:54:00 +0100666 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
667 GFP_NOFS);
David Sterba8546b572015-11-10 18:54:03 +0100668 pending_snapshot->path = btrfs_alloc_path();
669 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100670 ret = -ENOMEM;
671 goto free_pending;
672 }
673
Miao Xie8257b2d2014-03-06 13:38:19 +0800674 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100675 smp_mb__after_atomic();
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000676 btrfs_wait_for_no_snapshoting_writes(root);
Miao Xie8257b2d2014-03-06 13:38:19 +0800677
Miao Xie6a038432013-05-15 07:48:24 +0000678 ret = btrfs_start_delalloc_inodes(root, 0);
679 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100680 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000681
Miao Xieb0244192013-11-04 23:13:25 +0800682 btrfs_wait_ordered_extents(root, -1);
Miao Xie6a038432013-05-15 07:48:24 +0000683
Miao Xie66d8f3d2012-09-06 04:02:28 -0600684 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
685 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000686 /*
687 * 1 - parent dir inode
688 * 2 - dir entries
689 * 1 - root item
690 * 2 - root ref/backref
691 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200692 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000693 */
694 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200695 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400696 &pending_snapshot->qgroup_reserved,
697 false);
Miao Xied5c12072013-02-28 10:04:33 +0000698 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100699 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000700
Yan, Zhenga22285a2010-05-16 10:48:46 -0400701 pending_snapshot->dentry = dentry;
702 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800703 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000704 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000705 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400706
Miao Xied5c12072013-02-28 10:04:33 +0000707 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400708 if (IS_ERR(trans)) {
709 ret = PTR_ERR(trans);
710 goto fail;
711 }
712
Josef Bacik83515832011-06-14 15:16:14 -0400713 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400714 list_add(&pending_snapshot->list,
715 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400716 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400717 if (async_transid) {
718 *async_transid = trans->transid;
719 ret = btrfs_commit_transaction_async(trans,
720 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000721 if (ret)
722 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400723 } else {
724 ret = btrfs_commit_transaction(trans,
725 root->fs_info->extent_root);
726 }
Miao Xieaec80302013-03-04 09:44:29 +0000727 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400728 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400729
730 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400731 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000732 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400733
Chris Masond3797302014-10-15 13:50:56 -0700734 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
735 if (ret)
736 goto fail;
737
David Howells2b0143b2015-03-17 22:25:59 +0000738 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000739 if (IS_ERR(inode)) {
740 ret = PTR_ERR(inode);
741 goto fail;
742 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900743
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000744 d_instantiate(dentry, inode);
745 ret = 0;
746fail:
Miao Xied5c12072013-02-28 10:04:33 +0000747 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
748 &pending_snapshot->block_rsv,
749 pending_snapshot->qgroup_reserved);
David Sterbaa1ee7362015-11-10 18:53:56 +0100750dec_and_free:
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000751 if (atomic_dec_and_test(&root->will_be_snapshoted))
752 wake_up_atomic_t(&root->will_be_snapshoted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100753free_pending:
754 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100755 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100756 kfree(pending_snapshot);
757
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400758 return ret;
759}
760
Sage Weil4260f7c2010-10-29 15:46:43 -0400761/* copy of may_delete in fs/namei.c()
762 * Check whether we can remove a link victim from directory dir, check
763 * whether the type of victim is right.
764 * 1. We can't do it if dir is read-only (done in permission())
765 * 2. We should have write and exec permissions on dir
766 * 3. We can't remove anything from append-only dir
767 * 4. We can't do anything with immutable dir (done in permission())
768 * 5. If the sticky bit on dir is set we should either
769 * a. be owner of dir, or
770 * b. be owner of victim, or
771 * c. have CAP_FOWNER capability
772 * 6. If the victim is append-only or immutable we can't do antyhing with
773 * links pointing to it.
774 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
775 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
776 * 9. We can't remove a root or mountpoint.
777 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
778 * nfs_async_unlink().
779 */
780
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530781static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400782{
783 int error;
784
David Howells2b0143b2015-03-17 22:25:59 +0000785 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400786 return -ENOENT;
787
David Howells2b0143b2015-03-17 22:25:59 +0000788 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400789 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400790
791 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
792 if (error)
793 return error;
794 if (IS_APPEND(dir))
795 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000796 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
797 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400798 return -EPERM;
799 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000800 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400801 return -ENOTDIR;
802 if (IS_ROOT(victim))
803 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000804 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400805 return -EISDIR;
806 if (IS_DEADDIR(dir))
807 return -ENOENT;
808 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
809 return -EBUSY;
810 return 0;
811}
812
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400813/* copy of may_create in fs/namei.c() */
814static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
815{
David Howells2b0143b2015-03-17 22:25:59 +0000816 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400817 return -EEXIST;
818 if (IS_DEADDIR(dir))
819 return -ENOENT;
820 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
821}
822
823/*
824 * Create a new subvolume below @parent. This is largely modeled after
825 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
826 * inside this filesystem so it's quite a bit simpler.
827 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400828static noinline int btrfs_mksubvol(struct path *parent,
829 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400830 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200831 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000832 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400833{
David Howells2b0143b2015-03-17 22:25:59 +0000834 struct inode *dir = d_inode(parent->dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400835 struct dentry *dentry;
836 int error;
837
David Sterba5c50c9b2013-03-22 18:12:51 +0000838 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
839 if (error == -EINTR)
840 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400841
842 dentry = lookup_one_len(name, parent->dentry, namelen);
843 error = PTR_ERR(dentry);
844 if (IS_ERR(dentry))
845 goto out_unlock;
846
Yan, Zheng76dda932009-09-21 16:00:26 -0400847 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400848 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600849 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400850
Chris Mason9c520572012-12-17 14:26:57 -0500851 /*
852 * even if this name doesn't exist, we may get hash collisions.
853 * check for them now when we can safely fail
854 */
855 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
856 dir->i_ino, name,
857 namelen);
858 if (error)
859 goto out_dput;
860
Yan, Zheng76dda932009-09-21 16:00:26 -0400861 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
862
863 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
864 goto out_up_read;
865
Chris Mason3de45862008-11-17 21:02:50 -0500866 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000867 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200868 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500869 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000870 error = create_subvol(dir, dentry, name, namelen,
871 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500872 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400873 if (!error)
874 fsnotify_mkdir(dir, dentry);
875out_up_read:
876 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400877out_dput:
878 dput(dentry);
879out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500880 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400881 return error;
882}
883
Chris Mason4cb53002011-05-24 15:35:30 -0400884/*
885 * When we're defragging a range, we don't want to kick it off again
886 * if it is really just waiting for delalloc to send it down.
887 * If we find a nice big extent or delalloc range for the bytes in the
888 * file you want to defrag, we return 0 to let you know to skip this
889 * part of the file
890 */
David Sterbaaab110a2014-07-29 17:32:10 +0200891static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400892{
893 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
894 struct extent_map *em = NULL;
895 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
896 u64 end;
897
898 read_lock(&em_tree->lock);
899 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
900 read_unlock(&em_tree->lock);
901
902 if (em) {
903 end = extent_map_end(em);
904 free_extent_map(em);
905 if (end - offset > thresh)
906 return 0;
907 }
908 /* if we already have a nice delalloc here, just stop */
909 thresh /= 2;
910 end = count_range_bits(io_tree, &offset, offset + thresh,
911 thresh, EXTENT_DELALLOC, 1);
912 if (end >= thresh)
913 return 0;
914 return 1;
915}
916
917/*
918 * helper function to walk through a file and find extents
919 * newer than a specific transid, and smaller than thresh.
920 *
921 * This is used by the defragging code to find new and small
922 * extents
923 */
924static int find_new_extents(struct btrfs_root *root,
925 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200926 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400927{
928 struct btrfs_path *path;
929 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400930 struct extent_buffer *leaf;
931 struct btrfs_file_extent_item *extent;
932 int type;
933 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000934 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400935
936 path = btrfs_alloc_path();
937 if (!path)
938 return -ENOMEM;
939
David Sterbaa4689d22011-05-31 17:08:14 +0000940 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400941 min_key.type = BTRFS_EXTENT_DATA_KEY;
942 min_key.offset = *off;
943
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530944 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100945 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400946 if (ret != 0)
947 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000948process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000949 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400950 goto none;
951 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
952 goto none;
953
954 leaf = path->nodes[0];
955 extent = btrfs_item_ptr(leaf, path->slots[0],
956 struct btrfs_file_extent_item);
957
958 type = btrfs_file_extent_type(leaf, extent);
959 if (type == BTRFS_FILE_EXTENT_REG &&
960 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
961 check_defrag_in_cache(inode, min_key.offset, thresh)) {
962 *off = min_key.offset;
963 btrfs_free_path(path);
964 return 0;
965 }
966
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000967 path->slots[0]++;
968 if (path->slots[0] < btrfs_header_nritems(leaf)) {
969 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
970 goto process_slot;
971 }
972
Chris Mason4cb53002011-05-24 15:35:30 -0400973 if (min_key.offset == (u64)-1)
974 goto none;
975
976 min_key.offset++;
977 btrfs_release_path(path);
978 }
979none:
980 btrfs_free_path(path);
981 return -ENOENT;
982}
983
Li Zefan6c282eb2012-06-11 16:03:35 +0800984static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400985{
986 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500987 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800988 struct extent_map *em;
989 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500990
991 /*
992 * hopefully we have this extent in the tree already, try without
993 * the full extent lock
994 */
995 read_lock(&em_tree->lock);
996 em = lookup_extent_mapping(em_tree, start, len);
997 read_unlock(&em_tree->lock);
998
999 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001000 struct extent_state *cached = NULL;
1001 u64 end = start + len - 1;
1002
Chris Mason940100a2010-03-10 10:52:59 -05001003 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001004 lock_extent_bits(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001005 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +00001006 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -05001007
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001008 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001009 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001010 }
1011
Li Zefan6c282eb2012-06-11 16:03:35 +08001012 return em;
1013}
1014
1015static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1016{
1017 struct extent_map *next;
1018 bool ret = true;
1019
1020 /* this is the last extent */
1021 if (em->start + em->len >= i_size_read(inode))
1022 return false;
1023
1024 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001025 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1026 ret = false;
1027 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001028 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001029 ret = false;
1030
1031 free_extent_map(next);
1032 return ret;
1033}
1034
David Sterbaaab110a2014-07-29 17:32:10 +02001035static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001036 u64 *last_len, u64 *skip, u64 *defrag_end,
1037 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001038{
1039 struct extent_map *em;
1040 int ret = 1;
1041 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001042 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001043
1044 /*
1045 * make sure that once we start defragging an extent, we keep on
1046 * defragging it
1047 */
1048 if (start < *defrag_end)
1049 return 1;
1050
1051 *skip = 0;
1052
1053 em = defrag_lookup_extent(inode, start);
1054 if (!em)
1055 return 0;
1056
Chris Mason940100a2010-03-10 10:52:59 -05001057 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001058 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001059 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001060 goto out;
1061 }
1062
Liu Bo4a3560c2015-08-07 16:48:41 +08001063 if (!*defrag_end)
1064 prev_mergeable = false;
1065
Li Zefan6c282eb2012-06-11 16:03:35 +08001066 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001067 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001068 * we hit a real extent, if it is big or the next extent is not a
1069 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001070 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001071 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001072 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001073 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001074out:
Chris Mason940100a2010-03-10 10:52:59 -05001075 /*
1076 * last_len ends up being a counter of how many bytes we've defragged.
1077 * every time we choose not to defrag an extent, we reset *last_len
1078 * so that the next tiny extent will force a defrag.
1079 *
1080 * The end result of this is that tiny extents before a single big
1081 * extent will force at least part of that big extent to be defragged.
1082 */
1083 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001084 *defrag_end = extent_map_end(em);
1085 } else {
1086 *last_len = 0;
1087 *skip = extent_map_end(em);
1088 *defrag_end = 0;
1089 }
1090
1091 free_extent_map(em);
1092 return ret;
1093}
1094
Chris Mason4cb53002011-05-24 15:35:30 -04001095/*
1096 * it doesn't do much good to defrag one or two pages
1097 * at a time. This pulls in a nice chunk of pages
1098 * to COW and defrag.
1099 *
1100 * It also makes sure the delalloc code has enough
1101 * dirty data to avoid making new small extents as part
1102 * of the defrag
1103 *
1104 * It's a good idea to start RA on this range
1105 * before calling this.
1106 */
1107static int cluster_pages_for_defrag(struct inode *inode,
1108 struct page **pages,
1109 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001110 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001111{
Chris Mason4cb53002011-05-24 15:35:30 -04001112 unsigned long file_end;
1113 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001114 u64 page_start;
1115 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001116 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001117 int ret;
1118 int i;
1119 int i_done;
1120 struct btrfs_ordered_extent *ordered;
1121 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001122 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001123 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001124
Chris Mason4cb53002011-05-24 15:35:30 -04001125 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001126 if (!isize || start_index > file_end)
1127 return 0;
1128
1129 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001130
1131 ret = btrfs_delalloc_reserve_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001132 start_index << PAGE_CACHE_SHIFT,
1133 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001134 if (ret)
1135 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001136 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001137 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001138
1139 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001140 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001141 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001142again:
Josef Bacika94733d2011-07-11 10:47:06 -04001143 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001144 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001145 if (!page)
1146 break;
1147
Miao Xie600a45e2012-02-16 15:01:24 +08001148 page_start = page_offset(page);
1149 page_end = page_start + PAGE_CACHE_SIZE - 1;
1150 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001151 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001152 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001153 ordered = btrfs_lookup_ordered_extent(inode,
1154 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001155 unlock_extent_cached(tree, page_start, page_end,
1156 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001157 if (!ordered)
1158 break;
1159
1160 unlock_page(page);
1161 btrfs_start_ordered_extent(inode, ordered, 1);
1162 btrfs_put_ordered_extent(ordered);
1163 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001164 /*
1165 * we unlocked the page above, so we need check if
1166 * it was released or not.
1167 */
1168 if (page->mapping != inode->i_mapping) {
1169 unlock_page(page);
1170 page_cache_release(page);
1171 goto again;
1172 }
Miao Xie600a45e2012-02-16 15:01:24 +08001173 }
1174
Chris Mason4cb53002011-05-24 15:35:30 -04001175 if (!PageUptodate(page)) {
1176 btrfs_readpage(NULL, page);
1177 lock_page(page);
1178 if (!PageUptodate(page)) {
1179 unlock_page(page);
1180 page_cache_release(page);
1181 ret = -EIO;
1182 break;
1183 }
1184 }
Miao Xie600a45e2012-02-16 15:01:24 +08001185
Miao Xie600a45e2012-02-16 15:01:24 +08001186 if (page->mapping != inode->i_mapping) {
1187 unlock_page(page);
1188 page_cache_release(page);
1189 goto again;
1190 }
1191
Chris Mason4cb53002011-05-24 15:35:30 -04001192 pages[i] = page;
1193 i_done++;
1194 }
1195 if (!i_done || ret)
1196 goto out;
1197
1198 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1199 goto out;
1200
1201 /*
1202 * so now we have a nice long stream of locked
1203 * and up to date pages, lets wait on them
1204 */
1205 for (i = 0; i < i_done; i++)
1206 wait_on_page_writeback(pages[i]);
1207
1208 page_start = page_offset(pages[0]);
1209 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1210
1211 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001212 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001213 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1214 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001215 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1216 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001217
Liu Bo1f12bd02012-03-29 09:57:44 -04001218 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001219 spin_lock(&BTRFS_I(inode)->lock);
1220 BTRFS_I(inode)->outstanding_extents++;
1221 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001222 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001223 start_index << PAGE_CACHE_SHIFT,
1224 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001225 }
1226
1227
Liu Bo9e8a4a82012-09-05 19:10:51 -06001228 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1229 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001230
1231 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1232 page_start, page_end - 1, &cached_state,
1233 GFP_NOFS);
1234
1235 for (i = 0; i < i_done; i++) {
1236 clear_page_dirty_for_io(pages[i]);
1237 ClearPageChecked(pages[i]);
1238 set_page_extent_mapped(pages[i]);
1239 set_page_dirty(pages[i]);
1240 unlock_page(pages[i]);
1241 page_cache_release(pages[i]);
1242 }
1243 return i_done;
1244out:
1245 for (i = 0; i < i_done; i++) {
1246 unlock_page(pages[i]);
1247 page_cache_release(pages[i]);
1248 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001249 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001250 start_index << PAGE_CACHE_SHIFT,
1251 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001252 return ret;
1253
1254}
1255
1256int btrfs_defrag_file(struct inode *inode, struct file *file,
1257 struct btrfs_ioctl_defrag_range_args *range,
1258 u64 newer_than, unsigned long max_to_defrag)
1259{
1260 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001261 struct file_ra_state *ra = NULL;
1262 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001263 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001264 u64 last_len = 0;
1265 u64 skip = 0;
1266 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001267 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001268 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001269 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001270 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001271 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001272 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001273 u32 extent_thresh = range->extent_thresh;
Byongho Leeee221842015-12-15 01:42:10 +09001274 unsigned long max_cluster = SZ_256K >> PAGE_CACHE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001275 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001276 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001277 struct page **pages = NULL;
1278
Liu Bo0abd5b12013-04-16 09:20:28 +00001279 if (isize == 0)
1280 return 0;
1281
1282 if (range->start >= isize)
1283 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001284
1285 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1286 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1287 return -EINVAL;
1288 if (range->compress_type)
1289 compress_type = range->compress_type;
1290 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001291
Liu Bo0abd5b12013-04-16 09:20:28 +00001292 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001293 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001294
Chris Mason4cb53002011-05-24 15:35:30 -04001295 /*
1296 * if we were not given a file, allocate a readahead
1297 * context
1298 */
1299 if (!file) {
1300 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1301 if (!ra)
1302 return -ENOMEM;
1303 file_ra_state_init(ra, inode->i_mapping);
1304 } else {
1305 ra = &file->f_ra;
1306 }
1307
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301308 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001309 GFP_NOFS);
1310 if (!pages) {
1311 ret = -ENOMEM;
1312 goto out_ra;
1313 }
1314
1315 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001316 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001317 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001318 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1319 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001320 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001321 }
1322
Chris Mason4cb53002011-05-24 15:35:30 -04001323 if (newer_than) {
1324 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001325 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001326 if (!ret) {
1327 range->start = newer_off;
1328 /*
1329 * we always align our defrag to help keep
1330 * the extents in the file evenly spaced
1331 */
1332 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001333 } else
1334 goto out_ra;
1335 } else {
1336 i = range->start >> PAGE_CACHE_SHIFT;
1337 }
1338 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301339 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001340
Li Zefan2a0f7f52011-10-10 15:43:34 -04001341 /*
1342 * make writeback starts from i, so the defrag range can be
1343 * written sequentially.
1344 */
1345 if (i < inode->i_mapping->writeback_index)
1346 inode->i_mapping->writeback_index = i;
1347
Chris Masonf7f43cc2011-10-11 11:41:40 -04001348 while (i <= last_index && defrag_count < max_to_defrag &&
David Sterbaed6078f2014-06-05 01:59:57 +02001349 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001350 /*
1351 * make sure we stop running if someone unmounts
1352 * the FS
1353 */
1354 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1355 break;
1356
David Sterba210549e2013-02-09 23:38:06 +00001357 if (btrfs_defrag_cancelled(root->fs_info)) {
David Sterbaf14d1042015-10-08 11:37:06 +02001358 btrfs_debug(root->fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001359 ret = -EAGAIN;
1360 break;
1361 }
1362
Liu Bo66c26892012-03-29 09:57:45 -04001363 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001364 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001365 &defrag_end, range->flags &
1366 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001367 unsigned long next;
1368 /*
1369 * the should_defrag function tells us how much to skip
1370 * bump our counter by the suggested amount
1371 */
David Sterbaed6078f2014-06-05 01:59:57 +02001372 next = DIV_ROUND_UP(skip, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001373 i = max(i + 1, next);
1374 continue;
1375 }
Li Zefan008873e2011-09-02 15:57:07 +08001376
1377 if (!newer_than) {
1378 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1379 PAGE_CACHE_SHIFT) - i;
1380 cluster = min(cluster, max_cluster);
1381 } else {
1382 cluster = max_cluster;
1383 }
1384
Li Zefan008873e2011-09-02 15:57:07 +08001385 if (i + cluster > ra_index) {
1386 ra_index = max(i, ra_index);
1387 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1388 cluster);
chandane4826a52015-06-09 17:38:32 +05301389 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001390 }
Chris Mason940100a2010-03-10 10:52:59 -05001391
Al Viro59551022016-01-22 15:40:57 -05001392 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001393 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1394 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001395 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001396 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001397 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001398 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001399 }
Chris Mason940100a2010-03-10 10:52:59 -05001400
Chris Mason4cb53002011-05-24 15:35:30 -04001401 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001402 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001403 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001404
1405 if (newer_than) {
1406 if (newer_off == (u64)-1)
1407 break;
1408
Liu Boe1f041e2012-03-29 09:57:45 -04001409 if (ret > 0)
1410 i += ret;
1411
Chris Mason4cb53002011-05-24 15:35:30 -04001412 newer_off = max(newer_off + 1,
1413 (u64)i << PAGE_CACHE_SHIFT);
1414
Byongho Leeee221842015-12-15 01:42:10 +09001415 ret = find_new_extents(root, inode, newer_than,
1416 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001417 if (!ret) {
1418 range->start = newer_off;
1419 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001420 } else {
1421 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001422 }
Chris Mason4cb53002011-05-24 15:35:30 -04001423 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001424 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001425 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001426 last_len += ret << PAGE_CACHE_SHIFT;
1427 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001428 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001429 last_len = 0;
1430 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001432 }
1433
Filipe Mananadec8ef92014-03-01 10:55:54 +00001434 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001435 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001436 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1437 &BTRFS_I(inode)->runtime_flags))
1438 filemap_flush(inode->i_mapping);
1439 }
Chris Mason1e701a32010-03-11 09:42:04 -05001440
1441 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1442 /* the filemap_flush will queue IO into the worker threads, but
1443 * we have to make sure the IO is actually started and that
1444 * ordered extents get created before we return
1445 */
1446 atomic_inc(&root->fs_info->async_submit_draining);
1447 while (atomic_read(&root->fs_info->nr_async_submits) ||
1448 atomic_read(&root->fs_info->async_delalloc_pages)) {
1449 wait_event(root->fs_info->async_submit_wait,
1450 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1451 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1452 }
1453 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001454 }
1455
Li Zefan1a419d82010-10-25 15:12:50 +08001456 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001457 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001458 }
1459
Diego Calleja60ccf822011-09-01 16:33:57 +02001460 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001461
Chris Mason4cb53002011-05-24 15:35:30 -04001462out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001463 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
Al Viro59551022016-01-22 15:40:57 -05001464 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001465 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001466 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001467 }
Chris Mason4cb53002011-05-24 15:35:30 -04001468 if (!file)
1469 kfree(ra);
1470 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001471 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001472}
1473
Miao Xie198605a2012-11-26 08:43:45 +00001474static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001475 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001476{
1477 u64 new_size;
1478 u64 old_size;
1479 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001480 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001481 struct btrfs_ioctl_vol_args *vol_args;
1482 struct btrfs_trans_handle *trans;
1483 struct btrfs_device *device = NULL;
1484 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001485 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001486 char *devstr = NULL;
1487 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001488 int mod = 0;
1489
Chris Masone441d542009-01-05 16:57:23 -05001490 if (!capable(CAP_SYS_ADMIN))
1491 return -EPERM;
1492
Miao Xie198605a2012-11-26 08:43:45 +00001493 ret = mnt_want_write_file(file);
1494 if (ret)
1495 return ret;
1496
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001497 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1498 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001499 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001500 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001501 }
1502
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001503 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001504 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001505 if (IS_ERR(vol_args)) {
1506 ret = PTR_ERR(vol_args);
1507 goto out;
1508 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001509
1510 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001511
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001512 sizestr = vol_args->name;
1513 devstr = strchr(sizestr, ':');
1514 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001515 sizestr = devstr + 1;
1516 *devstr = '\0';
1517 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001518 ret = kstrtoull(devstr, 10, &devid);
1519 if (ret)
1520 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001521 if (!devid) {
1522 ret = -EINVAL;
1523 goto out_free;
1524 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001525 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001526 }
Miao Xiedba60f32012-12-21 09:19:51 +00001527
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001528 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001529 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001530 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001531 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001532 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001533 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001534 }
Miao Xiedba60f32012-12-21 09:19:51 +00001535
1536 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001537 btrfs_info(root->fs_info,
1538 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001539 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001540 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001541 goto out_free;
1542 }
1543
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001544 if (!strcmp(sizestr, "max"))
1545 new_size = device->bdev->bd_inode->i_size;
1546 else {
1547 if (sizestr[0] == '-') {
1548 mod = -1;
1549 sizestr++;
1550 } else if (sizestr[0] == '+') {
1551 mod = 1;
1552 sizestr++;
1553 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001554 new_size = memparse(sizestr, &retptr);
1555 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001556 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001557 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001558 }
1559 }
1560
Stefan Behrens63a212a2012-11-05 18:29:28 +01001561 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001562 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001563 goto out_free;
1564 }
1565
Miao Xie7cc8e582014-09-03 21:35:38 +08001566 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001567
1568 if (mod < 0) {
1569 if (new_size > old_size) {
1570 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001571 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572 }
1573 new_size = old_size - new_size;
1574 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001575 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001576 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001577 goto out_free;
1578 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001579 new_size = old_size + new_size;
1580 }
1581
Byongho Leeee221842015-12-15 01:42:10 +09001582 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001583 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001584 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001585 }
1586 if (new_size > device->bdev->bd_inode->i_size) {
1587 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001588 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001589 }
1590
David Sterbab8b93ad2015-01-16 17:26:13 +01001591 new_size = div_u64(new_size, root->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001592 new_size *= root->sectorsize;
1593
David Sterbaecaeb142015-10-08 09:01:03 +02001594 btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001595 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001596
1597 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001598 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001599 if (IS_ERR(trans)) {
1600 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001601 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001602 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001603 ret = btrfs_grow_device(trans, device, new_size);
1604 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001605 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001606 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001607 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001608
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001609out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001610 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001611out:
1612 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001613 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001614 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001615 return ret;
1616}
1617
Sage Weil72fd0322010-10-29 15:41:32 -04001618static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001619 char *name, unsigned long fd, int subvol,
1620 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001621 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001622{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001623 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001624 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001625
Liu Boa874a632012-06-29 03:58:46 -06001626 ret = mnt_want_write_file(file);
1627 if (ret)
1628 goto out;
1629
Sage Weil72fd0322010-10-29 15:41:32 -04001630 namelen = strlen(name);
1631 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001632 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001633 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001634 }
1635
Chris Mason16780ca2012-02-20 22:14:55 -05001636 if (name[0] == '.' &&
1637 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1638 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001639 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001640 }
1641
Chris Mason3de45862008-11-17 21:02:50 -05001642 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001643 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001644 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001645 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001646 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001647 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001648 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001649 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001650 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001651 }
1652
Al Viro496ad9a2013-01-23 17:07:38 -05001653 src_inode = file_inode(src.file);
1654 if (src_inode->i_sb != file_inode(file)->i_sb) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001655 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1656 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001657 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001658 } else if (!inode_owner_or_capable(src_inode)) {
1659 /*
1660 * Subvolume creation is not restricted, but snapshots
1661 * are limited to own subvolumes only
1662 */
1663 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001664 } else {
1665 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1666 BTRFS_I(src_inode)->root,
1667 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001668 }
Al Viro2903ff02012-08-28 12:52:22 -04001669 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001670 }
Liu Boa874a632012-06-29 03:58:46 -06001671out_drop_write:
1672 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001673out:
Sage Weil72fd0322010-10-29 15:41:32 -04001674 return ret;
1675}
1676
1677static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001678 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001679{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001680 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001681 int ret;
1682
Li Zefanfa0d2b92010-12-20 15:53:28 +08001683 vol_args = memdup_user(arg, sizeof(*vol_args));
1684 if (IS_ERR(vol_args))
1685 return PTR_ERR(vol_args);
1686 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001687
Li Zefanfa0d2b92010-12-20 15:53:28 +08001688 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001689 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001690 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001691
Li Zefanfa0d2b92010-12-20 15:53:28 +08001692 kfree(vol_args);
1693 return ret;
1694}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001695
Li Zefanfa0d2b92010-12-20 15:53:28 +08001696static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1697 void __user *arg, int subvol)
1698{
1699 struct btrfs_ioctl_vol_args_v2 *vol_args;
1700 int ret;
1701 u64 transid = 0;
1702 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001703 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001704 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001705
Li Zefanfa0d2b92010-12-20 15:53:28 +08001706 vol_args = memdup_user(arg, sizeof(*vol_args));
1707 if (IS_ERR(vol_args))
1708 return PTR_ERR(vol_args);
1709 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001710
Li Zefanb83cc962010-12-20 16:04:08 +08001711 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001712 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1713 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001714 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001715 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001716 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001717
1718 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1719 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001720 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1721 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001722 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1723 if (vol_args->size > PAGE_CACHE_SIZE) {
1724 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001725 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001726 }
1727 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1728 if (IS_ERR(inherit)) {
1729 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001730 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001731 }
1732 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001733
1734 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001735 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001736 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001737 if (ret)
1738 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001739
Dan Carpenterc47ca322014-09-04 14:09:15 +03001740 if (ptr && copy_to_user(arg +
1741 offsetof(struct btrfs_ioctl_vol_args_v2,
1742 transid),
1743 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001744 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001745
1746free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001747 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001748free_args:
1749 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750 return ret;
1751}
1752
Li Zefan0caa1022010-12-20 16:30:25 +08001753static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1754 void __user *arg)
1755{
Al Viro496ad9a2013-01-23 17:07:38 -05001756 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001757 struct btrfs_root *root = BTRFS_I(inode)->root;
1758 int ret = 0;
1759 u64 flags = 0;
1760
Li Zefan33345d012011-04-20 10:31:50 +08001761 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001762 return -EINVAL;
1763
1764 down_read(&root->fs_info->subvol_sem);
1765 if (btrfs_root_readonly(root))
1766 flags |= BTRFS_SUBVOL_RDONLY;
1767 up_read(&root->fs_info->subvol_sem);
1768
1769 if (copy_to_user(arg, &flags, sizeof(flags)))
1770 ret = -EFAULT;
1771
1772 return ret;
1773}
1774
1775static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1776 void __user *arg)
1777{
Al Viro496ad9a2013-01-23 17:07:38 -05001778 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001779 struct btrfs_root *root = BTRFS_I(inode)->root;
1780 struct btrfs_trans_handle *trans;
1781 u64 root_flags;
1782 u64 flags;
1783 int ret = 0;
1784
David Sterbabd60ea02014-01-16 15:50:22 +01001785 if (!inode_owner_or_capable(inode))
1786 return -EPERM;
1787
Liu Bob9ca0662012-06-29 03:58:49 -06001788 ret = mnt_want_write_file(file);
1789 if (ret)
1790 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001791
Liu Bob9ca0662012-06-29 03:58:49 -06001792 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1793 ret = -EINVAL;
1794 goto out_drop_write;
1795 }
Li Zefan0caa1022010-12-20 16:30:25 +08001796
Liu Bob9ca0662012-06-29 03:58:49 -06001797 if (copy_from_user(&flags, arg, sizeof(flags))) {
1798 ret = -EFAULT;
1799 goto out_drop_write;
1800 }
Li Zefan0caa1022010-12-20 16:30:25 +08001801
Liu Bob9ca0662012-06-29 03:58:49 -06001802 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1803 ret = -EINVAL;
1804 goto out_drop_write;
1805 }
Li Zefan0caa1022010-12-20 16:30:25 +08001806
Liu Bob9ca0662012-06-29 03:58:49 -06001807 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1808 ret = -EOPNOTSUPP;
1809 goto out_drop_write;
1810 }
Li Zefan0caa1022010-12-20 16:30:25 +08001811
1812 down_write(&root->fs_info->subvol_sem);
1813
1814 /* nothing to do */
1815 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001816 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001817
1818 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001819 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001820 btrfs_set_root_flags(&root->root_item,
1821 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001822 } else {
1823 /*
1824 * Block RO -> RW transition if this subvolume is involved in
1825 * send
1826 */
1827 spin_lock(&root->root_item_lock);
1828 if (root->send_in_progress == 0) {
1829 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001830 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001831 spin_unlock(&root->root_item_lock);
1832 } else {
1833 spin_unlock(&root->root_item_lock);
1834 btrfs_warn(root->fs_info,
1835 "Attempt to set subvolume %llu read-write during send",
1836 root->root_key.objectid);
1837 ret = -EPERM;
1838 goto out_drop_sem;
1839 }
1840 }
Li Zefan0caa1022010-12-20 16:30:25 +08001841
1842 trans = btrfs_start_transaction(root, 1);
1843 if (IS_ERR(trans)) {
1844 ret = PTR_ERR(trans);
1845 goto out_reset;
1846 }
1847
Li Zefanb4dc2b82011-02-16 06:06:34 +00001848 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001849 &root->root_key, &root->root_item);
1850
1851 btrfs_commit_transaction(trans, root);
1852out_reset:
1853 if (ret)
1854 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001855out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001856 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001857out_drop_write:
1858 mnt_drop_write_file(file);
1859out:
Li Zefan0caa1022010-12-20 16:30:25 +08001860 return ret;
1861}
1862
Yan, Zheng76dda932009-09-21 16:00:26 -04001863/*
1864 * helper to check if the subvolume references other subvolumes
1865 */
1866static noinline int may_destroy_subvol(struct btrfs_root *root)
1867{
1868 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001869 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001870 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001871 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001872 int ret;
1873
1874 path = btrfs_alloc_path();
1875 if (!path)
1876 return -ENOMEM;
1877
Josef Bacik175a2b82013-08-12 15:36:44 -04001878 /* Make sure this root isn't set as the default subvol */
1879 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1880 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1881 dir_id, "default", 7, 0);
1882 if (di && !IS_ERR(di)) {
1883 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1884 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001885 ret = -EPERM;
1886 btrfs_err(root->fs_info, "deleting default subvolume "
1887 "%llu is not allowed", key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001888 goto out;
1889 }
1890 btrfs_release_path(path);
1891 }
1892
Yan, Zheng76dda932009-09-21 16:00:26 -04001893 key.objectid = root->root_key.objectid;
1894 key.type = BTRFS_ROOT_REF_KEY;
1895 key.offset = (u64)-1;
1896
1897 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1898 &key, path, 0, 0);
1899 if (ret < 0)
1900 goto out;
1901 BUG_ON(ret == 0);
1902
1903 ret = 0;
1904 if (path->slots[0] > 0) {
1905 path->slots[0]--;
1906 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1907 if (key.objectid == root->root_key.objectid &&
1908 key.type == BTRFS_ROOT_REF_KEY)
1909 ret = -ENOTEMPTY;
1910 }
1911out:
1912 btrfs_free_path(path);
1913 return ret;
1914}
1915
Chris Masonac8e9812010-02-28 15:39:26 -05001916static noinline int key_in_sk(struct btrfs_key *key,
1917 struct btrfs_ioctl_search_key *sk)
1918{
Chris Masonabc6e132010-03-18 12:10:08 -04001919 struct btrfs_key test;
1920 int ret;
1921
1922 test.objectid = sk->min_objectid;
1923 test.type = sk->min_type;
1924 test.offset = sk->min_offset;
1925
1926 ret = btrfs_comp_cpu_keys(key, &test);
1927 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001928 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001929
1930 test.objectid = sk->max_objectid;
1931 test.type = sk->max_type;
1932 test.offset = sk->max_offset;
1933
1934 ret = btrfs_comp_cpu_keys(key, &test);
1935 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001936 return 0;
1937 return 1;
1938}
1939
1940static noinline int copy_to_sk(struct btrfs_root *root,
1941 struct btrfs_path *path,
1942 struct btrfs_key *key,
1943 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001944 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001945 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001946 unsigned long *sk_offset,
1947 int *num_found)
1948{
1949 u64 found_transid;
1950 struct extent_buffer *leaf;
1951 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001952 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001953 unsigned long item_off;
1954 unsigned long item_len;
1955 int nritems;
1956 int i;
1957 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001958 int ret = 0;
1959
1960 leaf = path->nodes[0];
1961 slot = path->slots[0];
1962 nritems = btrfs_header_nritems(leaf);
1963
1964 if (btrfs_header_generation(leaf) > sk->max_transid) {
1965 i = nritems;
1966 goto advance_key;
1967 }
1968 found_transid = btrfs_header_generation(leaf);
1969
1970 for (i = slot; i < nritems; i++) {
1971 item_off = btrfs_item_ptr_offset(leaf, i);
1972 item_len = btrfs_item_size_nr(leaf, i);
1973
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001974 btrfs_item_key_to_cpu(leaf, key, i);
1975 if (!key_in_sk(key, sk))
1976 continue;
1977
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001978 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001979 if (*num_found) {
1980 ret = 1;
1981 goto out;
1982 }
Chris Masonac8e9812010-02-28 15:39:26 -05001983
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001984 /*
1985 * return one empty item back for v1, which does not
1986 * handle -EOVERFLOW
1987 */
1988
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001989 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001990 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001991 ret = -EOVERFLOW;
1992 }
Chris Masonac8e9812010-02-28 15:39:26 -05001993
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001994 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05001995 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001996 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001997 }
1998
Chris Masonac8e9812010-02-28 15:39:26 -05001999 sh.objectid = key->objectid;
2000 sh.offset = key->offset;
2001 sh.type = key->type;
2002 sh.len = item_len;
2003 sh.transid = found_transid;
2004
2005 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002006 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2007 ret = -EFAULT;
2008 goto out;
2009 }
2010
Chris Masonac8e9812010-02-28 15:39:26 -05002011 *sk_offset += sizeof(sh);
2012
2013 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002014 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002015 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002016 if (read_extent_buffer_to_user(leaf, up,
2017 item_off, item_len)) {
2018 ret = -EFAULT;
2019 goto out;
2020 }
2021
Chris Masonac8e9812010-02-28 15:39:26 -05002022 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002023 }
Hugo Millse2156862011-05-14 17:43:41 +00002024 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002025
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002026 if (ret) /* -EOVERFLOW from above */
2027 goto out;
2028
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002029 if (*num_found >= sk->nr_items) {
2030 ret = 1;
2031 goto out;
2032 }
Chris Masonac8e9812010-02-28 15:39:26 -05002033 }
2034advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002035 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002036 test.objectid = sk->max_objectid;
2037 test.type = sk->max_type;
2038 test.offset = sk->max_offset;
2039 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2040 ret = 1;
2041 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002042 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002043 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002044 key->offset = 0;
2045 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002046 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002047 key->offset = 0;
2048 key->type = 0;
2049 key->objectid++;
2050 } else
2051 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002052out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002053 /*
2054 * 0: all items from this leaf copied, continue with next
2055 * 1: * more items can be copied, but unused buffer is too small
2056 * * all items were found
2057 * Either way, it will stops the loop which iterates to the next
2058 * leaf
2059 * -EOVERFLOW: item was to large for buffer
2060 * -EFAULT: could not copy extent buffer back to userspace
2061 */
Chris Masonac8e9812010-02-28 15:39:26 -05002062 return ret;
2063}
2064
2065static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002066 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002067 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002068 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002069{
2070 struct btrfs_root *root;
2071 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002072 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002073 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2074 int ret;
2075 int num_found = 0;
2076 unsigned long sk_offset = 0;
2077
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002078 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2079 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002080 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002081 }
Gerhard Heift12544442014-01-30 16:23:58 +01002082
Chris Masonac8e9812010-02-28 15:39:26 -05002083 path = btrfs_alloc_path();
2084 if (!path)
2085 return -ENOMEM;
2086
2087 if (sk->tree_id == 0) {
2088 /* search the root of the inode that was passed */
2089 root = BTRFS_I(inode)->root;
2090 } else {
2091 key.objectid = sk->tree_id;
2092 key.type = BTRFS_ROOT_ITEM_KEY;
2093 key.offset = (u64)-1;
2094 root = btrfs_read_fs_root_no_name(info, &key);
2095 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002096 btrfs_err(info, "could not find root %llu",
Chris Masonac8e9812010-02-28 15:39:26 -05002097 sk->tree_id);
2098 btrfs_free_path(path);
2099 return -ENOENT;
2100 }
2101 }
2102
2103 key.objectid = sk->min_objectid;
2104 key.type = sk->min_type;
2105 key.offset = sk->min_offset;
2106
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302107 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002108 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002109 if (ret != 0) {
2110 if (ret > 0)
2111 ret = 0;
2112 goto err;
2113 }
Gerhard Heiftba346b32014-01-30 16:24:02 +01002114 ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002115 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002116 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002117 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002118 break;
2119
2120 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002121 if (ret > 0)
2122 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002123err:
2124 sk->nr_items = num_found;
2125 btrfs_free_path(path);
2126 return ret;
2127}
2128
2129static noinline int btrfs_ioctl_tree_search(struct file *file,
2130 void __user *argp)
2131{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002132 struct btrfs_ioctl_search_args __user *uargs;
2133 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002134 struct inode *inode;
2135 int ret;
2136 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002137
2138 if (!capable(CAP_SYS_ADMIN))
2139 return -EPERM;
2140
Gerhard Heiftba346b32014-01-30 16:24:02 +01002141 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002142
Gerhard Heiftba346b32014-01-30 16:24:02 +01002143 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2144 return -EFAULT;
2145
2146 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002147
Al Viro496ad9a2013-01-23 17:07:38 -05002148 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002149 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002150
2151 /*
2152 * In the origin implementation an overflow is handled by returning a
2153 * search header with a len of zero, so reset ret.
2154 */
2155 if (ret == -EOVERFLOW)
2156 ret = 0;
2157
Gerhard Heiftba346b32014-01-30 16:24:02 +01002158 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002159 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002160 return ret;
2161}
2162
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002163static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2164 void __user *argp)
2165{
2166 struct btrfs_ioctl_search_args_v2 __user *uarg;
2167 struct btrfs_ioctl_search_args_v2 args;
2168 struct inode *inode;
2169 int ret;
2170 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002171 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002172
2173 if (!capable(CAP_SYS_ADMIN))
2174 return -EPERM;
2175
2176 /* copy search header and buffer size */
2177 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2178 if (copy_from_user(&args, uarg, sizeof(args)))
2179 return -EFAULT;
2180
2181 buf_size = args.buf_size;
2182
2183 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2184 return -EOVERFLOW;
2185
2186 /* limit result size to 16MB */
2187 if (buf_size > buf_limit)
2188 buf_size = buf_limit;
2189
2190 inode = file_inode(file);
2191 ret = search_ioctl(inode, &args.key, &buf_size,
2192 (char *)(&uarg->buf[0]));
2193 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2194 ret = -EFAULT;
2195 else if (ret == -EOVERFLOW &&
2196 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2197 ret = -EFAULT;
2198
Yan, Zheng76dda932009-09-21 16:00:26 -04002199 return ret;
2200}
2201
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002202/*
Chris Masonac8e9812010-02-28 15:39:26 -05002203 * Search INODE_REFs to identify path name of 'dirid' directory
2204 * in a 'tree_id' tree. and sets path name to 'name'.
2205 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002206static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2207 u64 tree_id, u64 dirid, char *name)
2208{
2209 struct btrfs_root *root;
2210 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002211 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002212 int ret = -1;
2213 int slot;
2214 int len;
2215 int total_len = 0;
2216 struct btrfs_inode_ref *iref;
2217 struct extent_buffer *l;
2218 struct btrfs_path *path;
2219
2220 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2221 name[0]='\0';
2222 return 0;
2223 }
2224
2225 path = btrfs_alloc_path();
2226 if (!path)
2227 return -ENOMEM;
2228
Chris Masonac8e9812010-02-28 15:39:26 -05002229 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002230
2231 key.objectid = tree_id;
2232 key.type = BTRFS_ROOT_ITEM_KEY;
2233 key.offset = (u64)-1;
2234 root = btrfs_read_fs_root_no_name(info, &key);
2235 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002236 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002237 ret = -ENOENT;
2238 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002239 }
2240
2241 key.objectid = dirid;
2242 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002243 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002244
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302245 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002246 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2247 if (ret < 0)
2248 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002249 else if (ret > 0) {
2250 ret = btrfs_previous_item(root, path, dirid,
2251 BTRFS_INODE_REF_KEY);
2252 if (ret < 0)
2253 goto out;
2254 else if (ret > 0) {
2255 ret = -ENOENT;
2256 goto out;
2257 }
2258 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002259
2260 l = path->nodes[0];
2261 slot = path->slots[0];
2262 btrfs_item_key_to_cpu(l, &key, slot);
2263
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002264 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2265 len = btrfs_inode_ref_name_len(l, iref);
2266 ptr -= len + 1;
2267 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002268 if (ptr < name) {
2269 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002270 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002271 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002272
2273 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302274 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002275
2276 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2277 break;
2278
David Sterbab3b4aa72011-04-21 01:20:15 +02002279 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002280 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002281 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002282 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002283 }
Li Zefan77906a502011-07-14 03:16:00 +00002284 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302285 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002286 ret = 0;
2287out:
2288 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002289 return ret;
2290}
2291
2292static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2293 void __user *argp)
2294{
2295 struct btrfs_ioctl_ino_lookup_args *args;
2296 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002297 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002298
Julia Lawall2354d082010-10-29 15:14:18 -04002299 args = memdup_user(argp, sizeof(*args));
2300 if (IS_ERR(args))
2301 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002302
Al Viro496ad9a2013-01-23 17:07:38 -05002303 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002304
David Sterba01b810b2015-05-12 19:14:49 +02002305 /*
2306 * Unprivileged query to obtain the containing subvolume root id. The
2307 * path is reset so it's consistent with btrfs_search_path_in_tree.
2308 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002309 if (args->treeid == 0)
2310 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2311
David Sterba01b810b2015-05-12 19:14:49 +02002312 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2313 args->name[0] = 0;
2314 goto out;
2315 }
2316
2317 if (!capable(CAP_SYS_ADMIN)) {
2318 ret = -EPERM;
2319 goto out;
2320 }
2321
Chris Masonac8e9812010-02-28 15:39:26 -05002322 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2323 args->treeid, args->objectid,
2324 args->name);
2325
David Sterba01b810b2015-05-12 19:14:49 +02002326out:
Chris Masonac8e9812010-02-28 15:39:26 -05002327 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2328 ret = -EFAULT;
2329
2330 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002331 return ret;
2332}
2333
Yan, Zheng76dda932009-09-21 16:00:26 -04002334static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2335 void __user *arg)
2336{
Al Viro54563d42013-09-01 15:57:51 -04002337 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002338 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002339 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002340 struct inode *inode;
2341 struct btrfs_root *root = BTRFS_I(dir)->root;
2342 struct btrfs_root *dest = NULL;
2343 struct btrfs_ioctl_vol_args *vol_args;
2344 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002345 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002346 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002347 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002348 int namelen;
2349 int ret;
2350 int err = 0;
2351
Yan, Zheng76dda932009-09-21 16:00:26 -04002352 vol_args = memdup_user(arg, sizeof(*vol_args));
2353 if (IS_ERR(vol_args))
2354 return PTR_ERR(vol_args);
2355
2356 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2357 namelen = strlen(vol_args->name);
2358 if (strchr(vol_args->name, '/') ||
2359 strncmp(vol_args->name, "..", namelen) == 0) {
2360 err = -EINVAL;
2361 goto out;
2362 }
2363
Al Viroa561be72011-11-23 11:57:51 -05002364 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002365 if (err)
2366 goto out;
2367
David Sterba521e0542014-04-15 16:41:44 +02002368
David Sterba5c50c9b2013-03-22 18:12:51 +00002369 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2370 if (err == -EINTR)
David Sterbae43f9982013-12-06 17:51:32 +01002371 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002372 dentry = lookup_one_len(vol_args->name, parent, namelen);
2373 if (IS_ERR(dentry)) {
2374 err = PTR_ERR(dentry);
2375 goto out_unlock_dir;
2376 }
2377
David Howells2b0143b2015-03-17 22:25:59 +00002378 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002379 err = -ENOENT;
2380 goto out_dput;
2381 }
2382
David Howells2b0143b2015-03-17 22:25:59 +00002383 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002384 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302385 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002386 /*
2387 * Regular user. Only allow this with a special mount
2388 * option, when the user has write+exec access to the
2389 * subvol root, and when rmdir(2) would have been
2390 * allowed.
2391 *
2392 * Note that this is _not_ check that the subvol is
2393 * empty or doesn't contain data that we wouldn't
2394 * otherwise be able to delete.
2395 *
2396 * Users who want to delete empty subvols should try
2397 * rmdir(2).
2398 */
2399 err = -EPERM;
2400 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2401 goto out_dput;
2402
2403 /*
2404 * Do not allow deletion if the parent dir is the same
2405 * as the dir to be deleted. That means the ioctl
2406 * must be called on the dentry referencing the root
2407 * of the subvol, not a random directory contained
2408 * within it.
2409 */
2410 err = -EINVAL;
2411 if (root == dest)
2412 goto out_dput;
2413
2414 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2415 if (err)
2416 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002417 }
2418
Miao Xie5c39da52012-10-22 11:39:53 +00002419 /* check if subvolume may be deleted by a user */
2420 err = btrfs_may_delete(dir, dentry, 1);
2421 if (err)
2422 goto out_dput;
2423
Li Zefan33345d012011-04-20 10:31:50 +08002424 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002425 err = -EINVAL;
2426 goto out_dput;
2427 }
2428
Al Viro59551022016-01-22 15:40:57 -05002429 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002430
2431 /*
2432 * Don't allow to delete a subvolume with send in progress. This is
2433 * inside the i_mutex so the error handling that has to drop the bit
2434 * again is not run concurrently.
2435 */
2436 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002437 root_flags = btrfs_root_flags(&dest->root_item);
2438 if (dest->send_in_progress == 0) {
2439 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002440 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2441 spin_unlock(&dest->root_item_lock);
2442 } else {
2443 spin_unlock(&dest->root_item_lock);
2444 btrfs_warn(root->fs_info,
2445 "Attempt to delete subvolume %llu during send",
Filipe Mananac55bfa62014-05-25 03:55:44 +01002446 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002447 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002448 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002449 }
2450
Yan, Zheng76dda932009-09-21 16:00:26 -04002451 down_write(&root->fs_info->subvol_sem);
2452
2453 err = may_destroy_subvol(dest);
2454 if (err)
2455 goto out_up_write;
2456
Miao Xiec58aaad2013-02-28 10:05:36 +00002457 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2458 /*
2459 * One for dir inode, two for dir entries, two for root
2460 * ref/backref.
2461 */
2462 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002463 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002464 if (err)
2465 goto out_up_write;
2466
Yan, Zhenga22285a2010-05-16 10:48:46 -04002467 trans = btrfs_start_transaction(root, 0);
2468 if (IS_ERR(trans)) {
2469 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002470 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002471 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002472 trans->block_rsv = &block_rsv;
2473 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002474
Yan, Zheng76dda932009-09-21 16:00:26 -04002475 ret = btrfs_unlink_subvol(trans, root, dir,
2476 dest->root_key.objectid,
2477 dentry->d_name.name,
2478 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002479 if (ret) {
2480 err = ret;
2481 btrfs_abort_transaction(trans, root, ret);
2482 goto out_end_trans;
2483 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002484
2485 btrfs_record_root_in_trans(trans, dest);
2486
2487 memset(&dest->root_item.drop_progress, 0,
2488 sizeof(dest->root_item.drop_progress));
2489 dest->root_item.drop_level = 0;
2490 btrfs_set_root_refs(&dest->root_item, 0);
2491
Miao Xie27cdeb72014-04-02 19:51:05 +08002492 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002493 ret = btrfs_insert_orphan_item(trans,
2494 root->fs_info->tree_root,
2495 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002496 if (ret) {
2497 btrfs_abort_transaction(trans, root, ret);
2498 err = ret;
2499 goto out_end_trans;
2500 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002501 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002502
2503 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2504 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2505 dest->root_key.objectid);
2506 if (ret && ret != -ENOENT) {
2507 btrfs_abort_transaction(trans, root, ret);
2508 err = ret;
2509 goto out_end_trans;
2510 }
2511 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2512 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2513 dest->root_item.received_uuid,
2514 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2515 dest->root_key.objectid);
2516 if (ret && ret != -ENOENT) {
2517 btrfs_abort_transaction(trans, root, ret);
2518 err = ret;
2519 goto out_end_trans;
2520 }
2521 }
2522
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002523out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002524 trans->block_rsv = NULL;
2525 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002526 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002527 if (ret && !err)
2528 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002529 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002530out_release:
2531 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002532out_up_write:
2533 up_write(&root->fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002534 if (err) {
2535 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002536 root_flags = btrfs_root_flags(&dest->root_item);
2537 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002538 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2539 spin_unlock(&dest->root_item_lock);
2540 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002541out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002542 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002543 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002544 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002545 btrfs_invalidate_inodes(dest);
2546 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002547 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002548
2549 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002550 if (dest->ino_cache_inode) {
2551 iput(dest->ino_cache_inode);
2552 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002553 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002554 }
2555out_dput:
2556 dput(dentry);
2557out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002558 inode_unlock(dir);
David Sterbae43f9982013-12-06 17:51:32 +01002559out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002560 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002561out:
2562 kfree(vol_args);
2563 return err;
2564}
2565
Chris Mason1e701a32010-03-11 09:42:04 -05002566static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002567{
Al Viro496ad9a2013-01-23 17:07:38 -05002568 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002569 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002570 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002571 int ret;
2572
Ilya Dryomov25122d12013-01-20 15:57:57 +02002573 ret = mnt_want_write_file(file);
2574 if (ret)
2575 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002576
Ilya Dryomov25122d12013-01-20 15:57:57 +02002577 if (btrfs_root_readonly(root)) {
2578 ret = -EROFS;
2579 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002580 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002581
2582 switch (inode->i_mode & S_IFMT) {
2583 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002584 if (!capable(CAP_SYS_ADMIN)) {
2585 ret = -EPERM;
2586 goto out;
2587 }
Eric Sandeende78b512013-01-31 18:21:12 +00002588 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002589 if (ret)
2590 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002591 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002592 break;
2593 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002594 if (!(file->f_mode & FMODE_WRITE)) {
2595 ret = -EINVAL;
2596 goto out;
2597 }
Chris Mason1e701a32010-03-11 09:42:04 -05002598
2599 range = kzalloc(sizeof(*range), GFP_KERNEL);
2600 if (!range) {
2601 ret = -ENOMEM;
2602 goto out;
2603 }
2604
2605 if (argp) {
2606 if (copy_from_user(range, argp,
2607 sizeof(*range))) {
2608 ret = -EFAULT;
2609 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002610 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002611 }
2612 /* compression requires us to start the IO */
2613 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2614 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2615 range->extent_thresh = (u32)-1;
2616 }
2617 } else {
2618 /* the rest are all set to zero by kzalloc */
2619 range->len = (u64)-1;
2620 }
Al Viro496ad9a2013-01-23 17:07:38 -05002621 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002622 range, 0, 0);
2623 if (ret > 0)
2624 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002625 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002626 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002627 default:
2628 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002629 }
Chris Masone441d542009-01-05 16:57:23 -05002630out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002631 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002632 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002633}
2634
Christoph Hellwigb2950862008-12-02 09:54:17 -05002635static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002636{
2637 struct btrfs_ioctl_vol_args *vol_args;
2638 int ret;
2639
Chris Masone441d542009-01-05 16:57:23 -05002640 if (!capable(CAP_SYS_ADMIN))
2641 return -EPERM;
2642
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002643 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2644 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002645 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002646 }
2647
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002648 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002649 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002650 if (IS_ERR(vol_args)) {
2651 ret = PTR_ERR(vol_args);
2652 goto out;
2653 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002654
Mark Fasheh5516e592008-07-24 12:20:14 -04002655 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002656 ret = btrfs_init_new_device(root, vol_args->name);
2657
Anand Jain43d20762014-07-01 00:58:56 +08002658 if (!ret)
2659 btrfs_info(root->fs_info, "disk added %s",vol_args->name);
2660
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002661 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002662out:
2663 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002664 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002665 return ret;
2666}
2667
Miao Xieda249272012-11-26 08:44:50 +00002668static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002669{
Al Viro496ad9a2013-01-23 17:07:38 -05002670 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002671 struct btrfs_ioctl_vol_args *vol_args;
2672 int ret;
2673
Chris Masone441d542009-01-05 16:57:23 -05002674 if (!capable(CAP_SYS_ADMIN))
2675 return -EPERM;
2676
Miao Xieda249272012-11-26 08:44:50 +00002677 ret = mnt_want_write_file(file);
2678 if (ret)
2679 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002680
Li Zefandae7b662009-04-08 15:06:54 +08002681 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002682 if (IS_ERR(vol_args)) {
2683 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002684 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002685 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002686
Mark Fasheh5516e592008-07-24 12:20:14 -04002687 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002688
Anand Jain183860f2013-05-17 10:52:45 +00002689 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2690 1)) {
2691 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2692 goto out;
2693 }
2694
2695 mutex_lock(&root->fs_info->volume_mutex);
2696 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002697 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002698 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002699
Anand Jainec95d492014-07-01 00:58:57 +08002700 if (!ret)
2701 btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
2702
Anand Jain183860f2013-05-17 10:52:45 +00002703out:
2704 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002705err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002706 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002707 return ret;
2708}
2709
Jan Schmidt475f6382011-03-11 15:41:01 +01002710static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2711{
Li Zefan027ed2f2011-06-08 08:27:56 +00002712 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002713 struct btrfs_device *device;
Jan Schmidt475f6382011-03-11 15:41:01 +01002714 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002715 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002716
Li Zefan027ed2f2011-06-08 08:27:56 +00002717 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2718 if (!fi_args)
2719 return -ENOMEM;
2720
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002721 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002722 fi_args->num_devices = fs_devices->num_devices;
2723 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002724
Byongho Leed7641a42015-09-01 23:10:57 +09002725 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002726 if (device->devid > fi_args->max_id)
2727 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002728 }
2729 mutex_unlock(&fs_devices->device_list_mutex);
2730
David Sterba80a773f2014-05-07 18:17:06 +02002731 fi_args->nodesize = root->fs_info->super_copy->nodesize;
2732 fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
2733 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
2734
Li Zefan027ed2f2011-06-08 08:27:56 +00002735 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2736 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002737
Li Zefan027ed2f2011-06-08 08:27:56 +00002738 kfree(fi_args);
2739 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002740}
2741
2742static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2743{
2744 struct btrfs_ioctl_dev_info_args *di_args;
2745 struct btrfs_device *dev;
2746 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2747 int ret = 0;
2748 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002749
Jan Schmidt475f6382011-03-11 15:41:01 +01002750 di_args = memdup_user(arg, sizeof(*di_args));
2751 if (IS_ERR(di_args))
2752 return PTR_ERR(di_args);
2753
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002754 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002755 s_uuid = di_args->uuid;
2756
2757 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002758 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002759
2760 if (!dev) {
2761 ret = -ENODEV;
2762 goto out;
2763 }
2764
2765 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002766 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2767 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002768 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002769 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002770 struct rcu_string *name;
2771
2772 rcu_read_lock();
2773 name = rcu_dereference(dev->name);
2774 strncpy(di_args->path, name->str, sizeof(di_args->path));
2775 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002776 di_args->path[sizeof(di_args->path) - 1] = 0;
2777 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002778 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002779 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002780
2781out:
David Sterba55793c02013-04-26 15:20:23 +00002782 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002783 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2784 ret = -EFAULT;
2785
2786 kfree(di_args);
2787 return ret;
2788}
2789
Mark Fashehf4414602015-06-30 14:42:05 -07002790static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002791{
2792 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002793 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2794
Mark Fasheh416161d2013-08-06 11:42:51 -07002795 page = grab_cache_page(inode->i_mapping, index);
2796 if (!page)
2797 return NULL;
2798
2799 if (!PageUptodate(page)) {
2800 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2801 0))
2802 return NULL;
2803 lock_page(page);
2804 if (!PageUptodate(page)) {
2805 unlock_page(page);
2806 page_cache_release(page);
2807 return NULL;
2808 }
2809 }
2810 unlock_page(page);
2811
2812 return page;
2813}
2814
Mark Fashehf4414602015-06-30 14:42:05 -07002815static int gather_extent_pages(struct inode *inode, struct page **pages,
2816 int num_pages, u64 off)
2817{
2818 int i;
2819 pgoff_t index = off >> PAGE_CACHE_SHIFT;
2820
2821 for (i = 0; i < num_pages; i++) {
2822 pages[i] = extent_same_get_page(inode, index + i);
2823 if (!pages[i])
2824 return -ENOMEM;
2825 }
2826 return 0;
2827}
2828
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002829static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2830{
2831 /* do any pending delalloc/csum calc on src, one way or
2832 another, and lock file content */
2833 while (1) {
2834 struct btrfs_ordered_extent *ordered;
2835 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2836 ordered = btrfs_lookup_first_ordered_extent(inode,
2837 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002838 if ((!ordered ||
2839 ordered->file_offset + ordered->len <= off ||
2840 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002841 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002842 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2843 if (ordered)
2844 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002845 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002846 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002847 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2848 if (ordered)
2849 btrfs_put_ordered_extent(ordered);
2850 btrfs_wait_ordered_range(inode, off, len);
2851 }
2852}
2853
Mark Fashehf4414602015-06-30 14:42:05 -07002854static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002855{
Al Viro59551022016-01-22 15:40:57 -05002856 inode_unlock(inode1);
2857 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002858}
2859
Mark Fashehf4414602015-06-30 14:42:05 -07002860static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2861{
2862 if (inode1 < inode2)
2863 swap(inode1, inode2);
2864
Al Viro59551022016-01-22 15:40:57 -05002865 inode_lock_nested(inode1, I_MUTEX_PARENT);
2866 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002867}
2868
2869static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2870 struct inode *inode2, u64 loff2, u64 len)
2871{
2872 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2873 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2874}
2875
2876static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2877 struct inode *inode2, u64 loff2, u64 len)
Mark Fasheh416161d2013-08-06 11:42:51 -07002878{
2879 if (inode1 < inode2) {
2880 swap(inode1, inode2);
2881 swap(loff1, loff2);
2882 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002883 lock_extent_range(inode1, loff1, len);
Mark Fasheh293a8482015-06-30 14:42:06 -07002884 lock_extent_range(inode2, loff2, len);
Mark Fashehf4414602015-06-30 14:42:05 -07002885}
2886
2887struct cmp_pages {
2888 int num_pages;
2889 struct page **src_pages;
2890 struct page **dst_pages;
2891};
2892
2893static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2894{
2895 int i;
2896 struct page *pg;
2897
2898 for (i = 0; i < cmp->num_pages; i++) {
2899 pg = cmp->src_pages[i];
2900 if (pg)
2901 page_cache_release(pg);
2902 pg = cmp->dst_pages[i];
2903 if (pg)
2904 page_cache_release(pg);
Mark Fasheh416161d2013-08-06 11:42:51 -07002905 }
Mark Fashehf4414602015-06-30 14:42:05 -07002906 kfree(cmp->src_pages);
2907 kfree(cmp->dst_pages);
2908}
2909
2910static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2911 struct inode *dst, u64 dst_loff,
2912 u64 len, struct cmp_pages *cmp)
2913{
2914 int ret;
2915 int num_pages = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
2916 struct page **src_pgarr, **dst_pgarr;
2917
2918 /*
2919 * We must gather up all the pages before we initiate our
2920 * extent locking. We use an array for the page pointers. Size
2921 * of the array is bounded by len, which is in turn bounded by
2922 * BTRFS_MAX_DEDUPE_LEN.
2923 */
2924 src_pgarr = kzalloc(num_pages * sizeof(struct page *), GFP_NOFS);
2925 dst_pgarr = kzalloc(num_pages * sizeof(struct page *), GFP_NOFS);
2926 if (!src_pgarr || !dst_pgarr) {
2927 kfree(src_pgarr);
2928 kfree(dst_pgarr);
2929 return -ENOMEM;
2930 }
2931 cmp->num_pages = num_pages;
2932 cmp->src_pages = src_pgarr;
2933 cmp->dst_pages = dst_pgarr;
2934
2935 ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff);
2936 if (ret)
2937 goto out;
2938
2939 ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff);
2940
2941out:
2942 if (ret)
2943 btrfs_cmp_data_free(cmp);
2944 return 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002945}
2946
2947static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
Mark Fashehf4414602015-06-30 14:42:05 -07002948 u64 dst_loff, u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002949{
2950 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07002951 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07002952 struct page *src_page, *dst_page;
2953 unsigned int cmp_len = PAGE_CACHE_SIZE;
2954 void *addr, *dst_addr;
2955
Mark Fashehf4414602015-06-30 14:42:05 -07002956 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002957 while (len) {
2958 if (len < PAGE_CACHE_SIZE)
2959 cmp_len = len;
2960
Mark Fashehf4414602015-06-30 14:42:05 -07002961 BUG_ON(i >= cmp->num_pages);
2962
2963 src_page = cmp->src_pages[i];
2964 dst_page = cmp->dst_pages[i];
2965
Mark Fasheh416161d2013-08-06 11:42:51 -07002966 addr = kmap_atomic(src_page);
2967 dst_addr = kmap_atomic(dst_page);
2968
2969 flush_dcache_page(src_page);
2970 flush_dcache_page(dst_page);
2971
2972 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08002973 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002974
2975 kunmap_atomic(addr);
2976 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07002977
2978 if (ret)
2979 break;
2980
Mark Fasheh416161d2013-08-06 11:42:51 -07002981 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07002982 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07002983 }
2984
2985 return ret;
2986}
2987
Mark Fashehe1d227a2015-06-08 15:05:25 -07002988static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
2989 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07002990{
Mark Fashehe1d227a2015-06-08 15:05:25 -07002991 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07002992 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2993
Mark Fashehe1d227a2015-06-08 15:05:25 -07002994 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07002995 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07002996
2997 /* if we extend to eof, continue to block boundary */
2998 if (off + len == inode->i_size)
2999 *plen = len = ALIGN(inode->i_size, bs) - off;
3000
Mark Fasheh416161d2013-08-06 11:42:51 -07003001 /* Check that we are block aligned - btrfs_clone() requires this */
3002 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3003 return -EINVAL;
3004
3005 return 0;
3006}
3007
Mark Fashehe1d227a2015-06-08 15:05:25 -07003008static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003009 struct inode *dst, u64 dst_loff)
3010{
3011 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003012 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003013 struct cmp_pages cmp;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003014 int same_inode = 0;
3015 u64 same_lock_start = 0;
3016 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003017
Mark Fasheh416161d2013-08-06 11:42:51 -07003018 if (src == dst)
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003019 same_inode = 1;
Mark Fasheh416161d2013-08-06 11:42:51 -07003020
Filipe Manana113e8282015-03-30 18:26:47 +01003021 if (len == 0)
3022 return 0;
3023
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003024 if (same_inode) {
Al Viro59551022016-01-22 15:40:57 -05003025 inode_lock(src);
Mark Fasheh416161d2013-08-06 11:42:51 -07003026
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003027 ret = extent_same_check_offsets(src, loff, &len, olen);
3028 if (ret)
3029 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003030
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003031 /*
3032 * Single inode case wants the same checks, except we
3033 * don't want our length pushed out past i_size as
3034 * comparing that data range makes no sense.
3035 *
3036 * extent_same_check_offsets() will do this for an
3037 * unaligned length at i_size, so catch it here and
3038 * reject the request.
3039 *
3040 * This effectively means we require aligned extents
3041 * for the single-inode case, whereas the other cases
3042 * allow an unaligned length so long as it ends at
3043 * i_size.
3044 */
3045 if (len != olen) {
3046 ret = -EINVAL;
3047 goto out_unlock;
3048 }
3049
3050 /* Check for overlapping ranges */
3051 if (dst_loff + len > loff && dst_loff < loff + len) {
3052 ret = -EINVAL;
3053 goto out_unlock;
3054 }
3055
3056 same_lock_start = min_t(u64, loff, dst_loff);
3057 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
3058 } else {
3059 btrfs_double_inode_lock(src, dst);
3060
3061 ret = extent_same_check_offsets(src, loff, &len, olen);
3062 if (ret)
3063 goto out_unlock;
3064
3065 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3066 if (ret)
3067 goto out_unlock;
3068 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003069
3070 /* don't make the dst file partly checksummed */
3071 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3072 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3073 ret = -EINVAL;
3074 goto out_unlock;
3075 }
3076
Mark Fashehf4414602015-06-30 14:42:05 -07003077 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3078 if (ret)
3079 goto out_unlock;
3080
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003081 if (same_inode)
3082 lock_extent_range(src, same_lock_start, same_lock_len);
3083 else
3084 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003085
Mark Fasheh207910d2015-06-30 14:42:04 -07003086 /* pass original length for comparison so we stay within i_size */
Mark Fashehf4414602015-06-30 14:42:05 -07003087 ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003088 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003089 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003090
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003091 if (same_inode)
3092 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3093 same_lock_start + same_lock_len - 1);
3094 else
3095 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003096
3097 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003098out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003099 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003100 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003101 else
3102 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003103
3104 return ret;
3105}
3106
Byongho Leeee221842015-12-15 01:42:10 +09003107#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003108
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003109ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3110 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003111{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003112 struct inode *src = file_inode(src_file);
3113 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003114 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003115 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003116
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003117 if (olen > BTRFS_MAX_DEDUPE_LEN)
3118 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003119
3120 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
3121 /*
3122 * Btrfs does not support blocksize < page_size. As a
3123 * result, btrfs_cmp_data() won't correctly handle
3124 * this situation without an update.
3125 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003126 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003127 }
3128
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003129 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3130 if (res)
3131 return res;
3132 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003133}
3134
Filipe Mananaf82a9902014-06-01 01:50:28 +01003135static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3136 struct inode *inode,
3137 u64 endoff,
3138 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003139 const u64 olen,
3140 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003141{
3142 struct btrfs_root *root = BTRFS_I(inode)->root;
3143 int ret;
3144
3145 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003146 if (!no_time_update)
Deepa Dinamani04b285f2016-02-06 23:57:21 -08003147 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003148 /*
3149 * We round up to the block size at eof when determining which
3150 * extents to clone above, but shouldn't round up the file size.
3151 */
3152 if (endoff > destoff + olen)
3153 endoff = destoff + olen;
3154 if (endoff > inode->i_size)
3155 btrfs_i_size_write(inode, endoff);
3156
3157 ret = btrfs_update_inode(trans, root, inode);
3158 if (ret) {
3159 btrfs_abort_transaction(trans, root, ret);
3160 btrfs_end_transaction(trans, root);
3161 goto out;
3162 }
3163 ret = btrfs_end_transaction(trans, root);
3164out:
3165 return ret;
3166}
3167
Filipe Manana7ffbb592014-06-09 03:48:05 +01003168static void clone_update_extent_map(struct inode *inode,
3169 const struct btrfs_trans_handle *trans,
3170 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003171 const u64 hole_offset,
3172 const u64 hole_len)
3173{
3174 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3175 struct extent_map *em;
3176 int ret;
3177
3178 em = alloc_extent_map();
3179 if (!em) {
3180 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3181 &BTRFS_I(inode)->runtime_flags);
3182 return;
3183 }
3184
Filipe Manana14f59792014-06-29 21:45:40 +01003185 if (path) {
3186 struct btrfs_file_extent_item *fi;
3187
3188 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3189 struct btrfs_file_extent_item);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003190 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3191 em->generation = -1;
3192 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3193 BTRFS_FILE_EXTENT_INLINE)
3194 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3195 &BTRFS_I(inode)->runtime_flags);
3196 } else {
3197 em->start = hole_offset;
3198 em->len = hole_len;
3199 em->ram_bytes = em->len;
3200 em->orig_start = hole_offset;
3201 em->block_start = EXTENT_MAP_HOLE;
3202 em->block_len = 0;
3203 em->orig_block_len = 0;
3204 em->compress_type = BTRFS_COMPRESS_NONE;
3205 em->generation = trans->transid;
3206 }
3207
3208 while (1) {
3209 write_lock(&em_tree->lock);
3210 ret = add_extent_mapping(em_tree, em, 1);
3211 write_unlock(&em_tree->lock);
3212 if (ret != -EEXIST) {
3213 free_extent_map(em);
3214 break;
3215 }
3216 btrfs_drop_extent_cache(inode, em->start,
3217 em->start + em->len - 1, 0);
3218 }
3219
David Sterbaee39b432014-09-30 01:33:33 +02003220 if (ret)
Filipe Manana7ffbb592014-06-09 03:48:05 +01003221 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3222 &BTRFS_I(inode)->runtime_flags);
3223}
3224
Filipe Manana8039d872015-10-13 15:15:00 +01003225/*
3226 * Make sure we do not end up inserting an inline extent into a file that has
3227 * already other (non-inline) extents. If a file has an inline extent it can
3228 * not have any other extents and the (single) inline extent must start at the
3229 * file offset 0. Failing to respect these rules will lead to file corruption,
3230 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3231 *
3232 * We can have extents that have been already written to disk or we can have
3233 * dirty ranges still in delalloc, in which case the extent maps and items are
3234 * created only when we run delalloc, and the delalloc ranges might fall outside
3235 * the range we are currently locking in the inode's io tree. So we check the
3236 * inode's i_size because of that (i_size updates are done while holding the
3237 * i_mutex, which we are holding here).
3238 * We also check to see if the inode has a size not greater than "datal" but has
3239 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3240 * protected against such concurrent fallocate calls by the i_mutex).
3241 *
3242 * If the file has no extents but a size greater than datal, do not allow the
3243 * copy because we would need turn the inline extent into a non-inline one (even
3244 * with NO_HOLES enabled). If we find our destination inode only has one inline
3245 * extent, just overwrite it with the source inline extent if its size is less
3246 * than the source extent's size, or we could copy the source inline extent's
3247 * data into the destination inode's inline extent if the later is greater then
3248 * the former.
3249 */
3250static int clone_copy_inline_extent(struct inode *src,
3251 struct inode *dst,
3252 struct btrfs_trans_handle *trans,
3253 struct btrfs_path *path,
3254 struct btrfs_key *new_key,
3255 const u64 drop_start,
3256 const u64 datal,
3257 const u64 skip,
3258 const u64 size,
3259 char *inline_data)
3260{
3261 struct btrfs_root *root = BTRFS_I(dst)->root;
3262 const u64 aligned_end = ALIGN(new_key->offset + datal,
3263 root->sectorsize);
3264 int ret;
3265 struct btrfs_key key;
3266
3267 if (new_key->offset > 0)
3268 return -EOPNOTSUPP;
3269
3270 key.objectid = btrfs_ino(dst);
3271 key.type = BTRFS_EXTENT_DATA_KEY;
3272 key.offset = 0;
3273 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3274 if (ret < 0) {
3275 return ret;
3276 } else if (ret > 0) {
3277 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3278 ret = btrfs_next_leaf(root, path);
3279 if (ret < 0)
3280 return ret;
3281 else if (ret > 0)
3282 goto copy_inline_extent;
3283 }
3284 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3285 if (key.objectid == btrfs_ino(dst) &&
3286 key.type == BTRFS_EXTENT_DATA_KEY) {
3287 ASSERT(key.offset > 0);
3288 return -EOPNOTSUPP;
3289 }
3290 } else if (i_size_read(dst) <= datal) {
3291 struct btrfs_file_extent_item *ei;
3292 u64 ext_len;
3293
3294 /*
3295 * If the file size is <= datal, make sure there are no other
3296 * extents following (can happen do to an fallocate call with
3297 * the flag FALLOC_FL_KEEP_SIZE).
3298 */
3299 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3300 struct btrfs_file_extent_item);
3301 /*
3302 * If it's an inline extent, it can not have other extents
3303 * following it.
3304 */
3305 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3306 BTRFS_FILE_EXTENT_INLINE)
3307 goto copy_inline_extent;
3308
3309 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3310 if (ext_len > aligned_end)
3311 return -EOPNOTSUPP;
3312
3313 ret = btrfs_next_item(root, path);
3314 if (ret < 0) {
3315 return ret;
3316 } else if (ret == 0) {
3317 btrfs_item_key_to_cpu(path->nodes[0], &key,
3318 path->slots[0]);
3319 if (key.objectid == btrfs_ino(dst) &&
3320 key.type == BTRFS_EXTENT_DATA_KEY)
3321 return -EOPNOTSUPP;
3322 }
3323 }
3324
3325copy_inline_extent:
3326 /*
3327 * We have no extent items, or we have an extent at offset 0 which may
3328 * or may not be inlined. All these cases are dealt the same way.
3329 */
3330 if (i_size_read(dst) > datal) {
3331 /*
3332 * If the destination inode has an inline extent...
3333 * This would require copying the data from the source inline
3334 * extent into the beginning of the destination's inline extent.
3335 * But this is really complex, both extents can be compressed
3336 * or just one of them, which would require decompressing and
3337 * re-compressing data (which could increase the new compressed
3338 * size, not allowing the compressed data to fit anymore in an
3339 * inline extent).
3340 * So just don't support this case for now (it should be rare,
3341 * we are not really saving space when cloning inline extents).
3342 */
3343 return -EOPNOTSUPP;
3344 }
3345
3346 btrfs_release_path(path);
3347 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3348 if (ret)
3349 return ret;
3350 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3351 if (ret)
3352 return ret;
3353
3354 if (skip) {
3355 const u32 start = btrfs_file_extent_calc_inline_size(0);
3356
3357 memmove(inline_data + start, inline_data + start + skip, datal);
3358 }
3359
3360 write_extent_buffer(path->nodes[0], inline_data,
3361 btrfs_item_ptr_offset(path->nodes[0],
3362 path->slots[0]),
3363 size);
3364 inode_add_bytes(dst, datal);
3365
3366 return 0;
3367}
3368
Mark Fasheh32b7c682013-08-06 11:42:49 -07003369/**
3370 * btrfs_clone() - clone a range from inode file to another
3371 *
3372 * @src: Inode to clone from
3373 * @inode: Inode to clone to
3374 * @off: Offset within source to start clone from
3375 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003376 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003377 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003378 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003379 */
3380static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003381 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003382 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003383{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003384 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003385 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003386 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003387 struct btrfs_trans_handle *trans;
3388 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003389 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003390 u32 nritems;
3391 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003392 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003393 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003394 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003395
3396 ret = -ENOMEM;
David Sterba707e8a02014-06-04 19:22:26 +02003397 buf = vmalloc(root->nodesize);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003398 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003399 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003400
3401 path = btrfs_alloc_path();
3402 if (!path) {
3403 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003404 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003405 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003406
David Sterbae4058b52015-11-27 16:31:35 +01003407 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003408 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08003409 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003410 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003411 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003412
3413 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003414 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003415
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003416 /*
3417 * note the key will change type as we walk through the
3418 * tree.
3419 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003420 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003421 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3422 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003423 if (ret < 0)
3424 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003425 /*
3426 * First search, if no extent item that starts at offset off was
3427 * found but the previous item is an extent item, it's possible
3428 * it might overlap our target range, therefore process it.
3429 */
3430 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3431 btrfs_item_key_to_cpu(path->nodes[0], &key,
3432 path->slots[0] - 1);
3433 if (key.type == BTRFS_EXTENT_DATA_KEY)
3434 path->slots[0]--;
3435 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003436
Yan Zhengae01a0a2008-08-04 23:23:47 -04003437 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003438process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003439 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003440 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003441 if (ret < 0)
3442 goto out;
3443 if (ret > 0)
3444 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003445 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003446 }
3447 leaf = path->nodes[0];
3448 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003449
Yan Zhengae01a0a2008-08-04 23:23:47 -04003450 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003451 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08003452 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003453 break;
3454
David Sterba962a2982014-06-04 18:41:45 +02003455 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003456 struct btrfs_file_extent_item *extent;
3457 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003458 u32 size;
3459 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003460 u64 disko = 0, diskl = 0;
3461 u64 datao = 0, datal = 0;
3462 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003463 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003464
Sage Weilc5c9cd42008-11-12 14:32:25 -05003465 extent = btrfs_item_ptr(leaf, slot,
3466 struct btrfs_file_extent_item);
3467 comp = btrfs_file_extent_compression(leaf, extent);
3468 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003469 if (type == BTRFS_FILE_EXTENT_REG ||
3470 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003471 disko = btrfs_file_extent_disk_bytenr(leaf,
3472 extent);
3473 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3474 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003475 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003476 datal = btrfs_file_extent_num_bytes(leaf,
3477 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003478 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3479 /* take upper bound, may be compressed */
3480 datal = btrfs_file_extent_ram_bytes(leaf,
3481 extent);
3482 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003483
Filipe Manana2c463822014-05-31 02:31:05 +01003484 /*
3485 * The first search might have left us at an extent
3486 * item that ends before our target range's start, can
3487 * happen if we have holes and NO_HOLES feature enabled.
3488 */
3489 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003490 path->slots[0]++;
3491 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003492 } else if (key.offset >= off + len) {
3493 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003494 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003495 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003496 size = btrfs_item_size_nr(leaf, slot);
3497 read_extent_buffer(leaf, buf,
3498 btrfs_item_ptr_offset(leaf, slot),
3499 size);
3500
3501 btrfs_release_path(path);
3502 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003503
Zheng Yan31840ae2008-09-23 13:14:14 -04003504 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08003505 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08003506 if (off <= key.offset)
3507 new_key.offset = key.offset + destoff - off;
3508 else
3509 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003510
Sage Weilb6f34092011-09-20 14:48:51 -04003511 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003512 * Deal with a hole that doesn't have an extent item
3513 * that represents it (NO_HOLES feature enabled).
3514 * This hole is either in the middle of the cloning
3515 * range or at the beginning (fully overlaps it or
3516 * partially overlaps it).
3517 */
3518 if (new_key.offset != last_dest_end)
3519 drop_start = last_dest_end;
3520 else
3521 drop_start = new_key.offset;
3522
3523 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003524 * 1 - adjusting old extent (we may have to split it)
3525 * 1 - add new extent
3526 * 1 - inode update
3527 */
3528 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003529 if (IS_ERR(trans)) {
3530 ret = PTR_ERR(trans);
3531 goto out;
3532 }
3533
Chris Masonc8a894d2009-06-27 21:07:03 -04003534 if (type == BTRFS_FILE_EXTENT_REG ||
3535 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003536 /*
3537 * a | --- range to clone ---| b
3538 * | ------------- extent ------------- |
3539 */
3540
Antonio Ospite93915582014-06-04 14:03:48 +02003541 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003542 if (key.offset + datal > off + len)
3543 datal = off + len - key.offset;
3544
Antonio Ospite93915582014-06-04 14:03:48 +02003545 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003546 if (off > key.offset) {
3547 datao += off - key.offset;
3548 datal -= off - key.offset;
3549 }
3550
Josef Bacik5dc562c2012-08-17 13:14:17 -04003551 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003552 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003553 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003554 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003555 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003556 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003557 btrfs_abort_transaction(trans,
3558 root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003559 btrfs_end_transaction(trans, root);
3560 goto out;
3561 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003562
Sage Weilc5c9cd42008-11-12 14:32:25 -05003563 ret = btrfs_insert_empty_item(trans, root, path,
3564 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003565 if (ret) {
3566 btrfs_abort_transaction(trans, root,
3567 ret);
3568 btrfs_end_transaction(trans, root);
3569 goto out;
3570 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003571
3572 leaf = path->nodes[0];
3573 slot = path->slots[0];
3574 write_extent_buffer(leaf, buf,
3575 btrfs_item_ptr_offset(leaf, slot),
3576 size);
3577
3578 extent = btrfs_item_ptr(leaf, slot,
3579 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003580
Sage Weilc5c9cd42008-11-12 14:32:25 -05003581 /* disko == 0 means it's a hole */
3582 if (!disko)
3583 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003584
3585 btrfs_set_file_extent_offset(leaf, extent,
3586 datao);
3587 btrfs_set_file_extent_num_bytes(leaf, extent,
3588 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003589
Sage Weilc5c9cd42008-11-12 14:32:25 -05003590 if (disko) {
3591 inode_add_bytes(inode, datal);
3592 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003593 disko, diskl, 0,
3594 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003595 btrfs_ino(inode),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003596 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003597 if (ret) {
3598 btrfs_abort_transaction(trans,
3599 root,
3600 ret);
3601 btrfs_end_transaction(trans,
3602 root);
3603 goto out;
3604
3605 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003606 }
3607 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3608 u64 skip = 0;
3609 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003610
Sage Weilc5c9cd42008-11-12 14:32:25 -05003611 if (off > key.offset) {
3612 skip = off - key.offset;
3613 new_key.offset += skip;
3614 }
Chris Masond3977122009-01-05 21:25:51 -05003615
Liu Boaa42ffd2012-09-18 03:52:23 -06003616 if (key.offset + datal > off + len)
3617 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003618
Sage Weilc5c9cd42008-11-12 14:32:25 -05003619 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003620 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003621 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003622 goto out;
3623 }
3624 size -= skip + trim;
3625 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003626
Filipe Manana8039d872015-10-13 15:15:00 +01003627 ret = clone_copy_inline_extent(src, inode,
3628 trans, path,
3629 &new_key,
3630 drop_start,
3631 datal,
3632 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003633 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003634 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003635 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003636 root,
3637 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003638 btrfs_end_transaction(trans, root);
3639 goto out;
3640 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003641 leaf = path->nodes[0];
3642 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003643 }
3644
Filipe Manana7ffbb592014-06-09 03:48:05 +01003645 /* If we have an implicit hole (NO_HOLES feature). */
3646 if (drop_start < new_key.offset)
3647 clone_update_extent_map(inode, trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003648 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003649 new_key.offset - drop_start);
3650
Filipe Manana14f59792014-06-29 21:45:40 +01003651 clone_update_extent_map(inode, trans, path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003652
Sage Weilc5c9cd42008-11-12 14:32:25 -05003653 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003654 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003655
Filipe Manana62e23902014-08-08 02:47:06 +01003656 last_dest_end = ALIGN(new_key.offset + datal,
3657 root->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003658 ret = clone_finish_inode_update(trans, inode,
3659 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003660 destoff, olen,
3661 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003662 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003663 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003664 if (new_key.offset + datal >= destoff + len)
3665 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003666 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003667 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003668 key.offset = next_key_min_offset;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003669 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003670 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003671
Filipe Mananaf82a9902014-06-01 01:50:28 +01003672 if (last_dest_end < destoff + len) {
3673 /*
3674 * We have an implicit hole (NO_HOLES feature is enabled) that
3675 * fully or partially overlaps our cloning range at its end.
3676 */
3677 btrfs_release_path(path);
3678
3679 /*
3680 * 1 - remove extent(s)
3681 * 1 - inode update
3682 */
3683 trans = btrfs_start_transaction(root, 2);
3684 if (IS_ERR(trans)) {
3685 ret = PTR_ERR(trans);
3686 goto out;
3687 }
3688 ret = btrfs_drop_extents(trans, root, inode,
3689 last_dest_end, destoff + len, 1);
3690 if (ret) {
3691 if (ret != -EOPNOTSUPP)
3692 btrfs_abort_transaction(trans, root, ret);
3693 btrfs_end_transaction(trans, root);
3694 goto out;
3695 }
Filipe Manana14f59792014-06-29 21:45:40 +01003696 clone_update_extent_map(inode, trans, NULL, last_dest_end,
3697 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003698 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003699 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003700 }
3701
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003702out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003703 btrfs_free_path(path);
3704 vfree(buf);
3705 return ret;
3706}
3707
Zach Brown3db11b22015-11-10 16:53:32 -05003708static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3709 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003710{
Al Viro54563d42013-09-01 15:57:51 -04003711 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003712 struct inode *src = file_inode(file_src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003713 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003714 int ret;
3715 u64 len = olen;
3716 u64 bs = root->fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003717 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003718
3719 /*
3720 * TODO:
3721 * - split compressed inline extents. annoying: we need to
3722 * decompress into destination's address_space (the file offset
3723 * may change, so source mapping won't do), then recompress (or
3724 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003725 *
3726 * - split destination inode's inline extents. The inline extents can
3727 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003728 */
3729
Mark Fasheh32b7c682013-08-06 11:42:49 -07003730 if (btrfs_root_readonly(root))
3731 return -EROFS;
3732
Zach Brown3db11b22015-11-10 16:53:32 -05003733 if (file_src->f_path.mnt != file->f_path.mnt ||
3734 src->i_sb != inode->i_sb)
3735 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003736
3737 /* don't make the dst file partly checksummed */
3738 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3739 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003740 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003741
Mark Fasheh32b7c682013-08-06 11:42:49 -07003742 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003743 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003744
3745 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003746 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003747 } else {
Al Viro59551022016-01-22 15:40:57 -05003748 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003749 }
3750
3751 /* determine range to clone */
3752 ret = -EINVAL;
3753 if (off + len > src->i_size || off + len < off)
3754 goto out_unlock;
3755 if (len == 0)
3756 olen = len = src->i_size - off;
3757 /* if we extend to eof, continue to block boundary */
3758 if (off + len == src->i_size)
3759 len = ALIGN(src->i_size, bs) - off;
3760
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003761 if (len == 0) {
3762 ret = 0;
3763 goto out_unlock;
3764 }
3765
Mark Fasheh32b7c682013-08-06 11:42:49 -07003766 /* verify the end result is block aligned */
3767 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3768 !IS_ALIGNED(destoff, bs))
3769 goto out_unlock;
3770
3771 /* verify if ranges are overlapped within the same file */
3772 if (same_inode) {
3773 if (destoff + len > off && destoff < off + len)
3774 goto out_unlock;
3775 }
3776
3777 if (destoff > inode->i_size) {
3778 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3779 if (ret)
3780 goto out_unlock;
3781 }
3782
Filipe Mananac125b8b2014-05-23 05:03:34 +01003783 /*
3784 * Lock the target range too. Right after we replace the file extent
3785 * items in the fs tree (which now point to the cloned data), we might
3786 * have a worker replace them with extent items relative to a write
3787 * operation that was issued before this clone operation (i.e. confront
3788 * with inode.c:btrfs_finish_ordered_io).
3789 */
3790 if (same_inode) {
3791 u64 lock_start = min_t(u64, off, destoff);
3792 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003793
Filipe Mananac125b8b2014-05-23 05:03:34 +01003794 lock_extent_range(src, lock_start, lock_len);
3795 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003796 btrfs_double_extent_lock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003797 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003798
Mark Fasheh1c919a52015-06-30 14:42:08 -07003799 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003800
Filipe Mananac125b8b2014-05-23 05:03:34 +01003801 if (same_inode) {
3802 u64 lock_start = min_t(u64, off, destoff);
3803 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3804
3805 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3806 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003807 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003808 }
3809 /*
3810 * Truncate page cache pages so that future reads will see the cloned
3811 * data immediately and not the previous data.
3812 */
3813 truncate_inode_pages_range(&inode->i_data, destoff,
3814 PAGE_CACHE_ALIGN(destoff + len) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003815out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003816 if (!same_inode)
3817 btrfs_double_inode_unlock(src, inode);
3818 else
Al Viro59551022016-01-22 15:40:57 -05003819 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003820 return ret;
3821}
3822
3823ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
3824 struct file *file_out, loff_t pos_out,
3825 size_t len, unsigned int flags)
3826{
3827 ssize_t ret;
3828
3829 ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
3830 if (ret == 0)
3831 ret = len;
3832 return ret;
3833}
3834
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003835int btrfs_clone_file_range(struct file *src_file, loff_t off,
3836 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003837{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003838 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003839}
3840
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003841/*
3842 * there are many ways the trans_start and trans_end ioctls can lead
3843 * to deadlocks. They should only be used by applications that
3844 * basically own the machine, and have a very in depth understanding
3845 * of all the possible deadlocks and enospc problems.
3846 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003847static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003848{
Al Viro496ad9a2013-01-23 17:07:38 -05003849 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003850 struct btrfs_root *root = BTRFS_I(inode)->root;
3851 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003852 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003853
Sage Weil1ab86ae2009-09-29 18:38:44 -04003854 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003855 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003856 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003857
3858 ret = -EINPROGRESS;
3859 if (file->private_data)
3860 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003861
Li Zefanb83cc962010-12-20 16:04:08 +08003862 ret = -EROFS;
3863 if (btrfs_root_readonly(root))
3864 goto out;
3865
Al Viroa561be72011-11-23 11:57:51 -05003866 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003867 if (ret)
3868 goto out;
3869
Josef Bacika4abeea2011-04-11 17:25:13 -04003870 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003871
Sage Weil1ab86ae2009-09-29 18:38:44 -04003872 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003873 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003874 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003875 goto out_drop;
3876
3877 file->private_data = trans;
3878 return 0;
3879
3880out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003881 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003882 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003883out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003884 return ret;
3885}
3886
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003887static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3888{
Al Viro496ad9a2013-01-23 17:07:38 -05003889 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003890 struct btrfs_root *root = BTRFS_I(inode)->root;
3891 struct btrfs_root *new_root;
3892 struct btrfs_dir_item *di;
3893 struct btrfs_trans_handle *trans;
3894 struct btrfs_path *path;
3895 struct btrfs_key location;
3896 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003897 u64 objectid = 0;
3898 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003899 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003900
3901 if (!capable(CAP_SYS_ADMIN))
3902 return -EPERM;
3903
Miao Xie3c04ce02012-11-26 08:43:07 +00003904 ret = mnt_want_write_file(file);
3905 if (ret)
3906 return ret;
3907
3908 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3909 ret = -EFAULT;
3910 goto out;
3911 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003912
3913 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303914 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003915
3916 location.objectid = objectid;
3917 location.type = BTRFS_ROOT_ITEM_KEY;
3918 location.offset = (u64)-1;
3919
3920 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003921 if (IS_ERR(new_root)) {
3922 ret = PTR_ERR(new_root);
3923 goto out;
3924 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003925
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003926 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003927 if (!path) {
3928 ret = -ENOMEM;
3929 goto out;
3930 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003931 path->leave_spinning = 1;
3932
3933 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003934 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003935 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003936 ret = PTR_ERR(trans);
3937 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003938 }
3939
David Sterba6c417612011-04-13 15:41:04 +02003940 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003941 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3942 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003943 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003944 btrfs_free_path(path);
3945 btrfs_end_transaction(trans, root);
Frank Holtonefe120a2013-12-20 11:37:06 -05003946 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3947 "item, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003948 ret = -ENOENT;
3949 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003950 }
3951
3952 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3953 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3954 btrfs_mark_buffer_dirty(path->nodes[0]);
3955 btrfs_free_path(path);
3956
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003957 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003958 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003959out:
3960 mnt_drop_write_file(file);
3961 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003962}
3963
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003964void btrfs_get_block_group_info(struct list_head *groups_list,
3965 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003966{
3967 struct btrfs_block_group_cache *block_group;
3968
3969 space->total_bytes = 0;
3970 space->used_bytes = 0;
3971 space->flags = 0;
3972 list_for_each_entry(block_group, groups_list, list) {
3973 space->flags = block_group->flags;
3974 space->total_bytes += block_group->key.offset;
3975 space->used_bytes +=
3976 btrfs_block_group_used(&block_group->item);
3977 }
3978}
3979
Eric Sandeen48a3b632013-04-25 20:41:01 +00003980static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003981{
3982 struct btrfs_ioctl_space_args space_args;
3983 struct btrfs_ioctl_space_info space;
3984 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003985 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003986 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003987 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003988 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3989 BTRFS_BLOCK_GROUP_SYSTEM,
3990 BTRFS_BLOCK_GROUP_METADATA,
3991 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3992 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003993 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003994 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003995 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003996 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003997
3998 if (copy_from_user(&space_args,
3999 (struct btrfs_ioctl_space_args __user *)arg,
4000 sizeof(space_args)))
4001 return -EFAULT;
4002
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004003 for (i = 0; i < num_types; i++) {
4004 struct btrfs_space_info *tmp;
4005
4006 info = NULL;
4007 rcu_read_lock();
4008 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4009 list) {
4010 if (tmp->flags == types[i]) {
4011 info = tmp;
4012 break;
4013 }
4014 }
4015 rcu_read_unlock();
4016
4017 if (!info)
4018 continue;
4019
4020 down_read(&info->groups_sem);
4021 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4022 if (!list_empty(&info->block_groups[c]))
4023 slot_count++;
4024 }
4025 up_read(&info->groups_sem);
4026 }
Josef Bacik1406e432010-01-13 18:19:06 +00004027
David Sterba36523e952014-02-07 14:34:12 +01004028 /*
4029 * Global block reserve, exported as a space_info
4030 */
4031 slot_count++;
4032
Chris Mason7fde62b2010-03-16 15:40:10 -04004033 /* space_slots == 0 means they are asking for a count */
4034 if (space_args.space_slots == 0) {
4035 space_args.total_spaces = slot_count;
4036 goto out;
4037 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004038
Dan Rosenberg51788b12011-02-14 16:04:23 -05004039 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004040
Chris Mason7fde62b2010-03-16 15:40:10 -04004041 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004042
Chris Mason7fde62b2010-03-16 15:40:10 -04004043 /* we generally have at most 6 or so space infos, one for each raid
4044 * level. So, a whole page should be more than enough for everyone
4045 */
4046 if (alloc_size > PAGE_CACHE_SIZE)
4047 return -ENOMEM;
4048
4049 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004050 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004051 if (!dest)
4052 return -ENOMEM;
4053 dest_orig = dest;
4054
4055 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004056 for (i = 0; i < num_types; i++) {
4057 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004058
Dan Rosenberg51788b12011-02-14 16:04:23 -05004059 if (!slot_count)
4060 break;
4061
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004062 info = NULL;
4063 rcu_read_lock();
4064 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4065 list) {
4066 if (tmp->flags == types[i]) {
4067 info = tmp;
4068 break;
4069 }
4070 }
4071 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004072
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004073 if (!info)
4074 continue;
4075 down_read(&info->groups_sem);
4076 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4077 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004078 btrfs_get_block_group_info(
4079 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004080 memcpy(dest, &space, sizeof(space));
4081 dest++;
4082 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004083 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004084 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004085 if (!slot_count)
4086 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004087 }
4088 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004089 }
Josef Bacik1406e432010-01-13 18:19:06 +00004090
David Sterba36523e952014-02-07 14:34:12 +01004091 /*
4092 * Add global block reserve
4093 */
4094 if (slot_count) {
4095 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
4096
4097 spin_lock(&block_rsv->lock);
4098 space.total_bytes = block_rsv->size;
4099 space.used_bytes = block_rsv->size - block_rsv->reserved;
4100 spin_unlock(&block_rsv->lock);
4101 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4102 memcpy(dest, &space, sizeof(space));
4103 space_args.total_spaces++;
4104 }
4105
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004106 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004107 (arg + sizeof(struct btrfs_ioctl_space_args));
4108
4109 if (copy_to_user(user_dest, dest_orig, alloc_size))
4110 ret = -EFAULT;
4111
4112 kfree(dest_orig);
4113out:
4114 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004115 ret = -EFAULT;
4116
4117 return ret;
4118}
4119
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004120/*
4121 * there are many ways the trans_start and trans_end ioctls can lead
4122 * to deadlocks. They should only be used by applications that
4123 * basically own the machine, and have a very in depth understanding
4124 * of all the possible deadlocks and enospc problems.
4125 */
4126long btrfs_ioctl_trans_end(struct file *file)
4127{
Al Viro496ad9a2013-01-23 17:07:38 -05004128 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004129 struct btrfs_root *root = BTRFS_I(inode)->root;
4130 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004131
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004132 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004133 if (!trans)
4134 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004135 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004136
Sage Weil1ab86ae2009-09-29 18:38:44 -04004137 btrfs_end_transaction(trans, root);
4138
Josef Bacika4abeea2011-04-11 17:25:13 -04004139 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004140
Al Viro2a79f172011-12-09 08:06:57 -05004141 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004142 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004143}
4144
Miao Xie9a8c28b2012-11-26 08:40:43 +00004145static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4146 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004147{
Sage Weil46204592010-10-29 15:41:32 -04004148 struct btrfs_trans_handle *trans;
4149 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004150 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004151
Miao Xied4edf392013-02-20 09:17:06 +00004152 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004153 if (IS_ERR(trans)) {
4154 if (PTR_ERR(trans) != -ENOENT)
4155 return PTR_ERR(trans);
4156
4157 /* No running transaction, don't bother */
4158 transid = root->fs_info->last_trans_committed;
4159 goto out;
4160 }
Sage Weil46204592010-10-29 15:41:32 -04004161 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004162 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004163 if (ret) {
4164 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004165 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004166 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004167out:
Sage Weil46204592010-10-29 15:41:32 -04004168 if (argp)
4169 if (copy_to_user(argp, &transid, sizeof(transid)))
4170 return -EFAULT;
4171 return 0;
4172}
4173
Miao Xie9a8c28b2012-11-26 08:40:43 +00004174static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4175 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004176{
Sage Weil46204592010-10-29 15:41:32 -04004177 u64 transid;
4178
4179 if (argp) {
4180 if (copy_from_user(&transid, argp, sizeof(transid)))
4181 return -EFAULT;
4182 } else {
4183 transid = 0; /* current trans */
4184 }
4185 return btrfs_wait_for_commit(root, transid);
4186}
4187
Miao Xieb8e95482012-11-26 08:48:01 +00004188static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004189{
Al Viro496ad9a2013-01-23 17:07:38 -05004190 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01004191 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004192 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004193
4194 if (!capable(CAP_SYS_ADMIN))
4195 return -EPERM;
4196
4197 sa = memdup_user(arg, sizeof(*sa));
4198 if (IS_ERR(sa))
4199 return PTR_ERR(sa);
4200
Miao Xieb8e95482012-11-26 08:48:01 +00004201 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4202 ret = mnt_want_write_file(file);
4203 if (ret)
4204 goto out;
4205 }
4206
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004207 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004208 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4209 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004210
4211 if (copy_to_user(arg, sa, sizeof(*sa)))
4212 ret = -EFAULT;
4213
Miao Xieb8e95482012-11-26 08:48:01 +00004214 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4215 mnt_drop_write_file(file);
4216out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004217 kfree(sa);
4218 return ret;
4219}
4220
4221static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4222{
4223 if (!capable(CAP_SYS_ADMIN))
4224 return -EPERM;
4225
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004226 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004227}
4228
4229static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4230 void __user *arg)
4231{
4232 struct btrfs_ioctl_scrub_args *sa;
4233 int ret;
4234
4235 if (!capable(CAP_SYS_ADMIN))
4236 return -EPERM;
4237
4238 sa = memdup_user(arg, sizeof(*sa));
4239 if (IS_ERR(sa))
4240 return PTR_ERR(sa);
4241
4242 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4243
4244 if (copy_to_user(arg, sa, sizeof(*sa)))
4245 ret = -EFAULT;
4246
4247 kfree(sa);
4248 return ret;
4249}
4250
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004251static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004252 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004253{
4254 struct btrfs_ioctl_get_dev_stats *sa;
4255 int ret;
4256
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004257 sa = memdup_user(arg, sizeof(*sa));
4258 if (IS_ERR(sa))
4259 return PTR_ERR(sa);
4260
David Sterbab27f7c02012-06-22 06:30:39 -06004261 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4262 kfree(sa);
4263 return -EPERM;
4264 }
4265
4266 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004267
4268 if (copy_to_user(arg, sa, sizeof(*sa)))
4269 ret = -EFAULT;
4270
4271 kfree(sa);
4272 return ret;
4273}
4274
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004275static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4276{
4277 struct btrfs_ioctl_dev_replace_args *p;
4278 int ret;
4279
4280 if (!capable(CAP_SYS_ADMIN))
4281 return -EPERM;
4282
4283 p = memdup_user(arg, sizeof(*p));
4284 if (IS_ERR(p))
4285 return PTR_ERR(p);
4286
4287 switch (p->cmd) {
4288 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004289 if (root->fs_info->sb->s_flags & MS_RDONLY) {
4290 ret = -EROFS;
4291 goto out;
4292 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004293 if (atomic_xchg(
4294 &root->fs_info->mutually_exclusive_operation_running,
4295 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004296 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004297 } else {
4298 ret = btrfs_dev_replace_start(root, p);
4299 atomic_set(
4300 &root->fs_info->mutually_exclusive_operation_running,
4301 0);
4302 }
4303 break;
4304 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4305 btrfs_dev_replace_status(root->fs_info, p);
4306 ret = 0;
4307 break;
4308 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4309 ret = btrfs_dev_replace_cancel(root->fs_info, p);
4310 break;
4311 default:
4312 ret = -EINVAL;
4313 break;
4314 }
4315
4316 if (copy_to_user(arg, p, sizeof(*p)))
4317 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004318out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004319 kfree(p);
4320 return ret;
4321}
4322
Jan Schmidtd7728c92011-07-07 16:48:38 +02004323static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4324{
4325 int ret = 0;
4326 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004327 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004328 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004329 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004330 struct inode_fs_paths *ipath = NULL;
4331 struct btrfs_path *path;
4332
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004333 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004334 return -EPERM;
4335
4336 path = btrfs_alloc_path();
4337 if (!path) {
4338 ret = -ENOMEM;
4339 goto out;
4340 }
4341
4342 ipa = memdup_user(arg, sizeof(*ipa));
4343 if (IS_ERR(ipa)) {
4344 ret = PTR_ERR(ipa);
4345 ipa = NULL;
4346 goto out;
4347 }
4348
4349 size = min_t(u32, ipa->size, 4096);
4350 ipath = init_ipath(size, root, path);
4351 if (IS_ERR(ipath)) {
4352 ret = PTR_ERR(ipath);
4353 ipath = NULL;
4354 goto out;
4355 }
4356
4357 ret = paths_from_inode(ipa->inum, ipath);
4358 if (ret < 0)
4359 goto out;
4360
4361 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004362 rel_ptr = ipath->fspath->val[i] -
4363 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004364 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004365 }
4366
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004367 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4368 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004369 if (ret) {
4370 ret = -EFAULT;
4371 goto out;
4372 }
4373
4374out:
4375 btrfs_free_path(path);
4376 free_ipath(ipath);
4377 kfree(ipa);
4378
4379 return ret;
4380}
4381
4382static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4383{
4384 struct btrfs_data_container *inodes = ctx;
4385 const size_t c = 3 * sizeof(u64);
4386
4387 if (inodes->bytes_left >= c) {
4388 inodes->bytes_left -= c;
4389 inodes->val[inodes->elem_cnt] = inum;
4390 inodes->val[inodes->elem_cnt + 1] = offset;
4391 inodes->val[inodes->elem_cnt + 2] = root;
4392 inodes->elem_cnt += 3;
4393 } else {
4394 inodes->bytes_missing += c - inodes->bytes_left;
4395 inodes->bytes_left = 0;
4396 inodes->elem_missed += 3;
4397 }
4398
4399 return 0;
4400}
4401
4402static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4403 void __user *arg)
4404{
4405 int ret = 0;
4406 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004407 struct btrfs_ioctl_logical_ino_args *loi;
4408 struct btrfs_data_container *inodes = NULL;
4409 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004410
4411 if (!capable(CAP_SYS_ADMIN))
4412 return -EPERM;
4413
4414 loi = memdup_user(arg, sizeof(*loi));
4415 if (IS_ERR(loi)) {
4416 ret = PTR_ERR(loi);
4417 loi = NULL;
4418 goto out;
4419 }
4420
4421 path = btrfs_alloc_path();
4422 if (!path) {
4423 ret = -ENOMEM;
4424 goto out;
4425 }
4426
Byongho Leeee221842015-12-15 01:42:10 +09004427 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004428 inodes = init_data_container(size);
4429 if (IS_ERR(inodes)) {
4430 ret = PTR_ERR(inodes);
4431 inodes = NULL;
4432 goto out;
4433 }
4434
Liu Bodf031f02012-09-07 20:01:29 -06004435 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4436 build_ino_list, inodes);
4437 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004438 ret = -ENOENT;
4439 if (ret < 0)
4440 goto out;
4441
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004442 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4443 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004444 if (ret)
4445 ret = -EFAULT;
4446
4447out:
4448 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06004449 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004450 kfree(loi);
4451
4452 return ret;
4453}
4454
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004455void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004456 struct btrfs_ioctl_balance_args *bargs)
4457{
4458 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4459
4460 bargs->flags = bctl->flags;
4461
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004462 if (atomic_read(&fs_info->balance_running))
4463 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4464 if (atomic_read(&fs_info->balance_pause_req))
4465 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004466 if (atomic_read(&fs_info->balance_cancel_req))
4467 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004468
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004469 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4470 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4471 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004472
4473 if (lock) {
4474 spin_lock(&fs_info->balance_lock);
4475 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4476 spin_unlock(&fs_info->balance_lock);
4477 } else {
4478 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4479 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004480}
4481
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004482static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004483{
Al Viro496ad9a2013-01-23 17:07:38 -05004484 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004485 struct btrfs_fs_info *fs_info = root->fs_info;
4486 struct btrfs_ioctl_balance_args *bargs;
4487 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004488 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004489 int ret;
4490
4491 if (!capable(CAP_SYS_ADMIN))
4492 return -EPERM;
4493
Liu Boe54bfa32012-06-29 03:58:48 -06004494 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004495 if (ret)
4496 return ret;
4497
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004498again:
4499 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4500 mutex_lock(&fs_info->volume_mutex);
4501 mutex_lock(&fs_info->balance_mutex);
4502 need_unlock = true;
4503 goto locked;
4504 }
4505
4506 /*
4507 * mut. excl. ops lock is locked. Three possibilites:
4508 * (1) some other op is running
4509 * (2) balance is running
4510 * (3) balance is paused -- special case (think resume)
4511 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004512 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004513 if (fs_info->balance_ctl) {
4514 /* this is either (2) or (3) */
4515 if (!atomic_read(&fs_info->balance_running)) {
4516 mutex_unlock(&fs_info->balance_mutex);
4517 if (!mutex_trylock(&fs_info->volume_mutex))
4518 goto again;
4519 mutex_lock(&fs_info->balance_mutex);
4520
4521 if (fs_info->balance_ctl &&
4522 !atomic_read(&fs_info->balance_running)) {
4523 /* this is (3) */
4524 need_unlock = false;
4525 goto locked;
4526 }
4527
4528 mutex_unlock(&fs_info->balance_mutex);
4529 mutex_unlock(&fs_info->volume_mutex);
4530 goto again;
4531 } else {
4532 /* this is (2) */
4533 mutex_unlock(&fs_info->balance_mutex);
4534 ret = -EINPROGRESS;
4535 goto out;
4536 }
4537 } else {
4538 /* this is (1) */
4539 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004540 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004541 goto out;
4542 }
4543
4544locked:
4545 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004546
4547 if (arg) {
4548 bargs = memdup_user(arg, sizeof(*bargs));
4549 if (IS_ERR(bargs)) {
4550 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004551 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004552 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004553
4554 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4555 if (!fs_info->balance_ctl) {
4556 ret = -ENOTCONN;
4557 goto out_bargs;
4558 }
4559
4560 bctl = fs_info->balance_ctl;
4561 spin_lock(&fs_info->balance_lock);
4562 bctl->flags |= BTRFS_BALANCE_RESUME;
4563 spin_unlock(&fs_info->balance_lock);
4564
4565 goto do_balance;
4566 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004567 } else {
4568 bargs = NULL;
4569 }
4570
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004571 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004572 ret = -EINPROGRESS;
4573 goto out_bargs;
4574 }
4575
David Sterba8d2db782015-11-04 15:38:29 +01004576 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004577 if (!bctl) {
4578 ret = -ENOMEM;
4579 goto out_bargs;
4580 }
4581
4582 bctl->fs_info = fs_info;
4583 if (arg) {
4584 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4585 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4586 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4587
4588 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004589 } else {
4590 /* balance everything - no filters */
4591 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004592 }
4593
David Sterba8eb93452015-10-12 16:55:54 +02004594 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4595 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004596 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004597 }
4598
Ilya Dryomovde322262012-01-16 22:04:49 +02004599do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004600 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004601 * Ownership of bctl and mutually_exclusive_operation_running
4602 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4603 * or, if restriper was paused all the way until unmount, in
4604 * free_fs_info. mutually_exclusive_operation_running is
4605 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004606 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004607 need_unlock = false;
4608
4609 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004610 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004611
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004612 if (arg) {
4613 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4614 ret = -EFAULT;
4615 }
4616
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004617out_bctl:
4618 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004619out_bargs:
4620 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004621out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004622 mutex_unlock(&fs_info->balance_mutex);
4623 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004624 if (need_unlock)
4625 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4626out:
Liu Boe54bfa32012-06-29 03:58:48 -06004627 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004628 return ret;
4629}
4630
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004631static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4632{
4633 if (!capable(CAP_SYS_ADMIN))
4634 return -EPERM;
4635
4636 switch (cmd) {
4637 case BTRFS_BALANCE_CTL_PAUSE:
4638 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004639 case BTRFS_BALANCE_CTL_CANCEL:
4640 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004641 }
4642
4643 return -EINVAL;
4644}
4645
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004646static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4647 void __user *arg)
4648{
4649 struct btrfs_fs_info *fs_info = root->fs_info;
4650 struct btrfs_ioctl_balance_args *bargs;
4651 int ret = 0;
4652
4653 if (!capable(CAP_SYS_ADMIN))
4654 return -EPERM;
4655
4656 mutex_lock(&fs_info->balance_mutex);
4657 if (!fs_info->balance_ctl) {
4658 ret = -ENOTCONN;
4659 goto out;
4660 }
4661
David Sterba8d2db782015-11-04 15:38:29 +01004662 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004663 if (!bargs) {
4664 ret = -ENOMEM;
4665 goto out;
4666 }
4667
4668 update_ioctl_balance_args(fs_info, 1, bargs);
4669
4670 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4671 ret = -EFAULT;
4672
4673 kfree(bargs);
4674out:
4675 mutex_unlock(&fs_info->balance_mutex);
4676 return ret;
4677}
4678
Miao Xie905b0dd2012-11-26 08:50:11 +00004679static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004680{
Al Viro496ad9a2013-01-23 17:07:38 -05004681 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004682 struct btrfs_ioctl_quota_ctl_args *sa;
4683 struct btrfs_trans_handle *trans = NULL;
4684 int ret;
4685 int err;
4686
4687 if (!capable(CAP_SYS_ADMIN))
4688 return -EPERM;
4689
Miao Xie905b0dd2012-11-26 08:50:11 +00004690 ret = mnt_want_write_file(file);
4691 if (ret)
4692 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004693
4694 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004695 if (IS_ERR(sa)) {
4696 ret = PTR_ERR(sa);
4697 goto drop_write;
4698 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004699
Wang Shilong7708f022013-04-07 10:24:57 +00004700 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004701 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4702 if (IS_ERR(trans)) {
4703 ret = PTR_ERR(trans);
4704 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004705 }
4706
4707 switch (sa->cmd) {
4708 case BTRFS_QUOTA_CTL_ENABLE:
4709 ret = btrfs_quota_enable(trans, root->fs_info);
4710 break;
4711 case BTRFS_QUOTA_CTL_DISABLE:
4712 ret = btrfs_quota_disable(trans, root->fs_info);
4713 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004714 default:
4715 ret = -EINVAL;
4716 break;
4717 }
4718
Jan Schmidt2f232032013-04-25 16:04:51 +00004719 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4720 if (err && !ret)
4721 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004722out:
4723 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004724 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004725drop_write:
4726 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004727 return ret;
4728}
4729
Miao Xie905b0dd2012-11-26 08:50:11 +00004730static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004731{
Al Viro496ad9a2013-01-23 17:07:38 -05004732 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004733 struct btrfs_ioctl_qgroup_assign_args *sa;
4734 struct btrfs_trans_handle *trans;
4735 int ret;
4736 int err;
4737
4738 if (!capable(CAP_SYS_ADMIN))
4739 return -EPERM;
4740
Miao Xie905b0dd2012-11-26 08:50:11 +00004741 ret = mnt_want_write_file(file);
4742 if (ret)
4743 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004744
4745 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004746 if (IS_ERR(sa)) {
4747 ret = PTR_ERR(sa);
4748 goto drop_write;
4749 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004750
4751 trans = btrfs_join_transaction(root);
4752 if (IS_ERR(trans)) {
4753 ret = PTR_ERR(trans);
4754 goto out;
4755 }
4756
4757 /* FIXME: check if the IDs really exist */
4758 if (sa->assign) {
4759 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4760 sa->src, sa->dst);
4761 } else {
4762 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4763 sa->src, sa->dst);
4764 }
4765
Qu Wenruoe082f562015-02-27 16:24:28 +08004766 /* update qgroup status and info */
4767 err = btrfs_run_qgroups(trans, root->fs_info);
4768 if (err < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08004769 btrfs_std_error(root->fs_info, ret,
Qu Wenruoe082f562015-02-27 16:24:28 +08004770 "failed to update qgroup status and info\n");
Arne Jansen5d13a372011-09-14 15:53:51 +02004771 err = btrfs_end_transaction(trans, root);
4772 if (err && !ret)
4773 ret = err;
4774
4775out:
4776 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004777drop_write:
4778 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004779 return ret;
4780}
4781
Miao Xie905b0dd2012-11-26 08:50:11 +00004782static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004783{
Al Viro496ad9a2013-01-23 17:07:38 -05004784 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004785 struct btrfs_ioctl_qgroup_create_args *sa;
4786 struct btrfs_trans_handle *trans;
4787 int ret;
4788 int err;
4789
4790 if (!capable(CAP_SYS_ADMIN))
4791 return -EPERM;
4792
Miao Xie905b0dd2012-11-26 08:50:11 +00004793 ret = mnt_want_write_file(file);
4794 if (ret)
4795 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004796
4797 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004798 if (IS_ERR(sa)) {
4799 ret = PTR_ERR(sa);
4800 goto drop_write;
4801 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004802
Miao Xied86e56c2012-11-15 11:35:41 +00004803 if (!sa->qgroupid) {
4804 ret = -EINVAL;
4805 goto out;
4806 }
4807
Arne Jansen5d13a372011-09-14 15:53:51 +02004808 trans = btrfs_join_transaction(root);
4809 if (IS_ERR(trans)) {
4810 ret = PTR_ERR(trans);
4811 goto out;
4812 }
4813
4814 /* FIXME: check if the IDs really exist */
4815 if (sa->create) {
Dongsheng Yang4087cf22015-01-18 10:59:23 -05004816 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004817 } else {
4818 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4819 }
4820
4821 err = btrfs_end_transaction(trans, root);
4822 if (err && !ret)
4823 ret = err;
4824
4825out:
4826 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004827drop_write:
4828 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004829 return ret;
4830}
4831
Miao Xie905b0dd2012-11-26 08:50:11 +00004832static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004833{
Al Viro496ad9a2013-01-23 17:07:38 -05004834 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004835 struct btrfs_ioctl_qgroup_limit_args *sa;
4836 struct btrfs_trans_handle *trans;
4837 int ret;
4838 int err;
4839 u64 qgroupid;
4840
4841 if (!capable(CAP_SYS_ADMIN))
4842 return -EPERM;
4843
Miao Xie905b0dd2012-11-26 08:50:11 +00004844 ret = mnt_want_write_file(file);
4845 if (ret)
4846 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004847
4848 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004849 if (IS_ERR(sa)) {
4850 ret = PTR_ERR(sa);
4851 goto drop_write;
4852 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004853
4854 trans = btrfs_join_transaction(root);
4855 if (IS_ERR(trans)) {
4856 ret = PTR_ERR(trans);
4857 goto out;
4858 }
4859
4860 qgroupid = sa->qgroupid;
4861 if (!qgroupid) {
4862 /* take the current subvol as qgroup */
4863 qgroupid = root->root_key.objectid;
4864 }
4865
4866 /* FIXME: check if the IDs really exist */
4867 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4868
4869 err = btrfs_end_transaction(trans, root);
4870 if (err && !ret)
4871 ret = err;
4872
4873out:
4874 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004875drop_write:
4876 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004877 return ret;
4878}
4879
Jan Schmidt2f232032013-04-25 16:04:51 +00004880static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4881{
Al Viro6d0379e2013-06-16 19:32:35 +04004882 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004883 struct btrfs_ioctl_quota_rescan_args *qsa;
4884 int ret;
4885
4886 if (!capable(CAP_SYS_ADMIN))
4887 return -EPERM;
4888
4889 ret = mnt_want_write_file(file);
4890 if (ret)
4891 return ret;
4892
4893 qsa = memdup_user(arg, sizeof(*qsa));
4894 if (IS_ERR(qsa)) {
4895 ret = PTR_ERR(qsa);
4896 goto drop_write;
4897 }
4898
4899 if (qsa->flags) {
4900 ret = -EINVAL;
4901 goto out;
4902 }
4903
4904 ret = btrfs_qgroup_rescan(root->fs_info);
4905
4906out:
4907 kfree(qsa);
4908drop_write:
4909 mnt_drop_write_file(file);
4910 return ret;
4911}
4912
4913static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4914{
Al Viro6d0379e2013-06-16 19:32:35 +04004915 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004916 struct btrfs_ioctl_quota_rescan_args *qsa;
4917 int ret = 0;
4918
4919 if (!capable(CAP_SYS_ADMIN))
4920 return -EPERM;
4921
David Sterba8d2db782015-11-04 15:38:29 +01004922 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004923 if (!qsa)
4924 return -ENOMEM;
4925
4926 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4927 qsa->flags = 1;
4928 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4929 }
4930
4931 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4932 ret = -EFAULT;
4933
4934 kfree(qsa);
4935 return ret;
4936}
4937
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004938static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4939{
Al Viro54563d42013-09-01 15:57:51 -04004940 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004941
4942 if (!capable(CAP_SYS_ADMIN))
4943 return -EPERM;
4944
4945 return btrfs_qgroup_wait_for_completion(root->fs_info);
4946}
4947
Hugo Millsabccd002014-01-30 20:17:00 +00004948static long _btrfs_ioctl_set_received_subvol(struct file *file,
4949 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004950{
Al Viro496ad9a2013-01-23 17:07:38 -05004951 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004952 struct btrfs_root *root = BTRFS_I(inode)->root;
4953 struct btrfs_root_item *root_item = &root->root_item;
4954 struct btrfs_trans_handle *trans;
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004955 struct timespec ct = current_fs_time(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004956 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004957 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004958
David Sterbabd60ea02014-01-16 15:50:22 +01004959 if (!inode_owner_or_capable(inode))
4960 return -EPERM;
4961
Alexander Block8ea05e32012-07-25 17:35:53 +02004962 ret = mnt_want_write_file(file);
4963 if (ret < 0)
4964 return ret;
4965
4966 down_write(&root->fs_info->subvol_sem);
4967
4968 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4969 ret = -EINVAL;
4970 goto out;
4971 }
4972
4973 if (btrfs_root_readonly(root)) {
4974 ret = -EROFS;
4975 goto out;
4976 }
4977
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004978 /*
4979 * 1 - root item
4980 * 2 - uuid items (received uuid + subvol uuid)
4981 */
4982 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004983 if (IS_ERR(trans)) {
4984 ret = PTR_ERR(trans);
4985 trans = NULL;
4986 goto out;
4987 }
4988
4989 sa->rtransid = trans->transid;
4990 sa->rtime.sec = ct.tv_sec;
4991 sa->rtime.nsec = ct.tv_nsec;
4992
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004993 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4994 BTRFS_UUID_SIZE);
4995 if (received_uuid_changed &&
4996 !btrfs_is_empty_uuid(root_item->received_uuid))
4997 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4998 root_item->received_uuid,
4999 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5000 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005001 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5002 btrfs_set_root_stransid(root_item, sa->stransid);
5003 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005004 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5005 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5006 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5007 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005008
5009 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5010 &root->root_key, &root->root_item);
5011 if (ret < 0) {
5012 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02005013 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005014 }
5015 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5016 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
5017 sa->uuid,
5018 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5019 root->root_key.objectid);
5020 if (ret < 0 && ret != -EEXIST) {
5021 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005022 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005023 }
5024 }
5025 ret = btrfs_commit_transaction(trans, root);
5026 if (ret < 0) {
5027 btrfs_abort_transaction(trans, root, ret);
5028 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005029 }
5030
Hugo Millsabccd002014-01-30 20:17:00 +00005031out:
5032 up_write(&root->fs_info->subvol_sem);
5033 mnt_drop_write_file(file);
5034 return ret;
5035}
5036
5037#ifdef CONFIG_64BIT
5038static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5039 void __user *arg)
5040{
5041 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5042 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5043 int ret = 0;
5044
5045 args32 = memdup_user(arg, sizeof(*args32));
5046 if (IS_ERR(args32)) {
5047 ret = PTR_ERR(args32);
5048 args32 = NULL;
5049 goto out;
5050 }
5051
David Sterba8d2db782015-11-04 15:38:29 +01005052 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005053 if (!args64) {
5054 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005055 goto out;
5056 }
5057
5058 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5059 args64->stransid = args32->stransid;
5060 args64->rtransid = args32->rtransid;
5061 args64->stime.sec = args32->stime.sec;
5062 args64->stime.nsec = args32->stime.nsec;
5063 args64->rtime.sec = args32->rtime.sec;
5064 args64->rtime.nsec = args32->rtime.nsec;
5065 args64->flags = args32->flags;
5066
5067 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5068 if (ret)
5069 goto out;
5070
5071 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5072 args32->stransid = args64->stransid;
5073 args32->rtransid = args64->rtransid;
5074 args32->stime.sec = args64->stime.sec;
5075 args32->stime.nsec = args64->stime.nsec;
5076 args32->rtime.sec = args64->rtime.sec;
5077 args32->rtime.nsec = args64->rtime.nsec;
5078 args32->flags = args64->flags;
5079
5080 ret = copy_to_user(arg, args32, sizeof(*args32));
5081 if (ret)
5082 ret = -EFAULT;
5083
5084out:
5085 kfree(args32);
5086 kfree(args64);
5087 return ret;
5088}
5089#endif
5090
5091static long btrfs_ioctl_set_received_subvol(struct file *file,
5092 void __user *arg)
5093{
5094 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5095 int ret = 0;
5096
5097 sa = memdup_user(arg, sizeof(*sa));
5098 if (IS_ERR(sa)) {
5099 ret = PTR_ERR(sa);
5100 sa = NULL;
5101 goto out;
5102 }
5103
5104 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5105
5106 if (ret)
5107 goto out;
5108
Alexander Block8ea05e32012-07-25 17:35:53 +02005109 ret = copy_to_user(arg, sa, sizeof(*sa));
5110 if (ret)
5111 ret = -EFAULT;
5112
5113out:
5114 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005115 return ret;
5116}
5117
jeff.liu867ab662013-01-05 02:48:01 +00005118static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5119{
Al Viro6d0379e2013-06-16 19:32:35 +04005120 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005121 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005122 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005123 char label[BTRFS_LABEL_SIZE];
5124
5125 spin_lock(&root->fs_info->super_lock);
5126 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5127 spin_unlock(&root->fs_info->super_lock);
5128
5129 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005130
5131 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005132 btrfs_warn(root->fs_info,
5133 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00005134 }
5135
jeff.liu867ab662013-01-05 02:48:01 +00005136 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005137
5138 return ret ? -EFAULT : 0;
5139}
5140
jeff.liua8bfd4a2013-01-05 02:48:08 +00005141static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5142{
Al Viro6d0379e2013-06-16 19:32:35 +04005143 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005144 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5145 struct btrfs_trans_handle *trans;
5146 char label[BTRFS_LABEL_SIZE];
5147 int ret;
5148
5149 if (!capable(CAP_SYS_ADMIN))
5150 return -EPERM;
5151
5152 if (copy_from_user(label, arg, sizeof(label)))
5153 return -EFAULT;
5154
5155 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005156 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
jeff.liua8bfd4a2013-01-05 02:48:08 +00005157 BTRFS_LABEL_SIZE - 1);
5158 return -EINVAL;
5159 }
5160
5161 ret = mnt_want_write_file(file);
5162 if (ret)
5163 return ret;
5164
jeff.liua8bfd4a2013-01-05 02:48:08 +00005165 trans = btrfs_start_transaction(root, 0);
5166 if (IS_ERR(trans)) {
5167 ret = PTR_ERR(trans);
5168 goto out_unlock;
5169 }
5170
Anand Jaina1b83ac2013-07-19 17:39:32 +08005171 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005172 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005173 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005174 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005175
5176out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005177 mnt_drop_write_file(file);
5178 return ret;
5179}
5180
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005181#define INIT_FEATURE_FLAGS(suffix) \
5182 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5183 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5184 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5185
5186static int btrfs_ioctl_get_supported_features(struct file *file,
5187 void __user *arg)
5188{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005189 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005190 INIT_FEATURE_FLAGS(SUPP),
5191 INIT_FEATURE_FLAGS(SAFE_SET),
5192 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5193 };
5194
5195 if (copy_to_user(arg, &features, sizeof(features)))
5196 return -EFAULT;
5197
5198 return 0;
5199}
5200
5201static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5202{
5203 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5204 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5205 struct btrfs_ioctl_feature_flags features;
5206
5207 features.compat_flags = btrfs_super_compat_flags(super_block);
5208 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5209 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5210
5211 if (copy_to_user(arg, &features, sizeof(features)))
5212 return -EFAULT;
5213
5214 return 0;
5215}
5216
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005217static int check_feature_bits(struct btrfs_root *root,
5218 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005219 u64 change_mask, u64 flags, u64 supported_flags,
5220 u64 safe_set, u64 safe_clear)
5221{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005222 const char *type = btrfs_feature_set_names[set];
5223 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005224 u64 disallowed, unsupported;
5225 u64 set_mask = flags & change_mask;
5226 u64 clear_mask = ~flags & change_mask;
5227
5228 unsupported = set_mask & ~supported_flags;
5229 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005230 names = btrfs_printable_features(set, unsupported);
5231 if (names) {
5232 btrfs_warn(root->fs_info,
5233 "this kernel does not support the %s feature bit%s",
5234 names, strchr(names, ',') ? "s" : "");
5235 kfree(names);
5236 } else
5237 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005238 "this kernel does not support %s bits 0x%llx",
5239 type, unsupported);
5240 return -EOPNOTSUPP;
5241 }
5242
5243 disallowed = set_mask & ~safe_set;
5244 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005245 names = btrfs_printable_features(set, disallowed);
5246 if (names) {
5247 btrfs_warn(root->fs_info,
5248 "can't set the %s feature bit%s while mounted",
5249 names, strchr(names, ',') ? "s" : "");
5250 kfree(names);
5251 } else
5252 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005253 "can't set %s bits 0x%llx while mounted",
5254 type, disallowed);
5255 return -EPERM;
5256 }
5257
5258 disallowed = clear_mask & ~safe_clear;
5259 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005260 names = btrfs_printable_features(set, disallowed);
5261 if (names) {
5262 btrfs_warn(root->fs_info,
5263 "can't clear the %s feature bit%s while mounted",
5264 names, strchr(names, ',') ? "s" : "");
5265 kfree(names);
5266 } else
5267 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005268 "can't clear %s bits 0x%llx while mounted",
5269 type, disallowed);
5270 return -EPERM;
5271 }
5272
5273 return 0;
5274}
5275
5276#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005277check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005278 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5279 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5280 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5281
5282static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5283{
5284 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5285 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5286 struct btrfs_ioctl_feature_flags flags[2];
5287 struct btrfs_trans_handle *trans;
5288 u64 newflags;
5289 int ret;
5290
5291 if (!capable(CAP_SYS_ADMIN))
5292 return -EPERM;
5293
5294 if (copy_from_user(flags, arg, sizeof(flags)))
5295 return -EFAULT;
5296
5297 /* Nothing to do */
5298 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5299 !flags[0].incompat_flags)
5300 return 0;
5301
5302 ret = check_feature(root, flags[0].compat_flags,
5303 flags[1].compat_flags, COMPAT);
5304 if (ret)
5305 return ret;
5306
5307 ret = check_feature(root, flags[0].compat_ro_flags,
5308 flags[1].compat_ro_flags, COMPAT_RO);
5309 if (ret)
5310 return ret;
5311
5312 ret = check_feature(root, flags[0].incompat_flags,
5313 flags[1].incompat_flags, INCOMPAT);
5314 if (ret)
5315 return ret;
5316
David Sterba8051aa12014-02-07 14:34:04 +01005317 trans = btrfs_start_transaction(root, 0);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005318 if (IS_ERR(trans))
5319 return PTR_ERR(trans);
5320
5321 spin_lock(&root->fs_info->super_lock);
5322 newflags = btrfs_super_compat_flags(super_block);
5323 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5324 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5325 btrfs_set_super_compat_flags(super_block, newflags);
5326
5327 newflags = btrfs_super_compat_ro_flags(super_block);
5328 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5329 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5330 btrfs_set_super_compat_ro_flags(super_block, newflags);
5331
5332 newflags = btrfs_super_incompat_flags(super_block);
5333 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5334 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5335 btrfs_set_super_incompat_flags(super_block, newflags);
5336 spin_unlock(&root->fs_info->super_lock);
5337
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005338 return btrfs_commit_transaction(trans, root);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005339}
5340
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005341long btrfs_ioctl(struct file *file, unsigned int
5342 cmd, unsigned long arg)
5343{
Al Viro496ad9a2013-01-23 17:07:38 -05005344 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005345 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005346
5347 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005348 case FS_IOC_GETFLAGS:
5349 return btrfs_ioctl_getflags(file, argp);
5350 case FS_IOC_SETFLAGS:
5351 return btrfs_ioctl_setflags(file, argp);
5352 case FS_IOC_GETVERSION:
5353 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005354 case FITRIM:
5355 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005356 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005357 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005358 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005359 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005360 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005361 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005362 case BTRFS_IOC_SUBVOL_CREATE_V2:
5363 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005364 case BTRFS_IOC_SNAP_DESTROY:
5365 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005366 case BTRFS_IOC_SUBVOL_GETFLAGS:
5367 return btrfs_ioctl_subvol_getflags(file, argp);
5368 case BTRFS_IOC_SUBVOL_SETFLAGS:
5369 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005370 case BTRFS_IOC_DEFAULT_SUBVOL:
5371 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005372 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005373 return btrfs_ioctl_defrag(file, NULL);
5374 case BTRFS_IOC_DEFRAG_RANGE:
5375 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005376 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005377 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005378 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005379 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005380 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005381 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005382 case BTRFS_IOC_FS_INFO:
5383 return btrfs_ioctl_fs_info(root, argp);
5384 case BTRFS_IOC_DEV_INFO:
5385 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005386 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005387 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005388 case BTRFS_IOC_TRANS_START:
5389 return btrfs_ioctl_trans_start(file);
5390 case BTRFS_IOC_TRANS_END:
5391 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005392 case BTRFS_IOC_TREE_SEARCH:
5393 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005394 case BTRFS_IOC_TREE_SEARCH_V2:
5395 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005396 case BTRFS_IOC_INO_LOOKUP:
5397 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005398 case BTRFS_IOC_INO_PATHS:
5399 return btrfs_ioctl_ino_to_path(root, argp);
5400 case BTRFS_IOC_LOGICAL_INO:
5401 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005402 case BTRFS_IOC_SPACE_INFO:
5403 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005404 case BTRFS_IOC_SYNC: {
5405 int ret;
5406
Miao Xie6c255e62014-03-06 13:55:01 +08005407 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005408 if (ret)
5409 return ret;
Al Viroddb52f42014-10-21 20:21:18 -04005410 ret = btrfs_sync_fs(file_inode(file)->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005411 /*
5412 * The transaction thread may want to do more work,
5413 * namely it pokes the cleaner ktread that will start
5414 * processing uncleaned subvols.
5415 */
5416 wake_up_process(root->fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005417 return ret;
5418 }
Sage Weil46204592010-10-29 15:41:32 -04005419 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005420 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005421 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005422 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005423 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005424 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005425 case BTRFS_IOC_SCRUB_CANCEL:
5426 return btrfs_ioctl_scrub_cancel(root, argp);
5427 case BTRFS_IOC_SCRUB_PROGRESS:
5428 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005429 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005430 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005431 case BTRFS_IOC_BALANCE_CTL:
5432 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005433 case BTRFS_IOC_BALANCE_PROGRESS:
5434 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005435 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5436 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005437#ifdef CONFIG_64BIT
5438 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5439 return btrfs_ioctl_set_received_subvol_32(file, argp);
5440#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005441 case BTRFS_IOC_SEND:
5442 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005443 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06005444 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005445 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005446 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005447 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005448 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005449 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005450 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005451 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005452 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005453 case BTRFS_IOC_QUOTA_RESCAN:
5454 return btrfs_ioctl_quota_rescan(file, argp);
5455 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5456 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005457 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5458 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005459 case BTRFS_IOC_DEV_REPLACE:
5460 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005461 case BTRFS_IOC_GET_FSLABEL:
5462 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005463 case BTRFS_IOC_SET_FSLABEL:
5464 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005465 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5466 return btrfs_ioctl_get_supported_features(file, argp);
5467 case BTRFS_IOC_GET_FEATURES:
5468 return btrfs_ioctl_get_features(file, argp);
5469 case BTRFS_IOC_SET_FEATURES:
5470 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005471 }
5472
5473 return -ENOTTY;
5474}