blob: 6523108d29844bf804a9a23cb875342ef5c4dc6e [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"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040059
Mark Fasheh416161d2013-08-06 11:42:51 -070060static int btrfs_clone(struct inode *src, struct inode *inode,
61 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
62
Christoph Hellwig6cbff002009-04-17 10:37:41 +020063/* Mask out flags that are inappropriate for the given type of inode. */
64static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
65{
66 if (S_ISDIR(mode))
67 return flags;
68 else if (S_ISREG(mode))
69 return flags & ~FS_DIRSYNC_FL;
70 else
71 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
72}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040073
Christoph Hellwig6cbff002009-04-17 10:37:41 +020074/*
75 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
76 */
77static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
78{
79 unsigned int iflags = 0;
80
81 if (flags & BTRFS_INODE_SYNC)
82 iflags |= FS_SYNC_FL;
83 if (flags & BTRFS_INODE_IMMUTABLE)
84 iflags |= FS_IMMUTABLE_FL;
85 if (flags & BTRFS_INODE_APPEND)
86 iflags |= FS_APPEND_FL;
87 if (flags & BTRFS_INODE_NODUMP)
88 iflags |= FS_NODUMP_FL;
89 if (flags & BTRFS_INODE_NOATIME)
90 iflags |= FS_NOATIME_FL;
91 if (flags & BTRFS_INODE_DIRSYNC)
92 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000093 if (flags & BTRFS_INODE_NODATACOW)
94 iflags |= FS_NOCOW_FL;
95
96 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
97 iflags |= FS_COMPR_FL;
98 else if (flags & BTRFS_INODE_NOCOMPRESS)
99 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200100
101 return iflags;
102}
103
104/*
105 * Update inode->i_flags based on the btrfs internal flags.
106 */
107void btrfs_update_iflags(struct inode *inode)
108{
109 struct btrfs_inode *ip = BTRFS_I(inode);
110
111 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
112
113 if (ip->flags & BTRFS_INODE_SYNC)
114 inode->i_flags |= S_SYNC;
115 if (ip->flags & BTRFS_INODE_IMMUTABLE)
116 inode->i_flags |= S_IMMUTABLE;
117 if (ip->flags & BTRFS_INODE_APPEND)
118 inode->i_flags |= S_APPEND;
119 if (ip->flags & BTRFS_INODE_NOATIME)
120 inode->i_flags |= S_NOATIME;
121 if (ip->flags & BTRFS_INODE_DIRSYNC)
122 inode->i_flags |= S_DIRSYNC;
123}
124
125/*
126 * Inherit flags from the parent inode.
127 *
Josef Bacike27425d2011-09-27 11:01:30 -0400128 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129 */
130void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
131{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400132 unsigned int flags;
133
134 if (!dir)
135 return;
136
137 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200138
Josef Bacike27425d2011-09-27 11:01:30 -0400139 if (flags & BTRFS_INODE_NOCOMPRESS) {
140 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
141 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
142 } else if (flags & BTRFS_INODE_COMPRESS) {
143 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
144 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
145 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200146
Liu Bo213490b2012-09-11 08:33:50 -0600147 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400148 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600149 if (S_ISREG(inode->i_mode))
150 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
151 }
Josef Bacike27425d2011-09-27 11:01:30 -0400152
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200153 btrfs_update_iflags(inode);
154}
155
156static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
157{
Al Viro496ad9a2013-01-23 17:07:38 -0500158 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200159 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
160
161 if (copy_to_user(arg, &flags, sizeof(flags)))
162 return -EFAULT;
163 return 0;
164}
165
Liu Bo75e7cb72011-03-22 10:12:20 +0000166static int check_flags(unsigned int flags)
167{
168 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
169 FS_NOATIME_FL | FS_NODUMP_FL | \
170 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000171 FS_NOCOMP_FL | FS_COMPR_FL |
172 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000173 return -EOPNOTSUPP;
174
175 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
176 return -EINVAL;
177
Liu Bo75e7cb72011-03-22 10:12:20 +0000178 return 0;
179}
180
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
182{
Al Viro496ad9a2013-01-23 17:07:38 -0500183 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200184 struct btrfs_inode *ip = BTRFS_I(inode);
185 struct btrfs_root *root = ip->root;
186 struct btrfs_trans_handle *trans;
187 unsigned int flags, oldflags;
188 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800189 u64 ip_oldflags;
190 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600191 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200192
Li Zefanb83cc962010-12-20 16:04:08 +0800193 if (btrfs_root_readonly(root))
194 return -EROFS;
195
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200196 if (copy_from_user(&flags, arg, sizeof(flags)))
197 return -EFAULT;
198
Liu Bo75e7cb72011-03-22 10:12:20 +0000199 ret = check_flags(flags);
200 if (ret)
201 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200202
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700203 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200204 return -EACCES;
205
Jan Karae7848682012-06-12 16:20:32 +0200206 ret = mnt_want_write_file(file);
207 if (ret)
208 return ret;
209
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200210 mutex_lock(&inode->i_mutex);
211
Li Zefanf062abf2011-12-29 13:36:45 +0800212 ip_oldflags = ip->flags;
213 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600214 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800215
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200216 flags = btrfs_mask_flags(inode->i_mode, flags);
217 oldflags = btrfs_flags_to_ioctl(ip->flags);
218 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
219 if (!capable(CAP_LINUX_IMMUTABLE)) {
220 ret = -EPERM;
221 goto out_unlock;
222 }
223 }
224
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200225 if (flags & FS_SYNC_FL)
226 ip->flags |= BTRFS_INODE_SYNC;
227 else
228 ip->flags &= ~BTRFS_INODE_SYNC;
229 if (flags & FS_IMMUTABLE_FL)
230 ip->flags |= BTRFS_INODE_IMMUTABLE;
231 else
232 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
233 if (flags & FS_APPEND_FL)
234 ip->flags |= BTRFS_INODE_APPEND;
235 else
236 ip->flags &= ~BTRFS_INODE_APPEND;
237 if (flags & FS_NODUMP_FL)
238 ip->flags |= BTRFS_INODE_NODUMP;
239 else
240 ip->flags &= ~BTRFS_INODE_NODUMP;
241 if (flags & FS_NOATIME_FL)
242 ip->flags |= BTRFS_INODE_NOATIME;
243 else
244 ip->flags &= ~BTRFS_INODE_NOATIME;
245 if (flags & FS_DIRSYNC_FL)
246 ip->flags |= BTRFS_INODE_DIRSYNC;
247 else
248 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600249 if (flags & FS_NOCOW_FL) {
250 if (S_ISREG(mode)) {
251 /*
252 * It's safe to turn csums off here, no extents exist.
253 * Otherwise we want the flag to reflect the real COW
254 * status of the file and will not set it.
255 */
256 if (inode->i_size == 0)
257 ip->flags |= BTRFS_INODE_NODATACOW
258 | BTRFS_INODE_NODATASUM;
259 } else {
260 ip->flags |= BTRFS_INODE_NODATACOW;
261 }
262 } else {
263 /*
264 * Revert back under same assuptions as above
265 */
266 if (S_ISREG(mode)) {
267 if (inode->i_size == 0)
268 ip->flags &= ~(BTRFS_INODE_NODATACOW
269 | BTRFS_INODE_NODATASUM);
270 } else {
271 ip->flags &= ~BTRFS_INODE_NODATACOW;
272 }
273 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200274
Liu Bo75e7cb72011-03-22 10:12:20 +0000275 /*
276 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
277 * flag may be changed automatically if compression code won't make
278 * things smaller.
279 */
280 if (flags & FS_NOCOMP_FL) {
281 ip->flags &= ~BTRFS_INODE_COMPRESS;
282 ip->flags |= BTRFS_INODE_NOCOMPRESS;
283 } else if (flags & FS_COMPR_FL) {
284 ip->flags |= BTRFS_INODE_COMPRESS;
285 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000286 } else {
287 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000288 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200289
Li Zefan4da6f1a2011-12-29 13:39:50 +0800290 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800291 if (IS_ERR(trans)) {
292 ret = PTR_ERR(trans);
293 goto out_drop;
294 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200295
Li Zefan306424cc2011-12-14 20:12:02 -0500296 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400297 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500298 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200299 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200300
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800302 out_drop:
303 if (ret) {
304 ip->flags = ip_oldflags;
305 inode->i_flags = i_oldflags;
306 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200307
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200308 out_unlock:
309 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200310 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000311 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200312}
313
314static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
315{
Al Viro496ad9a2013-01-23 17:07:38 -0500316 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200317
318 return put_user(inode->i_generation, arg);
319}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400320
Li Dongyangf7039b12011-03-24 10:24:28 +0000321static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
322{
Al Viro815745c2011-11-17 15:40:49 -0500323 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000324 struct btrfs_device *device;
325 struct request_queue *q;
326 struct fstrim_range range;
327 u64 minlen = ULLONG_MAX;
328 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500329 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000330 int ret;
331
332 if (!capable(CAP_SYS_ADMIN))
333 return -EPERM;
334
Xiao Guangrong1f781602011-04-20 10:09:16 +0000335 rcu_read_lock();
336 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
337 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000338 if (!device->bdev)
339 continue;
340 q = bdev_get_queue(device->bdev);
341 if (blk_queue_discard(q)) {
342 num_devices++;
343 minlen = min((u64)q->limits.discard_granularity,
344 minlen);
345 }
346 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000347 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200348
Li Dongyangf7039b12011-03-24 10:24:28 +0000349 if (!num_devices)
350 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000351 if (copy_from_user(&range, arg, sizeof(range)))
352 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000353 if (range.start > total_bytes ||
354 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200355 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000356
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200357 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000358 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500359 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000360 if (ret < 0)
361 return ret;
362
363 if (copy_to_user(arg, &range, sizeof(range)))
364 return -EFAULT;
365
366 return 0;
367}
368
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200369int btrfs_is_empty_uuid(u8 *uuid)
370{
Dulshani Gunawardhanab19e6842013-10-31 10:29:38 +0530371 BUILD_BUG_ON(BTRFS_UUID_SIZE > PAGE_SIZE);
372 return !memcmp(uuid, empty_zero_page, BTRFS_UUID_SIZE);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200373}
374
Miao Xied5c12072013-02-28 10:04:33 +0000375static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400376 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400377 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200378 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000379 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380{
381 struct btrfs_trans_handle *trans;
382 struct btrfs_key key;
383 struct btrfs_root_item root_item;
384 struct btrfs_inode_item *inode_item;
385 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000386 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400387 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000388 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200389 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400390 int ret;
391 int err;
392 u64 objectid;
393 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500394 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000395 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200396 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397
Li Zefan581bb052011-04-20 10:06:11 +0800398 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400399 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400400 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000401
Miao Xied5c12072013-02-28 10:04:33 +0000402 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400403 /*
Miao Xied5c12072013-02-28 10:04:33 +0000404 * The same as the snapshot creation, please see the comment
405 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400406 */
Miao Xied5c12072013-02-28 10:04:33 +0000407 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200408 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000409 if (ret)
410 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400411
Miao Xied5c12072013-02-28 10:04:33 +0000412 trans = btrfs_start_transaction(root, 0);
413 if (IS_ERR(trans)) {
414 ret = PTR_ERR(trans);
415 goto out;
416 }
417 trans->block_rsv = &block_rsv;
418 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400419
Miao Xie8696c532013-02-07 06:02:44 +0000420 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200421 if (ret)
422 goto fail;
423
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400424 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200425 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400426 if (IS_ERR(leaf)) {
427 ret = PTR_ERR(leaf);
428 goto fail;
429 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400430
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400431 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400432 btrfs_set_header_bytenr(leaf, leaf->start);
433 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400434 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400435 btrfs_set_header_owner(leaf, objectid);
436
Ross Kirk0a4e5582013-09-24 10:12:38 +0100437 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400438 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400439 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200440 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400441 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442 btrfs_mark_buffer_dirty(leaf);
443
Alexander Block8ea05e32012-07-25 17:35:53 +0200444 memset(&root_item, 0, sizeof(root_item));
445
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400446 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800447 btrfs_set_stack_inode_generation(inode_item, 1);
448 btrfs_set_stack_inode_size(inode_item, 3);
449 btrfs_set_stack_inode_nlink(inode_item, 1);
450 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
451 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400452
Qu Wenruo3cae2102013-07-16 11:19:18 +0800453 btrfs_set_root_flags(&root_item, 0);
454 btrfs_set_root_limit(&root_item, 0);
455 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000456
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400458 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400459 btrfs_set_root_level(&root_item, 0);
460 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000461 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400462 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400463
Alexander Block8ea05e32012-07-25 17:35:53 +0200464 btrfs_set_root_generation_v2(&root_item,
465 btrfs_root_generation(&root_item));
466 uuid_le_gen(&new_uuid);
467 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800468 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
469 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200470 root_item.ctime = root_item.otime;
471 btrfs_set_root_ctransid(&root_item, trans->transid);
472 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400473
Chris Mason925baed2008-06-25 16:01:30 -0400474 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400475 free_extent_buffer(leaf);
476 leaf = NULL;
477
478 btrfs_set_root_dirid(&root_item, new_dirid);
479
480 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400481 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400482 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
483 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
484 &root_item);
485 if (ret)
486 goto fail;
487
Yan, Zheng76dda932009-09-21 16:00:26 -0400488 key.offset = (u64)-1;
489 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100490 if (IS_ERR(new_root)) {
491 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
492 ret = PTR_ERR(new_root);
493 goto fail;
494 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400495
496 btrfs_record_root_in_trans(trans, new_root);
497
Josef Bacikd82a6f12011-05-11 15:26:06 -0400498 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700499 if (ret) {
500 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100501 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700502 goto fail;
503 }
504
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400505 /*
506 * insert the directory item
507 */
Chris Mason3de45862008-11-17 21:02:50 -0500508 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100509 if (ret) {
510 btrfs_abort_transaction(trans, root, ret);
511 goto fail;
512 }
Chris Mason3de45862008-11-17 21:02:50 -0500513
514 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800515 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500516 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100517 if (ret) {
518 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100520 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500521
Yan Zheng52c26172009-01-05 15:43:43 -0500522 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
523 ret = btrfs_update_inode(trans, root, dir);
524 BUG_ON(ret);
525
Chris Mason0660b5a2008-11-17 20:37:39 -0500526 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400527 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800528 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500529 BUG_ON(ret);
530
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200531 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
532 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
533 objectid);
534 if (ret)
535 btrfs_abort_transaction(trans, root, ret);
536
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400537fail:
Miao Xied5c12072013-02-28 10:04:33 +0000538 trans->block_rsv = NULL;
539 trans->bytes_reserved = 0;
Sage Weil72fd0322010-10-29 15:41:32 -0400540 if (async_transid) {
541 *async_transid = trans->transid;
542 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000543 if (err)
544 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400545 } else {
546 err = btrfs_commit_transaction(trans, root);
547 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400548 if (err && !ret)
549 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500550
551 if (!ret)
552 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Miao Xied5c12072013-02-28 10:04:33 +0000553out:
554 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555 return ret;
556}
557
Miao Xiee9662f72013-02-28 10:01:15 +0000558static int create_snapshot(struct btrfs_root *root, struct inode *dir,
559 struct dentry *dentry, char *name, int namelen,
560 u64 *async_transid, bool readonly,
561 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400562{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000563 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400564 struct btrfs_pending_snapshot *pending_snapshot;
565 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000566 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567
568 if (!root->ref_cows)
569 return -EINVAL;
570
Miao Xie6a038432013-05-15 07:48:24 +0000571 ret = btrfs_start_delalloc_inodes(root, 0);
572 if (ret)
573 return ret;
574
Josef Bacikf0de1812013-09-17 10:55:51 -0400575 btrfs_wait_ordered_extents(root);
Miao Xie6a038432013-05-15 07:48:24 +0000576
Yan, Zhenga22285a2010-05-16 10:48:46 -0400577 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
578 if (!pending_snapshot)
579 return -ENOMEM;
580
Miao Xie66d8f3d2012-09-06 04:02:28 -0600581 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
582 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000583 /*
584 * 1 - parent dir inode
585 * 2 - dir entries
586 * 1 - root item
587 * 2 - root ref/backref
588 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200589 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000590 */
591 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200592 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400593 &pending_snapshot->qgroup_reserved,
594 false);
Miao Xied5c12072013-02-28 10:04:33 +0000595 if (ret)
596 goto out;
597
Yan, Zhenga22285a2010-05-16 10:48:46 -0400598 pending_snapshot->dentry = dentry;
599 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800600 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000601 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000602 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400603
Miao Xied5c12072013-02-28 10:04:33 +0000604 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400605 if (IS_ERR(trans)) {
606 ret = PTR_ERR(trans);
607 goto fail;
608 }
609
Josef Bacik83515832011-06-14 15:16:14 -0400610 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400611 list_add(&pending_snapshot->list,
612 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400613 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400614 if (async_transid) {
615 *async_transid = trans->transid;
616 ret = btrfs_commit_transaction_async(trans,
617 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000618 if (ret)
619 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400620 } else {
621 ret = btrfs_commit_transaction(trans,
622 root->fs_info->extent_root);
623 }
Miao Xieaec80302013-03-04 09:44:29 +0000624 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400625 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400626
627 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400628 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000629 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500631 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
632 if (ret)
633 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634
Al Viro2fbe8c82011-07-16 21:38:06 -0400635 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000636 if (IS_ERR(inode)) {
637 ret = PTR_ERR(inode);
638 goto fail;
639 }
640 BUG_ON(!inode);
641 d_instantiate(dentry, inode);
642 ret = 0;
643fail:
Miao Xied5c12072013-02-28 10:04:33 +0000644 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
645 &pending_snapshot->block_rsv,
646 pending_snapshot->qgroup_reserved);
647out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400648 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400649 return ret;
650}
651
Sage Weil4260f7c2010-10-29 15:46:43 -0400652/* copy of check_sticky in fs/namei.c()
653* It's inline, so penalty for filesystems that don't use sticky bit is
654* minimal.
655*/
656static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
657{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800658 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400659
660 if (!(dir->i_mode & S_ISVTX))
661 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800662 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400663 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800664 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400665 return 0;
666 return !capable(CAP_FOWNER);
667}
668
669/* copy of may_delete in fs/namei.c()
670 * Check whether we can remove a link victim from directory dir, check
671 * whether the type of victim is right.
672 * 1. We can't do it if dir is read-only (done in permission())
673 * 2. We should have write and exec permissions on dir
674 * 3. We can't remove anything from append-only dir
675 * 4. We can't do anything with immutable dir (done in permission())
676 * 5. If the sticky bit on dir is set we should either
677 * a. be owner of dir, or
678 * b. be owner of victim, or
679 * c. have CAP_FOWNER capability
680 * 6. If the victim is append-only or immutable we can't do antyhing with
681 * links pointing to it.
682 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
683 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
684 * 9. We can't remove a root or mountpoint.
685 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
686 * nfs_async_unlink().
687 */
688
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530689static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400690{
691 int error;
692
693 if (!victim->d_inode)
694 return -ENOENT;
695
696 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400697 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400698
699 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
700 if (error)
701 return error;
702 if (IS_APPEND(dir))
703 return -EPERM;
704 if (btrfs_check_sticky(dir, victim->d_inode)||
705 IS_APPEND(victim->d_inode)||
706 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
707 return -EPERM;
708 if (isdir) {
709 if (!S_ISDIR(victim->d_inode->i_mode))
710 return -ENOTDIR;
711 if (IS_ROOT(victim))
712 return -EBUSY;
713 } else if (S_ISDIR(victim->d_inode->i_mode))
714 return -EISDIR;
715 if (IS_DEADDIR(dir))
716 return -ENOENT;
717 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
718 return -EBUSY;
719 return 0;
720}
721
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400722/* copy of may_create in fs/namei.c() */
723static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
724{
725 if (child->d_inode)
726 return -EEXIST;
727 if (IS_DEADDIR(dir))
728 return -ENOENT;
729 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
730}
731
732/*
733 * Create a new subvolume below @parent. This is largely modeled after
734 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
735 * inside this filesystem so it's quite a bit simpler.
736 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400737static noinline int btrfs_mksubvol(struct path *parent,
738 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400739 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200740 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000741 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400742{
Yan, Zheng76dda932009-09-21 16:00:26 -0400743 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400744 struct dentry *dentry;
745 int error;
746
David Sterba5c50c9b2013-03-22 18:12:51 +0000747 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
748 if (error == -EINTR)
749 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400750
751 dentry = lookup_one_len(name, parent->dentry, namelen);
752 error = PTR_ERR(dentry);
753 if (IS_ERR(dentry))
754 goto out_unlock;
755
756 error = -EEXIST;
757 if (dentry->d_inode)
758 goto out_dput;
759
Yan, Zheng76dda932009-09-21 16:00:26 -0400760 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400761 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600762 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400763
Chris Mason9c520572012-12-17 14:26:57 -0500764 /*
765 * even if this name doesn't exist, we may get hash collisions.
766 * check for them now when we can safely fail
767 */
768 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
769 dir->i_ino, name,
770 namelen);
771 if (error)
772 goto out_dput;
773
Yan, Zheng76dda932009-09-21 16:00:26 -0400774 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
775
776 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
777 goto out_up_read;
778
Chris Mason3de45862008-11-17 21:02:50 -0500779 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000780 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200781 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500782 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000783 error = create_subvol(dir, dentry, name, namelen,
784 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500785 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400786 if (!error)
787 fsnotify_mkdir(dir, dentry);
788out_up_read:
789 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400790out_dput:
791 dput(dentry);
792out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400793 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400794 return error;
795}
796
Chris Mason4cb53002011-05-24 15:35:30 -0400797/*
798 * When we're defragging a range, we don't want to kick it off again
799 * if it is really just waiting for delalloc to send it down.
800 * If we find a nice big extent or delalloc range for the bytes in the
801 * file you want to defrag, we return 0 to let you know to skip this
802 * part of the file
803 */
804static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
805{
806 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
807 struct extent_map *em = NULL;
808 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
809 u64 end;
810
811 read_lock(&em_tree->lock);
812 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
813 read_unlock(&em_tree->lock);
814
815 if (em) {
816 end = extent_map_end(em);
817 free_extent_map(em);
818 if (end - offset > thresh)
819 return 0;
820 }
821 /* if we already have a nice delalloc here, just stop */
822 thresh /= 2;
823 end = count_range_bits(io_tree, &offset, offset + thresh,
824 thresh, EXTENT_DELALLOC, 1);
825 if (end >= thresh)
826 return 0;
827 return 1;
828}
829
830/*
831 * helper function to walk through a file and find extents
832 * newer than a specific transid, and smaller than thresh.
833 *
834 * This is used by the defragging code to find new and small
835 * extents
836 */
837static int find_new_extents(struct btrfs_root *root,
838 struct inode *inode, u64 newer_than,
839 u64 *off, int thresh)
840{
841 struct btrfs_path *path;
842 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400843 struct extent_buffer *leaf;
844 struct btrfs_file_extent_item *extent;
845 int type;
846 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000847 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400848
849 path = btrfs_alloc_path();
850 if (!path)
851 return -ENOMEM;
852
David Sterbaa4689d22011-05-31 17:08:14 +0000853 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400854 min_key.type = BTRFS_EXTENT_DATA_KEY;
855 min_key.offset = *off;
856
Chris Mason4cb53002011-05-24 15:35:30 -0400857 path->keep_locks = 1;
858
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530859 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100860 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400861 if (ret != 0)
862 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000863 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400864 goto none;
865 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
866 goto none;
867
868 leaf = path->nodes[0];
869 extent = btrfs_item_ptr(leaf, path->slots[0],
870 struct btrfs_file_extent_item);
871
872 type = btrfs_file_extent_type(leaf, extent);
873 if (type == BTRFS_FILE_EXTENT_REG &&
874 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
875 check_defrag_in_cache(inode, min_key.offset, thresh)) {
876 *off = min_key.offset;
877 btrfs_free_path(path);
878 return 0;
879 }
880
881 if (min_key.offset == (u64)-1)
882 goto none;
883
884 min_key.offset++;
885 btrfs_release_path(path);
886 }
887none:
888 btrfs_free_path(path);
889 return -ENOENT;
890}
891
Li Zefan6c282eb2012-06-11 16:03:35 +0800892static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400893{
894 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500895 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800896 struct extent_map *em;
897 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500898
899 /*
900 * hopefully we have this extent in the tree already, try without
901 * the full extent lock
902 */
903 read_lock(&em_tree->lock);
904 em = lookup_extent_mapping(em_tree, start, len);
905 read_unlock(&em_tree->lock);
906
907 if (!em) {
908 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100909 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500910 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100911 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500912
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000913 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800914 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500915 }
916
Li Zefan6c282eb2012-06-11 16:03:35 +0800917 return em;
918}
919
920static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
921{
922 struct extent_map *next;
923 bool ret = true;
924
925 /* this is the last extent */
926 if (em->start + em->len >= i_size_read(inode))
927 return false;
928
929 next = defrag_lookup_extent(inode, em->start + em->len);
930 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
931 ret = false;
932
933 free_extent_map(next);
934 return ret;
935}
936
937static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400938 u64 *last_len, u64 *skip, u64 *defrag_end,
939 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800940{
941 struct extent_map *em;
942 int ret = 1;
943 bool next_mergeable = true;
944
945 /*
946 * make sure that once we start defragging an extent, we keep on
947 * defragging it
948 */
949 if (start < *defrag_end)
950 return 1;
951
952 *skip = 0;
953
954 em = defrag_lookup_extent(inode, start);
955 if (!em)
956 return 0;
957
Chris Mason940100a2010-03-10 10:52:59 -0500958 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400959 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500960 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400961 goto out;
962 }
963
Li Zefan6c282eb2012-06-11 16:03:35 +0800964 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500965
966 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800967 * we hit a real extent, if it is big or the next extent is not a
968 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500969 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400970 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800971 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500972 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400973out:
Chris Mason940100a2010-03-10 10:52:59 -0500974 /*
975 * last_len ends up being a counter of how many bytes we've defragged.
976 * every time we choose not to defrag an extent, we reset *last_len
977 * so that the next tiny extent will force a defrag.
978 *
979 * The end result of this is that tiny extents before a single big
980 * extent will force at least part of that big extent to be defragged.
981 */
982 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500983 *defrag_end = extent_map_end(em);
984 } else {
985 *last_len = 0;
986 *skip = extent_map_end(em);
987 *defrag_end = 0;
988 }
989
990 free_extent_map(em);
991 return ret;
992}
993
Chris Mason4cb53002011-05-24 15:35:30 -0400994/*
995 * it doesn't do much good to defrag one or two pages
996 * at a time. This pulls in a nice chunk of pages
997 * to COW and defrag.
998 *
999 * It also makes sure the delalloc code has enough
1000 * dirty data to avoid making new small extents as part
1001 * of the defrag
1002 *
1003 * It's a good idea to start RA on this range
1004 * before calling this.
1005 */
1006static int cluster_pages_for_defrag(struct inode *inode,
1007 struct page **pages,
1008 unsigned long start_index,
1009 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001010{
Chris Mason4cb53002011-05-24 15:35:30 -04001011 unsigned long file_end;
1012 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001013 u64 page_start;
1014 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001015 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001016 int ret;
1017 int i;
1018 int i_done;
1019 struct btrfs_ordered_extent *ordered;
1020 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001021 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001022 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001023
Chris Mason4cb53002011-05-24 15:35:30 -04001024 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001025 if (!isize || start_index > file_end)
1026 return 0;
1027
1028 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001029
1030 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001031 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001032 if (ret)
1033 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001034 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001035 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001036
1037 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001038 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001039 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001040again:
Josef Bacika94733d2011-07-11 10:47:06 -04001041 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001042 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001043 if (!page)
1044 break;
1045
Miao Xie600a45e2012-02-16 15:01:24 +08001046 page_start = page_offset(page);
1047 page_end = page_start + PAGE_CACHE_SIZE - 1;
1048 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001049 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001050 ordered = btrfs_lookup_ordered_extent(inode,
1051 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001052 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001053 if (!ordered)
1054 break;
1055
1056 unlock_page(page);
1057 btrfs_start_ordered_extent(inode, ordered, 1);
1058 btrfs_put_ordered_extent(ordered);
1059 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001060 /*
1061 * we unlocked the page above, so we need check if
1062 * it was released or not.
1063 */
1064 if (page->mapping != inode->i_mapping) {
1065 unlock_page(page);
1066 page_cache_release(page);
1067 goto again;
1068 }
Miao Xie600a45e2012-02-16 15:01:24 +08001069 }
1070
Chris Mason4cb53002011-05-24 15:35:30 -04001071 if (!PageUptodate(page)) {
1072 btrfs_readpage(NULL, page);
1073 lock_page(page);
1074 if (!PageUptodate(page)) {
1075 unlock_page(page);
1076 page_cache_release(page);
1077 ret = -EIO;
1078 break;
1079 }
1080 }
Miao Xie600a45e2012-02-16 15:01:24 +08001081
Miao Xie600a45e2012-02-16 15:01:24 +08001082 if (page->mapping != inode->i_mapping) {
1083 unlock_page(page);
1084 page_cache_release(page);
1085 goto again;
1086 }
1087
Chris Mason4cb53002011-05-24 15:35:30 -04001088 pages[i] = page;
1089 i_done++;
1090 }
1091 if (!i_done || ret)
1092 goto out;
1093
1094 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1095 goto out;
1096
1097 /*
1098 * so now we have a nice long stream of locked
1099 * and up to date pages, lets wait on them
1100 */
1101 for (i = 0; i < i_done; i++)
1102 wait_on_page_writeback(pages[i]);
1103
1104 page_start = page_offset(pages[0]);
1105 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1106
1107 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001108 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001109 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1110 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001111 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1112 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001113
Liu Bo1f12bd02012-03-29 09:57:44 -04001114 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001115 spin_lock(&BTRFS_I(inode)->lock);
1116 BTRFS_I(inode)->outstanding_extents++;
1117 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001118 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001119 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001120 }
1121
1122
Liu Bo9e8a4a82012-09-05 19:10:51 -06001123 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1124 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001125
1126 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1127 page_start, page_end - 1, &cached_state,
1128 GFP_NOFS);
1129
1130 for (i = 0; i < i_done; i++) {
1131 clear_page_dirty_for_io(pages[i]);
1132 ClearPageChecked(pages[i]);
1133 set_page_extent_mapped(pages[i]);
1134 set_page_dirty(pages[i]);
1135 unlock_page(pages[i]);
1136 page_cache_release(pages[i]);
1137 }
1138 return i_done;
1139out:
1140 for (i = 0; i < i_done; i++) {
1141 unlock_page(pages[i]);
1142 page_cache_release(pages[i]);
1143 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001144 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001145 return ret;
1146
1147}
1148
1149int btrfs_defrag_file(struct inode *inode, struct file *file,
1150 struct btrfs_ioctl_defrag_range_args *range,
1151 u64 newer_than, unsigned long max_to_defrag)
1152{
1153 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001154 struct file_ra_state *ra = NULL;
1155 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001156 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001157 u64 last_len = 0;
1158 u64 skip = 0;
1159 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001160 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001161 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001162 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001163 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001164 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001165 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001166 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001167 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1168 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001169 u64 new_align = ~((u64)128 * 1024 - 1);
1170 struct page **pages = NULL;
1171
Liu Bo0abd5b12013-04-16 09:20:28 +00001172 if (isize == 0)
1173 return 0;
1174
1175 if (range->start >= isize)
1176 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001177
1178 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1179 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1180 return -EINVAL;
1181 if (range->compress_type)
1182 compress_type = range->compress_type;
1183 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001184
Liu Bo0abd5b12013-04-16 09:20:28 +00001185 if (extent_thresh == 0)
1186 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001187
Chris Mason4cb53002011-05-24 15:35:30 -04001188 /*
1189 * if we were not given a file, allocate a readahead
1190 * context
1191 */
1192 if (!file) {
1193 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1194 if (!ra)
1195 return -ENOMEM;
1196 file_ra_state_init(ra, inode->i_mapping);
1197 } else {
1198 ra = &file->f_ra;
1199 }
1200
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301201 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001202 GFP_NOFS);
1203 if (!pages) {
1204 ret = -ENOMEM;
1205 goto out_ra;
1206 }
1207
1208 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001209 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001210 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001211 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1212 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001213 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001214 }
1215
Chris Mason4cb53002011-05-24 15:35:30 -04001216 if (newer_than) {
1217 ret = find_new_extents(root, inode, newer_than,
1218 &newer_off, 64 * 1024);
1219 if (!ret) {
1220 range->start = newer_off;
1221 /*
1222 * we always align our defrag to help keep
1223 * the extents in the file evenly spaced
1224 */
1225 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001226 } else
1227 goto out_ra;
1228 } else {
1229 i = range->start >> PAGE_CACHE_SHIFT;
1230 }
1231 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001232 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001233
Li Zefan2a0f7f52011-10-10 15:43:34 -04001234 /*
1235 * make writeback starts from i, so the defrag range can be
1236 * written sequentially.
1237 */
1238 if (i < inode->i_mapping->writeback_index)
1239 inode->i_mapping->writeback_index = i;
1240
Chris Masonf7f43cc2011-10-11 11:41:40 -04001241 while (i <= last_index && defrag_count < max_to_defrag &&
1242 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1243 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001244 /*
1245 * make sure we stop running if someone unmounts
1246 * the FS
1247 */
1248 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1249 break;
1250
David Sterba210549e2013-02-09 23:38:06 +00001251 if (btrfs_defrag_cancelled(root->fs_info)) {
1252 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1253 ret = -EAGAIN;
1254 break;
1255 }
1256
Liu Bo66c26892012-03-29 09:57:45 -04001257 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001258 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001259 &defrag_end, range->flags &
1260 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001261 unsigned long next;
1262 /*
1263 * the should_defrag function tells us how much to skip
1264 * bump our counter by the suggested amount
1265 */
1266 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1267 i = max(i + 1, next);
1268 continue;
1269 }
Li Zefan008873e2011-09-02 15:57:07 +08001270
1271 if (!newer_than) {
1272 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1273 PAGE_CACHE_SHIFT) - i;
1274 cluster = min(cluster, max_cluster);
1275 } else {
1276 cluster = max_cluster;
1277 }
1278
Li Zefan008873e2011-09-02 15:57:07 +08001279 if (i + cluster > ra_index) {
1280 ra_index = max(i, ra_index);
1281 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1282 cluster);
1283 ra_index += max_cluster;
1284 }
Chris Mason940100a2010-03-10 10:52:59 -05001285
Liu Boecb8bea2012-03-29 09:57:44 -04001286 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001287 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1288 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001289 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001290 if (ret < 0) {
1291 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001292 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001293 }
Chris Mason940100a2010-03-10 10:52:59 -05001294
Chris Mason4cb53002011-05-24 15:35:30 -04001295 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001296 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001297 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001298
1299 if (newer_than) {
1300 if (newer_off == (u64)-1)
1301 break;
1302
Liu Boe1f041e2012-03-29 09:57:45 -04001303 if (ret > 0)
1304 i += ret;
1305
Chris Mason4cb53002011-05-24 15:35:30 -04001306 newer_off = max(newer_off + 1,
1307 (u64)i << PAGE_CACHE_SHIFT);
1308
1309 ret = find_new_extents(root, inode,
1310 newer_than, &newer_off,
1311 64 * 1024);
1312 if (!ret) {
1313 range->start = newer_off;
1314 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001315 } else {
1316 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001317 }
Chris Mason4cb53002011-05-24 15:35:30 -04001318 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001319 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001320 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001321 last_len += ret << PAGE_CACHE_SHIFT;
1322 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001323 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001324 last_len = 0;
1325 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001326 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001327 }
1328
Chris Mason1e701a32010-03-11 09:42:04 -05001329 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1330 filemap_flush(inode->i_mapping);
1331
1332 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1333 /* the filemap_flush will queue IO into the worker threads, but
1334 * we have to make sure the IO is actually started and that
1335 * ordered extents get created before we return
1336 */
1337 atomic_inc(&root->fs_info->async_submit_draining);
1338 while (atomic_read(&root->fs_info->nr_async_submits) ||
1339 atomic_read(&root->fs_info->async_delalloc_pages)) {
1340 wait_event(root->fs_info->async_submit_wait,
1341 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1342 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1343 }
1344 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001345 }
1346
Li Zefan1a419d82010-10-25 15:12:50 +08001347 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001348 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001349 }
1350
Diego Calleja60ccf822011-09-01 16:33:57 +02001351 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001352
Chris Mason4cb53002011-05-24 15:35:30 -04001353out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001354 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1355 mutex_lock(&inode->i_mutex);
1356 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1357 mutex_unlock(&inode->i_mutex);
1358 }
Chris Mason4cb53002011-05-24 15:35:30 -04001359 if (!file)
1360 kfree(ra);
1361 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001362 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001363}
1364
Miao Xie198605a2012-11-26 08:43:45 +00001365static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001366 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001367{
1368 u64 new_size;
1369 u64 old_size;
1370 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001371 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001372 struct btrfs_ioctl_vol_args *vol_args;
1373 struct btrfs_trans_handle *trans;
1374 struct btrfs_device *device = NULL;
1375 char *sizestr;
1376 char *devstr = NULL;
1377 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001378 int mod = 0;
1379
Chris Masone441d542009-01-05 16:57:23 -05001380 if (!capable(CAP_SYS_ADMIN))
1381 return -EPERM;
1382
Miao Xie198605a2012-11-26 08:43:45 +00001383 ret = mnt_want_write_file(file);
1384 if (ret)
1385 return ret;
1386
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001387 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1388 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001389 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001390 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001391 }
1392
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001393 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001394 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001395 if (IS_ERR(vol_args)) {
1396 ret = PTR_ERR(vol_args);
1397 goto out;
1398 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001399
1400 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001401
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001402 sizestr = vol_args->name;
1403 devstr = strchr(sizestr, ':');
1404 if (devstr) {
1405 char *end;
1406 sizestr = devstr + 1;
1407 *devstr = '\0';
1408 devstr = vol_args->name;
1409 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001410 if (!devid) {
1411 ret = -EINVAL;
1412 goto out_free;
1413 }
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001414 printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001415 }
Miao Xiedba60f32012-12-21 09:19:51 +00001416
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001417 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001418 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001419 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001420 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001421 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001422 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001423 }
Miao Xiedba60f32012-12-21 09:19:51 +00001424
1425 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001426 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001427 "readonly device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001428 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001429 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001430 goto out_free;
1431 }
1432
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001433 if (!strcmp(sizestr, "max"))
1434 new_size = device->bdev->bd_inode->i_size;
1435 else {
1436 if (sizestr[0] == '-') {
1437 mod = -1;
1438 sizestr++;
1439 } else if (sizestr[0] == '+') {
1440 mod = 1;
1441 sizestr++;
1442 }
Akinobu Mita91748462010-02-28 10:59:11 +00001443 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001444 if (new_size == 0) {
1445 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001446 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001447 }
1448 }
1449
Stefan Behrens63a212a2012-11-05 18:29:28 +01001450 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001451 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001452 goto out_free;
1453 }
1454
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001455 old_size = device->total_bytes;
1456
1457 if (mod < 0) {
1458 if (new_size > old_size) {
1459 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001460 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001461 }
1462 new_size = old_size - new_size;
1463 } else if (mod > 0) {
1464 new_size = old_size + new_size;
1465 }
1466
1467 if (new_size < 256 * 1024 * 1024) {
1468 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001469 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001470 }
1471 if (new_size > device->bdev->bd_inode->i_size) {
1472 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001473 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001474 }
1475
1476 do_div(new_size, root->sectorsize);
1477 new_size *= root->sectorsize;
1478
Josef Bacik606686e2012-06-04 14:03:51 -04001479 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001480 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001481
1482 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001483 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001484 if (IS_ERR(trans)) {
1485 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001486 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001487 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001488 ret = btrfs_grow_device(trans, device, new_size);
1489 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001490 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001491 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001492 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001493
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001494out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001495 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001496out:
1497 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001498 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001499 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001500 return ret;
1501}
1502
Sage Weil72fd0322010-10-29 15:41:32 -04001503static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001504 char *name, unsigned long fd, int subvol,
1505 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001506 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001507{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001508 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001509 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001510
Liu Boa874a632012-06-29 03:58:46 -06001511 ret = mnt_want_write_file(file);
1512 if (ret)
1513 goto out;
1514
Sage Weil72fd0322010-10-29 15:41:32 -04001515 namelen = strlen(name);
1516 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001517 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001518 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001519 }
1520
Chris Mason16780ca2012-02-20 22:14:55 -05001521 if (name[0] == '.' &&
1522 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1523 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001524 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001525 }
1526
Chris Mason3de45862008-11-17 21:02:50 -05001527 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001528 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001529 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001530 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001531 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001532 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001533 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001534 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001535 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001536 }
1537
Al Viro496ad9a2013-01-23 17:07:38 -05001538 src_inode = file_inode(src.file);
1539 if (src_inode->i_sb != file_inode(file)->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001540 printk(KERN_INFO "btrfs: Snapshot src from "
1541 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001542 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001543 } else {
1544 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1545 BTRFS_I(src_inode)->root,
1546 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001547 }
Al Viro2903ff02012-08-28 12:52:22 -04001548 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001549 }
Liu Boa874a632012-06-29 03:58:46 -06001550out_drop_write:
1551 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001552out:
Sage Weil72fd0322010-10-29 15:41:32 -04001553 return ret;
1554}
1555
1556static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001557 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001558{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001559 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001560 int ret;
1561
Li Zefanfa0d2b92010-12-20 15:53:28 +08001562 vol_args = memdup_user(arg, sizeof(*vol_args));
1563 if (IS_ERR(vol_args))
1564 return PTR_ERR(vol_args);
1565 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001566
Li Zefanfa0d2b92010-12-20 15:53:28 +08001567 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001568 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001569 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001570
Li Zefanfa0d2b92010-12-20 15:53:28 +08001571 kfree(vol_args);
1572 return ret;
1573}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001574
Li Zefanfa0d2b92010-12-20 15:53:28 +08001575static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1576 void __user *arg, int subvol)
1577{
1578 struct btrfs_ioctl_vol_args_v2 *vol_args;
1579 int ret;
1580 u64 transid = 0;
1581 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001582 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001583 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001584
Li Zefanfa0d2b92010-12-20 15:53:28 +08001585 vol_args = memdup_user(arg, sizeof(*vol_args));
1586 if (IS_ERR(vol_args))
1587 return PTR_ERR(vol_args);
1588 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001589
Li Zefanb83cc962010-12-20 16:04:08 +08001590 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001591 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1592 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001593 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001594 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001595 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001596
1597 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1598 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001599 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1600 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001601 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1602 if (vol_args->size > PAGE_CACHE_SIZE) {
1603 ret = -EINVAL;
1604 goto out;
1605 }
1606 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1607 if (IS_ERR(inherit)) {
1608 ret = PTR_ERR(inherit);
1609 goto out;
1610 }
1611 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001612
1613 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001614 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001615 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001616
1617 if (ret == 0 && ptr &&
1618 copy_to_user(arg +
1619 offsetof(struct btrfs_ioctl_vol_args_v2,
1620 transid), ptr, sizeof(*ptr)))
1621 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001622out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001623 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001624 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001625 return ret;
1626}
1627
Li Zefan0caa1022010-12-20 16:30:25 +08001628static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1629 void __user *arg)
1630{
Al Viro496ad9a2013-01-23 17:07:38 -05001631 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001632 struct btrfs_root *root = BTRFS_I(inode)->root;
1633 int ret = 0;
1634 u64 flags = 0;
1635
Li Zefan33345d012011-04-20 10:31:50 +08001636 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001637 return -EINVAL;
1638
1639 down_read(&root->fs_info->subvol_sem);
1640 if (btrfs_root_readonly(root))
1641 flags |= BTRFS_SUBVOL_RDONLY;
1642 up_read(&root->fs_info->subvol_sem);
1643
1644 if (copy_to_user(arg, &flags, sizeof(flags)))
1645 ret = -EFAULT;
1646
1647 return ret;
1648}
1649
1650static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1651 void __user *arg)
1652{
Al Viro496ad9a2013-01-23 17:07:38 -05001653 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001654 struct btrfs_root *root = BTRFS_I(inode)->root;
1655 struct btrfs_trans_handle *trans;
1656 u64 root_flags;
1657 u64 flags;
1658 int ret = 0;
1659
Liu Bob9ca0662012-06-29 03:58:49 -06001660 ret = mnt_want_write_file(file);
1661 if (ret)
1662 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001663
Liu Bob9ca0662012-06-29 03:58:49 -06001664 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1665 ret = -EINVAL;
1666 goto out_drop_write;
1667 }
Li Zefan0caa1022010-12-20 16:30:25 +08001668
Liu Bob9ca0662012-06-29 03:58:49 -06001669 if (copy_from_user(&flags, arg, sizeof(flags))) {
1670 ret = -EFAULT;
1671 goto out_drop_write;
1672 }
Li Zefan0caa1022010-12-20 16:30:25 +08001673
Liu Bob9ca0662012-06-29 03:58:49 -06001674 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1675 ret = -EINVAL;
1676 goto out_drop_write;
1677 }
Li Zefan0caa1022010-12-20 16:30:25 +08001678
Liu Bob9ca0662012-06-29 03:58:49 -06001679 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1680 ret = -EOPNOTSUPP;
1681 goto out_drop_write;
1682 }
Li Zefan0caa1022010-12-20 16:30:25 +08001683
Liu Bob9ca0662012-06-29 03:58:49 -06001684 if (!inode_owner_or_capable(inode)) {
1685 ret = -EACCES;
1686 goto out_drop_write;
1687 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001688
Li Zefan0caa1022010-12-20 16:30:25 +08001689 down_write(&root->fs_info->subvol_sem);
1690
1691 /* nothing to do */
1692 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001693 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001694
1695 root_flags = btrfs_root_flags(&root->root_item);
1696 if (flags & BTRFS_SUBVOL_RDONLY)
1697 btrfs_set_root_flags(&root->root_item,
1698 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1699 else
1700 btrfs_set_root_flags(&root->root_item,
1701 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1702
1703 trans = btrfs_start_transaction(root, 1);
1704 if (IS_ERR(trans)) {
1705 ret = PTR_ERR(trans);
1706 goto out_reset;
1707 }
1708
Li Zefanb4dc2b82011-02-16 06:06:34 +00001709 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001710 &root->root_key, &root->root_item);
1711
1712 btrfs_commit_transaction(trans, root);
1713out_reset:
1714 if (ret)
1715 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001716out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001717 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001718out_drop_write:
1719 mnt_drop_write_file(file);
1720out:
Li Zefan0caa1022010-12-20 16:30:25 +08001721 return ret;
1722}
1723
Yan, Zheng76dda932009-09-21 16:00:26 -04001724/*
1725 * helper to check if the subvolume references other subvolumes
1726 */
1727static noinline int may_destroy_subvol(struct btrfs_root *root)
1728{
1729 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001730 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001731 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001732 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001733 int ret;
1734
1735 path = btrfs_alloc_path();
1736 if (!path)
1737 return -ENOMEM;
1738
Josef Bacik175a2b82013-08-12 15:36:44 -04001739 /* Make sure this root isn't set as the default subvol */
1740 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1741 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1742 dir_id, "default", 7, 0);
1743 if (di && !IS_ERR(di)) {
1744 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1745 if (key.objectid == root->root_key.objectid) {
1746 ret = -ENOTEMPTY;
1747 goto out;
1748 }
1749 btrfs_release_path(path);
1750 }
1751
Yan, Zheng76dda932009-09-21 16:00:26 -04001752 key.objectid = root->root_key.objectid;
1753 key.type = BTRFS_ROOT_REF_KEY;
1754 key.offset = (u64)-1;
1755
1756 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1757 &key, path, 0, 0);
1758 if (ret < 0)
1759 goto out;
1760 BUG_ON(ret == 0);
1761
1762 ret = 0;
1763 if (path->slots[0] > 0) {
1764 path->slots[0]--;
1765 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1766 if (key.objectid == root->root_key.objectid &&
1767 key.type == BTRFS_ROOT_REF_KEY)
1768 ret = -ENOTEMPTY;
1769 }
1770out:
1771 btrfs_free_path(path);
1772 return ret;
1773}
1774
Chris Masonac8e9812010-02-28 15:39:26 -05001775static noinline int key_in_sk(struct btrfs_key *key,
1776 struct btrfs_ioctl_search_key *sk)
1777{
Chris Masonabc6e132010-03-18 12:10:08 -04001778 struct btrfs_key test;
1779 int ret;
1780
1781 test.objectid = sk->min_objectid;
1782 test.type = sk->min_type;
1783 test.offset = sk->min_offset;
1784
1785 ret = btrfs_comp_cpu_keys(key, &test);
1786 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001787 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001788
1789 test.objectid = sk->max_objectid;
1790 test.type = sk->max_type;
1791 test.offset = sk->max_offset;
1792
1793 ret = btrfs_comp_cpu_keys(key, &test);
1794 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001795 return 0;
1796 return 1;
1797}
1798
1799static noinline int copy_to_sk(struct btrfs_root *root,
1800 struct btrfs_path *path,
1801 struct btrfs_key *key,
1802 struct btrfs_ioctl_search_key *sk,
1803 char *buf,
1804 unsigned long *sk_offset,
1805 int *num_found)
1806{
1807 u64 found_transid;
1808 struct extent_buffer *leaf;
1809 struct btrfs_ioctl_search_header sh;
1810 unsigned long item_off;
1811 unsigned long item_len;
1812 int nritems;
1813 int i;
1814 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001815 int ret = 0;
1816
1817 leaf = path->nodes[0];
1818 slot = path->slots[0];
1819 nritems = btrfs_header_nritems(leaf);
1820
1821 if (btrfs_header_generation(leaf) > sk->max_transid) {
1822 i = nritems;
1823 goto advance_key;
1824 }
1825 found_transid = btrfs_header_generation(leaf);
1826
1827 for (i = slot; i < nritems; i++) {
1828 item_off = btrfs_item_ptr_offset(leaf, i);
1829 item_len = btrfs_item_size_nr(leaf, i);
1830
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001831 btrfs_item_key_to_cpu(leaf, key, i);
1832 if (!key_in_sk(key, sk))
1833 continue;
1834
1835 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001836 item_len = 0;
1837
1838 if (sizeof(sh) + item_len + *sk_offset >
1839 BTRFS_SEARCH_ARGS_BUFSIZE) {
1840 ret = 1;
1841 goto overflow;
1842 }
1843
Chris Masonac8e9812010-02-28 15:39:26 -05001844 sh.objectid = key->objectid;
1845 sh.offset = key->offset;
1846 sh.type = key->type;
1847 sh.len = item_len;
1848 sh.transid = found_transid;
1849
1850 /* copy search result header */
1851 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1852 *sk_offset += sizeof(sh);
1853
1854 if (item_len) {
1855 char *p = buf + *sk_offset;
1856 /* copy the item */
1857 read_extent_buffer(leaf, p,
1858 item_off, item_len);
1859 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001860 }
Hugo Millse2156862011-05-14 17:43:41 +00001861 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001862
1863 if (*num_found >= sk->nr_items)
1864 break;
1865 }
1866advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001867 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001868 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1869 key->offset++;
1870 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1871 key->offset = 0;
1872 key->type++;
1873 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1874 key->offset = 0;
1875 key->type = 0;
1876 key->objectid++;
1877 } else
1878 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001879overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001880 return ret;
1881}
1882
1883static noinline int search_ioctl(struct inode *inode,
1884 struct btrfs_ioctl_search_args *args)
1885{
1886 struct btrfs_root *root;
1887 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001888 struct btrfs_path *path;
1889 struct btrfs_ioctl_search_key *sk = &args->key;
1890 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1891 int ret;
1892 int num_found = 0;
1893 unsigned long sk_offset = 0;
1894
1895 path = btrfs_alloc_path();
1896 if (!path)
1897 return -ENOMEM;
1898
1899 if (sk->tree_id == 0) {
1900 /* search the root of the inode that was passed */
1901 root = BTRFS_I(inode)->root;
1902 } else {
1903 key.objectid = sk->tree_id;
1904 key.type = BTRFS_ROOT_ITEM_KEY;
1905 key.offset = (u64)-1;
1906 root = btrfs_read_fs_root_no_name(info, &key);
1907 if (IS_ERR(root)) {
1908 printk(KERN_ERR "could not find root %llu\n",
1909 sk->tree_id);
1910 btrfs_free_path(path);
1911 return -ENOENT;
1912 }
1913 }
1914
1915 key.objectid = sk->min_objectid;
1916 key.type = sk->min_type;
1917 key.offset = sk->min_offset;
1918
Chris Masonac8e9812010-02-28 15:39:26 -05001919 path->keep_locks = 1;
1920
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301921 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001922 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05001923 if (ret != 0) {
1924 if (ret > 0)
1925 ret = 0;
1926 goto err;
1927 }
1928 ret = copy_to_sk(root, path, &key, sk, args->buf,
1929 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001930 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001931 if (ret || num_found >= sk->nr_items)
1932 break;
1933
1934 }
1935 ret = 0;
1936err:
1937 sk->nr_items = num_found;
1938 btrfs_free_path(path);
1939 return ret;
1940}
1941
1942static noinline int btrfs_ioctl_tree_search(struct file *file,
1943 void __user *argp)
1944{
1945 struct btrfs_ioctl_search_args *args;
1946 struct inode *inode;
1947 int ret;
1948
1949 if (!capable(CAP_SYS_ADMIN))
1950 return -EPERM;
1951
Julia Lawall2354d082010-10-29 15:14:18 -04001952 args = memdup_user(argp, sizeof(*args));
1953 if (IS_ERR(args))
1954 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001955
Al Viro496ad9a2013-01-23 17:07:38 -05001956 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001957 ret = search_ioctl(inode, args);
1958 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1959 ret = -EFAULT;
1960 kfree(args);
1961 return ret;
1962}
1963
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001964/*
Chris Masonac8e9812010-02-28 15:39:26 -05001965 * Search INODE_REFs to identify path name of 'dirid' directory
1966 * in a 'tree_id' tree. and sets path name to 'name'.
1967 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001968static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1969 u64 tree_id, u64 dirid, char *name)
1970{
1971 struct btrfs_root *root;
1972 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001973 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001974 int ret = -1;
1975 int slot;
1976 int len;
1977 int total_len = 0;
1978 struct btrfs_inode_ref *iref;
1979 struct extent_buffer *l;
1980 struct btrfs_path *path;
1981
1982 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1983 name[0]='\0';
1984 return 0;
1985 }
1986
1987 path = btrfs_alloc_path();
1988 if (!path)
1989 return -ENOMEM;
1990
Chris Masonac8e9812010-02-28 15:39:26 -05001991 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001992
1993 key.objectid = tree_id;
1994 key.type = BTRFS_ROOT_ITEM_KEY;
1995 key.offset = (u64)-1;
1996 root = btrfs_read_fs_root_no_name(info, &key);
1997 if (IS_ERR(root)) {
1998 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001999 ret = -ENOENT;
2000 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002001 }
2002
2003 key.objectid = dirid;
2004 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002005 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002006
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302007 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002008 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2009 if (ret < 0)
2010 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002011 else if (ret > 0) {
2012 ret = btrfs_previous_item(root, path, dirid,
2013 BTRFS_INODE_REF_KEY);
2014 if (ret < 0)
2015 goto out;
2016 else if (ret > 0) {
2017 ret = -ENOENT;
2018 goto out;
2019 }
2020 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002021
2022 l = path->nodes[0];
2023 slot = path->slots[0];
2024 btrfs_item_key_to_cpu(l, &key, slot);
2025
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002026 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2027 len = btrfs_inode_ref_name_len(l, iref);
2028 ptr -= len + 1;
2029 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002030 if (ptr < name) {
2031 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002032 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002033 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002034
2035 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302036 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002037
2038 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2039 break;
2040
David Sterbab3b4aa72011-04-21 01:20:15 +02002041 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002042 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002043 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002044 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002045 }
Li Zefan77906a502011-07-14 03:16:00 +00002046 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302047 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002048 ret = 0;
2049out:
2050 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002051 return ret;
2052}
2053
2054static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2055 void __user *argp)
2056{
2057 struct btrfs_ioctl_ino_lookup_args *args;
2058 struct inode *inode;
2059 int ret;
2060
2061 if (!capable(CAP_SYS_ADMIN))
2062 return -EPERM;
2063
Julia Lawall2354d082010-10-29 15:14:18 -04002064 args = memdup_user(argp, sizeof(*args));
2065 if (IS_ERR(args))
2066 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002067
Al Viro496ad9a2013-01-23 17:07:38 -05002068 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002069
Chris Mason1b53ac42010-03-18 12:17:05 -04002070 if (args->treeid == 0)
2071 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2072
Chris Masonac8e9812010-02-28 15:39:26 -05002073 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2074 args->treeid, args->objectid,
2075 args->name);
2076
2077 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2078 ret = -EFAULT;
2079
2080 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002081 return ret;
2082}
2083
Yan, Zheng76dda932009-09-21 16:00:26 -04002084static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2085 void __user *arg)
2086{
2087 struct dentry *parent = fdentry(file);
2088 struct dentry *dentry;
2089 struct inode *dir = parent->d_inode;
2090 struct inode *inode;
2091 struct btrfs_root *root = BTRFS_I(dir)->root;
2092 struct btrfs_root *dest = NULL;
2093 struct btrfs_ioctl_vol_args *vol_args;
2094 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002095 struct btrfs_block_rsv block_rsv;
2096 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002097 int namelen;
2098 int ret;
2099 int err = 0;
2100
Yan, Zheng76dda932009-09-21 16:00:26 -04002101 vol_args = memdup_user(arg, sizeof(*vol_args));
2102 if (IS_ERR(vol_args))
2103 return PTR_ERR(vol_args);
2104
2105 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2106 namelen = strlen(vol_args->name);
2107 if (strchr(vol_args->name, '/') ||
2108 strncmp(vol_args->name, "..", namelen) == 0) {
2109 err = -EINVAL;
2110 goto out;
2111 }
2112
Al Viroa561be72011-11-23 11:57:51 -05002113 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002114 if (err)
2115 goto out;
2116
David Sterba5c50c9b2013-03-22 18:12:51 +00002117 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2118 if (err == -EINTR)
2119 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002120 dentry = lookup_one_len(vol_args->name, parent, namelen);
2121 if (IS_ERR(dentry)) {
2122 err = PTR_ERR(dentry);
2123 goto out_unlock_dir;
2124 }
2125
2126 if (!dentry->d_inode) {
2127 err = -ENOENT;
2128 goto out_dput;
2129 }
2130
2131 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002132 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302133 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002134 /*
2135 * Regular user. Only allow this with a special mount
2136 * option, when the user has write+exec access to the
2137 * subvol root, and when rmdir(2) would have been
2138 * allowed.
2139 *
2140 * Note that this is _not_ check that the subvol is
2141 * empty or doesn't contain data that we wouldn't
2142 * otherwise be able to delete.
2143 *
2144 * Users who want to delete empty subvols should try
2145 * rmdir(2).
2146 */
2147 err = -EPERM;
2148 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2149 goto out_dput;
2150
2151 /*
2152 * Do not allow deletion if the parent dir is the same
2153 * as the dir to be deleted. That means the ioctl
2154 * must be called on the dentry referencing the root
2155 * of the subvol, not a random directory contained
2156 * within it.
2157 */
2158 err = -EINVAL;
2159 if (root == dest)
2160 goto out_dput;
2161
2162 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2163 if (err)
2164 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002165 }
2166
Miao Xie5c39da52012-10-22 11:39:53 +00002167 /* check if subvolume may be deleted by a user */
2168 err = btrfs_may_delete(dir, dentry, 1);
2169 if (err)
2170 goto out_dput;
2171
Li Zefan33345d012011-04-20 10:31:50 +08002172 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002173 err = -EINVAL;
2174 goto out_dput;
2175 }
2176
Yan, Zheng76dda932009-09-21 16:00:26 -04002177 mutex_lock(&inode->i_mutex);
2178 err = d_invalidate(dentry);
2179 if (err)
2180 goto out_unlock;
2181
2182 down_write(&root->fs_info->subvol_sem);
2183
2184 err = may_destroy_subvol(dest);
2185 if (err)
2186 goto out_up_write;
2187
Miao Xiec58aaad2013-02-28 10:05:36 +00002188 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2189 /*
2190 * One for dir inode, two for dir entries, two for root
2191 * ref/backref.
2192 */
2193 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002194 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002195 if (err)
2196 goto out_up_write;
2197
Yan, Zhenga22285a2010-05-16 10:48:46 -04002198 trans = btrfs_start_transaction(root, 0);
2199 if (IS_ERR(trans)) {
2200 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002201 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002202 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002203 trans->block_rsv = &block_rsv;
2204 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002205
Yan, Zheng76dda932009-09-21 16:00:26 -04002206 ret = btrfs_unlink_subvol(trans, root, dir,
2207 dest->root_key.objectid,
2208 dentry->d_name.name,
2209 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002210 if (ret) {
2211 err = ret;
2212 btrfs_abort_transaction(trans, root, ret);
2213 goto out_end_trans;
2214 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002215
2216 btrfs_record_root_in_trans(trans, dest);
2217
2218 memset(&dest->root_item.drop_progress, 0,
2219 sizeof(dest->root_item.drop_progress));
2220 dest->root_item.drop_level = 0;
2221 btrfs_set_root_refs(&dest->root_item, 0);
2222
Yan, Zhengd68fc572010-05-16 10:49:58 -04002223 if (!xchg(&dest->orphan_item_inserted, 1)) {
2224 ret = btrfs_insert_orphan_item(trans,
2225 root->fs_info->tree_root,
2226 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002227 if (ret) {
2228 btrfs_abort_transaction(trans, root, ret);
2229 err = ret;
2230 goto out_end_trans;
2231 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002232 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002233
2234 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2235 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2236 dest->root_key.objectid);
2237 if (ret && ret != -ENOENT) {
2238 btrfs_abort_transaction(trans, root, ret);
2239 err = ret;
2240 goto out_end_trans;
2241 }
2242 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2243 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2244 dest->root_item.received_uuid,
2245 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2246 dest->root_key.objectid);
2247 if (ret && ret != -ENOENT) {
2248 btrfs_abort_transaction(trans, root, ret);
2249 err = ret;
2250 goto out_end_trans;
2251 }
2252 }
2253
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002254out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002255 trans->block_rsv = NULL;
2256 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002257 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002258 if (ret && !err)
2259 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002260 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002261out_release:
2262 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002263out_up_write:
2264 up_write(&root->fs_info->subvol_sem);
2265out_unlock:
2266 mutex_unlock(&inode->i_mutex);
2267 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002268 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002269 btrfs_invalidate_inodes(dest);
2270 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002271
2272 /* the last ref */
2273 if (dest->cache_inode) {
2274 iput(dest->cache_inode);
2275 dest->cache_inode = NULL;
2276 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002277 }
2278out_dput:
2279 dput(dentry);
2280out_unlock_dir:
2281 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002282 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002283out:
2284 kfree(vol_args);
2285 return err;
2286}
2287
Chris Mason1e701a32010-03-11 09:42:04 -05002288static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002289{
Al Viro496ad9a2013-01-23 17:07:38 -05002290 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002291 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002292 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002293 int ret;
2294
Ilya Dryomov25122d12013-01-20 15:57:57 +02002295 ret = mnt_want_write_file(file);
2296 if (ret)
2297 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002298
Ilya Dryomov25122d12013-01-20 15:57:57 +02002299 if (btrfs_root_readonly(root)) {
2300 ret = -EROFS;
2301 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002302 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002303
2304 switch (inode->i_mode & S_IFMT) {
2305 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002306 if (!capable(CAP_SYS_ADMIN)) {
2307 ret = -EPERM;
2308 goto out;
2309 }
Eric Sandeende78b512013-01-31 18:21:12 +00002310 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002311 if (ret)
2312 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002313 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002314 break;
2315 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002316 if (!(file->f_mode & FMODE_WRITE)) {
2317 ret = -EINVAL;
2318 goto out;
2319 }
Chris Mason1e701a32010-03-11 09:42:04 -05002320
2321 range = kzalloc(sizeof(*range), GFP_KERNEL);
2322 if (!range) {
2323 ret = -ENOMEM;
2324 goto out;
2325 }
2326
2327 if (argp) {
2328 if (copy_from_user(range, argp,
2329 sizeof(*range))) {
2330 ret = -EFAULT;
2331 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002332 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002333 }
2334 /* compression requires us to start the IO */
2335 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2336 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2337 range->extent_thresh = (u32)-1;
2338 }
2339 } else {
2340 /* the rest are all set to zero by kzalloc */
2341 range->len = (u64)-1;
2342 }
Al Viro496ad9a2013-01-23 17:07:38 -05002343 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002344 range, 0, 0);
2345 if (ret > 0)
2346 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002347 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002348 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002349 default:
2350 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002351 }
Chris Masone441d542009-01-05 16:57:23 -05002352out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002353 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002354 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002355}
2356
Christoph Hellwigb2950862008-12-02 09:54:17 -05002357static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002358{
2359 struct btrfs_ioctl_vol_args *vol_args;
2360 int ret;
2361
Chris Masone441d542009-01-05 16:57:23 -05002362 if (!capable(CAP_SYS_ADMIN))
2363 return -EPERM;
2364
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002365 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2366 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002367 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002368 }
2369
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002370 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002371 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002372 if (IS_ERR(vol_args)) {
2373 ret = PTR_ERR(vol_args);
2374 goto out;
2375 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002376
Mark Fasheh5516e592008-07-24 12:20:14 -04002377 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002378 ret = btrfs_init_new_device(root, vol_args->name);
2379
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002380 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002381out:
2382 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002383 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002384 return ret;
2385}
2386
Miao Xieda249272012-11-26 08:44:50 +00002387static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002388{
Al Viro496ad9a2013-01-23 17:07:38 -05002389 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002390 struct btrfs_ioctl_vol_args *vol_args;
2391 int ret;
2392
Chris Masone441d542009-01-05 16:57:23 -05002393 if (!capable(CAP_SYS_ADMIN))
2394 return -EPERM;
2395
Miao Xieda249272012-11-26 08:44:50 +00002396 ret = mnt_want_write_file(file);
2397 if (ret)
2398 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002399
Li Zefandae7b662009-04-08 15:06:54 +08002400 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002401 if (IS_ERR(vol_args)) {
2402 ret = PTR_ERR(vol_args);
2403 goto out;
2404 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002405
Mark Fasheh5516e592008-07-24 12:20:14 -04002406 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002407
Anand Jain183860f2013-05-17 10:52:45 +00002408 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2409 1)) {
2410 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2411 goto out;
2412 }
2413
2414 mutex_lock(&root->fs_info->volume_mutex);
2415 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002416 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002417 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002418
2419out:
2420 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002421 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002422 return ret;
2423}
2424
Jan Schmidt475f6382011-03-11 15:41:01 +01002425static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2426{
Li Zefan027ed2f2011-06-08 08:27:56 +00002427 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002428 struct btrfs_device *device;
2429 struct btrfs_device *next;
2430 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002431 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002432
2433 if (!capable(CAP_SYS_ADMIN))
2434 return -EPERM;
2435
Li Zefan027ed2f2011-06-08 08:27:56 +00002436 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2437 if (!fi_args)
2438 return -ENOMEM;
2439
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002440 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002441 fi_args->num_devices = fs_devices->num_devices;
2442 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002443
Jan Schmidt475f6382011-03-11 15:41:01 +01002444 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002445 if (device->devid > fi_args->max_id)
2446 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002447 }
2448 mutex_unlock(&fs_devices->device_list_mutex);
2449
Li Zefan027ed2f2011-06-08 08:27:56 +00002450 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2451 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002452
Li Zefan027ed2f2011-06-08 08:27:56 +00002453 kfree(fi_args);
2454 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002455}
2456
2457static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2458{
2459 struct btrfs_ioctl_dev_info_args *di_args;
2460 struct btrfs_device *dev;
2461 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2462 int ret = 0;
2463 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002464
2465 if (!capable(CAP_SYS_ADMIN))
2466 return -EPERM;
2467
2468 di_args = memdup_user(arg, sizeof(*di_args));
2469 if (IS_ERR(di_args))
2470 return PTR_ERR(di_args);
2471
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002472 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002473 s_uuid = di_args->uuid;
2474
2475 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002476 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002477
2478 if (!dev) {
2479 ret = -ENODEV;
2480 goto out;
2481 }
2482
2483 di_args->devid = dev->devid;
2484 di_args->bytes_used = dev->bytes_used;
2485 di_args->total_bytes = dev->total_bytes;
2486 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002487 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002488 struct rcu_string *name;
2489
2490 rcu_read_lock();
2491 name = rcu_dereference(dev->name);
2492 strncpy(di_args->path, name->str, sizeof(di_args->path));
2493 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002494 di_args->path[sizeof(di_args->path) - 1] = 0;
2495 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002496 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002497 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002498
2499out:
David Sterba55793c02013-04-26 15:20:23 +00002500 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002501 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2502 ret = -EFAULT;
2503
2504 kfree(di_args);
2505 return ret;
2506}
2507
Mark Fasheh416161d2013-08-06 11:42:51 -07002508static struct page *extent_same_get_page(struct inode *inode, u64 off)
2509{
2510 struct page *page;
2511 pgoff_t index;
2512 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2513
2514 index = off >> PAGE_CACHE_SHIFT;
2515
2516 page = grab_cache_page(inode->i_mapping, index);
2517 if (!page)
2518 return NULL;
2519
2520 if (!PageUptodate(page)) {
2521 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2522 0))
2523 return NULL;
2524 lock_page(page);
2525 if (!PageUptodate(page)) {
2526 unlock_page(page);
2527 page_cache_release(page);
2528 return NULL;
2529 }
2530 }
2531 unlock_page(page);
2532
2533 return page;
2534}
2535
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002536static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2537{
2538 /* do any pending delalloc/csum calc on src, one way or
2539 another, and lock file content */
2540 while (1) {
2541 struct btrfs_ordered_extent *ordered;
2542 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2543 ordered = btrfs_lookup_first_ordered_extent(inode,
2544 off + len - 1);
2545 if (!ordered &&
2546 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2547 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2548 break;
2549 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2550 if (ordered)
2551 btrfs_put_ordered_extent(ordered);
2552 btrfs_wait_ordered_range(inode, off, len);
2553 }
2554}
2555
Mark Fasheh416161d2013-08-06 11:42:51 -07002556static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2557 struct inode *inode2, u64 loff2, u64 len)
2558{
2559 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2560 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2561
2562 mutex_unlock(&inode1->i_mutex);
2563 mutex_unlock(&inode2->i_mutex);
2564}
2565
2566static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2567 struct inode *inode2, u64 loff2, u64 len)
2568{
2569 if (inode1 < inode2) {
2570 swap(inode1, inode2);
2571 swap(loff1, loff2);
2572 }
2573
2574 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2575 lock_extent_range(inode1, loff1, len);
2576 if (inode1 != inode2) {
2577 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2578 lock_extent_range(inode2, loff2, len);
2579 }
2580}
2581
2582static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2583 u64 dst_loff, u64 len)
2584{
2585 int ret = 0;
2586 struct page *src_page, *dst_page;
2587 unsigned int cmp_len = PAGE_CACHE_SIZE;
2588 void *addr, *dst_addr;
2589
2590 while (len) {
2591 if (len < PAGE_CACHE_SIZE)
2592 cmp_len = len;
2593
2594 src_page = extent_same_get_page(src, loff);
2595 if (!src_page)
2596 return -EINVAL;
2597 dst_page = extent_same_get_page(dst, dst_loff);
2598 if (!dst_page) {
2599 page_cache_release(src_page);
2600 return -EINVAL;
2601 }
2602 addr = kmap_atomic(src_page);
2603 dst_addr = kmap_atomic(dst_page);
2604
2605 flush_dcache_page(src_page);
2606 flush_dcache_page(dst_page);
2607
2608 if (memcmp(addr, dst_addr, cmp_len))
2609 ret = BTRFS_SAME_DATA_DIFFERS;
2610
2611 kunmap_atomic(addr);
2612 kunmap_atomic(dst_addr);
2613 page_cache_release(src_page);
2614 page_cache_release(dst_page);
2615
2616 if (ret)
2617 break;
2618
2619 loff += cmp_len;
2620 dst_loff += cmp_len;
2621 len -= cmp_len;
2622 }
2623
2624 return ret;
2625}
2626
2627static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2628{
2629 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2630
2631 if (off + len > inode->i_size || off + len < off)
2632 return -EINVAL;
2633 /* Check that we are block aligned - btrfs_clone() requires this */
2634 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2635 return -EINVAL;
2636
2637 return 0;
2638}
2639
2640static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2641 struct inode *dst, u64 dst_loff)
2642{
2643 int ret;
2644
2645 /*
2646 * btrfs_clone() can't handle extents in the same file
2647 * yet. Once that works, we can drop this check and replace it
2648 * with a check for the same inode, but overlapping extents.
2649 */
2650 if (src == dst)
2651 return -EINVAL;
2652
2653 btrfs_double_lock(src, loff, dst, dst_loff, len);
2654
2655 ret = extent_same_check_offsets(src, loff, len);
2656 if (ret)
2657 goto out_unlock;
2658
2659 ret = extent_same_check_offsets(dst, dst_loff, len);
2660 if (ret)
2661 goto out_unlock;
2662
2663 /* don't make the dst file partly checksummed */
2664 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2665 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2666 ret = -EINVAL;
2667 goto out_unlock;
2668 }
2669
2670 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2671 if (ret == 0)
2672 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2673
2674out_unlock:
2675 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2676
2677 return ret;
2678}
2679
2680#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2681
2682static long btrfs_ioctl_file_extent_same(struct file *file,
2683 void __user *argp)
2684{
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002685 struct btrfs_ioctl_same_args tmp;
2686 struct btrfs_ioctl_same_args *same;
2687 struct btrfs_ioctl_same_extent_info *info;
Mark Fasheh416161d2013-08-06 11:42:51 -07002688 struct inode *src = file->f_dentry->d_inode;
2689 struct file *dst_file = NULL;
2690 struct inode *dst;
2691 u64 off;
2692 u64 len;
2693 int i;
2694 int ret;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002695 unsigned long size;
Mark Fasheh416161d2013-08-06 11:42:51 -07002696 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2697 bool is_admin = capable(CAP_SYS_ADMIN);
2698
2699 if (!(file->f_mode & FMODE_READ))
2700 return -EINVAL;
2701
2702 ret = mnt_want_write_file(file);
2703 if (ret)
2704 return ret;
2705
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002706 if (copy_from_user(&tmp,
Mark Fasheh416161d2013-08-06 11:42:51 -07002707 (struct btrfs_ioctl_same_args __user *)argp,
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002708 sizeof(tmp))) {
Mark Fasheh416161d2013-08-06 11:42:51 -07002709 ret = -EFAULT;
2710 goto out;
2711 }
2712
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002713 size = sizeof(tmp) +
2714 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2715
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002716 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002717
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002718 if (IS_ERR(same)) {
2719 ret = PTR_ERR(same);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002720 goto out;
2721 }
2722
2723 off = same->logical_offset;
2724 len = same->length;
Mark Fasheh416161d2013-08-06 11:42:51 -07002725
2726 /*
2727 * Limit the total length we will dedupe for each operation.
2728 * This is intended to bound the total time spent in this
2729 * ioctl to something sane.
2730 */
2731 if (len > BTRFS_MAX_DEDUPE_LEN)
2732 len = BTRFS_MAX_DEDUPE_LEN;
2733
2734 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2735 /*
2736 * Btrfs does not support blocksize < page_size. As a
2737 * result, btrfs_cmp_data() won't correctly handle
2738 * this situation without an update.
2739 */
2740 ret = -EINVAL;
2741 goto out;
2742 }
2743
2744 ret = -EISDIR;
2745 if (S_ISDIR(src->i_mode))
2746 goto out;
2747
2748 ret = -EACCES;
2749 if (!S_ISREG(src->i_mode))
2750 goto out;
2751
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002752 /* pre-format output fields to sane values */
2753 for (i = 0; i < same->dest_count; i++) {
2754 same->info[i].bytes_deduped = 0ULL;
2755 same->info[i].status = 0;
2756 }
2757
Mark Fasheh416161d2013-08-06 11:42:51 -07002758 ret = 0;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002759 for (i = 0; i < same->dest_count; i++) {
2760 info = &same->info[i];
Mark Fasheh416161d2013-08-06 11:42:51 -07002761
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002762 dst_file = fget(info->fd);
Mark Fasheh416161d2013-08-06 11:42:51 -07002763 if (!dst_file) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002764 info->status = -EBADF;
Mark Fasheh416161d2013-08-06 11:42:51 -07002765 goto next;
2766 }
2767
2768 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002769 info->status = -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07002770 goto next;
2771 }
2772
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002773 info->status = -EXDEV;
Mark Fasheh416161d2013-08-06 11:42:51 -07002774 if (file->f_path.mnt != dst_file->f_path.mnt)
2775 goto next;
2776
2777 dst = dst_file->f_dentry->d_inode;
2778 if (src->i_sb != dst->i_sb)
2779 goto next;
2780
2781 if (S_ISDIR(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002782 info->status = -EISDIR;
Mark Fasheh416161d2013-08-06 11:42:51 -07002783 goto next;
2784 }
2785
2786 if (!S_ISREG(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002787 info->status = -EACCES;
Mark Fasheh416161d2013-08-06 11:42:51 -07002788 goto next;
2789 }
2790
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002791 info->status = btrfs_extent_same(src, off, len, dst,
2792 info->logical_offset);
2793 if (info->status == 0)
2794 info->bytes_deduped += len;
Mark Fasheh416161d2013-08-06 11:42:51 -07002795
2796next:
2797 if (dst_file)
2798 fput(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07002799 }
2800
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002801 ret = copy_to_user(argp, same, size);
2802 if (ret)
2803 ret = -EFAULT;
2804
Mark Fasheh416161d2013-08-06 11:42:51 -07002805out:
2806 mnt_drop_write_file(file);
2807 return ret;
2808}
2809
Mark Fasheh32b7c682013-08-06 11:42:49 -07002810/**
2811 * btrfs_clone() - clone a range from inode file to another
2812 *
2813 * @src: Inode to clone from
2814 * @inode: Inode to clone to
2815 * @off: Offset within source to start clone from
2816 * @olen: Original length, passed by user, of range to clone
2817 * @olen_aligned: Block-aligned value of olen, extent_same uses
2818 * identical values here
2819 * @destoff: Offset within @inode to start clone
2820 */
2821static int btrfs_clone(struct inode *src, struct inode *inode,
2822 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002823{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002824 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002825 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002826 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002827 struct btrfs_trans_handle *trans;
2828 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002829 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002830 u32 nritems;
2831 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002832 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002833 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002834
2835 ret = -ENOMEM;
2836 buf = vmalloc(btrfs_level_size(root, 0));
2837 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002838 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002839
2840 path = btrfs_alloc_path();
2841 if (!path) {
2842 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002843 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002844 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002845
Yan Zhengae01a0a2008-08-04 23:23:47 -04002846 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002847 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002848 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002849 key.type = BTRFS_EXTENT_DATA_KEY;
2850 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002851
2852 while (1) {
2853 /*
2854 * note the key will change type as we walk through the
2855 * tree.
2856 */
David Sterba362a20c2011-08-01 18:11:57 +02002857 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2858 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002859 if (ret < 0)
2860 goto out;
2861
Yan Zhengae01a0a2008-08-04 23:23:47 -04002862 nritems = btrfs_header_nritems(path->nodes[0]);
2863 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002864 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002865 if (ret < 0)
2866 goto out;
2867 if (ret > 0)
2868 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002869 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002870 }
2871 leaf = path->nodes[0];
2872 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002873
Yan Zhengae01a0a2008-08-04 23:23:47 -04002874 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002875 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002876 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002877 break;
2878
Sage Weilc5c9cd42008-11-12 14:32:25 -05002879 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2880 struct btrfs_file_extent_item *extent;
2881 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002882 u32 size;
2883 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002884 u64 disko = 0, diskl = 0;
2885 u64 datao = 0, datal = 0;
2886 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002887 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002888
2889 size = btrfs_item_size_nr(leaf, slot);
2890 read_extent_buffer(leaf, buf,
2891 btrfs_item_ptr_offset(leaf, slot),
2892 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002893
2894 extent = btrfs_item_ptr(leaf, slot,
2895 struct btrfs_file_extent_item);
2896 comp = btrfs_file_extent_compression(leaf, extent);
2897 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002898 if (type == BTRFS_FILE_EXTENT_REG ||
2899 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002900 disko = btrfs_file_extent_disk_bytenr(leaf,
2901 extent);
2902 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2903 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002904 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002905 datal = btrfs_file_extent_num_bytes(leaf,
2906 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002907 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2908 /* take upper bound, may be compressed */
2909 datal = btrfs_file_extent_ram_bytes(leaf,
2910 extent);
2911 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002912 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002913
Sage Weil050006a2010-10-29 15:37:33 -04002914 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002915 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002916 goto next;
2917
Zheng Yan31840ae2008-09-23 13:14:14 -04002918 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002919 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002920 if (off <= key.offset)
2921 new_key.offset = key.offset + destoff - off;
2922 else
2923 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002924
Sage Weilb6f34092011-09-20 14:48:51 -04002925 /*
2926 * 1 - adjusting old extent (we may have to split it)
2927 * 1 - add new extent
2928 * 1 - inode update
2929 */
2930 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002931 if (IS_ERR(trans)) {
2932 ret = PTR_ERR(trans);
2933 goto out;
2934 }
2935
Chris Masonc8a894d2009-06-27 21:07:03 -04002936 if (type == BTRFS_FILE_EXTENT_REG ||
2937 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002938 /*
2939 * a | --- range to clone ---| b
2940 * | ------------- extent ------------- |
2941 */
2942
2943 /* substract range b */
2944 if (key.offset + datal > off + len)
2945 datal = off + len - key.offset;
2946
2947 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002948 if (off > key.offset) {
2949 datao += off - key.offset;
2950 datal -= off - key.offset;
2951 }
2952
Josef Bacik5dc562c2012-08-17 13:14:17 -04002953 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002954 new_key.offset,
2955 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002956 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002957 if (ret) {
2958 btrfs_abort_transaction(trans, root,
2959 ret);
2960 btrfs_end_transaction(trans, root);
2961 goto out;
2962 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002963
Sage Weilc5c9cd42008-11-12 14:32:25 -05002964 ret = btrfs_insert_empty_item(trans, root, path,
2965 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002966 if (ret) {
2967 btrfs_abort_transaction(trans, root,
2968 ret);
2969 btrfs_end_transaction(trans, root);
2970 goto out;
2971 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002972
2973 leaf = path->nodes[0];
2974 slot = path->slots[0];
2975 write_extent_buffer(leaf, buf,
2976 btrfs_item_ptr_offset(leaf, slot),
2977 size);
2978
2979 extent = btrfs_item_ptr(leaf, slot,
2980 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002981
Sage Weilc5c9cd42008-11-12 14:32:25 -05002982 /* disko == 0 means it's a hole */
2983 if (!disko)
2984 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002985
2986 btrfs_set_file_extent_offset(leaf, extent,
2987 datao);
2988 btrfs_set_file_extent_num_bytes(leaf, extent,
2989 datal);
2990 if (disko) {
2991 inode_add_bytes(inode, datal);
2992 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002993 disko, diskl, 0,
2994 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002995 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002996 new_key.offset - datao,
2997 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 if (ret) {
2999 btrfs_abort_transaction(trans,
3000 root,
3001 ret);
3002 btrfs_end_transaction(trans,
3003 root);
3004 goto out;
3005
3006 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003007 }
3008 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3009 u64 skip = 0;
3010 u64 trim = 0;
3011 if (off > key.offset) {
3012 skip = off - key.offset;
3013 new_key.offset += skip;
3014 }
Chris Masond3977122009-01-05 21:25:51 -05003015
Liu Boaa42ffd2012-09-18 03:52:23 -06003016 if (key.offset + datal > off + len)
3017 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003018
Sage Weilc5c9cd42008-11-12 14:32:25 -05003019 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003020 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003021 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003022 goto out;
3023 }
3024 size -= skip + trim;
3025 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003026
Josef Bacik5dc562c2012-08-17 13:14:17 -04003027 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003028 new_key.offset,
3029 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003030 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003031 if (ret) {
3032 btrfs_abort_transaction(trans, root,
3033 ret);
3034 btrfs_end_transaction(trans, root);
3035 goto out;
3036 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003037
Sage Weilc5c9cd42008-11-12 14:32:25 -05003038 ret = btrfs_insert_empty_item(trans, root, path,
3039 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003040 if (ret) {
3041 btrfs_abort_transaction(trans, root,
3042 ret);
3043 btrfs_end_transaction(trans, root);
3044 goto out;
3045 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003046
3047 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003048 u32 start =
3049 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003050 memmove(buf+start, buf+start+skip,
3051 datal);
3052 }
3053
3054 leaf = path->nodes[0];
3055 slot = path->slots[0];
3056 write_extent_buffer(leaf, buf,
3057 btrfs_item_ptr_offset(leaf, slot),
3058 size);
3059 inode_add_bytes(inode, datal);
3060 }
3061
3062 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003063 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003064
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003065 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003066 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003067
3068 /*
3069 * we round up to the block size at eof when
3070 * determining which extents to clone above,
3071 * but shouldn't round up the file size
3072 */
3073 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003074 if (endoff > destoff+olen)
3075 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003076 if (endoff > inode->i_size)
3077 btrfs_i_size_write(inode, endoff);
3078
Yan, Zhenga22285a2010-05-16 10:48:46 -04003079 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003080 if (ret) {
3081 btrfs_abort_transaction(trans, root, ret);
3082 btrfs_end_transaction(trans, root);
3083 goto out;
3084 }
3085 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003086 }
Chris Masond3977122009-01-05 21:25:51 -05003087next:
David Sterbab3b4aa72011-04-21 01:20:15 +02003088 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003089 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003090 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003091 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003092
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003093out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003094 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003095 btrfs_free_path(path);
3096 vfree(buf);
3097 return ret;
3098}
3099
3100static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3101 u64 off, u64 olen, u64 destoff)
3102{
3103 struct inode *inode = fdentry(file)->d_inode;
3104 struct btrfs_root *root = BTRFS_I(inode)->root;
3105 struct fd src_file;
3106 struct inode *src;
3107 int ret;
3108 u64 len = olen;
3109 u64 bs = root->fs_info->sb->s_blocksize;
3110 int same_inode = 0;
3111
3112 /*
3113 * TODO:
3114 * - split compressed inline extents. annoying: we need to
3115 * decompress into destination's address_space (the file offset
3116 * may change, so source mapping won't do), then recompress (or
3117 * otherwise reinsert) a subrange.
3118 * - allow ranges within the same file to be cloned (provided
3119 * they don't overlap)?
3120 */
3121
3122 /* the destination must be opened for writing */
3123 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3124 return -EINVAL;
3125
3126 if (btrfs_root_readonly(root))
3127 return -EROFS;
3128
3129 ret = mnt_want_write_file(file);
3130 if (ret)
3131 return ret;
3132
3133 src_file = fdget(srcfd);
3134 if (!src_file.file) {
3135 ret = -EBADF;
3136 goto out_drop_write;
3137 }
3138
3139 ret = -EXDEV;
3140 if (src_file.file->f_path.mnt != file->f_path.mnt)
3141 goto out_fput;
3142
3143 src = file_inode(src_file.file);
3144
3145 ret = -EINVAL;
3146 if (src == inode)
3147 same_inode = 1;
3148
3149 /* the src must be open for reading */
3150 if (!(src_file.file->f_mode & FMODE_READ))
3151 goto out_fput;
3152
3153 /* don't make the dst file partly checksummed */
3154 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3155 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3156 goto out_fput;
3157
3158 ret = -EISDIR;
3159 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3160 goto out_fput;
3161
3162 ret = -EXDEV;
3163 if (src->i_sb != inode->i_sb)
3164 goto out_fput;
3165
3166 if (!same_inode) {
3167 if (inode < src) {
3168 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3169 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3170 } else {
3171 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3172 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3173 }
3174 } else {
3175 mutex_lock(&src->i_mutex);
3176 }
3177
3178 /* determine range to clone */
3179 ret = -EINVAL;
3180 if (off + len > src->i_size || off + len < off)
3181 goto out_unlock;
3182 if (len == 0)
3183 olen = len = src->i_size - off;
3184 /* if we extend to eof, continue to block boundary */
3185 if (off + len == src->i_size)
3186 len = ALIGN(src->i_size, bs) - off;
3187
3188 /* verify the end result is block aligned */
3189 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3190 !IS_ALIGNED(destoff, bs))
3191 goto out_unlock;
3192
3193 /* verify if ranges are overlapped within the same file */
3194 if (same_inode) {
3195 if (destoff + len > off && destoff < off + len)
3196 goto out_unlock;
3197 }
3198
3199 if (destoff > inode->i_size) {
3200 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3201 if (ret)
3202 goto out_unlock;
3203 }
3204
3205 /* truncate page cache pages from target inode range */
3206 truncate_inode_pages_range(&inode->i_data, destoff,
3207 PAGE_CACHE_ALIGN(destoff + len) - 1);
3208
3209 lock_extent_range(src, off, len);
3210
3211 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3212
Liu Boaa42ffd2012-09-18 03:52:23 -06003213 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003214out_unlock:
3215 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00003216 if (!same_inode)
3217 mutex_unlock(&inode->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003218out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003219 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003220out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003221 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003222 return ret;
3223}
3224
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003225static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003226{
3227 struct btrfs_ioctl_clone_range_args args;
3228
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003229 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003230 return -EFAULT;
3231 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3232 args.src_length, args.dest_offset);
3233}
3234
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003235/*
3236 * there are many ways the trans_start and trans_end ioctls can lead
3237 * to deadlocks. They should only be used by applications that
3238 * basically own the machine, and have a very in depth understanding
3239 * of all the possible deadlocks and enospc problems.
3240 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003241static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003242{
Al Viro496ad9a2013-01-23 17:07:38 -05003243 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003244 struct btrfs_root *root = BTRFS_I(inode)->root;
3245 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003246 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003247
Sage Weil1ab86ae2009-09-29 18:38:44 -04003248 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003249 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003250 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003251
3252 ret = -EINPROGRESS;
3253 if (file->private_data)
3254 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003255
Li Zefanb83cc962010-12-20 16:04:08 +08003256 ret = -EROFS;
3257 if (btrfs_root_readonly(root))
3258 goto out;
3259
Al Viroa561be72011-11-23 11:57:51 -05003260 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003261 if (ret)
3262 goto out;
3263
Josef Bacika4abeea2011-04-11 17:25:13 -04003264 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003265
Sage Weil1ab86ae2009-09-29 18:38:44 -04003266 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003267 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003268 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003269 goto out_drop;
3270
3271 file->private_data = trans;
3272 return 0;
3273
3274out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003275 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003276 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003277out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003278 return ret;
3279}
3280
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003281static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3282{
Al Viro496ad9a2013-01-23 17:07:38 -05003283 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003284 struct btrfs_root *root = BTRFS_I(inode)->root;
3285 struct btrfs_root *new_root;
3286 struct btrfs_dir_item *di;
3287 struct btrfs_trans_handle *trans;
3288 struct btrfs_path *path;
3289 struct btrfs_key location;
3290 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003291 u64 objectid = 0;
3292 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003293 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003294
3295 if (!capable(CAP_SYS_ADMIN))
3296 return -EPERM;
3297
Miao Xie3c04ce02012-11-26 08:43:07 +00003298 ret = mnt_want_write_file(file);
3299 if (ret)
3300 return ret;
3301
3302 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3303 ret = -EFAULT;
3304 goto out;
3305 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003306
3307 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303308 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003309
3310 location.objectid = objectid;
3311 location.type = BTRFS_ROOT_ITEM_KEY;
3312 location.offset = (u64)-1;
3313
3314 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003315 if (IS_ERR(new_root)) {
3316 ret = PTR_ERR(new_root);
3317 goto out;
3318 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003319
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003320 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003321 if (!path) {
3322 ret = -ENOMEM;
3323 goto out;
3324 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003325 path->leave_spinning = 1;
3326
3327 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003328 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003329 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003330 ret = PTR_ERR(trans);
3331 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003332 }
3333
David Sterba6c417612011-04-13 15:41:04 +02003334 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003335 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3336 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003337 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003338 btrfs_free_path(path);
3339 btrfs_end_transaction(trans, root);
3340 printk(KERN_ERR "Umm, you don't have the default dir item, "
3341 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00003342 ret = -ENOENT;
3343 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003344 }
3345
3346 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3347 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3348 btrfs_mark_buffer_dirty(path->nodes[0]);
3349 btrfs_free_path(path);
3350
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003351 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003352 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003353out:
3354 mnt_drop_write_file(file);
3355 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003356}
3357
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003358void btrfs_get_block_group_info(struct list_head *groups_list,
3359 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003360{
3361 struct btrfs_block_group_cache *block_group;
3362
3363 space->total_bytes = 0;
3364 space->used_bytes = 0;
3365 space->flags = 0;
3366 list_for_each_entry(block_group, groups_list, list) {
3367 space->flags = block_group->flags;
3368 space->total_bytes += block_group->key.offset;
3369 space->used_bytes +=
3370 btrfs_block_group_used(&block_group->item);
3371 }
3372}
3373
Eric Sandeen48a3b632013-04-25 20:41:01 +00003374static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003375{
3376 struct btrfs_ioctl_space_args space_args;
3377 struct btrfs_ioctl_space_info space;
3378 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003379 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003380 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003381 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003382 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3383 BTRFS_BLOCK_GROUP_SYSTEM,
3384 BTRFS_BLOCK_GROUP_METADATA,
3385 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3386 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003387 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003388 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003389 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003390 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003391
3392 if (copy_from_user(&space_args,
3393 (struct btrfs_ioctl_space_args __user *)arg,
3394 sizeof(space_args)))
3395 return -EFAULT;
3396
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003397 for (i = 0; i < num_types; i++) {
3398 struct btrfs_space_info *tmp;
3399
3400 info = NULL;
3401 rcu_read_lock();
3402 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3403 list) {
3404 if (tmp->flags == types[i]) {
3405 info = tmp;
3406 break;
3407 }
3408 }
3409 rcu_read_unlock();
3410
3411 if (!info)
3412 continue;
3413
3414 down_read(&info->groups_sem);
3415 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3416 if (!list_empty(&info->block_groups[c]))
3417 slot_count++;
3418 }
3419 up_read(&info->groups_sem);
3420 }
Josef Bacik1406e432010-01-13 18:19:06 +00003421
Chris Mason7fde62b2010-03-16 15:40:10 -04003422 /* space_slots == 0 means they are asking for a count */
3423 if (space_args.space_slots == 0) {
3424 space_args.total_spaces = slot_count;
3425 goto out;
3426 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003427
Dan Rosenberg51788b12011-02-14 16:04:23 -05003428 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003429
Chris Mason7fde62b2010-03-16 15:40:10 -04003430 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003431
Chris Mason7fde62b2010-03-16 15:40:10 -04003432 /* we generally have at most 6 or so space infos, one for each raid
3433 * level. So, a whole page should be more than enough for everyone
3434 */
3435 if (alloc_size > PAGE_CACHE_SIZE)
3436 return -ENOMEM;
3437
3438 space_args.total_spaces = 0;
3439 dest = kmalloc(alloc_size, GFP_NOFS);
3440 if (!dest)
3441 return -ENOMEM;
3442 dest_orig = dest;
3443
3444 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003445 for (i = 0; i < num_types; i++) {
3446 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003447
Dan Rosenberg51788b12011-02-14 16:04:23 -05003448 if (!slot_count)
3449 break;
3450
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003451 info = NULL;
3452 rcu_read_lock();
3453 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3454 list) {
3455 if (tmp->flags == types[i]) {
3456 info = tmp;
3457 break;
3458 }
3459 }
3460 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003461
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003462 if (!info)
3463 continue;
3464 down_read(&info->groups_sem);
3465 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3466 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003467 btrfs_get_block_group_info(
3468 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003469 memcpy(dest, &space, sizeof(space));
3470 dest++;
3471 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003472 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003473 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003474 if (!slot_count)
3475 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003476 }
3477 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003478 }
Josef Bacik1406e432010-01-13 18:19:06 +00003479
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003480 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003481 (arg + sizeof(struct btrfs_ioctl_space_args));
3482
3483 if (copy_to_user(user_dest, dest_orig, alloc_size))
3484 ret = -EFAULT;
3485
3486 kfree(dest_orig);
3487out:
3488 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003489 ret = -EFAULT;
3490
3491 return ret;
3492}
3493
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003494/*
3495 * there are many ways the trans_start and trans_end ioctls can lead
3496 * to deadlocks. They should only be used by applications that
3497 * basically own the machine, and have a very in depth understanding
3498 * of all the possible deadlocks and enospc problems.
3499 */
3500long btrfs_ioctl_trans_end(struct file *file)
3501{
Al Viro496ad9a2013-01-23 17:07:38 -05003502 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003503 struct btrfs_root *root = BTRFS_I(inode)->root;
3504 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003505
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003506 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003507 if (!trans)
3508 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003509 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003510
Sage Weil1ab86ae2009-09-29 18:38:44 -04003511 btrfs_end_transaction(trans, root);
3512
Josef Bacika4abeea2011-04-11 17:25:13 -04003513 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003514
Al Viro2a79f172011-12-09 08:06:57 -05003515 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003516 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003517}
3518
Miao Xie9a8c28b2012-11-26 08:40:43 +00003519static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3520 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003521{
Sage Weil46204592010-10-29 15:41:32 -04003522 struct btrfs_trans_handle *trans;
3523 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003524 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003525
Miao Xied4edf392013-02-20 09:17:06 +00003526 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003527 if (IS_ERR(trans)) {
3528 if (PTR_ERR(trans) != -ENOENT)
3529 return PTR_ERR(trans);
3530
3531 /* No running transaction, don't bother */
3532 transid = root->fs_info->last_trans_committed;
3533 goto out;
3534 }
Sage Weil46204592010-10-29 15:41:32 -04003535 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003536 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003537 if (ret) {
3538 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003539 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003540 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003541out:
Sage Weil46204592010-10-29 15:41:32 -04003542 if (argp)
3543 if (copy_to_user(argp, &transid, sizeof(transid)))
3544 return -EFAULT;
3545 return 0;
3546}
3547
Miao Xie9a8c28b2012-11-26 08:40:43 +00003548static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3549 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003550{
Sage Weil46204592010-10-29 15:41:32 -04003551 u64 transid;
3552
3553 if (argp) {
3554 if (copy_from_user(&transid, argp, sizeof(transid)))
3555 return -EFAULT;
3556 } else {
3557 transid = 0; /* current trans */
3558 }
3559 return btrfs_wait_for_commit(root, transid);
3560}
3561
Miao Xieb8e95482012-11-26 08:48:01 +00003562static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003563{
Al Viro496ad9a2013-01-23 17:07:38 -05003564 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003565 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003566 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003567
3568 if (!capable(CAP_SYS_ADMIN))
3569 return -EPERM;
3570
3571 sa = memdup_user(arg, sizeof(*sa));
3572 if (IS_ERR(sa))
3573 return PTR_ERR(sa);
3574
Miao Xieb8e95482012-11-26 08:48:01 +00003575 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3576 ret = mnt_want_write_file(file);
3577 if (ret)
3578 goto out;
3579 }
3580
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003581 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003582 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3583 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003584
3585 if (copy_to_user(arg, sa, sizeof(*sa)))
3586 ret = -EFAULT;
3587
Miao Xieb8e95482012-11-26 08:48:01 +00003588 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3589 mnt_drop_write_file(file);
3590out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003591 kfree(sa);
3592 return ret;
3593}
3594
3595static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3596{
3597 if (!capable(CAP_SYS_ADMIN))
3598 return -EPERM;
3599
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003600 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003601}
3602
3603static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3604 void __user *arg)
3605{
3606 struct btrfs_ioctl_scrub_args *sa;
3607 int ret;
3608
3609 if (!capable(CAP_SYS_ADMIN))
3610 return -EPERM;
3611
3612 sa = memdup_user(arg, sizeof(*sa));
3613 if (IS_ERR(sa))
3614 return PTR_ERR(sa);
3615
3616 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3617
3618 if (copy_to_user(arg, sa, sizeof(*sa)))
3619 ret = -EFAULT;
3620
3621 kfree(sa);
3622 return ret;
3623}
3624
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003625static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003626 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003627{
3628 struct btrfs_ioctl_get_dev_stats *sa;
3629 int ret;
3630
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003631 sa = memdup_user(arg, sizeof(*sa));
3632 if (IS_ERR(sa))
3633 return PTR_ERR(sa);
3634
David Sterbab27f7c02012-06-22 06:30:39 -06003635 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3636 kfree(sa);
3637 return -EPERM;
3638 }
3639
3640 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003641
3642 if (copy_to_user(arg, sa, sizeof(*sa)))
3643 ret = -EFAULT;
3644
3645 kfree(sa);
3646 return ret;
3647}
3648
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003649static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3650{
3651 struct btrfs_ioctl_dev_replace_args *p;
3652 int ret;
3653
3654 if (!capable(CAP_SYS_ADMIN))
3655 return -EPERM;
3656
3657 p = memdup_user(arg, sizeof(*p));
3658 if (IS_ERR(p))
3659 return PTR_ERR(p);
3660
3661 switch (p->cmd) {
3662 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003663 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3664 ret = -EROFS;
3665 goto out;
3666 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003667 if (atomic_xchg(
3668 &root->fs_info->mutually_exclusive_operation_running,
3669 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003670 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003671 } else {
3672 ret = btrfs_dev_replace_start(root, p);
3673 atomic_set(
3674 &root->fs_info->mutually_exclusive_operation_running,
3675 0);
3676 }
3677 break;
3678 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3679 btrfs_dev_replace_status(root->fs_info, p);
3680 ret = 0;
3681 break;
3682 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3683 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3684 break;
3685 default:
3686 ret = -EINVAL;
3687 break;
3688 }
3689
3690 if (copy_to_user(arg, p, sizeof(*p)))
3691 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003692out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003693 kfree(p);
3694 return ret;
3695}
3696
Jan Schmidtd7728c92011-07-07 16:48:38 +02003697static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3698{
3699 int ret = 0;
3700 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003701 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003702 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003703 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003704 struct inode_fs_paths *ipath = NULL;
3705 struct btrfs_path *path;
3706
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003707 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003708 return -EPERM;
3709
3710 path = btrfs_alloc_path();
3711 if (!path) {
3712 ret = -ENOMEM;
3713 goto out;
3714 }
3715
3716 ipa = memdup_user(arg, sizeof(*ipa));
3717 if (IS_ERR(ipa)) {
3718 ret = PTR_ERR(ipa);
3719 ipa = NULL;
3720 goto out;
3721 }
3722
3723 size = min_t(u32, ipa->size, 4096);
3724 ipath = init_ipath(size, root, path);
3725 if (IS_ERR(ipath)) {
3726 ret = PTR_ERR(ipath);
3727 ipath = NULL;
3728 goto out;
3729 }
3730
3731 ret = paths_from_inode(ipa->inum, ipath);
3732 if (ret < 0)
3733 goto out;
3734
3735 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003736 rel_ptr = ipath->fspath->val[i] -
3737 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003738 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003739 }
3740
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003741 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3742 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003743 if (ret) {
3744 ret = -EFAULT;
3745 goto out;
3746 }
3747
3748out:
3749 btrfs_free_path(path);
3750 free_ipath(ipath);
3751 kfree(ipa);
3752
3753 return ret;
3754}
3755
3756static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3757{
3758 struct btrfs_data_container *inodes = ctx;
3759 const size_t c = 3 * sizeof(u64);
3760
3761 if (inodes->bytes_left >= c) {
3762 inodes->bytes_left -= c;
3763 inodes->val[inodes->elem_cnt] = inum;
3764 inodes->val[inodes->elem_cnt + 1] = offset;
3765 inodes->val[inodes->elem_cnt + 2] = root;
3766 inodes->elem_cnt += 3;
3767 } else {
3768 inodes->bytes_missing += c - inodes->bytes_left;
3769 inodes->bytes_left = 0;
3770 inodes->elem_missed += 3;
3771 }
3772
3773 return 0;
3774}
3775
3776static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3777 void __user *arg)
3778{
3779 int ret = 0;
3780 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003781 struct btrfs_ioctl_logical_ino_args *loi;
3782 struct btrfs_data_container *inodes = NULL;
3783 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003784
3785 if (!capable(CAP_SYS_ADMIN))
3786 return -EPERM;
3787
3788 loi = memdup_user(arg, sizeof(*loi));
3789 if (IS_ERR(loi)) {
3790 ret = PTR_ERR(loi);
3791 loi = NULL;
3792 goto out;
3793 }
3794
3795 path = btrfs_alloc_path();
3796 if (!path) {
3797 ret = -ENOMEM;
3798 goto out;
3799 }
3800
Liu Bo425d17a2012-09-07 20:01:30 -06003801 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003802 inodes = init_data_container(size);
3803 if (IS_ERR(inodes)) {
3804 ret = PTR_ERR(inodes);
3805 inodes = NULL;
3806 goto out;
3807 }
3808
Liu Bodf031f02012-09-07 20:01:29 -06003809 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3810 build_ino_list, inodes);
3811 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003812 ret = -ENOENT;
3813 if (ret < 0)
3814 goto out;
3815
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003816 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3817 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003818 if (ret)
3819 ret = -EFAULT;
3820
3821out:
3822 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003823 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003824 kfree(loi);
3825
3826 return ret;
3827}
3828
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003829void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003830 struct btrfs_ioctl_balance_args *bargs)
3831{
3832 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3833
3834 bargs->flags = bctl->flags;
3835
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003836 if (atomic_read(&fs_info->balance_running))
3837 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3838 if (atomic_read(&fs_info->balance_pause_req))
3839 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003840 if (atomic_read(&fs_info->balance_cancel_req))
3841 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003842
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003843 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3844 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3845 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003846
3847 if (lock) {
3848 spin_lock(&fs_info->balance_lock);
3849 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3850 spin_unlock(&fs_info->balance_lock);
3851 } else {
3852 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3853 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003854}
3855
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003856static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003857{
Al Viro496ad9a2013-01-23 17:07:38 -05003858 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003859 struct btrfs_fs_info *fs_info = root->fs_info;
3860 struct btrfs_ioctl_balance_args *bargs;
3861 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003862 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003863 int ret;
3864
3865 if (!capable(CAP_SYS_ADMIN))
3866 return -EPERM;
3867
Liu Boe54bfa32012-06-29 03:58:48 -06003868 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003869 if (ret)
3870 return ret;
3871
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003872again:
3873 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3874 mutex_lock(&fs_info->volume_mutex);
3875 mutex_lock(&fs_info->balance_mutex);
3876 need_unlock = true;
3877 goto locked;
3878 }
3879
3880 /*
3881 * mut. excl. ops lock is locked. Three possibilites:
3882 * (1) some other op is running
3883 * (2) balance is running
3884 * (3) balance is paused -- special case (think resume)
3885 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003886 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003887 if (fs_info->balance_ctl) {
3888 /* this is either (2) or (3) */
3889 if (!atomic_read(&fs_info->balance_running)) {
3890 mutex_unlock(&fs_info->balance_mutex);
3891 if (!mutex_trylock(&fs_info->volume_mutex))
3892 goto again;
3893 mutex_lock(&fs_info->balance_mutex);
3894
3895 if (fs_info->balance_ctl &&
3896 !atomic_read(&fs_info->balance_running)) {
3897 /* this is (3) */
3898 need_unlock = false;
3899 goto locked;
3900 }
3901
3902 mutex_unlock(&fs_info->balance_mutex);
3903 mutex_unlock(&fs_info->volume_mutex);
3904 goto again;
3905 } else {
3906 /* this is (2) */
3907 mutex_unlock(&fs_info->balance_mutex);
3908 ret = -EINPROGRESS;
3909 goto out;
3910 }
3911 } else {
3912 /* this is (1) */
3913 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08003914 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003915 goto out;
3916 }
3917
3918locked:
3919 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003920
3921 if (arg) {
3922 bargs = memdup_user(arg, sizeof(*bargs));
3923 if (IS_ERR(bargs)) {
3924 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003925 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003926 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003927
3928 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3929 if (!fs_info->balance_ctl) {
3930 ret = -ENOTCONN;
3931 goto out_bargs;
3932 }
3933
3934 bctl = fs_info->balance_ctl;
3935 spin_lock(&fs_info->balance_lock);
3936 bctl->flags |= BTRFS_BALANCE_RESUME;
3937 spin_unlock(&fs_info->balance_lock);
3938
3939 goto do_balance;
3940 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003941 } else {
3942 bargs = NULL;
3943 }
3944
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003945 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003946 ret = -EINPROGRESS;
3947 goto out_bargs;
3948 }
3949
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003950 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3951 if (!bctl) {
3952 ret = -ENOMEM;
3953 goto out_bargs;
3954 }
3955
3956 bctl->fs_info = fs_info;
3957 if (arg) {
3958 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3959 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3960 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3961
3962 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003963 } else {
3964 /* balance everything - no filters */
3965 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003966 }
3967
Ilya Dryomovde322262012-01-16 22:04:49 +02003968do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003969 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003970 * Ownership of bctl and mutually_exclusive_operation_running
3971 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3972 * or, if restriper was paused all the way until unmount, in
3973 * free_fs_info. mutually_exclusive_operation_running is
3974 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003975 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003976 need_unlock = false;
3977
3978 ret = btrfs_balance(bctl, bargs);
3979
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003980 if (arg) {
3981 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3982 ret = -EFAULT;
3983 }
3984
3985out_bargs:
3986 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003987out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003988 mutex_unlock(&fs_info->balance_mutex);
3989 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003990 if (need_unlock)
3991 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3992out:
Liu Boe54bfa32012-06-29 03:58:48 -06003993 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003994 return ret;
3995}
3996
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003997static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3998{
3999 if (!capable(CAP_SYS_ADMIN))
4000 return -EPERM;
4001
4002 switch (cmd) {
4003 case BTRFS_BALANCE_CTL_PAUSE:
4004 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004005 case BTRFS_BALANCE_CTL_CANCEL:
4006 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004007 }
4008
4009 return -EINVAL;
4010}
4011
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004012static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4013 void __user *arg)
4014{
4015 struct btrfs_fs_info *fs_info = root->fs_info;
4016 struct btrfs_ioctl_balance_args *bargs;
4017 int ret = 0;
4018
4019 if (!capable(CAP_SYS_ADMIN))
4020 return -EPERM;
4021
4022 mutex_lock(&fs_info->balance_mutex);
4023 if (!fs_info->balance_ctl) {
4024 ret = -ENOTCONN;
4025 goto out;
4026 }
4027
4028 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4029 if (!bargs) {
4030 ret = -ENOMEM;
4031 goto out;
4032 }
4033
4034 update_ioctl_balance_args(fs_info, 1, bargs);
4035
4036 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4037 ret = -EFAULT;
4038
4039 kfree(bargs);
4040out:
4041 mutex_unlock(&fs_info->balance_mutex);
4042 return ret;
4043}
4044
Miao Xie905b0dd2012-11-26 08:50:11 +00004045static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004046{
Al Viro496ad9a2013-01-23 17:07:38 -05004047 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004048 struct btrfs_ioctl_quota_ctl_args *sa;
4049 struct btrfs_trans_handle *trans = NULL;
4050 int ret;
4051 int err;
4052
4053 if (!capable(CAP_SYS_ADMIN))
4054 return -EPERM;
4055
Miao Xie905b0dd2012-11-26 08:50:11 +00004056 ret = mnt_want_write_file(file);
4057 if (ret)
4058 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004059
4060 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004061 if (IS_ERR(sa)) {
4062 ret = PTR_ERR(sa);
4063 goto drop_write;
4064 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004065
Wang Shilong7708f022013-04-07 10:24:57 +00004066 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004067 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4068 if (IS_ERR(trans)) {
4069 ret = PTR_ERR(trans);
4070 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004071 }
4072
4073 switch (sa->cmd) {
4074 case BTRFS_QUOTA_CTL_ENABLE:
4075 ret = btrfs_quota_enable(trans, root->fs_info);
4076 break;
4077 case BTRFS_QUOTA_CTL_DISABLE:
4078 ret = btrfs_quota_disable(trans, root->fs_info);
4079 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004080 default:
4081 ret = -EINVAL;
4082 break;
4083 }
4084
Jan Schmidt2f232032013-04-25 16:04:51 +00004085 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4086 if (err && !ret)
4087 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004088out:
4089 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004090 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004091drop_write:
4092 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004093 return ret;
4094}
4095
Miao Xie905b0dd2012-11-26 08:50:11 +00004096static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004097{
Al Viro496ad9a2013-01-23 17:07:38 -05004098 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004099 struct btrfs_ioctl_qgroup_assign_args *sa;
4100 struct btrfs_trans_handle *trans;
4101 int ret;
4102 int err;
4103
4104 if (!capable(CAP_SYS_ADMIN))
4105 return -EPERM;
4106
Miao Xie905b0dd2012-11-26 08:50:11 +00004107 ret = mnt_want_write_file(file);
4108 if (ret)
4109 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004110
4111 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004112 if (IS_ERR(sa)) {
4113 ret = PTR_ERR(sa);
4114 goto drop_write;
4115 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004116
4117 trans = btrfs_join_transaction(root);
4118 if (IS_ERR(trans)) {
4119 ret = PTR_ERR(trans);
4120 goto out;
4121 }
4122
4123 /* FIXME: check if the IDs really exist */
4124 if (sa->assign) {
4125 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4126 sa->src, sa->dst);
4127 } else {
4128 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4129 sa->src, sa->dst);
4130 }
4131
4132 err = btrfs_end_transaction(trans, root);
4133 if (err && !ret)
4134 ret = err;
4135
4136out:
4137 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004138drop_write:
4139 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004140 return ret;
4141}
4142
Miao Xie905b0dd2012-11-26 08:50:11 +00004143static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004144{
Al Viro496ad9a2013-01-23 17:07:38 -05004145 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004146 struct btrfs_ioctl_qgroup_create_args *sa;
4147 struct btrfs_trans_handle *trans;
4148 int ret;
4149 int err;
4150
4151 if (!capable(CAP_SYS_ADMIN))
4152 return -EPERM;
4153
Miao Xie905b0dd2012-11-26 08:50:11 +00004154 ret = mnt_want_write_file(file);
4155 if (ret)
4156 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004157
4158 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004159 if (IS_ERR(sa)) {
4160 ret = PTR_ERR(sa);
4161 goto drop_write;
4162 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004163
Miao Xied86e56c2012-11-15 11:35:41 +00004164 if (!sa->qgroupid) {
4165 ret = -EINVAL;
4166 goto out;
4167 }
4168
Arne Jansen5d13a372011-09-14 15:53:51 +02004169 trans = btrfs_join_transaction(root);
4170 if (IS_ERR(trans)) {
4171 ret = PTR_ERR(trans);
4172 goto out;
4173 }
4174
4175 /* FIXME: check if the IDs really exist */
4176 if (sa->create) {
4177 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4178 NULL);
4179 } else {
4180 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4181 }
4182
4183 err = btrfs_end_transaction(trans, root);
4184 if (err && !ret)
4185 ret = err;
4186
4187out:
4188 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004189drop_write:
4190 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004191 return ret;
4192}
4193
Miao Xie905b0dd2012-11-26 08:50:11 +00004194static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004195{
Al Viro496ad9a2013-01-23 17:07:38 -05004196 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004197 struct btrfs_ioctl_qgroup_limit_args *sa;
4198 struct btrfs_trans_handle *trans;
4199 int ret;
4200 int err;
4201 u64 qgroupid;
4202
4203 if (!capable(CAP_SYS_ADMIN))
4204 return -EPERM;
4205
Miao Xie905b0dd2012-11-26 08:50:11 +00004206 ret = mnt_want_write_file(file);
4207 if (ret)
4208 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004209
4210 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004211 if (IS_ERR(sa)) {
4212 ret = PTR_ERR(sa);
4213 goto drop_write;
4214 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004215
4216 trans = btrfs_join_transaction(root);
4217 if (IS_ERR(trans)) {
4218 ret = PTR_ERR(trans);
4219 goto out;
4220 }
4221
4222 qgroupid = sa->qgroupid;
4223 if (!qgroupid) {
4224 /* take the current subvol as qgroup */
4225 qgroupid = root->root_key.objectid;
4226 }
4227
4228 /* FIXME: check if the IDs really exist */
4229 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4230
4231 err = btrfs_end_transaction(trans, root);
4232 if (err && !ret)
4233 ret = err;
4234
4235out:
4236 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004237drop_write:
4238 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004239 return ret;
4240}
4241
Jan Schmidt2f232032013-04-25 16:04:51 +00004242static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4243{
Al Viro6d0379e2013-06-16 19:32:35 +04004244 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004245 struct btrfs_ioctl_quota_rescan_args *qsa;
4246 int ret;
4247
4248 if (!capable(CAP_SYS_ADMIN))
4249 return -EPERM;
4250
4251 ret = mnt_want_write_file(file);
4252 if (ret)
4253 return ret;
4254
4255 qsa = memdup_user(arg, sizeof(*qsa));
4256 if (IS_ERR(qsa)) {
4257 ret = PTR_ERR(qsa);
4258 goto drop_write;
4259 }
4260
4261 if (qsa->flags) {
4262 ret = -EINVAL;
4263 goto out;
4264 }
4265
4266 ret = btrfs_qgroup_rescan(root->fs_info);
4267
4268out:
4269 kfree(qsa);
4270drop_write:
4271 mnt_drop_write_file(file);
4272 return ret;
4273}
4274
4275static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4276{
Al Viro6d0379e2013-06-16 19:32:35 +04004277 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004278 struct btrfs_ioctl_quota_rescan_args *qsa;
4279 int ret = 0;
4280
4281 if (!capable(CAP_SYS_ADMIN))
4282 return -EPERM;
4283
4284 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4285 if (!qsa)
4286 return -ENOMEM;
4287
4288 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4289 qsa->flags = 1;
4290 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4291 }
4292
4293 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4294 ret = -EFAULT;
4295
4296 kfree(qsa);
4297 return ret;
4298}
4299
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004300static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4301{
4302 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4303
4304 if (!capable(CAP_SYS_ADMIN))
4305 return -EPERM;
4306
4307 return btrfs_qgroup_wait_for_completion(root->fs_info);
4308}
4309
Alexander Block8ea05e32012-07-25 17:35:53 +02004310static long btrfs_ioctl_set_received_subvol(struct file *file,
4311 void __user *arg)
4312{
4313 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004314 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004315 struct btrfs_root *root = BTRFS_I(inode)->root;
4316 struct btrfs_root_item *root_item = &root->root_item;
4317 struct btrfs_trans_handle *trans;
4318 struct timespec ct = CURRENT_TIME;
4319 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004320 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004321
4322 ret = mnt_want_write_file(file);
4323 if (ret < 0)
4324 return ret;
4325
4326 down_write(&root->fs_info->subvol_sem);
4327
4328 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4329 ret = -EINVAL;
4330 goto out;
4331 }
4332
4333 if (btrfs_root_readonly(root)) {
4334 ret = -EROFS;
4335 goto out;
4336 }
4337
4338 if (!inode_owner_or_capable(inode)) {
4339 ret = -EACCES;
4340 goto out;
4341 }
4342
4343 sa = memdup_user(arg, sizeof(*sa));
4344 if (IS_ERR(sa)) {
4345 ret = PTR_ERR(sa);
4346 sa = NULL;
4347 goto out;
4348 }
4349
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004350 /*
4351 * 1 - root item
4352 * 2 - uuid items (received uuid + subvol uuid)
4353 */
4354 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004355 if (IS_ERR(trans)) {
4356 ret = PTR_ERR(trans);
4357 trans = NULL;
4358 goto out;
4359 }
4360
4361 sa->rtransid = trans->transid;
4362 sa->rtime.sec = ct.tv_sec;
4363 sa->rtime.nsec = ct.tv_nsec;
4364
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004365 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4366 BTRFS_UUID_SIZE);
4367 if (received_uuid_changed &&
4368 !btrfs_is_empty_uuid(root_item->received_uuid))
4369 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4370 root_item->received_uuid,
4371 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4372 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004373 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4374 btrfs_set_root_stransid(root_item, sa->stransid);
4375 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004376 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4377 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4378 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4379 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004380
4381 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4382 &root->root_key, &root->root_item);
4383 if (ret < 0) {
4384 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004385 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004386 }
4387 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4388 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4389 sa->uuid,
4390 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4391 root->root_key.objectid);
4392 if (ret < 0 && ret != -EEXIST) {
4393 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004394 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004395 }
4396 }
4397 ret = btrfs_commit_transaction(trans, root);
4398 if (ret < 0) {
4399 btrfs_abort_transaction(trans, root, ret);
4400 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004401 }
4402
4403 ret = copy_to_user(arg, sa, sizeof(*sa));
4404 if (ret)
4405 ret = -EFAULT;
4406
4407out:
4408 kfree(sa);
4409 up_write(&root->fs_info->subvol_sem);
4410 mnt_drop_write_file(file);
4411 return ret;
4412}
4413
jeff.liu867ab662013-01-05 02:48:01 +00004414static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4415{
Al Viro6d0379e2013-06-16 19:32:35 +04004416 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004417 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004418 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004419 char label[BTRFS_LABEL_SIZE];
4420
4421 spin_lock(&root->fs_info->super_lock);
4422 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4423 spin_unlock(&root->fs_info->super_lock);
4424
4425 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004426
4427 if (len == BTRFS_LABEL_SIZE) {
4428 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4429 --len);
4430 }
4431
jeff.liu867ab662013-01-05 02:48:01 +00004432 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004433
4434 return ret ? -EFAULT : 0;
4435}
4436
jeff.liua8bfd4a2013-01-05 02:48:08 +00004437static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4438{
Al Viro6d0379e2013-06-16 19:32:35 +04004439 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004440 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4441 struct btrfs_trans_handle *trans;
4442 char label[BTRFS_LABEL_SIZE];
4443 int ret;
4444
4445 if (!capable(CAP_SYS_ADMIN))
4446 return -EPERM;
4447
4448 if (copy_from_user(label, arg, sizeof(label)))
4449 return -EFAULT;
4450
4451 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4452 pr_err("btrfs: unable to set label with more than %d bytes\n",
4453 BTRFS_LABEL_SIZE - 1);
4454 return -EINVAL;
4455 }
4456
4457 ret = mnt_want_write_file(file);
4458 if (ret)
4459 return ret;
4460
jeff.liua8bfd4a2013-01-05 02:48:08 +00004461 trans = btrfs_start_transaction(root, 0);
4462 if (IS_ERR(trans)) {
4463 ret = PTR_ERR(trans);
4464 goto out_unlock;
4465 }
4466
Anand Jaina1b83ac2013-07-19 17:39:32 +08004467 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004468 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004469 spin_unlock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004470 ret = btrfs_end_transaction(trans, root);
4471
4472out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004473 mnt_drop_write_file(file);
4474 return ret;
4475}
4476
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004477long btrfs_ioctl(struct file *file, unsigned int
4478 cmd, unsigned long arg)
4479{
Al Viro496ad9a2013-01-23 17:07:38 -05004480 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004481 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004482
4483 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004484 case FS_IOC_GETFLAGS:
4485 return btrfs_ioctl_getflags(file, argp);
4486 case FS_IOC_SETFLAGS:
4487 return btrfs_ioctl_setflags(file, argp);
4488 case FS_IOC_GETVERSION:
4489 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004490 case FITRIM:
4491 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004492 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004493 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004494 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004495 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004496 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004497 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004498 case BTRFS_IOC_SUBVOL_CREATE_V2:
4499 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004500 case BTRFS_IOC_SNAP_DESTROY:
4501 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004502 case BTRFS_IOC_SUBVOL_GETFLAGS:
4503 return btrfs_ioctl_subvol_getflags(file, argp);
4504 case BTRFS_IOC_SUBVOL_SETFLAGS:
4505 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004506 case BTRFS_IOC_DEFAULT_SUBVOL:
4507 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004508 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004509 return btrfs_ioctl_defrag(file, NULL);
4510 case BTRFS_IOC_DEFRAG_RANGE:
4511 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004512 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004513 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004514 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004515 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004516 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004517 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004518 case BTRFS_IOC_FS_INFO:
4519 return btrfs_ioctl_fs_info(root, argp);
4520 case BTRFS_IOC_DEV_INFO:
4521 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004522 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004523 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004524 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004525 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4526 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004527 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004528 case BTRFS_IOC_TRANS_START:
4529 return btrfs_ioctl_trans_start(file);
4530 case BTRFS_IOC_TRANS_END:
4531 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004532 case BTRFS_IOC_TREE_SEARCH:
4533 return btrfs_ioctl_tree_search(file, argp);
4534 case BTRFS_IOC_INO_LOOKUP:
4535 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004536 case BTRFS_IOC_INO_PATHS:
4537 return btrfs_ioctl_ino_to_path(root, argp);
4538 case BTRFS_IOC_LOGICAL_INO:
4539 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004540 case BTRFS_IOC_SPACE_INFO:
4541 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004542 case BTRFS_IOC_SYNC: {
4543 int ret;
4544
4545 ret = btrfs_start_all_delalloc_inodes(root->fs_info, 0);
4546 if (ret)
4547 return ret;
4548 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4549 return ret;
4550 }
Sage Weil46204592010-10-29 15:41:32 -04004551 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004552 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004553 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004554 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004555 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004556 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004557 case BTRFS_IOC_SCRUB_CANCEL:
4558 return btrfs_ioctl_scrub_cancel(root, argp);
4559 case BTRFS_IOC_SCRUB_PROGRESS:
4560 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004561 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004562 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004563 case BTRFS_IOC_BALANCE_CTL:
4564 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004565 case BTRFS_IOC_BALANCE_PROGRESS:
4566 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004567 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4568 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004569 case BTRFS_IOC_SEND:
4570 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004571 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004572 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004573 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004574 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004575 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004576 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004577 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004578 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004579 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004580 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004581 case BTRFS_IOC_QUOTA_RESCAN:
4582 return btrfs_ioctl_quota_rescan(file, argp);
4583 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4584 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004585 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4586 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004587 case BTRFS_IOC_DEV_REPLACE:
4588 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004589 case BTRFS_IOC_GET_FSLABEL:
4590 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004591 case BTRFS_IOC_SET_FSLABEL:
4592 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004593 case BTRFS_IOC_FILE_EXTENT_SAME:
4594 return btrfs_ioctl_file_extent_same(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004595 }
4596
4597 return -ENOTTY;
4598}