blob: 7a1ffacd314c32e617a54118fa9bc97103551dbe [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000043#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020044#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000045#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070046#include <linux/uaccess.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051#include "print-tree.h"
52#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040053#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020055#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040056#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020057#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010058#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000059#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040060#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070061#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000062#include "tree-log.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040063
Hugo Millsabccd002014-01-30 20:17:00 +000064#ifdef CONFIG_64BIT
65/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
66 * structures are incorrect, as the timespec structure from userspace
67 * is 4 bytes too small. We define these alternatives here to teach
68 * the kernel about the 32-bit struct packing.
69 */
70struct btrfs_ioctl_timespec_32 {
71 __u64 sec;
72 __u32 nsec;
73} __attribute__ ((__packed__));
74
75struct btrfs_ioctl_received_subvol_args_32 {
76 char uuid[BTRFS_UUID_SIZE]; /* in */
77 __u64 stransid; /* in */
78 __u64 rtransid; /* out */
79 struct btrfs_ioctl_timespec_32 stime; /* in */
80 struct btrfs_ioctl_timespec_32 rtime; /* out */
81 __u64 flags; /* in */
82 __u64 reserved[16]; /* in */
83} __attribute__ ((__packed__));
84
85#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
86 struct btrfs_ioctl_received_subvol_args_32)
87#endif
88
89
Mark Fasheh416161d2013-08-06 11:42:51 -070090static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070091 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
92 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070093
Christoph Hellwig6cbff002009-04-17 10:37:41 +020094/* Mask out flags that are inappropriate for the given type of inode. */
95static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
96{
97 if (S_ISDIR(mode))
98 return flags;
99 else if (S_ISREG(mode))
100 return flags & ~FS_DIRSYNC_FL;
101 else
102 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
103}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400104
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105/*
106 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
107 */
108static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
109{
110 unsigned int iflags = 0;
111
112 if (flags & BTRFS_INODE_SYNC)
113 iflags |= FS_SYNC_FL;
114 if (flags & BTRFS_INODE_IMMUTABLE)
115 iflags |= FS_IMMUTABLE_FL;
116 if (flags & BTRFS_INODE_APPEND)
117 iflags |= FS_APPEND_FL;
118 if (flags & BTRFS_INODE_NODUMP)
119 iflags |= FS_NODUMP_FL;
120 if (flags & BTRFS_INODE_NOATIME)
121 iflags |= FS_NOATIME_FL;
122 if (flags & BTRFS_INODE_DIRSYNC)
123 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000124 if (flags & BTRFS_INODE_NODATACOW)
125 iflags |= FS_NOCOW_FL;
126
127 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
128 iflags |= FS_COMPR_FL;
129 else if (flags & BTRFS_INODE_NOCOMPRESS)
130 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200131
132 return iflags;
133}
134
135/*
136 * Update inode->i_flags based on the btrfs internal flags.
137 */
138void btrfs_update_iflags(struct inode *inode)
139{
140 struct btrfs_inode *ip = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100141 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142
143 if (ip->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145 if (ip->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147 if (ip->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200149 if (ip->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200151 if (ip->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100152 new_fl |= S_DIRSYNC;
153
154 set_mask_bits(&inode->i_flags,
155 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
156 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157}
158
159/*
160 * Inherit flags from the parent inode.
161 *
Josef Bacike27425d2011-09-27 11:01:30 -0400162 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200163 */
164void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
165{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400166 unsigned int flags;
167
168 if (!dir)
169 return;
170
171 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200172
Josef Bacike27425d2011-09-27 11:01:30 -0400173 if (flags & BTRFS_INODE_NOCOMPRESS) {
174 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
175 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
176 } else if (flags & BTRFS_INODE_COMPRESS) {
177 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
178 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
179 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200180
Liu Bo213490b2012-09-11 08:33:50 -0600181 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400182 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600183 if (S_ISREG(inode->i_mode))
184 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
185 }
Josef Bacike27425d2011-09-27 11:01:30 -0400186
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200187 btrfs_update_iflags(inode);
188}
189
190static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
191{
Al Viro496ad9a2013-01-23 17:07:38 -0500192 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
194
195 if (copy_to_user(arg, &flags, sizeof(flags)))
196 return -EFAULT;
197 return 0;
198}
199
Liu Bo75e7cb72011-03-22 10:12:20 +0000200static int check_flags(unsigned int flags)
201{
202 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
203 FS_NOATIME_FL | FS_NODUMP_FL | \
204 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000205 FS_NOCOMP_FL | FS_COMPR_FL |
206 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000207 return -EOPNOTSUPP;
208
209 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
210 return -EINVAL;
211
Liu Bo75e7cb72011-03-22 10:12:20 +0000212 return 0;
213}
214
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200215static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
216{
Al Viro496ad9a2013-01-23 17:07:38 -0500217 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200218 struct btrfs_inode *ip = BTRFS_I(inode);
219 struct btrfs_root *root = ip->root;
220 struct btrfs_trans_handle *trans;
221 unsigned int flags, oldflags;
222 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800223 u64 ip_oldflags;
224 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600225 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226
David Sterbabd60ea02014-01-16 15:50:22 +0100227 if (!inode_owner_or_capable(inode))
228 return -EPERM;
229
Li Zefanb83cc962010-12-20 16:04:08 +0800230 if (btrfs_root_readonly(root))
231 return -EROFS;
232
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200233 if (copy_from_user(&flags, arg, sizeof(flags)))
234 return -EFAULT;
235
Liu Bo75e7cb72011-03-22 10:12:20 +0000236 ret = check_flags(flags);
237 if (ret)
238 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200239
Jan Karae7848682012-06-12 16:20:32 +0200240 ret = mnt_want_write_file(file);
241 if (ret)
242 return ret;
243
Al Viro59551022016-01-22 15:40:57 -0500244 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200245
Li Zefanf062abf2011-12-29 13:36:45 +0800246 ip_oldflags = ip->flags;
247 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600248 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800249
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200250 flags = btrfs_mask_flags(inode->i_mode, flags);
251 oldflags = btrfs_flags_to_ioctl(ip->flags);
252 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
253 if (!capable(CAP_LINUX_IMMUTABLE)) {
254 ret = -EPERM;
255 goto out_unlock;
256 }
257 }
258
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200259 if (flags & FS_SYNC_FL)
260 ip->flags |= BTRFS_INODE_SYNC;
261 else
262 ip->flags &= ~BTRFS_INODE_SYNC;
263 if (flags & FS_IMMUTABLE_FL)
264 ip->flags |= BTRFS_INODE_IMMUTABLE;
265 else
266 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
267 if (flags & FS_APPEND_FL)
268 ip->flags |= BTRFS_INODE_APPEND;
269 else
270 ip->flags &= ~BTRFS_INODE_APPEND;
271 if (flags & FS_NODUMP_FL)
272 ip->flags |= BTRFS_INODE_NODUMP;
273 else
274 ip->flags &= ~BTRFS_INODE_NODUMP;
275 if (flags & FS_NOATIME_FL)
276 ip->flags |= BTRFS_INODE_NOATIME;
277 else
278 ip->flags &= ~BTRFS_INODE_NOATIME;
279 if (flags & FS_DIRSYNC_FL)
280 ip->flags |= BTRFS_INODE_DIRSYNC;
281 else
282 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600283 if (flags & FS_NOCOW_FL) {
284 if (S_ISREG(mode)) {
285 /*
286 * It's safe to turn csums off here, no extents exist.
287 * Otherwise we want the flag to reflect the real COW
288 * status of the file and will not set it.
289 */
290 if (inode->i_size == 0)
291 ip->flags |= BTRFS_INODE_NODATACOW
292 | BTRFS_INODE_NODATASUM;
293 } else {
294 ip->flags |= BTRFS_INODE_NODATACOW;
295 }
296 } else {
297 /*
298 * Revert back under same assuptions as above
299 */
300 if (S_ISREG(mode)) {
301 if (inode->i_size == 0)
302 ip->flags &= ~(BTRFS_INODE_NODATACOW
303 | BTRFS_INODE_NODATASUM);
304 } else {
305 ip->flags &= ~BTRFS_INODE_NODATACOW;
306 }
307 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200308
Liu Bo75e7cb72011-03-22 10:12:20 +0000309 /*
310 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
311 * flag may be changed automatically if compression code won't make
312 * things smaller.
313 */
314 if (flags & FS_NOCOMP_FL) {
315 ip->flags &= ~BTRFS_INODE_COMPRESS;
316 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000317
318 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
319 if (ret && ret != -ENODATA)
320 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000321 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000322 const char *comp;
323
Liu Bo75e7cb72011-03-22 10:12:20 +0000324 ip->flags |= BTRFS_INODE_COMPRESS;
325 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000326
327 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
328 comp = "lzo";
329 else
330 comp = "zlib";
331 ret = btrfs_set_prop(inode, "btrfs.compression",
332 comp, strlen(comp), 0);
333 if (ret)
334 goto out_drop;
335
Li Zefanebcb9042011-04-15 03:03:17 +0000336 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100337 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
338 if (ret && ret != -ENODATA)
339 goto out_drop;
Li Zefanebcb9042011-04-15 03:03:17 +0000340 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000341 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200342
Li Zefan4da6f1a2011-12-29 13:39:50 +0800343 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800344 if (IS_ERR(trans)) {
345 ret = PTR_ERR(trans);
346 goto out_drop;
347 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200348
Li Zefan306424cc2011-12-14 20:12:02 -0500349 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400350 inode_inc_iversion(inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -0800351 inode->i_ctime = current_fs_time(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200352 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200353
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200354 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800355 out_drop:
356 if (ret) {
357 ip->flags = ip_oldflags;
358 inode->i_flags = i_oldflags;
359 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200360
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200361 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500362 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200363 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000364 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200365}
366
367static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
368{
Al Viro496ad9a2013-01-23 17:07:38 -0500369 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200370
371 return put_user(inode->i_generation, arg);
372}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400373
Li Dongyangf7039b12011-03-24 10:24:28 +0000374static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
375{
Al Viro54563d42013-09-01 15:57:51 -0400376 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000377 struct btrfs_device *device;
378 struct request_queue *q;
379 struct fstrim_range range;
380 u64 minlen = ULLONG_MAX;
381 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500382 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000383 int ret;
384
385 if (!capable(CAP_SYS_ADMIN))
386 return -EPERM;
387
Xiao Guangrong1f781602011-04-20 10:09:16 +0000388 rcu_read_lock();
389 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
390 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000391 if (!device->bdev)
392 continue;
393 q = bdev_get_queue(device->bdev);
394 if (blk_queue_discard(q)) {
395 num_devices++;
396 minlen = min((u64)q->limits.discard_granularity,
397 minlen);
398 }
399 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000400 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200401
Li Dongyangf7039b12011-03-24 10:24:28 +0000402 if (!num_devices)
403 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000404 if (copy_from_user(&range, arg, sizeof(range)))
405 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000406 if (range.start > total_bytes ||
407 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200408 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000409
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200410 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000411 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500412 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000413 if (ret < 0)
414 return ret;
415
416 if (copy_to_user(arg, &range, sizeof(range)))
417 return -EFAULT;
418
419 return 0;
420}
421
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200422int btrfs_is_empty_uuid(u8 *uuid)
423{
Chris Mason46e0f662013-11-15 12:14:55 +0100424 int i;
425
426 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
427 if (uuid[i])
428 return 0;
429 }
430 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200431}
432
Miao Xied5c12072013-02-28 10:04:33 +0000433static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400434 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400435 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200436 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000437 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400438{
439 struct btrfs_trans_handle *trans;
440 struct btrfs_key key;
441 struct btrfs_root_item root_item;
442 struct btrfs_inode_item *inode_item;
443 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000444 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400445 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000446 struct btrfs_block_rsv block_rsv;
Deepa Dinamani04b285f2016-02-06 23:57:21 -0800447 struct timespec cur_time = current_fs_time(dir->i_sb);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900448 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400449 int ret;
450 int err;
451 u64 objectid;
452 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500453 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000454 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200455 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400456
Li Zefan581bb052011-04-20 10:06:11 +0800457 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400458 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400459 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000460
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800461 /*
462 * Don't create subvolume whose level is not zero. Or qgroup will be
463 * screwed up since it assume subvolme qgroup's level to be 0.
464 */
465 if (btrfs_qgroup_level(objectid))
466 return -ENOSPC;
467
Miao Xied5c12072013-02-28 10:04:33 +0000468 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400469 /*
Miao Xied5c12072013-02-28 10:04:33 +0000470 * The same as the snapshot creation, please see the comment
471 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400472 */
Miao Xied5c12072013-02-28 10:04:33 +0000473 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200474 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000475 if (ret)
476 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400477
Miao Xied5c12072013-02-28 10:04:33 +0000478 trans = btrfs_start_transaction(root, 0);
479 if (IS_ERR(trans)) {
480 ret = PTR_ERR(trans);
Liu Bode6e8202014-01-09 14:57:06 +0800481 btrfs_subvolume_release_metadata(root, &block_rsv,
482 qgroup_reserved);
483 return ret;
Miao Xied5c12072013-02-28 10:04:33 +0000484 }
485 trans->block_rsv = &block_rsv;
486 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400487
Miao Xie8696c532013-02-07 06:02:44 +0000488 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200489 if (ret)
490 goto fail;
491
David Sterba4d75f8a2014-06-15 01:54:12 +0200492 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400493 if (IS_ERR(leaf)) {
494 ret = PTR_ERR(leaf);
495 goto fail;
496 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400497
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400498 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400499 btrfs_set_header_bytenr(leaf, leaf->start);
500 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400501 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400502 btrfs_set_header_owner(leaf, objectid);
503
Ross Kirk0a4e5582013-09-24 10:12:38 +0100504 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400505 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400506 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200507 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400508 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400509 btrfs_mark_buffer_dirty(leaf);
510
Alexander Block8ea05e32012-07-25 17:35:53 +0200511 memset(&root_item, 0, sizeof(root_item));
512
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400513 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800514 btrfs_set_stack_inode_generation(inode_item, 1);
515 btrfs_set_stack_inode_size(inode_item, 3);
516 btrfs_set_stack_inode_nlink(inode_item, 1);
David Sterba707e8a02014-06-04 19:22:26 +0200517 btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800518 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519
Qu Wenruo3cae2102013-07-16 11:19:18 +0800520 btrfs_set_root_flags(&root_item, 0);
521 btrfs_set_root_limit(&root_item, 0);
522 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000523
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400524 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400525 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400526 btrfs_set_root_level(&root_item, 0);
527 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000528 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400529 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400530
Alexander Block8ea05e32012-07-25 17:35:53 +0200531 btrfs_set_root_generation_v2(&root_item,
532 btrfs_root_generation(&root_item));
533 uuid_le_gen(&new_uuid);
534 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800535 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
536 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200537 root_item.ctime = root_item.otime;
538 btrfs_set_root_ctransid(&root_item, trans->transid);
539 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400540
Chris Mason925baed2008-06-25 16:01:30 -0400541 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400542 free_extent_buffer(leaf);
543 leaf = NULL;
544
545 btrfs_set_root_dirid(&root_item, new_dirid);
546
547 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200549 key.type = BTRFS_ROOT_ITEM_KEY;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400550 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
551 &root_item);
552 if (ret)
553 goto fail;
554
Yan, Zheng76dda932009-09-21 16:00:26 -0400555 key.offset = (u64)-1;
556 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 ret = PTR_ERR(new_root);
David Sterba6d13f542015-04-24 19:12:01 +0200559 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100560 goto fail;
561 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400562
563 btrfs_record_root_in_trans(trans, new_root);
564
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000565 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700566 if (ret) {
567 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700569 goto fail;
570 }
571
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530572 mutex_lock(&new_root->objectid_mutex);
573 new_root->highest_objectid = new_dirid;
574 mutex_unlock(&new_root->objectid_mutex);
575
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400576 /*
577 * insert the directory item
578 */
Chris Mason3de45862008-11-17 21:02:50 -0500579 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100580 if (ret) {
581 btrfs_abort_transaction(trans, root, ret);
582 goto fail;
583 }
Chris Mason3de45862008-11-17 21:02:50 -0500584
585 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800586 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500587 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100588 if (ret) {
589 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400590 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100591 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500592
Yan Zheng52c26172009-01-05 15:43:43 -0500593 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
594 ret = btrfs_update_inode(trans, root, dir);
595 BUG_ON(ret);
596
Chris Mason0660b5a2008-11-17 20:37:39 -0500597 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400598 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800599 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500600 BUG_ON(ret);
601
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200602 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
603 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
604 objectid);
605 if (ret)
606 btrfs_abort_transaction(trans, root, ret);
607
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608fail:
Miao Xied5c12072013-02-28 10:04:33 +0000609 trans->block_rsv = NULL;
610 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800611 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
612
Sage Weil72fd0322010-10-29 15:41:32 -0400613 if (async_transid) {
614 *async_transid = trans->transid;
615 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000616 if (err)
617 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400618 } else {
619 err = btrfs_commit_transaction(trans, root);
620 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400621 if (err && !ret)
622 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500623
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900624 if (!ret) {
625 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800626 if (IS_ERR(inode))
627 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900628 d_instantiate(dentry, inode);
629 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630 return ret;
631}
632
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000633static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +0800634{
635 s64 writers;
636 DEFINE_WAIT(wait);
637
638 do {
639 prepare_to_wait(&root->subv_writers->wait, &wait,
640 TASK_UNINTERRUPTIBLE);
641
642 writers = percpu_counter_sum(&root->subv_writers->counter);
643 if (writers)
644 schedule();
645
646 finish_wait(&root->subv_writers->wait, &wait);
647 } while (writers);
648}
649
Miao Xiee9662f72013-02-28 10:01:15 +0000650static int create_snapshot(struct btrfs_root *root, struct inode *dir,
651 struct dentry *dentry, char *name, int namelen,
652 u64 *async_transid, bool readonly,
653 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000655 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656 struct btrfs_pending_snapshot *pending_snapshot;
657 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000658 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400659
Miao Xie27cdeb72014-04-02 19:51:05 +0800660 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400661 return -EINVAL;
662
David Sterbaa1ee7362015-11-10 18:53:56 +0100663 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
664 if (!pending_snapshot)
665 return -ENOMEM;
666
David Sterbab0c0ea62015-11-10 18:54:00 +0100667 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
668 GFP_NOFS);
David Sterba8546b572015-11-10 18:54:03 +0100669 pending_snapshot->path = btrfs_alloc_path();
670 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100671 ret = -ENOMEM;
672 goto free_pending;
673 }
674
Miao Xie8257b2d2014-03-06 13:38:19 +0800675 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100676 smp_mb__after_atomic();
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000677 btrfs_wait_for_no_snapshoting_writes(root);
Miao Xie8257b2d2014-03-06 13:38:19 +0800678
Miao Xie6a038432013-05-15 07:48:24 +0000679 ret = btrfs_start_delalloc_inodes(root, 0);
680 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100681 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000682
Miao Xieb0244192013-11-04 23:13:25 +0800683 btrfs_wait_ordered_extents(root, -1);
Miao Xie6a038432013-05-15 07:48:24 +0000684
Miao Xie66d8f3d2012-09-06 04:02:28 -0600685 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
686 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000687 /*
688 * 1 - parent dir inode
689 * 2 - dir entries
690 * 1 - root item
691 * 2 - root ref/backref
692 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200693 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000694 */
695 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200696 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400697 &pending_snapshot->qgroup_reserved,
698 false);
Miao Xied5c12072013-02-28 10:04:33 +0000699 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100700 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000701
Yan, Zhenga22285a2010-05-16 10:48:46 -0400702 pending_snapshot->dentry = dentry;
703 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800704 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000705 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000706 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400707
Miao Xied5c12072013-02-28 10:04:33 +0000708 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400709 if (IS_ERR(trans)) {
710 ret = PTR_ERR(trans);
711 goto fail;
712 }
713
Josef Bacik83515832011-06-14 15:16:14 -0400714 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400715 list_add(&pending_snapshot->list,
716 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400717 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400718 if (async_transid) {
719 *async_transid = trans->transid;
720 ret = btrfs_commit_transaction_async(trans,
721 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000722 if (ret)
723 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400724 } else {
725 ret = btrfs_commit_transaction(trans,
726 root->fs_info->extent_root);
727 }
Miao Xieaec80302013-03-04 09:44:29 +0000728 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400729 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400730
731 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400732 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000733 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400734
Chris Masond3797302014-10-15 13:50:56 -0700735 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
736 if (ret)
737 goto fail;
738
David Howells2b0143b2015-03-17 22:25:59 +0000739 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000740 if (IS_ERR(inode)) {
741 ret = PTR_ERR(inode);
742 goto fail;
743 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900744
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000745 d_instantiate(dentry, inode);
746 ret = 0;
747fail:
Miao Xied5c12072013-02-28 10:04:33 +0000748 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
749 &pending_snapshot->block_rsv,
750 pending_snapshot->qgroup_reserved);
David Sterbaa1ee7362015-11-10 18:53:56 +0100751dec_and_free:
Filipe Manana9ea24bb2014-10-29 11:57:59 +0000752 if (atomic_dec_and_test(&root->will_be_snapshoted))
753 wake_up_atomic_t(&root->will_be_snapshoted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100754free_pending:
755 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100756 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100757 kfree(pending_snapshot);
758
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400759 return ret;
760}
761
Sage Weil4260f7c2010-10-29 15:46:43 -0400762/* copy of may_delete in fs/namei.c()
763 * Check whether we can remove a link victim from directory dir, check
764 * whether the type of victim is right.
765 * 1. We can't do it if dir is read-only (done in permission())
766 * 2. We should have write and exec permissions on dir
767 * 3. We can't remove anything from append-only dir
768 * 4. We can't do anything with immutable dir (done in permission())
769 * 5. If the sticky bit on dir is set we should either
770 * a. be owner of dir, or
771 * b. be owner of victim, or
772 * c. have CAP_FOWNER capability
773 * 6. If the victim is append-only or immutable we can't do antyhing with
774 * links pointing to it.
775 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
776 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
777 * 9. We can't remove a root or mountpoint.
778 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
779 * nfs_async_unlink().
780 */
781
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530782static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400783{
784 int error;
785
David Howells2b0143b2015-03-17 22:25:59 +0000786 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400787 return -ENOENT;
788
David Howells2b0143b2015-03-17 22:25:59 +0000789 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400790 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400791
792 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
793 if (error)
794 return error;
795 if (IS_APPEND(dir))
796 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000797 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
798 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400799 return -EPERM;
800 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000801 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400802 return -ENOTDIR;
803 if (IS_ROOT(victim))
804 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000805 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400806 return -EISDIR;
807 if (IS_DEADDIR(dir))
808 return -ENOENT;
809 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
810 return -EBUSY;
811 return 0;
812}
813
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400814/* copy of may_create in fs/namei.c() */
815static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
816{
David Howells2b0143b2015-03-17 22:25:59 +0000817 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400818 return -EEXIST;
819 if (IS_DEADDIR(dir))
820 return -ENOENT;
821 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
822}
823
824/*
825 * Create a new subvolume below @parent. This is largely modeled after
826 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
827 * inside this filesystem so it's quite a bit simpler.
828 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400829static noinline int btrfs_mksubvol(struct path *parent,
830 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400831 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200832 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000833 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400834{
David Howells2b0143b2015-03-17 22:25:59 +0000835 struct inode *dir = d_inode(parent->dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400836 struct dentry *dentry;
837 int error;
838
David Sterba5c50c9b2013-03-22 18:12:51 +0000839 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
840 if (error == -EINTR)
841 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400842
843 dentry = lookup_one_len(name, parent->dentry, namelen);
844 error = PTR_ERR(dentry);
845 if (IS_ERR(dentry))
846 goto out_unlock;
847
Yan, Zheng76dda932009-09-21 16:00:26 -0400848 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400849 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600850 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400851
Chris Mason9c520572012-12-17 14:26:57 -0500852 /*
853 * even if this name doesn't exist, we may get hash collisions.
854 * check for them now when we can safely fail
855 */
856 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
857 dir->i_ino, name,
858 namelen);
859 if (error)
860 goto out_dput;
861
Yan, Zheng76dda932009-09-21 16:00:26 -0400862 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
863
864 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
865 goto out_up_read;
866
Chris Mason3de45862008-11-17 21:02:50 -0500867 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000868 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200869 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500870 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000871 error = create_subvol(dir, dentry, name, namelen,
872 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500873 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400874 if (!error)
875 fsnotify_mkdir(dir, dentry);
876out_up_read:
877 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400878out_dput:
879 dput(dentry);
880out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500881 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400882 return error;
883}
884
Chris Mason4cb53002011-05-24 15:35:30 -0400885/*
886 * When we're defragging a range, we don't want to kick it off again
887 * if it is really just waiting for delalloc to send it down.
888 * If we find a nice big extent or delalloc range for the bytes in the
889 * file you want to defrag, we return 0 to let you know to skip this
890 * part of the file
891 */
David Sterbaaab110a2014-07-29 17:32:10 +0200892static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400893{
894 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
895 struct extent_map *em = NULL;
896 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
897 u64 end;
898
899 read_lock(&em_tree->lock);
900 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
901 read_unlock(&em_tree->lock);
902
903 if (em) {
904 end = extent_map_end(em);
905 free_extent_map(em);
906 if (end - offset > thresh)
907 return 0;
908 }
909 /* if we already have a nice delalloc here, just stop */
910 thresh /= 2;
911 end = count_range_bits(io_tree, &offset, offset + thresh,
912 thresh, EXTENT_DELALLOC, 1);
913 if (end >= thresh)
914 return 0;
915 return 1;
916}
917
918/*
919 * helper function to walk through a file and find extents
920 * newer than a specific transid, and smaller than thresh.
921 *
922 * This is used by the defragging code to find new and small
923 * extents
924 */
925static int find_new_extents(struct btrfs_root *root,
926 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +0200927 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400928{
929 struct btrfs_path *path;
930 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400931 struct extent_buffer *leaf;
932 struct btrfs_file_extent_item *extent;
933 int type;
934 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000935 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400936
937 path = btrfs_alloc_path();
938 if (!path)
939 return -ENOMEM;
940
David Sterbaa4689d22011-05-31 17:08:14 +0000941 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400942 min_key.type = BTRFS_EXTENT_DATA_KEY;
943 min_key.offset = *off;
944
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530945 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100946 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400947 if (ret != 0)
948 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000949process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000950 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400951 goto none;
952 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
953 goto none;
954
955 leaf = path->nodes[0];
956 extent = btrfs_item_ptr(leaf, path->slots[0],
957 struct btrfs_file_extent_item);
958
959 type = btrfs_file_extent_type(leaf, extent);
960 if (type == BTRFS_FILE_EXTENT_REG &&
961 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
962 check_defrag_in_cache(inode, min_key.offset, thresh)) {
963 *off = min_key.offset;
964 btrfs_free_path(path);
965 return 0;
966 }
967
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000968 path->slots[0]++;
969 if (path->slots[0] < btrfs_header_nritems(leaf)) {
970 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
971 goto process_slot;
972 }
973
Chris Mason4cb53002011-05-24 15:35:30 -0400974 if (min_key.offset == (u64)-1)
975 goto none;
976
977 min_key.offset++;
978 btrfs_release_path(path);
979 }
980none:
981 btrfs_free_path(path);
982 return -ENOENT;
983}
984
Li Zefan6c282eb2012-06-11 16:03:35 +0800985static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400986{
987 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500988 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800989 struct extent_map *em;
990 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500991
992 /*
993 * hopefully we have this extent in the tree already, try without
994 * the full extent lock
995 */
996 read_lock(&em_tree->lock);
997 em = lookup_extent_mapping(em_tree, start, len);
998 read_unlock(&em_tree->lock);
999
1000 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001001 struct extent_state *cached = NULL;
1002 u64 end = start + len - 1;
1003
Chris Mason940100a2010-03-10 10:52:59 -05001004 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001005 lock_extent_bits(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001006 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +00001007 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -05001008
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001009 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001010 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001011 }
1012
Li Zefan6c282eb2012-06-11 16:03:35 +08001013 return em;
1014}
1015
1016static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1017{
1018 struct extent_map *next;
1019 bool ret = true;
1020
1021 /* this is the last extent */
1022 if (em->start + em->len >= i_size_read(inode))
1023 return false;
1024
1025 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001026 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1027 ret = false;
1028 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001029 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001030 ret = false;
1031
1032 free_extent_map(next);
1033 return ret;
1034}
1035
David Sterbaaab110a2014-07-29 17:32:10 +02001036static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001037 u64 *last_len, u64 *skip, u64 *defrag_end,
1038 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001039{
1040 struct extent_map *em;
1041 int ret = 1;
1042 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001043 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001044
1045 /*
1046 * make sure that once we start defragging an extent, we keep on
1047 * defragging it
1048 */
1049 if (start < *defrag_end)
1050 return 1;
1051
1052 *skip = 0;
1053
1054 em = defrag_lookup_extent(inode, start);
1055 if (!em)
1056 return 0;
1057
Chris Mason940100a2010-03-10 10:52:59 -05001058 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001059 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001060 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001061 goto out;
1062 }
1063
Liu Bo4a3560c2015-08-07 16:48:41 +08001064 if (!*defrag_end)
1065 prev_mergeable = false;
1066
Li Zefan6c282eb2012-06-11 16:03:35 +08001067 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001068 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001069 * we hit a real extent, if it is big or the next extent is not a
1070 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001071 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001072 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001073 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001074 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001075out:
Chris Mason940100a2010-03-10 10:52:59 -05001076 /*
1077 * last_len ends up being a counter of how many bytes we've defragged.
1078 * every time we choose not to defrag an extent, we reset *last_len
1079 * so that the next tiny extent will force a defrag.
1080 *
1081 * The end result of this is that tiny extents before a single big
1082 * extent will force at least part of that big extent to be defragged.
1083 */
1084 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001085 *defrag_end = extent_map_end(em);
1086 } else {
1087 *last_len = 0;
1088 *skip = extent_map_end(em);
1089 *defrag_end = 0;
1090 }
1091
1092 free_extent_map(em);
1093 return ret;
1094}
1095
Chris Mason4cb53002011-05-24 15:35:30 -04001096/*
1097 * it doesn't do much good to defrag one or two pages
1098 * at a time. This pulls in a nice chunk of pages
1099 * to COW and defrag.
1100 *
1101 * It also makes sure the delalloc code has enough
1102 * dirty data to avoid making new small extents as part
1103 * of the defrag
1104 *
1105 * It's a good idea to start RA on this range
1106 * before calling this.
1107 */
1108static int cluster_pages_for_defrag(struct inode *inode,
1109 struct page **pages,
1110 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001111 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001112{
Chris Mason4cb53002011-05-24 15:35:30 -04001113 unsigned long file_end;
1114 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001115 u64 page_start;
1116 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001117 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001118 int ret;
1119 int i;
1120 int i_done;
1121 struct btrfs_ordered_extent *ordered;
1122 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001123 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001124 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001125
Chris Mason4cb53002011-05-24 15:35:30 -04001126 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001127 if (!isize || start_index > file_end)
1128 return 0;
1129
1130 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001131
1132 ret = btrfs_delalloc_reserve_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001133 start_index << PAGE_CACHE_SHIFT,
1134 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001135 if (ret)
1136 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001137 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001138 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001139
1140 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001141 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001142 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001143again:
Josef Bacika94733d2011-07-11 10:47:06 -04001144 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001145 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001146 if (!page)
1147 break;
1148
Miao Xie600a45e2012-02-16 15:01:24 +08001149 page_start = page_offset(page);
1150 page_end = page_start + PAGE_CACHE_SIZE - 1;
1151 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001152 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001153 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001154 ordered = btrfs_lookup_ordered_extent(inode,
1155 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001156 unlock_extent_cached(tree, page_start, page_end,
1157 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001158 if (!ordered)
1159 break;
1160
1161 unlock_page(page);
1162 btrfs_start_ordered_extent(inode, ordered, 1);
1163 btrfs_put_ordered_extent(ordered);
1164 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001165 /*
1166 * we unlocked the page above, so we need check if
1167 * it was released or not.
1168 */
1169 if (page->mapping != inode->i_mapping) {
1170 unlock_page(page);
1171 page_cache_release(page);
1172 goto again;
1173 }
Miao Xie600a45e2012-02-16 15:01:24 +08001174 }
1175
Chris Mason4cb53002011-05-24 15:35:30 -04001176 if (!PageUptodate(page)) {
1177 btrfs_readpage(NULL, page);
1178 lock_page(page);
1179 if (!PageUptodate(page)) {
1180 unlock_page(page);
1181 page_cache_release(page);
1182 ret = -EIO;
1183 break;
1184 }
1185 }
Miao Xie600a45e2012-02-16 15:01:24 +08001186
Miao Xie600a45e2012-02-16 15:01:24 +08001187 if (page->mapping != inode->i_mapping) {
1188 unlock_page(page);
1189 page_cache_release(page);
1190 goto again;
1191 }
1192
Chris Mason4cb53002011-05-24 15:35:30 -04001193 pages[i] = page;
1194 i_done++;
1195 }
1196 if (!i_done || ret)
1197 goto out;
1198
1199 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1200 goto out;
1201
1202 /*
1203 * so now we have a nice long stream of locked
1204 * and up to date pages, lets wait on them
1205 */
1206 for (i = 0; i < i_done; i++)
1207 wait_on_page_writeback(pages[i]);
1208
1209 page_start = page_offset(pages[0]);
1210 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1211
1212 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001213 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001214 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1215 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001216 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1217 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001218
Liu Bo1f12bd02012-03-29 09:57:44 -04001219 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001220 spin_lock(&BTRFS_I(inode)->lock);
1221 BTRFS_I(inode)->outstanding_extents++;
1222 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001223 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001224 start_index << PAGE_CACHE_SHIFT,
1225 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001226 }
1227
1228
Liu Bo9e8a4a82012-09-05 19:10:51 -06001229 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1230 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001231
1232 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1233 page_start, page_end - 1, &cached_state,
1234 GFP_NOFS);
1235
1236 for (i = 0; i < i_done; i++) {
1237 clear_page_dirty_for_io(pages[i]);
1238 ClearPageChecked(pages[i]);
1239 set_page_extent_mapped(pages[i]);
1240 set_page_dirty(pages[i]);
1241 unlock_page(pages[i]);
1242 page_cache_release(pages[i]);
1243 }
1244 return i_done;
1245out:
1246 for (i = 0; i < i_done; i++) {
1247 unlock_page(pages[i]);
1248 page_cache_release(pages[i]);
1249 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001250 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08001251 start_index << PAGE_CACHE_SHIFT,
1252 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001253 return ret;
1254
1255}
1256
1257int btrfs_defrag_file(struct inode *inode, struct file *file,
1258 struct btrfs_ioctl_defrag_range_args *range,
1259 u64 newer_than, unsigned long max_to_defrag)
1260{
1261 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001262 struct file_ra_state *ra = NULL;
1263 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001264 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001265 u64 last_len = 0;
1266 u64 skip = 0;
1267 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001268 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001269 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001270 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001271 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001272 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001273 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001274 u32 extent_thresh = range->extent_thresh;
Byongho Leeee221842015-12-15 01:42:10 +09001275 unsigned long max_cluster = SZ_256K >> PAGE_CACHE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001276 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001277 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001278 struct page **pages = NULL;
1279
Liu Bo0abd5b12013-04-16 09:20:28 +00001280 if (isize == 0)
1281 return 0;
1282
1283 if (range->start >= isize)
1284 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001285
1286 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1287 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1288 return -EINVAL;
1289 if (range->compress_type)
1290 compress_type = range->compress_type;
1291 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001292
Liu Bo0abd5b12013-04-16 09:20:28 +00001293 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001294 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001295
Chris Mason4cb53002011-05-24 15:35:30 -04001296 /*
1297 * if we were not given a file, allocate a readahead
1298 * context
1299 */
1300 if (!file) {
1301 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1302 if (!ra)
1303 return -ENOMEM;
1304 file_ra_state_init(ra, inode->i_mapping);
1305 } else {
1306 ra = &file->f_ra;
1307 }
1308
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301309 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001310 GFP_NOFS);
1311 if (!pages) {
1312 ret = -ENOMEM;
1313 goto out_ra;
1314 }
1315
1316 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001317 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001318 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001319 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1320 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001321 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001322 }
1323
Chris Mason4cb53002011-05-24 15:35:30 -04001324 if (newer_than) {
1325 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001326 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001327 if (!ret) {
1328 range->start = newer_off;
1329 /*
1330 * we always align our defrag to help keep
1331 * the extents in the file evenly spaced
1332 */
1333 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001334 } else
1335 goto out_ra;
1336 } else {
1337 i = range->start >> PAGE_CACHE_SHIFT;
1338 }
1339 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301340 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001341
Li Zefan2a0f7f52011-10-10 15:43:34 -04001342 /*
1343 * make writeback starts from i, so the defrag range can be
1344 * written sequentially.
1345 */
1346 if (i < inode->i_mapping->writeback_index)
1347 inode->i_mapping->writeback_index = i;
1348
Chris Masonf7f43cc2011-10-11 11:41:40 -04001349 while (i <= last_index && defrag_count < max_to_defrag &&
David Sterbaed6078f2014-06-05 01:59:57 +02001350 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001351 /*
1352 * make sure we stop running if someone unmounts
1353 * the FS
1354 */
1355 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1356 break;
1357
David Sterba210549e2013-02-09 23:38:06 +00001358 if (btrfs_defrag_cancelled(root->fs_info)) {
David Sterbaf14d1042015-10-08 11:37:06 +02001359 btrfs_debug(root->fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001360 ret = -EAGAIN;
1361 break;
1362 }
1363
Liu Bo66c26892012-03-29 09:57:45 -04001364 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001365 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001366 &defrag_end, range->flags &
1367 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001368 unsigned long next;
1369 /*
1370 * the should_defrag function tells us how much to skip
1371 * bump our counter by the suggested amount
1372 */
David Sterbaed6078f2014-06-05 01:59:57 +02001373 next = DIV_ROUND_UP(skip, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001374 i = max(i + 1, next);
1375 continue;
1376 }
Li Zefan008873e2011-09-02 15:57:07 +08001377
1378 if (!newer_than) {
1379 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1380 PAGE_CACHE_SHIFT) - i;
1381 cluster = min(cluster, max_cluster);
1382 } else {
1383 cluster = max_cluster;
1384 }
1385
Li Zefan008873e2011-09-02 15:57:07 +08001386 if (i + cluster > ra_index) {
1387 ra_index = max(i, ra_index);
1388 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1389 cluster);
chandane4826a52015-06-09 17:38:32 +05301390 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001391 }
Chris Mason940100a2010-03-10 10:52:59 -05001392
Al Viro59551022016-01-22 15:40:57 -05001393 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001394 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1395 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001396 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001397 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001398 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001399 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001400 }
Chris Mason940100a2010-03-10 10:52:59 -05001401
Chris Mason4cb53002011-05-24 15:35:30 -04001402 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001403 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001404 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001405
1406 if (newer_than) {
1407 if (newer_off == (u64)-1)
1408 break;
1409
Liu Boe1f041e12012-03-29 09:57:45 -04001410 if (ret > 0)
1411 i += ret;
1412
Chris Mason4cb53002011-05-24 15:35:30 -04001413 newer_off = max(newer_off + 1,
1414 (u64)i << PAGE_CACHE_SHIFT);
1415
Byongho Leeee221842015-12-15 01:42:10 +09001416 ret = find_new_extents(root, inode, newer_than,
1417 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001418 if (!ret) {
1419 range->start = newer_off;
1420 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001421 } else {
1422 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001423 }
Chris Mason4cb53002011-05-24 15:35:30 -04001424 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001425 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001426 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001427 last_len += ret << PAGE_CACHE_SHIFT;
1428 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001429 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001430 last_len = 0;
1431 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001432 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001433 }
1434
Filipe Mananadec8ef92014-03-01 10:55:54 +00001435 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001436 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001437 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1438 &BTRFS_I(inode)->runtime_flags))
1439 filemap_flush(inode->i_mapping);
1440 }
Chris Mason1e701a32010-03-11 09:42:04 -05001441
1442 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1443 /* the filemap_flush will queue IO into the worker threads, but
1444 * we have to make sure the IO is actually started and that
1445 * ordered extents get created before we return
1446 */
1447 atomic_inc(&root->fs_info->async_submit_draining);
1448 while (atomic_read(&root->fs_info->nr_async_submits) ||
1449 atomic_read(&root->fs_info->async_delalloc_pages)) {
1450 wait_event(root->fs_info->async_submit_wait,
1451 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1452 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1453 }
1454 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001455 }
1456
Li Zefan1a419d82010-10-25 15:12:50 +08001457 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001458 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001459 }
1460
Diego Calleja60ccf822011-09-01 16:33:57 +02001461 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001462
Chris Mason4cb53002011-05-24 15:35:30 -04001463out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001464 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
Al Viro59551022016-01-22 15:40:57 -05001465 inode_lock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001466 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001467 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001468 }
Chris Mason4cb53002011-05-24 15:35:30 -04001469 if (!file)
1470 kfree(ra);
1471 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001472 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001473}
1474
Miao Xie198605a2012-11-26 08:43:45 +00001475static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001476 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001477{
1478 u64 new_size;
1479 u64 old_size;
1480 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001481 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001482 struct btrfs_ioctl_vol_args *vol_args;
1483 struct btrfs_trans_handle *trans;
1484 struct btrfs_device *device = NULL;
1485 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001486 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001487 char *devstr = NULL;
1488 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001489 int mod = 0;
1490
Chris Masone441d542009-01-05 16:57:23 -05001491 if (!capable(CAP_SYS_ADMIN))
1492 return -EPERM;
1493
Miao Xie198605a2012-11-26 08:43:45 +00001494 ret = mnt_want_write_file(file);
1495 if (ret)
1496 return ret;
1497
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001498 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1499 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001500 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001501 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001502 }
1503
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001504 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001505 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001506 if (IS_ERR(vol_args)) {
1507 ret = PTR_ERR(vol_args);
1508 goto out;
1509 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001510
1511 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001512
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001513 sizestr = vol_args->name;
1514 devstr = strchr(sizestr, ':');
1515 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001516 sizestr = devstr + 1;
1517 *devstr = '\0';
1518 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001519 ret = kstrtoull(devstr, 10, &devid);
1520 if (ret)
1521 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001522 if (!devid) {
1523 ret = -EINVAL;
1524 goto out_free;
1525 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001526 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001527 }
Miao Xiedba60f32012-12-21 09:19:51 +00001528
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001529 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001530 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001531 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001532 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001533 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001534 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001535 }
Miao Xiedba60f32012-12-21 09:19:51 +00001536
1537 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001538 btrfs_info(root->fs_info,
1539 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001540 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001541 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001542 goto out_free;
1543 }
1544
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001545 if (!strcmp(sizestr, "max"))
1546 new_size = device->bdev->bd_inode->i_size;
1547 else {
1548 if (sizestr[0] == '-') {
1549 mod = -1;
1550 sizestr++;
1551 } else if (sizestr[0] == '+') {
1552 mod = 1;
1553 sizestr++;
1554 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001555 new_size = memparse(sizestr, &retptr);
1556 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001557 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001558 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001559 }
1560 }
1561
Stefan Behrens63a212a2012-11-05 18:29:28 +01001562 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001563 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001564 goto out_free;
1565 }
1566
Miao Xie7cc8e582014-09-03 21:35:38 +08001567 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001568
1569 if (mod < 0) {
1570 if (new_size > old_size) {
1571 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001572 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573 }
1574 new_size = old_size - new_size;
1575 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001576 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001577 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001578 goto out_free;
1579 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001580 new_size = old_size + new_size;
1581 }
1582
Byongho Leeee221842015-12-15 01:42:10 +09001583 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001584 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001585 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001586 }
1587 if (new_size > device->bdev->bd_inode->i_size) {
1588 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001589 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001590 }
1591
David Sterbab8b93ad2015-01-16 17:26:13 +01001592 new_size = div_u64(new_size, root->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001593 new_size *= root->sectorsize;
1594
David Sterbaecaeb142015-10-08 09:01:03 +02001595 btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001596 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001597
1598 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001599 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001600 if (IS_ERR(trans)) {
1601 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001602 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001603 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001604 ret = btrfs_grow_device(trans, device, new_size);
1605 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001606 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001607 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001608 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001609
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001610out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001611 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001612out:
1613 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001614 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001615 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616 return ret;
1617}
1618
Sage Weil72fd0322010-10-29 15:41:32 -04001619static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001620 char *name, unsigned long fd, int subvol,
1621 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001622 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001623{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001624 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001625 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626
Liu Boa874a632012-06-29 03:58:46 -06001627 ret = mnt_want_write_file(file);
1628 if (ret)
1629 goto out;
1630
Sage Weil72fd0322010-10-29 15:41:32 -04001631 namelen = strlen(name);
1632 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001634 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001635 }
1636
Chris Mason16780ca2012-02-20 22:14:55 -05001637 if (name[0] == '.' &&
1638 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1639 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001640 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001641 }
1642
Chris Mason3de45862008-11-17 21:02:50 -05001643 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001644 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001645 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001646 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001647 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001648 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001649 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001650 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001651 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001652 }
1653
Al Viro496ad9a2013-01-23 17:07:38 -05001654 src_inode = file_inode(src.file);
1655 if (src_inode->i_sb != file_inode(file)->i_sb) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001656 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1657 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001658 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001659 } else if (!inode_owner_or_capable(src_inode)) {
1660 /*
1661 * Subvolume creation is not restricted, but snapshots
1662 * are limited to own subvolumes only
1663 */
1664 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001665 } else {
1666 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1667 BTRFS_I(src_inode)->root,
1668 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001669 }
Al Viro2903ff02012-08-28 12:52:22 -04001670 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001671 }
Liu Boa874a632012-06-29 03:58:46 -06001672out_drop_write:
1673 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001674out:
Sage Weil72fd0322010-10-29 15:41:32 -04001675 return ret;
1676}
1677
1678static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001679 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001680{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001681 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001682 int ret;
1683
Li Zefanfa0d2b92010-12-20 15:53:28 +08001684 vol_args = memdup_user(arg, sizeof(*vol_args));
1685 if (IS_ERR(vol_args))
1686 return PTR_ERR(vol_args);
1687 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001688
Li Zefanfa0d2b92010-12-20 15:53:28 +08001689 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001690 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001691 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001692
Li Zefanfa0d2b92010-12-20 15:53:28 +08001693 kfree(vol_args);
1694 return ret;
1695}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001696
Li Zefanfa0d2b92010-12-20 15:53:28 +08001697static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1698 void __user *arg, int subvol)
1699{
1700 struct btrfs_ioctl_vol_args_v2 *vol_args;
1701 int ret;
1702 u64 transid = 0;
1703 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001704 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001705 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001706
Li Zefanfa0d2b92010-12-20 15:53:28 +08001707 vol_args = memdup_user(arg, sizeof(*vol_args));
1708 if (IS_ERR(vol_args))
1709 return PTR_ERR(vol_args);
1710 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001711
Li Zefanb83cc962010-12-20 16:04:08 +08001712 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001713 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1714 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001715 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001716 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001717 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001718
1719 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1720 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001721 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1722 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001723 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1724 if (vol_args->size > PAGE_CACHE_SIZE) {
1725 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001726 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001727 }
1728 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1729 if (IS_ERR(inherit)) {
1730 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001731 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001732 }
1733 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001734
1735 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001736 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001737 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001738 if (ret)
1739 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001740
Dan Carpenterc47ca322014-09-04 14:09:15 +03001741 if (ptr && copy_to_user(arg +
1742 offsetof(struct btrfs_ioctl_vol_args_v2,
1743 transid),
1744 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001745 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001746
1747free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001748 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001749free_args:
1750 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001751 return ret;
1752}
1753
Li Zefan0caa1022010-12-20 16:30:25 +08001754static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1755 void __user *arg)
1756{
Al Viro496ad9a2013-01-23 17:07:38 -05001757 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001758 struct btrfs_root *root = BTRFS_I(inode)->root;
1759 int ret = 0;
1760 u64 flags = 0;
1761
Li Zefan33345d012011-04-20 10:31:50 +08001762 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001763 return -EINVAL;
1764
1765 down_read(&root->fs_info->subvol_sem);
1766 if (btrfs_root_readonly(root))
1767 flags |= BTRFS_SUBVOL_RDONLY;
1768 up_read(&root->fs_info->subvol_sem);
1769
1770 if (copy_to_user(arg, &flags, sizeof(flags)))
1771 ret = -EFAULT;
1772
1773 return ret;
1774}
1775
1776static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1777 void __user *arg)
1778{
Al Viro496ad9a2013-01-23 17:07:38 -05001779 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001780 struct btrfs_root *root = BTRFS_I(inode)->root;
1781 struct btrfs_trans_handle *trans;
1782 u64 root_flags;
1783 u64 flags;
1784 int ret = 0;
1785
David Sterbabd60ea02014-01-16 15:50:22 +01001786 if (!inode_owner_or_capable(inode))
1787 return -EPERM;
1788
Liu Bob9ca0662012-06-29 03:58:49 -06001789 ret = mnt_want_write_file(file);
1790 if (ret)
1791 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001792
Liu Bob9ca0662012-06-29 03:58:49 -06001793 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1794 ret = -EINVAL;
1795 goto out_drop_write;
1796 }
Li Zefan0caa1022010-12-20 16:30:25 +08001797
Liu Bob9ca0662012-06-29 03:58:49 -06001798 if (copy_from_user(&flags, arg, sizeof(flags))) {
1799 ret = -EFAULT;
1800 goto out_drop_write;
1801 }
Li Zefan0caa1022010-12-20 16:30:25 +08001802
Liu Bob9ca0662012-06-29 03:58:49 -06001803 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1804 ret = -EINVAL;
1805 goto out_drop_write;
1806 }
Li Zefan0caa1022010-12-20 16:30:25 +08001807
Liu Bob9ca0662012-06-29 03:58:49 -06001808 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1809 ret = -EOPNOTSUPP;
1810 goto out_drop_write;
1811 }
Li Zefan0caa1022010-12-20 16:30:25 +08001812
1813 down_write(&root->fs_info->subvol_sem);
1814
1815 /* nothing to do */
1816 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001817 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001818
1819 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001820 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001821 btrfs_set_root_flags(&root->root_item,
1822 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001823 } else {
1824 /*
1825 * Block RO -> RW transition if this subvolume is involved in
1826 * send
1827 */
1828 spin_lock(&root->root_item_lock);
1829 if (root->send_in_progress == 0) {
1830 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001831 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001832 spin_unlock(&root->root_item_lock);
1833 } else {
1834 spin_unlock(&root->root_item_lock);
1835 btrfs_warn(root->fs_info,
1836 "Attempt to set subvolume %llu read-write during send",
1837 root->root_key.objectid);
1838 ret = -EPERM;
1839 goto out_drop_sem;
1840 }
1841 }
Li Zefan0caa1022010-12-20 16:30:25 +08001842
1843 trans = btrfs_start_transaction(root, 1);
1844 if (IS_ERR(trans)) {
1845 ret = PTR_ERR(trans);
1846 goto out_reset;
1847 }
1848
Li Zefanb4dc2b82011-02-16 06:06:34 +00001849 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001850 &root->root_key, &root->root_item);
1851
1852 btrfs_commit_transaction(trans, root);
1853out_reset:
1854 if (ret)
1855 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001856out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001857 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001858out_drop_write:
1859 mnt_drop_write_file(file);
1860out:
Li Zefan0caa1022010-12-20 16:30:25 +08001861 return ret;
1862}
1863
Yan, Zheng76dda932009-09-21 16:00:26 -04001864/*
1865 * helper to check if the subvolume references other subvolumes
1866 */
1867static noinline int may_destroy_subvol(struct btrfs_root *root)
1868{
1869 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001870 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001871 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001872 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001873 int ret;
1874
1875 path = btrfs_alloc_path();
1876 if (!path)
1877 return -ENOMEM;
1878
Josef Bacik175a2b82013-08-12 15:36:44 -04001879 /* Make sure this root isn't set as the default subvol */
1880 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1881 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1882 dir_id, "default", 7, 0);
1883 if (di && !IS_ERR(di)) {
1884 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1885 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001886 ret = -EPERM;
1887 btrfs_err(root->fs_info, "deleting default subvolume "
1888 "%llu is not allowed", key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001889 goto out;
1890 }
1891 btrfs_release_path(path);
1892 }
1893
Yan, Zheng76dda932009-09-21 16:00:26 -04001894 key.objectid = root->root_key.objectid;
1895 key.type = BTRFS_ROOT_REF_KEY;
1896 key.offset = (u64)-1;
1897
1898 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1899 &key, path, 0, 0);
1900 if (ret < 0)
1901 goto out;
1902 BUG_ON(ret == 0);
1903
1904 ret = 0;
1905 if (path->slots[0] > 0) {
1906 path->slots[0]--;
1907 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1908 if (key.objectid == root->root_key.objectid &&
1909 key.type == BTRFS_ROOT_REF_KEY)
1910 ret = -ENOTEMPTY;
1911 }
1912out:
1913 btrfs_free_path(path);
1914 return ret;
1915}
1916
Chris Masonac8e9812010-02-28 15:39:26 -05001917static noinline int key_in_sk(struct btrfs_key *key,
1918 struct btrfs_ioctl_search_key *sk)
1919{
Chris Masonabc6e132010-03-18 12:10:08 -04001920 struct btrfs_key test;
1921 int ret;
1922
1923 test.objectid = sk->min_objectid;
1924 test.type = sk->min_type;
1925 test.offset = sk->min_offset;
1926
1927 ret = btrfs_comp_cpu_keys(key, &test);
1928 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001929 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001930
1931 test.objectid = sk->max_objectid;
1932 test.type = sk->max_type;
1933 test.offset = sk->max_offset;
1934
1935 ret = btrfs_comp_cpu_keys(key, &test);
1936 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001937 return 0;
1938 return 1;
1939}
1940
1941static noinline int copy_to_sk(struct btrfs_root *root,
1942 struct btrfs_path *path,
1943 struct btrfs_key *key,
1944 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001945 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001946 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001947 unsigned long *sk_offset,
1948 int *num_found)
1949{
1950 u64 found_transid;
1951 struct extent_buffer *leaf;
1952 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09001953 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05001954 unsigned long item_off;
1955 unsigned long item_len;
1956 int nritems;
1957 int i;
1958 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001959 int ret = 0;
1960
1961 leaf = path->nodes[0];
1962 slot = path->slots[0];
1963 nritems = btrfs_header_nritems(leaf);
1964
1965 if (btrfs_header_generation(leaf) > sk->max_transid) {
1966 i = nritems;
1967 goto advance_key;
1968 }
1969 found_transid = btrfs_header_generation(leaf);
1970
1971 for (i = slot; i < nritems; i++) {
1972 item_off = btrfs_item_ptr_offset(leaf, i);
1973 item_len = btrfs_item_size_nr(leaf, i);
1974
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001975 btrfs_item_key_to_cpu(leaf, key, i);
1976 if (!key_in_sk(key, sk))
1977 continue;
1978
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001979 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001980 if (*num_found) {
1981 ret = 1;
1982 goto out;
1983 }
Chris Masonac8e9812010-02-28 15:39:26 -05001984
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001985 /*
1986 * return one empty item back for v1, which does not
1987 * handle -EOVERFLOW
1988 */
1989
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001990 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001991 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001992 ret = -EOVERFLOW;
1993 }
Chris Masonac8e9812010-02-28 15:39:26 -05001994
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001995 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05001996 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001997 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001998 }
1999
Chris Masonac8e9812010-02-28 15:39:26 -05002000 sh.objectid = key->objectid;
2001 sh.offset = key->offset;
2002 sh.type = key->type;
2003 sh.len = item_len;
2004 sh.transid = found_transid;
2005
2006 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002007 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2008 ret = -EFAULT;
2009 goto out;
2010 }
2011
Chris Masonac8e9812010-02-28 15:39:26 -05002012 *sk_offset += sizeof(sh);
2013
2014 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002015 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002016 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002017 if (read_extent_buffer_to_user(leaf, up,
2018 item_off, item_len)) {
2019 ret = -EFAULT;
2020 goto out;
2021 }
2022
Chris Masonac8e9812010-02-28 15:39:26 -05002023 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002024 }
Hugo Millse2156862011-05-14 17:43:41 +00002025 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002026
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002027 if (ret) /* -EOVERFLOW from above */
2028 goto out;
2029
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002030 if (*num_found >= sk->nr_items) {
2031 ret = 1;
2032 goto out;
2033 }
Chris Masonac8e9812010-02-28 15:39:26 -05002034 }
2035advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002036 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002037 test.objectid = sk->max_objectid;
2038 test.type = sk->max_type;
2039 test.offset = sk->max_offset;
2040 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2041 ret = 1;
2042 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002043 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002044 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002045 key->offset = 0;
2046 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002047 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002048 key->offset = 0;
2049 key->type = 0;
2050 key->objectid++;
2051 } else
2052 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002053out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002054 /*
2055 * 0: all items from this leaf copied, continue with next
2056 * 1: * more items can be copied, but unused buffer is too small
2057 * * all items were found
2058 * Either way, it will stops the loop which iterates to the next
2059 * leaf
2060 * -EOVERFLOW: item was to large for buffer
2061 * -EFAULT: could not copy extent buffer back to userspace
2062 */
Chris Masonac8e9812010-02-28 15:39:26 -05002063 return ret;
2064}
2065
2066static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002067 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002068 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002069 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002070{
2071 struct btrfs_root *root;
2072 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002073 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002074 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2075 int ret;
2076 int num_found = 0;
2077 unsigned long sk_offset = 0;
2078
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002079 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2080 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002081 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002082 }
Gerhard Heift12544442014-01-30 16:23:58 +01002083
Chris Masonac8e9812010-02-28 15:39:26 -05002084 path = btrfs_alloc_path();
2085 if (!path)
2086 return -ENOMEM;
2087
2088 if (sk->tree_id == 0) {
2089 /* search the root of the inode that was passed */
2090 root = BTRFS_I(inode)->root;
2091 } else {
2092 key.objectid = sk->tree_id;
2093 key.type = BTRFS_ROOT_ITEM_KEY;
2094 key.offset = (u64)-1;
2095 root = btrfs_read_fs_root_no_name(info, &key);
2096 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002097 btrfs_free_path(path);
2098 return -ENOENT;
2099 }
2100 }
2101
2102 key.objectid = sk->min_objectid;
2103 key.type = sk->min_type;
2104 key.offset = sk->min_offset;
2105
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302106 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002107 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002108 if (ret != 0) {
2109 if (ret > 0)
2110 ret = 0;
2111 goto err;
2112 }
Gerhard Heiftba346b32014-01-30 16:24:02 +01002113 ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002114 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002115 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002116 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002117 break;
2118
2119 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002120 if (ret > 0)
2121 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002122err:
2123 sk->nr_items = num_found;
2124 btrfs_free_path(path);
2125 return ret;
2126}
2127
2128static noinline int btrfs_ioctl_tree_search(struct file *file,
2129 void __user *argp)
2130{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002131 struct btrfs_ioctl_search_args __user *uargs;
2132 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002133 struct inode *inode;
2134 int ret;
2135 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002136
2137 if (!capable(CAP_SYS_ADMIN))
2138 return -EPERM;
2139
Gerhard Heiftba346b32014-01-30 16:24:02 +01002140 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002141
Gerhard Heiftba346b32014-01-30 16:24:02 +01002142 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2143 return -EFAULT;
2144
2145 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002146
Al Viro496ad9a2013-01-23 17:07:38 -05002147 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002148 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002149
2150 /*
2151 * In the origin implementation an overflow is handled by returning a
2152 * search header with a len of zero, so reset ret.
2153 */
2154 if (ret == -EOVERFLOW)
2155 ret = 0;
2156
Gerhard Heiftba346b32014-01-30 16:24:02 +01002157 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002158 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002159 return ret;
2160}
2161
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002162static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2163 void __user *argp)
2164{
2165 struct btrfs_ioctl_search_args_v2 __user *uarg;
2166 struct btrfs_ioctl_search_args_v2 args;
2167 struct inode *inode;
2168 int ret;
2169 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002170 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002171
2172 if (!capable(CAP_SYS_ADMIN))
2173 return -EPERM;
2174
2175 /* copy search header and buffer size */
2176 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2177 if (copy_from_user(&args, uarg, sizeof(args)))
2178 return -EFAULT;
2179
2180 buf_size = args.buf_size;
2181
2182 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2183 return -EOVERFLOW;
2184
2185 /* limit result size to 16MB */
2186 if (buf_size > buf_limit)
2187 buf_size = buf_limit;
2188
2189 inode = file_inode(file);
2190 ret = search_ioctl(inode, &args.key, &buf_size,
2191 (char *)(&uarg->buf[0]));
2192 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2193 ret = -EFAULT;
2194 else if (ret == -EOVERFLOW &&
2195 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2196 ret = -EFAULT;
2197
Yan, Zheng76dda932009-09-21 16:00:26 -04002198 return ret;
2199}
2200
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002201/*
Chris Masonac8e9812010-02-28 15:39:26 -05002202 * Search INODE_REFs to identify path name of 'dirid' directory
2203 * in a 'tree_id' tree. and sets path name to 'name'.
2204 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002205static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2206 u64 tree_id, u64 dirid, char *name)
2207{
2208 struct btrfs_root *root;
2209 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002210 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002211 int ret = -1;
2212 int slot;
2213 int len;
2214 int total_len = 0;
2215 struct btrfs_inode_ref *iref;
2216 struct extent_buffer *l;
2217 struct btrfs_path *path;
2218
2219 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2220 name[0]='\0';
2221 return 0;
2222 }
2223
2224 path = btrfs_alloc_path();
2225 if (!path)
2226 return -ENOMEM;
2227
Chris Masonac8e9812010-02-28 15:39:26 -05002228 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002229
2230 key.objectid = tree_id;
2231 key.type = BTRFS_ROOT_ITEM_KEY;
2232 key.offset = (u64)-1;
2233 root = btrfs_read_fs_root_no_name(info, &key);
2234 if (IS_ERR(root)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002235 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002236 ret = -ENOENT;
2237 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002238 }
2239
2240 key.objectid = dirid;
2241 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002242 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002243
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302244 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002245 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2246 if (ret < 0)
2247 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002248 else if (ret > 0) {
2249 ret = btrfs_previous_item(root, path, dirid,
2250 BTRFS_INODE_REF_KEY);
2251 if (ret < 0)
2252 goto out;
2253 else if (ret > 0) {
2254 ret = -ENOENT;
2255 goto out;
2256 }
2257 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002258
2259 l = path->nodes[0];
2260 slot = path->slots[0];
2261 btrfs_item_key_to_cpu(l, &key, slot);
2262
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002263 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2264 len = btrfs_inode_ref_name_len(l, iref);
2265 ptr -= len + 1;
2266 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002267 if (ptr < name) {
2268 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002269 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002270 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002271
2272 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302273 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002274
2275 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2276 break;
2277
David Sterbab3b4aa72011-04-21 01:20:15 +02002278 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002279 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002280 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002281 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002282 }
Li Zefan77906a502011-07-14 03:16:00 +00002283 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302284 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002285 ret = 0;
2286out:
2287 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002288 return ret;
2289}
2290
2291static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2292 void __user *argp)
2293{
2294 struct btrfs_ioctl_ino_lookup_args *args;
2295 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002296 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002297
Julia Lawall2354d082010-10-29 15:14:18 -04002298 args = memdup_user(argp, sizeof(*args));
2299 if (IS_ERR(args))
2300 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002301
Al Viro496ad9a2013-01-23 17:07:38 -05002302 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002303
David Sterba01b810b2015-05-12 19:14:49 +02002304 /*
2305 * Unprivileged query to obtain the containing subvolume root id. The
2306 * path is reset so it's consistent with btrfs_search_path_in_tree.
2307 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002308 if (args->treeid == 0)
2309 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2310
David Sterba01b810b2015-05-12 19:14:49 +02002311 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2312 args->name[0] = 0;
2313 goto out;
2314 }
2315
2316 if (!capable(CAP_SYS_ADMIN)) {
2317 ret = -EPERM;
2318 goto out;
2319 }
2320
Chris Masonac8e9812010-02-28 15:39:26 -05002321 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2322 args->treeid, args->objectid,
2323 args->name);
2324
David Sterba01b810b2015-05-12 19:14:49 +02002325out:
Chris Masonac8e9812010-02-28 15:39:26 -05002326 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2327 ret = -EFAULT;
2328
2329 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002330 return ret;
2331}
2332
Yan, Zheng76dda932009-09-21 16:00:26 -04002333static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2334 void __user *arg)
2335{
Al Viro54563d42013-09-01 15:57:51 -04002336 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002337 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002338 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002339 struct inode *inode;
2340 struct btrfs_root *root = BTRFS_I(dir)->root;
2341 struct btrfs_root *dest = NULL;
2342 struct btrfs_ioctl_vol_args *vol_args;
2343 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002344 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002345 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002346 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002347 int namelen;
2348 int ret;
2349 int err = 0;
2350
Yan, Zheng76dda932009-09-21 16:00:26 -04002351 vol_args = memdup_user(arg, sizeof(*vol_args));
2352 if (IS_ERR(vol_args))
2353 return PTR_ERR(vol_args);
2354
2355 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2356 namelen = strlen(vol_args->name);
2357 if (strchr(vol_args->name, '/') ||
2358 strncmp(vol_args->name, "..", namelen) == 0) {
2359 err = -EINVAL;
2360 goto out;
2361 }
2362
Al Viroa561be72011-11-23 11:57:51 -05002363 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002364 if (err)
2365 goto out;
2366
David Sterba521e0542014-04-15 16:41:44 +02002367
David Sterba5c50c9b2013-03-22 18:12:51 +00002368 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2369 if (err == -EINTR)
David Sterbae43f9982013-12-06 17:51:32 +01002370 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002371 dentry = lookup_one_len(vol_args->name, parent, namelen);
2372 if (IS_ERR(dentry)) {
2373 err = PTR_ERR(dentry);
2374 goto out_unlock_dir;
2375 }
2376
David Howells2b0143b2015-03-17 22:25:59 +00002377 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002378 err = -ENOENT;
2379 goto out_dput;
2380 }
2381
David Howells2b0143b2015-03-17 22:25:59 +00002382 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002383 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302384 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002385 /*
2386 * Regular user. Only allow this with a special mount
2387 * option, when the user has write+exec access to the
2388 * subvol root, and when rmdir(2) would have been
2389 * allowed.
2390 *
2391 * Note that this is _not_ check that the subvol is
2392 * empty or doesn't contain data that we wouldn't
2393 * otherwise be able to delete.
2394 *
2395 * Users who want to delete empty subvols should try
2396 * rmdir(2).
2397 */
2398 err = -EPERM;
2399 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2400 goto out_dput;
2401
2402 /*
2403 * Do not allow deletion if the parent dir is the same
2404 * as the dir to be deleted. That means the ioctl
2405 * must be called on the dentry referencing the root
2406 * of the subvol, not a random directory contained
2407 * within it.
2408 */
2409 err = -EINVAL;
2410 if (root == dest)
2411 goto out_dput;
2412
2413 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2414 if (err)
2415 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002416 }
2417
Miao Xie5c39da52012-10-22 11:39:53 +00002418 /* check if subvolume may be deleted by a user */
2419 err = btrfs_may_delete(dir, dentry, 1);
2420 if (err)
2421 goto out_dput;
2422
Li Zefan33345d012011-04-20 10:31:50 +08002423 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002424 err = -EINVAL;
2425 goto out_dput;
2426 }
2427
Al Viro59551022016-01-22 15:40:57 -05002428 inode_lock(inode);
David Sterba521e0542014-04-15 16:41:44 +02002429
2430 /*
2431 * Don't allow to delete a subvolume with send in progress. This is
2432 * inside the i_mutex so the error handling that has to drop the bit
2433 * again is not run concurrently.
2434 */
2435 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002436 root_flags = btrfs_root_flags(&dest->root_item);
2437 if (dest->send_in_progress == 0) {
2438 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002439 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2440 spin_unlock(&dest->root_item_lock);
2441 } else {
2442 spin_unlock(&dest->root_item_lock);
2443 btrfs_warn(root->fs_info,
2444 "Attempt to delete subvolume %llu during send",
Filipe Mananac55bfa62014-05-25 03:55:44 +01002445 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002446 err = -EPERM;
Omar Sandoval909e26d2015-04-10 14:20:40 -07002447 goto out_unlock_inode;
David Sterba521e0542014-04-15 16:41:44 +02002448 }
2449
Yan, Zheng76dda932009-09-21 16:00:26 -04002450 down_write(&root->fs_info->subvol_sem);
2451
2452 err = may_destroy_subvol(dest);
2453 if (err)
2454 goto out_up_write;
2455
Miao Xiec58aaad2013-02-28 10:05:36 +00002456 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2457 /*
2458 * One for dir inode, two for dir entries, two for root
2459 * ref/backref.
2460 */
2461 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002462 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002463 if (err)
2464 goto out_up_write;
2465
Yan, Zhenga22285a2010-05-16 10:48:46 -04002466 trans = btrfs_start_transaction(root, 0);
2467 if (IS_ERR(trans)) {
2468 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002469 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002470 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002471 trans->block_rsv = &block_rsv;
2472 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002473
Filipe Manana2be63d52016-02-12 11:34:23 +00002474 btrfs_record_snapshot_destroy(trans, dir);
2475
Yan, Zheng76dda932009-09-21 16:00:26 -04002476 ret = btrfs_unlink_subvol(trans, root, dir,
2477 dest->root_key.objectid,
2478 dentry->d_name.name,
2479 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002480 if (ret) {
2481 err = ret;
2482 btrfs_abort_transaction(trans, root, ret);
2483 goto out_end_trans;
2484 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002485
2486 btrfs_record_root_in_trans(trans, dest);
2487
2488 memset(&dest->root_item.drop_progress, 0,
2489 sizeof(dest->root_item.drop_progress));
2490 dest->root_item.drop_level = 0;
2491 btrfs_set_root_refs(&dest->root_item, 0);
2492
Miao Xie27cdeb72014-04-02 19:51:05 +08002493 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002494 ret = btrfs_insert_orphan_item(trans,
2495 root->fs_info->tree_root,
2496 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002497 if (ret) {
2498 btrfs_abort_transaction(trans, root, ret);
2499 err = ret;
2500 goto out_end_trans;
2501 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002502 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002503
2504 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2505 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2506 dest->root_key.objectid);
2507 if (ret && ret != -ENOENT) {
2508 btrfs_abort_transaction(trans, root, ret);
2509 err = ret;
2510 goto out_end_trans;
2511 }
2512 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2513 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2514 dest->root_item.received_uuid,
2515 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2516 dest->root_key.objectid);
2517 if (ret && ret != -ENOENT) {
2518 btrfs_abort_transaction(trans, root, ret);
2519 err = ret;
2520 goto out_end_trans;
2521 }
2522 }
2523
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002524out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002525 trans->block_rsv = NULL;
2526 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002527 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002528 if (ret && !err)
2529 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002530 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002531out_release:
2532 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002533out_up_write:
2534 up_write(&root->fs_info->subvol_sem);
David Sterba521e0542014-04-15 16:41:44 +02002535 if (err) {
2536 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002537 root_flags = btrfs_root_flags(&dest->root_item);
2538 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002539 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2540 spin_unlock(&dest->root_item_lock);
2541 }
Omar Sandoval909e26d2015-04-10 14:20:40 -07002542out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -05002543 inode_unlock(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04002544 if (!err) {
Omar Sandoval64ad6c42015-06-02 17:31:00 -07002545 d_invalidate(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002546 btrfs_invalidate_inodes(dest);
2547 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002548 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002549
2550 /* the last ref */
David Sterba57cdc8d2014-02-05 02:37:48 +01002551 if (dest->ino_cache_inode) {
2552 iput(dest->ino_cache_inode);
2553 dest->ino_cache_inode = NULL;
Liu Bofa6ac872013-02-20 14:10:23 +00002554 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002555 }
2556out_dput:
2557 dput(dentry);
2558out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002559 inode_unlock(dir);
David Sterbae43f9982013-12-06 17:51:32 +01002560out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002561 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002562out:
2563 kfree(vol_args);
2564 return err;
2565}
2566
Chris Mason1e701a32010-03-11 09:42:04 -05002567static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002568{
Al Viro496ad9a2013-01-23 17:07:38 -05002569 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002570 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002571 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002572 int ret;
2573
Ilya Dryomov25122d12013-01-20 15:57:57 +02002574 ret = mnt_want_write_file(file);
2575 if (ret)
2576 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002577
Ilya Dryomov25122d12013-01-20 15:57:57 +02002578 if (btrfs_root_readonly(root)) {
2579 ret = -EROFS;
2580 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002581 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002582
2583 switch (inode->i_mode & S_IFMT) {
2584 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002585 if (!capable(CAP_SYS_ADMIN)) {
2586 ret = -EPERM;
2587 goto out;
2588 }
Eric Sandeende78b512013-01-31 18:21:12 +00002589 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002590 if (ret)
2591 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002592 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002593 break;
2594 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002595 if (!(file->f_mode & FMODE_WRITE)) {
2596 ret = -EINVAL;
2597 goto out;
2598 }
Chris Mason1e701a32010-03-11 09:42:04 -05002599
2600 range = kzalloc(sizeof(*range), GFP_KERNEL);
2601 if (!range) {
2602 ret = -ENOMEM;
2603 goto out;
2604 }
2605
2606 if (argp) {
2607 if (copy_from_user(range, argp,
2608 sizeof(*range))) {
2609 ret = -EFAULT;
2610 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002611 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002612 }
2613 /* compression requires us to start the IO */
2614 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2615 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2616 range->extent_thresh = (u32)-1;
2617 }
2618 } else {
2619 /* the rest are all set to zero by kzalloc */
2620 range->len = (u64)-1;
2621 }
Al Viro496ad9a2013-01-23 17:07:38 -05002622 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002623 range, 0, 0);
2624 if (ret > 0)
2625 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002626 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002627 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002628 default:
2629 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002630 }
Chris Masone441d542009-01-05 16:57:23 -05002631out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002632 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002633 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002634}
2635
Christoph Hellwigb2950862008-12-02 09:54:17 -05002636static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002637{
2638 struct btrfs_ioctl_vol_args *vol_args;
2639 int ret;
2640
Chris Masone441d542009-01-05 16:57:23 -05002641 if (!capable(CAP_SYS_ADMIN))
2642 return -EPERM;
2643
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002644 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2645 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002646 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002647 }
2648
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002649 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002650 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002651 if (IS_ERR(vol_args)) {
2652 ret = PTR_ERR(vol_args);
2653 goto out;
2654 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002655
Mark Fasheh5516e592008-07-24 12:20:14 -04002656 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002657 ret = btrfs_init_new_device(root, vol_args->name);
2658
Anand Jain43d20762014-07-01 00:58:56 +08002659 if (!ret)
2660 btrfs_info(root->fs_info, "disk added %s",vol_args->name);
2661
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002662 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002663out:
2664 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002665 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002666 return ret;
2667}
2668
Miao Xieda249272012-11-26 08:44:50 +00002669static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002670{
Al Viro496ad9a2013-01-23 17:07:38 -05002671 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002672 struct btrfs_ioctl_vol_args *vol_args;
2673 int ret;
2674
Chris Masone441d542009-01-05 16:57:23 -05002675 if (!capable(CAP_SYS_ADMIN))
2676 return -EPERM;
2677
Miao Xieda249272012-11-26 08:44:50 +00002678 ret = mnt_want_write_file(file);
2679 if (ret)
2680 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002681
Li Zefandae7b662009-04-08 15:06:54 +08002682 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002683 if (IS_ERR(vol_args)) {
2684 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002685 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002686 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002687
Mark Fasheh5516e592008-07-24 12:20:14 -04002688 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002689
Anand Jain183860f2013-05-17 10:52:45 +00002690 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2691 1)) {
2692 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2693 goto out;
2694 }
2695
2696 mutex_lock(&root->fs_info->volume_mutex);
2697 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002698 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002699 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002700
Anand Jainec95d492014-07-01 00:58:57 +08002701 if (!ret)
2702 btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
2703
Anand Jain183860f2013-05-17 10:52:45 +00002704out:
2705 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002706err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002707 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002708 return ret;
2709}
2710
Jan Schmidt475f6382011-03-11 15:41:01 +01002711static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2712{
Li Zefan027ed2f2011-06-08 08:27:56 +00002713 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002714 struct btrfs_device *device;
Jan Schmidt475f6382011-03-11 15:41:01 +01002715 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002716 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002717
Li Zefan027ed2f2011-06-08 08:27:56 +00002718 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2719 if (!fi_args)
2720 return -ENOMEM;
2721
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002722 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002723 fi_args->num_devices = fs_devices->num_devices;
2724 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002725
Byongho Leed7641a42015-09-01 23:10:57 +09002726 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002727 if (device->devid > fi_args->max_id)
2728 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002729 }
2730 mutex_unlock(&fs_devices->device_list_mutex);
2731
David Sterba80a773f2014-05-07 18:17:06 +02002732 fi_args->nodesize = root->fs_info->super_copy->nodesize;
2733 fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
2734 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
2735
Li Zefan027ed2f2011-06-08 08:27:56 +00002736 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2737 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002738
Li Zefan027ed2f2011-06-08 08:27:56 +00002739 kfree(fi_args);
2740 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002741}
2742
2743static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2744{
2745 struct btrfs_ioctl_dev_info_args *di_args;
2746 struct btrfs_device *dev;
2747 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2748 int ret = 0;
2749 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002750
Jan Schmidt475f6382011-03-11 15:41:01 +01002751 di_args = memdup_user(arg, sizeof(*di_args));
2752 if (IS_ERR(di_args))
2753 return PTR_ERR(di_args);
2754
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002755 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002756 s_uuid = di_args->uuid;
2757
2758 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002759 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002760
2761 if (!dev) {
2762 ret = -ENODEV;
2763 goto out;
2764 }
2765
2766 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002767 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2768 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002769 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002770 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002771 struct rcu_string *name;
2772
2773 rcu_read_lock();
2774 name = rcu_dereference(dev->name);
2775 strncpy(di_args->path, name->str, sizeof(di_args->path));
2776 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002777 di_args->path[sizeof(di_args->path) - 1] = 0;
2778 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002779 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002780 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002781
2782out:
David Sterba55793c02013-04-26 15:20:23 +00002783 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002784 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2785 ret = -EFAULT;
2786
2787 kfree(di_args);
2788 return ret;
2789}
2790
Mark Fashehf4414602015-06-30 14:42:05 -07002791static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002792{
2793 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002794
Mark Fasheh416161d2013-08-06 11:42:51 -07002795 page = grab_cache_page(inode->i_mapping, index);
2796 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002797 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002798
2799 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002800 int ret;
2801
2802 ret = btrfs_readpage(NULL, page);
2803 if (ret)
2804 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002805 lock_page(page);
2806 if (!PageUptodate(page)) {
2807 unlock_page(page);
2808 page_cache_release(page);
Filipe Manana31314002016-01-27 18:37:47 +00002809 return ERR_PTR(-EIO);
2810 }
2811 if (page->mapping != inode->i_mapping) {
2812 unlock_page(page);
2813 page_cache_release(page);
2814 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002815 }
2816 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002817
2818 return page;
2819}
2820
Mark Fashehf4414602015-06-30 14:42:05 -07002821static int gather_extent_pages(struct inode *inode, struct page **pages,
2822 int num_pages, u64 off)
2823{
2824 int i;
2825 pgoff_t index = off >> PAGE_CACHE_SHIFT;
2826
2827 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002828again:
Mark Fashehf4414602015-06-30 14:42:05 -07002829 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002830 if (IS_ERR(pages[i])) {
2831 int err = PTR_ERR(pages[i]);
2832
2833 if (err == -EAGAIN)
2834 goto again;
2835 pages[i] = NULL;
2836 return err;
2837 }
Mark Fashehf4414602015-06-30 14:42:05 -07002838 }
2839 return 0;
2840}
2841
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002842static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2843 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002844{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002845 /*
2846 * Do any pending delalloc/csum calculations on inode, one way or
2847 * another, and lock file content.
2848 * The locking order is:
2849 *
2850 * 1) pages
2851 * 2) range in the inode's io tree
2852 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002853 while (1) {
2854 struct btrfs_ordered_extent *ordered;
2855 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2856 ordered = btrfs_lookup_first_ordered_extent(inode,
2857 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002858 if ((!ordered ||
2859 ordered->file_offset + ordered->len <= off ||
2860 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002861 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002862 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2863 if (ordered)
2864 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002865 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002866 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002867 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2868 if (ordered)
2869 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002870 if (!retry_range_locking)
2871 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002872 btrfs_wait_ordered_range(inode, off, len);
2873 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002874 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002875}
2876
Mark Fashehf4414602015-06-30 14:42:05 -07002877static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002878{
Al Viro59551022016-01-22 15:40:57 -05002879 inode_unlock(inode1);
2880 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002881}
2882
Mark Fashehf4414602015-06-30 14:42:05 -07002883static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2884{
2885 if (inode1 < inode2)
2886 swap(inode1, inode2);
2887
Al Viro59551022016-01-22 15:40:57 -05002888 inode_lock_nested(inode1, I_MUTEX_PARENT);
2889 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002890}
2891
2892static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2893 struct inode *inode2, u64 loff2, u64 len)
2894{
2895 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2896 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2897}
2898
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002899static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2900 struct inode *inode2, u64 loff2, u64 len,
2901 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002902{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002903 int ret;
2904
Mark Fasheh416161d2013-08-06 11:42:51 -07002905 if (inode1 < inode2) {
2906 swap(inode1, inode2);
2907 swap(loff1, loff2);
2908 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002909 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2910 if (ret)
2911 return ret;
2912 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2913 if (ret)
2914 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2915 loff1 + len - 1);
2916 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002917}
2918
2919struct cmp_pages {
2920 int num_pages;
2921 struct page **src_pages;
2922 struct page **dst_pages;
2923};
2924
2925static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2926{
2927 int i;
2928 struct page *pg;
2929
2930 for (i = 0; i < cmp->num_pages; i++) {
2931 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002932 if (pg) {
2933 unlock_page(pg);
Mark Fashehf4414602015-06-30 14:42:05 -07002934 page_cache_release(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002935 }
Mark Fashehf4414602015-06-30 14:42:05 -07002936 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002937 if (pg) {
2938 unlock_page(pg);
Mark Fashehf4414602015-06-30 14:42:05 -07002939 page_cache_release(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002940 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002941 }
Mark Fashehf4414602015-06-30 14:42:05 -07002942 kfree(cmp->src_pages);
2943 kfree(cmp->dst_pages);
2944}
2945
2946static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2947 struct inode *dst, u64 dst_loff,
2948 u64 len, struct cmp_pages *cmp)
2949{
2950 int ret;
2951 int num_pages = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
2952 struct page **src_pgarr, **dst_pgarr;
2953
2954 /*
2955 * We must gather up all the pages before we initiate our
2956 * extent locking. We use an array for the page pointers. Size
2957 * of the array is bounded by len, which is in turn bounded by
2958 * BTRFS_MAX_DEDUPE_LEN.
2959 */
David Sterba66722f72016-02-11 15:01:38 +01002960 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
2961 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07002962 if (!src_pgarr || !dst_pgarr) {
2963 kfree(src_pgarr);
2964 kfree(dst_pgarr);
2965 return -ENOMEM;
2966 }
2967 cmp->num_pages = num_pages;
2968 cmp->src_pages = src_pgarr;
2969 cmp->dst_pages = dst_pgarr;
2970
2971 ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff);
2972 if (ret)
2973 goto out;
2974
2975 ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff);
2976
2977out:
2978 if (ret)
2979 btrfs_cmp_data_free(cmp);
2980 return 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002981}
2982
2983static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
Mark Fashehf4414602015-06-30 14:42:05 -07002984 u64 dst_loff, u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002985{
2986 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07002987 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07002988 struct page *src_page, *dst_page;
2989 unsigned int cmp_len = PAGE_CACHE_SIZE;
2990 void *addr, *dst_addr;
2991
Mark Fashehf4414602015-06-30 14:42:05 -07002992 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002993 while (len) {
2994 if (len < PAGE_CACHE_SIZE)
2995 cmp_len = len;
2996
Mark Fashehf4414602015-06-30 14:42:05 -07002997 BUG_ON(i >= cmp->num_pages);
2998
2999 src_page = cmp->src_pages[i];
3000 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003001 ASSERT(PageLocked(src_page));
3002 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003003
Mark Fasheh416161d2013-08-06 11:42:51 -07003004 addr = kmap_atomic(src_page);
3005 dst_addr = kmap_atomic(dst_page);
3006
3007 flush_dcache_page(src_page);
3008 flush_dcache_page(dst_page);
3009
3010 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003011 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003012
3013 kunmap_atomic(addr);
3014 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003015
3016 if (ret)
3017 break;
3018
Mark Fasheh416161d2013-08-06 11:42:51 -07003019 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003020 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003021 }
3022
3023 return ret;
3024}
3025
Mark Fashehe1d227a2015-06-08 15:05:25 -07003026static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3027 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003028{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003029 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003030 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3031
Mark Fashehe1d227a2015-06-08 15:05:25 -07003032 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003033 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003034
3035 /* if we extend to eof, continue to block boundary */
3036 if (off + len == inode->i_size)
3037 *plen = len = ALIGN(inode->i_size, bs) - off;
3038
Mark Fasheh416161d2013-08-06 11:42:51 -07003039 /* Check that we are block aligned - btrfs_clone() requires this */
3040 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3041 return -EINVAL;
3042
3043 return 0;
3044}
3045
Mark Fashehe1d227a2015-06-08 15:05:25 -07003046static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
Mark Fasheh416161d2013-08-06 11:42:51 -07003047 struct inode *dst, u64 dst_loff)
3048{
3049 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003050 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003051 struct cmp_pages cmp;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003052 int same_inode = 0;
3053 u64 same_lock_start = 0;
3054 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003055
Mark Fasheh416161d2013-08-06 11:42:51 -07003056 if (src == dst)
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003057 same_inode = 1;
Mark Fasheh416161d2013-08-06 11:42:51 -07003058
Filipe Manana113e8282015-03-30 18:26:47 +01003059 if (len == 0)
3060 return 0;
3061
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003062 if (same_inode) {
Al Viro59551022016-01-22 15:40:57 -05003063 inode_lock(src);
Mark Fasheh416161d2013-08-06 11:42:51 -07003064
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003065 ret = extent_same_check_offsets(src, loff, &len, olen);
3066 if (ret)
3067 goto out_unlock;
Filipe Mananaf4dfe682016-02-12 14:44:00 +00003068 ret = extent_same_check_offsets(src, dst_loff, &len, olen);
3069 if (ret)
3070 goto out_unlock;
Mark Fasheh416161d2013-08-06 11:42:51 -07003071
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003072 /*
3073 * Single inode case wants the same checks, except we
3074 * don't want our length pushed out past i_size as
3075 * comparing that data range makes no sense.
3076 *
3077 * extent_same_check_offsets() will do this for an
3078 * unaligned length at i_size, so catch it here and
3079 * reject the request.
3080 *
3081 * This effectively means we require aligned extents
3082 * for the single-inode case, whereas the other cases
3083 * allow an unaligned length so long as it ends at
3084 * i_size.
3085 */
3086 if (len != olen) {
3087 ret = -EINVAL;
3088 goto out_unlock;
3089 }
3090
3091 /* Check for overlapping ranges */
3092 if (dst_loff + len > loff && dst_loff < loff + len) {
3093 ret = -EINVAL;
3094 goto out_unlock;
3095 }
3096
3097 same_lock_start = min_t(u64, loff, dst_loff);
3098 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
3099 } else {
3100 btrfs_double_inode_lock(src, dst);
3101
3102 ret = extent_same_check_offsets(src, loff, &len, olen);
3103 if (ret)
3104 goto out_unlock;
3105
3106 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3107 if (ret)
3108 goto out_unlock;
3109 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003110
3111 /* don't make the dst file partly checksummed */
3112 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3113 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3114 ret = -EINVAL;
3115 goto out_unlock;
3116 }
3117
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003118again:
Mark Fashehf4414602015-06-30 14:42:05 -07003119 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3120 if (ret)
3121 goto out_unlock;
3122
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003123 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003124 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3125 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003126 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003127 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3128 false);
3129 /*
3130 * If one of the inodes has dirty pages in the respective range or
3131 * ordered extents, we need to flush dellaloc and wait for all ordered
3132 * extents in the range. We must unlock the pages and the ranges in the
3133 * io trees to avoid deadlocks when flushing delalloc (requires locking
3134 * pages) and when waiting for ordered extents to complete (they require
3135 * range locking).
3136 */
3137 if (ret == -EAGAIN) {
3138 /*
3139 * Ranges in the io trees already unlocked. Now unlock all
3140 * pages before waiting for all IO to complete.
3141 */
3142 btrfs_cmp_data_free(&cmp);
3143 if (same_inode) {
3144 btrfs_wait_ordered_range(src, same_lock_start,
3145 same_lock_len);
3146 } else {
3147 btrfs_wait_ordered_range(src, loff, len);
3148 btrfs_wait_ordered_range(dst, dst_loff, len);
3149 }
3150 goto again;
3151 }
3152 ASSERT(ret == 0);
3153 if (WARN_ON(ret)) {
3154 /* ranges in the io trees already unlocked */
3155 btrfs_cmp_data_free(&cmp);
3156 return ret;
3157 }
Mark Fashehf4414602015-06-30 14:42:05 -07003158
Mark Fasheh207910d2015-06-30 14:42:04 -07003159 /* pass original length for comparison so we stay within i_size */
Mark Fashehf4414602015-06-30 14:42:05 -07003160 ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003161 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003162 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003163
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003164 if (same_inode)
3165 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3166 same_lock_start + same_lock_len - 1);
3167 else
3168 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003169
3170 btrfs_cmp_data_free(&cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003171out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003172 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003173 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003174 else
3175 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003176
3177 return ret;
3178}
3179
Byongho Leeee221842015-12-15 01:42:10 +09003180#define BTRFS_MAX_DEDUPE_LEN SZ_16M
Mark Fasheh416161d2013-08-06 11:42:51 -07003181
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003182ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3183 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003184{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003185 struct inode *src = file_inode(src_file);
3186 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003187 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003188 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003189
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003190 if (olen > BTRFS_MAX_DEDUPE_LEN)
3191 olen = BTRFS_MAX_DEDUPE_LEN;
Mark Fasheh416161d2013-08-06 11:42:51 -07003192
3193 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
3194 /*
3195 * Btrfs does not support blocksize < page_size. As a
3196 * result, btrfs_cmp_data() won't correctly handle
3197 * this situation without an update.
3198 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003199 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003200 }
3201
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003202 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3203 if (res)
3204 return res;
3205 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003206}
3207
Filipe Mananaf82a9902014-06-01 01:50:28 +01003208static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3209 struct inode *inode,
3210 u64 endoff,
3211 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003212 const u64 olen,
3213 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003214{
3215 struct btrfs_root *root = BTRFS_I(inode)->root;
3216 int ret;
3217
3218 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003219 if (!no_time_update)
Deepa Dinamani04b285f2016-02-06 23:57:21 -08003220 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003221 /*
3222 * We round up to the block size at eof when determining which
3223 * extents to clone above, but shouldn't round up the file size.
3224 */
3225 if (endoff > destoff + olen)
3226 endoff = destoff + olen;
3227 if (endoff > inode->i_size)
3228 btrfs_i_size_write(inode, endoff);
3229
3230 ret = btrfs_update_inode(trans, root, inode);
3231 if (ret) {
3232 btrfs_abort_transaction(trans, root, ret);
3233 btrfs_end_transaction(trans, root);
3234 goto out;
3235 }
3236 ret = btrfs_end_transaction(trans, root);
3237out:
3238 return ret;
3239}
3240
Filipe Manana7ffbb592014-06-09 03:48:05 +01003241static void clone_update_extent_map(struct inode *inode,
3242 const struct btrfs_trans_handle *trans,
3243 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003244 const u64 hole_offset,
3245 const u64 hole_len)
3246{
3247 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3248 struct extent_map *em;
3249 int ret;
3250
3251 em = alloc_extent_map();
3252 if (!em) {
3253 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3254 &BTRFS_I(inode)->runtime_flags);
3255 return;
3256 }
3257
Filipe Manana14f59792014-06-29 21:45:40 +01003258 if (path) {
3259 struct btrfs_file_extent_item *fi;
3260
3261 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3262 struct btrfs_file_extent_item);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003263 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3264 em->generation = -1;
3265 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3266 BTRFS_FILE_EXTENT_INLINE)
3267 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3268 &BTRFS_I(inode)->runtime_flags);
3269 } else {
3270 em->start = hole_offset;
3271 em->len = hole_len;
3272 em->ram_bytes = em->len;
3273 em->orig_start = hole_offset;
3274 em->block_start = EXTENT_MAP_HOLE;
3275 em->block_len = 0;
3276 em->orig_block_len = 0;
3277 em->compress_type = BTRFS_COMPRESS_NONE;
3278 em->generation = trans->transid;
3279 }
3280
3281 while (1) {
3282 write_lock(&em_tree->lock);
3283 ret = add_extent_mapping(em_tree, em, 1);
3284 write_unlock(&em_tree->lock);
3285 if (ret != -EEXIST) {
3286 free_extent_map(em);
3287 break;
3288 }
3289 btrfs_drop_extent_cache(inode, em->start,
3290 em->start + em->len - 1, 0);
3291 }
3292
David Sterbaee39b432014-09-30 01:33:33 +02003293 if (ret)
Filipe Manana7ffbb592014-06-09 03:48:05 +01003294 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3295 &BTRFS_I(inode)->runtime_flags);
3296}
3297
Filipe Manana8039d872015-10-13 15:15:00 +01003298/*
3299 * Make sure we do not end up inserting an inline extent into a file that has
3300 * already other (non-inline) extents. If a file has an inline extent it can
3301 * not have any other extents and the (single) inline extent must start at the
3302 * file offset 0. Failing to respect these rules will lead to file corruption,
3303 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3304 *
3305 * We can have extents that have been already written to disk or we can have
3306 * dirty ranges still in delalloc, in which case the extent maps and items are
3307 * created only when we run delalloc, and the delalloc ranges might fall outside
3308 * the range we are currently locking in the inode's io tree. So we check the
3309 * inode's i_size because of that (i_size updates are done while holding the
3310 * i_mutex, which we are holding here).
3311 * We also check to see if the inode has a size not greater than "datal" but has
3312 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3313 * protected against such concurrent fallocate calls by the i_mutex).
3314 *
3315 * If the file has no extents but a size greater than datal, do not allow the
3316 * copy because we would need turn the inline extent into a non-inline one (even
3317 * with NO_HOLES enabled). If we find our destination inode only has one inline
3318 * extent, just overwrite it with the source inline extent if its size is less
3319 * than the source extent's size, or we could copy the source inline extent's
3320 * data into the destination inode's inline extent if the later is greater then
3321 * the former.
3322 */
3323static int clone_copy_inline_extent(struct inode *src,
3324 struct inode *dst,
3325 struct btrfs_trans_handle *trans,
3326 struct btrfs_path *path,
3327 struct btrfs_key *new_key,
3328 const u64 drop_start,
3329 const u64 datal,
3330 const u64 skip,
3331 const u64 size,
3332 char *inline_data)
3333{
3334 struct btrfs_root *root = BTRFS_I(dst)->root;
3335 const u64 aligned_end = ALIGN(new_key->offset + datal,
3336 root->sectorsize);
3337 int ret;
3338 struct btrfs_key key;
3339
3340 if (new_key->offset > 0)
3341 return -EOPNOTSUPP;
3342
3343 key.objectid = btrfs_ino(dst);
3344 key.type = BTRFS_EXTENT_DATA_KEY;
3345 key.offset = 0;
3346 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3347 if (ret < 0) {
3348 return ret;
3349 } else if (ret > 0) {
3350 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3351 ret = btrfs_next_leaf(root, path);
3352 if (ret < 0)
3353 return ret;
3354 else if (ret > 0)
3355 goto copy_inline_extent;
3356 }
3357 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3358 if (key.objectid == btrfs_ino(dst) &&
3359 key.type == BTRFS_EXTENT_DATA_KEY) {
3360 ASSERT(key.offset > 0);
3361 return -EOPNOTSUPP;
3362 }
3363 } else if (i_size_read(dst) <= datal) {
3364 struct btrfs_file_extent_item *ei;
3365 u64 ext_len;
3366
3367 /*
3368 * If the file size is <= datal, make sure there are no other
3369 * extents following (can happen do to an fallocate call with
3370 * the flag FALLOC_FL_KEEP_SIZE).
3371 */
3372 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3373 struct btrfs_file_extent_item);
3374 /*
3375 * If it's an inline extent, it can not have other extents
3376 * following it.
3377 */
3378 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3379 BTRFS_FILE_EXTENT_INLINE)
3380 goto copy_inline_extent;
3381
3382 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3383 if (ext_len > aligned_end)
3384 return -EOPNOTSUPP;
3385
3386 ret = btrfs_next_item(root, path);
3387 if (ret < 0) {
3388 return ret;
3389 } else if (ret == 0) {
3390 btrfs_item_key_to_cpu(path->nodes[0], &key,
3391 path->slots[0]);
3392 if (key.objectid == btrfs_ino(dst) &&
3393 key.type == BTRFS_EXTENT_DATA_KEY)
3394 return -EOPNOTSUPP;
3395 }
3396 }
3397
3398copy_inline_extent:
3399 /*
3400 * We have no extent items, or we have an extent at offset 0 which may
3401 * or may not be inlined. All these cases are dealt the same way.
3402 */
3403 if (i_size_read(dst) > datal) {
3404 /*
3405 * If the destination inode has an inline extent...
3406 * This would require copying the data from the source inline
3407 * extent into the beginning of the destination's inline extent.
3408 * But this is really complex, both extents can be compressed
3409 * or just one of them, which would require decompressing and
3410 * re-compressing data (which could increase the new compressed
3411 * size, not allowing the compressed data to fit anymore in an
3412 * inline extent).
3413 * So just don't support this case for now (it should be rare,
3414 * we are not really saving space when cloning inline extents).
3415 */
3416 return -EOPNOTSUPP;
3417 }
3418
3419 btrfs_release_path(path);
3420 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3421 if (ret)
3422 return ret;
3423 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3424 if (ret)
3425 return ret;
3426
3427 if (skip) {
3428 const u32 start = btrfs_file_extent_calc_inline_size(0);
3429
3430 memmove(inline_data + start, inline_data + start + skip, datal);
3431 }
3432
3433 write_extent_buffer(path->nodes[0], inline_data,
3434 btrfs_item_ptr_offset(path->nodes[0],
3435 path->slots[0]),
3436 size);
3437 inode_add_bytes(dst, datal);
3438
3439 return 0;
3440}
3441
Mark Fasheh32b7c682013-08-06 11:42:49 -07003442/**
3443 * btrfs_clone() - clone a range from inode file to another
3444 *
3445 * @src: Inode to clone from
3446 * @inode: Inode to clone to
3447 * @off: Offset within source to start clone from
3448 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003449 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003450 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003451 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003452 */
3453static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003454 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003455 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003456{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003457 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003458 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003459 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003460 struct btrfs_trans_handle *trans;
3461 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003462 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003463 u32 nritems;
3464 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003465 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003466 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003467 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003468
3469 ret = -ENOMEM;
David Sterba707e8a02014-06-04 19:22:26 +02003470 buf = vmalloc(root->nodesize);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003471 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003472 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003473
3474 path = btrfs_alloc_path();
3475 if (!path) {
3476 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003477 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003478 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003479
David Sterbae4058b52015-11-27 16:31:35 +01003480 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003481 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08003482 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003483 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003484 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003485
3486 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003487 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003488
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003489 /*
3490 * note the key will change type as we walk through the
3491 * tree.
3492 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003493 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003494 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3495 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003496 if (ret < 0)
3497 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003498 /*
3499 * First search, if no extent item that starts at offset off was
3500 * found but the previous item is an extent item, it's possible
3501 * it might overlap our target range, therefore process it.
3502 */
3503 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3504 btrfs_item_key_to_cpu(path->nodes[0], &key,
3505 path->slots[0] - 1);
3506 if (key.type == BTRFS_EXTENT_DATA_KEY)
3507 path->slots[0]--;
3508 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003509
Yan Zhengae01a0a2008-08-04 23:23:47 -04003510 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003511process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003512 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003513 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003514 if (ret < 0)
3515 goto out;
3516 if (ret > 0)
3517 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003518 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003519 }
3520 leaf = path->nodes[0];
3521 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003522
Yan Zhengae01a0a2008-08-04 23:23:47 -04003523 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003524 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08003525 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003526 break;
3527
David Sterba962a2982014-06-04 18:41:45 +02003528 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003529 struct btrfs_file_extent_item *extent;
3530 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003531 u32 size;
3532 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003533 u64 disko = 0, diskl = 0;
3534 u64 datao = 0, datal = 0;
3535 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003536 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003537
Sage Weilc5c9cd42008-11-12 14:32:25 -05003538 extent = btrfs_item_ptr(leaf, slot,
3539 struct btrfs_file_extent_item);
3540 comp = btrfs_file_extent_compression(leaf, extent);
3541 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003542 if (type == BTRFS_FILE_EXTENT_REG ||
3543 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003544 disko = btrfs_file_extent_disk_bytenr(leaf,
3545 extent);
3546 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3547 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003548 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003549 datal = btrfs_file_extent_num_bytes(leaf,
3550 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003551 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3552 /* take upper bound, may be compressed */
3553 datal = btrfs_file_extent_ram_bytes(leaf,
3554 extent);
3555 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003556
Filipe Manana2c463822014-05-31 02:31:05 +01003557 /*
3558 * The first search might have left us at an extent
3559 * item that ends before our target range's start, can
3560 * happen if we have holes and NO_HOLES feature enabled.
3561 */
3562 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003563 path->slots[0]++;
3564 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003565 } else if (key.offset >= off + len) {
3566 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003567 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003568 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003569 size = btrfs_item_size_nr(leaf, slot);
3570 read_extent_buffer(leaf, buf,
3571 btrfs_item_ptr_offset(leaf, slot),
3572 size);
3573
3574 btrfs_release_path(path);
3575 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003576
Zheng Yan31840ae2008-09-23 13:14:14 -04003577 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08003578 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08003579 if (off <= key.offset)
3580 new_key.offset = key.offset + destoff - off;
3581 else
3582 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003583
Sage Weilb6f34092011-09-20 14:48:51 -04003584 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003585 * Deal with a hole that doesn't have an extent item
3586 * that represents it (NO_HOLES feature enabled).
3587 * This hole is either in the middle of the cloning
3588 * range or at the beginning (fully overlaps it or
3589 * partially overlaps it).
3590 */
3591 if (new_key.offset != last_dest_end)
3592 drop_start = last_dest_end;
3593 else
3594 drop_start = new_key.offset;
3595
3596 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003597 * 1 - adjusting old extent (we may have to split it)
3598 * 1 - add new extent
3599 * 1 - inode update
3600 */
3601 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003602 if (IS_ERR(trans)) {
3603 ret = PTR_ERR(trans);
3604 goto out;
3605 }
3606
Chris Masonc8a894d2009-06-27 21:07:03 -04003607 if (type == BTRFS_FILE_EXTENT_REG ||
3608 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003609 /*
3610 * a | --- range to clone ---| b
3611 * | ------------- extent ------------- |
3612 */
3613
Antonio Ospite93915582014-06-04 14:03:48 +02003614 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003615 if (key.offset + datal > off + len)
3616 datal = off + len - key.offset;
3617
Antonio Ospite93915582014-06-04 14:03:48 +02003618 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003619 if (off > key.offset) {
3620 datao += off - key.offset;
3621 datal -= off - key.offset;
3622 }
3623
Josef Bacik5dc562c2012-08-17 13:14:17 -04003624 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003625 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003626 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003627 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003628 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003629 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003630 btrfs_abort_transaction(trans,
3631 root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003632 btrfs_end_transaction(trans, root);
3633 goto out;
3634 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003635
Sage Weilc5c9cd42008-11-12 14:32:25 -05003636 ret = btrfs_insert_empty_item(trans, root, path,
3637 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003638 if (ret) {
3639 btrfs_abort_transaction(trans, root,
3640 ret);
3641 btrfs_end_transaction(trans, root);
3642 goto out;
3643 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003644
3645 leaf = path->nodes[0];
3646 slot = path->slots[0];
3647 write_extent_buffer(leaf, buf,
3648 btrfs_item_ptr_offset(leaf, slot),
3649 size);
3650
3651 extent = btrfs_item_ptr(leaf, slot,
3652 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003653
Sage Weilc5c9cd42008-11-12 14:32:25 -05003654 /* disko == 0 means it's a hole */
3655 if (!disko)
3656 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003657
3658 btrfs_set_file_extent_offset(leaf, extent,
3659 datao);
3660 btrfs_set_file_extent_num_bytes(leaf, extent,
3661 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003662
Sage Weilc5c9cd42008-11-12 14:32:25 -05003663 if (disko) {
3664 inode_add_bytes(inode, datal);
3665 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003666 disko, diskl, 0,
3667 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003668 btrfs_ino(inode),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003669 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003670 if (ret) {
3671 btrfs_abort_transaction(trans,
3672 root,
3673 ret);
3674 btrfs_end_transaction(trans,
3675 root);
3676 goto out;
3677
3678 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003679 }
3680 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3681 u64 skip = 0;
3682 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003683
Sage Weilc5c9cd42008-11-12 14:32:25 -05003684 if (off > key.offset) {
3685 skip = off - key.offset;
3686 new_key.offset += skip;
3687 }
Chris Masond3977122009-01-05 21:25:51 -05003688
Liu Boaa42ffd2012-09-18 03:52:23 -06003689 if (key.offset + datal > off + len)
3690 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003691
Sage Weilc5c9cd42008-11-12 14:32:25 -05003692 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003693 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003694 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003695 goto out;
3696 }
3697 size -= skip + trim;
3698 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699
Filipe Manana8039d872015-10-13 15:15:00 +01003700 ret = clone_copy_inline_extent(src, inode,
3701 trans, path,
3702 &new_key,
3703 drop_start,
3704 datal,
3705 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003706 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003707 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003708 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003709 root,
3710 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003711 btrfs_end_transaction(trans, root);
3712 goto out;
3713 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003714 leaf = path->nodes[0];
3715 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003716 }
3717
Filipe Manana7ffbb592014-06-09 03:48:05 +01003718 /* If we have an implicit hole (NO_HOLES feature). */
3719 if (drop_start < new_key.offset)
3720 clone_update_extent_map(inode, trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003721 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003722 new_key.offset - drop_start);
3723
Filipe Manana14f59792014-06-29 21:45:40 +01003724 clone_update_extent_map(inode, trans, path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003725
Sage Weilc5c9cd42008-11-12 14:32:25 -05003726 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003727 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003728
Filipe Manana62e23902014-08-08 02:47:06 +01003729 last_dest_end = ALIGN(new_key.offset + datal,
3730 root->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003731 ret = clone_finish_inode_update(trans, inode,
3732 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003733 destoff, olen,
3734 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003735 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003736 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003737 if (new_key.offset + datal >= destoff + len)
3738 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003740 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003741 key.offset = next_key_min_offset;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003742 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003743 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003744
Filipe Mananaf82a9902014-06-01 01:50:28 +01003745 if (last_dest_end < destoff + len) {
3746 /*
3747 * We have an implicit hole (NO_HOLES feature is enabled) that
3748 * fully or partially overlaps our cloning range at its end.
3749 */
3750 btrfs_release_path(path);
3751
3752 /*
3753 * 1 - remove extent(s)
3754 * 1 - inode update
3755 */
3756 trans = btrfs_start_transaction(root, 2);
3757 if (IS_ERR(trans)) {
3758 ret = PTR_ERR(trans);
3759 goto out;
3760 }
3761 ret = btrfs_drop_extents(trans, root, inode,
3762 last_dest_end, destoff + len, 1);
3763 if (ret) {
3764 if (ret != -EOPNOTSUPP)
3765 btrfs_abort_transaction(trans, root, ret);
3766 btrfs_end_transaction(trans, root);
3767 goto out;
3768 }
Filipe Manana14f59792014-06-29 21:45:40 +01003769 clone_update_extent_map(inode, trans, NULL, last_dest_end,
3770 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003771 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003772 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003773 }
3774
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003775out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003776 btrfs_free_path(path);
3777 vfree(buf);
3778 return ret;
3779}
3780
Zach Brown3db11b22015-11-10 16:53:32 -05003781static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3782 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003783{
Al Viro54563d42013-09-01 15:57:51 -04003784 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003785 struct inode *src = file_inode(file_src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003786 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003787 int ret;
3788 u64 len = olen;
3789 u64 bs = root->fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003790 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003791
3792 /*
3793 * TODO:
3794 * - split compressed inline extents. annoying: we need to
3795 * decompress into destination's address_space (the file offset
3796 * may change, so source mapping won't do), then recompress (or
3797 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003798 *
3799 * - split destination inode's inline extents. The inline extents can
3800 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003801 */
3802
Mark Fasheh32b7c682013-08-06 11:42:49 -07003803 if (btrfs_root_readonly(root))
3804 return -EROFS;
3805
Zach Brown3db11b22015-11-10 16:53:32 -05003806 if (file_src->f_path.mnt != file->f_path.mnt ||
3807 src->i_sb != inode->i_sb)
3808 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003809
3810 /* don't make the dst file partly checksummed */
3811 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3812 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003813 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003814
Mark Fasheh32b7c682013-08-06 11:42:49 -07003815 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003816 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003817
3818 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003819 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003820 } else {
Al Viro59551022016-01-22 15:40:57 -05003821 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003822 }
3823
3824 /* determine range to clone */
3825 ret = -EINVAL;
3826 if (off + len > src->i_size || off + len < off)
3827 goto out_unlock;
3828 if (len == 0)
3829 olen = len = src->i_size - off;
3830 /* if we extend to eof, continue to block boundary */
3831 if (off + len == src->i_size)
3832 len = ALIGN(src->i_size, bs) - off;
3833
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003834 if (len == 0) {
3835 ret = 0;
3836 goto out_unlock;
3837 }
3838
Mark Fasheh32b7c682013-08-06 11:42:49 -07003839 /* verify the end result is block aligned */
3840 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3841 !IS_ALIGNED(destoff, bs))
3842 goto out_unlock;
3843
3844 /* verify if ranges are overlapped within the same file */
3845 if (same_inode) {
3846 if (destoff + len > off && destoff < off + len)
3847 goto out_unlock;
3848 }
3849
3850 if (destoff > inode->i_size) {
3851 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3852 if (ret)
3853 goto out_unlock;
3854 }
3855
Filipe Mananac125b8b2014-05-23 05:03:34 +01003856 /*
3857 * Lock the target range too. Right after we replace the file extent
3858 * items in the fs tree (which now point to the cloned data), we might
3859 * have a worker replace them with extent items relative to a write
3860 * operation that was issued before this clone operation (i.e. confront
3861 * with inode.c:btrfs_finish_ordered_io).
3862 */
3863 if (same_inode) {
3864 u64 lock_start = min_t(u64, off, destoff);
3865 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003866
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003867 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003868 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003869 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3870 true);
3871 }
3872 ASSERT(ret == 0);
3873 if (WARN_ON(ret)) {
3874 /* ranges in the io trees already unlocked */
3875 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003876 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003877
Mark Fasheh1c919a52015-06-30 14:42:08 -07003878 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003879
Filipe Mananac125b8b2014-05-23 05:03:34 +01003880 if (same_inode) {
3881 u64 lock_start = min_t(u64, off, destoff);
3882 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3883
3884 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3885 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003886 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003887 }
3888 /*
3889 * Truncate page cache pages so that future reads will see the cloned
3890 * data immediately and not the previous data.
3891 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303892 truncate_inode_pages_range(&inode->i_data,
3893 round_down(destoff, PAGE_CACHE_SIZE),
3894 round_up(destoff + len, PAGE_CACHE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003895out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003896 if (!same_inode)
3897 btrfs_double_inode_unlock(src, inode);
3898 else
Al Viro59551022016-01-22 15:40:57 -05003899 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003900 return ret;
3901}
3902
3903ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
3904 struct file *file_out, loff_t pos_out,
3905 size_t len, unsigned int flags)
3906{
3907 ssize_t ret;
3908
3909 ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
3910 if (ret == 0)
3911 ret = len;
3912 return ret;
3913}
3914
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003915int btrfs_clone_file_range(struct file *src_file, loff_t off,
3916 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003917{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003918 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003919}
3920
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003921/*
3922 * there are many ways the trans_start and trans_end ioctls can lead
3923 * to deadlocks. They should only be used by applications that
3924 * basically own the machine, and have a very in depth understanding
3925 * of all the possible deadlocks and enospc problems.
3926 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003927static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003928{
Al Viro496ad9a2013-01-23 17:07:38 -05003929 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003930 struct btrfs_root *root = BTRFS_I(inode)->root;
3931 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003932 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003933
Sage Weil1ab86ae2009-09-29 18:38:44 -04003934 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003935 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003936 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003937
3938 ret = -EINPROGRESS;
3939 if (file->private_data)
3940 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003941
Li Zefanb83cc962010-12-20 16:04:08 +08003942 ret = -EROFS;
3943 if (btrfs_root_readonly(root))
3944 goto out;
3945
Al Viroa561be72011-11-23 11:57:51 -05003946 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003947 if (ret)
3948 goto out;
3949
Josef Bacika4abeea2011-04-11 17:25:13 -04003950 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003951
Sage Weil1ab86ae2009-09-29 18:38:44 -04003952 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003953 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003954 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003955 goto out_drop;
3956
3957 file->private_data = trans;
3958 return 0;
3959
3960out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003961 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003962 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003963out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003964 return ret;
3965}
3966
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003967static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3968{
Al Viro496ad9a2013-01-23 17:07:38 -05003969 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003970 struct btrfs_root *root = BTRFS_I(inode)->root;
3971 struct btrfs_root *new_root;
3972 struct btrfs_dir_item *di;
3973 struct btrfs_trans_handle *trans;
3974 struct btrfs_path *path;
3975 struct btrfs_key location;
3976 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003977 u64 objectid = 0;
3978 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003979 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003980
3981 if (!capable(CAP_SYS_ADMIN))
3982 return -EPERM;
3983
Miao Xie3c04ce02012-11-26 08:43:07 +00003984 ret = mnt_want_write_file(file);
3985 if (ret)
3986 return ret;
3987
3988 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3989 ret = -EFAULT;
3990 goto out;
3991 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003992
3993 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303994 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003995
3996 location.objectid = objectid;
3997 location.type = BTRFS_ROOT_ITEM_KEY;
3998 location.offset = (u64)-1;
3999
4000 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004001 if (IS_ERR(new_root)) {
4002 ret = PTR_ERR(new_root);
4003 goto out;
4004 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004005
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004006 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004007 if (!path) {
4008 ret = -ENOMEM;
4009 goto out;
4010 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004011 path->leave_spinning = 1;
4012
4013 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004014 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004015 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004016 ret = PTR_ERR(trans);
4017 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004018 }
4019
David Sterba6c417612011-04-13 15:41:04 +02004020 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004021 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
4022 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004023 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004024 btrfs_free_path(path);
4025 btrfs_end_transaction(trans, root);
Frank Holtonefe120a2013-12-20 11:37:06 -05004026 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
4027 "item, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004028 ret = -ENOENT;
4029 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004030 }
4031
4032 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4033 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4034 btrfs_mark_buffer_dirty(path->nodes[0]);
4035 btrfs_free_path(path);
4036
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06004037 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004038 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00004039out:
4040 mnt_drop_write_file(file);
4041 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004042}
4043
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004044void btrfs_get_block_group_info(struct list_head *groups_list,
4045 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004046{
4047 struct btrfs_block_group_cache *block_group;
4048
4049 space->total_bytes = 0;
4050 space->used_bytes = 0;
4051 space->flags = 0;
4052 list_for_each_entry(block_group, groups_list, list) {
4053 space->flags = block_group->flags;
4054 space->total_bytes += block_group->key.offset;
4055 space->used_bytes +=
4056 btrfs_block_group_used(&block_group->item);
4057 }
4058}
4059
Eric Sandeen48a3b632013-04-25 20:41:01 +00004060static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004061{
4062 struct btrfs_ioctl_space_args space_args;
4063 struct btrfs_ioctl_space_info space;
4064 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004065 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004066 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004067 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004068 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4069 BTRFS_BLOCK_GROUP_SYSTEM,
4070 BTRFS_BLOCK_GROUP_METADATA,
4071 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4072 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004073 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004074 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004075 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004076 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004077
4078 if (copy_from_user(&space_args,
4079 (struct btrfs_ioctl_space_args __user *)arg,
4080 sizeof(space_args)))
4081 return -EFAULT;
4082
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004083 for (i = 0; i < num_types; i++) {
4084 struct btrfs_space_info *tmp;
4085
4086 info = NULL;
4087 rcu_read_lock();
4088 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4089 list) {
4090 if (tmp->flags == types[i]) {
4091 info = tmp;
4092 break;
4093 }
4094 }
4095 rcu_read_unlock();
4096
4097 if (!info)
4098 continue;
4099
4100 down_read(&info->groups_sem);
4101 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4102 if (!list_empty(&info->block_groups[c]))
4103 slot_count++;
4104 }
4105 up_read(&info->groups_sem);
4106 }
Josef Bacik1406e432010-01-13 18:19:06 +00004107
David Sterba36523e952014-02-07 14:34:12 +01004108 /*
4109 * Global block reserve, exported as a space_info
4110 */
4111 slot_count++;
4112
Chris Mason7fde62b2010-03-16 15:40:10 -04004113 /* space_slots == 0 means they are asking for a count */
4114 if (space_args.space_slots == 0) {
4115 space_args.total_spaces = slot_count;
4116 goto out;
4117 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004118
Dan Rosenberg51788b12011-02-14 16:04:23 -05004119 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004120
Chris Mason7fde62b2010-03-16 15:40:10 -04004121 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004122
Chris Mason7fde62b2010-03-16 15:40:10 -04004123 /* we generally have at most 6 or so space infos, one for each raid
4124 * level. So, a whole page should be more than enough for everyone
4125 */
4126 if (alloc_size > PAGE_CACHE_SIZE)
4127 return -ENOMEM;
4128
4129 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004130 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004131 if (!dest)
4132 return -ENOMEM;
4133 dest_orig = dest;
4134
4135 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004136 for (i = 0; i < num_types; i++) {
4137 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004138
Dan Rosenberg51788b12011-02-14 16:04:23 -05004139 if (!slot_count)
4140 break;
4141
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004142 info = NULL;
4143 rcu_read_lock();
4144 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4145 list) {
4146 if (tmp->flags == types[i]) {
4147 info = tmp;
4148 break;
4149 }
4150 }
4151 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004152
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004153 if (!info)
4154 continue;
4155 down_read(&info->groups_sem);
4156 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4157 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02004158 btrfs_get_block_group_info(
4159 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004160 memcpy(dest, &space, sizeof(space));
4161 dest++;
4162 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004163 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004164 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004165 if (!slot_count)
4166 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004167 }
4168 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004169 }
Josef Bacik1406e432010-01-13 18:19:06 +00004170
David Sterba36523e952014-02-07 14:34:12 +01004171 /*
4172 * Add global block reserve
4173 */
4174 if (slot_count) {
4175 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
4176
4177 spin_lock(&block_rsv->lock);
4178 space.total_bytes = block_rsv->size;
4179 space.used_bytes = block_rsv->size - block_rsv->reserved;
4180 spin_unlock(&block_rsv->lock);
4181 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4182 memcpy(dest, &space, sizeof(space));
4183 space_args.total_spaces++;
4184 }
4185
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004186 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004187 (arg + sizeof(struct btrfs_ioctl_space_args));
4188
4189 if (copy_to_user(user_dest, dest_orig, alloc_size))
4190 ret = -EFAULT;
4191
4192 kfree(dest_orig);
4193out:
4194 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004195 ret = -EFAULT;
4196
4197 return ret;
4198}
4199
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004200/*
4201 * there are many ways the trans_start and trans_end ioctls can lead
4202 * to deadlocks. They should only be used by applications that
4203 * basically own the machine, and have a very in depth understanding
4204 * of all the possible deadlocks and enospc problems.
4205 */
4206long btrfs_ioctl_trans_end(struct file *file)
4207{
Al Viro496ad9a2013-01-23 17:07:38 -05004208 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004209 struct btrfs_root *root = BTRFS_I(inode)->root;
4210 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004211
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004212 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004213 if (!trans)
4214 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004215 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004216
Sage Weil1ab86ae2009-09-29 18:38:44 -04004217 btrfs_end_transaction(trans, root);
4218
Josef Bacika4abeea2011-04-11 17:25:13 -04004219 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004220
Al Viro2a79f172011-12-09 08:06:57 -05004221 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004222 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004223}
4224
Miao Xie9a8c28b2012-11-26 08:40:43 +00004225static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4226 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004227{
Sage Weil46204592010-10-29 15:41:32 -04004228 struct btrfs_trans_handle *trans;
4229 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004230 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004231
Miao Xied4edf392013-02-20 09:17:06 +00004232 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004233 if (IS_ERR(trans)) {
4234 if (PTR_ERR(trans) != -ENOENT)
4235 return PTR_ERR(trans);
4236
4237 /* No running transaction, don't bother */
4238 transid = root->fs_info->last_trans_committed;
4239 goto out;
4240 }
Sage Weil46204592010-10-29 15:41:32 -04004241 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004242 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004243 if (ret) {
4244 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004245 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004246 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004247out:
Sage Weil46204592010-10-29 15:41:32 -04004248 if (argp)
4249 if (copy_to_user(argp, &transid, sizeof(transid)))
4250 return -EFAULT;
4251 return 0;
4252}
4253
Miao Xie9a8c28b2012-11-26 08:40:43 +00004254static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4255 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004256{
Sage Weil46204592010-10-29 15:41:32 -04004257 u64 transid;
4258
4259 if (argp) {
4260 if (copy_from_user(&transid, argp, sizeof(transid)))
4261 return -EFAULT;
4262 } else {
4263 transid = 0; /* current trans */
4264 }
4265 return btrfs_wait_for_commit(root, transid);
4266}
4267
Miao Xieb8e95482012-11-26 08:48:01 +00004268static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004269{
Al Viro496ad9a2013-01-23 17:07:38 -05004270 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01004271 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004272 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004273
4274 if (!capable(CAP_SYS_ADMIN))
4275 return -EPERM;
4276
4277 sa = memdup_user(arg, sizeof(*sa));
4278 if (IS_ERR(sa))
4279 return PTR_ERR(sa);
4280
Miao Xieb8e95482012-11-26 08:48:01 +00004281 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4282 ret = mnt_want_write_file(file);
4283 if (ret)
4284 goto out;
4285 }
4286
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004287 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004288 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4289 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004290
4291 if (copy_to_user(arg, sa, sizeof(*sa)))
4292 ret = -EFAULT;
4293
Miao Xieb8e95482012-11-26 08:48:01 +00004294 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4295 mnt_drop_write_file(file);
4296out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004297 kfree(sa);
4298 return ret;
4299}
4300
4301static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4302{
4303 if (!capable(CAP_SYS_ADMIN))
4304 return -EPERM;
4305
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004306 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004307}
4308
4309static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4310 void __user *arg)
4311{
4312 struct btrfs_ioctl_scrub_args *sa;
4313 int ret;
4314
4315 if (!capable(CAP_SYS_ADMIN))
4316 return -EPERM;
4317
4318 sa = memdup_user(arg, sizeof(*sa));
4319 if (IS_ERR(sa))
4320 return PTR_ERR(sa);
4321
4322 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4323
4324 if (copy_to_user(arg, sa, sizeof(*sa)))
4325 ret = -EFAULT;
4326
4327 kfree(sa);
4328 return ret;
4329}
4330
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004331static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004332 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004333{
4334 struct btrfs_ioctl_get_dev_stats *sa;
4335 int ret;
4336
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004337 sa = memdup_user(arg, sizeof(*sa));
4338 if (IS_ERR(sa))
4339 return PTR_ERR(sa);
4340
David Sterbab27f7c02012-06-22 06:30:39 -06004341 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4342 kfree(sa);
4343 return -EPERM;
4344 }
4345
4346 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004347
4348 if (copy_to_user(arg, sa, sizeof(*sa)))
4349 ret = -EFAULT;
4350
4351 kfree(sa);
4352 return ret;
4353}
4354
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004355static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4356{
4357 struct btrfs_ioctl_dev_replace_args *p;
4358 int ret;
4359
4360 if (!capable(CAP_SYS_ADMIN))
4361 return -EPERM;
4362
4363 p = memdup_user(arg, sizeof(*p));
4364 if (IS_ERR(p))
4365 return PTR_ERR(p);
4366
4367 switch (p->cmd) {
4368 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004369 if (root->fs_info->sb->s_flags & MS_RDONLY) {
4370 ret = -EROFS;
4371 goto out;
4372 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004373 if (atomic_xchg(
4374 &root->fs_info->mutually_exclusive_operation_running,
4375 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004376 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004377 } else {
4378 ret = btrfs_dev_replace_start(root, p);
4379 atomic_set(
4380 &root->fs_info->mutually_exclusive_operation_running,
4381 0);
4382 }
4383 break;
4384 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4385 btrfs_dev_replace_status(root->fs_info, p);
4386 ret = 0;
4387 break;
4388 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4389 ret = btrfs_dev_replace_cancel(root->fs_info, p);
4390 break;
4391 default:
4392 ret = -EINVAL;
4393 break;
4394 }
4395
4396 if (copy_to_user(arg, p, sizeof(*p)))
4397 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004398out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004399 kfree(p);
4400 return ret;
4401}
4402
Jan Schmidtd7728c92011-07-07 16:48:38 +02004403static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4404{
4405 int ret = 0;
4406 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004407 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004408 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004409 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004410 struct inode_fs_paths *ipath = NULL;
4411 struct btrfs_path *path;
4412
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004413 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004414 return -EPERM;
4415
4416 path = btrfs_alloc_path();
4417 if (!path) {
4418 ret = -ENOMEM;
4419 goto out;
4420 }
4421
4422 ipa = memdup_user(arg, sizeof(*ipa));
4423 if (IS_ERR(ipa)) {
4424 ret = PTR_ERR(ipa);
4425 ipa = NULL;
4426 goto out;
4427 }
4428
4429 size = min_t(u32, ipa->size, 4096);
4430 ipath = init_ipath(size, root, path);
4431 if (IS_ERR(ipath)) {
4432 ret = PTR_ERR(ipath);
4433 ipath = NULL;
4434 goto out;
4435 }
4436
4437 ret = paths_from_inode(ipa->inum, ipath);
4438 if (ret < 0)
4439 goto out;
4440
4441 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004442 rel_ptr = ipath->fspath->val[i] -
4443 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004444 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004445 }
4446
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004447 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4448 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004449 if (ret) {
4450 ret = -EFAULT;
4451 goto out;
4452 }
4453
4454out:
4455 btrfs_free_path(path);
4456 free_ipath(ipath);
4457 kfree(ipa);
4458
4459 return ret;
4460}
4461
4462static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4463{
4464 struct btrfs_data_container *inodes = ctx;
4465 const size_t c = 3 * sizeof(u64);
4466
4467 if (inodes->bytes_left >= c) {
4468 inodes->bytes_left -= c;
4469 inodes->val[inodes->elem_cnt] = inum;
4470 inodes->val[inodes->elem_cnt + 1] = offset;
4471 inodes->val[inodes->elem_cnt + 2] = root;
4472 inodes->elem_cnt += 3;
4473 } else {
4474 inodes->bytes_missing += c - inodes->bytes_left;
4475 inodes->bytes_left = 0;
4476 inodes->elem_missed += 3;
4477 }
4478
4479 return 0;
4480}
4481
4482static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4483 void __user *arg)
4484{
4485 int ret = 0;
4486 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004487 struct btrfs_ioctl_logical_ino_args *loi;
4488 struct btrfs_data_container *inodes = NULL;
4489 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004490
4491 if (!capable(CAP_SYS_ADMIN))
4492 return -EPERM;
4493
4494 loi = memdup_user(arg, sizeof(*loi));
4495 if (IS_ERR(loi)) {
4496 ret = PTR_ERR(loi);
4497 loi = NULL;
4498 goto out;
4499 }
4500
4501 path = btrfs_alloc_path();
4502 if (!path) {
4503 ret = -ENOMEM;
4504 goto out;
4505 }
4506
Byongho Leeee221842015-12-15 01:42:10 +09004507 size = min_t(u32, loi->size, SZ_64K);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004508 inodes = init_data_container(size);
4509 if (IS_ERR(inodes)) {
4510 ret = PTR_ERR(inodes);
4511 inodes = NULL;
4512 goto out;
4513 }
4514
Liu Bodf031f02012-09-07 20:01:29 -06004515 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4516 build_ino_list, inodes);
4517 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004518 ret = -ENOENT;
4519 if (ret < 0)
4520 goto out;
4521
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004522 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4523 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004524 if (ret)
4525 ret = -EFAULT;
4526
4527out:
4528 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06004529 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004530 kfree(loi);
4531
4532 return ret;
4533}
4534
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004535void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004536 struct btrfs_ioctl_balance_args *bargs)
4537{
4538 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4539
4540 bargs->flags = bctl->flags;
4541
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004542 if (atomic_read(&fs_info->balance_running))
4543 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4544 if (atomic_read(&fs_info->balance_pause_req))
4545 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004546 if (atomic_read(&fs_info->balance_cancel_req))
4547 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004548
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004549 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4550 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4551 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004552
4553 if (lock) {
4554 spin_lock(&fs_info->balance_lock);
4555 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4556 spin_unlock(&fs_info->balance_lock);
4557 } else {
4558 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4559 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004560}
4561
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004562static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004563{
Al Viro496ad9a2013-01-23 17:07:38 -05004564 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004565 struct btrfs_fs_info *fs_info = root->fs_info;
4566 struct btrfs_ioctl_balance_args *bargs;
4567 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004568 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004569 int ret;
4570
4571 if (!capable(CAP_SYS_ADMIN))
4572 return -EPERM;
4573
Liu Boe54bfa32012-06-29 03:58:48 -06004574 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004575 if (ret)
4576 return ret;
4577
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004578again:
4579 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4580 mutex_lock(&fs_info->volume_mutex);
4581 mutex_lock(&fs_info->balance_mutex);
4582 need_unlock = true;
4583 goto locked;
4584 }
4585
4586 /*
4587 * mut. excl. ops lock is locked. Three possibilites:
4588 * (1) some other op is running
4589 * (2) balance is running
4590 * (3) balance is paused -- special case (think resume)
4591 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004592 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004593 if (fs_info->balance_ctl) {
4594 /* this is either (2) or (3) */
4595 if (!atomic_read(&fs_info->balance_running)) {
4596 mutex_unlock(&fs_info->balance_mutex);
4597 if (!mutex_trylock(&fs_info->volume_mutex))
4598 goto again;
4599 mutex_lock(&fs_info->balance_mutex);
4600
4601 if (fs_info->balance_ctl &&
4602 !atomic_read(&fs_info->balance_running)) {
4603 /* this is (3) */
4604 need_unlock = false;
4605 goto locked;
4606 }
4607
4608 mutex_unlock(&fs_info->balance_mutex);
4609 mutex_unlock(&fs_info->volume_mutex);
4610 goto again;
4611 } else {
4612 /* this is (2) */
4613 mutex_unlock(&fs_info->balance_mutex);
4614 ret = -EINPROGRESS;
4615 goto out;
4616 }
4617 } else {
4618 /* this is (1) */
4619 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004620 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004621 goto out;
4622 }
4623
4624locked:
4625 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004626
4627 if (arg) {
4628 bargs = memdup_user(arg, sizeof(*bargs));
4629 if (IS_ERR(bargs)) {
4630 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004631 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004632 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004633
4634 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4635 if (!fs_info->balance_ctl) {
4636 ret = -ENOTCONN;
4637 goto out_bargs;
4638 }
4639
4640 bctl = fs_info->balance_ctl;
4641 spin_lock(&fs_info->balance_lock);
4642 bctl->flags |= BTRFS_BALANCE_RESUME;
4643 spin_unlock(&fs_info->balance_lock);
4644
4645 goto do_balance;
4646 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004647 } else {
4648 bargs = NULL;
4649 }
4650
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004651 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004652 ret = -EINPROGRESS;
4653 goto out_bargs;
4654 }
4655
David Sterba8d2db782015-11-04 15:38:29 +01004656 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004657 if (!bctl) {
4658 ret = -ENOMEM;
4659 goto out_bargs;
4660 }
4661
4662 bctl->fs_info = fs_info;
4663 if (arg) {
4664 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4665 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4666 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4667
4668 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004669 } else {
4670 /* balance everything - no filters */
4671 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004672 }
4673
David Sterba8eb93452015-10-12 16:55:54 +02004674 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4675 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004676 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004677 }
4678
Ilya Dryomovde322262012-01-16 22:04:49 +02004679do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004680 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004681 * Ownership of bctl and mutually_exclusive_operation_running
4682 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4683 * or, if restriper was paused all the way until unmount, in
4684 * free_fs_info. mutually_exclusive_operation_running is
4685 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004686 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004687 need_unlock = false;
4688
4689 ret = btrfs_balance(bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004690 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004691
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004692 if (arg) {
4693 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4694 ret = -EFAULT;
4695 }
4696
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004697out_bctl:
4698 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004699out_bargs:
4700 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004701out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004702 mutex_unlock(&fs_info->balance_mutex);
4703 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004704 if (need_unlock)
4705 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4706out:
Liu Boe54bfa32012-06-29 03:58:48 -06004707 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004708 return ret;
4709}
4710
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004711static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4712{
4713 if (!capable(CAP_SYS_ADMIN))
4714 return -EPERM;
4715
4716 switch (cmd) {
4717 case BTRFS_BALANCE_CTL_PAUSE:
4718 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004719 case BTRFS_BALANCE_CTL_CANCEL:
4720 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004721 }
4722
4723 return -EINVAL;
4724}
4725
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004726static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4727 void __user *arg)
4728{
4729 struct btrfs_fs_info *fs_info = root->fs_info;
4730 struct btrfs_ioctl_balance_args *bargs;
4731 int ret = 0;
4732
4733 if (!capable(CAP_SYS_ADMIN))
4734 return -EPERM;
4735
4736 mutex_lock(&fs_info->balance_mutex);
4737 if (!fs_info->balance_ctl) {
4738 ret = -ENOTCONN;
4739 goto out;
4740 }
4741
David Sterba8d2db782015-11-04 15:38:29 +01004742 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004743 if (!bargs) {
4744 ret = -ENOMEM;
4745 goto out;
4746 }
4747
4748 update_ioctl_balance_args(fs_info, 1, bargs);
4749
4750 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4751 ret = -EFAULT;
4752
4753 kfree(bargs);
4754out:
4755 mutex_unlock(&fs_info->balance_mutex);
4756 return ret;
4757}
4758
Miao Xie905b0dd2012-11-26 08:50:11 +00004759static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004760{
Al Viro496ad9a2013-01-23 17:07:38 -05004761 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004762 struct btrfs_ioctl_quota_ctl_args *sa;
4763 struct btrfs_trans_handle *trans = NULL;
4764 int ret;
4765 int err;
4766
4767 if (!capable(CAP_SYS_ADMIN))
4768 return -EPERM;
4769
Miao Xie905b0dd2012-11-26 08:50:11 +00004770 ret = mnt_want_write_file(file);
4771 if (ret)
4772 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004773
4774 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004775 if (IS_ERR(sa)) {
4776 ret = PTR_ERR(sa);
4777 goto drop_write;
4778 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004779
Wang Shilong7708f022013-04-07 10:24:57 +00004780 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004781 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4782 if (IS_ERR(trans)) {
4783 ret = PTR_ERR(trans);
4784 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004785 }
4786
4787 switch (sa->cmd) {
4788 case BTRFS_QUOTA_CTL_ENABLE:
4789 ret = btrfs_quota_enable(trans, root->fs_info);
4790 break;
4791 case BTRFS_QUOTA_CTL_DISABLE:
4792 ret = btrfs_quota_disable(trans, root->fs_info);
4793 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004794 default:
4795 ret = -EINVAL;
4796 break;
4797 }
4798
Jan Schmidt2f232032013-04-25 16:04:51 +00004799 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4800 if (err && !ret)
4801 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004802out:
4803 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004804 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004805drop_write:
4806 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004807 return ret;
4808}
4809
Miao Xie905b0dd2012-11-26 08:50:11 +00004810static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004811{
Al Viro496ad9a2013-01-23 17:07:38 -05004812 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004813 struct btrfs_ioctl_qgroup_assign_args *sa;
4814 struct btrfs_trans_handle *trans;
4815 int ret;
4816 int err;
4817
4818 if (!capable(CAP_SYS_ADMIN))
4819 return -EPERM;
4820
Miao Xie905b0dd2012-11-26 08:50:11 +00004821 ret = mnt_want_write_file(file);
4822 if (ret)
4823 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004824
4825 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004826 if (IS_ERR(sa)) {
4827 ret = PTR_ERR(sa);
4828 goto drop_write;
4829 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004830
4831 trans = btrfs_join_transaction(root);
4832 if (IS_ERR(trans)) {
4833 ret = PTR_ERR(trans);
4834 goto out;
4835 }
4836
4837 /* FIXME: check if the IDs really exist */
4838 if (sa->assign) {
4839 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4840 sa->src, sa->dst);
4841 } else {
4842 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4843 sa->src, sa->dst);
4844 }
4845
Qu Wenruoe082f562015-02-27 16:24:28 +08004846 /* update qgroup status and info */
4847 err = btrfs_run_qgroups(trans, root->fs_info);
4848 if (err < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08004849 btrfs_std_error(root->fs_info, ret,
Qu Wenruoe082f562015-02-27 16:24:28 +08004850 "failed to update qgroup status and info\n");
Arne Jansen5d13a372011-09-14 15:53:51 +02004851 err = btrfs_end_transaction(trans, root);
4852 if (err && !ret)
4853 ret = err;
4854
4855out:
4856 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004857drop_write:
4858 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004859 return ret;
4860}
4861
Miao Xie905b0dd2012-11-26 08:50:11 +00004862static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004863{
Al Viro496ad9a2013-01-23 17:07:38 -05004864 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004865 struct btrfs_ioctl_qgroup_create_args *sa;
4866 struct btrfs_trans_handle *trans;
4867 int ret;
4868 int err;
4869
4870 if (!capable(CAP_SYS_ADMIN))
4871 return -EPERM;
4872
Miao Xie905b0dd2012-11-26 08:50:11 +00004873 ret = mnt_want_write_file(file);
4874 if (ret)
4875 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004876
4877 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004878 if (IS_ERR(sa)) {
4879 ret = PTR_ERR(sa);
4880 goto drop_write;
4881 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004882
Miao Xied86e56c2012-11-15 11:35:41 +00004883 if (!sa->qgroupid) {
4884 ret = -EINVAL;
4885 goto out;
4886 }
4887
Arne Jansen5d13a372011-09-14 15:53:51 +02004888 trans = btrfs_join_transaction(root);
4889 if (IS_ERR(trans)) {
4890 ret = PTR_ERR(trans);
4891 goto out;
4892 }
4893
4894 /* FIXME: check if the IDs really exist */
4895 if (sa->create) {
Dongsheng Yang4087cf22015-01-18 10:59:23 -05004896 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004897 } else {
4898 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4899 }
4900
4901 err = btrfs_end_transaction(trans, root);
4902 if (err && !ret)
4903 ret = err;
4904
4905out:
4906 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004907drop_write:
4908 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004909 return ret;
4910}
4911
Miao Xie905b0dd2012-11-26 08:50:11 +00004912static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004913{
Al Viro496ad9a2013-01-23 17:07:38 -05004914 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004915 struct btrfs_ioctl_qgroup_limit_args *sa;
4916 struct btrfs_trans_handle *trans;
4917 int ret;
4918 int err;
4919 u64 qgroupid;
4920
4921 if (!capable(CAP_SYS_ADMIN))
4922 return -EPERM;
4923
Miao Xie905b0dd2012-11-26 08:50:11 +00004924 ret = mnt_want_write_file(file);
4925 if (ret)
4926 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004927
4928 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004929 if (IS_ERR(sa)) {
4930 ret = PTR_ERR(sa);
4931 goto drop_write;
4932 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004933
4934 trans = btrfs_join_transaction(root);
4935 if (IS_ERR(trans)) {
4936 ret = PTR_ERR(trans);
4937 goto out;
4938 }
4939
4940 qgroupid = sa->qgroupid;
4941 if (!qgroupid) {
4942 /* take the current subvol as qgroup */
4943 qgroupid = root->root_key.objectid;
4944 }
4945
4946 /* FIXME: check if the IDs really exist */
4947 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4948
4949 err = btrfs_end_transaction(trans, root);
4950 if (err && !ret)
4951 ret = err;
4952
4953out:
4954 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004955drop_write:
4956 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004957 return ret;
4958}
4959
Jan Schmidt2f232032013-04-25 16:04:51 +00004960static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4961{
Al Viro6d0379e2013-06-16 19:32:35 +04004962 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004963 struct btrfs_ioctl_quota_rescan_args *qsa;
4964 int ret;
4965
4966 if (!capable(CAP_SYS_ADMIN))
4967 return -EPERM;
4968
4969 ret = mnt_want_write_file(file);
4970 if (ret)
4971 return ret;
4972
4973 qsa = memdup_user(arg, sizeof(*qsa));
4974 if (IS_ERR(qsa)) {
4975 ret = PTR_ERR(qsa);
4976 goto drop_write;
4977 }
4978
4979 if (qsa->flags) {
4980 ret = -EINVAL;
4981 goto out;
4982 }
4983
4984 ret = btrfs_qgroup_rescan(root->fs_info);
4985
4986out:
4987 kfree(qsa);
4988drop_write:
4989 mnt_drop_write_file(file);
4990 return ret;
4991}
4992
4993static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4994{
Al Viro6d0379e2013-06-16 19:32:35 +04004995 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004996 struct btrfs_ioctl_quota_rescan_args *qsa;
4997 int ret = 0;
4998
4999 if (!capable(CAP_SYS_ADMIN))
5000 return -EPERM;
5001
David Sterba8d2db782015-11-04 15:38:29 +01005002 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005003 if (!qsa)
5004 return -ENOMEM;
5005
5006 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5007 qsa->flags = 1;
5008 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
5009 }
5010
5011 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5012 ret = -EFAULT;
5013
5014 kfree(qsa);
5015 return ret;
5016}
5017
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005018static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5019{
Al Viro54563d42013-09-01 15:57:51 -04005020 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005021
5022 if (!capable(CAP_SYS_ADMIN))
5023 return -EPERM;
5024
5025 return btrfs_qgroup_wait_for_completion(root->fs_info);
5026}
5027
Hugo Millsabccd002014-01-30 20:17:00 +00005028static long _btrfs_ioctl_set_received_subvol(struct file *file,
5029 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005030{
Al Viro496ad9a2013-01-23 17:07:38 -05005031 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02005032 struct btrfs_root *root = BTRFS_I(inode)->root;
5033 struct btrfs_root_item *root_item = &root->root_item;
5034 struct btrfs_trans_handle *trans;
Deepa Dinamani04b285f2016-02-06 23:57:21 -08005035 struct timespec ct = current_fs_time(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005036 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005037 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005038
David Sterbabd60ea02014-01-16 15:50:22 +01005039 if (!inode_owner_or_capable(inode))
5040 return -EPERM;
5041
Alexander Block8ea05e32012-07-25 17:35:53 +02005042 ret = mnt_want_write_file(file);
5043 if (ret < 0)
5044 return ret;
5045
5046 down_write(&root->fs_info->subvol_sem);
5047
5048 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
5049 ret = -EINVAL;
5050 goto out;
5051 }
5052
5053 if (btrfs_root_readonly(root)) {
5054 ret = -EROFS;
5055 goto out;
5056 }
5057
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005058 /*
5059 * 1 - root item
5060 * 2 - uuid items (received uuid + subvol uuid)
5061 */
5062 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005063 if (IS_ERR(trans)) {
5064 ret = PTR_ERR(trans);
5065 trans = NULL;
5066 goto out;
5067 }
5068
5069 sa->rtransid = trans->transid;
5070 sa->rtime.sec = ct.tv_sec;
5071 sa->rtime.nsec = ct.tv_nsec;
5072
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005073 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5074 BTRFS_UUID_SIZE);
5075 if (received_uuid_changed &&
5076 !btrfs_is_empty_uuid(root_item->received_uuid))
5077 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
5078 root_item->received_uuid,
5079 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5080 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02005081 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5082 btrfs_set_root_stransid(root_item, sa->stransid);
5083 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005084 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5085 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5086 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5087 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005088
5089 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5090 &root->root_key, &root->root_item);
5091 if (ret < 0) {
5092 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02005093 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005094 }
5095 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5096 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
5097 sa->uuid,
5098 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5099 root->root_key.objectid);
5100 if (ret < 0 && ret != -EEXIST) {
5101 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02005102 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005103 }
5104 }
5105 ret = btrfs_commit_transaction(trans, root);
5106 if (ret < 0) {
5107 btrfs_abort_transaction(trans, root, ret);
5108 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02005109 }
5110
Hugo Millsabccd002014-01-30 20:17:00 +00005111out:
5112 up_write(&root->fs_info->subvol_sem);
5113 mnt_drop_write_file(file);
5114 return ret;
5115}
5116
5117#ifdef CONFIG_64BIT
5118static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5119 void __user *arg)
5120{
5121 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5122 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5123 int ret = 0;
5124
5125 args32 = memdup_user(arg, sizeof(*args32));
5126 if (IS_ERR(args32)) {
5127 ret = PTR_ERR(args32);
5128 args32 = NULL;
5129 goto out;
5130 }
5131
David Sterba8d2db782015-11-04 15:38:29 +01005132 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005133 if (!args64) {
5134 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005135 goto out;
5136 }
5137
5138 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5139 args64->stransid = args32->stransid;
5140 args64->rtransid = args32->rtransid;
5141 args64->stime.sec = args32->stime.sec;
5142 args64->stime.nsec = args32->stime.nsec;
5143 args64->rtime.sec = args32->rtime.sec;
5144 args64->rtime.nsec = args32->rtime.nsec;
5145 args64->flags = args32->flags;
5146
5147 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5148 if (ret)
5149 goto out;
5150
5151 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5152 args32->stransid = args64->stransid;
5153 args32->rtransid = args64->rtransid;
5154 args32->stime.sec = args64->stime.sec;
5155 args32->stime.nsec = args64->stime.nsec;
5156 args32->rtime.sec = args64->rtime.sec;
5157 args32->rtime.nsec = args64->rtime.nsec;
5158 args32->flags = args64->flags;
5159
5160 ret = copy_to_user(arg, args32, sizeof(*args32));
5161 if (ret)
5162 ret = -EFAULT;
5163
5164out:
5165 kfree(args32);
5166 kfree(args64);
5167 return ret;
5168}
5169#endif
5170
5171static long btrfs_ioctl_set_received_subvol(struct file *file,
5172 void __user *arg)
5173{
5174 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5175 int ret = 0;
5176
5177 sa = memdup_user(arg, sizeof(*sa));
5178 if (IS_ERR(sa)) {
5179 ret = PTR_ERR(sa);
5180 sa = NULL;
5181 goto out;
5182 }
5183
5184 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5185
5186 if (ret)
5187 goto out;
5188
Alexander Block8ea05e32012-07-25 17:35:53 +02005189 ret = copy_to_user(arg, sa, sizeof(*sa));
5190 if (ret)
5191 ret = -EFAULT;
5192
5193out:
5194 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005195 return ret;
5196}
5197
jeff.liu867ab662013-01-05 02:48:01 +00005198static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5199{
Al Viro6d0379e2013-06-16 19:32:35 +04005200 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005201 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005202 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005203 char label[BTRFS_LABEL_SIZE];
5204
5205 spin_lock(&root->fs_info->super_lock);
5206 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5207 spin_unlock(&root->fs_info->super_lock);
5208
5209 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005210
5211 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005212 btrfs_warn(root->fs_info,
5213 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00005214 }
5215
jeff.liu867ab662013-01-05 02:48:01 +00005216 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005217
5218 return ret ? -EFAULT : 0;
5219}
5220
jeff.liua8bfd4a2013-01-05 02:48:08 +00005221static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5222{
Al Viro6d0379e2013-06-16 19:32:35 +04005223 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005224 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5225 struct btrfs_trans_handle *trans;
5226 char label[BTRFS_LABEL_SIZE];
5227 int ret;
5228
5229 if (!capable(CAP_SYS_ADMIN))
5230 return -EPERM;
5231
5232 if (copy_from_user(label, arg, sizeof(label)))
5233 return -EFAULT;
5234
5235 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005236 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
jeff.liua8bfd4a2013-01-05 02:48:08 +00005237 BTRFS_LABEL_SIZE - 1);
5238 return -EINVAL;
5239 }
5240
5241 ret = mnt_want_write_file(file);
5242 if (ret)
5243 return ret;
5244
jeff.liua8bfd4a2013-01-05 02:48:08 +00005245 trans = btrfs_start_transaction(root, 0);
5246 if (IS_ERR(trans)) {
5247 ret = PTR_ERR(trans);
5248 goto out_unlock;
5249 }
5250
Anand Jaina1b83ac2013-07-19 17:39:32 +08005251 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005252 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005253 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005254 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005255
5256out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005257 mnt_drop_write_file(file);
5258 return ret;
5259}
5260
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005261#define INIT_FEATURE_FLAGS(suffix) \
5262 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5263 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5264 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5265
David Sterbad5131b62016-02-17 15:26:27 +01005266int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005267{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005268 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005269 INIT_FEATURE_FLAGS(SUPP),
5270 INIT_FEATURE_FLAGS(SAFE_SET),
5271 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5272 };
5273
5274 if (copy_to_user(arg, &features, sizeof(features)))
5275 return -EFAULT;
5276
5277 return 0;
5278}
5279
5280static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5281{
5282 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5283 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5284 struct btrfs_ioctl_feature_flags features;
5285
5286 features.compat_flags = btrfs_super_compat_flags(super_block);
5287 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5288 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5289
5290 if (copy_to_user(arg, &features, sizeof(features)))
5291 return -EFAULT;
5292
5293 return 0;
5294}
5295
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005296static int check_feature_bits(struct btrfs_root *root,
5297 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005298 u64 change_mask, u64 flags, u64 supported_flags,
5299 u64 safe_set, u64 safe_clear)
5300{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005301 const char *type = btrfs_feature_set_names[set];
5302 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005303 u64 disallowed, unsupported;
5304 u64 set_mask = flags & change_mask;
5305 u64 clear_mask = ~flags & change_mask;
5306
5307 unsupported = set_mask & ~supported_flags;
5308 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005309 names = btrfs_printable_features(set, unsupported);
5310 if (names) {
5311 btrfs_warn(root->fs_info,
5312 "this kernel does not support the %s feature bit%s",
5313 names, strchr(names, ',') ? "s" : "");
5314 kfree(names);
5315 } else
5316 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005317 "this kernel does not support %s bits 0x%llx",
5318 type, unsupported);
5319 return -EOPNOTSUPP;
5320 }
5321
5322 disallowed = set_mask & ~safe_set;
5323 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005324 names = btrfs_printable_features(set, disallowed);
5325 if (names) {
5326 btrfs_warn(root->fs_info,
5327 "can't set the %s feature bit%s while mounted",
5328 names, strchr(names, ',') ? "s" : "");
5329 kfree(names);
5330 } else
5331 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005332 "can't set %s bits 0x%llx while mounted",
5333 type, disallowed);
5334 return -EPERM;
5335 }
5336
5337 disallowed = clear_mask & ~safe_clear;
5338 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005339 names = btrfs_printable_features(set, disallowed);
5340 if (names) {
5341 btrfs_warn(root->fs_info,
5342 "can't clear the %s feature bit%s while mounted",
5343 names, strchr(names, ',') ? "s" : "");
5344 kfree(names);
5345 } else
5346 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005347 "can't clear %s bits 0x%llx while mounted",
5348 type, disallowed);
5349 return -EPERM;
5350 }
5351
5352 return 0;
5353}
5354
5355#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005356check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005357 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5358 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5359 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5360
5361static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5362{
5363 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5364 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5365 struct btrfs_ioctl_feature_flags flags[2];
5366 struct btrfs_trans_handle *trans;
5367 u64 newflags;
5368 int ret;
5369
5370 if (!capable(CAP_SYS_ADMIN))
5371 return -EPERM;
5372
5373 if (copy_from_user(flags, arg, sizeof(flags)))
5374 return -EFAULT;
5375
5376 /* Nothing to do */
5377 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5378 !flags[0].incompat_flags)
5379 return 0;
5380
5381 ret = check_feature(root, flags[0].compat_flags,
5382 flags[1].compat_flags, COMPAT);
5383 if (ret)
5384 return ret;
5385
5386 ret = check_feature(root, flags[0].compat_ro_flags,
5387 flags[1].compat_ro_flags, COMPAT_RO);
5388 if (ret)
5389 return ret;
5390
5391 ret = check_feature(root, flags[0].incompat_flags,
5392 flags[1].incompat_flags, INCOMPAT);
5393 if (ret)
5394 return ret;
5395
David Sterba8051aa12014-02-07 14:34:04 +01005396 trans = btrfs_start_transaction(root, 0);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005397 if (IS_ERR(trans))
5398 return PTR_ERR(trans);
5399
5400 spin_lock(&root->fs_info->super_lock);
5401 newflags = btrfs_super_compat_flags(super_block);
5402 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5403 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5404 btrfs_set_super_compat_flags(super_block, newflags);
5405
5406 newflags = btrfs_super_compat_ro_flags(super_block);
5407 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5408 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5409 btrfs_set_super_compat_ro_flags(super_block, newflags);
5410
5411 newflags = btrfs_super_incompat_flags(super_block);
5412 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5413 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5414 btrfs_set_super_incompat_flags(super_block, newflags);
5415 spin_unlock(&root->fs_info->super_lock);
5416
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005417 return btrfs_commit_transaction(trans, root);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005418}
5419
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005420long btrfs_ioctl(struct file *file, unsigned int
5421 cmd, unsigned long arg)
5422{
Al Viro496ad9a2013-01-23 17:07:38 -05005423 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005424 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005425
5426 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005427 case FS_IOC_GETFLAGS:
5428 return btrfs_ioctl_getflags(file, argp);
5429 case FS_IOC_SETFLAGS:
5430 return btrfs_ioctl_setflags(file, argp);
5431 case FS_IOC_GETVERSION:
5432 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005433 case FITRIM:
5434 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005435 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005436 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005437 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005438 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005439 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005440 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005441 case BTRFS_IOC_SUBVOL_CREATE_V2:
5442 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005443 case BTRFS_IOC_SNAP_DESTROY:
5444 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005445 case BTRFS_IOC_SUBVOL_GETFLAGS:
5446 return btrfs_ioctl_subvol_getflags(file, argp);
5447 case BTRFS_IOC_SUBVOL_SETFLAGS:
5448 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005449 case BTRFS_IOC_DEFAULT_SUBVOL:
5450 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005451 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005452 return btrfs_ioctl_defrag(file, NULL);
5453 case BTRFS_IOC_DEFRAG_RANGE:
5454 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005455 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005456 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005457 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005458 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005459 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005460 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005461 case BTRFS_IOC_FS_INFO:
5462 return btrfs_ioctl_fs_info(root, argp);
5463 case BTRFS_IOC_DEV_INFO:
5464 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005465 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005466 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005467 case BTRFS_IOC_TRANS_START:
5468 return btrfs_ioctl_trans_start(file);
5469 case BTRFS_IOC_TRANS_END:
5470 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005471 case BTRFS_IOC_TREE_SEARCH:
5472 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005473 case BTRFS_IOC_TREE_SEARCH_V2:
5474 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005475 case BTRFS_IOC_INO_LOOKUP:
5476 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005477 case BTRFS_IOC_INO_PATHS:
5478 return btrfs_ioctl_ino_to_path(root, argp);
5479 case BTRFS_IOC_LOGICAL_INO:
5480 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005481 case BTRFS_IOC_SPACE_INFO:
5482 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005483 case BTRFS_IOC_SYNC: {
5484 int ret;
5485
Miao Xie6c255e62014-03-06 13:55:01 +08005486 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005487 if (ret)
5488 return ret;
Al Viroddb52f42014-10-21 20:21:18 -04005489 ret = btrfs_sync_fs(file_inode(file)->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005490 /*
5491 * The transaction thread may want to do more work,
5492 * namely it pokes the cleaner ktread that will start
5493 * processing uncleaned subvols.
5494 */
5495 wake_up_process(root->fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005496 return ret;
5497 }
Sage Weil46204592010-10-29 15:41:32 -04005498 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005499 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005500 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005501 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005502 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005503 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005504 case BTRFS_IOC_SCRUB_CANCEL:
5505 return btrfs_ioctl_scrub_cancel(root, argp);
5506 case BTRFS_IOC_SCRUB_PROGRESS:
5507 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005508 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005509 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005510 case BTRFS_IOC_BALANCE_CTL:
5511 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005512 case BTRFS_IOC_BALANCE_PROGRESS:
5513 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005514 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5515 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005516#ifdef CONFIG_64BIT
5517 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5518 return btrfs_ioctl_set_received_subvol_32(file, argp);
5519#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005520 case BTRFS_IOC_SEND:
5521 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005522 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06005523 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005524 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005525 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005526 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005527 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005528 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005529 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005530 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005531 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005532 case BTRFS_IOC_QUOTA_RESCAN:
5533 return btrfs_ioctl_quota_rescan(file, argp);
5534 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5535 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005536 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5537 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005538 case BTRFS_IOC_DEV_REPLACE:
5539 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005540 case BTRFS_IOC_GET_FSLABEL:
5541 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005542 case BTRFS_IOC_SET_FSLABEL:
5543 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005544 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005545 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005546 case BTRFS_IOC_GET_FEATURES:
5547 return btrfs_ioctl_get_features(file, argp);
5548 case BTRFS_IOC_SET_FEATURES:
5549 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005550 }
5551
5552 return -ENOTTY;
5553}