blob: a018ea484d39a4d75f807803ad7ae37bc588e5db [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000043#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020044#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000045#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070046#include <linux/uaccess.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051#include "print-tree.h"
52#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040053#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020055#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040056#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020057#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010058#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000059#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040060#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070061#include "qgroup.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040062
Hugo Millsabccd002014-01-30 20:17:00 +000063#ifdef CONFIG_64BIT
64/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
65 * structures are incorrect, as the timespec structure from userspace
66 * is 4 bytes too small. We define these alternatives here to teach
67 * the kernel about the 32-bit struct packing.
68 */
69struct btrfs_ioctl_timespec_32 {
70 __u64 sec;
71 __u32 nsec;
72} __attribute__ ((__packed__));
73
74struct btrfs_ioctl_received_subvol_args_32 {
75 char uuid[BTRFS_UUID_SIZE]; /* in */
76 __u64 stransid; /* in */
77 __u64 rtransid; /* out */
78 struct btrfs_ioctl_timespec_32 stime; /* in */
79 struct btrfs_ioctl_timespec_32 rtime; /* out */
80 __u64 flags; /* in */
81 __u64 reserved[16]; /* in */
82} __attribute__ ((__packed__));
83
84#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
85 struct btrfs_ioctl_received_subvol_args_32)
86#endif
87
88
Mark Fasheh416161d2013-08-06 11:42:51 -070089static int btrfs_clone(struct inode *src, struct inode *inode,
90 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
91
Christoph Hellwig6cbff002009-04-17 10:37:41 +020092/* Mask out flags that are inappropriate for the given type of inode. */
93static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
94{
95 if (S_ISDIR(mode))
96 return flags;
97 else if (S_ISREG(mode))
98 return flags & ~FS_DIRSYNC_FL;
99 else
100 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
101}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400102
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200103/*
104 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
105 */
106static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
107{
108 unsigned int iflags = 0;
109
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
124
125 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
126 iflags |= FS_COMPR_FL;
127 else if (flags & BTRFS_INODE_NOCOMPRESS)
128 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129
130 return iflags;
131}
132
133/*
134 * Update inode->i_flags based on the btrfs internal flags.
135 */
136void btrfs_update_iflags(struct inode *inode)
137{
138 struct btrfs_inode *ip = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100139 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140
141 if (ip->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100142 new_fl |= S_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143 if (ip->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145 if (ip->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147 if (ip->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200149 if (ip->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_DIRSYNC;
151
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
154 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200155}
156
157/*
158 * Inherit flags from the parent inode.
159 *
Josef Bacike27425d2011-09-27 11:01:30 -0400160 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200161 */
162void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
163{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400164 unsigned int flags;
165
166 if (!dir)
167 return;
168
169 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200170
Josef Bacike27425d2011-09-27 11:01:30 -0400171 if (flags & BTRFS_INODE_NOCOMPRESS) {
172 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
173 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
174 } else if (flags & BTRFS_INODE_COMPRESS) {
175 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
176 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
177 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200178
Liu Bo213490b2012-09-11 08:33:50 -0600179 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400180 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600181 if (S_ISREG(inode->i_mode))
182 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
183 }
Josef Bacike27425d2011-09-27 11:01:30 -0400184
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 btrfs_update_iflags(inode);
186}
187
188static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
189{
Al Viro496ad9a2013-01-23 17:07:38 -0500190 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
192
193 if (copy_to_user(arg, &flags, sizeof(flags)))
194 return -EFAULT;
195 return 0;
196}
197
Liu Bo75e7cb72011-03-22 10:12:20 +0000198static int check_flags(unsigned int flags)
199{
200 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
201 FS_NOATIME_FL | FS_NODUMP_FL | \
202 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000203 FS_NOCOMP_FL | FS_COMPR_FL |
204 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000205 return -EOPNOTSUPP;
206
207 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
208 return -EINVAL;
209
Liu Bo75e7cb72011-03-22 10:12:20 +0000210 return 0;
211}
212
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200213static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
214{
Al Viro496ad9a2013-01-23 17:07:38 -0500215 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200216 struct btrfs_inode *ip = BTRFS_I(inode);
217 struct btrfs_root *root = ip->root;
218 struct btrfs_trans_handle *trans;
219 unsigned int flags, oldflags;
220 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800221 u64 ip_oldflags;
222 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600223 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200224
David Sterbabd60ea02014-01-16 15:50:22 +0100225 if (!inode_owner_or_capable(inode))
226 return -EPERM;
227
Li Zefanb83cc962010-12-20 16:04:08 +0800228 if (btrfs_root_readonly(root))
229 return -EROFS;
230
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200231 if (copy_from_user(&flags, arg, sizeof(flags)))
232 return -EFAULT;
233
Liu Bo75e7cb72011-03-22 10:12:20 +0000234 ret = check_flags(flags);
235 if (ret)
236 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200237
Jan Karae7848682012-06-12 16:20:32 +0200238 ret = mnt_want_write_file(file);
239 if (ret)
240 return ret;
241
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200242 mutex_lock(&inode->i_mutex);
243
Li Zefanf062abf2011-12-29 13:36:45 +0800244 ip_oldflags = ip->flags;
245 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600246 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800247
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200248 flags = btrfs_mask_flags(inode->i_mode, flags);
249 oldflags = btrfs_flags_to_ioctl(ip->flags);
250 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
251 if (!capable(CAP_LINUX_IMMUTABLE)) {
252 ret = -EPERM;
253 goto out_unlock;
254 }
255 }
256
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200257 if (flags & FS_SYNC_FL)
258 ip->flags |= BTRFS_INODE_SYNC;
259 else
260 ip->flags &= ~BTRFS_INODE_SYNC;
261 if (flags & FS_IMMUTABLE_FL)
262 ip->flags |= BTRFS_INODE_IMMUTABLE;
263 else
264 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
265 if (flags & FS_APPEND_FL)
266 ip->flags |= BTRFS_INODE_APPEND;
267 else
268 ip->flags &= ~BTRFS_INODE_APPEND;
269 if (flags & FS_NODUMP_FL)
270 ip->flags |= BTRFS_INODE_NODUMP;
271 else
272 ip->flags &= ~BTRFS_INODE_NODUMP;
273 if (flags & FS_NOATIME_FL)
274 ip->flags |= BTRFS_INODE_NOATIME;
275 else
276 ip->flags &= ~BTRFS_INODE_NOATIME;
277 if (flags & FS_DIRSYNC_FL)
278 ip->flags |= BTRFS_INODE_DIRSYNC;
279 else
280 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600281 if (flags & FS_NOCOW_FL) {
282 if (S_ISREG(mode)) {
283 /*
284 * It's safe to turn csums off here, no extents exist.
285 * Otherwise we want the flag to reflect the real COW
286 * status of the file and will not set it.
287 */
288 if (inode->i_size == 0)
289 ip->flags |= BTRFS_INODE_NODATACOW
290 | BTRFS_INODE_NODATASUM;
291 } else {
292 ip->flags |= BTRFS_INODE_NODATACOW;
293 }
294 } else {
295 /*
296 * Revert back under same assuptions as above
297 */
298 if (S_ISREG(mode)) {
299 if (inode->i_size == 0)
300 ip->flags &= ~(BTRFS_INODE_NODATACOW
301 | BTRFS_INODE_NODATASUM);
302 } else {
303 ip->flags &= ~BTRFS_INODE_NODATACOW;
304 }
305 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200306
Liu Bo75e7cb72011-03-22 10:12:20 +0000307 /*
308 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
309 * flag may be changed automatically if compression code won't make
310 * things smaller.
311 */
312 if (flags & FS_NOCOMP_FL) {
313 ip->flags &= ~BTRFS_INODE_COMPRESS;
314 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000315
316 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
317 if (ret && ret != -ENODATA)
318 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000319 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000320 const char *comp;
321
Liu Bo75e7cb72011-03-22 10:12:20 +0000322 ip->flags |= BTRFS_INODE_COMPRESS;
323 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000324
325 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
326 comp = "lzo";
327 else
328 comp = "zlib";
329 ret = btrfs_set_prop(inode, "btrfs.compression",
330 comp, strlen(comp), 0);
331 if (ret)
332 goto out_drop;
333
Li Zefanebcb9042011-04-15 03:03:17 +0000334 } else {
335 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000336 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200337
Li Zefan4da6f1a2011-12-29 13:39:50 +0800338 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800339 if (IS_ERR(trans)) {
340 ret = PTR_ERR(trans);
341 goto out_drop;
342 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200343
Li Zefan306424cc2011-12-14 20:12:02 -0500344 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400345 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500346 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200347 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200348
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200349 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800350 out_drop:
351 if (ret) {
352 ip->flags = ip_oldflags;
353 inode->i_flags = i_oldflags;
354 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200355
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200356 out_unlock:
357 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200358 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000359 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200360}
361
362static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
363{
Al Viro496ad9a2013-01-23 17:07:38 -0500364 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200365
366 return put_user(inode->i_generation, arg);
367}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400368
Li Dongyangf7039b12011-03-24 10:24:28 +0000369static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
370{
Al Viro54563d42013-09-01 15:57:51 -0400371 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000372 struct btrfs_device *device;
373 struct request_queue *q;
374 struct fstrim_range range;
375 u64 minlen = ULLONG_MAX;
376 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500377 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000378 int ret;
379
380 if (!capable(CAP_SYS_ADMIN))
381 return -EPERM;
382
Xiao Guangrong1f781602011-04-20 10:09:16 +0000383 rcu_read_lock();
384 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
385 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000386 if (!device->bdev)
387 continue;
388 q = bdev_get_queue(device->bdev);
389 if (blk_queue_discard(q)) {
390 num_devices++;
391 minlen = min((u64)q->limits.discard_granularity,
392 minlen);
393 }
394 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000395 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200396
Li Dongyangf7039b12011-03-24 10:24:28 +0000397 if (!num_devices)
398 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000399 if (copy_from_user(&range, arg, sizeof(range)))
400 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000401 if (range.start > total_bytes ||
402 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200403 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000404
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200405 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000406 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500407 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000408 if (ret < 0)
409 return ret;
410
411 if (copy_to_user(arg, &range, sizeof(range)))
412 return -EFAULT;
413
414 return 0;
415}
416
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200417int btrfs_is_empty_uuid(u8 *uuid)
418{
Chris Mason46e0f662013-11-15 12:14:55 +0100419 int i;
420
421 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
422 if (uuid[i])
423 return 0;
424 }
425 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200426}
427
Miao Xied5c12072013-02-28 10:04:33 +0000428static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400429 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400430 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200431 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000432 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400433{
434 struct btrfs_trans_handle *trans;
435 struct btrfs_key key;
436 struct btrfs_root_item root_item;
437 struct btrfs_inode_item *inode_item;
438 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000439 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400440 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000441 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200442 struct timespec cur_time = CURRENT_TIME;
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900443 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400444 int ret;
445 int err;
446 u64 objectid;
447 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500448 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000449 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200450 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400451
Li Zefan581bb052011-04-20 10:06:11 +0800452 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400453 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400454 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000455
Miao Xied5c12072013-02-28 10:04:33 +0000456 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400457 /*
Miao Xied5c12072013-02-28 10:04:33 +0000458 * The same as the snapshot creation, please see the comment
459 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400460 */
Miao Xied5c12072013-02-28 10:04:33 +0000461 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200462 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000463 if (ret)
464 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400465
Miao Xied5c12072013-02-28 10:04:33 +0000466 trans = btrfs_start_transaction(root, 0);
467 if (IS_ERR(trans)) {
468 ret = PTR_ERR(trans);
Liu Bode6e8202014-01-09 14:57:06 +0800469 btrfs_subvolume_release_metadata(root, &block_rsv,
470 qgroup_reserved);
471 return ret;
Miao Xied5c12072013-02-28 10:04:33 +0000472 }
473 trans->block_rsv = &block_rsv;
474 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400475
Miao Xie8696c532013-02-07 06:02:44 +0000476 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200477 if (ret)
478 goto fail;
479
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400480 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200481 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400482 if (IS_ERR(leaf)) {
483 ret = PTR_ERR(leaf);
484 goto fail;
485 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400486
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400487 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400488 btrfs_set_header_bytenr(leaf, leaf->start);
489 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400490 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491 btrfs_set_header_owner(leaf, objectid);
492
Ross Kirk0a4e5582013-09-24 10:12:38 +0100493 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400494 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400495 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200496 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400497 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400498 btrfs_mark_buffer_dirty(leaf);
499
Alexander Block8ea05e32012-07-25 17:35:53 +0200500 memset(&root_item, 0, sizeof(root_item));
501
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400502 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800503 btrfs_set_stack_inode_generation(inode_item, 1);
504 btrfs_set_stack_inode_size(inode_item, 3);
505 btrfs_set_stack_inode_nlink(inode_item, 1);
506 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
507 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400508
Qu Wenruo3cae2102013-07-16 11:19:18 +0800509 btrfs_set_root_flags(&root_item, 0);
510 btrfs_set_root_limit(&root_item, 0);
511 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000512
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400513 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400514 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515 btrfs_set_root_level(&root_item, 0);
516 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000517 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400518 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519
Alexander Block8ea05e32012-07-25 17:35:53 +0200520 btrfs_set_root_generation_v2(&root_item,
521 btrfs_root_generation(&root_item));
522 uuid_le_gen(&new_uuid);
523 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800524 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
525 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200526 root_item.ctime = root_item.otime;
527 btrfs_set_root_ctransid(&root_item, trans->transid);
528 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400529
Chris Mason925baed2008-06-25 16:01:30 -0400530 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400531 free_extent_buffer(leaf);
532 leaf = NULL;
533
534 btrfs_set_root_dirid(&root_item, new_dirid);
535
536 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400537 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400538 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
539 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
540 &root_item);
541 if (ret)
542 goto fail;
543
Yan, Zheng76dda932009-09-21 16:00:26 -0400544 key.offset = (u64)-1;
545 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100546 if (IS_ERR(new_root)) {
547 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
548 ret = PTR_ERR(new_root);
549 goto fail;
550 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400551
552 btrfs_record_root_in_trans(trans, new_root);
553
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000554 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700555 if (ret) {
556 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700558 goto fail;
559 }
560
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400561 /*
562 * insert the directory item
563 */
Chris Mason3de45862008-11-17 21:02:50 -0500564 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100565 if (ret) {
566 btrfs_abort_transaction(trans, root, ret);
567 goto fail;
568 }
Chris Mason3de45862008-11-17 21:02:50 -0500569
570 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800571 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500572 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573 if (ret) {
574 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400575 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100576 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500577
Yan Zheng52c26172009-01-05 15:43:43 -0500578 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
579 ret = btrfs_update_inode(trans, root, dir);
580 BUG_ON(ret);
581
Chris Mason0660b5a2008-11-17 20:37:39 -0500582 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400583 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800584 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500585 BUG_ON(ret);
586
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200587 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
588 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
589 objectid);
590 if (ret)
591 btrfs_abort_transaction(trans, root, ret);
592
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400593fail:
Miao Xied5c12072013-02-28 10:04:33 +0000594 trans->block_rsv = NULL;
595 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800596 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
597
Sage Weil72fd0322010-10-29 15:41:32 -0400598 if (async_transid) {
599 *async_transid = trans->transid;
600 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000601 if (err)
602 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400603 } else {
604 err = btrfs_commit_transaction(trans, root);
605 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400606 if (err && !ret)
607 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500608
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900609 if (!ret) {
610 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800611 if (IS_ERR(inode))
612 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900613 d_instantiate(dentry, inode);
614 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400615 return ret;
616}
617
Miao Xie8257b2d2014-03-06 13:38:19 +0800618static void btrfs_wait_nocow_write(struct btrfs_root *root)
619{
620 s64 writers;
621 DEFINE_WAIT(wait);
622
623 do {
624 prepare_to_wait(&root->subv_writers->wait, &wait,
625 TASK_UNINTERRUPTIBLE);
626
627 writers = percpu_counter_sum(&root->subv_writers->counter);
628 if (writers)
629 schedule();
630
631 finish_wait(&root->subv_writers->wait, &wait);
632 } while (writers);
633}
634
Miao Xiee9662f72013-02-28 10:01:15 +0000635static int create_snapshot(struct btrfs_root *root, struct inode *dir,
636 struct dentry *dentry, char *name, int namelen,
637 u64 *async_transid, bool readonly,
638 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400639{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000640 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400641 struct btrfs_pending_snapshot *pending_snapshot;
642 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000643 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400644
Miao Xie27cdeb72014-04-02 19:51:05 +0800645 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400646 return -EINVAL;
647
Miao Xie8257b2d2014-03-06 13:38:19 +0800648 atomic_inc(&root->will_be_snapshoted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100649 smp_mb__after_atomic();
Miao Xie8257b2d2014-03-06 13:38:19 +0800650 btrfs_wait_nocow_write(root);
651
Miao Xie6a038432013-05-15 07:48:24 +0000652 ret = btrfs_start_delalloc_inodes(root, 0);
653 if (ret)
Miao Xie8257b2d2014-03-06 13:38:19 +0800654 goto out;
Miao Xie6a038432013-05-15 07:48:24 +0000655
Miao Xieb0244192013-11-04 23:13:25 +0800656 btrfs_wait_ordered_extents(root, -1);
Miao Xie6a038432013-05-15 07:48:24 +0000657
Yan, Zhenga22285a2010-05-16 10:48:46 -0400658 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Miao Xie8257b2d2014-03-06 13:38:19 +0800659 if (!pending_snapshot) {
660 ret = -ENOMEM;
661 goto out;
662 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400663
Miao Xie66d8f3d2012-09-06 04:02:28 -0600664 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
665 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000666 /*
667 * 1 - parent dir inode
668 * 2 - dir entries
669 * 1 - root item
670 * 2 - root ref/backref
671 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200672 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000673 */
674 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200675 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400676 &pending_snapshot->qgroup_reserved,
677 false);
Miao Xied5c12072013-02-28 10:04:33 +0000678 if (ret)
Miao Xie8257b2d2014-03-06 13:38:19 +0800679 goto free;
Miao Xied5c12072013-02-28 10:04:33 +0000680
Yan, Zhenga22285a2010-05-16 10:48:46 -0400681 pending_snapshot->dentry = dentry;
682 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800683 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000684 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000685 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400686
Miao Xied5c12072013-02-28 10:04:33 +0000687 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400688 if (IS_ERR(trans)) {
689 ret = PTR_ERR(trans);
690 goto fail;
691 }
692
Josef Bacik83515832011-06-14 15:16:14 -0400693 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400694 list_add(&pending_snapshot->list,
695 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400696 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400697 if (async_transid) {
698 *async_transid = trans->transid;
699 ret = btrfs_commit_transaction_async(trans,
700 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000701 if (ret)
702 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400703 } else {
704 ret = btrfs_commit_transaction(trans,
705 root->fs_info->extent_root);
706 }
Miao Xieaec80302013-03-04 09:44:29 +0000707 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400708 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400709
710 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400711 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000712 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400713
Al Viro2fbe8c82011-07-16 21:38:06 -0400714 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000715 if (IS_ERR(inode)) {
716 ret = PTR_ERR(inode);
717 goto fail;
718 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900719
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000720 d_instantiate(dentry, inode);
721 ret = 0;
722fail:
Miao Xied5c12072013-02-28 10:04:33 +0000723 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
724 &pending_snapshot->block_rsv,
725 pending_snapshot->qgroup_reserved);
Miao Xie8257b2d2014-03-06 13:38:19 +0800726free:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400727 kfree(pending_snapshot);
Miao Xie8257b2d2014-03-06 13:38:19 +0800728out:
729 atomic_dec(&root->will_be_snapshoted);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400730 return ret;
731}
732
Sage Weil4260f7c2010-10-29 15:46:43 -0400733/* copy of check_sticky in fs/namei.c()
734* It's inline, so penalty for filesystems that don't use sticky bit is
735* minimal.
736*/
737static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
738{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800739 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400740
741 if (!(dir->i_mode & S_ISVTX))
742 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800743 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400744 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800745 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400746 return 0;
747 return !capable(CAP_FOWNER);
748}
749
750/* copy of may_delete in fs/namei.c()
751 * Check whether we can remove a link victim from directory dir, check
752 * whether the type of victim is right.
753 * 1. We can't do it if dir is read-only (done in permission())
754 * 2. We should have write and exec permissions on dir
755 * 3. We can't remove anything from append-only dir
756 * 4. We can't do anything with immutable dir (done in permission())
757 * 5. If the sticky bit on dir is set we should either
758 * a. be owner of dir, or
759 * b. be owner of victim, or
760 * c. have CAP_FOWNER capability
761 * 6. If the victim is append-only or immutable we can't do antyhing with
762 * links pointing to it.
763 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
764 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
765 * 9. We can't remove a root or mountpoint.
766 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
767 * nfs_async_unlink().
768 */
769
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530770static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400771{
772 int error;
773
774 if (!victim->d_inode)
775 return -ENOENT;
776
777 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400778 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400779
780 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
781 if (error)
782 return error;
783 if (IS_APPEND(dir))
784 return -EPERM;
785 if (btrfs_check_sticky(dir, victim->d_inode)||
786 IS_APPEND(victim->d_inode)||
787 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
788 return -EPERM;
789 if (isdir) {
790 if (!S_ISDIR(victim->d_inode->i_mode))
791 return -ENOTDIR;
792 if (IS_ROOT(victim))
793 return -EBUSY;
794 } else if (S_ISDIR(victim->d_inode->i_mode))
795 return -EISDIR;
796 if (IS_DEADDIR(dir))
797 return -ENOENT;
798 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
799 return -EBUSY;
800 return 0;
801}
802
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400803/* copy of may_create in fs/namei.c() */
804static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
805{
806 if (child->d_inode)
807 return -EEXIST;
808 if (IS_DEADDIR(dir))
809 return -ENOENT;
810 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
811}
812
813/*
814 * Create a new subvolume below @parent. This is largely modeled after
815 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
816 * inside this filesystem so it's quite a bit simpler.
817 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400818static noinline int btrfs_mksubvol(struct path *parent,
819 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400820 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200821 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000822 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400823{
Yan, Zheng76dda932009-09-21 16:00:26 -0400824 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400825 struct dentry *dentry;
826 int error;
827
David Sterba5c50c9b2013-03-22 18:12:51 +0000828 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
829 if (error == -EINTR)
830 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400831
832 dentry = lookup_one_len(name, parent->dentry, namelen);
833 error = PTR_ERR(dentry);
834 if (IS_ERR(dentry))
835 goto out_unlock;
836
837 error = -EEXIST;
838 if (dentry->d_inode)
839 goto out_dput;
840
Yan, Zheng76dda932009-09-21 16:00:26 -0400841 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400842 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600843 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400844
Chris Mason9c520572012-12-17 14:26:57 -0500845 /*
846 * even if this name doesn't exist, we may get hash collisions.
847 * check for them now when we can safely fail
848 */
849 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
850 dir->i_ino, name,
851 namelen);
852 if (error)
853 goto out_dput;
854
Yan, Zheng76dda932009-09-21 16:00:26 -0400855 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
856
857 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
858 goto out_up_read;
859
Chris Mason3de45862008-11-17 21:02:50 -0500860 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000861 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200862 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500863 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000864 error = create_subvol(dir, dentry, name, namelen,
865 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500866 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400867 if (!error)
868 fsnotify_mkdir(dir, dentry);
869out_up_read:
870 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400871out_dput:
872 dput(dentry);
873out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400874 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400875 return error;
876}
877
Chris Mason4cb53002011-05-24 15:35:30 -0400878/*
879 * When we're defragging a range, we don't want to kick it off again
880 * if it is really just waiting for delalloc to send it down.
881 * If we find a nice big extent or delalloc range for the bytes in the
882 * file you want to defrag, we return 0 to let you know to skip this
883 * part of the file
884 */
885static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
886{
887 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
888 struct extent_map *em = NULL;
889 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
890 u64 end;
891
892 read_lock(&em_tree->lock);
893 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
894 read_unlock(&em_tree->lock);
895
896 if (em) {
897 end = extent_map_end(em);
898 free_extent_map(em);
899 if (end - offset > thresh)
900 return 0;
901 }
902 /* if we already have a nice delalloc here, just stop */
903 thresh /= 2;
904 end = count_range_bits(io_tree, &offset, offset + thresh,
905 thresh, EXTENT_DELALLOC, 1);
906 if (end >= thresh)
907 return 0;
908 return 1;
909}
910
911/*
912 * helper function to walk through a file and find extents
913 * newer than a specific transid, and smaller than thresh.
914 *
915 * This is used by the defragging code to find new and small
916 * extents
917 */
918static int find_new_extents(struct btrfs_root *root,
919 struct inode *inode, u64 newer_than,
920 u64 *off, int thresh)
921{
922 struct btrfs_path *path;
923 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400924 struct extent_buffer *leaf;
925 struct btrfs_file_extent_item *extent;
926 int type;
927 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000928 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400929
930 path = btrfs_alloc_path();
931 if (!path)
932 return -ENOMEM;
933
David Sterbaa4689d22011-05-31 17:08:14 +0000934 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400935 min_key.type = BTRFS_EXTENT_DATA_KEY;
936 min_key.offset = *off;
937
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530938 while (1) {
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000939 path->keep_locks = 1;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100940 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400941 if (ret != 0)
942 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000943 path->keep_locks = 0;
944 btrfs_unlock_up_safe(path, 1);
945process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +0000946 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400947 goto none;
948 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
949 goto none;
950
951 leaf = path->nodes[0];
952 extent = btrfs_item_ptr(leaf, path->slots[0],
953 struct btrfs_file_extent_item);
954
955 type = btrfs_file_extent_type(leaf, extent);
956 if (type == BTRFS_FILE_EXTENT_REG &&
957 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
958 check_defrag_in_cache(inode, min_key.offset, thresh)) {
959 *off = min_key.offset;
960 btrfs_free_path(path);
961 return 0;
962 }
963
Filipe Mananaf094c9bd2014-03-12 01:28:24 +0000964 path->slots[0]++;
965 if (path->slots[0] < btrfs_header_nritems(leaf)) {
966 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
967 goto process_slot;
968 }
969
Chris Mason4cb53002011-05-24 15:35:30 -0400970 if (min_key.offset == (u64)-1)
971 goto none;
972
973 min_key.offset++;
974 btrfs_release_path(path);
975 }
976none:
977 btrfs_free_path(path);
978 return -ENOENT;
979}
980
Li Zefan6c282eb2012-06-11 16:03:35 +0800981static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400982{
983 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500984 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800985 struct extent_map *em;
986 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500987
988 /*
989 * hopefully we have this extent in the tree already, try without
990 * the full extent lock
991 */
992 read_lock(&em_tree->lock);
993 em = lookup_extent_mapping(em_tree, start, len);
994 read_unlock(&em_tree->lock);
995
996 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +0000997 struct extent_state *cached = NULL;
998 u64 end = start + len - 1;
999
Chris Mason940100a2010-03-10 10:52:59 -05001000 /* get the big lock and read metadata off disk */
Filipe Manana308d9802014-03-11 13:56:15 +00001001 lock_extent_bits(io_tree, start, end, 0, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001002 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Filipe Manana308d9802014-03-11 13:56:15 +00001003 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -05001004
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001005 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001006 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001007 }
1008
Li Zefan6c282eb2012-06-11 16:03:35 +08001009 return em;
1010}
1011
1012static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1013{
1014 struct extent_map *next;
1015 bool ret = true;
1016
1017 /* this is the last extent */
1018 if (em->start + em->len >= i_size_read(inode))
1019 return false;
1020
1021 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001022 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1023 ret = false;
1024 else if ((em->block_start + em->block_len == next->block_start) &&
1025 (em->block_len > 128 * 1024 && next->block_len > 128 * 1024))
Li Zefan6c282eb2012-06-11 16:03:35 +08001026 ret = false;
1027
1028 free_extent_map(next);
1029 return ret;
1030}
1031
1032static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001033 u64 *last_len, u64 *skip, u64 *defrag_end,
1034 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001035{
1036 struct extent_map *em;
1037 int ret = 1;
1038 bool next_mergeable = true;
1039
1040 /*
1041 * make sure that once we start defragging an extent, we keep on
1042 * defragging it
1043 */
1044 if (start < *defrag_end)
1045 return 1;
1046
1047 *skip = 0;
1048
1049 em = defrag_lookup_extent(inode, start);
1050 if (!em)
1051 return 0;
1052
Chris Mason940100a2010-03-10 10:52:59 -05001053 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001054 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001055 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001056 goto out;
1057 }
1058
Li Zefan6c282eb2012-06-11 16:03:35 +08001059 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001060 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001061 * we hit a real extent, if it is big or the next extent is not a
1062 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001063 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001064 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +08001065 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -05001066 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001067out:
Chris Mason940100a2010-03-10 10:52:59 -05001068 /*
1069 * last_len ends up being a counter of how many bytes we've defragged.
1070 * every time we choose not to defrag an extent, we reset *last_len
1071 * so that the next tiny extent will force a defrag.
1072 *
1073 * The end result of this is that tiny extents before a single big
1074 * extent will force at least part of that big extent to be defragged.
1075 */
1076 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001077 *defrag_end = extent_map_end(em);
1078 } else {
1079 *last_len = 0;
1080 *skip = extent_map_end(em);
1081 *defrag_end = 0;
1082 }
1083
1084 free_extent_map(em);
1085 return ret;
1086}
1087
Chris Mason4cb53002011-05-24 15:35:30 -04001088/*
1089 * it doesn't do much good to defrag one or two pages
1090 * at a time. This pulls in a nice chunk of pages
1091 * to COW and defrag.
1092 *
1093 * It also makes sure the delalloc code has enough
1094 * dirty data to avoid making new small extents as part
1095 * of the defrag
1096 *
1097 * It's a good idea to start RA on this range
1098 * before calling this.
1099 */
1100static int cluster_pages_for_defrag(struct inode *inode,
1101 struct page **pages,
1102 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001103 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001104{
Chris Mason4cb53002011-05-24 15:35:30 -04001105 unsigned long file_end;
1106 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001107 u64 page_start;
1108 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001109 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001110 int ret;
1111 int i;
1112 int i_done;
1113 struct btrfs_ordered_extent *ordered;
1114 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001115 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001116 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001117
Chris Mason4cb53002011-05-24 15:35:30 -04001118 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001119 if (!isize || start_index > file_end)
1120 return 0;
1121
1122 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001123
1124 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001125 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001126 if (ret)
1127 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001128 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001129 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001130
1131 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001132 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001133 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001134again:
Josef Bacika94733d2011-07-11 10:47:06 -04001135 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001136 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001137 if (!page)
1138 break;
1139
Miao Xie600a45e2012-02-16 15:01:24 +08001140 page_start = page_offset(page);
1141 page_end = page_start + PAGE_CACHE_SIZE - 1;
1142 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001143 lock_extent_bits(tree, page_start, page_end,
1144 0, &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001145 ordered = btrfs_lookup_ordered_extent(inode,
1146 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001147 unlock_extent_cached(tree, page_start, page_end,
1148 &cached_state, GFP_NOFS);
Miao Xie600a45e2012-02-16 15:01:24 +08001149 if (!ordered)
1150 break;
1151
1152 unlock_page(page);
1153 btrfs_start_ordered_extent(inode, ordered, 1);
1154 btrfs_put_ordered_extent(ordered);
1155 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001156 /*
1157 * we unlocked the page above, so we need check if
1158 * it was released or not.
1159 */
1160 if (page->mapping != inode->i_mapping) {
1161 unlock_page(page);
1162 page_cache_release(page);
1163 goto again;
1164 }
Miao Xie600a45e2012-02-16 15:01:24 +08001165 }
1166
Chris Mason4cb53002011-05-24 15:35:30 -04001167 if (!PageUptodate(page)) {
1168 btrfs_readpage(NULL, page);
1169 lock_page(page);
1170 if (!PageUptodate(page)) {
1171 unlock_page(page);
1172 page_cache_release(page);
1173 ret = -EIO;
1174 break;
1175 }
1176 }
Miao Xie600a45e2012-02-16 15:01:24 +08001177
Miao Xie600a45e2012-02-16 15:01:24 +08001178 if (page->mapping != inode->i_mapping) {
1179 unlock_page(page);
1180 page_cache_release(page);
1181 goto again;
1182 }
1183
Chris Mason4cb53002011-05-24 15:35:30 -04001184 pages[i] = page;
1185 i_done++;
1186 }
1187 if (!i_done || ret)
1188 goto out;
1189
1190 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1191 goto out;
1192
1193 /*
1194 * so now we have a nice long stream of locked
1195 * and up to date pages, lets wait on them
1196 */
1197 for (i = 0; i < i_done; i++)
1198 wait_on_page_writeback(pages[i]);
1199
1200 page_start = page_offset(pages[0]);
1201 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1202
1203 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001204 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001205 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1206 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001207 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1208 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001209
Liu Bo1f12bd02012-03-29 09:57:44 -04001210 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001211 spin_lock(&BTRFS_I(inode)->lock);
1212 BTRFS_I(inode)->outstanding_extents++;
1213 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001214 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001215 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001216 }
1217
1218
Liu Bo9e8a4a82012-09-05 19:10:51 -06001219 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1220 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001221
1222 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1223 page_start, page_end - 1, &cached_state,
1224 GFP_NOFS);
1225
1226 for (i = 0; i < i_done; i++) {
1227 clear_page_dirty_for_io(pages[i]);
1228 ClearPageChecked(pages[i]);
1229 set_page_extent_mapped(pages[i]);
1230 set_page_dirty(pages[i]);
1231 unlock_page(pages[i]);
1232 page_cache_release(pages[i]);
1233 }
1234 return i_done;
1235out:
1236 for (i = 0; i < i_done; i++) {
1237 unlock_page(pages[i]);
1238 page_cache_release(pages[i]);
1239 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001240 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001241 return ret;
1242
1243}
1244
1245int btrfs_defrag_file(struct inode *inode, struct file *file,
1246 struct btrfs_ioctl_defrag_range_args *range,
1247 u64 newer_than, unsigned long max_to_defrag)
1248{
1249 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001250 struct file_ra_state *ra = NULL;
1251 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001252 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001253 u64 last_len = 0;
1254 u64 skip = 0;
1255 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001256 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001257 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001258 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001259 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001260 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001261 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001262 int extent_thresh = range->extent_thresh;
Justin Maggardc41570c2014-01-21 11:18:29 -08001263 unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1264 unsigned long cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001265 u64 new_align = ~((u64)128 * 1024 - 1);
1266 struct page **pages = NULL;
1267
Liu Bo0abd5b12013-04-16 09:20:28 +00001268 if (isize == 0)
1269 return 0;
1270
1271 if (range->start >= isize)
1272 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001273
1274 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1275 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1276 return -EINVAL;
1277 if (range->compress_type)
1278 compress_type = range->compress_type;
1279 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001280
Liu Bo0abd5b12013-04-16 09:20:28 +00001281 if (extent_thresh == 0)
1282 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001283
Chris Mason4cb53002011-05-24 15:35:30 -04001284 /*
1285 * if we were not given a file, allocate a readahead
1286 * context
1287 */
1288 if (!file) {
1289 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1290 if (!ra)
1291 return -ENOMEM;
1292 file_ra_state_init(ra, inode->i_mapping);
1293 } else {
1294 ra = &file->f_ra;
1295 }
1296
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301297 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001298 GFP_NOFS);
1299 if (!pages) {
1300 ret = -ENOMEM;
1301 goto out_ra;
1302 }
1303
1304 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001305 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001306 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001307 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1308 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001309 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001310 }
1311
Chris Mason4cb53002011-05-24 15:35:30 -04001312 if (newer_than) {
1313 ret = find_new_extents(root, inode, newer_than,
1314 &newer_off, 64 * 1024);
1315 if (!ret) {
1316 range->start = newer_off;
1317 /*
1318 * we always align our defrag to help keep
1319 * the extents in the file evenly spaced
1320 */
1321 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001322 } else
1323 goto out_ra;
1324 } else {
1325 i = range->start >> PAGE_CACHE_SHIFT;
1326 }
1327 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001328 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001329
Li Zefan2a0f7f52011-10-10 15:43:34 -04001330 /*
1331 * make writeback starts from i, so the defrag range can be
1332 * written sequentially.
1333 */
1334 if (i < inode->i_mapping->writeback_index)
1335 inode->i_mapping->writeback_index = i;
1336
Chris Masonf7f43cc2011-10-11 11:41:40 -04001337 while (i <= last_index && defrag_count < max_to_defrag &&
1338 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1339 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001340 /*
1341 * make sure we stop running if someone unmounts
1342 * the FS
1343 */
1344 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1345 break;
1346
David Sterba210549e2013-02-09 23:38:06 +00001347 if (btrfs_defrag_cancelled(root->fs_info)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001348 printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
David Sterba210549e2013-02-09 23:38:06 +00001349 ret = -EAGAIN;
1350 break;
1351 }
1352
Liu Bo66c26892012-03-29 09:57:45 -04001353 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001354 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001355 &defrag_end, range->flags &
1356 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001357 unsigned long next;
1358 /*
1359 * the should_defrag function tells us how much to skip
1360 * bump our counter by the suggested amount
1361 */
1362 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1363 i = max(i + 1, next);
1364 continue;
1365 }
Li Zefan008873e2011-09-02 15:57:07 +08001366
1367 if (!newer_than) {
1368 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1369 PAGE_CACHE_SHIFT) - i;
1370 cluster = min(cluster, max_cluster);
1371 } else {
1372 cluster = max_cluster;
1373 }
1374
Li Zefan008873e2011-09-02 15:57:07 +08001375 if (i + cluster > ra_index) {
1376 ra_index = max(i, ra_index);
1377 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1378 cluster);
1379 ra_index += max_cluster;
1380 }
Chris Mason940100a2010-03-10 10:52:59 -05001381
Liu Boecb8bea2012-03-29 09:57:44 -04001382 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001383 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1384 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001385 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001386 if (ret < 0) {
1387 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001388 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001389 }
Chris Mason940100a2010-03-10 10:52:59 -05001390
Chris Mason4cb53002011-05-24 15:35:30 -04001391 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001392 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001393 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001394
1395 if (newer_than) {
1396 if (newer_off == (u64)-1)
1397 break;
1398
Liu Boe1f041e2012-03-29 09:57:45 -04001399 if (ret > 0)
1400 i += ret;
1401
Chris Mason4cb53002011-05-24 15:35:30 -04001402 newer_off = max(newer_off + 1,
1403 (u64)i << PAGE_CACHE_SHIFT);
1404
1405 ret = find_new_extents(root, inode,
1406 newer_than, &newer_off,
1407 64 * 1024);
1408 if (!ret) {
1409 range->start = newer_off;
1410 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001411 } else {
1412 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001413 }
Chris Mason4cb53002011-05-24 15:35:30 -04001414 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001415 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001416 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001417 last_len += ret << PAGE_CACHE_SHIFT;
1418 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001419 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001420 last_len = 0;
1421 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001422 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001423 }
1424
Filipe Mananadec8ef92014-03-01 10:55:54 +00001425 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001426 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001427 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1428 &BTRFS_I(inode)->runtime_flags))
1429 filemap_flush(inode->i_mapping);
1430 }
Chris Mason1e701a32010-03-11 09:42:04 -05001431
1432 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1433 /* the filemap_flush will queue IO into the worker threads, but
1434 * we have to make sure the IO is actually started and that
1435 * ordered extents get created before we return
1436 */
1437 atomic_inc(&root->fs_info->async_submit_draining);
1438 while (atomic_read(&root->fs_info->nr_async_submits) ||
1439 atomic_read(&root->fs_info->async_delalloc_pages)) {
1440 wait_event(root->fs_info->async_submit_wait,
1441 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1442 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1443 }
1444 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001445 }
1446
Li Zefan1a419d82010-10-25 15:12:50 +08001447 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001448 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001449 }
1450
Diego Calleja60ccf822011-09-01 16:33:57 +02001451 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001452
Chris Mason4cb53002011-05-24 15:35:30 -04001453out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001454 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1455 mutex_lock(&inode->i_mutex);
1456 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1457 mutex_unlock(&inode->i_mutex);
1458 }
Chris Mason4cb53002011-05-24 15:35:30 -04001459 if (!file)
1460 kfree(ra);
1461 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001462 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001463}
1464
Miao Xie198605a2012-11-26 08:43:45 +00001465static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001466 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001467{
1468 u64 new_size;
1469 u64 old_size;
1470 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001471 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001472 struct btrfs_ioctl_vol_args *vol_args;
1473 struct btrfs_trans_handle *trans;
1474 struct btrfs_device *device = NULL;
1475 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001476 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001477 char *devstr = NULL;
1478 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001479 int mod = 0;
1480
Chris Masone441d542009-01-05 16:57:23 -05001481 if (!capable(CAP_SYS_ADMIN))
1482 return -EPERM;
1483
Miao Xie198605a2012-11-26 08:43:45 +00001484 ret = mnt_want_write_file(file);
1485 if (ret)
1486 return ret;
1487
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001488 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1489 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001490 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001491 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001492 }
1493
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001494 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001495 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001496 if (IS_ERR(vol_args)) {
1497 ret = PTR_ERR(vol_args);
1498 goto out;
1499 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001500
1501 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001502
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001503 sizestr = vol_args->name;
1504 devstr = strchr(sizestr, ':');
1505 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001506 sizestr = devstr + 1;
1507 *devstr = '\0';
1508 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001509 ret = kstrtoull(devstr, 10, &devid);
1510 if (ret)
1511 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001512 if (!devid) {
1513 ret = -EINVAL;
1514 goto out_free;
1515 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001516 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001517 }
Miao Xiedba60f32012-12-21 09:19:51 +00001518
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001519 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001521 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001522 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001523 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001524 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001525 }
Miao Xiedba60f32012-12-21 09:19:51 +00001526
1527 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001528 btrfs_info(root->fs_info,
1529 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001530 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001531 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001532 goto out_free;
1533 }
1534
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001535 if (!strcmp(sizestr, "max"))
1536 new_size = device->bdev->bd_inode->i_size;
1537 else {
1538 if (sizestr[0] == '-') {
1539 mod = -1;
1540 sizestr++;
1541 } else if (sizestr[0] == '+') {
1542 mod = 1;
1543 sizestr++;
1544 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001545 new_size = memparse(sizestr, &retptr);
1546 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001547 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001548 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001549 }
1550 }
1551
Stefan Behrens63a212a2012-11-05 18:29:28 +01001552 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001553 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001554 goto out_free;
1555 }
1556
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001557 old_size = device->total_bytes;
1558
1559 if (mod < 0) {
1560 if (new_size > old_size) {
1561 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001562 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001563 }
1564 new_size = old_size - new_size;
1565 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001566 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001567 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001568 goto out_free;
1569 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001570 new_size = old_size + new_size;
1571 }
1572
1573 if (new_size < 256 * 1024 * 1024) {
1574 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001575 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001576 }
1577 if (new_size > device->bdev->bd_inode->i_size) {
1578 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001579 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001580 }
1581
1582 do_div(new_size, root->sectorsize);
1583 new_size *= root->sectorsize;
1584
Frank Holtonefe120a2013-12-20 11:37:06 -05001585 printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001586 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001587
1588 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001589 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001590 if (IS_ERR(trans)) {
1591 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001592 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001593 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001594 ret = btrfs_grow_device(trans, device, new_size);
1595 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001596 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001597 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001598 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001599
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001600out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001601 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001602out:
1603 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001604 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001605 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001606 return ret;
1607}
1608
Sage Weil72fd0322010-10-29 15:41:32 -04001609static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001610 char *name, unsigned long fd, int subvol,
1611 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001612 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001613{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001615 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616
Liu Boa874a632012-06-29 03:58:46 -06001617 ret = mnt_want_write_file(file);
1618 if (ret)
1619 goto out;
1620
Sage Weil72fd0322010-10-29 15:41:32 -04001621 namelen = strlen(name);
1622 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001623 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001624 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001625 }
1626
Chris Mason16780ca2012-02-20 22:14:55 -05001627 if (name[0] == '.' &&
1628 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1629 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001630 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001631 }
1632
Chris Mason3de45862008-11-17 21:02:50 -05001633 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001634 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001635 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001636 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001637 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001638 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001639 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001640 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001641 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001642 }
1643
Al Viro496ad9a2013-01-23 17:07:38 -05001644 src_inode = file_inode(src.file);
1645 if (src_inode->i_sb != file_inode(file)->i_sb) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001646 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1647 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001648 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001649 } else if (!inode_owner_or_capable(src_inode)) {
1650 /*
1651 * Subvolume creation is not restricted, but snapshots
1652 * are limited to own subvolumes only
1653 */
1654 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001655 } else {
1656 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1657 BTRFS_I(src_inode)->root,
1658 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001659 }
Al Viro2903ff02012-08-28 12:52:22 -04001660 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001661 }
Liu Boa874a632012-06-29 03:58:46 -06001662out_drop_write:
1663 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001664out:
Sage Weil72fd0322010-10-29 15:41:32 -04001665 return ret;
1666}
1667
1668static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001669 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001670{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001671 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001672 int ret;
1673
Li Zefanfa0d2b92010-12-20 15:53:28 +08001674 vol_args = memdup_user(arg, sizeof(*vol_args));
1675 if (IS_ERR(vol_args))
1676 return PTR_ERR(vol_args);
1677 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001678
Li Zefanfa0d2b92010-12-20 15:53:28 +08001679 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001680 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001681 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001682
Li Zefanfa0d2b92010-12-20 15:53:28 +08001683 kfree(vol_args);
1684 return ret;
1685}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001686
Li Zefanfa0d2b92010-12-20 15:53:28 +08001687static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1688 void __user *arg, int subvol)
1689{
1690 struct btrfs_ioctl_vol_args_v2 *vol_args;
1691 int ret;
1692 u64 transid = 0;
1693 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001694 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001695 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001696
Li Zefanfa0d2b92010-12-20 15:53:28 +08001697 vol_args = memdup_user(arg, sizeof(*vol_args));
1698 if (IS_ERR(vol_args))
1699 return PTR_ERR(vol_args);
1700 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001701
Li Zefanb83cc962010-12-20 16:04:08 +08001702 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001703 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1704 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001705 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001706 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001707 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001708
1709 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1710 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001711 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1712 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001713 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1714 if (vol_args->size > PAGE_CACHE_SIZE) {
1715 ret = -EINVAL;
1716 goto out;
1717 }
1718 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1719 if (IS_ERR(inherit)) {
1720 ret = PTR_ERR(inherit);
1721 goto out;
1722 }
1723 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001724
1725 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001726 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001727 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001728
1729 if (ret == 0 && ptr &&
1730 copy_to_user(arg +
1731 offsetof(struct btrfs_ioctl_vol_args_v2,
1732 transid), ptr, sizeof(*ptr)))
1733 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001734out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001735 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001736 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001737 return ret;
1738}
1739
Li Zefan0caa1022010-12-20 16:30:25 +08001740static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1741 void __user *arg)
1742{
Al Viro496ad9a2013-01-23 17:07:38 -05001743 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001744 struct btrfs_root *root = BTRFS_I(inode)->root;
1745 int ret = 0;
1746 u64 flags = 0;
1747
Li Zefan33345d012011-04-20 10:31:50 +08001748 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001749 return -EINVAL;
1750
1751 down_read(&root->fs_info->subvol_sem);
1752 if (btrfs_root_readonly(root))
1753 flags |= BTRFS_SUBVOL_RDONLY;
1754 up_read(&root->fs_info->subvol_sem);
1755
1756 if (copy_to_user(arg, &flags, sizeof(flags)))
1757 ret = -EFAULT;
1758
1759 return ret;
1760}
1761
1762static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1763 void __user *arg)
1764{
Al Viro496ad9a2013-01-23 17:07:38 -05001765 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001766 struct btrfs_root *root = BTRFS_I(inode)->root;
1767 struct btrfs_trans_handle *trans;
1768 u64 root_flags;
1769 u64 flags;
1770 int ret = 0;
1771
David Sterbabd60ea02014-01-16 15:50:22 +01001772 if (!inode_owner_or_capable(inode))
1773 return -EPERM;
1774
Liu Bob9ca0662012-06-29 03:58:49 -06001775 ret = mnt_want_write_file(file);
1776 if (ret)
1777 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001778
Liu Bob9ca0662012-06-29 03:58:49 -06001779 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1780 ret = -EINVAL;
1781 goto out_drop_write;
1782 }
Li Zefan0caa1022010-12-20 16:30:25 +08001783
Liu Bob9ca0662012-06-29 03:58:49 -06001784 if (copy_from_user(&flags, arg, sizeof(flags))) {
1785 ret = -EFAULT;
1786 goto out_drop_write;
1787 }
Li Zefan0caa1022010-12-20 16:30:25 +08001788
Liu Bob9ca0662012-06-29 03:58:49 -06001789 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1790 ret = -EINVAL;
1791 goto out_drop_write;
1792 }
Li Zefan0caa1022010-12-20 16:30:25 +08001793
Liu Bob9ca0662012-06-29 03:58:49 -06001794 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1795 ret = -EOPNOTSUPP;
1796 goto out_drop_write;
1797 }
Li Zefan0caa1022010-12-20 16:30:25 +08001798
1799 down_write(&root->fs_info->subvol_sem);
1800
1801 /* nothing to do */
1802 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001803 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001804
1805 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001806 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001807 btrfs_set_root_flags(&root->root_item,
1808 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001809 } else {
1810 /*
1811 * Block RO -> RW transition if this subvolume is involved in
1812 * send
1813 */
1814 spin_lock(&root->root_item_lock);
1815 if (root->send_in_progress == 0) {
1816 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001817 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001818 spin_unlock(&root->root_item_lock);
1819 } else {
1820 spin_unlock(&root->root_item_lock);
1821 btrfs_warn(root->fs_info,
1822 "Attempt to set subvolume %llu read-write during send",
1823 root->root_key.objectid);
1824 ret = -EPERM;
1825 goto out_drop_sem;
1826 }
1827 }
Li Zefan0caa1022010-12-20 16:30:25 +08001828
1829 trans = btrfs_start_transaction(root, 1);
1830 if (IS_ERR(trans)) {
1831 ret = PTR_ERR(trans);
1832 goto out_reset;
1833 }
1834
Li Zefanb4dc2b82011-02-16 06:06:34 +00001835 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001836 &root->root_key, &root->root_item);
1837
1838 btrfs_commit_transaction(trans, root);
1839out_reset:
1840 if (ret)
1841 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001842out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001843 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001844out_drop_write:
1845 mnt_drop_write_file(file);
1846out:
Li Zefan0caa1022010-12-20 16:30:25 +08001847 return ret;
1848}
1849
Yan, Zheng76dda932009-09-21 16:00:26 -04001850/*
1851 * helper to check if the subvolume references other subvolumes
1852 */
1853static noinline int may_destroy_subvol(struct btrfs_root *root)
1854{
1855 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001856 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001857 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001858 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001859 int ret;
1860
1861 path = btrfs_alloc_path();
1862 if (!path)
1863 return -ENOMEM;
1864
Josef Bacik175a2b82013-08-12 15:36:44 -04001865 /* Make sure this root isn't set as the default subvol */
1866 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1867 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1868 dir_id, "default", 7, 0);
1869 if (di && !IS_ERR(di)) {
1870 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1871 if (key.objectid == root->root_key.objectid) {
Guangyu Sun72de6b52014-03-11 11:24:18 -07001872 ret = -EPERM;
1873 btrfs_err(root->fs_info, "deleting default subvolume "
1874 "%llu is not allowed", key.objectid);
Josef Bacik175a2b82013-08-12 15:36:44 -04001875 goto out;
1876 }
1877 btrfs_release_path(path);
1878 }
1879
Yan, Zheng76dda932009-09-21 16:00:26 -04001880 key.objectid = root->root_key.objectid;
1881 key.type = BTRFS_ROOT_REF_KEY;
1882 key.offset = (u64)-1;
1883
1884 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1885 &key, path, 0, 0);
1886 if (ret < 0)
1887 goto out;
1888 BUG_ON(ret == 0);
1889
1890 ret = 0;
1891 if (path->slots[0] > 0) {
1892 path->slots[0]--;
1893 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1894 if (key.objectid == root->root_key.objectid &&
1895 key.type == BTRFS_ROOT_REF_KEY)
1896 ret = -ENOTEMPTY;
1897 }
1898out:
1899 btrfs_free_path(path);
1900 return ret;
1901}
1902
Chris Masonac8e9812010-02-28 15:39:26 -05001903static noinline int key_in_sk(struct btrfs_key *key,
1904 struct btrfs_ioctl_search_key *sk)
1905{
Chris Masonabc6e132010-03-18 12:10:08 -04001906 struct btrfs_key test;
1907 int ret;
1908
1909 test.objectid = sk->min_objectid;
1910 test.type = sk->min_type;
1911 test.offset = sk->min_offset;
1912
1913 ret = btrfs_comp_cpu_keys(key, &test);
1914 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001915 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001916
1917 test.objectid = sk->max_objectid;
1918 test.type = sk->max_type;
1919 test.offset = sk->max_offset;
1920
1921 ret = btrfs_comp_cpu_keys(key, &test);
1922 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001923 return 0;
1924 return 1;
1925}
1926
1927static noinline int copy_to_sk(struct btrfs_root *root,
1928 struct btrfs_path *path,
1929 struct btrfs_key *key,
1930 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001931 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01001932 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05001933 unsigned long *sk_offset,
1934 int *num_found)
1935{
1936 u64 found_transid;
1937 struct extent_buffer *leaf;
1938 struct btrfs_ioctl_search_header sh;
1939 unsigned long item_off;
1940 unsigned long item_len;
1941 int nritems;
1942 int i;
1943 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001944 int ret = 0;
1945
1946 leaf = path->nodes[0];
1947 slot = path->slots[0];
1948 nritems = btrfs_header_nritems(leaf);
1949
1950 if (btrfs_header_generation(leaf) > sk->max_transid) {
1951 i = nritems;
1952 goto advance_key;
1953 }
1954 found_transid = btrfs_header_generation(leaf);
1955
1956 for (i = slot; i < nritems; i++) {
1957 item_off = btrfs_item_ptr_offset(leaf, i);
1958 item_len = btrfs_item_size_nr(leaf, i);
1959
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001960 btrfs_item_key_to_cpu(leaf, key, i);
1961 if (!key_in_sk(key, sk))
1962 continue;
1963
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001964 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001965 if (*num_found) {
1966 ret = 1;
1967 goto out;
1968 }
Chris Masonac8e9812010-02-28 15:39:26 -05001969
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001970 /*
1971 * return one empty item back for v1, which does not
1972 * handle -EOVERFLOW
1973 */
1974
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001975 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001976 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01001977 ret = -EOVERFLOW;
1978 }
Chris Masonac8e9812010-02-28 15:39:26 -05001979
Gerhard Heift9b6e8172014-01-30 16:24:00 +01001980 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05001981 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01001982 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001983 }
1984
Chris Masonac8e9812010-02-28 15:39:26 -05001985 sh.objectid = key->objectid;
1986 sh.offset = key->offset;
1987 sh.type = key->type;
1988 sh.len = item_len;
1989 sh.transid = found_transid;
1990
1991 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01001992 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
1993 ret = -EFAULT;
1994 goto out;
1995 }
1996
Chris Masonac8e9812010-02-28 15:39:26 -05001997 *sk_offset += sizeof(sh);
1998
1999 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002000 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002001 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002002 if (read_extent_buffer_to_user(leaf, up,
2003 item_off, item_len)) {
2004 ret = -EFAULT;
2005 goto out;
2006 }
2007
Chris Masonac8e9812010-02-28 15:39:26 -05002008 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002009 }
Hugo Millse2156862011-05-14 17:43:41 +00002010 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002011
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002012 if (ret) /* -EOVERFLOW from above */
2013 goto out;
2014
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002015 if (*num_found >= sk->nr_items) {
2016 ret = 1;
2017 goto out;
2018 }
Chris Masonac8e9812010-02-28 15:39:26 -05002019 }
2020advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002021 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002022 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
2023 key->offset++;
2024 else if (key->type < (u8)-1 && key->type < sk->max_type) {
2025 key->offset = 0;
2026 key->type++;
2027 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
2028 key->offset = 0;
2029 key->type = 0;
2030 key->objectid++;
2031 } else
2032 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002033out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002034 /*
2035 * 0: all items from this leaf copied, continue with next
2036 * 1: * more items can be copied, but unused buffer is too small
2037 * * all items were found
2038 * Either way, it will stops the loop which iterates to the next
2039 * leaf
2040 * -EOVERFLOW: item was to large for buffer
2041 * -EFAULT: could not copy extent buffer back to userspace
2042 */
Chris Masonac8e9812010-02-28 15:39:26 -05002043 return ret;
2044}
2045
2046static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002047 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002048 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002049 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002050{
2051 struct btrfs_root *root;
2052 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002053 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002054 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2055 int ret;
2056 int num_found = 0;
2057 unsigned long sk_offset = 0;
2058
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002059 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2060 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002061 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002062 }
Gerhard Heift12544442014-01-30 16:23:58 +01002063
Chris Masonac8e9812010-02-28 15:39:26 -05002064 path = btrfs_alloc_path();
2065 if (!path)
2066 return -ENOMEM;
2067
2068 if (sk->tree_id == 0) {
2069 /* search the root of the inode that was passed */
2070 root = BTRFS_I(inode)->root;
2071 } else {
2072 key.objectid = sk->tree_id;
2073 key.type = BTRFS_ROOT_ITEM_KEY;
2074 key.offset = (u64)-1;
2075 root = btrfs_read_fs_root_no_name(info, &key);
2076 if (IS_ERR(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002077 printk(KERN_ERR "BTRFS: could not find root %llu\n",
Chris Masonac8e9812010-02-28 15:39:26 -05002078 sk->tree_id);
2079 btrfs_free_path(path);
2080 return -ENOENT;
2081 }
2082 }
2083
2084 key.objectid = sk->min_objectid;
2085 key.type = sk->min_type;
2086 key.offset = sk->min_offset;
2087
Chris Masonac8e9812010-02-28 15:39:26 -05002088 path->keep_locks = 1;
2089
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302090 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002091 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002092 if (ret != 0) {
2093 if (ret > 0)
2094 ret = 0;
2095 goto err;
2096 }
Gerhard Heiftba346b32014-01-30 16:24:02 +01002097 ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002098 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002099 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002100 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002101 break;
2102
2103 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002104 if (ret > 0)
2105 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002106err:
2107 sk->nr_items = num_found;
2108 btrfs_free_path(path);
2109 return ret;
2110}
2111
2112static noinline int btrfs_ioctl_tree_search(struct file *file,
2113 void __user *argp)
2114{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002115 struct btrfs_ioctl_search_args __user *uargs;
2116 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002117 struct inode *inode;
2118 int ret;
2119 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002120
2121 if (!capable(CAP_SYS_ADMIN))
2122 return -EPERM;
2123
Gerhard Heiftba346b32014-01-30 16:24:02 +01002124 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002125
Gerhard Heiftba346b32014-01-30 16:24:02 +01002126 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2127 return -EFAULT;
2128
2129 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002130
Al Viro496ad9a2013-01-23 17:07:38 -05002131 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002132 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002133
2134 /*
2135 * In the origin implementation an overflow is handled by returning a
2136 * search header with a len of zero, so reset ret.
2137 */
2138 if (ret == -EOVERFLOW)
2139 ret = 0;
2140
Gerhard Heiftba346b32014-01-30 16:24:02 +01002141 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002142 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002143 return ret;
2144}
2145
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002146static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2147 void __user *argp)
2148{
2149 struct btrfs_ioctl_search_args_v2 __user *uarg;
2150 struct btrfs_ioctl_search_args_v2 args;
2151 struct inode *inode;
2152 int ret;
2153 size_t buf_size;
2154 const size_t buf_limit = 16 * 1024 * 1024;
2155
2156 if (!capable(CAP_SYS_ADMIN))
2157 return -EPERM;
2158
2159 /* copy search header and buffer size */
2160 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2161 if (copy_from_user(&args, uarg, sizeof(args)))
2162 return -EFAULT;
2163
2164 buf_size = args.buf_size;
2165
2166 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2167 return -EOVERFLOW;
2168
2169 /* limit result size to 16MB */
2170 if (buf_size > buf_limit)
2171 buf_size = buf_limit;
2172
2173 inode = file_inode(file);
2174 ret = search_ioctl(inode, &args.key, &buf_size,
2175 (char *)(&uarg->buf[0]));
2176 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2177 ret = -EFAULT;
2178 else if (ret == -EOVERFLOW &&
2179 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2180 ret = -EFAULT;
2181
Yan, Zheng76dda932009-09-21 16:00:26 -04002182 return ret;
2183}
2184
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002185/*
Chris Masonac8e9812010-02-28 15:39:26 -05002186 * Search INODE_REFs to identify path name of 'dirid' directory
2187 * in a 'tree_id' tree. and sets path name to 'name'.
2188 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002189static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2190 u64 tree_id, u64 dirid, char *name)
2191{
2192 struct btrfs_root *root;
2193 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002194 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002195 int ret = -1;
2196 int slot;
2197 int len;
2198 int total_len = 0;
2199 struct btrfs_inode_ref *iref;
2200 struct extent_buffer *l;
2201 struct btrfs_path *path;
2202
2203 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2204 name[0]='\0';
2205 return 0;
2206 }
2207
2208 path = btrfs_alloc_path();
2209 if (!path)
2210 return -ENOMEM;
2211
Chris Masonac8e9812010-02-28 15:39:26 -05002212 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002213
2214 key.objectid = tree_id;
2215 key.type = BTRFS_ROOT_ITEM_KEY;
2216 key.offset = (u64)-1;
2217 root = btrfs_read_fs_root_no_name(info, &key);
2218 if (IS_ERR(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002219 printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002220 ret = -ENOENT;
2221 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002222 }
2223
2224 key.objectid = dirid;
2225 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002226 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002227
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302228 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002229 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2230 if (ret < 0)
2231 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002232 else if (ret > 0) {
2233 ret = btrfs_previous_item(root, path, dirid,
2234 BTRFS_INODE_REF_KEY);
2235 if (ret < 0)
2236 goto out;
2237 else if (ret > 0) {
2238 ret = -ENOENT;
2239 goto out;
2240 }
2241 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002242
2243 l = path->nodes[0];
2244 slot = path->slots[0];
2245 btrfs_item_key_to_cpu(l, &key, slot);
2246
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002247 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2248 len = btrfs_inode_ref_name_len(l, iref);
2249 ptr -= len + 1;
2250 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002251 if (ptr < name) {
2252 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002253 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002254 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002255
2256 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302257 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002258
2259 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2260 break;
2261
David Sterbab3b4aa72011-04-21 01:20:15 +02002262 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002263 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002264 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002265 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002266 }
Li Zefan77906a502011-07-14 03:16:00 +00002267 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302268 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002269 ret = 0;
2270out:
2271 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002272 return ret;
2273}
2274
2275static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2276 void __user *argp)
2277{
2278 struct btrfs_ioctl_ino_lookup_args *args;
2279 struct inode *inode;
2280 int ret;
2281
2282 if (!capable(CAP_SYS_ADMIN))
2283 return -EPERM;
2284
Julia Lawall2354d082010-10-29 15:14:18 -04002285 args = memdup_user(argp, sizeof(*args));
2286 if (IS_ERR(args))
2287 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002288
Al Viro496ad9a2013-01-23 17:07:38 -05002289 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002290
Chris Mason1b53ac42010-03-18 12:17:05 -04002291 if (args->treeid == 0)
2292 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2293
Chris Masonac8e9812010-02-28 15:39:26 -05002294 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2295 args->treeid, args->objectid,
2296 args->name);
2297
2298 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2299 ret = -EFAULT;
2300
2301 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002302 return ret;
2303}
2304
Yan, Zheng76dda932009-09-21 16:00:26 -04002305static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2306 void __user *arg)
2307{
Al Viro54563d42013-09-01 15:57:51 -04002308 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002309 struct dentry *dentry;
2310 struct inode *dir = parent->d_inode;
2311 struct inode *inode;
2312 struct btrfs_root *root = BTRFS_I(dir)->root;
2313 struct btrfs_root *dest = NULL;
2314 struct btrfs_ioctl_vol_args *vol_args;
2315 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002316 struct btrfs_block_rsv block_rsv;
David Sterba521e0542014-04-15 16:41:44 +02002317 u64 root_flags;
Miao Xiec58aaad2013-02-28 10:05:36 +00002318 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002319 int namelen;
2320 int ret;
2321 int err = 0;
2322
Yan, Zheng76dda932009-09-21 16:00:26 -04002323 vol_args = memdup_user(arg, sizeof(*vol_args));
2324 if (IS_ERR(vol_args))
2325 return PTR_ERR(vol_args);
2326
2327 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2328 namelen = strlen(vol_args->name);
2329 if (strchr(vol_args->name, '/') ||
2330 strncmp(vol_args->name, "..", namelen) == 0) {
2331 err = -EINVAL;
2332 goto out;
2333 }
2334
Al Viroa561be72011-11-23 11:57:51 -05002335 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002336 if (err)
2337 goto out;
2338
David Sterba521e0542014-04-15 16:41:44 +02002339
David Sterba5c50c9b2013-03-22 18:12:51 +00002340 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2341 if (err == -EINTR)
David Sterbae43f9982013-12-06 17:51:32 +01002342 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002343 dentry = lookup_one_len(vol_args->name, parent, namelen);
2344 if (IS_ERR(dentry)) {
2345 err = PTR_ERR(dentry);
2346 goto out_unlock_dir;
2347 }
2348
2349 if (!dentry->d_inode) {
2350 err = -ENOENT;
2351 goto out_dput;
2352 }
2353
2354 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002355 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302356 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002357 /*
2358 * Regular user. Only allow this with a special mount
2359 * option, when the user has write+exec access to the
2360 * subvol root, and when rmdir(2) would have been
2361 * allowed.
2362 *
2363 * Note that this is _not_ check that the subvol is
2364 * empty or doesn't contain data that we wouldn't
2365 * otherwise be able to delete.
2366 *
2367 * Users who want to delete empty subvols should try
2368 * rmdir(2).
2369 */
2370 err = -EPERM;
2371 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2372 goto out_dput;
2373
2374 /*
2375 * Do not allow deletion if the parent dir is the same
2376 * as the dir to be deleted. That means the ioctl
2377 * must be called on the dentry referencing the root
2378 * of the subvol, not a random directory contained
2379 * within it.
2380 */
2381 err = -EINVAL;
2382 if (root == dest)
2383 goto out_dput;
2384
2385 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2386 if (err)
2387 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002388 }
2389
Miao Xie5c39da52012-10-22 11:39:53 +00002390 /* check if subvolume may be deleted by a user */
2391 err = btrfs_may_delete(dir, dentry, 1);
2392 if (err)
2393 goto out_dput;
2394
Li Zefan33345d012011-04-20 10:31:50 +08002395 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002396 err = -EINVAL;
2397 goto out_dput;
2398 }
2399
Yan, Zheng76dda932009-09-21 16:00:26 -04002400 mutex_lock(&inode->i_mutex);
David Sterba521e0542014-04-15 16:41:44 +02002401
2402 /*
2403 * Don't allow to delete a subvolume with send in progress. This is
2404 * inside the i_mutex so the error handling that has to drop the bit
2405 * again is not run concurrently.
2406 */
2407 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002408 root_flags = btrfs_root_flags(&dest->root_item);
2409 if (dest->send_in_progress == 0) {
2410 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002411 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2412 spin_unlock(&dest->root_item_lock);
2413 } else {
2414 spin_unlock(&dest->root_item_lock);
2415 btrfs_warn(root->fs_info,
2416 "Attempt to delete subvolume %llu during send",
Filipe Mananac55bfa62014-05-25 03:55:44 +01002417 dest->root_key.objectid);
David Sterba521e0542014-04-15 16:41:44 +02002418 err = -EPERM;
2419 goto out_dput;
2420 }
2421
Yan, Zheng76dda932009-09-21 16:00:26 -04002422 err = d_invalidate(dentry);
2423 if (err)
2424 goto out_unlock;
2425
2426 down_write(&root->fs_info->subvol_sem);
2427
2428 err = may_destroy_subvol(dest);
2429 if (err)
2430 goto out_up_write;
2431
Miao Xiec58aaad2013-02-28 10:05:36 +00002432 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2433 /*
2434 * One for dir inode, two for dir entries, two for root
2435 * ref/backref.
2436 */
2437 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002438 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002439 if (err)
2440 goto out_up_write;
2441
Yan, Zhenga22285a2010-05-16 10:48:46 -04002442 trans = btrfs_start_transaction(root, 0);
2443 if (IS_ERR(trans)) {
2444 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002445 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002446 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002447 trans->block_rsv = &block_rsv;
2448 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002449
Yan, Zheng76dda932009-09-21 16:00:26 -04002450 ret = btrfs_unlink_subvol(trans, root, dir,
2451 dest->root_key.objectid,
2452 dentry->d_name.name,
2453 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002454 if (ret) {
2455 err = ret;
2456 btrfs_abort_transaction(trans, root, ret);
2457 goto out_end_trans;
2458 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002459
2460 btrfs_record_root_in_trans(trans, dest);
2461
2462 memset(&dest->root_item.drop_progress, 0,
2463 sizeof(dest->root_item.drop_progress));
2464 dest->root_item.drop_level = 0;
2465 btrfs_set_root_refs(&dest->root_item, 0);
2466
Miao Xie27cdeb72014-04-02 19:51:05 +08002467 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002468 ret = btrfs_insert_orphan_item(trans,
2469 root->fs_info->tree_root,
2470 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002471 if (ret) {
2472 btrfs_abort_transaction(trans, root, ret);
2473 err = ret;
2474 goto out_end_trans;
2475 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002476 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002477
2478 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2479 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2480 dest->root_key.objectid);
2481 if (ret && ret != -ENOENT) {
2482 btrfs_abort_transaction(trans, root, ret);
2483 err = ret;
2484 goto out_end_trans;
2485 }
2486 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2487 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2488 dest->root_item.received_uuid,
2489 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2490 dest->root_key.objectid);
2491 if (ret && ret != -ENOENT) {
2492 btrfs_abort_transaction(trans, root, ret);
2493 err = ret;
2494 goto out_end_trans;
2495 }
2496 }
2497
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002498out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002499 trans->block_rsv = NULL;
2500 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002501 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002502 if (ret && !err)
2503 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002504 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002505out_release:
2506 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002507out_up_write:
2508 up_write(&root->fs_info->subvol_sem);
2509out_unlock:
David Sterba521e0542014-04-15 16:41:44 +02002510 if (err) {
2511 spin_lock(&dest->root_item_lock);
Filipe Mananac55bfa62014-05-25 03:55:44 +01002512 root_flags = btrfs_root_flags(&dest->root_item);
2513 btrfs_set_root_flags(&dest->root_item,
David Sterba521e0542014-04-15 16:41:44 +02002514 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2515 spin_unlock(&dest->root_item_lock);
2516 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002517 mutex_unlock(&inode->i_mutex);
2518 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002519 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002520 btrfs_invalidate_inodes(dest);
2521 d_delete(dentry);
David Sterba61155aa2014-04-15 16:42:03 +02002522 ASSERT(dest->send_in_progress == 0);
Liu Bofa6ac872013-02-20 14:10:23 +00002523
2524 /* the last ref */
2525 if (dest->cache_inode) {
2526 iput(dest->cache_inode);
2527 dest->cache_inode = NULL;
2528 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002529 }
2530out_dput:
2531 dput(dentry);
2532out_unlock_dir:
2533 mutex_unlock(&dir->i_mutex);
David Sterbae43f9982013-12-06 17:51:32 +01002534out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002535 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002536out:
2537 kfree(vol_args);
2538 return err;
2539}
2540
Chris Mason1e701a32010-03-11 09:42:04 -05002541static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002542{
Al Viro496ad9a2013-01-23 17:07:38 -05002543 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002544 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002545 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002546 int ret;
2547
Ilya Dryomov25122d12013-01-20 15:57:57 +02002548 ret = mnt_want_write_file(file);
2549 if (ret)
2550 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002551
Ilya Dryomov25122d12013-01-20 15:57:57 +02002552 if (btrfs_root_readonly(root)) {
2553 ret = -EROFS;
2554 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002555 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002556
2557 switch (inode->i_mode & S_IFMT) {
2558 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002559 if (!capable(CAP_SYS_ADMIN)) {
2560 ret = -EPERM;
2561 goto out;
2562 }
Eric Sandeende78b512013-01-31 18:21:12 +00002563 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002564 if (ret)
2565 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002566 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002567 break;
2568 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002569 if (!(file->f_mode & FMODE_WRITE)) {
2570 ret = -EINVAL;
2571 goto out;
2572 }
Chris Mason1e701a32010-03-11 09:42:04 -05002573
2574 range = kzalloc(sizeof(*range), GFP_KERNEL);
2575 if (!range) {
2576 ret = -ENOMEM;
2577 goto out;
2578 }
2579
2580 if (argp) {
2581 if (copy_from_user(range, argp,
2582 sizeof(*range))) {
2583 ret = -EFAULT;
2584 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002585 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002586 }
2587 /* compression requires us to start the IO */
2588 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2589 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2590 range->extent_thresh = (u32)-1;
2591 }
2592 } else {
2593 /* the rest are all set to zero by kzalloc */
2594 range->len = (u64)-1;
2595 }
Al Viro496ad9a2013-01-23 17:07:38 -05002596 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002597 range, 0, 0);
2598 if (ret > 0)
2599 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002600 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002601 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002602 default:
2603 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002604 }
Chris Masone441d542009-01-05 16:57:23 -05002605out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002606 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002607 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002608}
2609
Christoph Hellwigb2950862008-12-02 09:54:17 -05002610static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002611{
2612 struct btrfs_ioctl_vol_args *vol_args;
2613 int ret;
2614
Chris Masone441d542009-01-05 16:57:23 -05002615 if (!capable(CAP_SYS_ADMIN))
2616 return -EPERM;
2617
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002618 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2619 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002620 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002621 }
2622
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002623 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002624 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002625 if (IS_ERR(vol_args)) {
2626 ret = PTR_ERR(vol_args);
2627 goto out;
2628 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002629
Mark Fasheh5516e592008-07-24 12:20:14 -04002630 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002631 ret = btrfs_init_new_device(root, vol_args->name);
2632
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002633 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002634out:
2635 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002636 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002637 return ret;
2638}
2639
Miao Xieda249272012-11-26 08:44:50 +00002640static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002641{
Al Viro496ad9a2013-01-23 17:07:38 -05002642 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002643 struct btrfs_ioctl_vol_args *vol_args;
2644 int ret;
2645
Chris Masone441d542009-01-05 16:57:23 -05002646 if (!capable(CAP_SYS_ADMIN))
2647 return -EPERM;
2648
Miao Xieda249272012-11-26 08:44:50 +00002649 ret = mnt_want_write_file(file);
2650 if (ret)
2651 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002652
Li Zefandae7b662009-04-08 15:06:54 +08002653 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002654 if (IS_ERR(vol_args)) {
2655 ret = PTR_ERR(vol_args);
2656 goto out;
2657 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002658
Mark Fasheh5516e592008-07-24 12:20:14 -04002659 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002660
Anand Jain183860f2013-05-17 10:52:45 +00002661 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2662 1)) {
2663 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2664 goto out;
2665 }
2666
2667 mutex_lock(&root->fs_info->volume_mutex);
2668 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002669 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002670 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002671
2672out:
2673 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002674 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002675 return ret;
2676}
2677
Jan Schmidt475f6382011-03-11 15:41:01 +01002678static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2679{
Li Zefan027ed2f2011-06-08 08:27:56 +00002680 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002681 struct btrfs_device *device;
2682 struct btrfs_device *next;
2683 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002684 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002685
Li Zefan027ed2f2011-06-08 08:27:56 +00002686 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2687 if (!fi_args)
2688 return -ENOMEM;
2689
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002690 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002691 fi_args->num_devices = fs_devices->num_devices;
2692 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002693
Jan Schmidt475f6382011-03-11 15:41:01 +01002694 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002695 if (device->devid > fi_args->max_id)
2696 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002697 }
2698 mutex_unlock(&fs_devices->device_list_mutex);
2699
David Sterba80a773f2014-05-07 18:17:06 +02002700 fi_args->nodesize = root->fs_info->super_copy->nodesize;
2701 fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
2702 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
2703
Li Zefan027ed2f2011-06-08 08:27:56 +00002704 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2705 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002706
Li Zefan027ed2f2011-06-08 08:27:56 +00002707 kfree(fi_args);
2708 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002709}
2710
2711static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2712{
2713 struct btrfs_ioctl_dev_info_args *di_args;
2714 struct btrfs_device *dev;
2715 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2716 int ret = 0;
2717 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002718
Jan Schmidt475f6382011-03-11 15:41:01 +01002719 di_args = memdup_user(arg, sizeof(*di_args));
2720 if (IS_ERR(di_args))
2721 return PTR_ERR(di_args);
2722
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002723 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002724 s_uuid = di_args->uuid;
2725
2726 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002727 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002728
2729 if (!dev) {
2730 ret = -ENODEV;
2731 goto out;
2732 }
2733
2734 di_args->devid = dev->devid;
2735 di_args->bytes_used = dev->bytes_used;
2736 di_args->total_bytes = dev->total_bytes;
2737 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002738 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002739 struct rcu_string *name;
2740
2741 rcu_read_lock();
2742 name = rcu_dereference(dev->name);
2743 strncpy(di_args->path, name->str, sizeof(di_args->path));
2744 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002745 di_args->path[sizeof(di_args->path) - 1] = 0;
2746 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002747 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002748 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002749
2750out:
David Sterba55793c02013-04-26 15:20:23 +00002751 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002752 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2753 ret = -EFAULT;
2754
2755 kfree(di_args);
2756 return ret;
2757}
2758
Mark Fasheh416161d2013-08-06 11:42:51 -07002759static struct page *extent_same_get_page(struct inode *inode, u64 off)
2760{
2761 struct page *page;
2762 pgoff_t index;
2763 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2764
2765 index = off >> PAGE_CACHE_SHIFT;
2766
2767 page = grab_cache_page(inode->i_mapping, index);
2768 if (!page)
2769 return NULL;
2770
2771 if (!PageUptodate(page)) {
2772 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2773 0))
2774 return NULL;
2775 lock_page(page);
2776 if (!PageUptodate(page)) {
2777 unlock_page(page);
2778 page_cache_release(page);
2779 return NULL;
2780 }
2781 }
2782 unlock_page(page);
2783
2784 return page;
2785}
2786
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002787static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2788{
2789 /* do any pending delalloc/csum calc on src, one way or
2790 another, and lock file content */
2791 while (1) {
2792 struct btrfs_ordered_extent *ordered;
2793 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2794 ordered = btrfs_lookup_first_ordered_extent(inode,
2795 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002796 if ((!ordered ||
2797 ordered->file_offset + ordered->len <= off ||
2798 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002799 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002800 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2801 if (ordered)
2802 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002803 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002804 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002805 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2806 if (ordered)
2807 btrfs_put_ordered_extent(ordered);
2808 btrfs_wait_ordered_range(inode, off, len);
2809 }
2810}
2811
Mark Fasheh416161d2013-08-06 11:42:51 -07002812static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2813 struct inode *inode2, u64 loff2, u64 len)
2814{
2815 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2816 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2817
2818 mutex_unlock(&inode1->i_mutex);
2819 mutex_unlock(&inode2->i_mutex);
2820}
2821
2822static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2823 struct inode *inode2, u64 loff2, u64 len)
2824{
2825 if (inode1 < inode2) {
2826 swap(inode1, inode2);
2827 swap(loff1, loff2);
2828 }
2829
2830 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2831 lock_extent_range(inode1, loff1, len);
2832 if (inode1 != inode2) {
2833 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2834 lock_extent_range(inode2, loff2, len);
2835 }
2836}
2837
2838static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2839 u64 dst_loff, u64 len)
2840{
2841 int ret = 0;
2842 struct page *src_page, *dst_page;
2843 unsigned int cmp_len = PAGE_CACHE_SIZE;
2844 void *addr, *dst_addr;
2845
2846 while (len) {
2847 if (len < PAGE_CACHE_SIZE)
2848 cmp_len = len;
2849
2850 src_page = extent_same_get_page(src, loff);
2851 if (!src_page)
2852 return -EINVAL;
2853 dst_page = extent_same_get_page(dst, dst_loff);
2854 if (!dst_page) {
2855 page_cache_release(src_page);
2856 return -EINVAL;
2857 }
2858 addr = kmap_atomic(src_page);
2859 dst_addr = kmap_atomic(dst_page);
2860
2861 flush_dcache_page(src_page);
2862 flush_dcache_page(dst_page);
2863
2864 if (memcmp(addr, dst_addr, cmp_len))
2865 ret = BTRFS_SAME_DATA_DIFFERS;
2866
2867 kunmap_atomic(addr);
2868 kunmap_atomic(dst_addr);
2869 page_cache_release(src_page);
2870 page_cache_release(dst_page);
2871
2872 if (ret)
2873 break;
2874
2875 loff += cmp_len;
2876 dst_loff += cmp_len;
2877 len -= cmp_len;
2878 }
2879
2880 return ret;
2881}
2882
2883static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2884{
2885 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2886
2887 if (off + len > inode->i_size || off + len < off)
2888 return -EINVAL;
2889 /* Check that we are block aligned - btrfs_clone() requires this */
2890 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2891 return -EINVAL;
2892
2893 return 0;
2894}
2895
2896static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2897 struct inode *dst, u64 dst_loff)
2898{
2899 int ret;
2900
2901 /*
2902 * btrfs_clone() can't handle extents in the same file
2903 * yet. Once that works, we can drop this check and replace it
2904 * with a check for the same inode, but overlapping extents.
2905 */
2906 if (src == dst)
2907 return -EINVAL;
2908
2909 btrfs_double_lock(src, loff, dst, dst_loff, len);
2910
2911 ret = extent_same_check_offsets(src, loff, len);
2912 if (ret)
2913 goto out_unlock;
2914
2915 ret = extent_same_check_offsets(dst, dst_loff, len);
2916 if (ret)
2917 goto out_unlock;
2918
2919 /* don't make the dst file partly checksummed */
2920 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2921 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2922 ret = -EINVAL;
2923 goto out_unlock;
2924 }
2925
2926 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2927 if (ret == 0)
2928 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2929
2930out_unlock:
2931 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2932
2933 return ret;
2934}
2935
2936#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2937
2938static long btrfs_ioctl_file_extent_same(struct file *file,
Al Viro1c1c8742013-12-11 23:07:51 -05002939 struct btrfs_ioctl_same_args __user *argp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002940{
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002941 struct btrfs_ioctl_same_args *same;
2942 struct btrfs_ioctl_same_extent_info *info;
Al Viro1c1c8742013-12-11 23:07:51 -05002943 struct inode *src = file_inode(file);
Mark Fasheh416161d2013-08-06 11:42:51 -07002944 u64 off;
2945 u64 len;
2946 int i;
2947 int ret;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002948 unsigned long size;
Mark Fasheh416161d2013-08-06 11:42:51 -07002949 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2950 bool is_admin = capable(CAP_SYS_ADMIN);
Al Viro1c1c8742013-12-11 23:07:51 -05002951 u16 count;
Mark Fasheh416161d2013-08-06 11:42:51 -07002952
2953 if (!(file->f_mode & FMODE_READ))
2954 return -EINVAL;
2955
2956 ret = mnt_want_write_file(file);
2957 if (ret)
2958 return ret;
2959
Al Viro1c1c8742013-12-11 23:07:51 -05002960 if (get_user(count, &argp->dest_count)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07002961 ret = -EFAULT;
2962 goto out;
2963 }
2964
Al Viro1c1c8742013-12-11 23:07:51 -05002965 size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002966
Al Viro1c1c8742013-12-11 23:07:51 -05002967 same = memdup_user(argp, size);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002968
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002969 if (IS_ERR(same)) {
2970 ret = PTR_ERR(same);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002971 goto out;
2972 }
2973
2974 off = same->logical_offset;
2975 len = same->length;
Mark Fasheh416161d2013-08-06 11:42:51 -07002976
2977 /*
2978 * Limit the total length we will dedupe for each operation.
2979 * This is intended to bound the total time spent in this
2980 * ioctl to something sane.
2981 */
2982 if (len > BTRFS_MAX_DEDUPE_LEN)
2983 len = BTRFS_MAX_DEDUPE_LEN;
2984
2985 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2986 /*
2987 * Btrfs does not support blocksize < page_size. As a
2988 * result, btrfs_cmp_data() won't correctly handle
2989 * this situation without an update.
2990 */
2991 ret = -EINVAL;
2992 goto out;
2993 }
2994
2995 ret = -EISDIR;
2996 if (S_ISDIR(src->i_mode))
2997 goto out;
2998
2999 ret = -EACCES;
3000 if (!S_ISREG(src->i_mode))
3001 goto out;
3002
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003003 /* pre-format output fields to sane values */
Al Viro1c1c8742013-12-11 23:07:51 -05003004 for (i = 0; i < count; i++) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003005 same->info[i].bytes_deduped = 0ULL;
3006 same->info[i].status = 0;
3007 }
3008
Al Viro1c1c8742013-12-11 23:07:51 -05003009 for (i = 0, info = same->info; i < count; i++, info++) {
3010 struct inode *dst;
3011 struct fd dst_file = fdget(info->fd);
3012 if (!dst_file.file) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003013 info->status = -EBADF;
Al Viro1c1c8742013-12-11 23:07:51 -05003014 continue;
Mark Fasheh416161d2013-08-06 11:42:51 -07003015 }
Al Viro1c1c8742013-12-11 23:07:51 -05003016 dst = file_inode(dst_file.file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003017
Al Viro1c1c8742013-12-11 23:07:51 -05003018 if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003019 info->status = -EINVAL;
Al Viro1c1c8742013-12-11 23:07:51 -05003020 } else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
3021 info->status = -EXDEV;
3022 } else if (S_ISDIR(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003023 info->status = -EISDIR;
Al Viro1c1c8742013-12-11 23:07:51 -05003024 } else if (!S_ISREG(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003025 info->status = -EACCES;
Al Viro1c1c8742013-12-11 23:07:51 -05003026 } else {
3027 info->status = btrfs_extent_same(src, off, len, dst,
3028 info->logical_offset);
3029 if (info->status == 0)
3030 info->bytes_deduped += len;
Mark Fasheh416161d2013-08-06 11:42:51 -07003031 }
Al Viro1c1c8742013-12-11 23:07:51 -05003032 fdput(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003033 }
3034
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07003035 ret = copy_to_user(argp, same, size);
3036 if (ret)
3037 ret = -EFAULT;
3038
Mark Fasheh416161d2013-08-06 11:42:51 -07003039out:
3040 mnt_drop_write_file(file);
3041 return ret;
3042}
3043
Josef Bacikfcebe452014-05-13 17:30:47 -07003044/* Helper to check and see if this root currently has a ref on the given disk
3045 * bytenr. If it does then we need to update the quota for this root. This
3046 * doesn't do anything if quotas aren't enabled.
3047 */
3048static int check_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3049 u64 disko)
3050{
3051 struct seq_list tree_mod_seq_elem = {};
3052 struct ulist *roots;
3053 struct ulist_iterator uiter;
3054 struct ulist_node *root_node = NULL;
3055 int ret;
3056
3057 if (!root->fs_info->quota_enabled)
3058 return 1;
3059
3060 btrfs_get_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
3061 ret = btrfs_find_all_roots(trans, root->fs_info, disko,
3062 tree_mod_seq_elem.seq, &roots);
3063 if (ret < 0)
3064 goto out;
3065 ret = 0;
3066 ULIST_ITER_INIT(&uiter);
3067 while ((root_node = ulist_next(roots, &uiter))) {
3068 if (root_node->val == root->objectid) {
3069 ret = 1;
3070 break;
3071 }
3072 }
3073 ulist_free(roots);
3074out:
3075 btrfs_put_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
3076 return ret;
3077}
3078
Filipe Mananaf82a9902014-06-01 01:50:28 +01003079static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3080 struct inode *inode,
3081 u64 endoff,
3082 const u64 destoff,
3083 const u64 olen)
3084{
3085 struct btrfs_root *root = BTRFS_I(inode)->root;
3086 int ret;
3087
3088 inode_inc_iversion(inode);
3089 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3090 /*
3091 * We round up to the block size at eof when determining which
3092 * extents to clone above, but shouldn't round up the file size.
3093 */
3094 if (endoff > destoff + olen)
3095 endoff = destoff + olen;
3096 if (endoff > inode->i_size)
3097 btrfs_i_size_write(inode, endoff);
3098
3099 ret = btrfs_update_inode(trans, root, inode);
3100 if (ret) {
3101 btrfs_abort_transaction(trans, root, ret);
3102 btrfs_end_transaction(trans, root);
3103 goto out;
3104 }
3105 ret = btrfs_end_transaction(trans, root);
3106out:
3107 return ret;
3108}
3109
Filipe Manana7ffbb592014-06-09 03:48:05 +01003110static void clone_update_extent_map(struct inode *inode,
3111 const struct btrfs_trans_handle *trans,
3112 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003113 const u64 hole_offset,
3114 const u64 hole_len)
3115{
3116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3117 struct extent_map *em;
3118 int ret;
3119
3120 em = alloc_extent_map();
3121 if (!em) {
3122 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3123 &BTRFS_I(inode)->runtime_flags);
3124 return;
3125 }
3126
Filipe Manana14f59792014-06-29 21:45:40 +01003127 if (path) {
3128 struct btrfs_file_extent_item *fi;
3129
3130 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3131 struct btrfs_file_extent_item);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003132 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3133 em->generation = -1;
3134 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3135 BTRFS_FILE_EXTENT_INLINE)
3136 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3137 &BTRFS_I(inode)->runtime_flags);
3138 } else {
3139 em->start = hole_offset;
3140 em->len = hole_len;
3141 em->ram_bytes = em->len;
3142 em->orig_start = hole_offset;
3143 em->block_start = EXTENT_MAP_HOLE;
3144 em->block_len = 0;
3145 em->orig_block_len = 0;
3146 em->compress_type = BTRFS_COMPRESS_NONE;
3147 em->generation = trans->transid;
3148 }
3149
3150 while (1) {
3151 write_lock(&em_tree->lock);
3152 ret = add_extent_mapping(em_tree, em, 1);
3153 write_unlock(&em_tree->lock);
3154 if (ret != -EEXIST) {
3155 free_extent_map(em);
3156 break;
3157 }
3158 btrfs_drop_extent_cache(inode, em->start,
3159 em->start + em->len - 1, 0);
3160 }
3161
3162 if (unlikely(ret))
3163 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3164 &BTRFS_I(inode)->runtime_flags);
3165}
3166
Mark Fasheh32b7c682013-08-06 11:42:49 -07003167/**
3168 * btrfs_clone() - clone a range from inode file to another
3169 *
3170 * @src: Inode to clone from
3171 * @inode: Inode to clone to
3172 * @off: Offset within source to start clone from
3173 * @olen: Original length, passed by user, of range to clone
3174 * @olen_aligned: Block-aligned value of olen, extent_same uses
3175 * identical values here
3176 * @destoff: Offset within @inode to start clone
3177 */
3178static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003179 const u64 off, const u64 olen, const u64 olen_aligned,
3180 const u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003181{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003182 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003183 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003184 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003185 struct btrfs_trans_handle *trans;
3186 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003187 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003188 u32 nritems;
3189 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003190 int ret;
Josef Bacikfcebe452014-05-13 17:30:47 -07003191 int no_quota;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003192 const u64 len = olen_aligned;
Josef Bacikfcebe452014-05-13 17:30:47 -07003193 u64 last_disko = 0;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003194 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003195
3196 ret = -ENOMEM;
3197 buf = vmalloc(btrfs_level_size(root, 0));
3198 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003199 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003200
3201 path = btrfs_alloc_path();
3202 if (!path) {
3203 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003204 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003205 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003206
Yan Zhengae01a0a2008-08-04 23:23:47 -04003207 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003208 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08003209 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04003210 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003211 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003212
3213 while (1) {
3214 /*
3215 * note the key will change type as we walk through the
3216 * tree.
3217 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003218 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003219 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3220 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003221 if (ret < 0)
3222 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003223 /*
3224 * First search, if no extent item that starts at offset off was
3225 * found but the previous item is an extent item, it's possible
3226 * it might overlap our target range, therefore process it.
3227 */
3228 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3229 btrfs_item_key_to_cpu(path->nodes[0], &key,
3230 path->slots[0] - 1);
3231 if (key.type == BTRFS_EXTENT_DATA_KEY)
3232 path->slots[0]--;
3233 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003234
Yan Zhengae01a0a2008-08-04 23:23:47 -04003235 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003236process_slot:
Josef Bacikfcebe452014-05-13 17:30:47 -07003237 no_quota = 1;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003238 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003239 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003240 if (ret < 0)
3241 goto out;
3242 if (ret > 0)
3243 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003244 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003245 }
3246 leaf = path->nodes[0];
3247 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003248
Yan Zhengae01a0a2008-08-04 23:23:47 -04003249 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05003250 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08003251 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003252 break;
3253
Sage Weilc5c9cd42008-11-12 14:32:25 -05003254 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3255 struct btrfs_file_extent_item *extent;
3256 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003257 u32 size;
3258 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003259 u64 disko = 0, diskl = 0;
3260 u64 datao = 0, datal = 0;
3261 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003262 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003263
Sage Weilc5c9cd42008-11-12 14:32:25 -05003264 extent = btrfs_item_ptr(leaf, slot,
3265 struct btrfs_file_extent_item);
3266 comp = btrfs_file_extent_compression(leaf, extent);
3267 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003268 if (type == BTRFS_FILE_EXTENT_REG ||
3269 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003270 disko = btrfs_file_extent_disk_bytenr(leaf,
3271 extent);
3272 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3273 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003274 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003275 datal = btrfs_file_extent_num_bytes(leaf,
3276 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003277 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3278 /* take upper bound, may be compressed */
3279 datal = btrfs_file_extent_ram_bytes(leaf,
3280 extent);
3281 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003282
Filipe Manana2c463822014-05-31 02:31:05 +01003283 /*
3284 * The first search might have left us at an extent
3285 * item that ends before our target range's start, can
3286 * happen if we have holes and NO_HOLES feature enabled.
3287 */
3288 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003289 path->slots[0]++;
3290 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003291 } else if (key.offset >= off + len) {
3292 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003293 }
3294
3295 size = btrfs_item_size_nr(leaf, slot);
3296 read_extent_buffer(leaf, buf,
3297 btrfs_item_ptr_offset(leaf, slot),
3298 size);
3299
3300 btrfs_release_path(path);
3301 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003302
Zheng Yan31840ae2008-09-23 13:14:14 -04003303 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08003304 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08003305 if (off <= key.offset)
3306 new_key.offset = key.offset + destoff - off;
3307 else
3308 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003309
Sage Weilb6f34092011-09-20 14:48:51 -04003310 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003311 * Deal with a hole that doesn't have an extent item
3312 * that represents it (NO_HOLES feature enabled).
3313 * This hole is either in the middle of the cloning
3314 * range or at the beginning (fully overlaps it or
3315 * partially overlaps it).
3316 */
3317 if (new_key.offset != last_dest_end)
3318 drop_start = last_dest_end;
3319 else
3320 drop_start = new_key.offset;
3321
3322 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003323 * 1 - adjusting old extent (we may have to split it)
3324 * 1 - add new extent
3325 * 1 - inode update
3326 */
3327 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003328 if (IS_ERR(trans)) {
3329 ret = PTR_ERR(trans);
3330 goto out;
3331 }
3332
Chris Masonc8a894d2009-06-27 21:07:03 -04003333 if (type == BTRFS_FILE_EXTENT_REG ||
3334 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003335 /*
3336 * a | --- range to clone ---| b
3337 * | ------------- extent ------------- |
3338 */
3339
Antonio Ospite93915582014-06-04 14:03:48 +02003340 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003341 if (key.offset + datal > off + len)
3342 datal = off + len - key.offset;
3343
Antonio Ospite93915582014-06-04 14:03:48 +02003344 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003345 if (off > key.offset) {
3346 datao += off - key.offset;
3347 datal -= off - key.offset;
3348 }
3349
Josef Bacik5dc562c2012-08-17 13:14:17 -04003350 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003351 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003352 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003353 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003354 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003355 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003356 btrfs_abort_transaction(trans,
3357 root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003358 btrfs_end_transaction(trans, root);
3359 goto out;
3360 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003361
Sage Weilc5c9cd42008-11-12 14:32:25 -05003362 ret = btrfs_insert_empty_item(trans, root, path,
3363 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003364 if (ret) {
3365 btrfs_abort_transaction(trans, root,
3366 ret);
3367 btrfs_end_transaction(trans, root);
3368 goto out;
3369 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003370
3371 leaf = path->nodes[0];
3372 slot = path->slots[0];
3373 write_extent_buffer(leaf, buf,
3374 btrfs_item_ptr_offset(leaf, slot),
3375 size);
3376
3377 extent = btrfs_item_ptr(leaf, slot,
3378 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003379
Sage Weilc5c9cd42008-11-12 14:32:25 -05003380 /* disko == 0 means it's a hole */
3381 if (!disko)
3382 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003383
3384 btrfs_set_file_extent_offset(leaf, extent,
3385 datao);
3386 btrfs_set_file_extent_num_bytes(leaf, extent,
3387 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003388
3389 /*
3390 * We need to look up the roots that point at
3391 * this bytenr and see if the new root does. If
3392 * it does not we need to make sure we update
3393 * quotas appropriately.
3394 */
3395 if (disko && root != BTRFS_I(src)->root &&
3396 disko != last_disko) {
3397 no_quota = check_ref(trans, root,
3398 disko);
3399 if (no_quota < 0) {
3400 btrfs_abort_transaction(trans,
3401 root,
3402 ret);
3403 btrfs_end_transaction(trans,
3404 root);
3405 ret = no_quota;
3406 goto out;
3407 }
3408 }
3409
Sage Weilc5c9cd42008-11-12 14:32:25 -05003410 if (disko) {
3411 inode_add_bytes(inode, datal);
3412 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003413 disko, diskl, 0,
3414 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003415 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003416 new_key.offset - datao,
Josef Bacikfcebe452014-05-13 17:30:47 -07003417 no_quota);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003418 if (ret) {
3419 btrfs_abort_transaction(trans,
3420 root,
3421 ret);
3422 btrfs_end_transaction(trans,
3423 root);
3424 goto out;
3425
3426 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003427 }
3428 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3429 u64 skip = 0;
3430 u64 trim = 0;
Liu Bod3ecfcd2014-05-09 10:01:02 +08003431 u64 aligned_end = 0;
3432
Sage Weilc5c9cd42008-11-12 14:32:25 -05003433 if (off > key.offset) {
3434 skip = off - key.offset;
3435 new_key.offset += skip;
3436 }
Chris Masond3977122009-01-05 21:25:51 -05003437
Liu Boaa42ffd2012-09-18 03:52:23 -06003438 if (key.offset + datal > off + len)
3439 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003440
Sage Weilc5c9cd42008-11-12 14:32:25 -05003441 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003442 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003443 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003444 goto out;
3445 }
3446 size -= skip + trim;
3447 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003448
Liu Bod3ecfcd2014-05-09 10:01:02 +08003449 aligned_end = ALIGN(new_key.offset + datal,
3450 root->sectorsize);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003451 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003452 drop_start,
Liu Bod3ecfcd2014-05-09 10:01:02 +08003453 aligned_end,
Josef Bacik26714852012-08-29 12:24:27 -04003454 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003455 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003456 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003457 btrfs_abort_transaction(trans,
3458 root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003459 btrfs_end_transaction(trans, root);
3460 goto out;
3461 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003462
Sage Weilc5c9cd42008-11-12 14:32:25 -05003463 ret = btrfs_insert_empty_item(trans, root, path,
3464 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003465 if (ret) {
3466 btrfs_abort_transaction(trans, root,
3467 ret);
3468 btrfs_end_transaction(trans, root);
3469 goto out;
3470 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003471
3472 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003473 u32 start =
3474 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003475 memmove(buf+start, buf+start+skip,
3476 datal);
3477 }
3478
3479 leaf = path->nodes[0];
3480 slot = path->slots[0];
3481 write_extent_buffer(leaf, buf,
3482 btrfs_item_ptr_offset(leaf, slot),
3483 size);
3484 inode_add_bytes(inode, datal);
3485 }
3486
Filipe Manana7ffbb592014-06-09 03:48:05 +01003487 /* If we have an implicit hole (NO_HOLES feature). */
3488 if (drop_start < new_key.offset)
3489 clone_update_extent_map(inode, trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003490 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003491 new_key.offset - drop_start);
3492
Filipe Manana14f59792014-06-29 21:45:40 +01003493 clone_update_extent_map(inode, trans, path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003494
Sage Weilc5c9cd42008-11-12 14:32:25 -05003495 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003496 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003497
Filipe Manana62e23902014-08-08 02:47:06 +01003498 last_dest_end = ALIGN(new_key.offset + datal,
3499 root->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003500 ret = clone_finish_inode_update(trans, inode,
3501 last_dest_end,
3502 destoff, olen);
3503 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003504 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003505 if (new_key.offset + datal >= destoff + len)
3506 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003507 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003508 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003509 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003510 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003511 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003512
Filipe Mananaf82a9902014-06-01 01:50:28 +01003513 if (last_dest_end < destoff + len) {
3514 /*
3515 * We have an implicit hole (NO_HOLES feature is enabled) that
3516 * fully or partially overlaps our cloning range at its end.
3517 */
3518 btrfs_release_path(path);
3519
3520 /*
3521 * 1 - remove extent(s)
3522 * 1 - inode update
3523 */
3524 trans = btrfs_start_transaction(root, 2);
3525 if (IS_ERR(trans)) {
3526 ret = PTR_ERR(trans);
3527 goto out;
3528 }
3529 ret = btrfs_drop_extents(trans, root, inode,
3530 last_dest_end, destoff + len, 1);
3531 if (ret) {
3532 if (ret != -EOPNOTSUPP)
3533 btrfs_abort_transaction(trans, root, ret);
3534 btrfs_end_transaction(trans, root);
3535 goto out;
3536 }
Filipe Manana14f59792014-06-29 21:45:40 +01003537 clone_update_extent_map(inode, trans, NULL, last_dest_end,
3538 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003539 ret = clone_finish_inode_update(trans, inode, destoff + len,
3540 destoff, olen);
3541 }
3542
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003543out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003544 btrfs_free_path(path);
3545 vfree(buf);
3546 return ret;
3547}
3548
3549static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3550 u64 off, u64 olen, u64 destoff)
3551{
Al Viro54563d42013-09-01 15:57:51 -04003552 struct inode *inode = file_inode(file);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003553 struct btrfs_root *root = BTRFS_I(inode)->root;
3554 struct fd src_file;
3555 struct inode *src;
3556 int ret;
3557 u64 len = olen;
3558 u64 bs = root->fs_info->sb->s_blocksize;
3559 int same_inode = 0;
3560
3561 /*
3562 * TODO:
3563 * - split compressed inline extents. annoying: we need to
3564 * decompress into destination's address_space (the file offset
3565 * may change, so source mapping won't do), then recompress (or
3566 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003567 *
3568 * - split destination inode's inline extents. The inline extents can
3569 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003570 */
3571
3572 /* the destination must be opened for writing */
3573 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3574 return -EINVAL;
3575
3576 if (btrfs_root_readonly(root))
3577 return -EROFS;
3578
3579 ret = mnt_want_write_file(file);
3580 if (ret)
3581 return ret;
3582
3583 src_file = fdget(srcfd);
3584 if (!src_file.file) {
3585 ret = -EBADF;
3586 goto out_drop_write;
3587 }
3588
3589 ret = -EXDEV;
3590 if (src_file.file->f_path.mnt != file->f_path.mnt)
3591 goto out_fput;
3592
3593 src = file_inode(src_file.file);
3594
3595 ret = -EINVAL;
3596 if (src == inode)
3597 same_inode = 1;
3598
3599 /* the src must be open for reading */
3600 if (!(src_file.file->f_mode & FMODE_READ))
3601 goto out_fput;
3602
3603 /* don't make the dst file partly checksummed */
3604 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3605 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3606 goto out_fput;
3607
3608 ret = -EISDIR;
3609 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3610 goto out_fput;
3611
3612 ret = -EXDEV;
3613 if (src->i_sb != inode->i_sb)
3614 goto out_fput;
3615
3616 if (!same_inode) {
3617 if (inode < src) {
3618 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3619 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3620 } else {
3621 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3622 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3623 }
3624 } else {
3625 mutex_lock(&src->i_mutex);
3626 }
3627
3628 /* determine range to clone */
3629 ret = -EINVAL;
3630 if (off + len > src->i_size || off + len < off)
3631 goto out_unlock;
3632 if (len == 0)
3633 olen = len = src->i_size - off;
3634 /* if we extend to eof, continue to block boundary */
3635 if (off + len == src->i_size)
3636 len = ALIGN(src->i_size, bs) - off;
3637
3638 /* verify the end result is block aligned */
3639 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3640 !IS_ALIGNED(destoff, bs))
3641 goto out_unlock;
3642
3643 /* verify if ranges are overlapped within the same file */
3644 if (same_inode) {
3645 if (destoff + len > off && destoff < off + len)
3646 goto out_unlock;
3647 }
3648
3649 if (destoff > inode->i_size) {
3650 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3651 if (ret)
3652 goto out_unlock;
3653 }
3654
Filipe Mananac125b8b2014-05-23 05:03:34 +01003655 /*
3656 * Lock the target range too. Right after we replace the file extent
3657 * items in the fs tree (which now point to the cloned data), we might
3658 * have a worker replace them with extent items relative to a write
3659 * operation that was issued before this clone operation (i.e. confront
3660 * with inode.c:btrfs_finish_ordered_io).
3661 */
3662 if (same_inode) {
3663 u64 lock_start = min_t(u64, off, destoff);
3664 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003665
Filipe Mananac125b8b2014-05-23 05:03:34 +01003666 lock_extent_range(src, lock_start, lock_len);
3667 } else {
3668 lock_extent_range(src, off, len);
3669 lock_extent_range(inode, destoff, len);
3670 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003671
3672 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3673
Filipe Mananac125b8b2014-05-23 05:03:34 +01003674 if (same_inode) {
3675 u64 lock_start = min_t(u64, off, destoff);
3676 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3677
3678 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3679 } else {
3680 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
3681 unlock_extent(&BTRFS_I(inode)->io_tree, destoff,
3682 destoff + len - 1);
3683 }
3684 /*
3685 * Truncate page cache pages so that future reads will see the cloned
3686 * data immediately and not the previous data.
3687 */
3688 truncate_inode_pages_range(&inode->i_data, destoff,
3689 PAGE_CACHE_ALIGN(destoff + len) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003690out_unlock:
Filipe David Borba Mananac57c2b32014-01-11 21:31:25 +00003691 if (!same_inode) {
3692 if (inode < src) {
3693 mutex_unlock(&src->i_mutex);
3694 mutex_unlock(&inode->i_mutex);
3695 } else {
3696 mutex_unlock(&inode->i_mutex);
3697 mutex_unlock(&src->i_mutex);
3698 }
3699 } else {
3700 mutex_unlock(&src->i_mutex);
3701 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003702out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003703 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003704out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003705 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003706 return ret;
3707}
3708
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003709static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003710{
3711 struct btrfs_ioctl_clone_range_args args;
3712
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003713 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003714 return -EFAULT;
3715 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3716 args.src_length, args.dest_offset);
3717}
3718
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003719/*
3720 * there are many ways the trans_start and trans_end ioctls can lead
3721 * to deadlocks. They should only be used by applications that
3722 * basically own the machine, and have a very in depth understanding
3723 * of all the possible deadlocks and enospc problems.
3724 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003725static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003726{
Al Viro496ad9a2013-01-23 17:07:38 -05003727 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003728 struct btrfs_root *root = BTRFS_I(inode)->root;
3729 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003730 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003731
Sage Weil1ab86ae2009-09-29 18:38:44 -04003732 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003733 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003734 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003735
3736 ret = -EINPROGRESS;
3737 if (file->private_data)
3738 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003739
Li Zefanb83cc962010-12-20 16:04:08 +08003740 ret = -EROFS;
3741 if (btrfs_root_readonly(root))
3742 goto out;
3743
Al Viroa561be72011-11-23 11:57:51 -05003744 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003745 if (ret)
3746 goto out;
3747
Josef Bacika4abeea2011-04-11 17:25:13 -04003748 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003749
Sage Weil1ab86ae2009-09-29 18:38:44 -04003750 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003751 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003752 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003753 goto out_drop;
3754
3755 file->private_data = trans;
3756 return 0;
3757
3758out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003759 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003760 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003761out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003762 return ret;
3763}
3764
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003765static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3766{
Al Viro496ad9a2013-01-23 17:07:38 -05003767 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003768 struct btrfs_root *root = BTRFS_I(inode)->root;
3769 struct btrfs_root *new_root;
3770 struct btrfs_dir_item *di;
3771 struct btrfs_trans_handle *trans;
3772 struct btrfs_path *path;
3773 struct btrfs_key location;
3774 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003775 u64 objectid = 0;
3776 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003777 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003778
3779 if (!capable(CAP_SYS_ADMIN))
3780 return -EPERM;
3781
Miao Xie3c04ce02012-11-26 08:43:07 +00003782 ret = mnt_want_write_file(file);
3783 if (ret)
3784 return ret;
3785
3786 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3787 ret = -EFAULT;
3788 goto out;
3789 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003790
3791 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303792 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003793
3794 location.objectid = objectid;
3795 location.type = BTRFS_ROOT_ITEM_KEY;
3796 location.offset = (u64)-1;
3797
3798 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003799 if (IS_ERR(new_root)) {
3800 ret = PTR_ERR(new_root);
3801 goto out;
3802 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003803
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003804 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003805 if (!path) {
3806 ret = -ENOMEM;
3807 goto out;
3808 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003809 path->leave_spinning = 1;
3810
3811 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003812 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003813 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003814 ret = PTR_ERR(trans);
3815 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003816 }
3817
David Sterba6c417612011-04-13 15:41:04 +02003818 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003819 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3820 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003821 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003822 btrfs_free_path(path);
3823 btrfs_end_transaction(trans, root);
Frank Holtonefe120a2013-12-20 11:37:06 -05003824 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3825 "item, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003826 ret = -ENOENT;
3827 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003828 }
3829
3830 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3831 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3832 btrfs_mark_buffer_dirty(path->nodes[0]);
3833 btrfs_free_path(path);
3834
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003835 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003836 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003837out:
3838 mnt_drop_write_file(file);
3839 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003840}
3841
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003842void btrfs_get_block_group_info(struct list_head *groups_list,
3843 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003844{
3845 struct btrfs_block_group_cache *block_group;
3846
3847 space->total_bytes = 0;
3848 space->used_bytes = 0;
3849 space->flags = 0;
3850 list_for_each_entry(block_group, groups_list, list) {
3851 space->flags = block_group->flags;
3852 space->total_bytes += block_group->key.offset;
3853 space->used_bytes +=
3854 btrfs_block_group_used(&block_group->item);
3855 }
3856}
3857
Eric Sandeen48a3b632013-04-25 20:41:01 +00003858static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003859{
3860 struct btrfs_ioctl_space_args space_args;
3861 struct btrfs_ioctl_space_info space;
3862 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003863 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003864 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003865 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003866 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3867 BTRFS_BLOCK_GROUP_SYSTEM,
3868 BTRFS_BLOCK_GROUP_METADATA,
3869 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3870 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003871 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003872 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003873 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003874 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003875
3876 if (copy_from_user(&space_args,
3877 (struct btrfs_ioctl_space_args __user *)arg,
3878 sizeof(space_args)))
3879 return -EFAULT;
3880
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003881 for (i = 0; i < num_types; i++) {
3882 struct btrfs_space_info *tmp;
3883
3884 info = NULL;
3885 rcu_read_lock();
3886 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3887 list) {
3888 if (tmp->flags == types[i]) {
3889 info = tmp;
3890 break;
3891 }
3892 }
3893 rcu_read_unlock();
3894
3895 if (!info)
3896 continue;
3897
3898 down_read(&info->groups_sem);
3899 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3900 if (!list_empty(&info->block_groups[c]))
3901 slot_count++;
3902 }
3903 up_read(&info->groups_sem);
3904 }
Josef Bacik1406e432010-01-13 18:19:06 +00003905
David Sterba36523e952014-02-07 14:34:12 +01003906 /*
3907 * Global block reserve, exported as a space_info
3908 */
3909 slot_count++;
3910
Chris Mason7fde62b2010-03-16 15:40:10 -04003911 /* space_slots == 0 means they are asking for a count */
3912 if (space_args.space_slots == 0) {
3913 space_args.total_spaces = slot_count;
3914 goto out;
3915 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003916
Dan Rosenberg51788b12011-02-14 16:04:23 -05003917 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003918
Chris Mason7fde62b2010-03-16 15:40:10 -04003919 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003920
Chris Mason7fde62b2010-03-16 15:40:10 -04003921 /* we generally have at most 6 or so space infos, one for each raid
3922 * level. So, a whole page should be more than enough for everyone
3923 */
3924 if (alloc_size > PAGE_CACHE_SIZE)
3925 return -ENOMEM;
3926
3927 space_args.total_spaces = 0;
3928 dest = kmalloc(alloc_size, GFP_NOFS);
3929 if (!dest)
3930 return -ENOMEM;
3931 dest_orig = dest;
3932
3933 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003934 for (i = 0; i < num_types; i++) {
3935 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003936
Dan Rosenberg51788b12011-02-14 16:04:23 -05003937 if (!slot_count)
3938 break;
3939
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003940 info = NULL;
3941 rcu_read_lock();
3942 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3943 list) {
3944 if (tmp->flags == types[i]) {
3945 info = tmp;
3946 break;
3947 }
3948 }
3949 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003950
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003951 if (!info)
3952 continue;
3953 down_read(&info->groups_sem);
3954 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3955 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003956 btrfs_get_block_group_info(
3957 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003958 memcpy(dest, &space, sizeof(space));
3959 dest++;
3960 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003961 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003962 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003963 if (!slot_count)
3964 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003965 }
3966 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003967 }
Josef Bacik1406e432010-01-13 18:19:06 +00003968
David Sterba36523e952014-02-07 14:34:12 +01003969 /*
3970 * Add global block reserve
3971 */
3972 if (slot_count) {
3973 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
3974
3975 spin_lock(&block_rsv->lock);
3976 space.total_bytes = block_rsv->size;
3977 space.used_bytes = block_rsv->size - block_rsv->reserved;
3978 spin_unlock(&block_rsv->lock);
3979 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3980 memcpy(dest, &space, sizeof(space));
3981 space_args.total_spaces++;
3982 }
3983
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003984 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003985 (arg + sizeof(struct btrfs_ioctl_space_args));
3986
3987 if (copy_to_user(user_dest, dest_orig, alloc_size))
3988 ret = -EFAULT;
3989
3990 kfree(dest_orig);
3991out:
3992 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003993 ret = -EFAULT;
3994
3995 return ret;
3996}
3997
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003998/*
3999 * there are many ways the trans_start and trans_end ioctls can lead
4000 * to deadlocks. They should only be used by applications that
4001 * basically own the machine, and have a very in depth understanding
4002 * of all the possible deadlocks and enospc problems.
4003 */
4004long btrfs_ioctl_trans_end(struct file *file)
4005{
Al Viro496ad9a2013-01-23 17:07:38 -05004006 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004007 struct btrfs_root *root = BTRFS_I(inode)->root;
4008 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004009
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004010 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04004011 if (!trans)
4012 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04004013 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04004014
Sage Weil1ab86ae2009-09-29 18:38:44 -04004015 btrfs_end_transaction(trans, root);
4016
Josef Bacika4abeea2011-04-11 17:25:13 -04004017 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04004018
Al Viro2a79f172011-12-09 08:06:57 -05004019 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04004020 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004021}
4022
Miao Xie9a8c28b2012-11-26 08:40:43 +00004023static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4024 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004025{
Sage Weil46204592010-10-29 15:41:32 -04004026 struct btrfs_trans_handle *trans;
4027 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004028 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004029
Miao Xied4edf392013-02-20 09:17:06 +00004030 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004031 if (IS_ERR(trans)) {
4032 if (PTR_ERR(trans) != -ENOENT)
4033 return PTR_ERR(trans);
4034
4035 /* No running transaction, don't bother */
4036 transid = root->fs_info->last_trans_committed;
4037 goto out;
4038 }
Sage Weil46204592010-10-29 15:41:32 -04004039 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004040 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004041 if (ret) {
4042 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004043 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004044 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004045out:
Sage Weil46204592010-10-29 15:41:32 -04004046 if (argp)
4047 if (copy_to_user(argp, &transid, sizeof(transid)))
4048 return -EFAULT;
4049 return 0;
4050}
4051
Miao Xie9a8c28b2012-11-26 08:40:43 +00004052static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4053 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004054{
Sage Weil46204592010-10-29 15:41:32 -04004055 u64 transid;
4056
4057 if (argp) {
4058 if (copy_from_user(&transid, argp, sizeof(transid)))
4059 return -EFAULT;
4060 } else {
4061 transid = 0; /* current trans */
4062 }
4063 return btrfs_wait_for_commit(root, transid);
4064}
4065
Miao Xieb8e95482012-11-26 08:48:01 +00004066static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004067{
Al Viro496ad9a2013-01-23 17:07:38 -05004068 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01004069 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004070 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004071
4072 if (!capable(CAP_SYS_ADMIN))
4073 return -EPERM;
4074
4075 sa = memdup_user(arg, sizeof(*sa));
4076 if (IS_ERR(sa))
4077 return PTR_ERR(sa);
4078
Miao Xieb8e95482012-11-26 08:48:01 +00004079 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4080 ret = mnt_want_write_file(file);
4081 if (ret)
4082 goto out;
4083 }
4084
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004085 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004086 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4087 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004088
4089 if (copy_to_user(arg, sa, sizeof(*sa)))
4090 ret = -EFAULT;
4091
Miao Xieb8e95482012-11-26 08:48:01 +00004092 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4093 mnt_drop_write_file(file);
4094out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004095 kfree(sa);
4096 return ret;
4097}
4098
4099static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4100{
4101 if (!capable(CAP_SYS_ADMIN))
4102 return -EPERM;
4103
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004104 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004105}
4106
4107static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4108 void __user *arg)
4109{
4110 struct btrfs_ioctl_scrub_args *sa;
4111 int ret;
4112
4113 if (!capable(CAP_SYS_ADMIN))
4114 return -EPERM;
4115
4116 sa = memdup_user(arg, sizeof(*sa));
4117 if (IS_ERR(sa))
4118 return PTR_ERR(sa);
4119
4120 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4121
4122 if (copy_to_user(arg, sa, sizeof(*sa)))
4123 ret = -EFAULT;
4124
4125 kfree(sa);
4126 return ret;
4127}
4128
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004129static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004130 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004131{
4132 struct btrfs_ioctl_get_dev_stats *sa;
4133 int ret;
4134
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004135 sa = memdup_user(arg, sizeof(*sa));
4136 if (IS_ERR(sa))
4137 return PTR_ERR(sa);
4138
David Sterbab27f7c02012-06-22 06:30:39 -06004139 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4140 kfree(sa);
4141 return -EPERM;
4142 }
4143
4144 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004145
4146 if (copy_to_user(arg, sa, sizeof(*sa)))
4147 ret = -EFAULT;
4148
4149 kfree(sa);
4150 return ret;
4151}
4152
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004153static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4154{
4155 struct btrfs_ioctl_dev_replace_args *p;
4156 int ret;
4157
4158 if (!capable(CAP_SYS_ADMIN))
4159 return -EPERM;
4160
4161 p = memdup_user(arg, sizeof(*p));
4162 if (IS_ERR(p))
4163 return PTR_ERR(p);
4164
4165 switch (p->cmd) {
4166 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004167 if (root->fs_info->sb->s_flags & MS_RDONLY) {
4168 ret = -EROFS;
4169 goto out;
4170 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004171 if (atomic_xchg(
4172 &root->fs_info->mutually_exclusive_operation_running,
4173 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004174 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004175 } else {
4176 ret = btrfs_dev_replace_start(root, p);
4177 atomic_set(
4178 &root->fs_info->mutually_exclusive_operation_running,
4179 0);
4180 }
4181 break;
4182 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4183 btrfs_dev_replace_status(root->fs_info, p);
4184 ret = 0;
4185 break;
4186 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4187 ret = btrfs_dev_replace_cancel(root->fs_info, p);
4188 break;
4189 default:
4190 ret = -EINVAL;
4191 break;
4192 }
4193
4194 if (copy_to_user(arg, p, sizeof(*p)))
4195 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004196out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004197 kfree(p);
4198 return ret;
4199}
4200
Jan Schmidtd7728c92011-07-07 16:48:38 +02004201static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4202{
4203 int ret = 0;
4204 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004205 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004206 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004207 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004208 struct inode_fs_paths *ipath = NULL;
4209 struct btrfs_path *path;
4210
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004211 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004212 return -EPERM;
4213
4214 path = btrfs_alloc_path();
4215 if (!path) {
4216 ret = -ENOMEM;
4217 goto out;
4218 }
4219
4220 ipa = memdup_user(arg, sizeof(*ipa));
4221 if (IS_ERR(ipa)) {
4222 ret = PTR_ERR(ipa);
4223 ipa = NULL;
4224 goto out;
4225 }
4226
4227 size = min_t(u32, ipa->size, 4096);
4228 ipath = init_ipath(size, root, path);
4229 if (IS_ERR(ipath)) {
4230 ret = PTR_ERR(ipath);
4231 ipath = NULL;
4232 goto out;
4233 }
4234
4235 ret = paths_from_inode(ipa->inum, ipath);
4236 if (ret < 0)
4237 goto out;
4238
4239 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004240 rel_ptr = ipath->fspath->val[i] -
4241 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004242 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004243 }
4244
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004245 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4246 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004247 if (ret) {
4248 ret = -EFAULT;
4249 goto out;
4250 }
4251
4252out:
4253 btrfs_free_path(path);
4254 free_ipath(ipath);
4255 kfree(ipa);
4256
4257 return ret;
4258}
4259
4260static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4261{
4262 struct btrfs_data_container *inodes = ctx;
4263 const size_t c = 3 * sizeof(u64);
4264
4265 if (inodes->bytes_left >= c) {
4266 inodes->bytes_left -= c;
4267 inodes->val[inodes->elem_cnt] = inum;
4268 inodes->val[inodes->elem_cnt + 1] = offset;
4269 inodes->val[inodes->elem_cnt + 2] = root;
4270 inodes->elem_cnt += 3;
4271 } else {
4272 inodes->bytes_missing += c - inodes->bytes_left;
4273 inodes->bytes_left = 0;
4274 inodes->elem_missed += 3;
4275 }
4276
4277 return 0;
4278}
4279
4280static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4281 void __user *arg)
4282{
4283 int ret = 0;
4284 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004285 struct btrfs_ioctl_logical_ino_args *loi;
4286 struct btrfs_data_container *inodes = NULL;
4287 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004288
4289 if (!capable(CAP_SYS_ADMIN))
4290 return -EPERM;
4291
4292 loi = memdup_user(arg, sizeof(*loi));
4293 if (IS_ERR(loi)) {
4294 ret = PTR_ERR(loi);
4295 loi = NULL;
4296 goto out;
4297 }
4298
4299 path = btrfs_alloc_path();
4300 if (!path) {
4301 ret = -ENOMEM;
4302 goto out;
4303 }
4304
Liu Bo425d17a2012-09-07 20:01:30 -06004305 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004306 inodes = init_data_container(size);
4307 if (IS_ERR(inodes)) {
4308 ret = PTR_ERR(inodes);
4309 inodes = NULL;
4310 goto out;
4311 }
4312
Liu Bodf031f02012-09-07 20:01:29 -06004313 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4314 build_ino_list, inodes);
4315 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004316 ret = -ENOENT;
4317 if (ret < 0)
4318 goto out;
4319
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004320 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4321 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004322 if (ret)
4323 ret = -EFAULT;
4324
4325out:
4326 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06004327 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004328 kfree(loi);
4329
4330 return ret;
4331}
4332
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004333void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004334 struct btrfs_ioctl_balance_args *bargs)
4335{
4336 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4337
4338 bargs->flags = bctl->flags;
4339
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004340 if (atomic_read(&fs_info->balance_running))
4341 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4342 if (atomic_read(&fs_info->balance_pause_req))
4343 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004344 if (atomic_read(&fs_info->balance_cancel_req))
4345 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004346
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004347 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4348 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4349 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004350
4351 if (lock) {
4352 spin_lock(&fs_info->balance_lock);
4353 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4354 spin_unlock(&fs_info->balance_lock);
4355 } else {
4356 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4357 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004358}
4359
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004360static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004361{
Al Viro496ad9a2013-01-23 17:07:38 -05004362 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004363 struct btrfs_fs_info *fs_info = root->fs_info;
4364 struct btrfs_ioctl_balance_args *bargs;
4365 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004366 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004367 int ret;
4368
4369 if (!capable(CAP_SYS_ADMIN))
4370 return -EPERM;
4371
Liu Boe54bfa32012-06-29 03:58:48 -06004372 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004373 if (ret)
4374 return ret;
4375
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004376again:
4377 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4378 mutex_lock(&fs_info->volume_mutex);
4379 mutex_lock(&fs_info->balance_mutex);
4380 need_unlock = true;
4381 goto locked;
4382 }
4383
4384 /*
4385 * mut. excl. ops lock is locked. Three possibilites:
4386 * (1) some other op is running
4387 * (2) balance is running
4388 * (3) balance is paused -- special case (think resume)
4389 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004390 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004391 if (fs_info->balance_ctl) {
4392 /* this is either (2) or (3) */
4393 if (!atomic_read(&fs_info->balance_running)) {
4394 mutex_unlock(&fs_info->balance_mutex);
4395 if (!mutex_trylock(&fs_info->volume_mutex))
4396 goto again;
4397 mutex_lock(&fs_info->balance_mutex);
4398
4399 if (fs_info->balance_ctl &&
4400 !atomic_read(&fs_info->balance_running)) {
4401 /* this is (3) */
4402 need_unlock = false;
4403 goto locked;
4404 }
4405
4406 mutex_unlock(&fs_info->balance_mutex);
4407 mutex_unlock(&fs_info->volume_mutex);
4408 goto again;
4409 } else {
4410 /* this is (2) */
4411 mutex_unlock(&fs_info->balance_mutex);
4412 ret = -EINPROGRESS;
4413 goto out;
4414 }
4415 } else {
4416 /* this is (1) */
4417 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004418 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004419 goto out;
4420 }
4421
4422locked:
4423 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004424
4425 if (arg) {
4426 bargs = memdup_user(arg, sizeof(*bargs));
4427 if (IS_ERR(bargs)) {
4428 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004429 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004430 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004431
4432 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4433 if (!fs_info->balance_ctl) {
4434 ret = -ENOTCONN;
4435 goto out_bargs;
4436 }
4437
4438 bctl = fs_info->balance_ctl;
4439 spin_lock(&fs_info->balance_lock);
4440 bctl->flags |= BTRFS_BALANCE_RESUME;
4441 spin_unlock(&fs_info->balance_lock);
4442
4443 goto do_balance;
4444 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004445 } else {
4446 bargs = NULL;
4447 }
4448
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004449 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004450 ret = -EINPROGRESS;
4451 goto out_bargs;
4452 }
4453
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004454 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4455 if (!bctl) {
4456 ret = -ENOMEM;
4457 goto out_bargs;
4458 }
4459
4460 bctl->fs_info = fs_info;
4461 if (arg) {
4462 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4463 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4464 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4465
4466 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004467 } else {
4468 /* balance everything - no filters */
4469 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004470 }
4471
Ilya Dryomovde322262012-01-16 22:04:49 +02004472do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004473 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004474 * Ownership of bctl and mutually_exclusive_operation_running
4475 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4476 * or, if restriper was paused all the way until unmount, in
4477 * free_fs_info. mutually_exclusive_operation_running is
4478 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004479 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004480 need_unlock = false;
4481
4482 ret = btrfs_balance(bctl, bargs);
4483
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004484 if (arg) {
4485 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4486 ret = -EFAULT;
4487 }
4488
4489out_bargs:
4490 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004491out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004492 mutex_unlock(&fs_info->balance_mutex);
4493 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004494 if (need_unlock)
4495 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4496out:
Liu Boe54bfa32012-06-29 03:58:48 -06004497 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004498 return ret;
4499}
4500
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004501static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4502{
4503 if (!capable(CAP_SYS_ADMIN))
4504 return -EPERM;
4505
4506 switch (cmd) {
4507 case BTRFS_BALANCE_CTL_PAUSE:
4508 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004509 case BTRFS_BALANCE_CTL_CANCEL:
4510 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004511 }
4512
4513 return -EINVAL;
4514}
4515
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004516static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4517 void __user *arg)
4518{
4519 struct btrfs_fs_info *fs_info = root->fs_info;
4520 struct btrfs_ioctl_balance_args *bargs;
4521 int ret = 0;
4522
4523 if (!capable(CAP_SYS_ADMIN))
4524 return -EPERM;
4525
4526 mutex_lock(&fs_info->balance_mutex);
4527 if (!fs_info->balance_ctl) {
4528 ret = -ENOTCONN;
4529 goto out;
4530 }
4531
4532 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4533 if (!bargs) {
4534 ret = -ENOMEM;
4535 goto out;
4536 }
4537
4538 update_ioctl_balance_args(fs_info, 1, bargs);
4539
4540 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4541 ret = -EFAULT;
4542
4543 kfree(bargs);
4544out:
4545 mutex_unlock(&fs_info->balance_mutex);
4546 return ret;
4547}
4548
Miao Xie905b0dd2012-11-26 08:50:11 +00004549static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004550{
Al Viro496ad9a2013-01-23 17:07:38 -05004551 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004552 struct btrfs_ioctl_quota_ctl_args *sa;
4553 struct btrfs_trans_handle *trans = NULL;
4554 int ret;
4555 int err;
4556
4557 if (!capable(CAP_SYS_ADMIN))
4558 return -EPERM;
4559
Miao Xie905b0dd2012-11-26 08:50:11 +00004560 ret = mnt_want_write_file(file);
4561 if (ret)
4562 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004563
4564 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004565 if (IS_ERR(sa)) {
4566 ret = PTR_ERR(sa);
4567 goto drop_write;
4568 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004569
Wang Shilong7708f022013-04-07 10:24:57 +00004570 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004571 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4572 if (IS_ERR(trans)) {
4573 ret = PTR_ERR(trans);
4574 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004575 }
4576
4577 switch (sa->cmd) {
4578 case BTRFS_QUOTA_CTL_ENABLE:
4579 ret = btrfs_quota_enable(trans, root->fs_info);
4580 break;
4581 case BTRFS_QUOTA_CTL_DISABLE:
4582 ret = btrfs_quota_disable(trans, root->fs_info);
4583 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004584 default:
4585 ret = -EINVAL;
4586 break;
4587 }
4588
Jan Schmidt2f232032013-04-25 16:04:51 +00004589 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4590 if (err && !ret)
4591 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004592out:
4593 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004594 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004595drop_write:
4596 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004597 return ret;
4598}
4599
Miao Xie905b0dd2012-11-26 08:50:11 +00004600static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004601{
Al Viro496ad9a2013-01-23 17:07:38 -05004602 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004603 struct btrfs_ioctl_qgroup_assign_args *sa;
4604 struct btrfs_trans_handle *trans;
4605 int ret;
4606 int err;
4607
4608 if (!capable(CAP_SYS_ADMIN))
4609 return -EPERM;
4610
Miao Xie905b0dd2012-11-26 08:50:11 +00004611 ret = mnt_want_write_file(file);
4612 if (ret)
4613 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004614
4615 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004616 if (IS_ERR(sa)) {
4617 ret = PTR_ERR(sa);
4618 goto drop_write;
4619 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004620
4621 trans = btrfs_join_transaction(root);
4622 if (IS_ERR(trans)) {
4623 ret = PTR_ERR(trans);
4624 goto out;
4625 }
4626
4627 /* FIXME: check if the IDs really exist */
4628 if (sa->assign) {
4629 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4630 sa->src, sa->dst);
4631 } else {
4632 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4633 sa->src, sa->dst);
4634 }
4635
4636 err = btrfs_end_transaction(trans, root);
4637 if (err && !ret)
4638 ret = err;
4639
4640out:
4641 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004642drop_write:
4643 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004644 return ret;
4645}
4646
Miao Xie905b0dd2012-11-26 08:50:11 +00004647static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004648{
Al Viro496ad9a2013-01-23 17:07:38 -05004649 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004650 struct btrfs_ioctl_qgroup_create_args *sa;
4651 struct btrfs_trans_handle *trans;
4652 int ret;
4653 int err;
4654
4655 if (!capable(CAP_SYS_ADMIN))
4656 return -EPERM;
4657
Miao Xie905b0dd2012-11-26 08:50:11 +00004658 ret = mnt_want_write_file(file);
4659 if (ret)
4660 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004661
4662 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004663 if (IS_ERR(sa)) {
4664 ret = PTR_ERR(sa);
4665 goto drop_write;
4666 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004667
Miao Xied86e56c2012-11-15 11:35:41 +00004668 if (!sa->qgroupid) {
4669 ret = -EINVAL;
4670 goto out;
4671 }
4672
Arne Jansen5d13a372011-09-14 15:53:51 +02004673 trans = btrfs_join_transaction(root);
4674 if (IS_ERR(trans)) {
4675 ret = PTR_ERR(trans);
4676 goto out;
4677 }
4678
4679 /* FIXME: check if the IDs really exist */
4680 if (sa->create) {
4681 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4682 NULL);
4683 } else {
4684 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4685 }
4686
4687 err = btrfs_end_transaction(trans, root);
4688 if (err && !ret)
4689 ret = err;
4690
4691out:
4692 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004693drop_write:
4694 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004695 return ret;
4696}
4697
Miao Xie905b0dd2012-11-26 08:50:11 +00004698static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004699{
Al Viro496ad9a2013-01-23 17:07:38 -05004700 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004701 struct btrfs_ioctl_qgroup_limit_args *sa;
4702 struct btrfs_trans_handle *trans;
4703 int ret;
4704 int err;
4705 u64 qgroupid;
4706
4707 if (!capable(CAP_SYS_ADMIN))
4708 return -EPERM;
4709
Miao Xie905b0dd2012-11-26 08:50:11 +00004710 ret = mnt_want_write_file(file);
4711 if (ret)
4712 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004713
4714 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004715 if (IS_ERR(sa)) {
4716 ret = PTR_ERR(sa);
4717 goto drop_write;
4718 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004719
4720 trans = btrfs_join_transaction(root);
4721 if (IS_ERR(trans)) {
4722 ret = PTR_ERR(trans);
4723 goto out;
4724 }
4725
4726 qgroupid = sa->qgroupid;
4727 if (!qgroupid) {
4728 /* take the current subvol as qgroup */
4729 qgroupid = root->root_key.objectid;
4730 }
4731
4732 /* FIXME: check if the IDs really exist */
4733 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4734
4735 err = btrfs_end_transaction(trans, root);
4736 if (err && !ret)
4737 ret = err;
4738
4739out:
4740 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004741drop_write:
4742 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004743 return ret;
4744}
4745
Jan Schmidt2f232032013-04-25 16:04:51 +00004746static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4747{
Al Viro6d0379e2013-06-16 19:32:35 +04004748 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004749 struct btrfs_ioctl_quota_rescan_args *qsa;
4750 int ret;
4751
4752 if (!capable(CAP_SYS_ADMIN))
4753 return -EPERM;
4754
4755 ret = mnt_want_write_file(file);
4756 if (ret)
4757 return ret;
4758
4759 qsa = memdup_user(arg, sizeof(*qsa));
4760 if (IS_ERR(qsa)) {
4761 ret = PTR_ERR(qsa);
4762 goto drop_write;
4763 }
4764
4765 if (qsa->flags) {
4766 ret = -EINVAL;
4767 goto out;
4768 }
4769
4770 ret = btrfs_qgroup_rescan(root->fs_info);
4771
4772out:
4773 kfree(qsa);
4774drop_write:
4775 mnt_drop_write_file(file);
4776 return ret;
4777}
4778
4779static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4780{
Al Viro6d0379e2013-06-16 19:32:35 +04004781 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004782 struct btrfs_ioctl_quota_rescan_args *qsa;
4783 int ret = 0;
4784
4785 if (!capable(CAP_SYS_ADMIN))
4786 return -EPERM;
4787
4788 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4789 if (!qsa)
4790 return -ENOMEM;
4791
4792 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4793 qsa->flags = 1;
4794 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4795 }
4796
4797 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4798 ret = -EFAULT;
4799
4800 kfree(qsa);
4801 return ret;
4802}
4803
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004804static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4805{
Al Viro54563d42013-09-01 15:57:51 -04004806 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004807
4808 if (!capable(CAP_SYS_ADMIN))
4809 return -EPERM;
4810
4811 return btrfs_qgroup_wait_for_completion(root->fs_info);
4812}
4813
Hugo Millsabccd002014-01-30 20:17:00 +00004814static long _btrfs_ioctl_set_received_subvol(struct file *file,
4815 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004816{
Al Viro496ad9a2013-01-23 17:07:38 -05004817 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004818 struct btrfs_root *root = BTRFS_I(inode)->root;
4819 struct btrfs_root_item *root_item = &root->root_item;
4820 struct btrfs_trans_handle *trans;
4821 struct timespec ct = CURRENT_TIME;
4822 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004823 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004824
David Sterbabd60ea02014-01-16 15:50:22 +01004825 if (!inode_owner_or_capable(inode))
4826 return -EPERM;
4827
Alexander Block8ea05e32012-07-25 17:35:53 +02004828 ret = mnt_want_write_file(file);
4829 if (ret < 0)
4830 return ret;
4831
4832 down_write(&root->fs_info->subvol_sem);
4833
4834 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4835 ret = -EINVAL;
4836 goto out;
4837 }
4838
4839 if (btrfs_root_readonly(root)) {
4840 ret = -EROFS;
4841 goto out;
4842 }
4843
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004844 /*
4845 * 1 - root item
4846 * 2 - uuid items (received uuid + subvol uuid)
4847 */
4848 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004849 if (IS_ERR(trans)) {
4850 ret = PTR_ERR(trans);
4851 trans = NULL;
4852 goto out;
4853 }
4854
4855 sa->rtransid = trans->transid;
4856 sa->rtime.sec = ct.tv_sec;
4857 sa->rtime.nsec = ct.tv_nsec;
4858
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004859 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4860 BTRFS_UUID_SIZE);
4861 if (received_uuid_changed &&
4862 !btrfs_is_empty_uuid(root_item->received_uuid))
4863 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4864 root_item->received_uuid,
4865 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4866 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004867 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4868 btrfs_set_root_stransid(root_item, sa->stransid);
4869 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004870 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4871 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4872 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4873 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004874
4875 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4876 &root->root_key, &root->root_item);
4877 if (ret < 0) {
4878 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004879 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004880 }
4881 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4882 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4883 sa->uuid,
4884 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4885 root->root_key.objectid);
4886 if (ret < 0 && ret != -EEXIST) {
4887 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004888 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004889 }
4890 }
4891 ret = btrfs_commit_transaction(trans, root);
4892 if (ret < 0) {
4893 btrfs_abort_transaction(trans, root, ret);
4894 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004895 }
4896
Hugo Millsabccd002014-01-30 20:17:00 +00004897out:
4898 up_write(&root->fs_info->subvol_sem);
4899 mnt_drop_write_file(file);
4900 return ret;
4901}
4902
4903#ifdef CONFIG_64BIT
4904static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4905 void __user *arg)
4906{
4907 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4908 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4909 int ret = 0;
4910
4911 args32 = memdup_user(arg, sizeof(*args32));
4912 if (IS_ERR(args32)) {
4913 ret = PTR_ERR(args32);
4914 args32 = NULL;
4915 goto out;
4916 }
4917
4918 args64 = kmalloc(sizeof(*args64), GFP_NOFS);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03004919 if (!args64) {
4920 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00004921 goto out;
4922 }
4923
4924 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4925 args64->stransid = args32->stransid;
4926 args64->rtransid = args32->rtransid;
4927 args64->stime.sec = args32->stime.sec;
4928 args64->stime.nsec = args32->stime.nsec;
4929 args64->rtime.sec = args32->rtime.sec;
4930 args64->rtime.nsec = args32->rtime.nsec;
4931 args64->flags = args32->flags;
4932
4933 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4934 if (ret)
4935 goto out;
4936
4937 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4938 args32->stransid = args64->stransid;
4939 args32->rtransid = args64->rtransid;
4940 args32->stime.sec = args64->stime.sec;
4941 args32->stime.nsec = args64->stime.nsec;
4942 args32->rtime.sec = args64->rtime.sec;
4943 args32->rtime.nsec = args64->rtime.nsec;
4944 args32->flags = args64->flags;
4945
4946 ret = copy_to_user(arg, args32, sizeof(*args32));
4947 if (ret)
4948 ret = -EFAULT;
4949
4950out:
4951 kfree(args32);
4952 kfree(args64);
4953 return ret;
4954}
4955#endif
4956
4957static long btrfs_ioctl_set_received_subvol(struct file *file,
4958 void __user *arg)
4959{
4960 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4961 int ret = 0;
4962
4963 sa = memdup_user(arg, sizeof(*sa));
4964 if (IS_ERR(sa)) {
4965 ret = PTR_ERR(sa);
4966 sa = NULL;
4967 goto out;
4968 }
4969
4970 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4971
4972 if (ret)
4973 goto out;
4974
Alexander Block8ea05e32012-07-25 17:35:53 +02004975 ret = copy_to_user(arg, sa, sizeof(*sa));
4976 if (ret)
4977 ret = -EFAULT;
4978
4979out:
4980 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02004981 return ret;
4982}
4983
jeff.liu867ab662013-01-05 02:48:01 +00004984static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4985{
Al Viro6d0379e2013-06-16 19:32:35 +04004986 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004987 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004988 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004989 char label[BTRFS_LABEL_SIZE];
4990
4991 spin_lock(&root->fs_info->super_lock);
4992 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4993 spin_unlock(&root->fs_info->super_lock);
4994
4995 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004996
4997 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004998 btrfs_warn(root->fs_info,
4999 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00005000 }
5001
jeff.liu867ab662013-01-05 02:48:01 +00005002 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005003
5004 return ret ? -EFAULT : 0;
5005}
5006
jeff.liua8bfd4a2013-01-05 02:48:08 +00005007static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5008{
Al Viro6d0379e2013-06-16 19:32:35 +04005009 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005010 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5011 struct btrfs_trans_handle *trans;
5012 char label[BTRFS_LABEL_SIZE];
5013 int ret;
5014
5015 if (!capable(CAP_SYS_ADMIN))
5016 return -EPERM;
5017
5018 if (copy_from_user(label, arg, sizeof(label)))
5019 return -EFAULT;
5020
5021 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005022 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
jeff.liua8bfd4a2013-01-05 02:48:08 +00005023 BTRFS_LABEL_SIZE - 1);
5024 return -EINVAL;
5025 }
5026
5027 ret = mnt_want_write_file(file);
5028 if (ret)
5029 return ret;
5030
jeff.liua8bfd4a2013-01-05 02:48:08 +00005031 trans = btrfs_start_transaction(root, 0);
5032 if (IS_ERR(trans)) {
5033 ret = PTR_ERR(trans);
5034 goto out_unlock;
5035 }
5036
Anand Jaina1b83ac2013-07-19 17:39:32 +08005037 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005038 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005039 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005040 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005041
5042out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005043 mnt_drop_write_file(file);
5044 return ret;
5045}
5046
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005047#define INIT_FEATURE_FLAGS(suffix) \
5048 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5049 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5050 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5051
5052static int btrfs_ioctl_get_supported_features(struct file *file,
5053 void __user *arg)
5054{
5055 static struct btrfs_ioctl_feature_flags features[3] = {
5056 INIT_FEATURE_FLAGS(SUPP),
5057 INIT_FEATURE_FLAGS(SAFE_SET),
5058 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5059 };
5060
5061 if (copy_to_user(arg, &features, sizeof(features)))
5062 return -EFAULT;
5063
5064 return 0;
5065}
5066
5067static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5068{
5069 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5070 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5071 struct btrfs_ioctl_feature_flags features;
5072
5073 features.compat_flags = btrfs_super_compat_flags(super_block);
5074 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5075 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5076
5077 if (copy_to_user(arg, &features, sizeof(features)))
5078 return -EFAULT;
5079
5080 return 0;
5081}
5082
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005083static int check_feature_bits(struct btrfs_root *root,
5084 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005085 u64 change_mask, u64 flags, u64 supported_flags,
5086 u64 safe_set, u64 safe_clear)
5087{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005088 const char *type = btrfs_feature_set_names[set];
5089 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005090 u64 disallowed, unsupported;
5091 u64 set_mask = flags & change_mask;
5092 u64 clear_mask = ~flags & change_mask;
5093
5094 unsupported = set_mask & ~supported_flags;
5095 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005096 names = btrfs_printable_features(set, unsupported);
5097 if (names) {
5098 btrfs_warn(root->fs_info,
5099 "this kernel does not support the %s feature bit%s",
5100 names, strchr(names, ',') ? "s" : "");
5101 kfree(names);
5102 } else
5103 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005104 "this kernel does not support %s bits 0x%llx",
5105 type, unsupported);
5106 return -EOPNOTSUPP;
5107 }
5108
5109 disallowed = set_mask & ~safe_set;
5110 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005111 names = btrfs_printable_features(set, disallowed);
5112 if (names) {
5113 btrfs_warn(root->fs_info,
5114 "can't set the %s feature bit%s while mounted",
5115 names, strchr(names, ',') ? "s" : "");
5116 kfree(names);
5117 } else
5118 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005119 "can't set %s bits 0x%llx while mounted",
5120 type, disallowed);
5121 return -EPERM;
5122 }
5123
5124 disallowed = clear_mask & ~safe_clear;
5125 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005126 names = btrfs_printable_features(set, disallowed);
5127 if (names) {
5128 btrfs_warn(root->fs_info,
5129 "can't clear the %s feature bit%s while mounted",
5130 names, strchr(names, ',') ? "s" : "");
5131 kfree(names);
5132 } else
5133 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005134 "can't clear %s bits 0x%llx while mounted",
5135 type, disallowed);
5136 return -EPERM;
5137 }
5138
5139 return 0;
5140}
5141
5142#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005143check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005144 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5145 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5146 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5147
5148static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5149{
5150 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5151 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5152 struct btrfs_ioctl_feature_flags flags[2];
5153 struct btrfs_trans_handle *trans;
5154 u64 newflags;
5155 int ret;
5156
5157 if (!capable(CAP_SYS_ADMIN))
5158 return -EPERM;
5159
5160 if (copy_from_user(flags, arg, sizeof(flags)))
5161 return -EFAULT;
5162
5163 /* Nothing to do */
5164 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5165 !flags[0].incompat_flags)
5166 return 0;
5167
5168 ret = check_feature(root, flags[0].compat_flags,
5169 flags[1].compat_flags, COMPAT);
5170 if (ret)
5171 return ret;
5172
5173 ret = check_feature(root, flags[0].compat_ro_flags,
5174 flags[1].compat_ro_flags, COMPAT_RO);
5175 if (ret)
5176 return ret;
5177
5178 ret = check_feature(root, flags[0].incompat_flags,
5179 flags[1].incompat_flags, INCOMPAT);
5180 if (ret)
5181 return ret;
5182
David Sterba8051aa12014-02-07 14:34:04 +01005183 trans = btrfs_start_transaction(root, 0);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005184 if (IS_ERR(trans))
5185 return PTR_ERR(trans);
5186
5187 spin_lock(&root->fs_info->super_lock);
5188 newflags = btrfs_super_compat_flags(super_block);
5189 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5190 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5191 btrfs_set_super_compat_flags(super_block, newflags);
5192
5193 newflags = btrfs_super_compat_ro_flags(super_block);
5194 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5195 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5196 btrfs_set_super_compat_ro_flags(super_block, newflags);
5197
5198 newflags = btrfs_super_incompat_flags(super_block);
5199 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5200 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5201 btrfs_set_super_incompat_flags(super_block, newflags);
5202 spin_unlock(&root->fs_info->super_lock);
5203
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01005204 return btrfs_commit_transaction(trans, root);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005205}
5206
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005207long btrfs_ioctl(struct file *file, unsigned int
5208 cmd, unsigned long arg)
5209{
Al Viro496ad9a2013-01-23 17:07:38 -05005210 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005211 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005212
5213 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005214 case FS_IOC_GETFLAGS:
5215 return btrfs_ioctl_getflags(file, argp);
5216 case FS_IOC_SETFLAGS:
5217 return btrfs_ioctl_setflags(file, argp);
5218 case FS_IOC_GETVERSION:
5219 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005220 case FITRIM:
5221 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005222 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005223 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005224 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005225 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005226 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005227 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005228 case BTRFS_IOC_SUBVOL_CREATE_V2:
5229 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005230 case BTRFS_IOC_SNAP_DESTROY:
5231 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005232 case BTRFS_IOC_SUBVOL_GETFLAGS:
5233 return btrfs_ioctl_subvol_getflags(file, argp);
5234 case BTRFS_IOC_SUBVOL_SETFLAGS:
5235 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005236 case BTRFS_IOC_DEFAULT_SUBVOL:
5237 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005238 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005239 return btrfs_ioctl_defrag(file, NULL);
5240 case BTRFS_IOC_DEFRAG_RANGE:
5241 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005242 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005243 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005244 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005245 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005246 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005247 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005248 case BTRFS_IOC_FS_INFO:
5249 return btrfs_ioctl_fs_info(root, argp);
5250 case BTRFS_IOC_DEV_INFO:
5251 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005252 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005253 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005254 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05005255 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
5256 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05005257 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005258 case BTRFS_IOC_TRANS_START:
5259 return btrfs_ioctl_trans_start(file);
5260 case BTRFS_IOC_TRANS_END:
5261 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05005262 case BTRFS_IOC_TREE_SEARCH:
5263 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005264 case BTRFS_IOC_TREE_SEARCH_V2:
5265 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005266 case BTRFS_IOC_INO_LOOKUP:
5267 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005268 case BTRFS_IOC_INO_PATHS:
5269 return btrfs_ioctl_ino_to_path(root, argp);
5270 case BTRFS_IOC_LOGICAL_INO:
5271 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00005272 case BTRFS_IOC_SPACE_INFO:
5273 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005274 case BTRFS_IOC_SYNC: {
5275 int ret;
5276
Miao Xie6c255e62014-03-06 13:55:01 +08005277 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005278 if (ret)
5279 return ret;
5280 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
5281 return ret;
5282 }
Sage Weil46204592010-10-29 15:41:32 -04005283 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005284 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005285 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005286 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005287 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005288 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005289 case BTRFS_IOC_SCRUB_CANCEL:
5290 return btrfs_ioctl_scrub_cancel(root, argp);
5291 case BTRFS_IOC_SCRUB_PROGRESS:
5292 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005293 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005294 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005295 case BTRFS_IOC_BALANCE_CTL:
5296 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005297 case BTRFS_IOC_BALANCE_PROGRESS:
5298 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005299 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5300 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005301#ifdef CONFIG_64BIT
5302 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5303 return btrfs_ioctl_set_received_subvol_32(file, argp);
5304#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005305 case BTRFS_IOC_SEND:
5306 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005307 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06005308 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005309 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005310 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005311 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005312 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005313 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005314 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005315 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005316 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005317 case BTRFS_IOC_QUOTA_RESCAN:
5318 return btrfs_ioctl_quota_rescan(file, argp);
5319 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5320 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005321 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5322 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005323 case BTRFS_IOC_DEV_REPLACE:
5324 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005325 case BTRFS_IOC_GET_FSLABEL:
5326 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005327 case BTRFS_IOC_SET_FSLABEL:
5328 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07005329 case BTRFS_IOC_FILE_EXTENT_SAME:
5330 return btrfs_ioctl_file_extent_same(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005331 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5332 return btrfs_ioctl_get_supported_features(file, argp);
5333 case BTRFS_IOC_GET_FEATURES:
5334 return btrfs_ioctl_get_features(file, argp);
5335 case BTRFS_IOC_SET_FEATURES:
5336 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005337 }
5338
5339 return -ENOTTY;
5340}