blob: 864aab4b1bfa77ff95eb9c83d7584e6e4b6ed95f [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050047#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040048#include "ctree.h"
49#include "disk-io.h"
50#include "transaction.h"
51#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052#include "print-tree.h"
53#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040054#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080055#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020056#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040057#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020058#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010059#include "dev-replace.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040060
Mark Fasheh416161d2013-08-06 11:42:51 -070061static int btrfs_clone(struct inode *src, struct inode *inode,
62 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
63
Christoph Hellwig6cbff002009-04-17 10:37:41 +020064/* Mask out flags that are inappropriate for the given type of inode. */
65static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
66{
67 if (S_ISDIR(mode))
68 return flags;
69 else if (S_ISREG(mode))
70 return flags & ~FS_DIRSYNC_FL;
71 else
72 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
73}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040074
Christoph Hellwig6cbff002009-04-17 10:37:41 +020075/*
76 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
77 */
78static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
79{
80 unsigned int iflags = 0;
81
82 if (flags & BTRFS_INODE_SYNC)
83 iflags |= FS_SYNC_FL;
84 if (flags & BTRFS_INODE_IMMUTABLE)
85 iflags |= FS_IMMUTABLE_FL;
86 if (flags & BTRFS_INODE_APPEND)
87 iflags |= FS_APPEND_FL;
88 if (flags & BTRFS_INODE_NODUMP)
89 iflags |= FS_NODUMP_FL;
90 if (flags & BTRFS_INODE_NOATIME)
91 iflags |= FS_NOATIME_FL;
92 if (flags & BTRFS_INODE_DIRSYNC)
93 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000094 if (flags & BTRFS_INODE_NODATACOW)
95 iflags |= FS_NOCOW_FL;
96
97 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98 iflags |= FS_COMPR_FL;
99 else if (flags & BTRFS_INODE_NOCOMPRESS)
100 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200101
102 return iflags;
103}
104
105/*
106 * Update inode->i_flags based on the btrfs internal flags.
107 */
108void btrfs_update_iflags(struct inode *inode)
109{
110 struct btrfs_inode *ip = BTRFS_I(inode);
111
112 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
113
114 if (ip->flags & BTRFS_INODE_SYNC)
115 inode->i_flags |= S_SYNC;
116 if (ip->flags & BTRFS_INODE_IMMUTABLE)
117 inode->i_flags |= S_IMMUTABLE;
118 if (ip->flags & BTRFS_INODE_APPEND)
119 inode->i_flags |= S_APPEND;
120 if (ip->flags & BTRFS_INODE_NOATIME)
121 inode->i_flags |= S_NOATIME;
122 if (ip->flags & BTRFS_INODE_DIRSYNC)
123 inode->i_flags |= S_DIRSYNC;
124}
125
126/*
127 * Inherit flags from the parent inode.
128 *
Josef Bacike27425d2011-09-27 11:01:30 -0400129 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200130 */
131void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
132{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400133 unsigned int flags;
134
135 if (!dir)
136 return;
137
138 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200139
Josef Bacike27425d2011-09-27 11:01:30 -0400140 if (flags & BTRFS_INODE_NOCOMPRESS) {
141 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143 } else if (flags & BTRFS_INODE_COMPRESS) {
144 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
146 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147
Liu Bo213490b2012-09-11 08:33:50 -0600148 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400149 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600150 if (S_ISREG(inode->i_mode))
151 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
152 }
Josef Bacike27425d2011-09-27 11:01:30 -0400153
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200154 btrfs_update_iflags(inode);
155}
156
157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
Al Viro496ad9a2013-01-23 17:07:38 -0500159 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
Liu Bo75e7cb72011-03-22 10:12:20 +0000167static int check_flags(unsigned int flags)
168{
169 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
170 FS_NOATIME_FL | FS_NODUMP_FL | \
171 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000172 FS_NOCOMP_FL | FS_COMPR_FL |
173 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000174 return -EOPNOTSUPP;
175
176 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
177 return -EINVAL;
178
Liu Bo75e7cb72011-03-22 10:12:20 +0000179 return 0;
180}
181
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200182static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
183{
Al Viro496ad9a2013-01-23 17:07:38 -0500184 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 struct btrfs_inode *ip = BTRFS_I(inode);
186 struct btrfs_root *root = ip->root;
187 struct btrfs_trans_handle *trans;
188 unsigned int flags, oldflags;
189 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800190 u64 ip_oldflags;
191 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600192 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193
Li Zefanb83cc962010-12-20 16:04:08 +0800194 if (btrfs_root_readonly(root))
195 return -EROFS;
196
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200197 if (copy_from_user(&flags, arg, sizeof(flags)))
198 return -EFAULT;
199
Liu Bo75e7cb72011-03-22 10:12:20 +0000200 ret = check_flags(flags);
201 if (ret)
202 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700204 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200205 return -EACCES;
206
Jan Karae7848682012-06-12 16:20:32 +0200207 ret = mnt_want_write_file(file);
208 if (ret)
209 return ret;
210
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200211 mutex_lock(&inode->i_mutex);
212
Li Zefanf062abf02011-12-29 13:36:45 +0800213 ip_oldflags = ip->flags;
214 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600215 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800216
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200217 flags = btrfs_mask_flags(inode->i_mode, flags);
218 oldflags = btrfs_flags_to_ioctl(ip->flags);
219 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
220 if (!capable(CAP_LINUX_IMMUTABLE)) {
221 ret = -EPERM;
222 goto out_unlock;
223 }
224 }
225
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226 if (flags & FS_SYNC_FL)
227 ip->flags |= BTRFS_INODE_SYNC;
228 else
229 ip->flags &= ~BTRFS_INODE_SYNC;
230 if (flags & FS_IMMUTABLE_FL)
231 ip->flags |= BTRFS_INODE_IMMUTABLE;
232 else
233 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
234 if (flags & FS_APPEND_FL)
235 ip->flags |= BTRFS_INODE_APPEND;
236 else
237 ip->flags &= ~BTRFS_INODE_APPEND;
238 if (flags & FS_NODUMP_FL)
239 ip->flags |= BTRFS_INODE_NODUMP;
240 else
241 ip->flags &= ~BTRFS_INODE_NODUMP;
242 if (flags & FS_NOATIME_FL)
243 ip->flags |= BTRFS_INODE_NOATIME;
244 else
245 ip->flags &= ~BTRFS_INODE_NOATIME;
246 if (flags & FS_DIRSYNC_FL)
247 ip->flags |= BTRFS_INODE_DIRSYNC;
248 else
249 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600250 if (flags & FS_NOCOW_FL) {
251 if (S_ISREG(mode)) {
252 /*
253 * It's safe to turn csums off here, no extents exist.
254 * Otherwise we want the flag to reflect the real COW
255 * status of the file and will not set it.
256 */
257 if (inode->i_size == 0)
258 ip->flags |= BTRFS_INODE_NODATACOW
259 | BTRFS_INODE_NODATASUM;
260 } else {
261 ip->flags |= BTRFS_INODE_NODATACOW;
262 }
263 } else {
264 /*
265 * Revert back under same assuptions as above
266 */
267 if (S_ISREG(mode)) {
268 if (inode->i_size == 0)
269 ip->flags &= ~(BTRFS_INODE_NODATACOW
270 | BTRFS_INODE_NODATASUM);
271 } else {
272 ip->flags &= ~BTRFS_INODE_NODATACOW;
273 }
274 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200275
Liu Bo75e7cb72011-03-22 10:12:20 +0000276 /*
277 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
278 * flag may be changed automatically if compression code won't make
279 * things smaller.
280 */
281 if (flags & FS_NOCOMP_FL) {
282 ip->flags &= ~BTRFS_INODE_COMPRESS;
283 ip->flags |= BTRFS_INODE_NOCOMPRESS;
284 } else if (flags & FS_COMPR_FL) {
285 ip->flags |= BTRFS_INODE_COMPRESS;
286 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000287 } else {
288 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000289 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200290
Li Zefan4da6f1a2011-12-29 13:39:50 +0800291 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800292 if (IS_ERR(trans)) {
293 ret = PTR_ERR(trans);
294 goto out_drop;
295 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200296
Li Zefan306424cc2011-12-14 20:12:02 -0500297 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400298 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500299 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200300 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200302 btrfs_end_transaction(trans, root);
Li Zefanf062abf02011-12-29 13:36:45 +0800303 out_drop:
304 if (ret) {
305 ip->flags = ip_oldflags;
306 inode->i_flags = i_oldflags;
307 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200308
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200309 out_unlock:
310 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200311 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000312 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200313}
314
315static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
316{
Al Viro496ad9a2013-01-23 17:07:38 -0500317 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200318
319 return put_user(inode->i_generation, arg);
320}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400321
Li Dongyangf7039b12011-03-24 10:24:28 +0000322static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
323{
Al Viro815745c2011-11-17 15:40:49 -0500324 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000325 struct btrfs_device *device;
326 struct request_queue *q;
327 struct fstrim_range range;
328 u64 minlen = ULLONG_MAX;
329 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500330 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000331 int ret;
332
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
335
Xiao Guangrong1f781602011-04-20 10:09:16 +0000336 rcu_read_lock();
337 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
338 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000339 if (!device->bdev)
340 continue;
341 q = bdev_get_queue(device->bdev);
342 if (blk_queue_discard(q)) {
343 num_devices++;
344 minlen = min((u64)q->limits.discard_granularity,
345 minlen);
346 }
347 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000348 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200349
Li Dongyangf7039b12011-03-24 10:24:28 +0000350 if (!num_devices)
351 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000352 if (copy_from_user(&range, arg, sizeof(range)))
353 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000354 if (range.start > total_bytes ||
355 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200356 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000357
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200358 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000359 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500360 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000361 if (ret < 0)
362 return ret;
363
364 if (copy_to_user(arg, &range, sizeof(range)))
365 return -EFAULT;
366
367 return 0;
368}
369
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200370int btrfs_is_empty_uuid(u8 *uuid)
371{
372 static char empty_uuid[BTRFS_UUID_SIZE] = {0};
373
374 return !memcmp(uuid, empty_uuid, BTRFS_UUID_SIZE);
375}
376
Miao Xied5c12072013-02-28 10:04:33 +0000377static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400378 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400379 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200380 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000381 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400382{
383 struct btrfs_trans_handle *trans;
384 struct btrfs_key key;
385 struct btrfs_root_item root_item;
386 struct btrfs_inode_item *inode_item;
387 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000388 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400389 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000390 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200391 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392 int ret;
393 int err;
394 u64 objectid;
395 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500396 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000397 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200398 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400399
Li Zefan581bb052011-04-20 10:06:11 +0800400 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400401 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400402 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000403
Miao Xied5c12072013-02-28 10:04:33 +0000404 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400405 /*
Miao Xied5c12072013-02-28 10:04:33 +0000406 * The same as the snapshot creation, please see the comment
407 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400408 */
Miao Xied5c12072013-02-28 10:04:33 +0000409 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200410 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000411 if (ret)
412 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400413
Miao Xied5c12072013-02-28 10:04:33 +0000414 trans = btrfs_start_transaction(root, 0);
415 if (IS_ERR(trans)) {
416 ret = PTR_ERR(trans);
417 goto out;
418 }
419 trans->block_rsv = &block_rsv;
420 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421
Miao Xie8696c532013-02-07 06:02:44 +0000422 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200423 if (ret)
424 goto fail;
425
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400426 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200427 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400428 if (IS_ERR(leaf)) {
429 ret = PTR_ERR(leaf);
430 goto fail;
431 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400432
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400433 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400434 btrfs_set_header_bytenr(leaf, leaf->start);
435 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400437 btrfs_set_header_owner(leaf, objectid);
438
Ross Kirk0a4e5582013-09-24 10:12:38 +0100439 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400440 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400441 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200442 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400443 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400444 btrfs_mark_buffer_dirty(leaf);
445
Alexander Block8ea05e32012-07-25 17:35:53 +0200446 memset(&root_item, 0, sizeof(root_item));
447
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400448 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800449 btrfs_set_stack_inode_generation(inode_item, 1);
450 btrfs_set_stack_inode_size(inode_item, 3);
451 btrfs_set_stack_inode_nlink(inode_item, 1);
452 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
453 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400454
Qu Wenruo3cae2102013-07-16 11:19:18 +0800455 btrfs_set_root_flags(&root_item, 0);
456 btrfs_set_root_limit(&root_item, 0);
457 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000458
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400459 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400460 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400461 btrfs_set_root_level(&root_item, 0);
462 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000463 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400464 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400465
Alexander Block8ea05e32012-07-25 17:35:53 +0200466 btrfs_set_root_generation_v2(&root_item,
467 btrfs_root_generation(&root_item));
468 uuid_le_gen(&new_uuid);
469 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800470 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
471 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200472 root_item.ctime = root_item.otime;
473 btrfs_set_root_ctransid(&root_item, trans->transid);
474 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400475
Chris Mason925baed2008-06-25 16:01:30 -0400476 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400477 free_extent_buffer(leaf);
478 leaf = NULL;
479
480 btrfs_set_root_dirid(&root_item, new_dirid);
481
482 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400483 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400484 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
485 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
486 &root_item);
487 if (ret)
488 goto fail;
489
Yan, Zheng76dda932009-09-21 16:00:26 -0400490 key.offset = (u64)-1;
491 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100492 if (IS_ERR(new_root)) {
493 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
494 ret = PTR_ERR(new_root);
495 goto fail;
496 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400497
498 btrfs_record_root_in_trans(trans, new_root);
499
Josef Bacikd82a6f1d2011-05-11 15:26:06 -0400500 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700501 if (ret) {
502 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100503 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700504 goto fail;
505 }
506
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400507 /*
508 * insert the directory item
509 */
Chris Mason3de45862008-11-17 21:02:50 -0500510 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100511 if (ret) {
512 btrfs_abort_transaction(trans, root, ret);
513 goto fail;
514 }
Chris Mason3de45862008-11-17 21:02:50 -0500515
516 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800517 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500518 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100519 if (ret) {
520 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400521 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100522 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500523
Yan Zheng52c26172009-01-05 15:43:43 -0500524 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
525 ret = btrfs_update_inode(trans, root, dir);
526 BUG_ON(ret);
527
Chris Mason0660b5a2008-11-17 20:37:39 -0500528 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400529 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800530 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500531 BUG_ON(ret);
532
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200533 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
534 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
535 objectid);
536 if (ret)
537 btrfs_abort_transaction(trans, root, ret);
538
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539fail:
Miao Xied5c12072013-02-28 10:04:33 +0000540 trans->block_rsv = NULL;
541 trans->bytes_reserved = 0;
Sage Weil72fd0322010-10-29 15:41:32 -0400542 if (async_transid) {
543 *async_transid = trans->transid;
544 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000545 if (err)
546 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400547 } else {
548 err = btrfs_commit_transaction(trans, root);
549 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400550 if (err && !ret)
551 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500552
553 if (!ret)
554 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Miao Xied5c12072013-02-28 10:04:33 +0000555out:
556 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557 return ret;
558}
559
Miao Xiee9662f72013-02-28 10:01:15 +0000560static int create_snapshot(struct btrfs_root *root, struct inode *dir,
561 struct dentry *dentry, char *name, int namelen,
562 u64 *async_transid, bool readonly,
563 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400564{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000565 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400566 struct btrfs_pending_snapshot *pending_snapshot;
567 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000568 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400569
570 if (!root->ref_cows)
571 return -EINVAL;
572
Miao Xie6a038432013-05-15 07:48:24 +0000573 ret = btrfs_start_delalloc_inodes(root, 0);
574 if (ret)
575 return ret;
576
Josef Bacikf0de1812013-09-17 10:55:51 -0400577 btrfs_wait_ordered_extents(root);
Miao Xie6a038432013-05-15 07:48:24 +0000578
Yan, Zhenga22285a2010-05-16 10:48:46 -0400579 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
580 if (!pending_snapshot)
581 return -ENOMEM;
582
Miao Xie66d8f3d2012-09-06 04:02:28 -0600583 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
584 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000585 /*
586 * 1 - parent dir inode
587 * 2 - dir entries
588 * 1 - root item
589 * 2 - root ref/backref
590 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200591 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000592 */
593 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200594 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400595 &pending_snapshot->qgroup_reserved,
596 false);
Miao Xied5c12072013-02-28 10:04:33 +0000597 if (ret)
598 goto out;
599
Yan, Zhenga22285a2010-05-16 10:48:46 -0400600 pending_snapshot->dentry = dentry;
601 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800602 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000603 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000604 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400605
Miao Xied5c12072013-02-28 10:04:33 +0000606 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400607 if (IS_ERR(trans)) {
608 ret = PTR_ERR(trans);
609 goto fail;
610 }
611
Josef Bacik83515832011-06-14 15:16:14 -0400612 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400613 list_add(&pending_snapshot->list,
614 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400615 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400616 if (async_transid) {
617 *async_transid = trans->transid;
618 ret = btrfs_commit_transaction_async(trans,
619 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000620 if (ret)
621 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400622 } else {
623 ret = btrfs_commit_transaction(trans,
624 root->fs_info->extent_root);
625 }
Miao Xieaec80302013-03-04 09:44:29 +0000626 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400627 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400628
629 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000631 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400632
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500633 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
634 if (ret)
635 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400636
Al Viro2fbe8c82011-07-16 21:38:06 -0400637 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000638 if (IS_ERR(inode)) {
639 ret = PTR_ERR(inode);
640 goto fail;
641 }
642 BUG_ON(!inode);
643 d_instantiate(dentry, inode);
644 ret = 0;
645fail:
Miao Xied5c12072013-02-28 10:04:33 +0000646 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
647 &pending_snapshot->block_rsv,
648 pending_snapshot->qgroup_reserved);
649out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400650 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400651 return ret;
652}
653
Sage Weil4260f7c2010-10-29 15:46:43 -0400654/* copy of check_sticky in fs/namei.c()
655* It's inline, so penalty for filesystems that don't use sticky bit is
656* minimal.
657*/
658static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
659{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800660 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400661
662 if (!(dir->i_mode & S_ISVTX))
663 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800664 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400665 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800666 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400667 return 0;
668 return !capable(CAP_FOWNER);
669}
670
671/* copy of may_delete in fs/namei.c()
672 * Check whether we can remove a link victim from directory dir, check
673 * whether the type of victim is right.
674 * 1. We can't do it if dir is read-only (done in permission())
675 * 2. We should have write and exec permissions on dir
676 * 3. We can't remove anything from append-only dir
677 * 4. We can't do anything with immutable dir (done in permission())
678 * 5. If the sticky bit on dir is set we should either
679 * a. be owner of dir, or
680 * b. be owner of victim, or
681 * c. have CAP_FOWNER capability
682 * 6. If the victim is append-only or immutable we can't do antyhing with
683 * links pointing to it.
684 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
685 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
686 * 9. We can't remove a root or mountpoint.
687 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
688 * nfs_async_unlink().
689 */
690
691static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
692{
693 int error;
694
695 if (!victim->d_inode)
696 return -ENOENT;
697
698 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400699 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400700
701 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
702 if (error)
703 return error;
704 if (IS_APPEND(dir))
705 return -EPERM;
706 if (btrfs_check_sticky(dir, victim->d_inode)||
707 IS_APPEND(victim->d_inode)||
708 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
709 return -EPERM;
710 if (isdir) {
711 if (!S_ISDIR(victim->d_inode->i_mode))
712 return -ENOTDIR;
713 if (IS_ROOT(victim))
714 return -EBUSY;
715 } else if (S_ISDIR(victim->d_inode->i_mode))
716 return -EISDIR;
717 if (IS_DEADDIR(dir))
718 return -ENOENT;
719 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
720 return -EBUSY;
721 return 0;
722}
723
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400724/* copy of may_create in fs/namei.c() */
725static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
726{
727 if (child->d_inode)
728 return -EEXIST;
729 if (IS_DEADDIR(dir))
730 return -ENOENT;
731 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
732}
733
734/*
735 * Create a new subvolume below @parent. This is largely modeled after
736 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
737 * inside this filesystem so it's quite a bit simpler.
738 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400739static noinline int btrfs_mksubvol(struct path *parent,
740 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400741 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200742 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000743 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400744{
Yan, Zheng76dda932009-09-21 16:00:26 -0400745 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400746 struct dentry *dentry;
747 int error;
748
David Sterba5c50c9b2013-03-22 18:12:51 +0000749 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
750 if (error == -EINTR)
751 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400752
753 dentry = lookup_one_len(name, parent->dentry, namelen);
754 error = PTR_ERR(dentry);
755 if (IS_ERR(dentry))
756 goto out_unlock;
757
758 error = -EEXIST;
759 if (dentry->d_inode)
760 goto out_dput;
761
Yan, Zheng76dda932009-09-21 16:00:26 -0400762 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400763 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600764 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400765
Chris Mason9c520572012-12-17 14:26:57 -0500766 /*
767 * even if this name doesn't exist, we may get hash collisions.
768 * check for them now when we can safely fail
769 */
770 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
771 dir->i_ino, name,
772 namelen);
773 if (error)
774 goto out_dput;
775
Yan, Zheng76dda932009-09-21 16:00:26 -0400776 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
777
778 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
779 goto out_up_read;
780
Chris Mason3de45862008-11-17 21:02:50 -0500781 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000782 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200783 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500784 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000785 error = create_subvol(dir, dentry, name, namelen,
786 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500787 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400788 if (!error)
789 fsnotify_mkdir(dir, dentry);
790out_up_read:
791 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400792out_dput:
793 dput(dentry);
794out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400795 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400796 return error;
797}
798
Chris Mason4cb53002011-05-24 15:35:30 -0400799/*
800 * When we're defragging a range, we don't want to kick it off again
801 * if it is really just waiting for delalloc to send it down.
802 * If we find a nice big extent or delalloc range for the bytes in the
803 * file you want to defrag, we return 0 to let you know to skip this
804 * part of the file
805 */
806static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
807{
808 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
809 struct extent_map *em = NULL;
810 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
811 u64 end;
812
813 read_lock(&em_tree->lock);
814 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
815 read_unlock(&em_tree->lock);
816
817 if (em) {
818 end = extent_map_end(em);
819 free_extent_map(em);
820 if (end - offset > thresh)
821 return 0;
822 }
823 /* if we already have a nice delalloc here, just stop */
824 thresh /= 2;
825 end = count_range_bits(io_tree, &offset, offset + thresh,
826 thresh, EXTENT_DELALLOC, 1);
827 if (end >= thresh)
828 return 0;
829 return 1;
830}
831
832/*
833 * helper function to walk through a file and find extents
834 * newer than a specific transid, and smaller than thresh.
835 *
836 * This is used by the defragging code to find new and small
837 * extents
838 */
839static int find_new_extents(struct btrfs_root *root,
840 struct inode *inode, u64 newer_than,
841 u64 *off, int thresh)
842{
843 struct btrfs_path *path;
844 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400845 struct extent_buffer *leaf;
846 struct btrfs_file_extent_item *extent;
847 int type;
848 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000849 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400850
851 path = btrfs_alloc_path();
852 if (!path)
853 return -ENOMEM;
854
David Sterbaa4689d22011-05-31 17:08:14 +0000855 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400856 min_key.type = BTRFS_EXTENT_DATA_KEY;
857 min_key.offset = *off;
858
Chris Mason4cb53002011-05-24 15:35:30 -0400859 path->keep_locks = 1;
860
861 while(1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100862 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400863 if (ret != 0)
864 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000865 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400866 goto none;
867 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
868 goto none;
869
870 leaf = path->nodes[0];
871 extent = btrfs_item_ptr(leaf, path->slots[0],
872 struct btrfs_file_extent_item);
873
874 type = btrfs_file_extent_type(leaf, extent);
875 if (type == BTRFS_FILE_EXTENT_REG &&
876 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
877 check_defrag_in_cache(inode, min_key.offset, thresh)) {
878 *off = min_key.offset;
879 btrfs_free_path(path);
880 return 0;
881 }
882
883 if (min_key.offset == (u64)-1)
884 goto none;
885
886 min_key.offset++;
887 btrfs_release_path(path);
888 }
889none:
890 btrfs_free_path(path);
891 return -ENOENT;
892}
893
Li Zefan6c282eb2012-06-11 16:03:35 +0800894static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400895{
896 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500897 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800898 struct extent_map *em;
899 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500900
901 /*
902 * hopefully we have this extent in the tree already, try without
903 * the full extent lock
904 */
905 read_lock(&em_tree->lock);
906 em = lookup_extent_mapping(em_tree, start, len);
907 read_unlock(&em_tree->lock);
908
909 if (!em) {
910 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100911 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500912 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100913 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500914
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000915 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800916 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500917 }
918
Li Zefan6c282eb2012-06-11 16:03:35 +0800919 return em;
920}
921
922static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
923{
924 struct extent_map *next;
925 bool ret = true;
926
927 /* this is the last extent */
928 if (em->start + em->len >= i_size_read(inode))
929 return false;
930
931 next = defrag_lookup_extent(inode, em->start + em->len);
932 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
933 ret = false;
934
935 free_extent_map(next);
936 return ret;
937}
938
939static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400940 u64 *last_len, u64 *skip, u64 *defrag_end,
941 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800942{
943 struct extent_map *em;
944 int ret = 1;
945 bool next_mergeable = true;
946
947 /*
948 * make sure that once we start defragging an extent, we keep on
949 * defragging it
950 */
951 if (start < *defrag_end)
952 return 1;
953
954 *skip = 0;
955
956 em = defrag_lookup_extent(inode, start);
957 if (!em)
958 return 0;
959
Chris Mason940100a2010-03-10 10:52:59 -0500960 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400961 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500962 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400963 goto out;
964 }
965
Li Zefan6c282eb2012-06-11 16:03:35 +0800966 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500967
968 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800969 * we hit a real extent, if it is big or the next extent is not a
970 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500971 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400972 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800973 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500974 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400975out:
Chris Mason940100a2010-03-10 10:52:59 -0500976 /*
977 * last_len ends up being a counter of how many bytes we've defragged.
978 * every time we choose not to defrag an extent, we reset *last_len
979 * so that the next tiny extent will force a defrag.
980 *
981 * The end result of this is that tiny extents before a single big
982 * extent will force at least part of that big extent to be defragged.
983 */
984 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500985 *defrag_end = extent_map_end(em);
986 } else {
987 *last_len = 0;
988 *skip = extent_map_end(em);
989 *defrag_end = 0;
990 }
991
992 free_extent_map(em);
993 return ret;
994}
995
Chris Mason4cb53002011-05-24 15:35:30 -0400996/*
997 * it doesn't do much good to defrag one or two pages
998 * at a time. This pulls in a nice chunk of pages
999 * to COW and defrag.
1000 *
1001 * It also makes sure the delalloc code has enough
1002 * dirty data to avoid making new small extents as part
1003 * of the defrag
1004 *
1005 * It's a good idea to start RA on this range
1006 * before calling this.
1007 */
1008static int cluster_pages_for_defrag(struct inode *inode,
1009 struct page **pages,
1010 unsigned long start_index,
1011 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001012{
Chris Mason4cb53002011-05-24 15:35:30 -04001013 unsigned long file_end;
1014 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001015 u64 page_start;
1016 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001017 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001018 int ret;
1019 int i;
1020 int i_done;
1021 struct btrfs_ordered_extent *ordered;
1022 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001023 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001024 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001025
Chris Mason4cb53002011-05-24 15:35:30 -04001026 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001027 if (!isize || start_index > file_end)
1028 return 0;
1029
1030 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001031
1032 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001033 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001034 if (ret)
1035 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001036 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001037 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001038
1039 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001040 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001041 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001042again:
Josef Bacika94733d2011-07-11 10:47:06 -04001043 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001044 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001045 if (!page)
1046 break;
1047
Miao Xie600a45e2012-02-16 15:01:24 +08001048 page_start = page_offset(page);
1049 page_end = page_start + PAGE_CACHE_SIZE - 1;
1050 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001051 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001052 ordered = btrfs_lookup_ordered_extent(inode,
1053 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001054 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001055 if (!ordered)
1056 break;
1057
1058 unlock_page(page);
1059 btrfs_start_ordered_extent(inode, ordered, 1);
1060 btrfs_put_ordered_extent(ordered);
1061 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001062 /*
1063 * we unlocked the page above, so we need check if
1064 * it was released or not.
1065 */
1066 if (page->mapping != inode->i_mapping) {
1067 unlock_page(page);
1068 page_cache_release(page);
1069 goto again;
1070 }
Miao Xie600a45e2012-02-16 15:01:24 +08001071 }
1072
Chris Mason4cb53002011-05-24 15:35:30 -04001073 if (!PageUptodate(page)) {
1074 btrfs_readpage(NULL, page);
1075 lock_page(page);
1076 if (!PageUptodate(page)) {
1077 unlock_page(page);
1078 page_cache_release(page);
1079 ret = -EIO;
1080 break;
1081 }
1082 }
Miao Xie600a45e2012-02-16 15:01:24 +08001083
Miao Xie600a45e2012-02-16 15:01:24 +08001084 if (page->mapping != inode->i_mapping) {
1085 unlock_page(page);
1086 page_cache_release(page);
1087 goto again;
1088 }
1089
Chris Mason4cb53002011-05-24 15:35:30 -04001090 pages[i] = page;
1091 i_done++;
1092 }
1093 if (!i_done || ret)
1094 goto out;
1095
1096 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1097 goto out;
1098
1099 /*
1100 * so now we have a nice long stream of locked
1101 * and up to date pages, lets wait on them
1102 */
1103 for (i = 0; i < i_done; i++)
1104 wait_on_page_writeback(pages[i]);
1105
1106 page_start = page_offset(pages[0]);
1107 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1108
1109 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001110 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001111 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1112 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001113 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1114 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001115
Liu Bo1f12bd02012-03-29 09:57:44 -04001116 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001117 spin_lock(&BTRFS_I(inode)->lock);
1118 BTRFS_I(inode)->outstanding_extents++;
1119 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001120 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001121 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001122 }
1123
1124
Liu Bo9e8a4a82012-09-05 19:10:51 -06001125 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1126 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001127
1128 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1129 page_start, page_end - 1, &cached_state,
1130 GFP_NOFS);
1131
1132 for (i = 0; i < i_done; i++) {
1133 clear_page_dirty_for_io(pages[i]);
1134 ClearPageChecked(pages[i]);
1135 set_page_extent_mapped(pages[i]);
1136 set_page_dirty(pages[i]);
1137 unlock_page(pages[i]);
1138 page_cache_release(pages[i]);
1139 }
1140 return i_done;
1141out:
1142 for (i = 0; i < i_done; i++) {
1143 unlock_page(pages[i]);
1144 page_cache_release(pages[i]);
1145 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001146 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001147 return ret;
1148
1149}
1150
1151int btrfs_defrag_file(struct inode *inode, struct file *file,
1152 struct btrfs_ioctl_defrag_range_args *range,
1153 u64 newer_than, unsigned long max_to_defrag)
1154{
1155 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001156 struct file_ra_state *ra = NULL;
1157 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001158 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001159 u64 last_len = 0;
1160 u64 skip = 0;
1161 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001162 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001163 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001164 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001165 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001166 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001167 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001168 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001169 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1170 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001171 u64 new_align = ~((u64)128 * 1024 - 1);
1172 struct page **pages = NULL;
1173
Liu Bo0abd5b12013-04-16 09:20:28 +00001174 if (isize == 0)
1175 return 0;
1176
1177 if (range->start >= isize)
1178 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001179
1180 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1181 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1182 return -EINVAL;
1183 if (range->compress_type)
1184 compress_type = range->compress_type;
1185 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001186
Liu Bo0abd5b12013-04-16 09:20:28 +00001187 if (extent_thresh == 0)
1188 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001189
Chris Mason4cb53002011-05-24 15:35:30 -04001190 /*
1191 * if we were not given a file, allocate a readahead
1192 * context
1193 */
1194 if (!file) {
1195 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1196 if (!ra)
1197 return -ENOMEM;
1198 file_ra_state_init(ra, inode->i_mapping);
1199 } else {
1200 ra = &file->f_ra;
1201 }
1202
Li Zefan008873e2011-09-02 15:57:07 +08001203 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001204 GFP_NOFS);
1205 if (!pages) {
1206 ret = -ENOMEM;
1207 goto out_ra;
1208 }
1209
1210 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001211 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001212 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001213 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1214 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001215 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001216 }
1217
Chris Mason4cb53002011-05-24 15:35:30 -04001218 if (newer_than) {
1219 ret = find_new_extents(root, inode, newer_than,
1220 &newer_off, 64 * 1024);
1221 if (!ret) {
1222 range->start = newer_off;
1223 /*
1224 * we always align our defrag to help keep
1225 * the extents in the file evenly spaced
1226 */
1227 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001228 } else
1229 goto out_ra;
1230 } else {
1231 i = range->start >> PAGE_CACHE_SHIFT;
1232 }
1233 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001234 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001235
Li Zefan2a0f7f52011-10-10 15:43:34 -04001236 /*
1237 * make writeback starts from i, so the defrag range can be
1238 * written sequentially.
1239 */
1240 if (i < inode->i_mapping->writeback_index)
1241 inode->i_mapping->writeback_index = i;
1242
Chris Masonf7f43cc2011-10-11 11:41:40 -04001243 while (i <= last_index && defrag_count < max_to_defrag &&
1244 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1245 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001246 /*
1247 * make sure we stop running if someone unmounts
1248 * the FS
1249 */
1250 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1251 break;
1252
David Sterba210549e2013-02-09 23:38:06 +00001253 if (btrfs_defrag_cancelled(root->fs_info)) {
1254 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1255 ret = -EAGAIN;
1256 break;
1257 }
1258
Liu Bo66c26892012-03-29 09:57:45 -04001259 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001260 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001261 &defrag_end, range->flags &
1262 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001263 unsigned long next;
1264 /*
1265 * the should_defrag function tells us how much to skip
1266 * bump our counter by the suggested amount
1267 */
1268 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1269 i = max(i + 1, next);
1270 continue;
1271 }
Li Zefan008873e2011-09-02 15:57:07 +08001272
1273 if (!newer_than) {
1274 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1275 PAGE_CACHE_SHIFT) - i;
1276 cluster = min(cluster, max_cluster);
1277 } else {
1278 cluster = max_cluster;
1279 }
1280
Li Zefan008873e2011-09-02 15:57:07 +08001281 if (i + cluster > ra_index) {
1282 ra_index = max(i, ra_index);
1283 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1284 cluster);
1285 ra_index += max_cluster;
1286 }
Chris Mason940100a2010-03-10 10:52:59 -05001287
Liu Boecb8bea2012-03-29 09:57:44 -04001288 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001289 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1290 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001291 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001292 if (ret < 0) {
1293 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001294 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001295 }
Chris Mason940100a2010-03-10 10:52:59 -05001296
Chris Mason4cb53002011-05-24 15:35:30 -04001297 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001298 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001299 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001300
1301 if (newer_than) {
1302 if (newer_off == (u64)-1)
1303 break;
1304
Liu Boe1f041e2012-03-29 09:57:45 -04001305 if (ret > 0)
1306 i += ret;
1307
Chris Mason4cb53002011-05-24 15:35:30 -04001308 newer_off = max(newer_off + 1,
1309 (u64)i << PAGE_CACHE_SHIFT);
1310
1311 ret = find_new_extents(root, inode,
1312 newer_than, &newer_off,
1313 64 * 1024);
1314 if (!ret) {
1315 range->start = newer_off;
1316 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001317 } else {
1318 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001319 }
Chris Mason4cb53002011-05-24 15:35:30 -04001320 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001321 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001322 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001323 last_len += ret << PAGE_CACHE_SHIFT;
1324 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001325 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001326 last_len = 0;
1327 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001328 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001329 }
1330
Chris Mason1e701a32010-03-11 09:42:04 -05001331 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1332 filemap_flush(inode->i_mapping);
1333
1334 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1335 /* the filemap_flush will queue IO into the worker threads, but
1336 * we have to make sure the IO is actually started and that
1337 * ordered extents get created before we return
1338 */
1339 atomic_inc(&root->fs_info->async_submit_draining);
1340 while (atomic_read(&root->fs_info->nr_async_submits) ||
1341 atomic_read(&root->fs_info->async_delalloc_pages)) {
1342 wait_event(root->fs_info->async_submit_wait,
1343 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1344 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1345 }
1346 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001347 }
1348
Li Zefan1a419d82010-10-25 15:12:50 +08001349 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001350 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001351 }
1352
Diego Calleja60ccf822011-09-01 16:33:57 +02001353 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001354
Chris Mason4cb53002011-05-24 15:35:30 -04001355out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001356 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1357 mutex_lock(&inode->i_mutex);
1358 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1359 mutex_unlock(&inode->i_mutex);
1360 }
Chris Mason4cb53002011-05-24 15:35:30 -04001361 if (!file)
1362 kfree(ra);
1363 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001364 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001365}
1366
Miao Xie198605a2012-11-26 08:43:45 +00001367static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001368 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001369{
1370 u64 new_size;
1371 u64 old_size;
1372 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001373 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001374 struct btrfs_ioctl_vol_args *vol_args;
1375 struct btrfs_trans_handle *trans;
1376 struct btrfs_device *device = NULL;
1377 char *sizestr;
1378 char *devstr = NULL;
1379 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001380 int mod = 0;
1381
Chris Masone441d542009-01-05 16:57:23 -05001382 if (!capable(CAP_SYS_ADMIN))
1383 return -EPERM;
1384
Miao Xie198605a2012-11-26 08:43:45 +00001385 ret = mnt_want_write_file(file);
1386 if (ret)
1387 return ret;
1388
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001389 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1390 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001391 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001392 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001393 }
1394
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001395 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001396 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001397 if (IS_ERR(vol_args)) {
1398 ret = PTR_ERR(vol_args);
1399 goto out;
1400 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001401
1402 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001403
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001404 sizestr = vol_args->name;
1405 devstr = strchr(sizestr, ':');
1406 if (devstr) {
1407 char *end;
1408 sizestr = devstr + 1;
1409 *devstr = '\0';
1410 devstr = vol_args->name;
1411 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001412 if (!devid) {
1413 ret = -EINVAL;
1414 goto out_free;
1415 }
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001416 printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001417 }
Miao Xiedba60f32012-12-21 09:19:51 +00001418
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001419 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001420 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001421 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001422 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001423 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001424 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001425 }
Miao Xiedba60f32012-12-21 09:19:51 +00001426
1427 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001428 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001429 "readonly device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001430 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001431 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001432 goto out_free;
1433 }
1434
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001435 if (!strcmp(sizestr, "max"))
1436 new_size = device->bdev->bd_inode->i_size;
1437 else {
1438 if (sizestr[0] == '-') {
1439 mod = -1;
1440 sizestr++;
1441 } else if (sizestr[0] == '+') {
1442 mod = 1;
1443 sizestr++;
1444 }
Akinobu Mita91748462010-02-28 10:59:11 +00001445 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001446 if (new_size == 0) {
1447 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001448 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001449 }
1450 }
1451
Stefan Behrens63a212a2012-11-05 18:29:28 +01001452 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001453 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001454 goto out_free;
1455 }
1456
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001457 old_size = device->total_bytes;
1458
1459 if (mod < 0) {
1460 if (new_size > old_size) {
1461 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001462 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001463 }
1464 new_size = old_size - new_size;
1465 } else if (mod > 0) {
1466 new_size = old_size + new_size;
1467 }
1468
1469 if (new_size < 256 * 1024 * 1024) {
1470 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001471 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001472 }
1473 if (new_size > device->bdev->bd_inode->i_size) {
1474 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001475 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001476 }
1477
1478 do_div(new_size, root->sectorsize);
1479 new_size *= root->sectorsize;
1480
Josef Bacik606686e2012-06-04 14:03:51 -04001481 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001482 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001483
1484 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001485 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001486 if (IS_ERR(trans)) {
1487 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001488 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001489 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001490 ret = btrfs_grow_device(trans, device, new_size);
1491 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001492 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001493 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001494 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001495
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001496out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001497 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001498out:
1499 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001500 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001501 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001502 return ret;
1503}
1504
Sage Weil72fd0322010-10-29 15:41:32 -04001505static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001506 char *name, unsigned long fd, int subvol,
1507 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001508 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001509{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001510 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001511 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001512
Liu Boa874a632012-06-29 03:58:46 -06001513 ret = mnt_want_write_file(file);
1514 if (ret)
1515 goto out;
1516
Sage Weil72fd0322010-10-29 15:41:32 -04001517 namelen = strlen(name);
1518 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001519 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001520 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001521 }
1522
Chris Mason16780ca2012-02-20 22:14:55 -05001523 if (name[0] == '.' &&
1524 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1525 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001526 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001527 }
1528
Chris Mason3de45862008-11-17 21:02:50 -05001529 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001530 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001531 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001532 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001533 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001534 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001535 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001536 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001537 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001538 }
1539
Al Viro496ad9a2013-01-23 17:07:38 -05001540 src_inode = file_inode(src.file);
1541 if (src_inode->i_sb != file_inode(file)->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001542 printk(KERN_INFO "btrfs: Snapshot src from "
1543 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001544 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001545 } else {
1546 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1547 BTRFS_I(src_inode)->root,
1548 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001549 }
Al Viro2903ff02012-08-28 12:52:22 -04001550 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001551 }
Liu Boa874a632012-06-29 03:58:46 -06001552out_drop_write:
1553 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001554out:
Sage Weil72fd0322010-10-29 15:41:32 -04001555 return ret;
1556}
1557
1558static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001559 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001560{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001561 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001562 int ret;
1563
Li Zefanfa0d2b92010-12-20 15:53:28 +08001564 vol_args = memdup_user(arg, sizeof(*vol_args));
1565 if (IS_ERR(vol_args))
1566 return PTR_ERR(vol_args);
1567 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001568
Li Zefanfa0d2b92010-12-20 15:53:28 +08001569 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001570 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001571 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001572
Li Zefanfa0d2b92010-12-20 15:53:28 +08001573 kfree(vol_args);
1574 return ret;
1575}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001576
Li Zefanfa0d2b92010-12-20 15:53:28 +08001577static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1578 void __user *arg, int subvol)
1579{
1580 struct btrfs_ioctl_vol_args_v2 *vol_args;
1581 int ret;
1582 u64 transid = 0;
1583 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001584 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001585 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001586
Li Zefanfa0d2b92010-12-20 15:53:28 +08001587 vol_args = memdup_user(arg, sizeof(*vol_args));
1588 if (IS_ERR(vol_args))
1589 return PTR_ERR(vol_args);
1590 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001591
Li Zefanb83cc962010-12-20 16:04:08 +08001592 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001593 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1594 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001595 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001596 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001597 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001598
1599 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1600 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001601 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1602 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001603 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1604 if (vol_args->size > PAGE_CACHE_SIZE) {
1605 ret = -EINVAL;
1606 goto out;
1607 }
1608 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1609 if (IS_ERR(inherit)) {
1610 ret = PTR_ERR(inherit);
1611 goto out;
1612 }
1613 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001614
1615 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001616 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001617 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001618
1619 if (ret == 0 && ptr &&
1620 copy_to_user(arg +
1621 offsetof(struct btrfs_ioctl_vol_args_v2,
1622 transid), ptr, sizeof(*ptr)))
1623 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001624out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001625 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001626 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001627 return ret;
1628}
1629
Li Zefan0caa1022010-12-20 16:30:25 +08001630static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1631 void __user *arg)
1632{
Al Viro496ad9a2013-01-23 17:07:38 -05001633 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001634 struct btrfs_root *root = BTRFS_I(inode)->root;
1635 int ret = 0;
1636 u64 flags = 0;
1637
Li Zefan33345d012011-04-20 10:31:50 +08001638 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001639 return -EINVAL;
1640
1641 down_read(&root->fs_info->subvol_sem);
1642 if (btrfs_root_readonly(root))
1643 flags |= BTRFS_SUBVOL_RDONLY;
1644 up_read(&root->fs_info->subvol_sem);
1645
1646 if (copy_to_user(arg, &flags, sizeof(flags)))
1647 ret = -EFAULT;
1648
1649 return ret;
1650}
1651
1652static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1653 void __user *arg)
1654{
Al Viro496ad9a2013-01-23 17:07:38 -05001655 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001656 struct btrfs_root *root = BTRFS_I(inode)->root;
1657 struct btrfs_trans_handle *trans;
1658 u64 root_flags;
1659 u64 flags;
1660 int ret = 0;
1661
Liu Bob9ca0662012-06-29 03:58:49 -06001662 ret = mnt_want_write_file(file);
1663 if (ret)
1664 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001665
Liu Bob9ca0662012-06-29 03:58:49 -06001666 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1667 ret = -EINVAL;
1668 goto out_drop_write;
1669 }
Li Zefan0caa1022010-12-20 16:30:25 +08001670
Liu Bob9ca0662012-06-29 03:58:49 -06001671 if (copy_from_user(&flags, arg, sizeof(flags))) {
1672 ret = -EFAULT;
1673 goto out_drop_write;
1674 }
Li Zefan0caa1022010-12-20 16:30:25 +08001675
Liu Bob9ca0662012-06-29 03:58:49 -06001676 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1677 ret = -EINVAL;
1678 goto out_drop_write;
1679 }
Li Zefan0caa1022010-12-20 16:30:25 +08001680
Liu Bob9ca0662012-06-29 03:58:49 -06001681 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1682 ret = -EOPNOTSUPP;
1683 goto out_drop_write;
1684 }
Li Zefan0caa1022010-12-20 16:30:25 +08001685
Liu Bob9ca0662012-06-29 03:58:49 -06001686 if (!inode_owner_or_capable(inode)) {
1687 ret = -EACCES;
1688 goto out_drop_write;
1689 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001690
Li Zefan0caa1022010-12-20 16:30:25 +08001691 down_write(&root->fs_info->subvol_sem);
1692
1693 /* nothing to do */
1694 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001695 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001696
1697 root_flags = btrfs_root_flags(&root->root_item);
1698 if (flags & BTRFS_SUBVOL_RDONLY)
1699 btrfs_set_root_flags(&root->root_item,
1700 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1701 else
1702 btrfs_set_root_flags(&root->root_item,
1703 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1704
1705 trans = btrfs_start_transaction(root, 1);
1706 if (IS_ERR(trans)) {
1707 ret = PTR_ERR(trans);
1708 goto out_reset;
1709 }
1710
Li Zefanb4dc2b82011-02-16 06:06:34 +00001711 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001712 &root->root_key, &root->root_item);
1713
1714 btrfs_commit_transaction(trans, root);
1715out_reset:
1716 if (ret)
1717 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001718out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001719 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001720out_drop_write:
1721 mnt_drop_write_file(file);
1722out:
Li Zefan0caa1022010-12-20 16:30:25 +08001723 return ret;
1724}
1725
Yan, Zheng76dda932009-09-21 16:00:26 -04001726/*
1727 * helper to check if the subvolume references other subvolumes
1728 */
1729static noinline int may_destroy_subvol(struct btrfs_root *root)
1730{
1731 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001732 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001733 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001734 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001735 int ret;
1736
1737 path = btrfs_alloc_path();
1738 if (!path)
1739 return -ENOMEM;
1740
Josef Bacik175a2b82013-08-12 15:36:44 -04001741 /* Make sure this root isn't set as the default subvol */
1742 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1743 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1744 dir_id, "default", 7, 0);
1745 if (di && !IS_ERR(di)) {
1746 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1747 if (key.objectid == root->root_key.objectid) {
1748 ret = -ENOTEMPTY;
1749 goto out;
1750 }
1751 btrfs_release_path(path);
1752 }
1753
Yan, Zheng76dda932009-09-21 16:00:26 -04001754 key.objectid = root->root_key.objectid;
1755 key.type = BTRFS_ROOT_REF_KEY;
1756 key.offset = (u64)-1;
1757
1758 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1759 &key, path, 0, 0);
1760 if (ret < 0)
1761 goto out;
1762 BUG_ON(ret == 0);
1763
1764 ret = 0;
1765 if (path->slots[0] > 0) {
1766 path->slots[0]--;
1767 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1768 if (key.objectid == root->root_key.objectid &&
1769 key.type == BTRFS_ROOT_REF_KEY)
1770 ret = -ENOTEMPTY;
1771 }
1772out:
1773 btrfs_free_path(path);
1774 return ret;
1775}
1776
Chris Masonac8e9812010-02-28 15:39:26 -05001777static noinline int key_in_sk(struct btrfs_key *key,
1778 struct btrfs_ioctl_search_key *sk)
1779{
Chris Masonabc6e132010-03-18 12:10:08 -04001780 struct btrfs_key test;
1781 int ret;
1782
1783 test.objectid = sk->min_objectid;
1784 test.type = sk->min_type;
1785 test.offset = sk->min_offset;
1786
1787 ret = btrfs_comp_cpu_keys(key, &test);
1788 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001789 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001790
1791 test.objectid = sk->max_objectid;
1792 test.type = sk->max_type;
1793 test.offset = sk->max_offset;
1794
1795 ret = btrfs_comp_cpu_keys(key, &test);
1796 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001797 return 0;
1798 return 1;
1799}
1800
1801static noinline int copy_to_sk(struct btrfs_root *root,
1802 struct btrfs_path *path,
1803 struct btrfs_key *key,
1804 struct btrfs_ioctl_search_key *sk,
1805 char *buf,
1806 unsigned long *sk_offset,
1807 int *num_found)
1808{
1809 u64 found_transid;
1810 struct extent_buffer *leaf;
1811 struct btrfs_ioctl_search_header sh;
1812 unsigned long item_off;
1813 unsigned long item_len;
1814 int nritems;
1815 int i;
1816 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001817 int ret = 0;
1818
1819 leaf = path->nodes[0];
1820 slot = path->slots[0];
1821 nritems = btrfs_header_nritems(leaf);
1822
1823 if (btrfs_header_generation(leaf) > sk->max_transid) {
1824 i = nritems;
1825 goto advance_key;
1826 }
1827 found_transid = btrfs_header_generation(leaf);
1828
1829 for (i = slot; i < nritems; i++) {
1830 item_off = btrfs_item_ptr_offset(leaf, i);
1831 item_len = btrfs_item_size_nr(leaf, i);
1832
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001833 btrfs_item_key_to_cpu(leaf, key, i);
1834 if (!key_in_sk(key, sk))
1835 continue;
1836
1837 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001838 item_len = 0;
1839
1840 if (sizeof(sh) + item_len + *sk_offset >
1841 BTRFS_SEARCH_ARGS_BUFSIZE) {
1842 ret = 1;
1843 goto overflow;
1844 }
1845
Chris Masonac8e9812010-02-28 15:39:26 -05001846 sh.objectid = key->objectid;
1847 sh.offset = key->offset;
1848 sh.type = key->type;
1849 sh.len = item_len;
1850 sh.transid = found_transid;
1851
1852 /* copy search result header */
1853 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1854 *sk_offset += sizeof(sh);
1855
1856 if (item_len) {
1857 char *p = buf + *sk_offset;
1858 /* copy the item */
1859 read_extent_buffer(leaf, p,
1860 item_off, item_len);
1861 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001862 }
Hugo Millse2156862011-05-14 17:43:41 +00001863 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001864
1865 if (*num_found >= sk->nr_items)
1866 break;
1867 }
1868advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001869 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001870 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1871 key->offset++;
1872 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1873 key->offset = 0;
1874 key->type++;
1875 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1876 key->offset = 0;
1877 key->type = 0;
1878 key->objectid++;
1879 } else
1880 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001881overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001882 return ret;
1883}
1884
1885static noinline int search_ioctl(struct inode *inode,
1886 struct btrfs_ioctl_search_args *args)
1887{
1888 struct btrfs_root *root;
1889 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001890 struct btrfs_path *path;
1891 struct btrfs_ioctl_search_key *sk = &args->key;
1892 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1893 int ret;
1894 int num_found = 0;
1895 unsigned long sk_offset = 0;
1896
1897 path = btrfs_alloc_path();
1898 if (!path)
1899 return -ENOMEM;
1900
1901 if (sk->tree_id == 0) {
1902 /* search the root of the inode that was passed */
1903 root = BTRFS_I(inode)->root;
1904 } else {
1905 key.objectid = sk->tree_id;
1906 key.type = BTRFS_ROOT_ITEM_KEY;
1907 key.offset = (u64)-1;
1908 root = btrfs_read_fs_root_no_name(info, &key);
1909 if (IS_ERR(root)) {
1910 printk(KERN_ERR "could not find root %llu\n",
1911 sk->tree_id);
1912 btrfs_free_path(path);
1913 return -ENOENT;
1914 }
1915 }
1916
1917 key.objectid = sk->min_objectid;
1918 key.type = sk->min_type;
1919 key.offset = sk->min_offset;
1920
Chris Masonac8e9812010-02-28 15:39:26 -05001921 path->keep_locks = 1;
1922
1923 while(1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001924 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05001925 if (ret != 0) {
1926 if (ret > 0)
1927 ret = 0;
1928 goto err;
1929 }
1930 ret = copy_to_sk(root, path, &key, sk, args->buf,
1931 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001932 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001933 if (ret || num_found >= sk->nr_items)
1934 break;
1935
1936 }
1937 ret = 0;
1938err:
1939 sk->nr_items = num_found;
1940 btrfs_free_path(path);
1941 return ret;
1942}
1943
1944static noinline int btrfs_ioctl_tree_search(struct file *file,
1945 void __user *argp)
1946{
1947 struct btrfs_ioctl_search_args *args;
1948 struct inode *inode;
1949 int ret;
1950
1951 if (!capable(CAP_SYS_ADMIN))
1952 return -EPERM;
1953
Julia Lawall2354d08f2010-10-29 15:14:18 -04001954 args = memdup_user(argp, sizeof(*args));
1955 if (IS_ERR(args))
1956 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001957
Al Viro496ad9a2013-01-23 17:07:38 -05001958 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001959 ret = search_ioctl(inode, args);
1960 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1961 ret = -EFAULT;
1962 kfree(args);
1963 return ret;
1964}
1965
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001966/*
Chris Masonac8e9812010-02-28 15:39:26 -05001967 * Search INODE_REFs to identify path name of 'dirid' directory
1968 * in a 'tree_id' tree. and sets path name to 'name'.
1969 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001970static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1971 u64 tree_id, u64 dirid, char *name)
1972{
1973 struct btrfs_root *root;
1974 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001975 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001976 int ret = -1;
1977 int slot;
1978 int len;
1979 int total_len = 0;
1980 struct btrfs_inode_ref *iref;
1981 struct extent_buffer *l;
1982 struct btrfs_path *path;
1983
1984 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1985 name[0]='\0';
1986 return 0;
1987 }
1988
1989 path = btrfs_alloc_path();
1990 if (!path)
1991 return -ENOMEM;
1992
Chris Masonac8e9812010-02-28 15:39:26 -05001993 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001994
1995 key.objectid = tree_id;
1996 key.type = BTRFS_ROOT_ITEM_KEY;
1997 key.offset = (u64)-1;
1998 root = btrfs_read_fs_root_no_name(info, &key);
1999 if (IS_ERR(root)) {
2000 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002001 ret = -ENOENT;
2002 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002003 }
2004
2005 key.objectid = dirid;
2006 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002007 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002008
2009 while(1) {
2010 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2011 if (ret < 0)
2012 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002013 else if (ret > 0) {
2014 ret = btrfs_previous_item(root, path, dirid,
2015 BTRFS_INODE_REF_KEY);
2016 if (ret < 0)
2017 goto out;
2018 else if (ret > 0) {
2019 ret = -ENOENT;
2020 goto out;
2021 }
2022 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002023
2024 l = path->nodes[0];
2025 slot = path->slots[0];
2026 btrfs_item_key_to_cpu(l, &key, slot);
2027
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002028 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2029 len = btrfs_inode_ref_name_len(l, iref);
2030 ptr -= len + 1;
2031 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002032 if (ptr < name) {
2033 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002034 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002035 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002036
2037 *(ptr + len) = '/';
2038 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
2039
2040 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2041 break;
2042
David Sterbab3b4aa72011-04-21 01:20:15 +02002043 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002044 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002045 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002046 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002047 }
Li Zefan77906a502011-07-14 03:16:00 +00002048 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002049 name[total_len]='\0';
2050 ret = 0;
2051out:
2052 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002053 return ret;
2054}
2055
2056static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2057 void __user *argp)
2058{
2059 struct btrfs_ioctl_ino_lookup_args *args;
2060 struct inode *inode;
2061 int ret;
2062
2063 if (!capable(CAP_SYS_ADMIN))
2064 return -EPERM;
2065
Julia Lawall2354d08f2010-10-29 15:14:18 -04002066 args = memdup_user(argp, sizeof(*args));
2067 if (IS_ERR(args))
2068 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002069
Al Viro496ad9a2013-01-23 17:07:38 -05002070 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002071
Chris Mason1b53ac42010-03-18 12:17:05 -04002072 if (args->treeid == 0)
2073 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2074
Chris Masonac8e9812010-02-28 15:39:26 -05002075 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2076 args->treeid, args->objectid,
2077 args->name);
2078
2079 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2080 ret = -EFAULT;
2081
2082 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002083 return ret;
2084}
2085
Yan, Zheng76dda932009-09-21 16:00:26 -04002086static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2087 void __user *arg)
2088{
2089 struct dentry *parent = fdentry(file);
2090 struct dentry *dentry;
2091 struct inode *dir = parent->d_inode;
2092 struct inode *inode;
2093 struct btrfs_root *root = BTRFS_I(dir)->root;
2094 struct btrfs_root *dest = NULL;
2095 struct btrfs_ioctl_vol_args *vol_args;
2096 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002097 struct btrfs_block_rsv block_rsv;
2098 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002099 int namelen;
2100 int ret;
2101 int err = 0;
2102
Yan, Zheng76dda932009-09-21 16:00:26 -04002103 vol_args = memdup_user(arg, sizeof(*vol_args));
2104 if (IS_ERR(vol_args))
2105 return PTR_ERR(vol_args);
2106
2107 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2108 namelen = strlen(vol_args->name);
2109 if (strchr(vol_args->name, '/') ||
2110 strncmp(vol_args->name, "..", namelen) == 0) {
2111 err = -EINVAL;
2112 goto out;
2113 }
2114
Al Viroa561be72011-11-23 11:57:51 -05002115 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002116 if (err)
2117 goto out;
2118
David Sterba5c50c9b2013-03-22 18:12:51 +00002119 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2120 if (err == -EINTR)
2121 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002122 dentry = lookup_one_len(vol_args->name, parent, namelen);
2123 if (IS_ERR(dentry)) {
2124 err = PTR_ERR(dentry);
2125 goto out_unlock_dir;
2126 }
2127
2128 if (!dentry->d_inode) {
2129 err = -ENOENT;
2130 goto out_dput;
2131 }
2132
2133 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002134 dest = BTRFS_I(inode)->root;
2135 if (!capable(CAP_SYS_ADMIN)){
2136 /*
2137 * Regular user. Only allow this with a special mount
2138 * option, when the user has write+exec access to the
2139 * subvol root, and when rmdir(2) would have been
2140 * allowed.
2141 *
2142 * Note that this is _not_ check that the subvol is
2143 * empty or doesn't contain data that we wouldn't
2144 * otherwise be able to delete.
2145 *
2146 * Users who want to delete empty subvols should try
2147 * rmdir(2).
2148 */
2149 err = -EPERM;
2150 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2151 goto out_dput;
2152
2153 /*
2154 * Do not allow deletion if the parent dir is the same
2155 * as the dir to be deleted. That means the ioctl
2156 * must be called on the dentry referencing the root
2157 * of the subvol, not a random directory contained
2158 * within it.
2159 */
2160 err = -EINVAL;
2161 if (root == dest)
2162 goto out_dput;
2163
2164 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2165 if (err)
2166 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002167 }
2168
Miao Xie5c39da52012-10-22 11:39:53 +00002169 /* check if subvolume may be deleted by a user */
2170 err = btrfs_may_delete(dir, dentry, 1);
2171 if (err)
2172 goto out_dput;
2173
Li Zefan33345d012011-04-20 10:31:50 +08002174 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002175 err = -EINVAL;
2176 goto out_dput;
2177 }
2178
Yan, Zheng76dda932009-09-21 16:00:26 -04002179 mutex_lock(&inode->i_mutex);
2180 err = d_invalidate(dentry);
2181 if (err)
2182 goto out_unlock;
2183
2184 down_write(&root->fs_info->subvol_sem);
2185
2186 err = may_destroy_subvol(dest);
2187 if (err)
2188 goto out_up_write;
2189
Miao Xiec58aaad2013-02-28 10:05:36 +00002190 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2191 /*
2192 * One for dir inode, two for dir entries, two for root
2193 * ref/backref.
2194 */
2195 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002196 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002197 if (err)
2198 goto out_up_write;
2199
Yan, Zhenga22285a2010-05-16 10:48:46 -04002200 trans = btrfs_start_transaction(root, 0);
2201 if (IS_ERR(trans)) {
2202 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002203 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002204 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002205 trans->block_rsv = &block_rsv;
2206 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002207
Yan, Zheng76dda932009-09-21 16:00:26 -04002208 ret = btrfs_unlink_subvol(trans, root, dir,
2209 dest->root_key.objectid,
2210 dentry->d_name.name,
2211 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002212 if (ret) {
2213 err = ret;
2214 btrfs_abort_transaction(trans, root, ret);
2215 goto out_end_trans;
2216 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002217
2218 btrfs_record_root_in_trans(trans, dest);
2219
2220 memset(&dest->root_item.drop_progress, 0,
2221 sizeof(dest->root_item.drop_progress));
2222 dest->root_item.drop_level = 0;
2223 btrfs_set_root_refs(&dest->root_item, 0);
2224
Yan, Zhengd68fc572010-05-16 10:49:58 -04002225 if (!xchg(&dest->orphan_item_inserted, 1)) {
2226 ret = btrfs_insert_orphan_item(trans,
2227 root->fs_info->tree_root,
2228 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002229 if (ret) {
2230 btrfs_abort_transaction(trans, root, ret);
2231 err = ret;
2232 goto out_end_trans;
2233 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002234 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002235
2236 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2237 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2238 dest->root_key.objectid);
2239 if (ret && ret != -ENOENT) {
2240 btrfs_abort_transaction(trans, root, ret);
2241 err = ret;
2242 goto out_end_trans;
2243 }
2244 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2245 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2246 dest->root_item.received_uuid,
2247 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2248 dest->root_key.objectid);
2249 if (ret && ret != -ENOENT) {
2250 btrfs_abort_transaction(trans, root, ret);
2251 err = ret;
2252 goto out_end_trans;
2253 }
2254 }
2255
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002256out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002257 trans->block_rsv = NULL;
2258 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002259 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002260 if (ret && !err)
2261 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002262 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002263out_release:
2264 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002265out_up_write:
2266 up_write(&root->fs_info->subvol_sem);
2267out_unlock:
2268 mutex_unlock(&inode->i_mutex);
2269 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002270 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002271 btrfs_invalidate_inodes(dest);
2272 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002273
2274 /* the last ref */
2275 if (dest->cache_inode) {
2276 iput(dest->cache_inode);
2277 dest->cache_inode = NULL;
2278 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002279 }
2280out_dput:
2281 dput(dentry);
2282out_unlock_dir:
2283 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002284 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002285out:
2286 kfree(vol_args);
2287 return err;
2288}
2289
Chris Mason1e701a32010-03-11 09:42:04 -05002290static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002291{
Al Viro496ad9a2013-01-23 17:07:38 -05002292 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002293 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002294 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002295 int ret;
2296
Ilya Dryomov25122d12013-01-20 15:57:57 +02002297 ret = mnt_want_write_file(file);
2298 if (ret)
2299 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002300
Ilya Dryomov25122d12013-01-20 15:57:57 +02002301 if (btrfs_root_readonly(root)) {
2302 ret = -EROFS;
2303 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002304 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002305
2306 switch (inode->i_mode & S_IFMT) {
2307 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002308 if (!capable(CAP_SYS_ADMIN)) {
2309 ret = -EPERM;
2310 goto out;
2311 }
Eric Sandeende78b512013-01-31 18:21:12 +00002312 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002313 if (ret)
2314 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002315 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002316 break;
2317 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002318 if (!(file->f_mode & FMODE_WRITE)) {
2319 ret = -EINVAL;
2320 goto out;
2321 }
Chris Mason1e701a32010-03-11 09:42:04 -05002322
2323 range = kzalloc(sizeof(*range), GFP_KERNEL);
2324 if (!range) {
2325 ret = -ENOMEM;
2326 goto out;
2327 }
2328
2329 if (argp) {
2330 if (copy_from_user(range, argp,
2331 sizeof(*range))) {
2332 ret = -EFAULT;
2333 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002334 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002335 }
2336 /* compression requires us to start the IO */
2337 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2338 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2339 range->extent_thresh = (u32)-1;
2340 }
2341 } else {
2342 /* the rest are all set to zero by kzalloc */
2343 range->len = (u64)-1;
2344 }
Al Viro496ad9a2013-01-23 17:07:38 -05002345 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002346 range, 0, 0);
2347 if (ret > 0)
2348 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002349 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002350 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002351 default:
2352 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002353 }
Chris Masone441d542009-01-05 16:57:23 -05002354out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002355 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002356 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002357}
2358
Christoph Hellwigb2950862008-12-02 09:54:17 -05002359static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002360{
2361 struct btrfs_ioctl_vol_args *vol_args;
2362 int ret;
2363
Chris Masone441d542009-01-05 16:57:23 -05002364 if (!capable(CAP_SYS_ADMIN))
2365 return -EPERM;
2366
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002367 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2368 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002369 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002370 }
2371
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002372 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002373 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002374 if (IS_ERR(vol_args)) {
2375 ret = PTR_ERR(vol_args);
2376 goto out;
2377 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002378
Mark Fasheh5516e592008-07-24 12:20:14 -04002379 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002380 ret = btrfs_init_new_device(root, vol_args->name);
2381
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002382 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002383out:
2384 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002385 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002386 return ret;
2387}
2388
Miao Xieda249272012-11-26 08:44:50 +00002389static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002390{
Al Viro496ad9a2013-01-23 17:07:38 -05002391 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002392 struct btrfs_ioctl_vol_args *vol_args;
2393 int ret;
2394
Chris Masone441d542009-01-05 16:57:23 -05002395 if (!capable(CAP_SYS_ADMIN))
2396 return -EPERM;
2397
Miao Xieda249272012-11-26 08:44:50 +00002398 ret = mnt_want_write_file(file);
2399 if (ret)
2400 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002401
Li Zefandae7b662009-04-08 15:06:54 +08002402 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002403 if (IS_ERR(vol_args)) {
2404 ret = PTR_ERR(vol_args);
2405 goto out;
2406 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002407
Mark Fasheh5516e592008-07-24 12:20:14 -04002408 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002409
Anand Jain183860f2013-05-17 10:52:45 +00002410 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2411 1)) {
2412 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2413 goto out;
2414 }
2415
2416 mutex_lock(&root->fs_info->volume_mutex);
2417 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002418 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002419 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002420
2421out:
2422 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002423 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002424 return ret;
2425}
2426
Jan Schmidt475f6382011-03-11 15:41:01 +01002427static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2428{
Li Zefan027ed2f2011-06-08 08:27:56 +00002429 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002430 struct btrfs_device *device;
2431 struct btrfs_device *next;
2432 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002433 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002434
2435 if (!capable(CAP_SYS_ADMIN))
2436 return -EPERM;
2437
Li Zefan027ed2f2011-06-08 08:27:56 +00002438 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2439 if (!fi_args)
2440 return -ENOMEM;
2441
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002442 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002443 fi_args->num_devices = fs_devices->num_devices;
2444 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002445
Jan Schmidt475f6382011-03-11 15:41:01 +01002446 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002447 if (device->devid > fi_args->max_id)
2448 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002449 }
2450 mutex_unlock(&fs_devices->device_list_mutex);
2451
Li Zefan027ed2f2011-06-08 08:27:56 +00002452 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2453 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002454
Li Zefan027ed2f2011-06-08 08:27:56 +00002455 kfree(fi_args);
2456 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002457}
2458
2459static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2460{
2461 struct btrfs_ioctl_dev_info_args *di_args;
2462 struct btrfs_device *dev;
2463 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2464 int ret = 0;
2465 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002466
2467 if (!capable(CAP_SYS_ADMIN))
2468 return -EPERM;
2469
2470 di_args = memdup_user(arg, sizeof(*di_args));
2471 if (IS_ERR(di_args))
2472 return PTR_ERR(di_args);
2473
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002474 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002475 s_uuid = di_args->uuid;
2476
2477 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002478 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002479
2480 if (!dev) {
2481 ret = -ENODEV;
2482 goto out;
2483 }
2484
2485 di_args->devid = dev->devid;
2486 di_args->bytes_used = dev->bytes_used;
2487 di_args->total_bytes = dev->total_bytes;
2488 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002489 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002490 struct rcu_string *name;
2491
2492 rcu_read_lock();
2493 name = rcu_dereference(dev->name);
2494 strncpy(di_args->path, name->str, sizeof(di_args->path));
2495 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002496 di_args->path[sizeof(di_args->path) - 1] = 0;
2497 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002498 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002499 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002500
2501out:
David Sterba55793c02013-04-26 15:20:23 +00002502 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002503 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2504 ret = -EFAULT;
2505
2506 kfree(di_args);
2507 return ret;
2508}
2509
Mark Fasheh416161d2013-08-06 11:42:51 -07002510static struct page *extent_same_get_page(struct inode *inode, u64 off)
2511{
2512 struct page *page;
2513 pgoff_t index;
2514 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2515
2516 index = off >> PAGE_CACHE_SHIFT;
2517
2518 page = grab_cache_page(inode->i_mapping, index);
2519 if (!page)
2520 return NULL;
2521
2522 if (!PageUptodate(page)) {
2523 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2524 0))
2525 return NULL;
2526 lock_page(page);
2527 if (!PageUptodate(page)) {
2528 unlock_page(page);
2529 page_cache_release(page);
2530 return NULL;
2531 }
2532 }
2533 unlock_page(page);
2534
2535 return page;
2536}
2537
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002538static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2539{
2540 /* do any pending delalloc/csum calc on src, one way or
2541 another, and lock file content */
2542 while (1) {
2543 struct btrfs_ordered_extent *ordered;
2544 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2545 ordered = btrfs_lookup_first_ordered_extent(inode,
2546 off + len - 1);
2547 if (!ordered &&
2548 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2549 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2550 break;
2551 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2552 if (ordered)
2553 btrfs_put_ordered_extent(ordered);
2554 btrfs_wait_ordered_range(inode, off, len);
2555 }
2556}
2557
Mark Fasheh416161d2013-08-06 11:42:51 -07002558static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2559 struct inode *inode2, u64 loff2, u64 len)
2560{
2561 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2562 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2563
2564 mutex_unlock(&inode1->i_mutex);
2565 mutex_unlock(&inode2->i_mutex);
2566}
2567
2568static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2569 struct inode *inode2, u64 loff2, u64 len)
2570{
2571 if (inode1 < inode2) {
2572 swap(inode1, inode2);
2573 swap(loff1, loff2);
2574 }
2575
2576 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2577 lock_extent_range(inode1, loff1, len);
2578 if (inode1 != inode2) {
2579 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2580 lock_extent_range(inode2, loff2, len);
2581 }
2582}
2583
2584static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2585 u64 dst_loff, u64 len)
2586{
2587 int ret = 0;
2588 struct page *src_page, *dst_page;
2589 unsigned int cmp_len = PAGE_CACHE_SIZE;
2590 void *addr, *dst_addr;
2591
2592 while (len) {
2593 if (len < PAGE_CACHE_SIZE)
2594 cmp_len = len;
2595
2596 src_page = extent_same_get_page(src, loff);
2597 if (!src_page)
2598 return -EINVAL;
2599 dst_page = extent_same_get_page(dst, dst_loff);
2600 if (!dst_page) {
2601 page_cache_release(src_page);
2602 return -EINVAL;
2603 }
2604 addr = kmap_atomic(src_page);
2605 dst_addr = kmap_atomic(dst_page);
2606
2607 flush_dcache_page(src_page);
2608 flush_dcache_page(dst_page);
2609
2610 if (memcmp(addr, dst_addr, cmp_len))
2611 ret = BTRFS_SAME_DATA_DIFFERS;
2612
2613 kunmap_atomic(addr);
2614 kunmap_atomic(dst_addr);
2615 page_cache_release(src_page);
2616 page_cache_release(dst_page);
2617
2618 if (ret)
2619 break;
2620
2621 loff += cmp_len;
2622 dst_loff += cmp_len;
2623 len -= cmp_len;
2624 }
2625
2626 return ret;
2627}
2628
2629static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2630{
2631 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2632
2633 if (off + len > inode->i_size || off + len < off)
2634 return -EINVAL;
2635 /* Check that we are block aligned - btrfs_clone() requires this */
2636 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2637 return -EINVAL;
2638
2639 return 0;
2640}
2641
2642static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2643 struct inode *dst, u64 dst_loff)
2644{
2645 int ret;
2646
2647 /*
2648 * btrfs_clone() can't handle extents in the same file
2649 * yet. Once that works, we can drop this check and replace it
2650 * with a check for the same inode, but overlapping extents.
2651 */
2652 if (src == dst)
2653 return -EINVAL;
2654
2655 btrfs_double_lock(src, loff, dst, dst_loff, len);
2656
2657 ret = extent_same_check_offsets(src, loff, len);
2658 if (ret)
2659 goto out_unlock;
2660
2661 ret = extent_same_check_offsets(dst, dst_loff, len);
2662 if (ret)
2663 goto out_unlock;
2664
2665 /* don't make the dst file partly checksummed */
2666 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2667 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2668 ret = -EINVAL;
2669 goto out_unlock;
2670 }
2671
2672 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2673 if (ret == 0)
2674 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2675
2676out_unlock:
2677 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2678
2679 return ret;
2680}
2681
2682#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2683
2684static long btrfs_ioctl_file_extent_same(struct file *file,
2685 void __user *argp)
2686{
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002687 struct btrfs_ioctl_same_args tmp;
2688 struct btrfs_ioctl_same_args *same;
2689 struct btrfs_ioctl_same_extent_info *info;
Mark Fasheh416161d2013-08-06 11:42:51 -07002690 struct inode *src = file->f_dentry->d_inode;
2691 struct file *dst_file = NULL;
2692 struct inode *dst;
2693 u64 off;
2694 u64 len;
2695 int i;
2696 int ret;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002697 unsigned long size;
Mark Fasheh416161d2013-08-06 11:42:51 -07002698 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2699 bool is_admin = capable(CAP_SYS_ADMIN);
2700
2701 if (!(file->f_mode & FMODE_READ))
2702 return -EINVAL;
2703
2704 ret = mnt_want_write_file(file);
2705 if (ret)
2706 return ret;
2707
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002708 if (copy_from_user(&tmp,
Mark Fasheh416161d2013-08-06 11:42:51 -07002709 (struct btrfs_ioctl_same_args __user *)argp,
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002710 sizeof(tmp))) {
Mark Fasheh416161d2013-08-06 11:42:51 -07002711 ret = -EFAULT;
2712 goto out;
2713 }
2714
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002715 size = sizeof(tmp) +
2716 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2717
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002718 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002719
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002720 if (IS_ERR(same)) {
2721 ret = PTR_ERR(same);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002722 goto out;
2723 }
2724
2725 off = same->logical_offset;
2726 len = same->length;
Mark Fasheh416161d2013-08-06 11:42:51 -07002727
2728 /*
2729 * Limit the total length we will dedupe for each operation.
2730 * This is intended to bound the total time spent in this
2731 * ioctl to something sane.
2732 */
2733 if (len > BTRFS_MAX_DEDUPE_LEN)
2734 len = BTRFS_MAX_DEDUPE_LEN;
2735
2736 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2737 /*
2738 * Btrfs does not support blocksize < page_size. As a
2739 * result, btrfs_cmp_data() won't correctly handle
2740 * this situation without an update.
2741 */
2742 ret = -EINVAL;
2743 goto out;
2744 }
2745
2746 ret = -EISDIR;
2747 if (S_ISDIR(src->i_mode))
2748 goto out;
2749
2750 ret = -EACCES;
2751 if (!S_ISREG(src->i_mode))
2752 goto out;
2753
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002754 /* pre-format output fields to sane values */
2755 for (i = 0; i < same->dest_count; i++) {
2756 same->info[i].bytes_deduped = 0ULL;
2757 same->info[i].status = 0;
2758 }
2759
Mark Fasheh416161d2013-08-06 11:42:51 -07002760 ret = 0;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002761 for (i = 0; i < same->dest_count; i++) {
2762 info = &same->info[i];
Mark Fasheh416161d2013-08-06 11:42:51 -07002763
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002764 dst_file = fget(info->fd);
Mark Fasheh416161d2013-08-06 11:42:51 -07002765 if (!dst_file) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002766 info->status = -EBADF;
Mark Fasheh416161d2013-08-06 11:42:51 -07002767 goto next;
2768 }
2769
2770 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002771 info->status = -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07002772 goto next;
2773 }
2774
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002775 info->status = -EXDEV;
Mark Fasheh416161d2013-08-06 11:42:51 -07002776 if (file->f_path.mnt != dst_file->f_path.mnt)
2777 goto next;
2778
2779 dst = dst_file->f_dentry->d_inode;
2780 if (src->i_sb != dst->i_sb)
2781 goto next;
2782
2783 if (S_ISDIR(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002784 info->status = -EISDIR;
Mark Fasheh416161d2013-08-06 11:42:51 -07002785 goto next;
2786 }
2787
2788 if (!S_ISREG(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002789 info->status = -EACCES;
Mark Fasheh416161d2013-08-06 11:42:51 -07002790 goto next;
2791 }
2792
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002793 info->status = btrfs_extent_same(src, off, len, dst,
2794 info->logical_offset);
2795 if (info->status == 0)
2796 info->bytes_deduped += len;
Mark Fasheh416161d2013-08-06 11:42:51 -07002797
2798next:
2799 if (dst_file)
2800 fput(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07002801 }
2802
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002803 ret = copy_to_user(argp, same, size);
2804 if (ret)
2805 ret = -EFAULT;
2806
Mark Fasheh416161d2013-08-06 11:42:51 -07002807out:
2808 mnt_drop_write_file(file);
2809 return ret;
2810}
2811
Mark Fasheh32b7c682013-08-06 11:42:49 -07002812/**
2813 * btrfs_clone() - clone a range from inode file to another
2814 *
2815 * @src: Inode to clone from
2816 * @inode: Inode to clone to
2817 * @off: Offset within source to start clone from
2818 * @olen: Original length, passed by user, of range to clone
2819 * @olen_aligned: Block-aligned value of olen, extent_same uses
2820 * identical values here
2821 * @destoff: Offset within @inode to start clone
2822 */
2823static int btrfs_clone(struct inode *src, struct inode *inode,
2824 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002825{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002826 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002827 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002828 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002829 struct btrfs_trans_handle *trans;
2830 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002831 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002832 u32 nritems;
2833 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002834 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002835 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002836
2837 ret = -ENOMEM;
2838 buf = vmalloc(btrfs_level_size(root, 0));
2839 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002840 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002841
2842 path = btrfs_alloc_path();
2843 if (!path) {
2844 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002845 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002846 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002847
Yan Zhengae01a0a2008-08-04 23:23:47 -04002848 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002849 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002850 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002851 key.type = BTRFS_EXTENT_DATA_KEY;
2852 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002853
2854 while (1) {
2855 /*
2856 * note the key will change type as we walk through the
2857 * tree.
2858 */
David Sterba362a20c2011-08-01 18:11:57 +02002859 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2860 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002861 if (ret < 0)
2862 goto out;
2863
Yan Zhengae01a0a2008-08-04 23:23:47 -04002864 nritems = btrfs_header_nritems(path->nodes[0]);
2865 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002866 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002867 if (ret < 0)
2868 goto out;
2869 if (ret > 0)
2870 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002871 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002872 }
2873 leaf = path->nodes[0];
2874 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002875
Yan Zhengae01a0a2008-08-04 23:23:47 -04002876 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002877 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002878 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002879 break;
2880
Sage Weilc5c9cd42008-11-12 14:32:25 -05002881 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2882 struct btrfs_file_extent_item *extent;
2883 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002884 u32 size;
2885 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002886 u64 disko = 0, diskl = 0;
2887 u64 datao = 0, datal = 0;
2888 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002889 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002890
2891 size = btrfs_item_size_nr(leaf, slot);
2892 read_extent_buffer(leaf, buf,
2893 btrfs_item_ptr_offset(leaf, slot),
2894 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002895
2896 extent = btrfs_item_ptr(leaf, slot,
2897 struct btrfs_file_extent_item);
2898 comp = btrfs_file_extent_compression(leaf, extent);
2899 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002900 if (type == BTRFS_FILE_EXTENT_REG ||
2901 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002902 disko = btrfs_file_extent_disk_bytenr(leaf,
2903 extent);
2904 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2905 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002906 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002907 datal = btrfs_file_extent_num_bytes(leaf,
2908 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002909 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2910 /* take upper bound, may be compressed */
2911 datal = btrfs_file_extent_ram_bytes(leaf,
2912 extent);
2913 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002914 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002915
Sage Weil050006a2010-10-29 15:37:33 -04002916 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002917 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002918 goto next;
2919
Zheng Yan31840ae2008-09-23 13:14:14 -04002920 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002921 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002922 if (off <= key.offset)
2923 new_key.offset = key.offset + destoff - off;
2924 else
2925 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002926
Sage Weilb6f34092011-09-20 14:48:51 -04002927 /*
2928 * 1 - adjusting old extent (we may have to split it)
2929 * 1 - add new extent
2930 * 1 - inode update
2931 */
2932 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002933 if (IS_ERR(trans)) {
2934 ret = PTR_ERR(trans);
2935 goto out;
2936 }
2937
Chris Masonc8a894d2009-06-27 21:07:03 -04002938 if (type == BTRFS_FILE_EXTENT_REG ||
2939 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002940 /*
2941 * a | --- range to clone ---| b
2942 * | ------------- extent ------------- |
2943 */
2944
2945 /* substract range b */
2946 if (key.offset + datal > off + len)
2947 datal = off + len - key.offset;
2948
2949 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002950 if (off > key.offset) {
2951 datao += off - key.offset;
2952 datal -= off - key.offset;
2953 }
2954
Josef Bacik5dc562c2012-08-17 13:14:17 -04002955 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002956 new_key.offset,
2957 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002958 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002959 if (ret) {
2960 btrfs_abort_transaction(trans, root,
2961 ret);
2962 btrfs_end_transaction(trans, root);
2963 goto out;
2964 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002965
Sage Weilc5c9cd42008-11-12 14:32:25 -05002966 ret = btrfs_insert_empty_item(trans, root, path,
2967 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002968 if (ret) {
2969 btrfs_abort_transaction(trans, root,
2970 ret);
2971 btrfs_end_transaction(trans, root);
2972 goto out;
2973 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002974
2975 leaf = path->nodes[0];
2976 slot = path->slots[0];
2977 write_extent_buffer(leaf, buf,
2978 btrfs_item_ptr_offset(leaf, slot),
2979 size);
2980
2981 extent = btrfs_item_ptr(leaf, slot,
2982 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002983
Sage Weilc5c9cd42008-11-12 14:32:25 -05002984 /* disko == 0 means it's a hole */
2985 if (!disko)
2986 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002987
2988 btrfs_set_file_extent_offset(leaf, extent,
2989 datao);
2990 btrfs_set_file_extent_num_bytes(leaf, extent,
2991 datal);
2992 if (disko) {
2993 inode_add_bytes(inode, datal);
2994 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002995 disko, diskl, 0,
2996 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002997 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002998 new_key.offset - datao,
2999 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003000 if (ret) {
3001 btrfs_abort_transaction(trans,
3002 root,
3003 ret);
3004 btrfs_end_transaction(trans,
3005 root);
3006 goto out;
3007
3008 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003009 }
3010 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3011 u64 skip = 0;
3012 u64 trim = 0;
3013 if (off > key.offset) {
3014 skip = off - key.offset;
3015 new_key.offset += skip;
3016 }
Chris Masond3977122009-01-05 21:25:51 -05003017
Liu Boaa42ffd2012-09-18 03:52:23 -06003018 if (key.offset + datal > off + len)
3019 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003020
Sage Weilc5c9cd42008-11-12 14:32:25 -05003021 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003022 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003023 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003024 goto out;
3025 }
3026 size -= skip + trim;
3027 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003028
Josef Bacik5dc562c2012-08-17 13:14:17 -04003029 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003030 new_key.offset,
3031 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003032 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003033 if (ret) {
3034 btrfs_abort_transaction(trans, root,
3035 ret);
3036 btrfs_end_transaction(trans, root);
3037 goto out;
3038 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003039
Sage Weilc5c9cd42008-11-12 14:32:25 -05003040 ret = btrfs_insert_empty_item(trans, root, path,
3041 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003042 if (ret) {
3043 btrfs_abort_transaction(trans, root,
3044 ret);
3045 btrfs_end_transaction(trans, root);
3046 goto out;
3047 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003048
3049 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003050 u32 start =
3051 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003052 memmove(buf+start, buf+start+skip,
3053 datal);
3054 }
3055
3056 leaf = path->nodes[0];
3057 slot = path->slots[0];
3058 write_extent_buffer(leaf, buf,
3059 btrfs_item_ptr_offset(leaf, slot),
3060 size);
3061 inode_add_bytes(inode, datal);
3062 }
3063
3064 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003065 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003066
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003067 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003068 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003069
3070 /*
3071 * we round up to the block size at eof when
3072 * determining which extents to clone above,
3073 * but shouldn't round up the file size
3074 */
3075 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003076 if (endoff > destoff+olen)
3077 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003078 if (endoff > inode->i_size)
3079 btrfs_i_size_write(inode, endoff);
3080
Yan, Zhenga22285a2010-05-16 10:48:46 -04003081 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003082 if (ret) {
3083 btrfs_abort_transaction(trans, root, ret);
3084 btrfs_end_transaction(trans, root);
3085 goto out;
3086 }
3087 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003088 }
Chris Masond3977122009-01-05 21:25:51 -05003089next:
David Sterbab3b4aa72011-04-21 01:20:15 +02003090 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003091 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003092 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003093 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003094
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003095out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003096 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003097 btrfs_free_path(path);
3098 vfree(buf);
3099 return ret;
3100}
3101
3102static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3103 u64 off, u64 olen, u64 destoff)
3104{
3105 struct inode *inode = fdentry(file)->d_inode;
3106 struct btrfs_root *root = BTRFS_I(inode)->root;
3107 struct fd src_file;
3108 struct inode *src;
3109 int ret;
3110 u64 len = olen;
3111 u64 bs = root->fs_info->sb->s_blocksize;
3112 int same_inode = 0;
3113
3114 /*
3115 * TODO:
3116 * - split compressed inline extents. annoying: we need to
3117 * decompress into destination's address_space (the file offset
3118 * may change, so source mapping won't do), then recompress (or
3119 * otherwise reinsert) a subrange.
3120 * - allow ranges within the same file to be cloned (provided
3121 * they don't overlap)?
3122 */
3123
3124 /* the destination must be opened for writing */
3125 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3126 return -EINVAL;
3127
3128 if (btrfs_root_readonly(root))
3129 return -EROFS;
3130
3131 ret = mnt_want_write_file(file);
3132 if (ret)
3133 return ret;
3134
3135 src_file = fdget(srcfd);
3136 if (!src_file.file) {
3137 ret = -EBADF;
3138 goto out_drop_write;
3139 }
3140
3141 ret = -EXDEV;
3142 if (src_file.file->f_path.mnt != file->f_path.mnt)
3143 goto out_fput;
3144
3145 src = file_inode(src_file.file);
3146
3147 ret = -EINVAL;
3148 if (src == inode)
3149 same_inode = 1;
3150
3151 /* the src must be open for reading */
3152 if (!(src_file.file->f_mode & FMODE_READ))
3153 goto out_fput;
3154
3155 /* don't make the dst file partly checksummed */
3156 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3157 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3158 goto out_fput;
3159
3160 ret = -EISDIR;
3161 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3162 goto out_fput;
3163
3164 ret = -EXDEV;
3165 if (src->i_sb != inode->i_sb)
3166 goto out_fput;
3167
3168 if (!same_inode) {
3169 if (inode < src) {
3170 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3171 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3172 } else {
3173 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3174 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3175 }
3176 } else {
3177 mutex_lock(&src->i_mutex);
3178 }
3179
3180 /* determine range to clone */
3181 ret = -EINVAL;
3182 if (off + len > src->i_size || off + len < off)
3183 goto out_unlock;
3184 if (len == 0)
3185 olen = len = src->i_size - off;
3186 /* if we extend to eof, continue to block boundary */
3187 if (off + len == src->i_size)
3188 len = ALIGN(src->i_size, bs) - off;
3189
3190 /* verify the end result is block aligned */
3191 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3192 !IS_ALIGNED(destoff, bs))
3193 goto out_unlock;
3194
3195 /* verify if ranges are overlapped within the same file */
3196 if (same_inode) {
3197 if (destoff + len > off && destoff < off + len)
3198 goto out_unlock;
3199 }
3200
3201 if (destoff > inode->i_size) {
3202 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3203 if (ret)
3204 goto out_unlock;
3205 }
3206
3207 /* truncate page cache pages from target inode range */
3208 truncate_inode_pages_range(&inode->i_data, destoff,
3209 PAGE_CACHE_ALIGN(destoff + len) - 1);
3210
3211 lock_extent_range(src, off, len);
3212
3213 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3214
Liu Boaa42ffd2012-09-18 03:52:23 -06003215 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003216out_unlock:
3217 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00003218 if (!same_inode)
3219 mutex_unlock(&inode->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003220out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003221 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003222out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003223 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003224 return ret;
3225}
3226
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003227static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003228{
3229 struct btrfs_ioctl_clone_range_args args;
3230
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003231 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003232 return -EFAULT;
3233 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3234 args.src_length, args.dest_offset);
3235}
3236
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003237/*
3238 * there are many ways the trans_start and trans_end ioctls can lead
3239 * to deadlocks. They should only be used by applications that
3240 * basically own the machine, and have a very in depth understanding
3241 * of all the possible deadlocks and enospc problems.
3242 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003243static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003244{
Al Viro496ad9a2013-01-23 17:07:38 -05003245 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003246 struct btrfs_root *root = BTRFS_I(inode)->root;
3247 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003248 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003249
Sage Weil1ab86ae2009-09-29 18:38:44 -04003250 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003251 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003252 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003253
3254 ret = -EINPROGRESS;
3255 if (file->private_data)
3256 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003257
Li Zefanb83cc962010-12-20 16:04:08 +08003258 ret = -EROFS;
3259 if (btrfs_root_readonly(root))
3260 goto out;
3261
Al Viroa561be72011-11-23 11:57:51 -05003262 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003263 if (ret)
3264 goto out;
3265
Josef Bacika4abeea2011-04-11 17:25:13 -04003266 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003267
Sage Weil1ab86ae2009-09-29 18:38:44 -04003268 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003269 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003270 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003271 goto out_drop;
3272
3273 file->private_data = trans;
3274 return 0;
3275
3276out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003277 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003278 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003279out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003280 return ret;
3281}
3282
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003283static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3284{
Al Viro496ad9a2013-01-23 17:07:38 -05003285 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003286 struct btrfs_root *root = BTRFS_I(inode)->root;
3287 struct btrfs_root *new_root;
3288 struct btrfs_dir_item *di;
3289 struct btrfs_trans_handle *trans;
3290 struct btrfs_path *path;
3291 struct btrfs_key location;
3292 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003293 u64 objectid = 0;
3294 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003295 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003296
3297 if (!capable(CAP_SYS_ADMIN))
3298 return -EPERM;
3299
Miao Xie3c04ce02012-11-26 08:43:07 +00003300 ret = mnt_want_write_file(file);
3301 if (ret)
3302 return ret;
3303
3304 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3305 ret = -EFAULT;
3306 goto out;
3307 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003308
3309 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303310 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003311
3312 location.objectid = objectid;
3313 location.type = BTRFS_ROOT_ITEM_KEY;
3314 location.offset = (u64)-1;
3315
3316 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003317 if (IS_ERR(new_root)) {
3318 ret = PTR_ERR(new_root);
3319 goto out;
3320 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003321
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003322 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003323 if (!path) {
3324 ret = -ENOMEM;
3325 goto out;
3326 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003327 path->leave_spinning = 1;
3328
3329 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003330 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003331 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003332 ret = PTR_ERR(trans);
3333 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003334 }
3335
David Sterba6c417612011-04-13 15:41:04 +02003336 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003337 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3338 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003339 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003340 btrfs_free_path(path);
3341 btrfs_end_transaction(trans, root);
3342 printk(KERN_ERR "Umm, you don't have the default dir item, "
3343 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00003344 ret = -ENOENT;
3345 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003346 }
3347
3348 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3349 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3350 btrfs_mark_buffer_dirty(path->nodes[0]);
3351 btrfs_free_path(path);
3352
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003353 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003354 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003355out:
3356 mnt_drop_write_file(file);
3357 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003358}
3359
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003360void btrfs_get_block_group_info(struct list_head *groups_list,
3361 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003362{
3363 struct btrfs_block_group_cache *block_group;
3364
3365 space->total_bytes = 0;
3366 space->used_bytes = 0;
3367 space->flags = 0;
3368 list_for_each_entry(block_group, groups_list, list) {
3369 space->flags = block_group->flags;
3370 space->total_bytes += block_group->key.offset;
3371 space->used_bytes +=
3372 btrfs_block_group_used(&block_group->item);
3373 }
3374}
3375
Eric Sandeen48a3b632013-04-25 20:41:01 +00003376static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003377{
3378 struct btrfs_ioctl_space_args space_args;
3379 struct btrfs_ioctl_space_info space;
3380 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003381 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003382 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003383 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003384 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3385 BTRFS_BLOCK_GROUP_SYSTEM,
3386 BTRFS_BLOCK_GROUP_METADATA,
3387 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3388 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003389 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003390 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003391 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003392 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003393
3394 if (copy_from_user(&space_args,
3395 (struct btrfs_ioctl_space_args __user *)arg,
3396 sizeof(space_args)))
3397 return -EFAULT;
3398
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003399 for (i = 0; i < num_types; i++) {
3400 struct btrfs_space_info *tmp;
3401
3402 info = NULL;
3403 rcu_read_lock();
3404 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3405 list) {
3406 if (tmp->flags == types[i]) {
3407 info = tmp;
3408 break;
3409 }
3410 }
3411 rcu_read_unlock();
3412
3413 if (!info)
3414 continue;
3415
3416 down_read(&info->groups_sem);
3417 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3418 if (!list_empty(&info->block_groups[c]))
3419 slot_count++;
3420 }
3421 up_read(&info->groups_sem);
3422 }
Josef Bacik1406e432010-01-13 18:19:06 +00003423
Chris Mason7fde62b2010-03-16 15:40:10 -04003424 /* space_slots == 0 means they are asking for a count */
3425 if (space_args.space_slots == 0) {
3426 space_args.total_spaces = slot_count;
3427 goto out;
3428 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003429
Dan Rosenberg51788b12011-02-14 16:04:23 -05003430 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003431
Chris Mason7fde62b2010-03-16 15:40:10 -04003432 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003433
Chris Mason7fde62b2010-03-16 15:40:10 -04003434 /* we generally have at most 6 or so space infos, one for each raid
3435 * level. So, a whole page should be more than enough for everyone
3436 */
3437 if (alloc_size > PAGE_CACHE_SIZE)
3438 return -ENOMEM;
3439
3440 space_args.total_spaces = 0;
3441 dest = kmalloc(alloc_size, GFP_NOFS);
3442 if (!dest)
3443 return -ENOMEM;
3444 dest_orig = dest;
3445
3446 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003447 for (i = 0; i < num_types; i++) {
3448 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003449
Dan Rosenberg51788b12011-02-14 16:04:23 -05003450 if (!slot_count)
3451 break;
3452
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003453 info = NULL;
3454 rcu_read_lock();
3455 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3456 list) {
3457 if (tmp->flags == types[i]) {
3458 info = tmp;
3459 break;
3460 }
3461 }
3462 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003463
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003464 if (!info)
3465 continue;
3466 down_read(&info->groups_sem);
3467 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3468 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003469 btrfs_get_block_group_info(
3470 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003471 memcpy(dest, &space, sizeof(space));
3472 dest++;
3473 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003474 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003475 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003476 if (!slot_count)
3477 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003478 }
3479 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003480 }
Josef Bacik1406e432010-01-13 18:19:06 +00003481
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003482 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003483 (arg + sizeof(struct btrfs_ioctl_space_args));
3484
3485 if (copy_to_user(user_dest, dest_orig, alloc_size))
3486 ret = -EFAULT;
3487
3488 kfree(dest_orig);
3489out:
3490 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003491 ret = -EFAULT;
3492
3493 return ret;
3494}
3495
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003496/*
3497 * there are many ways the trans_start and trans_end ioctls can lead
3498 * to deadlocks. They should only be used by applications that
3499 * basically own the machine, and have a very in depth understanding
3500 * of all the possible deadlocks and enospc problems.
3501 */
3502long btrfs_ioctl_trans_end(struct file *file)
3503{
Al Viro496ad9a2013-01-23 17:07:38 -05003504 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003505 struct btrfs_root *root = BTRFS_I(inode)->root;
3506 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003507
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003508 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003509 if (!trans)
3510 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003511 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003512
Sage Weil1ab86ae2009-09-29 18:38:44 -04003513 btrfs_end_transaction(trans, root);
3514
Josef Bacika4abeea2011-04-11 17:25:13 -04003515 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003516
Al Viro2a79f172011-12-09 08:06:57 -05003517 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003518 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003519}
3520
Miao Xie9a8c28b2012-11-26 08:40:43 +00003521static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3522 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003523{
Sage Weil46204592010-10-29 15:41:32 -04003524 struct btrfs_trans_handle *trans;
3525 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003526 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003527
Miao Xied4edf392013-02-20 09:17:06 +00003528 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003529 if (IS_ERR(trans)) {
3530 if (PTR_ERR(trans) != -ENOENT)
3531 return PTR_ERR(trans);
3532
3533 /* No running transaction, don't bother */
3534 transid = root->fs_info->last_trans_committed;
3535 goto out;
3536 }
Sage Weil46204592010-10-29 15:41:32 -04003537 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003538 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003539 if (ret) {
3540 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003541 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003542 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003543out:
Sage Weil46204592010-10-29 15:41:32 -04003544 if (argp)
3545 if (copy_to_user(argp, &transid, sizeof(transid)))
3546 return -EFAULT;
3547 return 0;
3548}
3549
Miao Xie9a8c28b2012-11-26 08:40:43 +00003550static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3551 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003552{
Sage Weil46204592010-10-29 15:41:32 -04003553 u64 transid;
3554
3555 if (argp) {
3556 if (copy_from_user(&transid, argp, sizeof(transid)))
3557 return -EFAULT;
3558 } else {
3559 transid = 0; /* current trans */
3560 }
3561 return btrfs_wait_for_commit(root, transid);
3562}
3563
Miao Xieb8e95482012-11-26 08:48:01 +00003564static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003565{
Al Viro496ad9a2013-01-23 17:07:38 -05003566 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003567 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003568 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003569
3570 if (!capable(CAP_SYS_ADMIN))
3571 return -EPERM;
3572
3573 sa = memdup_user(arg, sizeof(*sa));
3574 if (IS_ERR(sa))
3575 return PTR_ERR(sa);
3576
Miao Xieb8e95482012-11-26 08:48:01 +00003577 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3578 ret = mnt_want_write_file(file);
3579 if (ret)
3580 goto out;
3581 }
3582
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003583 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003584 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3585 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003586
3587 if (copy_to_user(arg, sa, sizeof(*sa)))
3588 ret = -EFAULT;
3589
Miao Xieb8e95482012-11-26 08:48:01 +00003590 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3591 mnt_drop_write_file(file);
3592out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003593 kfree(sa);
3594 return ret;
3595}
3596
3597static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3598{
3599 if (!capable(CAP_SYS_ADMIN))
3600 return -EPERM;
3601
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003602 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003603}
3604
3605static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3606 void __user *arg)
3607{
3608 struct btrfs_ioctl_scrub_args *sa;
3609 int ret;
3610
3611 if (!capable(CAP_SYS_ADMIN))
3612 return -EPERM;
3613
3614 sa = memdup_user(arg, sizeof(*sa));
3615 if (IS_ERR(sa))
3616 return PTR_ERR(sa);
3617
3618 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3619
3620 if (copy_to_user(arg, sa, sizeof(*sa)))
3621 ret = -EFAULT;
3622
3623 kfree(sa);
3624 return ret;
3625}
3626
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003627static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003628 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003629{
3630 struct btrfs_ioctl_get_dev_stats *sa;
3631 int ret;
3632
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003633 sa = memdup_user(arg, sizeof(*sa));
3634 if (IS_ERR(sa))
3635 return PTR_ERR(sa);
3636
David Sterbab27f7c02012-06-22 06:30:39 -06003637 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3638 kfree(sa);
3639 return -EPERM;
3640 }
3641
3642 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003643
3644 if (copy_to_user(arg, sa, sizeof(*sa)))
3645 ret = -EFAULT;
3646
3647 kfree(sa);
3648 return ret;
3649}
3650
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003651static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3652{
3653 struct btrfs_ioctl_dev_replace_args *p;
3654 int ret;
3655
3656 if (!capable(CAP_SYS_ADMIN))
3657 return -EPERM;
3658
3659 p = memdup_user(arg, sizeof(*p));
3660 if (IS_ERR(p))
3661 return PTR_ERR(p);
3662
3663 switch (p->cmd) {
3664 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003665 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3666 ret = -EROFS;
3667 goto out;
3668 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003669 if (atomic_xchg(
3670 &root->fs_info->mutually_exclusive_operation_running,
3671 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003672 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003673 } else {
3674 ret = btrfs_dev_replace_start(root, p);
3675 atomic_set(
3676 &root->fs_info->mutually_exclusive_operation_running,
3677 0);
3678 }
3679 break;
3680 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3681 btrfs_dev_replace_status(root->fs_info, p);
3682 ret = 0;
3683 break;
3684 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3685 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3686 break;
3687 default:
3688 ret = -EINVAL;
3689 break;
3690 }
3691
3692 if (copy_to_user(arg, p, sizeof(*p)))
3693 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003694out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003695 kfree(p);
3696 return ret;
3697}
3698
Jan Schmidtd7728c92011-07-07 16:48:38 +02003699static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3700{
3701 int ret = 0;
3702 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003703 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003704 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003705 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003706 struct inode_fs_paths *ipath = NULL;
3707 struct btrfs_path *path;
3708
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003709 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003710 return -EPERM;
3711
3712 path = btrfs_alloc_path();
3713 if (!path) {
3714 ret = -ENOMEM;
3715 goto out;
3716 }
3717
3718 ipa = memdup_user(arg, sizeof(*ipa));
3719 if (IS_ERR(ipa)) {
3720 ret = PTR_ERR(ipa);
3721 ipa = NULL;
3722 goto out;
3723 }
3724
3725 size = min_t(u32, ipa->size, 4096);
3726 ipath = init_ipath(size, root, path);
3727 if (IS_ERR(ipath)) {
3728 ret = PTR_ERR(ipath);
3729 ipath = NULL;
3730 goto out;
3731 }
3732
3733 ret = paths_from_inode(ipa->inum, ipath);
3734 if (ret < 0)
3735 goto out;
3736
3737 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003738 rel_ptr = ipath->fspath->val[i] -
3739 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003740 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003741 }
3742
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003743 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3744 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003745 if (ret) {
3746 ret = -EFAULT;
3747 goto out;
3748 }
3749
3750out:
3751 btrfs_free_path(path);
3752 free_ipath(ipath);
3753 kfree(ipa);
3754
3755 return ret;
3756}
3757
3758static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3759{
3760 struct btrfs_data_container *inodes = ctx;
3761 const size_t c = 3 * sizeof(u64);
3762
3763 if (inodes->bytes_left >= c) {
3764 inodes->bytes_left -= c;
3765 inodes->val[inodes->elem_cnt] = inum;
3766 inodes->val[inodes->elem_cnt + 1] = offset;
3767 inodes->val[inodes->elem_cnt + 2] = root;
3768 inodes->elem_cnt += 3;
3769 } else {
3770 inodes->bytes_missing += c - inodes->bytes_left;
3771 inodes->bytes_left = 0;
3772 inodes->elem_missed += 3;
3773 }
3774
3775 return 0;
3776}
3777
3778static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3779 void __user *arg)
3780{
3781 int ret = 0;
3782 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003783 struct btrfs_ioctl_logical_ino_args *loi;
3784 struct btrfs_data_container *inodes = NULL;
3785 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003786
3787 if (!capable(CAP_SYS_ADMIN))
3788 return -EPERM;
3789
3790 loi = memdup_user(arg, sizeof(*loi));
3791 if (IS_ERR(loi)) {
3792 ret = PTR_ERR(loi);
3793 loi = NULL;
3794 goto out;
3795 }
3796
3797 path = btrfs_alloc_path();
3798 if (!path) {
3799 ret = -ENOMEM;
3800 goto out;
3801 }
3802
Liu Bo425d17a2012-09-07 20:01:30 -06003803 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003804 inodes = init_data_container(size);
3805 if (IS_ERR(inodes)) {
3806 ret = PTR_ERR(inodes);
3807 inodes = NULL;
3808 goto out;
3809 }
3810
Liu Bodf031f02012-09-07 20:01:29 -06003811 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3812 build_ino_list, inodes);
3813 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003814 ret = -ENOENT;
3815 if (ret < 0)
3816 goto out;
3817
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003818 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3819 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003820 if (ret)
3821 ret = -EFAULT;
3822
3823out:
3824 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003825 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003826 kfree(loi);
3827
3828 return ret;
3829}
3830
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003831void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003832 struct btrfs_ioctl_balance_args *bargs)
3833{
3834 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3835
3836 bargs->flags = bctl->flags;
3837
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003838 if (atomic_read(&fs_info->balance_running))
3839 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3840 if (atomic_read(&fs_info->balance_pause_req))
3841 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003842 if (atomic_read(&fs_info->balance_cancel_req))
3843 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003844
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003845 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3846 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3847 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003848
3849 if (lock) {
3850 spin_lock(&fs_info->balance_lock);
3851 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3852 spin_unlock(&fs_info->balance_lock);
3853 } else {
3854 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3855 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003856}
3857
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003858static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003859{
Al Viro496ad9a2013-01-23 17:07:38 -05003860 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003861 struct btrfs_fs_info *fs_info = root->fs_info;
3862 struct btrfs_ioctl_balance_args *bargs;
3863 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003864 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003865 int ret;
3866
3867 if (!capable(CAP_SYS_ADMIN))
3868 return -EPERM;
3869
Liu Boe54bfa312012-06-29 03:58:48 -06003870 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003871 if (ret)
3872 return ret;
3873
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003874again:
3875 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3876 mutex_lock(&fs_info->volume_mutex);
3877 mutex_lock(&fs_info->balance_mutex);
3878 need_unlock = true;
3879 goto locked;
3880 }
3881
3882 /*
3883 * mut. excl. ops lock is locked. Three possibilites:
3884 * (1) some other op is running
3885 * (2) balance is running
3886 * (3) balance is paused -- special case (think resume)
3887 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003888 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003889 if (fs_info->balance_ctl) {
3890 /* this is either (2) or (3) */
3891 if (!atomic_read(&fs_info->balance_running)) {
3892 mutex_unlock(&fs_info->balance_mutex);
3893 if (!mutex_trylock(&fs_info->volume_mutex))
3894 goto again;
3895 mutex_lock(&fs_info->balance_mutex);
3896
3897 if (fs_info->balance_ctl &&
3898 !atomic_read(&fs_info->balance_running)) {
3899 /* this is (3) */
3900 need_unlock = false;
3901 goto locked;
3902 }
3903
3904 mutex_unlock(&fs_info->balance_mutex);
3905 mutex_unlock(&fs_info->volume_mutex);
3906 goto again;
3907 } else {
3908 /* this is (2) */
3909 mutex_unlock(&fs_info->balance_mutex);
3910 ret = -EINPROGRESS;
3911 goto out;
3912 }
3913 } else {
3914 /* this is (1) */
3915 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08003916 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003917 goto out;
3918 }
3919
3920locked:
3921 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003922
3923 if (arg) {
3924 bargs = memdup_user(arg, sizeof(*bargs));
3925 if (IS_ERR(bargs)) {
3926 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003927 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003928 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003929
3930 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3931 if (!fs_info->balance_ctl) {
3932 ret = -ENOTCONN;
3933 goto out_bargs;
3934 }
3935
3936 bctl = fs_info->balance_ctl;
3937 spin_lock(&fs_info->balance_lock);
3938 bctl->flags |= BTRFS_BALANCE_RESUME;
3939 spin_unlock(&fs_info->balance_lock);
3940
3941 goto do_balance;
3942 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003943 } else {
3944 bargs = NULL;
3945 }
3946
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003947 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003948 ret = -EINPROGRESS;
3949 goto out_bargs;
3950 }
3951
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003952 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3953 if (!bctl) {
3954 ret = -ENOMEM;
3955 goto out_bargs;
3956 }
3957
3958 bctl->fs_info = fs_info;
3959 if (arg) {
3960 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3961 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3962 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3963
3964 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003965 } else {
3966 /* balance everything - no filters */
3967 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003968 }
3969
Ilya Dryomovde322262012-01-16 22:04:49 +02003970do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003971 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003972 * Ownership of bctl and mutually_exclusive_operation_running
3973 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3974 * or, if restriper was paused all the way until unmount, in
3975 * free_fs_info. mutually_exclusive_operation_running is
3976 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003977 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003978 need_unlock = false;
3979
3980 ret = btrfs_balance(bctl, bargs);
3981
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003982 if (arg) {
3983 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3984 ret = -EFAULT;
3985 }
3986
3987out_bargs:
3988 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003989out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003990 mutex_unlock(&fs_info->balance_mutex);
3991 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003992 if (need_unlock)
3993 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3994out:
Liu Boe54bfa312012-06-29 03:58:48 -06003995 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003996 return ret;
3997}
3998
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003999static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4000{
4001 if (!capable(CAP_SYS_ADMIN))
4002 return -EPERM;
4003
4004 switch (cmd) {
4005 case BTRFS_BALANCE_CTL_PAUSE:
4006 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004007 case BTRFS_BALANCE_CTL_CANCEL:
4008 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004009 }
4010
4011 return -EINVAL;
4012}
4013
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004014static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4015 void __user *arg)
4016{
4017 struct btrfs_fs_info *fs_info = root->fs_info;
4018 struct btrfs_ioctl_balance_args *bargs;
4019 int ret = 0;
4020
4021 if (!capable(CAP_SYS_ADMIN))
4022 return -EPERM;
4023
4024 mutex_lock(&fs_info->balance_mutex);
4025 if (!fs_info->balance_ctl) {
4026 ret = -ENOTCONN;
4027 goto out;
4028 }
4029
4030 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4031 if (!bargs) {
4032 ret = -ENOMEM;
4033 goto out;
4034 }
4035
4036 update_ioctl_balance_args(fs_info, 1, bargs);
4037
4038 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4039 ret = -EFAULT;
4040
4041 kfree(bargs);
4042out:
4043 mutex_unlock(&fs_info->balance_mutex);
4044 return ret;
4045}
4046
Miao Xie905b0dd2012-11-26 08:50:11 +00004047static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004048{
Al Viro496ad9a2013-01-23 17:07:38 -05004049 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004050 struct btrfs_ioctl_quota_ctl_args *sa;
4051 struct btrfs_trans_handle *trans = NULL;
4052 int ret;
4053 int err;
4054
4055 if (!capable(CAP_SYS_ADMIN))
4056 return -EPERM;
4057
Miao Xie905b0dd2012-11-26 08:50:11 +00004058 ret = mnt_want_write_file(file);
4059 if (ret)
4060 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004061
4062 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004063 if (IS_ERR(sa)) {
4064 ret = PTR_ERR(sa);
4065 goto drop_write;
4066 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004067
Wang Shilong7708f022013-04-07 10:24:57 +00004068 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004069 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4070 if (IS_ERR(trans)) {
4071 ret = PTR_ERR(trans);
4072 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004073 }
4074
4075 switch (sa->cmd) {
4076 case BTRFS_QUOTA_CTL_ENABLE:
4077 ret = btrfs_quota_enable(trans, root->fs_info);
4078 break;
4079 case BTRFS_QUOTA_CTL_DISABLE:
4080 ret = btrfs_quota_disable(trans, root->fs_info);
4081 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004082 default:
4083 ret = -EINVAL;
4084 break;
4085 }
4086
Jan Schmidt2f232032013-04-25 16:04:51 +00004087 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4088 if (err && !ret)
4089 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004090out:
4091 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004092 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004093drop_write:
4094 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004095 return ret;
4096}
4097
Miao Xie905b0dd2012-11-26 08:50:11 +00004098static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004099{
Al Viro496ad9a2013-01-23 17:07:38 -05004100 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004101 struct btrfs_ioctl_qgroup_assign_args *sa;
4102 struct btrfs_trans_handle *trans;
4103 int ret;
4104 int err;
4105
4106 if (!capable(CAP_SYS_ADMIN))
4107 return -EPERM;
4108
Miao Xie905b0dd2012-11-26 08:50:11 +00004109 ret = mnt_want_write_file(file);
4110 if (ret)
4111 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004112
4113 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004114 if (IS_ERR(sa)) {
4115 ret = PTR_ERR(sa);
4116 goto drop_write;
4117 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004118
4119 trans = btrfs_join_transaction(root);
4120 if (IS_ERR(trans)) {
4121 ret = PTR_ERR(trans);
4122 goto out;
4123 }
4124
4125 /* FIXME: check if the IDs really exist */
4126 if (sa->assign) {
4127 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4128 sa->src, sa->dst);
4129 } else {
4130 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4131 sa->src, sa->dst);
4132 }
4133
4134 err = btrfs_end_transaction(trans, root);
4135 if (err && !ret)
4136 ret = err;
4137
4138out:
4139 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004140drop_write:
4141 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004142 return ret;
4143}
4144
Miao Xie905b0dd2012-11-26 08:50:11 +00004145static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004146{
Al Viro496ad9a2013-01-23 17:07:38 -05004147 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004148 struct btrfs_ioctl_qgroup_create_args *sa;
4149 struct btrfs_trans_handle *trans;
4150 int ret;
4151 int err;
4152
4153 if (!capable(CAP_SYS_ADMIN))
4154 return -EPERM;
4155
Miao Xie905b0dd2012-11-26 08:50:11 +00004156 ret = mnt_want_write_file(file);
4157 if (ret)
4158 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004159
4160 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004161 if (IS_ERR(sa)) {
4162 ret = PTR_ERR(sa);
4163 goto drop_write;
4164 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004165
Miao Xied86e56c2012-11-15 11:35:41 +00004166 if (!sa->qgroupid) {
4167 ret = -EINVAL;
4168 goto out;
4169 }
4170
Arne Jansen5d13a372011-09-14 15:53:51 +02004171 trans = btrfs_join_transaction(root);
4172 if (IS_ERR(trans)) {
4173 ret = PTR_ERR(trans);
4174 goto out;
4175 }
4176
4177 /* FIXME: check if the IDs really exist */
4178 if (sa->create) {
4179 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4180 NULL);
4181 } else {
4182 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4183 }
4184
4185 err = btrfs_end_transaction(trans, root);
4186 if (err && !ret)
4187 ret = err;
4188
4189out:
4190 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004191drop_write:
4192 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004193 return ret;
4194}
4195
Miao Xie905b0dd2012-11-26 08:50:11 +00004196static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004197{
Al Viro496ad9a2013-01-23 17:07:38 -05004198 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004199 struct btrfs_ioctl_qgroup_limit_args *sa;
4200 struct btrfs_trans_handle *trans;
4201 int ret;
4202 int err;
4203 u64 qgroupid;
4204
4205 if (!capable(CAP_SYS_ADMIN))
4206 return -EPERM;
4207
Miao Xie905b0dd2012-11-26 08:50:11 +00004208 ret = mnt_want_write_file(file);
4209 if (ret)
4210 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004211
4212 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004213 if (IS_ERR(sa)) {
4214 ret = PTR_ERR(sa);
4215 goto drop_write;
4216 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004217
4218 trans = btrfs_join_transaction(root);
4219 if (IS_ERR(trans)) {
4220 ret = PTR_ERR(trans);
4221 goto out;
4222 }
4223
4224 qgroupid = sa->qgroupid;
4225 if (!qgroupid) {
4226 /* take the current subvol as qgroup */
4227 qgroupid = root->root_key.objectid;
4228 }
4229
4230 /* FIXME: check if the IDs really exist */
4231 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4232
4233 err = btrfs_end_transaction(trans, root);
4234 if (err && !ret)
4235 ret = err;
4236
4237out:
4238 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004239drop_write:
4240 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004241 return ret;
4242}
4243
Jan Schmidt2f232032013-04-25 16:04:51 +00004244static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4245{
Al Viro6d0379e2013-06-16 19:32:35 +04004246 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004247 struct btrfs_ioctl_quota_rescan_args *qsa;
4248 int ret;
4249
4250 if (!capable(CAP_SYS_ADMIN))
4251 return -EPERM;
4252
4253 ret = mnt_want_write_file(file);
4254 if (ret)
4255 return ret;
4256
4257 qsa = memdup_user(arg, sizeof(*qsa));
4258 if (IS_ERR(qsa)) {
4259 ret = PTR_ERR(qsa);
4260 goto drop_write;
4261 }
4262
4263 if (qsa->flags) {
4264 ret = -EINVAL;
4265 goto out;
4266 }
4267
4268 ret = btrfs_qgroup_rescan(root->fs_info);
4269
4270out:
4271 kfree(qsa);
4272drop_write:
4273 mnt_drop_write_file(file);
4274 return ret;
4275}
4276
4277static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4278{
Al Viro6d0379e2013-06-16 19:32:35 +04004279 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004280 struct btrfs_ioctl_quota_rescan_args *qsa;
4281 int ret = 0;
4282
4283 if (!capable(CAP_SYS_ADMIN))
4284 return -EPERM;
4285
4286 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4287 if (!qsa)
4288 return -ENOMEM;
4289
4290 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4291 qsa->flags = 1;
4292 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4293 }
4294
4295 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4296 ret = -EFAULT;
4297
4298 kfree(qsa);
4299 return ret;
4300}
4301
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004302static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4303{
4304 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4305
4306 if (!capable(CAP_SYS_ADMIN))
4307 return -EPERM;
4308
4309 return btrfs_qgroup_wait_for_completion(root->fs_info);
4310}
4311
Alexander Block8ea05e32012-07-25 17:35:53 +02004312static long btrfs_ioctl_set_received_subvol(struct file *file,
4313 void __user *arg)
4314{
4315 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004316 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004317 struct btrfs_root *root = BTRFS_I(inode)->root;
4318 struct btrfs_root_item *root_item = &root->root_item;
4319 struct btrfs_trans_handle *trans;
4320 struct timespec ct = CURRENT_TIME;
4321 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004322 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004323
4324 ret = mnt_want_write_file(file);
4325 if (ret < 0)
4326 return ret;
4327
4328 down_write(&root->fs_info->subvol_sem);
4329
4330 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4331 ret = -EINVAL;
4332 goto out;
4333 }
4334
4335 if (btrfs_root_readonly(root)) {
4336 ret = -EROFS;
4337 goto out;
4338 }
4339
4340 if (!inode_owner_or_capable(inode)) {
4341 ret = -EACCES;
4342 goto out;
4343 }
4344
4345 sa = memdup_user(arg, sizeof(*sa));
4346 if (IS_ERR(sa)) {
4347 ret = PTR_ERR(sa);
4348 sa = NULL;
4349 goto out;
4350 }
4351
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004352 /*
4353 * 1 - root item
4354 * 2 - uuid items (received uuid + subvol uuid)
4355 */
4356 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004357 if (IS_ERR(trans)) {
4358 ret = PTR_ERR(trans);
4359 trans = NULL;
4360 goto out;
4361 }
4362
4363 sa->rtransid = trans->transid;
4364 sa->rtime.sec = ct.tv_sec;
4365 sa->rtime.nsec = ct.tv_nsec;
4366
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004367 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4368 BTRFS_UUID_SIZE);
4369 if (received_uuid_changed &&
4370 !btrfs_is_empty_uuid(root_item->received_uuid))
4371 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4372 root_item->received_uuid,
4373 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4374 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004375 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4376 btrfs_set_root_stransid(root_item, sa->stransid);
4377 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004378 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4379 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4380 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4381 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004382
4383 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4384 &root->root_key, &root->root_item);
4385 if (ret < 0) {
4386 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004387 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004388 }
4389 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4390 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4391 sa->uuid,
4392 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4393 root->root_key.objectid);
4394 if (ret < 0 && ret != -EEXIST) {
4395 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004396 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004397 }
4398 }
4399 ret = btrfs_commit_transaction(trans, root);
4400 if (ret < 0) {
4401 btrfs_abort_transaction(trans, root, ret);
4402 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004403 }
4404
4405 ret = copy_to_user(arg, sa, sizeof(*sa));
4406 if (ret)
4407 ret = -EFAULT;
4408
4409out:
4410 kfree(sa);
4411 up_write(&root->fs_info->subvol_sem);
4412 mnt_drop_write_file(file);
4413 return ret;
4414}
4415
jeff.liu867ab662013-01-05 02:48:01 +00004416static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4417{
Al Viro6d0379e2013-06-16 19:32:35 +04004418 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004419 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004420 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004421 char label[BTRFS_LABEL_SIZE];
4422
4423 spin_lock(&root->fs_info->super_lock);
4424 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4425 spin_unlock(&root->fs_info->super_lock);
4426
4427 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004428
4429 if (len == BTRFS_LABEL_SIZE) {
4430 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4431 --len);
4432 }
4433
jeff.liu867ab662013-01-05 02:48:01 +00004434 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004435
4436 return ret ? -EFAULT : 0;
4437}
4438
jeff.liua8bfd4a2013-01-05 02:48:08 +00004439static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4440{
Al Viro6d0379e2013-06-16 19:32:35 +04004441 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004442 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4443 struct btrfs_trans_handle *trans;
4444 char label[BTRFS_LABEL_SIZE];
4445 int ret;
4446
4447 if (!capable(CAP_SYS_ADMIN))
4448 return -EPERM;
4449
4450 if (copy_from_user(label, arg, sizeof(label)))
4451 return -EFAULT;
4452
4453 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4454 pr_err("btrfs: unable to set label with more than %d bytes\n",
4455 BTRFS_LABEL_SIZE - 1);
4456 return -EINVAL;
4457 }
4458
4459 ret = mnt_want_write_file(file);
4460 if (ret)
4461 return ret;
4462
jeff.liua8bfd4a2013-01-05 02:48:08 +00004463 trans = btrfs_start_transaction(root, 0);
4464 if (IS_ERR(trans)) {
4465 ret = PTR_ERR(trans);
4466 goto out_unlock;
4467 }
4468
Anand Jaina1b83ac2013-07-19 17:39:32 +08004469 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004470 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004471 spin_unlock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004472 ret = btrfs_end_transaction(trans, root);
4473
4474out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004475 mnt_drop_write_file(file);
4476 return ret;
4477}
4478
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004479long btrfs_ioctl(struct file *file, unsigned int
4480 cmd, unsigned long arg)
4481{
Al Viro496ad9a2013-01-23 17:07:38 -05004482 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004483 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004484
4485 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004486 case FS_IOC_GETFLAGS:
4487 return btrfs_ioctl_getflags(file, argp);
4488 case FS_IOC_SETFLAGS:
4489 return btrfs_ioctl_setflags(file, argp);
4490 case FS_IOC_GETVERSION:
4491 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004492 case FITRIM:
4493 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004494 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004495 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004496 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004497 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004498 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004499 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004500 case BTRFS_IOC_SUBVOL_CREATE_V2:
4501 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004502 case BTRFS_IOC_SNAP_DESTROY:
4503 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004504 case BTRFS_IOC_SUBVOL_GETFLAGS:
4505 return btrfs_ioctl_subvol_getflags(file, argp);
4506 case BTRFS_IOC_SUBVOL_SETFLAGS:
4507 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004508 case BTRFS_IOC_DEFAULT_SUBVOL:
4509 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004510 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004511 return btrfs_ioctl_defrag(file, NULL);
4512 case BTRFS_IOC_DEFRAG_RANGE:
4513 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004514 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004515 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004516 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004517 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004518 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004519 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004520 case BTRFS_IOC_FS_INFO:
4521 return btrfs_ioctl_fs_info(root, argp);
4522 case BTRFS_IOC_DEV_INFO:
4523 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004524 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004525 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004526 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004527 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4528 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004529 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004530 case BTRFS_IOC_TRANS_START:
4531 return btrfs_ioctl_trans_start(file);
4532 case BTRFS_IOC_TRANS_END:
4533 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004534 case BTRFS_IOC_TREE_SEARCH:
4535 return btrfs_ioctl_tree_search(file, argp);
4536 case BTRFS_IOC_INO_LOOKUP:
4537 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004538 case BTRFS_IOC_INO_PATHS:
4539 return btrfs_ioctl_ino_to_path(root, argp);
4540 case BTRFS_IOC_LOGICAL_INO:
4541 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004542 case BTRFS_IOC_SPACE_INFO:
4543 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004544 case BTRFS_IOC_SYNC: {
4545 int ret;
4546
4547 ret = btrfs_start_all_delalloc_inodes(root->fs_info, 0);
4548 if (ret)
4549 return ret;
4550 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4551 return ret;
4552 }
Sage Weil46204592010-10-29 15:41:32 -04004553 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004554 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004555 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004556 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004557 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004558 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004559 case BTRFS_IOC_SCRUB_CANCEL:
4560 return btrfs_ioctl_scrub_cancel(root, argp);
4561 case BTRFS_IOC_SCRUB_PROGRESS:
4562 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004563 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004564 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004565 case BTRFS_IOC_BALANCE_CTL:
4566 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004567 case BTRFS_IOC_BALANCE_PROGRESS:
4568 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004569 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4570 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004571 case BTRFS_IOC_SEND:
4572 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004573 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004574 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004575 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004576 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004577 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004578 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004579 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004580 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004581 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004582 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004583 case BTRFS_IOC_QUOTA_RESCAN:
4584 return btrfs_ioctl_quota_rescan(file, argp);
4585 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4586 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004587 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4588 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004589 case BTRFS_IOC_DEV_REPLACE:
4590 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004591 case BTRFS_IOC_GET_FSLABEL:
4592 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004593 case BTRFS_IOC_SET_FSLABEL:
4594 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004595 case BTRFS_IOC_FILE_EXTENT_SAME:
4596 return btrfs_ioctl_file_extent_same(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004597 }
4598
4599 return -ENOTTY;
4600}