blob: 5543fd562b552c3f0bad4156fb012f280012ab29 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050045#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
50#include "ioctl.h"
51#include "print-tree.h"
52#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040053#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020055#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040056#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020057#include "send.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040058
Christoph Hellwig6cbff002009-04-17 10:37:41 +020059/* Mask out flags that are inappropriate for the given type of inode. */
60static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
61{
62 if (S_ISDIR(mode))
63 return flags;
64 else if (S_ISREG(mode))
65 return flags & ~FS_DIRSYNC_FL;
66 else
67 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
68}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040069
Christoph Hellwig6cbff002009-04-17 10:37:41 +020070/*
71 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
72 */
73static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
74{
75 unsigned int iflags = 0;
76
77 if (flags & BTRFS_INODE_SYNC)
78 iflags |= FS_SYNC_FL;
79 if (flags & BTRFS_INODE_IMMUTABLE)
80 iflags |= FS_IMMUTABLE_FL;
81 if (flags & BTRFS_INODE_APPEND)
82 iflags |= FS_APPEND_FL;
83 if (flags & BTRFS_INODE_NODUMP)
84 iflags |= FS_NODUMP_FL;
85 if (flags & BTRFS_INODE_NOATIME)
86 iflags |= FS_NOATIME_FL;
87 if (flags & BTRFS_INODE_DIRSYNC)
88 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000089 if (flags & BTRFS_INODE_NODATACOW)
90 iflags |= FS_NOCOW_FL;
91
92 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
93 iflags |= FS_COMPR_FL;
94 else if (flags & BTRFS_INODE_NOCOMPRESS)
95 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020096
97 return iflags;
98}
99
100/*
101 * Update inode->i_flags based on the btrfs internal flags.
102 */
103void btrfs_update_iflags(struct inode *inode)
104{
105 struct btrfs_inode *ip = BTRFS_I(inode);
106
107 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
108
109 if (ip->flags & BTRFS_INODE_SYNC)
110 inode->i_flags |= S_SYNC;
111 if (ip->flags & BTRFS_INODE_IMMUTABLE)
112 inode->i_flags |= S_IMMUTABLE;
113 if (ip->flags & BTRFS_INODE_APPEND)
114 inode->i_flags |= S_APPEND;
115 if (ip->flags & BTRFS_INODE_NOATIME)
116 inode->i_flags |= S_NOATIME;
117 if (ip->flags & BTRFS_INODE_DIRSYNC)
118 inode->i_flags |= S_DIRSYNC;
119}
120
121/*
122 * Inherit flags from the parent inode.
123 *
Josef Bacike27425d2011-09-27 11:01:30 -0400124 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200125 */
126void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
127{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400128 unsigned int flags;
129
130 if (!dir)
131 return;
132
133 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200134
Josef Bacike27425d2011-09-27 11:01:30 -0400135 if (flags & BTRFS_INODE_NOCOMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
138 } else if (flags & BTRFS_INODE_COMPRESS) {
139 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
140 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
141 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142
Josef Bacike27425d2011-09-27 11:01:30 -0400143 if (flags & BTRFS_INODE_NODATACOW)
144 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
145
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200146 btrfs_update_iflags(inode);
147}
148
149static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
150{
151 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
152 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
153
154 if (copy_to_user(arg, &flags, sizeof(flags)))
155 return -EFAULT;
156 return 0;
157}
158
Liu Bo75e7cb72011-03-22 10:12:20 +0000159static int check_flags(unsigned int flags)
160{
161 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
162 FS_NOATIME_FL | FS_NODUMP_FL | \
163 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000164 FS_NOCOMP_FL | FS_COMPR_FL |
165 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000166 return -EOPNOTSUPP;
167
168 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
169 return -EINVAL;
170
Liu Bo75e7cb72011-03-22 10:12:20 +0000171 return 0;
172}
173
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200174static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
175{
176 struct inode *inode = file->f_path.dentry->d_inode;
177 struct btrfs_inode *ip = BTRFS_I(inode);
178 struct btrfs_root *root = ip->root;
179 struct btrfs_trans_handle *trans;
180 unsigned int flags, oldflags;
181 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800182 u64 ip_oldflags;
183 unsigned int i_oldflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200184
Li Zefanb83cc962010-12-20 16:04:08 +0800185 if (btrfs_root_readonly(root))
186 return -EROFS;
187
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200188 if (copy_from_user(&flags, arg, sizeof(flags)))
189 return -EFAULT;
190
Liu Bo75e7cb72011-03-22 10:12:20 +0000191 ret = check_flags(flags);
192 if (ret)
193 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700195 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200196 return -EACCES;
197
Jan Karae7848682012-06-12 16:20:32 +0200198 ret = mnt_want_write_file(file);
199 if (ret)
200 return ret;
201
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200202 mutex_lock(&inode->i_mutex);
203
Li Zefanf062abf2011-12-29 13:36:45 +0800204 ip_oldflags = ip->flags;
205 i_oldflags = inode->i_flags;
206
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200207 flags = btrfs_mask_flags(inode->i_mode, flags);
208 oldflags = btrfs_flags_to_ioctl(ip->flags);
209 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
210 if (!capable(CAP_LINUX_IMMUTABLE)) {
211 ret = -EPERM;
212 goto out_unlock;
213 }
214 }
215
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200216 if (flags & FS_SYNC_FL)
217 ip->flags |= BTRFS_INODE_SYNC;
218 else
219 ip->flags &= ~BTRFS_INODE_SYNC;
220 if (flags & FS_IMMUTABLE_FL)
221 ip->flags |= BTRFS_INODE_IMMUTABLE;
222 else
223 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
224 if (flags & FS_APPEND_FL)
225 ip->flags |= BTRFS_INODE_APPEND;
226 else
227 ip->flags &= ~BTRFS_INODE_APPEND;
228 if (flags & FS_NODUMP_FL)
229 ip->flags |= BTRFS_INODE_NODUMP;
230 else
231 ip->flags &= ~BTRFS_INODE_NODUMP;
232 if (flags & FS_NOATIME_FL)
233 ip->flags |= BTRFS_INODE_NOATIME;
234 else
235 ip->flags &= ~BTRFS_INODE_NOATIME;
236 if (flags & FS_DIRSYNC_FL)
237 ip->flags |= BTRFS_INODE_DIRSYNC;
238 else
239 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000240 if (flags & FS_NOCOW_FL)
241 ip->flags |= BTRFS_INODE_NODATACOW;
242 else
243 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200244
Liu Bo75e7cb72011-03-22 10:12:20 +0000245 /*
246 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
247 * flag may be changed automatically if compression code won't make
248 * things smaller.
249 */
250 if (flags & FS_NOCOMP_FL) {
251 ip->flags &= ~BTRFS_INODE_COMPRESS;
252 ip->flags |= BTRFS_INODE_NOCOMPRESS;
253 } else if (flags & FS_COMPR_FL) {
254 ip->flags |= BTRFS_INODE_COMPRESS;
255 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000256 } else {
257 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000258 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200259
Li Zefan4da6f1a2011-12-29 13:39:50 +0800260 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800261 if (IS_ERR(trans)) {
262 ret = PTR_ERR(trans);
263 goto out_drop;
264 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200265
Li Zefan306424c2011-12-14 20:12:02 -0500266 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400267 inode_inc_iversion(inode);
Li Zefan306424c2011-12-14 20:12:02 -0500268 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200269 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200270
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200271 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800272 out_drop:
273 if (ret) {
274 ip->flags = ip_oldflags;
275 inode->i_flags = i_oldflags;
276 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200277
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200278 out_unlock:
279 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200280 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000281 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200282}
283
284static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
285{
286 struct inode *inode = file->f_path.dentry->d_inode;
287
288 return put_user(inode->i_generation, arg);
289}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400290
Li Dongyangf7039b12011-03-24 10:24:28 +0000291static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
292{
Al Viro815745c2011-11-17 15:40:49 -0500293 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000294 struct btrfs_device *device;
295 struct request_queue *q;
296 struct fstrim_range range;
297 u64 minlen = ULLONG_MAX;
298 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500299 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000300 int ret;
301
302 if (!capable(CAP_SYS_ADMIN))
303 return -EPERM;
304
Xiao Guangrong1f781602011-04-20 10:09:16 +0000305 rcu_read_lock();
306 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
307 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000308 if (!device->bdev)
309 continue;
310 q = bdev_get_queue(device->bdev);
311 if (blk_queue_discard(q)) {
312 num_devices++;
313 minlen = min((u64)q->limits.discard_granularity,
314 minlen);
315 }
316 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000317 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200318
Li Dongyangf7039b12011-03-24 10:24:28 +0000319 if (!num_devices)
320 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000321 if (copy_from_user(&range, arg, sizeof(range)))
322 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200323 if (range.start > total_bytes)
324 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000325
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200326 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000327 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500328 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000329 if (ret < 0)
330 return ret;
331
332 if (copy_to_user(arg, &range, sizeof(range)))
333 return -EFAULT;
334
335 return 0;
336}
337
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400338static noinline int create_subvol(struct btrfs_root *root,
339 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400340 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200341 u64 *async_transid,
342 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400343{
344 struct btrfs_trans_handle *trans;
345 struct btrfs_key key;
346 struct btrfs_root_item root_item;
347 struct btrfs_inode_item *inode_item;
348 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400349 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400350 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000351 struct inode *dir;
Alexander Block8ea05e32012-07-25 17:35:53 +0200352 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353 int ret;
354 int err;
355 u64 objectid;
356 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500357 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200358 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400359
Li Zefan581bb052011-04-20 10:06:11 +0800360 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400361 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400362 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000363
364 dir = parent->d_inode;
365
Josef Bacik9ed74f22009-09-11 16:12:44 -0400366 /*
367 * 1 - inode item
368 * 2 - refs
369 * 1 - root item
370 * 2 - dir items
371 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400372 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400373 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400374 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400375
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200376 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
377 inherit ? *inherit : NULL);
378 if (ret)
379 goto fail;
380
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400381 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200382 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400383 if (IS_ERR(leaf)) {
384 ret = PTR_ERR(leaf);
385 goto fail;
386 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400387
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400388 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400389 btrfs_set_header_bytenr(leaf, leaf->start);
390 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400391 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392 btrfs_set_header_owner(leaf, objectid);
393
394 write_extent_buffer(leaf, root->fs_info->fsid,
395 (unsigned long)btrfs_header_fsid(leaf),
396 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400397 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
398 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
399 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400400 btrfs_mark_buffer_dirty(leaf);
401
Alexander Block8ea05e32012-07-25 17:35:53 +0200402 memset(&root_item, 0, sizeof(root_item));
403
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404 inode_item = &root_item.inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400405 inode_item->generation = cpu_to_le64(1);
406 inode_item->size = cpu_to_le64(3);
407 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400408 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
410
Li Zefan08fe4db2011-03-28 02:01:25 +0000411 root_item.flags = 0;
412 root_item.byte_limit = 0;
413 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
414
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400415 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400416 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400417 btrfs_set_root_level(&root_item, 0);
418 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000419 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400420 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421
Alexander Block8ea05e32012-07-25 17:35:53 +0200422 btrfs_set_root_generation_v2(&root_item,
423 btrfs_root_generation(&root_item));
424 uuid_le_gen(&new_uuid);
425 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
426 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600427 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200428 root_item.ctime = root_item.otime;
429 btrfs_set_root_ctransid(&root_item, trans->transid);
430 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400431
Chris Mason925baed2008-06-25 16:01:30 -0400432 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400433 free_extent_buffer(leaf);
434 leaf = NULL;
435
436 btrfs_set_root_dirid(&root_item, new_dirid);
437
438 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400439 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400440 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
441 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
442 &root_item);
443 if (ret)
444 goto fail;
445
Yan, Zheng76dda932009-09-21 16:00:26 -0400446 key.offset = (u64)-1;
447 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100448 if (IS_ERR(new_root)) {
449 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
450 ret = PTR_ERR(new_root);
451 goto fail;
452 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400453
454 btrfs_record_root_in_trans(trans, new_root);
455
Josef Bacikd82a6f12011-05-11 15:26:06 -0400456 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700457 if (ret) {
458 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100459 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700460 goto fail;
461 }
462
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400463 /*
464 * insert the directory item
465 */
Chris Mason3de45862008-11-17 21:02:50 -0500466 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100467 if (ret) {
468 btrfs_abort_transaction(trans, root, ret);
469 goto fail;
470 }
Chris Mason3de45862008-11-17 21:02:50 -0500471
472 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800473 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500474 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100475 if (ret) {
476 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400477 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100478 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500479
Yan Zheng52c26172009-01-05 15:43:43 -0500480 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
481 ret = btrfs_update_inode(trans, root, dir);
482 BUG_ON(ret);
483
Chris Mason0660b5a2008-11-17 20:37:39 -0500484 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400485 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800486 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400487
Chris Mason0660b5a2008-11-17 20:37:39 -0500488 BUG_ON(ret);
489
Yan, Zheng76dda932009-09-21 16:00:26 -0400490 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400492 if (async_transid) {
493 *async_transid = trans->transid;
494 err = btrfs_commit_transaction_async(trans, root, 1);
495 } else {
496 err = btrfs_commit_transaction(trans, root);
497 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400498 if (err && !ret)
499 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400500 return ret;
501}
502
Sage Weil72fd0322010-10-29 15:41:32 -0400503static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800504 char *name, int namelen, u64 *async_transid,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200505 bool readonly, struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400506{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000507 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400508 struct btrfs_pending_snapshot *pending_snapshot;
509 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000510 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400511
512 if (!root->ref_cows)
513 return -EINVAL;
514
Yan, Zhenga22285a2010-05-16 10:48:46 -0400515 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
516 if (!pending_snapshot)
517 return -ENOMEM;
518
519 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
520 pending_snapshot->dentry = dentry;
521 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800522 pending_snapshot->readonly = readonly;
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200523 if (inherit) {
524 pending_snapshot->inherit = *inherit;
525 *inherit = NULL; /* take responsibility to free it */
526 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400527
528 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
529 if (IS_ERR(trans)) {
530 ret = PTR_ERR(trans);
531 goto fail;
532 }
533
534 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
535 BUG_ON(ret);
536
Josef Bacik83515832011-06-14 15:16:14 -0400537 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400538 list_add(&pending_snapshot->list,
539 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400540 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400541 if (async_transid) {
542 *async_transid = trans->transid;
543 ret = btrfs_commit_transaction_async(trans,
544 root->fs_info->extent_root, 1);
545 } else {
546 ret = btrfs_commit_transaction(trans,
547 root->fs_info->extent_root);
548 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400549 BUG_ON(ret);
550
551 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400552 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000553 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400554
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500555 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
556 if (ret)
557 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558
Al Viro2fbe8c82011-07-16 21:38:06 -0400559 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000560 if (IS_ERR(inode)) {
561 ret = PTR_ERR(inode);
562 goto fail;
563 }
564 BUG_ON(!inode);
565 d_instantiate(dentry, inode);
566 ret = 0;
567fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400568 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400569 return ret;
570}
571
Sage Weil4260f7c2010-10-29 15:46:43 -0400572/* copy of check_sticky in fs/namei.c()
573* It's inline, so penalty for filesystems that don't use sticky bit is
574* minimal.
575*/
576static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
577{
578 uid_t fsuid = current_fsuid();
579
580 if (!(dir->i_mode & S_ISVTX))
581 return 0;
582 if (inode->i_uid == fsuid)
583 return 0;
584 if (dir->i_uid == fsuid)
585 return 0;
586 return !capable(CAP_FOWNER);
587}
588
589/* copy of may_delete in fs/namei.c()
590 * Check whether we can remove a link victim from directory dir, check
591 * whether the type of victim is right.
592 * 1. We can't do it if dir is read-only (done in permission())
593 * 2. We should have write and exec permissions on dir
594 * 3. We can't remove anything from append-only dir
595 * 4. We can't do anything with immutable dir (done in permission())
596 * 5. If the sticky bit on dir is set we should either
597 * a. be owner of dir, or
598 * b. be owner of victim, or
599 * c. have CAP_FOWNER capability
600 * 6. If the victim is append-only or immutable we can't do antyhing with
601 * links pointing to it.
602 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
603 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
604 * 9. We can't remove a root or mountpoint.
605 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
606 * nfs_async_unlink().
607 */
608
609static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
610{
611 int error;
612
613 if (!victim->d_inode)
614 return -ENOENT;
615
616 BUG_ON(victim->d_parent->d_inode != dir);
617 audit_inode_child(victim, dir);
618
619 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
620 if (error)
621 return error;
622 if (IS_APPEND(dir))
623 return -EPERM;
624 if (btrfs_check_sticky(dir, victim->d_inode)||
625 IS_APPEND(victim->d_inode)||
626 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
627 return -EPERM;
628 if (isdir) {
629 if (!S_ISDIR(victim->d_inode->i_mode))
630 return -ENOTDIR;
631 if (IS_ROOT(victim))
632 return -EBUSY;
633 } else if (S_ISDIR(victim->d_inode->i_mode))
634 return -EISDIR;
635 if (IS_DEADDIR(dir))
636 return -ENOENT;
637 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
638 return -EBUSY;
639 return 0;
640}
641
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400642/* copy of may_create in fs/namei.c() */
643static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
644{
645 if (child->d_inode)
646 return -EEXIST;
647 if (IS_DEADDIR(dir))
648 return -ENOENT;
649 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
650}
651
652/*
653 * Create a new subvolume below @parent. This is largely modeled after
654 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
655 * inside this filesystem so it's quite a bit simpler.
656 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400657static noinline int btrfs_mksubvol(struct path *parent,
658 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400659 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200660 u64 *async_transid, bool readonly,
661 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400662{
Yan, Zheng76dda932009-09-21 16:00:26 -0400663 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400664 struct dentry *dentry;
665 int error;
666
Yan, Zheng76dda932009-09-21 16:00:26 -0400667 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400668
669 dentry = lookup_one_len(name, parent->dentry, namelen);
670 error = PTR_ERR(dentry);
671 if (IS_ERR(dentry))
672 goto out_unlock;
673
674 error = -EEXIST;
675 if (dentry->d_inode)
676 goto out_dput;
677
Yan, Zheng76dda932009-09-21 16:00:26 -0400678 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400679 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600680 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400681
Yan, Zheng76dda932009-09-21 16:00:26 -0400682 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
683
684 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
685 goto out_up_read;
686
Chris Mason3de45862008-11-17 21:02:50 -0500687 if (snap_src) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200688 error = create_snapshot(snap_src, dentry, name, namelen,
689 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500690 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400691 error = create_subvol(BTRFS_I(dir)->root, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200692 name, namelen, async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500693 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400694 if (!error)
695 fsnotify_mkdir(dir, dentry);
696out_up_read:
697 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400698out_dput:
699 dput(dentry);
700out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400701 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400702 return error;
703}
704
Chris Mason4cb53002011-05-24 15:35:30 -0400705/*
706 * When we're defragging a range, we don't want to kick it off again
707 * if it is really just waiting for delalloc to send it down.
708 * If we find a nice big extent or delalloc range for the bytes in the
709 * file you want to defrag, we return 0 to let you know to skip this
710 * part of the file
711 */
712static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
713{
714 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
715 struct extent_map *em = NULL;
716 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
717 u64 end;
718
719 read_lock(&em_tree->lock);
720 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
721 read_unlock(&em_tree->lock);
722
723 if (em) {
724 end = extent_map_end(em);
725 free_extent_map(em);
726 if (end - offset > thresh)
727 return 0;
728 }
729 /* if we already have a nice delalloc here, just stop */
730 thresh /= 2;
731 end = count_range_bits(io_tree, &offset, offset + thresh,
732 thresh, EXTENT_DELALLOC, 1);
733 if (end >= thresh)
734 return 0;
735 return 1;
736}
737
738/*
739 * helper function to walk through a file and find extents
740 * newer than a specific transid, and smaller than thresh.
741 *
742 * This is used by the defragging code to find new and small
743 * extents
744 */
745static int find_new_extents(struct btrfs_root *root,
746 struct inode *inode, u64 newer_than,
747 u64 *off, int thresh)
748{
749 struct btrfs_path *path;
750 struct btrfs_key min_key;
751 struct btrfs_key max_key;
752 struct extent_buffer *leaf;
753 struct btrfs_file_extent_item *extent;
754 int type;
755 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000756 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400757
758 path = btrfs_alloc_path();
759 if (!path)
760 return -ENOMEM;
761
David Sterbaa4689d22011-05-31 17:08:14 +0000762 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400763 min_key.type = BTRFS_EXTENT_DATA_KEY;
764 min_key.offset = *off;
765
David Sterbaa4689d22011-05-31 17:08:14 +0000766 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400767 max_key.type = (u8)-1;
768 max_key.offset = (u64)-1;
769
770 path->keep_locks = 1;
771
772 while(1) {
773 ret = btrfs_search_forward(root, &min_key, &max_key,
774 path, 0, newer_than);
775 if (ret != 0)
776 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000777 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400778 goto none;
779 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
780 goto none;
781
782 leaf = path->nodes[0];
783 extent = btrfs_item_ptr(leaf, path->slots[0],
784 struct btrfs_file_extent_item);
785
786 type = btrfs_file_extent_type(leaf, extent);
787 if (type == BTRFS_FILE_EXTENT_REG &&
788 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
789 check_defrag_in_cache(inode, min_key.offset, thresh)) {
790 *off = min_key.offset;
791 btrfs_free_path(path);
792 return 0;
793 }
794
795 if (min_key.offset == (u64)-1)
796 goto none;
797
798 min_key.offset++;
799 btrfs_release_path(path);
800 }
801none:
802 btrfs_free_path(path);
803 return -ENOENT;
804}
805
Li Zefan6c282eb2012-06-11 16:03:35 +0800806static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400807{
808 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500809 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800810 struct extent_map *em;
811 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500812
813 /*
814 * hopefully we have this extent in the tree already, try without
815 * the full extent lock
816 */
817 read_lock(&em_tree->lock);
818 em = lookup_extent_mapping(em_tree, start, len);
819 read_unlock(&em_tree->lock);
820
821 if (!em) {
822 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100823 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500824 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100825 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500826
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000827 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800828 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500829 }
830
Li Zefan6c282eb2012-06-11 16:03:35 +0800831 return em;
832}
833
834static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
835{
836 struct extent_map *next;
837 bool ret = true;
838
839 /* this is the last extent */
840 if (em->start + em->len >= i_size_read(inode))
841 return false;
842
843 next = defrag_lookup_extent(inode, em->start + em->len);
844 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
845 ret = false;
846
847 free_extent_map(next);
848 return ret;
849}
850
851static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400852 u64 *last_len, u64 *skip, u64 *defrag_end,
853 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800854{
855 struct extent_map *em;
856 int ret = 1;
857 bool next_mergeable = true;
858
859 /*
860 * make sure that once we start defragging an extent, we keep on
861 * defragging it
862 */
863 if (start < *defrag_end)
864 return 1;
865
866 *skip = 0;
867
868 em = defrag_lookup_extent(inode, start);
869 if (!em)
870 return 0;
871
Chris Mason940100a2010-03-10 10:52:59 -0500872 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400873 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500874 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400875 goto out;
876 }
877
Li Zefan6c282eb2012-06-11 16:03:35 +0800878 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500879
880 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800881 * we hit a real extent, if it is big or the next extent is not a
882 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500883 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400884 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800885 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500886 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400887out:
Chris Mason940100a2010-03-10 10:52:59 -0500888 /*
889 * last_len ends up being a counter of how many bytes we've defragged.
890 * every time we choose not to defrag an extent, we reset *last_len
891 * so that the next tiny extent will force a defrag.
892 *
893 * The end result of this is that tiny extents before a single big
894 * extent will force at least part of that big extent to be defragged.
895 */
896 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500897 *defrag_end = extent_map_end(em);
898 } else {
899 *last_len = 0;
900 *skip = extent_map_end(em);
901 *defrag_end = 0;
902 }
903
904 free_extent_map(em);
905 return ret;
906}
907
Chris Mason4cb53002011-05-24 15:35:30 -0400908/*
909 * it doesn't do much good to defrag one or two pages
910 * at a time. This pulls in a nice chunk of pages
911 * to COW and defrag.
912 *
913 * It also makes sure the delalloc code has enough
914 * dirty data to avoid making new small extents as part
915 * of the defrag
916 *
917 * It's a good idea to start RA on this range
918 * before calling this.
919 */
920static int cluster_pages_for_defrag(struct inode *inode,
921 struct page **pages,
922 unsigned long start_index,
923 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400924{
Chris Mason4cb53002011-05-24 15:35:30 -0400925 unsigned long file_end;
926 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400927 u64 page_start;
928 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400929 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400930 int ret;
931 int i;
932 int i_done;
933 struct btrfs_ordered_extent *ordered;
934 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800935 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400936 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400937
Chris Mason4cb53002011-05-24 15:35:30 -0400938 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400939 if (!isize || start_index > file_end)
940 return 0;
941
942 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400943
944 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400945 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400946 if (ret)
947 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400948 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800949 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400950
951 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400952 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400953 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800954again:
Josef Bacika94733d2011-07-11 10:47:06 -0400955 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800956 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400957 if (!page)
958 break;
959
Miao Xie600a45e2012-02-16 15:01:24 +0800960 page_start = page_offset(page);
961 page_end = page_start + PAGE_CACHE_SIZE - 1;
962 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100963 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800964 ordered = btrfs_lookup_ordered_extent(inode,
965 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100966 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800967 if (!ordered)
968 break;
969
970 unlock_page(page);
971 btrfs_start_ordered_extent(inode, ordered, 1);
972 btrfs_put_ordered_extent(ordered);
973 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -0400974 /*
975 * we unlocked the page above, so we need check if
976 * it was released or not.
977 */
978 if (page->mapping != inode->i_mapping) {
979 unlock_page(page);
980 page_cache_release(page);
981 goto again;
982 }
Miao Xie600a45e2012-02-16 15:01:24 +0800983 }
984
Chris Mason4cb53002011-05-24 15:35:30 -0400985 if (!PageUptodate(page)) {
986 btrfs_readpage(NULL, page);
987 lock_page(page);
988 if (!PageUptodate(page)) {
989 unlock_page(page);
990 page_cache_release(page);
991 ret = -EIO;
992 break;
993 }
994 }
Miao Xie600a45e2012-02-16 15:01:24 +0800995
Miao Xie600a45e2012-02-16 15:01:24 +0800996 if (page->mapping != inode->i_mapping) {
997 unlock_page(page);
998 page_cache_release(page);
999 goto again;
1000 }
1001
Chris Mason4cb53002011-05-24 15:35:30 -04001002 pages[i] = page;
1003 i_done++;
1004 }
1005 if (!i_done || ret)
1006 goto out;
1007
1008 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1009 goto out;
1010
1011 /*
1012 * so now we have a nice long stream of locked
1013 * and up to date pages, lets wait on them
1014 */
1015 for (i = 0; i < i_done; i++)
1016 wait_on_page_writeback(pages[i]);
1017
1018 page_start = page_offset(pages[0]);
1019 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1020
1021 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001022 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001023 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1024 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1025 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
1026 GFP_NOFS);
1027
Liu Bo1f12bd02012-03-29 09:57:44 -04001028 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001029 spin_lock(&BTRFS_I(inode)->lock);
1030 BTRFS_I(inode)->outstanding_extents++;
1031 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001032 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001033 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001034 }
1035
1036
1037 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
1038 &cached_state);
1039
1040 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1041 page_start, page_end - 1, &cached_state,
1042 GFP_NOFS);
1043
1044 for (i = 0; i < i_done; i++) {
1045 clear_page_dirty_for_io(pages[i]);
1046 ClearPageChecked(pages[i]);
1047 set_page_extent_mapped(pages[i]);
1048 set_page_dirty(pages[i]);
1049 unlock_page(pages[i]);
1050 page_cache_release(pages[i]);
1051 }
1052 return i_done;
1053out:
1054 for (i = 0; i < i_done; i++) {
1055 unlock_page(pages[i]);
1056 page_cache_release(pages[i]);
1057 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001058 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001059 return ret;
1060
1061}
1062
1063int btrfs_defrag_file(struct inode *inode, struct file *file,
1064 struct btrfs_ioctl_defrag_range_args *range,
1065 u64 newer_than, unsigned long max_to_defrag)
1066{
1067 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001068 struct file_ra_state *ra = NULL;
1069 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001070 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001071 u64 last_len = 0;
1072 u64 skip = 0;
1073 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001074 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001075 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001076 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001077 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001078 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001079 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001080 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001081 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1082 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001083 u64 new_align = ~((u64)128 * 1024 - 1);
1084 struct page **pages = NULL;
1085
1086 if (extent_thresh == 0)
1087 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001088
1089 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1090 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1091 return -EINVAL;
1092 if (range->compress_type)
1093 compress_type = range->compress_type;
1094 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001095
Li Zefan151a31b2011-09-02 15:56:39 +08001096 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001097 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001098
Chris Mason4cb53002011-05-24 15:35:30 -04001099 /*
1100 * if we were not given a file, allocate a readahead
1101 * context
1102 */
1103 if (!file) {
1104 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1105 if (!ra)
1106 return -ENOMEM;
1107 file_ra_state_init(ra, inode->i_mapping);
1108 } else {
1109 ra = &file->f_ra;
1110 }
1111
Li Zefan008873e2011-09-02 15:57:07 +08001112 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001113 GFP_NOFS);
1114 if (!pages) {
1115 ret = -ENOMEM;
1116 goto out_ra;
1117 }
1118
1119 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001120 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001121 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001122 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1123 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001124 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001125 }
1126
Chris Mason4cb53002011-05-24 15:35:30 -04001127 if (newer_than) {
1128 ret = find_new_extents(root, inode, newer_than,
1129 &newer_off, 64 * 1024);
1130 if (!ret) {
1131 range->start = newer_off;
1132 /*
1133 * we always align our defrag to help keep
1134 * the extents in the file evenly spaced
1135 */
1136 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001137 } else
1138 goto out_ra;
1139 } else {
1140 i = range->start >> PAGE_CACHE_SHIFT;
1141 }
1142 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001143 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001144
Li Zefan2a0f7f52011-10-10 15:43:34 -04001145 /*
1146 * make writeback starts from i, so the defrag range can be
1147 * written sequentially.
1148 */
1149 if (i < inode->i_mapping->writeback_index)
1150 inode->i_mapping->writeback_index = i;
1151
Chris Masonf7f43cc2011-10-11 11:41:40 -04001152 while (i <= last_index && defrag_count < max_to_defrag &&
1153 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1154 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001155 /*
1156 * make sure we stop running if someone unmounts
1157 * the FS
1158 */
1159 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1160 break;
1161
Liu Bo66c26892012-03-29 09:57:45 -04001162 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001163 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001164 &defrag_end, range->flags &
1165 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001166 unsigned long next;
1167 /*
1168 * the should_defrag function tells us how much to skip
1169 * bump our counter by the suggested amount
1170 */
1171 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1172 i = max(i + 1, next);
1173 continue;
1174 }
Li Zefan008873e2011-09-02 15:57:07 +08001175
1176 if (!newer_than) {
1177 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1178 PAGE_CACHE_SHIFT) - i;
1179 cluster = min(cluster, max_cluster);
1180 } else {
1181 cluster = max_cluster;
1182 }
1183
Chris Mason1e701a32010-03-11 09:42:04 -05001184 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001185 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001186
Li Zefan008873e2011-09-02 15:57:07 +08001187 if (i + cluster > ra_index) {
1188 ra_index = max(i, ra_index);
1189 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1190 cluster);
1191 ra_index += max_cluster;
1192 }
Chris Mason940100a2010-03-10 10:52:59 -05001193
Liu Boecb8bea2012-03-29 09:57:44 -04001194 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001195 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001196 if (ret < 0) {
1197 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001198 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001199 }
Chris Mason940100a2010-03-10 10:52:59 -05001200
Chris Mason4cb53002011-05-24 15:35:30 -04001201 defrag_count += ret;
1202 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001203 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001204
1205 if (newer_than) {
1206 if (newer_off == (u64)-1)
1207 break;
1208
Liu Boe1f041e2012-03-29 09:57:45 -04001209 if (ret > 0)
1210 i += ret;
1211
Chris Mason4cb53002011-05-24 15:35:30 -04001212 newer_off = max(newer_off + 1,
1213 (u64)i << PAGE_CACHE_SHIFT);
1214
1215 ret = find_new_extents(root, inode,
1216 newer_than, &newer_off,
1217 64 * 1024);
1218 if (!ret) {
1219 range->start = newer_off;
1220 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001221 } else {
1222 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001223 }
Chris Mason4cb53002011-05-24 15:35:30 -04001224 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001225 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001226 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001227 last_len += ret << PAGE_CACHE_SHIFT;
1228 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001229 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001230 last_len = 0;
1231 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001232 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001233 }
1234
Chris Mason1e701a32010-03-11 09:42:04 -05001235 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1236 filemap_flush(inode->i_mapping);
1237
1238 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1239 /* the filemap_flush will queue IO into the worker threads, but
1240 * we have to make sure the IO is actually started and that
1241 * ordered extents get created before we return
1242 */
1243 atomic_inc(&root->fs_info->async_submit_draining);
1244 while (atomic_read(&root->fs_info->nr_async_submits) ||
1245 atomic_read(&root->fs_info->async_delalloc_pages)) {
1246 wait_event(root->fs_info->async_submit_wait,
1247 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1248 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1249 }
1250 atomic_dec(&root->fs_info->async_submit_draining);
1251
1252 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001253 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001254 mutex_unlock(&inode->i_mutex);
1255 }
1256
Li Zefan1a419d82010-10-25 15:12:50 +08001257 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001258 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001259 }
1260
Diego Calleja60ccf822011-09-01 16:33:57 +02001261 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001262
Chris Mason4cb53002011-05-24 15:35:30 -04001263out_ra:
1264 if (!file)
1265 kfree(ra);
1266 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001267 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001268}
1269
Yan, Zheng76dda932009-09-21 16:00:26 -04001270static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1271 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001272{
1273 u64 new_size;
1274 u64 old_size;
1275 u64 devid = 1;
1276 struct btrfs_ioctl_vol_args *vol_args;
1277 struct btrfs_trans_handle *trans;
1278 struct btrfs_device *device = NULL;
1279 char *sizestr;
1280 char *devstr = NULL;
1281 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001282 int mod = 0;
1283
Yan Zhengc146afa2008-11-12 14:34:12 -05001284 if (root->fs_info->sb->s_flags & MS_RDONLY)
1285 return -EROFS;
1286
Chris Masone441d542009-01-05 16:57:23 -05001287 if (!capable(CAP_SYS_ADMIN))
1288 return -EPERM;
1289
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001290 mutex_lock(&root->fs_info->volume_mutex);
1291 if (root->fs_info->balance_ctl) {
1292 printk(KERN_INFO "btrfs: balance in progress\n");
1293 ret = -EINVAL;
1294 goto out;
1295 }
1296
Li Zefandae7b662009-04-08 15:06:54 +08001297 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001298 if (IS_ERR(vol_args)) {
1299 ret = PTR_ERR(vol_args);
1300 goto out;
1301 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001302
1303 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001304
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001305 sizestr = vol_args->name;
1306 devstr = strchr(sizestr, ':');
1307 if (devstr) {
1308 char *end;
1309 sizestr = devstr + 1;
1310 *devstr = '\0';
1311 devstr = vol_args->name;
1312 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001313 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001314 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001315 }
Yan Zheng2b820322008-11-17 21:11:30 -05001316 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001317 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001318 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001319 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001320 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001321 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001322 }
Liu Bo4e42ae12012-06-14 02:23:19 -06001323 if (device->fs_devices && device->fs_devices->seeding) {
1324 printk(KERN_INFO "btrfs: resizer unable to apply on "
Chris Masona8c4a332012-06-15 20:07:17 -04001325 "seeding device %llu\n",
1326 (unsigned long long)devid);
Liu Bo4e42ae12012-06-14 02:23:19 -06001327 ret = -EINVAL;
1328 goto out_free;
1329 }
1330
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001331 if (!strcmp(sizestr, "max"))
1332 new_size = device->bdev->bd_inode->i_size;
1333 else {
1334 if (sizestr[0] == '-') {
1335 mod = -1;
1336 sizestr++;
1337 } else if (sizestr[0] == '+') {
1338 mod = 1;
1339 sizestr++;
1340 }
Akinobu Mita91748462010-02-28 10:59:11 +00001341 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001342 if (new_size == 0) {
1343 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001344 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001345 }
1346 }
1347
1348 old_size = device->total_bytes;
1349
1350 if (mod < 0) {
1351 if (new_size > old_size) {
1352 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001353 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001354 }
1355 new_size = old_size - new_size;
1356 } else if (mod > 0) {
1357 new_size = old_size + new_size;
1358 }
1359
1360 if (new_size < 256 * 1024 * 1024) {
1361 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001362 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001363 }
1364 if (new_size > device->bdev->bd_inode->i_size) {
1365 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001366 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001367 }
1368
1369 do_div(new_size, root->sectorsize);
1370 new_size *= root->sectorsize;
1371
Josef Bacik606686e2012-06-04 14:03:51 -04001372 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1373 rcu_str_deref(device->name),
1374 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001375
1376 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001377 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001378 if (IS_ERR(trans)) {
1379 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001380 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001381 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001382 ret = btrfs_grow_device(trans, device, new_size);
1383 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001384 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001385 ret = btrfs_shrink_device(device, new_size);
1386 }
1387
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001388out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001389 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001390out:
1391 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001392 return ret;
1393}
1394
Sage Weil72fd0322010-10-29 15:41:32 -04001395static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001396 char *name, unsigned long fd, int subvol,
1397 u64 *transid, bool readonly,
1398 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001399{
Chris Mason3de45862008-11-17 21:02:50 -05001400 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001401 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001402 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001403
Liu Boa874a632012-06-29 03:58:46 -06001404 ret = mnt_want_write_file(file);
1405 if (ret)
1406 goto out;
1407
Sage Weil72fd0322010-10-29 15:41:32 -04001408 namelen = strlen(name);
1409 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001410 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001411 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001412 }
1413
Chris Mason16780ca2012-02-20 22:14:55 -05001414 if (name[0] == '.' &&
1415 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1416 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001417 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001418 }
1419
Chris Mason3de45862008-11-17 21:02:50 -05001420 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001421 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001422 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001423 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001424 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001425 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001426 if (!src_file) {
1427 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001428 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001429 }
1430
1431 src_inode = src_file->f_path.dentry->d_inode;
1432 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001433 printk(KERN_INFO "btrfs: Snapshot src from "
1434 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001435 ret = -EINVAL;
1436 fput(src_file);
Liu Boa874a632012-06-29 03:58:46 -06001437 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001438 }
Sage Weil72fd0322010-10-29 15:41:32 -04001439 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1440 BTRFS_I(src_inode)->root,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001441 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001442 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001443 }
Liu Boa874a632012-06-29 03:58:46 -06001444out_drop_write:
1445 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001446out:
Sage Weil72fd0322010-10-29 15:41:32 -04001447 return ret;
1448}
1449
1450static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001451 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001452{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001453 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001454 int ret;
1455
Li Zefanfa0d2b92010-12-20 15:53:28 +08001456 vol_args = memdup_user(arg, sizeof(*vol_args));
1457 if (IS_ERR(vol_args))
1458 return PTR_ERR(vol_args);
1459 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001460
Li Zefanfa0d2b92010-12-20 15:53:28 +08001461 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001462 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001463 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001464
Li Zefanfa0d2b92010-12-20 15:53:28 +08001465 kfree(vol_args);
1466 return ret;
1467}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001468
Li Zefanfa0d2b92010-12-20 15:53:28 +08001469static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1470 void __user *arg, int subvol)
1471{
1472 struct btrfs_ioctl_vol_args_v2 *vol_args;
1473 int ret;
1474 u64 transid = 0;
1475 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001476 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001477 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001478
Li Zefanfa0d2b92010-12-20 15:53:28 +08001479 vol_args = memdup_user(arg, sizeof(*vol_args));
1480 if (IS_ERR(vol_args))
1481 return PTR_ERR(vol_args);
1482 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001483
Li Zefanb83cc962010-12-20 16:04:08 +08001484 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001485 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1486 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001487 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001488 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001489 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001490
1491 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1492 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001493 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1494 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001495 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1496 if (vol_args->size > PAGE_CACHE_SIZE) {
1497 ret = -EINVAL;
1498 goto out;
1499 }
1500 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1501 if (IS_ERR(inherit)) {
1502 ret = PTR_ERR(inherit);
1503 goto out;
1504 }
1505 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001506
1507 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001508 vol_args->fd, subvol, ptr,
1509 readonly, &inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001510
1511 if (ret == 0 && ptr &&
1512 copy_to_user(arg +
1513 offsetof(struct btrfs_ioctl_vol_args_v2,
1514 transid), ptr, sizeof(*ptr)))
1515 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001516out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001517 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001518 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001519 return ret;
1520}
1521
Li Zefan0caa1022010-12-20 16:30:25 +08001522static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1523 void __user *arg)
1524{
1525 struct inode *inode = fdentry(file)->d_inode;
1526 struct btrfs_root *root = BTRFS_I(inode)->root;
1527 int ret = 0;
1528 u64 flags = 0;
1529
Li Zefan33345d012011-04-20 10:31:50 +08001530 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001531 return -EINVAL;
1532
1533 down_read(&root->fs_info->subvol_sem);
1534 if (btrfs_root_readonly(root))
1535 flags |= BTRFS_SUBVOL_RDONLY;
1536 up_read(&root->fs_info->subvol_sem);
1537
1538 if (copy_to_user(arg, &flags, sizeof(flags)))
1539 ret = -EFAULT;
1540
1541 return ret;
1542}
1543
1544static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1545 void __user *arg)
1546{
1547 struct inode *inode = fdentry(file)->d_inode;
1548 struct btrfs_root *root = BTRFS_I(inode)->root;
1549 struct btrfs_trans_handle *trans;
1550 u64 root_flags;
1551 u64 flags;
1552 int ret = 0;
1553
Liu Bob9ca0662012-06-29 03:58:49 -06001554 ret = mnt_want_write_file(file);
1555 if (ret)
1556 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001557
Liu Bob9ca0662012-06-29 03:58:49 -06001558 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1559 ret = -EINVAL;
1560 goto out_drop_write;
1561 }
Li Zefan0caa1022010-12-20 16:30:25 +08001562
Liu Bob9ca0662012-06-29 03:58:49 -06001563 if (copy_from_user(&flags, arg, sizeof(flags))) {
1564 ret = -EFAULT;
1565 goto out_drop_write;
1566 }
Li Zefan0caa1022010-12-20 16:30:25 +08001567
Liu Bob9ca0662012-06-29 03:58:49 -06001568 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1569 ret = -EINVAL;
1570 goto out_drop_write;
1571 }
Li Zefan0caa1022010-12-20 16:30:25 +08001572
Liu Bob9ca0662012-06-29 03:58:49 -06001573 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1574 ret = -EOPNOTSUPP;
1575 goto out_drop_write;
1576 }
Li Zefan0caa1022010-12-20 16:30:25 +08001577
Liu Bob9ca0662012-06-29 03:58:49 -06001578 if (!inode_owner_or_capable(inode)) {
1579 ret = -EACCES;
1580 goto out_drop_write;
1581 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001582
Li Zefan0caa1022010-12-20 16:30:25 +08001583 down_write(&root->fs_info->subvol_sem);
1584
1585 /* nothing to do */
1586 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001587 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001588
1589 root_flags = btrfs_root_flags(&root->root_item);
1590 if (flags & BTRFS_SUBVOL_RDONLY)
1591 btrfs_set_root_flags(&root->root_item,
1592 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1593 else
1594 btrfs_set_root_flags(&root->root_item,
1595 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1596
1597 trans = btrfs_start_transaction(root, 1);
1598 if (IS_ERR(trans)) {
1599 ret = PTR_ERR(trans);
1600 goto out_reset;
1601 }
1602
Li Zefanb4dc2b82011-02-16 06:06:34 +00001603 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001604 &root->root_key, &root->root_item);
1605
1606 btrfs_commit_transaction(trans, root);
1607out_reset:
1608 if (ret)
1609 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001610out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001611 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001612out_drop_write:
1613 mnt_drop_write_file(file);
1614out:
Li Zefan0caa1022010-12-20 16:30:25 +08001615 return ret;
1616}
1617
Yan, Zheng76dda932009-09-21 16:00:26 -04001618/*
1619 * helper to check if the subvolume references other subvolumes
1620 */
1621static noinline int may_destroy_subvol(struct btrfs_root *root)
1622{
1623 struct btrfs_path *path;
1624 struct btrfs_key key;
1625 int ret;
1626
1627 path = btrfs_alloc_path();
1628 if (!path)
1629 return -ENOMEM;
1630
1631 key.objectid = root->root_key.objectid;
1632 key.type = BTRFS_ROOT_REF_KEY;
1633 key.offset = (u64)-1;
1634
1635 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1636 &key, path, 0, 0);
1637 if (ret < 0)
1638 goto out;
1639 BUG_ON(ret == 0);
1640
1641 ret = 0;
1642 if (path->slots[0] > 0) {
1643 path->slots[0]--;
1644 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1645 if (key.objectid == root->root_key.objectid &&
1646 key.type == BTRFS_ROOT_REF_KEY)
1647 ret = -ENOTEMPTY;
1648 }
1649out:
1650 btrfs_free_path(path);
1651 return ret;
1652}
1653
Chris Masonac8e9812010-02-28 15:39:26 -05001654static noinline int key_in_sk(struct btrfs_key *key,
1655 struct btrfs_ioctl_search_key *sk)
1656{
Chris Masonabc6e132010-03-18 12:10:08 -04001657 struct btrfs_key test;
1658 int ret;
1659
1660 test.objectid = sk->min_objectid;
1661 test.type = sk->min_type;
1662 test.offset = sk->min_offset;
1663
1664 ret = btrfs_comp_cpu_keys(key, &test);
1665 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001666 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001667
1668 test.objectid = sk->max_objectid;
1669 test.type = sk->max_type;
1670 test.offset = sk->max_offset;
1671
1672 ret = btrfs_comp_cpu_keys(key, &test);
1673 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001674 return 0;
1675 return 1;
1676}
1677
1678static noinline int copy_to_sk(struct btrfs_root *root,
1679 struct btrfs_path *path,
1680 struct btrfs_key *key,
1681 struct btrfs_ioctl_search_key *sk,
1682 char *buf,
1683 unsigned long *sk_offset,
1684 int *num_found)
1685{
1686 u64 found_transid;
1687 struct extent_buffer *leaf;
1688 struct btrfs_ioctl_search_header sh;
1689 unsigned long item_off;
1690 unsigned long item_len;
1691 int nritems;
1692 int i;
1693 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001694 int ret = 0;
1695
1696 leaf = path->nodes[0];
1697 slot = path->slots[0];
1698 nritems = btrfs_header_nritems(leaf);
1699
1700 if (btrfs_header_generation(leaf) > sk->max_transid) {
1701 i = nritems;
1702 goto advance_key;
1703 }
1704 found_transid = btrfs_header_generation(leaf);
1705
1706 for (i = slot; i < nritems; i++) {
1707 item_off = btrfs_item_ptr_offset(leaf, i);
1708 item_len = btrfs_item_size_nr(leaf, i);
1709
1710 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1711 item_len = 0;
1712
1713 if (sizeof(sh) + item_len + *sk_offset >
1714 BTRFS_SEARCH_ARGS_BUFSIZE) {
1715 ret = 1;
1716 goto overflow;
1717 }
1718
1719 btrfs_item_key_to_cpu(leaf, key, i);
1720 if (!key_in_sk(key, sk))
1721 continue;
1722
1723 sh.objectid = key->objectid;
1724 sh.offset = key->offset;
1725 sh.type = key->type;
1726 sh.len = item_len;
1727 sh.transid = found_transid;
1728
1729 /* copy search result header */
1730 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1731 *sk_offset += sizeof(sh);
1732
1733 if (item_len) {
1734 char *p = buf + *sk_offset;
1735 /* copy the item */
1736 read_extent_buffer(leaf, p,
1737 item_off, item_len);
1738 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001739 }
Hugo Millse2156862011-05-14 17:43:41 +00001740 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001741
1742 if (*num_found >= sk->nr_items)
1743 break;
1744 }
1745advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001746 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001747 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1748 key->offset++;
1749 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1750 key->offset = 0;
1751 key->type++;
1752 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1753 key->offset = 0;
1754 key->type = 0;
1755 key->objectid++;
1756 } else
1757 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001758overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001759 return ret;
1760}
1761
1762static noinline int search_ioctl(struct inode *inode,
1763 struct btrfs_ioctl_search_args *args)
1764{
1765 struct btrfs_root *root;
1766 struct btrfs_key key;
1767 struct btrfs_key max_key;
1768 struct btrfs_path *path;
1769 struct btrfs_ioctl_search_key *sk = &args->key;
1770 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1771 int ret;
1772 int num_found = 0;
1773 unsigned long sk_offset = 0;
1774
1775 path = btrfs_alloc_path();
1776 if (!path)
1777 return -ENOMEM;
1778
1779 if (sk->tree_id == 0) {
1780 /* search the root of the inode that was passed */
1781 root = BTRFS_I(inode)->root;
1782 } else {
1783 key.objectid = sk->tree_id;
1784 key.type = BTRFS_ROOT_ITEM_KEY;
1785 key.offset = (u64)-1;
1786 root = btrfs_read_fs_root_no_name(info, &key);
1787 if (IS_ERR(root)) {
1788 printk(KERN_ERR "could not find root %llu\n",
1789 sk->tree_id);
1790 btrfs_free_path(path);
1791 return -ENOENT;
1792 }
1793 }
1794
1795 key.objectid = sk->min_objectid;
1796 key.type = sk->min_type;
1797 key.offset = sk->min_offset;
1798
1799 max_key.objectid = sk->max_objectid;
1800 max_key.type = sk->max_type;
1801 max_key.offset = sk->max_offset;
1802
1803 path->keep_locks = 1;
1804
1805 while(1) {
1806 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1807 sk->min_transid);
1808 if (ret != 0) {
1809 if (ret > 0)
1810 ret = 0;
1811 goto err;
1812 }
1813 ret = copy_to_sk(root, path, &key, sk, args->buf,
1814 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001815 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001816 if (ret || num_found >= sk->nr_items)
1817 break;
1818
1819 }
1820 ret = 0;
1821err:
1822 sk->nr_items = num_found;
1823 btrfs_free_path(path);
1824 return ret;
1825}
1826
1827static noinline int btrfs_ioctl_tree_search(struct file *file,
1828 void __user *argp)
1829{
1830 struct btrfs_ioctl_search_args *args;
1831 struct inode *inode;
1832 int ret;
1833
1834 if (!capable(CAP_SYS_ADMIN))
1835 return -EPERM;
1836
Julia Lawall2354d082010-10-29 15:14:18 -04001837 args = memdup_user(argp, sizeof(*args));
1838 if (IS_ERR(args))
1839 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001840
Chris Masonac8e9812010-02-28 15:39:26 -05001841 inode = fdentry(file)->d_inode;
1842 ret = search_ioctl(inode, args);
1843 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1844 ret = -EFAULT;
1845 kfree(args);
1846 return ret;
1847}
1848
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001849/*
Chris Masonac8e9812010-02-28 15:39:26 -05001850 * Search INODE_REFs to identify path name of 'dirid' directory
1851 * in a 'tree_id' tree. and sets path name to 'name'.
1852 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001853static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1854 u64 tree_id, u64 dirid, char *name)
1855{
1856 struct btrfs_root *root;
1857 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001858 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001859 int ret = -1;
1860 int slot;
1861 int len;
1862 int total_len = 0;
1863 struct btrfs_inode_ref *iref;
1864 struct extent_buffer *l;
1865 struct btrfs_path *path;
1866
1867 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1868 name[0]='\0';
1869 return 0;
1870 }
1871
1872 path = btrfs_alloc_path();
1873 if (!path)
1874 return -ENOMEM;
1875
Chris Masonac8e9812010-02-28 15:39:26 -05001876 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001877
1878 key.objectid = tree_id;
1879 key.type = BTRFS_ROOT_ITEM_KEY;
1880 key.offset = (u64)-1;
1881 root = btrfs_read_fs_root_no_name(info, &key);
1882 if (IS_ERR(root)) {
1883 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001884 ret = -ENOENT;
1885 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001886 }
1887
1888 key.objectid = dirid;
1889 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001890 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001891
1892 while(1) {
1893 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1894 if (ret < 0)
1895 goto out;
1896
1897 l = path->nodes[0];
1898 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001899 if (ret > 0 && slot > 0)
1900 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001901 btrfs_item_key_to_cpu(l, &key, slot);
1902
1903 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001904 key.type != BTRFS_INODE_REF_KEY)) {
1905 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001906 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001907 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001908
1909 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1910 len = btrfs_inode_ref_name_len(l, iref);
1911 ptr -= len + 1;
1912 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001913 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001914 goto out;
1915
1916 *(ptr + len) = '/';
1917 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1918
1919 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1920 break;
1921
David Sterbab3b4aa72011-04-21 01:20:15 +02001922 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001923 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001924 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001925 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001926 }
Chris Masonac8e9812010-02-28 15:39:26 -05001927 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001928 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001929 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001930 name[total_len]='\0';
1931 ret = 0;
1932out:
1933 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001934 return ret;
1935}
1936
1937static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1938 void __user *argp)
1939{
1940 struct btrfs_ioctl_ino_lookup_args *args;
1941 struct inode *inode;
1942 int ret;
1943
1944 if (!capable(CAP_SYS_ADMIN))
1945 return -EPERM;
1946
Julia Lawall2354d082010-10-29 15:14:18 -04001947 args = memdup_user(argp, sizeof(*args));
1948 if (IS_ERR(args))
1949 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001950
Chris Masonac8e9812010-02-28 15:39:26 -05001951 inode = fdentry(file)->d_inode;
1952
Chris Mason1b53ac42010-03-18 12:17:05 -04001953 if (args->treeid == 0)
1954 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1955
Chris Masonac8e9812010-02-28 15:39:26 -05001956 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1957 args->treeid, args->objectid,
1958 args->name);
1959
1960 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1961 ret = -EFAULT;
1962
1963 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001964 return ret;
1965}
1966
Yan, Zheng76dda932009-09-21 16:00:26 -04001967static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1968 void __user *arg)
1969{
1970 struct dentry *parent = fdentry(file);
1971 struct dentry *dentry;
1972 struct inode *dir = parent->d_inode;
1973 struct inode *inode;
1974 struct btrfs_root *root = BTRFS_I(dir)->root;
1975 struct btrfs_root *dest = NULL;
1976 struct btrfs_ioctl_vol_args *vol_args;
1977 struct btrfs_trans_handle *trans;
1978 int namelen;
1979 int ret;
1980 int err = 0;
1981
Yan, Zheng76dda932009-09-21 16:00:26 -04001982 vol_args = memdup_user(arg, sizeof(*vol_args));
1983 if (IS_ERR(vol_args))
1984 return PTR_ERR(vol_args);
1985
1986 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1987 namelen = strlen(vol_args->name);
1988 if (strchr(vol_args->name, '/') ||
1989 strncmp(vol_args->name, "..", namelen) == 0) {
1990 err = -EINVAL;
1991 goto out;
1992 }
1993
Al Viroa561be72011-11-23 11:57:51 -05001994 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001995 if (err)
1996 goto out;
1997
1998 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1999 dentry = lookup_one_len(vol_args->name, parent, namelen);
2000 if (IS_ERR(dentry)) {
2001 err = PTR_ERR(dentry);
2002 goto out_unlock_dir;
2003 }
2004
2005 if (!dentry->d_inode) {
2006 err = -ENOENT;
2007 goto out_dput;
2008 }
2009
2010 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002011 dest = BTRFS_I(inode)->root;
2012 if (!capable(CAP_SYS_ADMIN)){
2013 /*
2014 * Regular user. Only allow this with a special mount
2015 * option, when the user has write+exec access to the
2016 * subvol root, and when rmdir(2) would have been
2017 * allowed.
2018 *
2019 * Note that this is _not_ check that the subvol is
2020 * empty or doesn't contain data that we wouldn't
2021 * otherwise be able to delete.
2022 *
2023 * Users who want to delete empty subvols should try
2024 * rmdir(2).
2025 */
2026 err = -EPERM;
2027 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2028 goto out_dput;
2029
2030 /*
2031 * Do not allow deletion if the parent dir is the same
2032 * as the dir to be deleted. That means the ioctl
2033 * must be called on the dentry referencing the root
2034 * of the subvol, not a random directory contained
2035 * within it.
2036 */
2037 err = -EINVAL;
2038 if (root == dest)
2039 goto out_dput;
2040
2041 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2042 if (err)
2043 goto out_dput;
2044
2045 /* check if subvolume may be deleted by a non-root user */
2046 err = btrfs_may_delete(dir, dentry, 1);
2047 if (err)
2048 goto out_dput;
2049 }
2050
Li Zefan33345d012011-04-20 10:31:50 +08002051 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002052 err = -EINVAL;
2053 goto out_dput;
2054 }
2055
Yan, Zheng76dda932009-09-21 16:00:26 -04002056 mutex_lock(&inode->i_mutex);
2057 err = d_invalidate(dentry);
2058 if (err)
2059 goto out_unlock;
2060
2061 down_write(&root->fs_info->subvol_sem);
2062
2063 err = may_destroy_subvol(dest);
2064 if (err)
2065 goto out_up_write;
2066
Yan, Zhenga22285a2010-05-16 10:48:46 -04002067 trans = btrfs_start_transaction(root, 0);
2068 if (IS_ERR(trans)) {
2069 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002070 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002071 }
2072 trans->block_rsv = &root->fs_info->global_block_rsv;
2073
Yan, Zheng76dda932009-09-21 16:00:26 -04002074 ret = btrfs_unlink_subvol(trans, root, dir,
2075 dest->root_key.objectid,
2076 dentry->d_name.name,
2077 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002078 if (ret) {
2079 err = ret;
2080 btrfs_abort_transaction(trans, root, ret);
2081 goto out_end_trans;
2082 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002083
2084 btrfs_record_root_in_trans(trans, dest);
2085
2086 memset(&dest->root_item.drop_progress, 0,
2087 sizeof(dest->root_item.drop_progress));
2088 dest->root_item.drop_level = 0;
2089 btrfs_set_root_refs(&dest->root_item, 0);
2090
Yan, Zhengd68fc572010-05-16 10:49:58 -04002091 if (!xchg(&dest->orphan_item_inserted, 1)) {
2092 ret = btrfs_insert_orphan_item(trans,
2093 root->fs_info->tree_root,
2094 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002095 if (ret) {
2096 btrfs_abort_transaction(trans, root, ret);
2097 err = ret;
2098 goto out_end_trans;
2099 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002100 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002101out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002102 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002103 if (ret && !err)
2104 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002105 inode->i_flags |= S_DEAD;
2106out_up_write:
2107 up_write(&root->fs_info->subvol_sem);
2108out_unlock:
2109 mutex_unlock(&inode->i_mutex);
2110 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002111 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002112 btrfs_invalidate_inodes(dest);
2113 d_delete(dentry);
2114 }
2115out_dput:
2116 dput(dentry);
2117out_unlock_dir:
2118 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002119 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002120out:
2121 kfree(vol_args);
2122 return err;
2123}
2124
Chris Mason1e701a32010-03-11 09:42:04 -05002125static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002126{
2127 struct inode *inode = fdentry(file)->d_inode;
2128 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002129 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002130 int ret;
2131
Li Zefanb83cc962010-12-20 16:04:08 +08002132 if (btrfs_root_readonly(root))
2133 return -EROFS;
2134
Al Viroa561be72011-11-23 11:57:51 -05002135 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002136 if (ret)
2137 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002138
2139 switch (inode->i_mode & S_IFMT) {
2140 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002141 if (!capable(CAP_SYS_ADMIN)) {
2142 ret = -EPERM;
2143 goto out;
2144 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002145 ret = btrfs_defrag_root(root, 0);
2146 if (ret)
2147 goto out;
2148 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002149 break;
2150 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002151 if (!(file->f_mode & FMODE_WRITE)) {
2152 ret = -EINVAL;
2153 goto out;
2154 }
Chris Mason1e701a32010-03-11 09:42:04 -05002155
2156 range = kzalloc(sizeof(*range), GFP_KERNEL);
2157 if (!range) {
2158 ret = -ENOMEM;
2159 goto out;
2160 }
2161
2162 if (argp) {
2163 if (copy_from_user(range, argp,
2164 sizeof(*range))) {
2165 ret = -EFAULT;
2166 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002167 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002168 }
2169 /* compression requires us to start the IO */
2170 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2171 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2172 range->extent_thresh = (u32)-1;
2173 }
2174 } else {
2175 /* the rest are all set to zero by kzalloc */
2176 range->len = (u64)-1;
2177 }
Chris Mason4cb53002011-05-24 15:35:30 -04002178 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2179 range, 0, 0);
2180 if (ret > 0)
2181 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002182 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002183 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002184 default:
2185 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002186 }
Chris Masone441d542009-01-05 16:57:23 -05002187out:
Al Viro2a79f172011-12-09 08:06:57 -05002188 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002189 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002190}
2191
Christoph Hellwigb2950862008-12-02 09:54:17 -05002192static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002193{
2194 struct btrfs_ioctl_vol_args *vol_args;
2195 int ret;
2196
Chris Masone441d542009-01-05 16:57:23 -05002197 if (!capable(CAP_SYS_ADMIN))
2198 return -EPERM;
2199
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002200 mutex_lock(&root->fs_info->volume_mutex);
2201 if (root->fs_info->balance_ctl) {
2202 printk(KERN_INFO "btrfs: balance in progress\n");
2203 ret = -EINVAL;
2204 goto out;
2205 }
2206
Li Zefandae7b662009-04-08 15:06:54 +08002207 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002208 if (IS_ERR(vol_args)) {
2209 ret = PTR_ERR(vol_args);
2210 goto out;
2211 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002212
Mark Fasheh5516e592008-07-24 12:20:14 -04002213 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002214 ret = btrfs_init_new_device(root, vol_args->name);
2215
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002216 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002217out:
2218 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002219 return ret;
2220}
2221
Christoph Hellwigb2950862008-12-02 09:54:17 -05002222static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002223{
2224 struct btrfs_ioctl_vol_args *vol_args;
2225 int ret;
2226
Chris Masone441d542009-01-05 16:57:23 -05002227 if (!capable(CAP_SYS_ADMIN))
2228 return -EPERM;
2229
Yan Zhengc146afa2008-11-12 14:34:12 -05002230 if (root->fs_info->sb->s_flags & MS_RDONLY)
2231 return -EROFS;
2232
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002233 mutex_lock(&root->fs_info->volume_mutex);
2234 if (root->fs_info->balance_ctl) {
2235 printk(KERN_INFO "btrfs: balance in progress\n");
2236 ret = -EINVAL;
2237 goto out;
2238 }
2239
Li Zefandae7b662009-04-08 15:06:54 +08002240 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002241 if (IS_ERR(vol_args)) {
2242 ret = PTR_ERR(vol_args);
2243 goto out;
2244 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002245
Mark Fasheh5516e592008-07-24 12:20:14 -04002246 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002247 ret = btrfs_rm_device(root, vol_args->name);
2248
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002249 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002250out:
2251 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002252 return ret;
2253}
2254
Jan Schmidt475f6382011-03-11 15:41:01 +01002255static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2256{
Li Zefan027ed2f2011-06-08 08:27:56 +00002257 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002258 struct btrfs_device *device;
2259 struct btrfs_device *next;
2260 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002261 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002262
2263 if (!capable(CAP_SYS_ADMIN))
2264 return -EPERM;
2265
Li Zefan027ed2f2011-06-08 08:27:56 +00002266 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2267 if (!fi_args)
2268 return -ENOMEM;
2269
2270 fi_args->num_devices = fs_devices->num_devices;
2271 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002272
2273 mutex_lock(&fs_devices->device_list_mutex);
2274 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002275 if (device->devid > fi_args->max_id)
2276 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002277 }
2278 mutex_unlock(&fs_devices->device_list_mutex);
2279
Li Zefan027ed2f2011-06-08 08:27:56 +00002280 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2281 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002282
Li Zefan027ed2f2011-06-08 08:27:56 +00002283 kfree(fi_args);
2284 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002285}
2286
2287static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2288{
2289 struct btrfs_ioctl_dev_info_args *di_args;
2290 struct btrfs_device *dev;
2291 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2292 int ret = 0;
2293 char *s_uuid = NULL;
2294 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2295
2296 if (!capable(CAP_SYS_ADMIN))
2297 return -EPERM;
2298
2299 di_args = memdup_user(arg, sizeof(*di_args));
2300 if (IS_ERR(di_args))
2301 return PTR_ERR(di_args);
2302
2303 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2304 s_uuid = di_args->uuid;
2305
2306 mutex_lock(&fs_devices->device_list_mutex);
2307 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2308 mutex_unlock(&fs_devices->device_list_mutex);
2309
2310 if (!dev) {
2311 ret = -ENODEV;
2312 goto out;
2313 }
2314
2315 di_args->devid = dev->devid;
2316 di_args->bytes_used = dev->bytes_used;
2317 di_args->total_bytes = dev->total_bytes;
2318 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002319 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002320 struct rcu_string *name;
2321
2322 rcu_read_lock();
2323 name = rcu_dereference(dev->name);
2324 strncpy(di_args->path, name->str, sizeof(di_args->path));
2325 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002326 di_args->path[sizeof(di_args->path) - 1] = 0;
2327 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002328 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002329 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002330
2331out:
2332 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2333 ret = -EFAULT;
2334
2335 kfree(di_args);
2336 return ret;
2337}
2338
Yan, Zheng76dda932009-09-21 16:00:26 -04002339static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2340 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002341{
2342 struct inode *inode = fdentry(file)->d_inode;
2343 struct btrfs_root *root = BTRFS_I(inode)->root;
2344 struct file *src_file;
2345 struct inode *src;
2346 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002347 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002348 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002349 char *buf;
2350 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002351 u32 nritems;
2352 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002353 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002354 u64 len = olen;
2355 u64 bs = root->fs_info->sb->s_blocksize;
Chris Masond20f7042008-12-08 16:58:54 -05002356
Sage Weilc5c9cd42008-11-12 14:32:25 -05002357 /*
2358 * TODO:
2359 * - split compressed inline extents. annoying: we need to
2360 * decompress into destination's address_space (the file offset
2361 * may change, so source mapping won't do), then recompress (or
2362 * otherwise reinsert) a subrange.
2363 * - allow ranges within the same file to be cloned (provided
2364 * they don't overlap)?
2365 */
2366
Chris Masone441d542009-01-05 16:57:23 -05002367 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002368 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002369 return -EINVAL;
2370
Li Zefanb83cc962010-12-20 16:04:08 +08002371 if (btrfs_root_readonly(root))
2372 return -EROFS;
2373
Al Viroa561be72011-11-23 11:57:51 -05002374 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002375 if (ret)
2376 return ret;
2377
Sage Weilc5c9cd42008-11-12 14:32:25 -05002378 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002379 if (!src_file) {
2380 ret = -EBADF;
2381 goto out_drop_write;
2382 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002383
David Sterba362a20c2011-08-01 18:11:57 +02002384 ret = -EXDEV;
2385 if (src_file->f_path.mnt != file->f_path.mnt)
2386 goto out_fput;
2387
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002388 src = src_file->f_dentry->d_inode;
2389
Sage Weilc5c9cd42008-11-12 14:32:25 -05002390 ret = -EINVAL;
2391 if (src == inode)
2392 goto out_fput;
2393
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002394 /* the src must be open for reading */
2395 if (!(src_file->f_mode & FMODE_READ))
2396 goto out_fput;
2397
Li Zefan0e7b8242011-09-18 10:20:46 -04002398 /* don't make the dst file partly checksummed */
2399 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2400 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2401 goto out_fput;
2402
Yan Zhengae01a0a2008-08-04 23:23:47 -04002403 ret = -EISDIR;
2404 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002405 goto out_fput;
2406
Yan Zhengae01a0a2008-08-04 23:23:47 -04002407 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002408 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002409 goto out_fput;
2410
2411 ret = -ENOMEM;
2412 buf = vmalloc(btrfs_level_size(root, 0));
2413 if (!buf)
2414 goto out_fput;
2415
2416 path = btrfs_alloc_path();
2417 if (!path) {
2418 vfree(buf);
2419 goto out_fput;
2420 }
2421 path->reada = 2;
2422
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002423 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002424 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2425 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002426 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002427 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2428 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002429 }
2430
Sage Weilc5c9cd42008-11-12 14:32:25 -05002431 /* determine range to clone */
2432 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002433 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002434 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002435 if (len == 0)
2436 olen = len = src->i_size - off;
2437 /* if we extend to eof, continue to block boundary */
2438 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002439 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002440
2441 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002442 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2443 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002444 goto out_unlock;
2445
Li Zefand525e8a2011-09-11 10:52:25 -04002446 if (destoff > inode->i_size) {
2447 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2448 if (ret)
2449 goto out_unlock;
2450 }
2451
Li Zefan71ef0782011-09-18 10:20:46 -04002452 /* truncate page cache pages from target inode range */
2453 truncate_inode_pages_range(&inode->i_data, destoff,
2454 PAGE_CACHE_ALIGN(destoff + len) - 1);
2455
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002456 /* do any pending delalloc/csum calc on src, one way or
2457 another, and lock file content */
2458 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002459 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002460 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002461 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2462 if (!ordered &&
2463 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2464 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002465 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002466 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002467 if (ordered)
2468 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002469 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002470 }
2471
Sage Weilc5c9cd42008-11-12 14:32:25 -05002472 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002473 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002474 key.type = BTRFS_EXTENT_DATA_KEY;
2475 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002476
2477 while (1) {
2478 /*
2479 * note the key will change type as we walk through the
2480 * tree.
2481 */
David Sterba362a20c2011-08-01 18:11:57 +02002482 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2483 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002484 if (ret < 0)
2485 goto out;
2486
Yan Zhengae01a0a2008-08-04 23:23:47 -04002487 nritems = btrfs_header_nritems(path->nodes[0]);
2488 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002489 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002490 if (ret < 0)
2491 goto out;
2492 if (ret > 0)
2493 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002494 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002495 }
2496 leaf = path->nodes[0];
2497 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002498
Yan Zhengae01a0a2008-08-04 23:23:47 -04002499 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002500 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002501 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002502 break;
2503
Sage Weilc5c9cd42008-11-12 14:32:25 -05002504 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2505 struct btrfs_file_extent_item *extent;
2506 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002507 u32 size;
2508 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002509 u64 disko = 0, diskl = 0;
2510 u64 datao = 0, datal = 0;
2511 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002512 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002513
2514 size = btrfs_item_size_nr(leaf, slot);
2515 read_extent_buffer(leaf, buf,
2516 btrfs_item_ptr_offset(leaf, slot),
2517 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002518
2519 extent = btrfs_item_ptr(leaf, slot,
2520 struct btrfs_file_extent_item);
2521 comp = btrfs_file_extent_compression(leaf, extent);
2522 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002523 if (type == BTRFS_FILE_EXTENT_REG ||
2524 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002525 disko = btrfs_file_extent_disk_bytenr(leaf,
2526 extent);
2527 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2528 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002529 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002530 datal = btrfs_file_extent_num_bytes(leaf,
2531 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002532 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2533 /* take upper bound, may be compressed */
2534 datal = btrfs_file_extent_ram_bytes(leaf,
2535 extent);
2536 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002537 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002538
Sage Weil050006a2010-10-29 15:37:33 -04002539 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002540 key.offset >= off+len)
2541 goto next;
2542
Zheng Yan31840ae2008-09-23 13:14:14 -04002543 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002544 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002545 if (off <= key.offset)
2546 new_key.offset = key.offset + destoff - off;
2547 else
2548 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002549
Sage Weilb6f34092011-09-20 14:48:51 -04002550 /*
2551 * 1 - adjusting old extent (we may have to split it)
2552 * 1 - add new extent
2553 * 1 - inode update
2554 */
2555 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002556 if (IS_ERR(trans)) {
2557 ret = PTR_ERR(trans);
2558 goto out;
2559 }
2560
Chris Masonc8a894d2009-06-27 21:07:03 -04002561 if (type == BTRFS_FILE_EXTENT_REG ||
2562 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002563 /*
2564 * a | --- range to clone ---| b
2565 * | ------------- extent ------------- |
2566 */
2567
2568 /* substract range b */
2569 if (key.offset + datal > off + len)
2570 datal = off + len - key.offset;
2571
2572 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002573 if (off > key.offset) {
2574 datao += off - key.offset;
2575 datal -= off - key.offset;
2576 }
2577
Josef Bacik5dc562c2012-08-17 13:14:17 -04002578 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002579 new_key.offset,
2580 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002581 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002582 if (ret) {
2583 btrfs_abort_transaction(trans, root,
2584 ret);
2585 btrfs_end_transaction(trans, root);
2586 goto out;
2587 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002588
Sage Weilc5c9cd42008-11-12 14:32:25 -05002589 ret = btrfs_insert_empty_item(trans, root, path,
2590 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002591 if (ret) {
2592 btrfs_abort_transaction(trans, root,
2593 ret);
2594 btrfs_end_transaction(trans, root);
2595 goto out;
2596 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002597
2598 leaf = path->nodes[0];
2599 slot = path->slots[0];
2600 write_extent_buffer(leaf, buf,
2601 btrfs_item_ptr_offset(leaf, slot),
2602 size);
2603
2604 extent = btrfs_item_ptr(leaf, slot,
2605 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002606
Sage Weilc5c9cd42008-11-12 14:32:25 -05002607 /* disko == 0 means it's a hole */
2608 if (!disko)
2609 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002610
2611 btrfs_set_file_extent_offset(leaf, extent,
2612 datao);
2613 btrfs_set_file_extent_num_bytes(leaf, extent,
2614 datal);
2615 if (disko) {
2616 inode_add_bytes(inode, datal);
2617 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002618 disko, diskl, 0,
2619 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002620 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002621 new_key.offset - datao,
2622 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002623 if (ret) {
2624 btrfs_abort_transaction(trans,
2625 root,
2626 ret);
2627 btrfs_end_transaction(trans,
2628 root);
2629 goto out;
2630
2631 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002632 }
2633 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2634 u64 skip = 0;
2635 u64 trim = 0;
2636 if (off > key.offset) {
2637 skip = off - key.offset;
2638 new_key.offset += skip;
2639 }
Chris Masond3977122009-01-05 21:25:51 -05002640
Sage Weilc5c9cd42008-11-12 14:32:25 -05002641 if (key.offset + datal > off+len)
2642 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002643
Sage Weilc5c9cd42008-11-12 14:32:25 -05002644 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002645 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002646 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002647 goto out;
2648 }
2649 size -= skip + trim;
2650 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002651
Josef Bacik5dc562c2012-08-17 13:14:17 -04002652 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002653 new_key.offset,
2654 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002655 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002656 if (ret) {
2657 btrfs_abort_transaction(trans, root,
2658 ret);
2659 btrfs_end_transaction(trans, root);
2660 goto out;
2661 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002662
Sage Weilc5c9cd42008-11-12 14:32:25 -05002663 ret = btrfs_insert_empty_item(trans, root, path,
2664 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002665 if (ret) {
2666 btrfs_abort_transaction(trans, root,
2667 ret);
2668 btrfs_end_transaction(trans, root);
2669 goto out;
2670 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002671
2672 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002673 u32 start =
2674 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002675 memmove(buf+start, buf+start+skip,
2676 datal);
2677 }
2678
2679 leaf = path->nodes[0];
2680 slot = path->slots[0];
2681 write_extent_buffer(leaf, buf,
2682 btrfs_item_ptr_offset(leaf, slot),
2683 size);
2684 inode_add_bytes(inode, datal);
2685 }
2686
2687 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002688 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002689
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002690 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002691 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002692
2693 /*
2694 * we round up to the block size at eof when
2695 * determining which extents to clone above,
2696 * but shouldn't round up the file size
2697 */
2698 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002699 if (endoff > destoff+olen)
2700 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002701 if (endoff > inode->i_size)
2702 btrfs_i_size_write(inode, endoff);
2703
Yan, Zhenga22285a2010-05-16 10:48:46 -04002704 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002705 if (ret) {
2706 btrfs_abort_transaction(trans, root, ret);
2707 btrfs_end_transaction(trans, root);
2708 goto out;
2709 }
2710 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002711 }
Chris Masond3977122009-01-05 21:25:51 -05002712next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002713 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002714 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002715 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002716 ret = 0;
2717out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002718 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002719 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002720out_unlock:
2721 mutex_unlock(&src->i_mutex);
2722 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002723 vfree(buf);
2724 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002725out_fput:
2726 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002727out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002728 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002729 return ret;
2730}
2731
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002732static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002733{
2734 struct btrfs_ioctl_clone_range_args args;
2735
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002736 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002737 return -EFAULT;
2738 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2739 args.src_length, args.dest_offset);
2740}
2741
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002742/*
2743 * there are many ways the trans_start and trans_end ioctls can lead
2744 * to deadlocks. They should only be used by applications that
2745 * basically own the machine, and have a very in depth understanding
2746 * of all the possible deadlocks and enospc problems.
2747 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002748static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002749{
2750 struct inode *inode = fdentry(file)->d_inode;
2751 struct btrfs_root *root = BTRFS_I(inode)->root;
2752 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002753 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002754
Sage Weil1ab86ae2009-09-29 18:38:44 -04002755 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002756 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002757 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002758
2759 ret = -EINPROGRESS;
2760 if (file->private_data)
2761 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002762
Li Zefanb83cc962010-12-20 16:04:08 +08002763 ret = -EROFS;
2764 if (btrfs_root_readonly(root))
2765 goto out;
2766
Al Viroa561be72011-11-23 11:57:51 -05002767 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002768 if (ret)
2769 goto out;
2770
Josef Bacika4abeea2011-04-11 17:25:13 -04002771 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002772
Sage Weil1ab86ae2009-09-29 18:38:44 -04002773 ret = -ENOMEM;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002774 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002775 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002776 goto out_drop;
2777
2778 file->private_data = trans;
2779 return 0;
2780
2781out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002782 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002783 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002784out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002785 return ret;
2786}
2787
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002788static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2789{
2790 struct inode *inode = fdentry(file)->d_inode;
2791 struct btrfs_root *root = BTRFS_I(inode)->root;
2792 struct btrfs_root *new_root;
2793 struct btrfs_dir_item *di;
2794 struct btrfs_trans_handle *trans;
2795 struct btrfs_path *path;
2796 struct btrfs_key location;
2797 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002798 u64 objectid = 0;
2799 u64 dir_id;
2800
2801 if (!capable(CAP_SYS_ADMIN))
2802 return -EPERM;
2803
2804 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2805 return -EFAULT;
2806
2807 if (!objectid)
2808 objectid = root->root_key.objectid;
2809
2810 location.objectid = objectid;
2811 location.type = BTRFS_ROOT_ITEM_KEY;
2812 location.offset = (u64)-1;
2813
2814 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2815 if (IS_ERR(new_root))
2816 return PTR_ERR(new_root);
2817
2818 if (btrfs_root_refs(&new_root->root_item) == 0)
2819 return -ENOENT;
2820
2821 path = btrfs_alloc_path();
2822 if (!path)
2823 return -ENOMEM;
2824 path->leave_spinning = 1;
2825
2826 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002827 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002828 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002829 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002830 }
2831
David Sterba6c417612011-04-13 15:41:04 +02002832 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002833 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2834 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002835 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002836 btrfs_free_path(path);
2837 btrfs_end_transaction(trans, root);
2838 printk(KERN_ERR "Umm, you don't have the default dir item, "
2839 "this isn't going to work\n");
2840 return -ENOENT;
2841 }
2842
2843 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2844 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2845 btrfs_mark_buffer_dirty(path->nodes[0]);
2846 btrfs_free_path(path);
2847
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06002848 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002849 btrfs_end_transaction(trans, root);
2850
2851 return 0;
2852}
2853
Josef Bacikbf5fc092010-09-29 11:22:36 -04002854static void get_block_group_info(struct list_head *groups_list,
2855 struct btrfs_ioctl_space_info *space)
2856{
2857 struct btrfs_block_group_cache *block_group;
2858
2859 space->total_bytes = 0;
2860 space->used_bytes = 0;
2861 space->flags = 0;
2862 list_for_each_entry(block_group, groups_list, list) {
2863 space->flags = block_group->flags;
2864 space->total_bytes += block_group->key.offset;
2865 space->used_bytes +=
2866 btrfs_block_group_used(&block_group->item);
2867 }
2868}
2869
Josef Bacik1406e432010-01-13 18:19:06 +00002870long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2871{
2872 struct btrfs_ioctl_space_args space_args;
2873 struct btrfs_ioctl_space_info space;
2874 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002875 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002876 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002877 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002878 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2879 BTRFS_BLOCK_GROUP_SYSTEM,
2880 BTRFS_BLOCK_GROUP_METADATA,
2881 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2882 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002883 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002884 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002885 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002886 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002887
2888 if (copy_from_user(&space_args,
2889 (struct btrfs_ioctl_space_args __user *)arg,
2890 sizeof(space_args)))
2891 return -EFAULT;
2892
Josef Bacikbf5fc092010-09-29 11:22:36 -04002893 for (i = 0; i < num_types; i++) {
2894 struct btrfs_space_info *tmp;
2895
2896 info = NULL;
2897 rcu_read_lock();
2898 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2899 list) {
2900 if (tmp->flags == types[i]) {
2901 info = tmp;
2902 break;
2903 }
2904 }
2905 rcu_read_unlock();
2906
2907 if (!info)
2908 continue;
2909
2910 down_read(&info->groups_sem);
2911 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2912 if (!list_empty(&info->block_groups[c]))
2913 slot_count++;
2914 }
2915 up_read(&info->groups_sem);
2916 }
Josef Bacik1406e432010-01-13 18:19:06 +00002917
Chris Mason7fde62b2010-03-16 15:40:10 -04002918 /* space_slots == 0 means they are asking for a count */
2919 if (space_args.space_slots == 0) {
2920 space_args.total_spaces = slot_count;
2921 goto out;
2922 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002923
Dan Rosenberg51788b12011-02-14 16:04:23 -05002924 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002925
Chris Mason7fde62b2010-03-16 15:40:10 -04002926 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002927
Chris Mason7fde62b2010-03-16 15:40:10 -04002928 /* we generally have at most 6 or so space infos, one for each raid
2929 * level. So, a whole page should be more than enough for everyone
2930 */
2931 if (alloc_size > PAGE_CACHE_SIZE)
2932 return -ENOMEM;
2933
2934 space_args.total_spaces = 0;
2935 dest = kmalloc(alloc_size, GFP_NOFS);
2936 if (!dest)
2937 return -ENOMEM;
2938 dest_orig = dest;
2939
2940 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002941 for (i = 0; i < num_types; i++) {
2942 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002943
Dan Rosenberg51788b12011-02-14 16:04:23 -05002944 if (!slot_count)
2945 break;
2946
Josef Bacikbf5fc092010-09-29 11:22:36 -04002947 info = NULL;
2948 rcu_read_lock();
2949 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2950 list) {
2951 if (tmp->flags == types[i]) {
2952 info = tmp;
2953 break;
2954 }
2955 }
2956 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002957
Josef Bacikbf5fc092010-09-29 11:22:36 -04002958 if (!info)
2959 continue;
2960 down_read(&info->groups_sem);
2961 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2962 if (!list_empty(&info->block_groups[c])) {
2963 get_block_group_info(&info->block_groups[c],
2964 &space);
2965 memcpy(dest, &space, sizeof(space));
2966 dest++;
2967 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002968 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002969 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002970 if (!slot_count)
2971 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002972 }
2973 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002974 }
Josef Bacik1406e432010-01-13 18:19:06 +00002975
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08002976 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04002977 (arg + sizeof(struct btrfs_ioctl_space_args));
2978
2979 if (copy_to_user(user_dest, dest_orig, alloc_size))
2980 ret = -EFAULT;
2981
2982 kfree(dest_orig);
2983out:
2984 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002985 ret = -EFAULT;
2986
2987 return ret;
2988}
2989
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002990/*
2991 * there are many ways the trans_start and trans_end ioctls can lead
2992 * to deadlocks. They should only be used by applications that
2993 * basically own the machine, and have a very in depth understanding
2994 * of all the possible deadlocks and enospc problems.
2995 */
2996long btrfs_ioctl_trans_end(struct file *file)
2997{
2998 struct inode *inode = fdentry(file)->d_inode;
2999 struct btrfs_root *root = BTRFS_I(inode)->root;
3000 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003001
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003002 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003003 if (!trans)
3004 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003005 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003006
Sage Weil1ab86ae2009-09-29 18:38:44 -04003007 btrfs_end_transaction(trans, root);
3008
Josef Bacika4abeea2011-04-11 17:25:13 -04003009 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003010
Al Viro2a79f172011-12-09 08:06:57 -05003011 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003012 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003013}
3014
Sage Weil46204592010-10-29 15:41:32 -04003015static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
3016{
3017 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3018 struct btrfs_trans_handle *trans;
3019 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003020 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003021
3022 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003023 if (IS_ERR(trans))
3024 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04003025 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003026 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003027 if (ret) {
3028 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003029 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003030 }
Sage Weil46204592010-10-29 15:41:32 -04003031
3032 if (argp)
3033 if (copy_to_user(argp, &transid, sizeof(transid)))
3034 return -EFAULT;
3035 return 0;
3036}
3037
3038static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
3039{
3040 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3041 u64 transid;
3042
3043 if (argp) {
3044 if (copy_from_user(&transid, argp, sizeof(transid)))
3045 return -EFAULT;
3046 } else {
3047 transid = 0; /* current trans */
3048 }
3049 return btrfs_wait_for_commit(root, transid);
3050}
3051
Jan Schmidt475f6382011-03-11 15:41:01 +01003052static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3053{
3054 int ret;
3055 struct btrfs_ioctl_scrub_args *sa;
3056
3057 if (!capable(CAP_SYS_ADMIN))
3058 return -EPERM;
3059
3060 sa = memdup_user(arg, sizeof(*sa));
3061 if (IS_ERR(sa))
3062 return PTR_ERR(sa);
3063
3064 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01003065 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01003066
3067 if (copy_to_user(arg, sa, sizeof(*sa)))
3068 ret = -EFAULT;
3069
3070 kfree(sa);
3071 return ret;
3072}
3073
3074static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3075{
3076 if (!capable(CAP_SYS_ADMIN))
3077 return -EPERM;
3078
3079 return btrfs_scrub_cancel(root);
3080}
3081
3082static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3083 void __user *arg)
3084{
3085 struct btrfs_ioctl_scrub_args *sa;
3086 int ret;
3087
3088 if (!capable(CAP_SYS_ADMIN))
3089 return -EPERM;
3090
3091 sa = memdup_user(arg, sizeof(*sa));
3092 if (IS_ERR(sa))
3093 return PTR_ERR(sa);
3094
3095 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3096
3097 if (copy_to_user(arg, sa, sizeof(*sa)))
3098 ret = -EFAULT;
3099
3100 kfree(sa);
3101 return ret;
3102}
3103
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003104static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003105 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003106{
3107 struct btrfs_ioctl_get_dev_stats *sa;
3108 int ret;
3109
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003110 sa = memdup_user(arg, sizeof(*sa));
3111 if (IS_ERR(sa))
3112 return PTR_ERR(sa);
3113
David Sterbab27f7c02012-06-22 06:30:39 -06003114 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3115 kfree(sa);
3116 return -EPERM;
3117 }
3118
3119 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003120
3121 if (copy_to_user(arg, sa, sizeof(*sa)))
3122 ret = -EFAULT;
3123
3124 kfree(sa);
3125 return ret;
3126}
3127
Jan Schmidtd7728c92011-07-07 16:48:38 +02003128static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3129{
3130 int ret = 0;
3131 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003132 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003133 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003134 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003135 struct inode_fs_paths *ipath = NULL;
3136 struct btrfs_path *path;
3137
3138 if (!capable(CAP_SYS_ADMIN))
3139 return -EPERM;
3140
3141 path = btrfs_alloc_path();
3142 if (!path) {
3143 ret = -ENOMEM;
3144 goto out;
3145 }
3146
3147 ipa = memdup_user(arg, sizeof(*ipa));
3148 if (IS_ERR(ipa)) {
3149 ret = PTR_ERR(ipa);
3150 ipa = NULL;
3151 goto out;
3152 }
3153
3154 size = min_t(u32, ipa->size, 4096);
3155 ipath = init_ipath(size, root, path);
3156 if (IS_ERR(ipath)) {
3157 ret = PTR_ERR(ipath);
3158 ipath = NULL;
3159 goto out;
3160 }
3161
3162 ret = paths_from_inode(ipa->inum, ipath);
3163 if (ret < 0)
3164 goto out;
3165
3166 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003167 rel_ptr = ipath->fspath->val[i] -
3168 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003169 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003170 }
3171
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003172 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3173 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003174 if (ret) {
3175 ret = -EFAULT;
3176 goto out;
3177 }
3178
3179out:
3180 btrfs_free_path(path);
3181 free_ipath(ipath);
3182 kfree(ipa);
3183
3184 return ret;
3185}
3186
3187static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3188{
3189 struct btrfs_data_container *inodes = ctx;
3190 const size_t c = 3 * sizeof(u64);
3191
3192 if (inodes->bytes_left >= c) {
3193 inodes->bytes_left -= c;
3194 inodes->val[inodes->elem_cnt] = inum;
3195 inodes->val[inodes->elem_cnt + 1] = offset;
3196 inodes->val[inodes->elem_cnt + 2] = root;
3197 inodes->elem_cnt += 3;
3198 } else {
3199 inodes->bytes_missing += c - inodes->bytes_left;
3200 inodes->bytes_left = 0;
3201 inodes->elem_missed += 3;
3202 }
3203
3204 return 0;
3205}
3206
3207static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3208 void __user *arg)
3209{
3210 int ret = 0;
3211 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003212 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003213 struct btrfs_ioctl_logical_ino_args *loi;
3214 struct btrfs_data_container *inodes = NULL;
3215 struct btrfs_path *path = NULL;
3216 struct btrfs_key key;
3217
3218 if (!capable(CAP_SYS_ADMIN))
3219 return -EPERM;
3220
3221 loi = memdup_user(arg, sizeof(*loi));
3222 if (IS_ERR(loi)) {
3223 ret = PTR_ERR(loi);
3224 loi = NULL;
3225 goto out;
3226 }
3227
3228 path = btrfs_alloc_path();
3229 if (!path) {
3230 ret = -ENOMEM;
3231 goto out;
3232 }
3233
3234 size = min_t(u32, loi->size, 4096);
3235 inodes = init_data_container(size);
3236 if (IS_ERR(inodes)) {
3237 ret = PTR_ERR(inodes);
3238 inodes = NULL;
3239 goto out;
3240 }
3241
3242 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003243 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003244
3245 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3246 ret = -ENOENT;
3247 if (ret < 0)
3248 goto out;
3249
Jan Schmidt4692cf52011-12-02 14:56:41 +01003250 extent_item_pos = loi->logical - key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01003251 ret = iterate_extent_inodes(root->fs_info, key.objectid,
3252 extent_item_pos, 0, build_ino_list,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003253 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003254
3255 if (ret < 0)
3256 goto out;
3257
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003258 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3259 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003260 if (ret)
3261 ret = -EFAULT;
3262
3263out:
3264 btrfs_free_path(path);
3265 kfree(inodes);
3266 kfree(loi);
3267
3268 return ret;
3269}
3270
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003271void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003272 struct btrfs_ioctl_balance_args *bargs)
3273{
3274 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3275
3276 bargs->flags = bctl->flags;
3277
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003278 if (atomic_read(&fs_info->balance_running))
3279 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3280 if (atomic_read(&fs_info->balance_pause_req))
3281 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003282 if (atomic_read(&fs_info->balance_cancel_req))
3283 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003284
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003285 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3286 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3287 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003288
3289 if (lock) {
3290 spin_lock(&fs_info->balance_lock);
3291 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3292 spin_unlock(&fs_info->balance_lock);
3293 } else {
3294 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3295 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003296}
3297
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003298static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003299{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003300 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003301 struct btrfs_fs_info *fs_info = root->fs_info;
3302 struct btrfs_ioctl_balance_args *bargs;
3303 struct btrfs_balance_control *bctl;
3304 int ret;
3305
3306 if (!capable(CAP_SYS_ADMIN))
3307 return -EPERM;
3308
Liu Boe54bfa32012-06-29 03:58:48 -06003309 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003310 if (ret)
3311 return ret;
3312
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003313 mutex_lock(&fs_info->volume_mutex);
3314 mutex_lock(&fs_info->balance_mutex);
3315
3316 if (arg) {
3317 bargs = memdup_user(arg, sizeof(*bargs));
3318 if (IS_ERR(bargs)) {
3319 ret = PTR_ERR(bargs);
3320 goto out;
3321 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003322
3323 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3324 if (!fs_info->balance_ctl) {
3325 ret = -ENOTCONN;
3326 goto out_bargs;
3327 }
3328
3329 bctl = fs_info->balance_ctl;
3330 spin_lock(&fs_info->balance_lock);
3331 bctl->flags |= BTRFS_BALANCE_RESUME;
3332 spin_unlock(&fs_info->balance_lock);
3333
3334 goto do_balance;
3335 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003336 } else {
3337 bargs = NULL;
3338 }
3339
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003340 if (fs_info->balance_ctl) {
3341 ret = -EINPROGRESS;
3342 goto out_bargs;
3343 }
3344
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003345 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3346 if (!bctl) {
3347 ret = -ENOMEM;
3348 goto out_bargs;
3349 }
3350
3351 bctl->fs_info = fs_info;
3352 if (arg) {
3353 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3354 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3355 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3356
3357 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003358 } else {
3359 /* balance everything - no filters */
3360 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003361 }
3362
Ilya Dryomovde322262012-01-16 22:04:49 +02003363do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003364 ret = btrfs_balance(bctl, bargs);
3365 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003366 * bctl is freed in __cancel_balance or in free_fs_info if
3367 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003368 */
3369 if (arg) {
3370 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3371 ret = -EFAULT;
3372 }
3373
3374out_bargs:
3375 kfree(bargs);
3376out:
3377 mutex_unlock(&fs_info->balance_mutex);
3378 mutex_unlock(&fs_info->volume_mutex);
Liu Boe54bfa32012-06-29 03:58:48 -06003379 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003380 return ret;
3381}
3382
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003383static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3384{
3385 if (!capable(CAP_SYS_ADMIN))
3386 return -EPERM;
3387
3388 switch (cmd) {
3389 case BTRFS_BALANCE_CTL_PAUSE:
3390 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003391 case BTRFS_BALANCE_CTL_CANCEL:
3392 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003393 }
3394
3395 return -EINVAL;
3396}
3397
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003398static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3399 void __user *arg)
3400{
3401 struct btrfs_fs_info *fs_info = root->fs_info;
3402 struct btrfs_ioctl_balance_args *bargs;
3403 int ret = 0;
3404
3405 if (!capable(CAP_SYS_ADMIN))
3406 return -EPERM;
3407
3408 mutex_lock(&fs_info->balance_mutex);
3409 if (!fs_info->balance_ctl) {
3410 ret = -ENOTCONN;
3411 goto out;
3412 }
3413
3414 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3415 if (!bargs) {
3416 ret = -ENOMEM;
3417 goto out;
3418 }
3419
3420 update_ioctl_balance_args(fs_info, 1, bargs);
3421
3422 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3423 ret = -EFAULT;
3424
3425 kfree(bargs);
3426out:
3427 mutex_unlock(&fs_info->balance_mutex);
3428 return ret;
3429}
3430
Arne Jansen5d13a372011-09-14 15:53:51 +02003431static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3432{
3433 struct btrfs_ioctl_quota_ctl_args *sa;
3434 struct btrfs_trans_handle *trans = NULL;
3435 int ret;
3436 int err;
3437
3438 if (!capable(CAP_SYS_ADMIN))
3439 return -EPERM;
3440
3441 if (root->fs_info->sb->s_flags & MS_RDONLY)
3442 return -EROFS;
3443
3444 sa = memdup_user(arg, sizeof(*sa));
3445 if (IS_ERR(sa))
3446 return PTR_ERR(sa);
3447
3448 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3449 trans = btrfs_start_transaction(root, 2);
3450 if (IS_ERR(trans)) {
3451 ret = PTR_ERR(trans);
3452 goto out;
3453 }
3454 }
3455
3456 switch (sa->cmd) {
3457 case BTRFS_QUOTA_CTL_ENABLE:
3458 ret = btrfs_quota_enable(trans, root->fs_info);
3459 break;
3460 case BTRFS_QUOTA_CTL_DISABLE:
3461 ret = btrfs_quota_disable(trans, root->fs_info);
3462 break;
3463 case BTRFS_QUOTA_CTL_RESCAN:
3464 ret = btrfs_quota_rescan(root->fs_info);
3465 break;
3466 default:
3467 ret = -EINVAL;
3468 break;
3469 }
3470
3471 if (copy_to_user(arg, sa, sizeof(*sa)))
3472 ret = -EFAULT;
3473
3474 if (trans) {
3475 err = btrfs_commit_transaction(trans, root);
3476 if (err && !ret)
3477 ret = err;
3478 }
3479
3480out:
3481 kfree(sa);
3482 return ret;
3483}
3484
3485static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3486{
3487 struct btrfs_ioctl_qgroup_assign_args *sa;
3488 struct btrfs_trans_handle *trans;
3489 int ret;
3490 int err;
3491
3492 if (!capable(CAP_SYS_ADMIN))
3493 return -EPERM;
3494
3495 if (root->fs_info->sb->s_flags & MS_RDONLY)
3496 return -EROFS;
3497
3498 sa = memdup_user(arg, sizeof(*sa));
3499 if (IS_ERR(sa))
3500 return PTR_ERR(sa);
3501
3502 trans = btrfs_join_transaction(root);
3503 if (IS_ERR(trans)) {
3504 ret = PTR_ERR(trans);
3505 goto out;
3506 }
3507
3508 /* FIXME: check if the IDs really exist */
3509 if (sa->assign) {
3510 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3511 sa->src, sa->dst);
3512 } else {
3513 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3514 sa->src, sa->dst);
3515 }
3516
3517 err = btrfs_end_transaction(trans, root);
3518 if (err && !ret)
3519 ret = err;
3520
3521out:
3522 kfree(sa);
3523 return ret;
3524}
3525
3526static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3527{
3528 struct btrfs_ioctl_qgroup_create_args *sa;
3529 struct btrfs_trans_handle *trans;
3530 int ret;
3531 int err;
3532
3533 if (!capable(CAP_SYS_ADMIN))
3534 return -EPERM;
3535
3536 if (root->fs_info->sb->s_flags & MS_RDONLY)
3537 return -EROFS;
3538
3539 sa = memdup_user(arg, sizeof(*sa));
3540 if (IS_ERR(sa))
3541 return PTR_ERR(sa);
3542
3543 trans = btrfs_join_transaction(root);
3544 if (IS_ERR(trans)) {
3545 ret = PTR_ERR(trans);
3546 goto out;
3547 }
3548
3549 /* FIXME: check if the IDs really exist */
3550 if (sa->create) {
3551 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3552 NULL);
3553 } else {
3554 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3555 }
3556
3557 err = btrfs_end_transaction(trans, root);
3558 if (err && !ret)
3559 ret = err;
3560
3561out:
3562 kfree(sa);
3563 return ret;
3564}
3565
3566static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3567{
3568 struct btrfs_ioctl_qgroup_limit_args *sa;
3569 struct btrfs_trans_handle *trans;
3570 int ret;
3571 int err;
3572 u64 qgroupid;
3573
3574 if (!capable(CAP_SYS_ADMIN))
3575 return -EPERM;
3576
3577 if (root->fs_info->sb->s_flags & MS_RDONLY)
3578 return -EROFS;
3579
3580 sa = memdup_user(arg, sizeof(*sa));
3581 if (IS_ERR(sa))
3582 return PTR_ERR(sa);
3583
3584 trans = btrfs_join_transaction(root);
3585 if (IS_ERR(trans)) {
3586 ret = PTR_ERR(trans);
3587 goto out;
3588 }
3589
3590 qgroupid = sa->qgroupid;
3591 if (!qgroupid) {
3592 /* take the current subvol as qgroup */
3593 qgroupid = root->root_key.objectid;
3594 }
3595
3596 /* FIXME: check if the IDs really exist */
3597 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3598
3599 err = btrfs_end_transaction(trans, root);
3600 if (err && !ret)
3601 ret = err;
3602
3603out:
3604 kfree(sa);
3605 return ret;
3606}
3607
Alexander Block8ea05e32012-07-25 17:35:53 +02003608static long btrfs_ioctl_set_received_subvol(struct file *file,
3609 void __user *arg)
3610{
3611 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3612 struct inode *inode = fdentry(file)->d_inode;
3613 struct btrfs_root *root = BTRFS_I(inode)->root;
3614 struct btrfs_root_item *root_item = &root->root_item;
3615 struct btrfs_trans_handle *trans;
3616 struct timespec ct = CURRENT_TIME;
3617 int ret = 0;
3618
3619 ret = mnt_want_write_file(file);
3620 if (ret < 0)
3621 return ret;
3622
3623 down_write(&root->fs_info->subvol_sem);
3624
3625 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3626 ret = -EINVAL;
3627 goto out;
3628 }
3629
3630 if (btrfs_root_readonly(root)) {
3631 ret = -EROFS;
3632 goto out;
3633 }
3634
3635 if (!inode_owner_or_capable(inode)) {
3636 ret = -EACCES;
3637 goto out;
3638 }
3639
3640 sa = memdup_user(arg, sizeof(*sa));
3641 if (IS_ERR(sa)) {
3642 ret = PTR_ERR(sa);
3643 sa = NULL;
3644 goto out;
3645 }
3646
3647 trans = btrfs_start_transaction(root, 1);
3648 if (IS_ERR(trans)) {
3649 ret = PTR_ERR(trans);
3650 trans = NULL;
3651 goto out;
3652 }
3653
3654 sa->rtransid = trans->transid;
3655 sa->rtime.sec = ct.tv_sec;
3656 sa->rtime.nsec = ct.tv_nsec;
3657
3658 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3659 btrfs_set_root_stransid(root_item, sa->stransid);
3660 btrfs_set_root_rtransid(root_item, sa->rtransid);
3661 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3662 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3663 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3664 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3665
3666 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3667 &root->root_key, &root->root_item);
3668 if (ret < 0) {
3669 btrfs_end_transaction(trans, root);
3670 trans = NULL;
3671 goto out;
3672 } else {
3673 ret = btrfs_commit_transaction(trans, root);
3674 if (ret < 0)
3675 goto out;
3676 }
3677
3678 ret = copy_to_user(arg, sa, sizeof(*sa));
3679 if (ret)
3680 ret = -EFAULT;
3681
3682out:
3683 kfree(sa);
3684 up_write(&root->fs_info->subvol_sem);
3685 mnt_drop_write_file(file);
3686 return ret;
3687}
3688
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003689long btrfs_ioctl(struct file *file, unsigned int
3690 cmd, unsigned long arg)
3691{
3692 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003693 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003694
3695 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003696 case FS_IOC_GETFLAGS:
3697 return btrfs_ioctl_getflags(file, argp);
3698 case FS_IOC_SETFLAGS:
3699 return btrfs_ioctl_setflags(file, argp);
3700 case FS_IOC_GETVERSION:
3701 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003702 case FITRIM:
3703 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003704 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003705 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003706 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003707 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003708 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003709 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02003710 case BTRFS_IOC_SUBVOL_CREATE_V2:
3711 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003712 case BTRFS_IOC_SNAP_DESTROY:
3713 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003714 case BTRFS_IOC_SUBVOL_GETFLAGS:
3715 return btrfs_ioctl_subvol_getflags(file, argp);
3716 case BTRFS_IOC_SUBVOL_SETFLAGS:
3717 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003718 case BTRFS_IOC_DEFAULT_SUBVOL:
3719 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003720 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003721 return btrfs_ioctl_defrag(file, NULL);
3722 case BTRFS_IOC_DEFRAG_RANGE:
3723 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003724 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003725 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003726 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003727 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003728 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003729 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003730 case BTRFS_IOC_FS_INFO:
3731 return btrfs_ioctl_fs_info(root, argp);
3732 case BTRFS_IOC_DEV_INFO:
3733 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003734 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003735 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003736 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003737 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3738 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003739 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003740 case BTRFS_IOC_TRANS_START:
3741 return btrfs_ioctl_trans_start(file);
3742 case BTRFS_IOC_TRANS_END:
3743 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003744 case BTRFS_IOC_TREE_SEARCH:
3745 return btrfs_ioctl_tree_search(file, argp);
3746 case BTRFS_IOC_INO_LOOKUP:
3747 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003748 case BTRFS_IOC_INO_PATHS:
3749 return btrfs_ioctl_ino_to_path(root, argp);
3750 case BTRFS_IOC_LOGICAL_INO:
3751 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003752 case BTRFS_IOC_SPACE_INFO:
3753 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003754 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003755 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3756 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003757 case BTRFS_IOC_START_SYNC:
3758 return btrfs_ioctl_start_sync(file, argp);
3759 case BTRFS_IOC_WAIT_SYNC:
3760 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003761 case BTRFS_IOC_SCRUB:
3762 return btrfs_ioctl_scrub(root, argp);
3763 case BTRFS_IOC_SCRUB_CANCEL:
3764 return btrfs_ioctl_scrub_cancel(root, argp);
3765 case BTRFS_IOC_SCRUB_PROGRESS:
3766 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003767 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003768 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003769 case BTRFS_IOC_BALANCE_CTL:
3770 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003771 case BTRFS_IOC_BALANCE_PROGRESS:
3772 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02003773 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3774 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02003775 case BTRFS_IOC_SEND:
3776 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003777 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06003778 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003779 case BTRFS_IOC_QUOTA_CTL:
3780 return btrfs_ioctl_quota_ctl(root, argp);
3781 case BTRFS_IOC_QGROUP_ASSIGN:
3782 return btrfs_ioctl_qgroup_assign(root, argp);
3783 case BTRFS_IOC_QGROUP_CREATE:
3784 return btrfs_ioctl_qgroup_create(root, argp);
3785 case BTRFS_IOC_QGROUP_LIMIT:
3786 return btrfs_ioctl_qgroup_limit(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003787 }
3788
3789 return -ENOTTY;
3790}