blob: 38e3d6ab89d37250b937d8c61b581f7b75f30952 [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 Zefan306424cc2011-12-14 20:12:02 -0500266 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400267 inode_inc_iversion(inode);
Li Zefan306424cc2011-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
Miao Xie66d8f3d2012-09-06 04:02:28 -0600519 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
520 BTRFS_BLOCK_RSV_TEMP);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400521 pending_snapshot->dentry = dentry;
522 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800523 pending_snapshot->readonly = readonly;
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200524 if (inherit) {
525 pending_snapshot->inherit = *inherit;
526 *inherit = NULL; /* take responsibility to free it */
527 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400528
Miao Xie48c03c42012-09-06 04:03:56 -0600529 trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400530 if (IS_ERR(trans)) {
531 ret = PTR_ERR(trans);
532 goto fail;
533 }
534
535 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
536 BUG_ON(ret);
537
Josef Bacik83515832011-06-14 15:16:14 -0400538 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400539 list_add(&pending_snapshot->list,
540 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400541 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400542 if (async_transid) {
543 *async_transid = trans->transid;
544 ret = btrfs_commit_transaction_async(trans,
545 root->fs_info->extent_root, 1);
546 } else {
547 ret = btrfs_commit_transaction(trans,
548 root->fs_info->extent_root);
549 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400550 BUG_ON(ret);
551
552 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400553 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000554 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500556 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
557 if (ret)
558 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400559
Al Viro2fbe8c82011-07-16 21:38:06 -0400560 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000561 if (IS_ERR(inode)) {
562 ret = PTR_ERR(inode);
563 goto fail;
564 }
565 BUG_ON(!inode);
566 d_instantiate(dentry, inode);
567 ret = 0;
568fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400569 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570 return ret;
571}
572
Sage Weil4260f7c2010-10-29 15:46:43 -0400573/* copy of check_sticky in fs/namei.c()
574* It's inline, so penalty for filesystems that don't use sticky bit is
575* minimal.
576*/
577static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
578{
579 uid_t fsuid = current_fsuid();
580
581 if (!(dir->i_mode & S_ISVTX))
582 return 0;
583 if (inode->i_uid == fsuid)
584 return 0;
585 if (dir->i_uid == fsuid)
586 return 0;
587 return !capable(CAP_FOWNER);
588}
589
590/* copy of may_delete in fs/namei.c()
591 * Check whether we can remove a link victim from directory dir, check
592 * whether the type of victim is right.
593 * 1. We can't do it if dir is read-only (done in permission())
594 * 2. We should have write and exec permissions on dir
595 * 3. We can't remove anything from append-only dir
596 * 4. We can't do anything with immutable dir (done in permission())
597 * 5. If the sticky bit on dir is set we should either
598 * a. be owner of dir, or
599 * b. be owner of victim, or
600 * c. have CAP_FOWNER capability
601 * 6. If the victim is append-only or immutable we can't do antyhing with
602 * links pointing to it.
603 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
604 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
605 * 9. We can't remove a root or mountpoint.
606 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
607 * nfs_async_unlink().
608 */
609
610static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
611{
612 int error;
613
614 if (!victim->d_inode)
615 return -ENOENT;
616
617 BUG_ON(victim->d_parent->d_inode != dir);
618 audit_inode_child(victim, dir);
619
620 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
621 if (error)
622 return error;
623 if (IS_APPEND(dir))
624 return -EPERM;
625 if (btrfs_check_sticky(dir, victim->d_inode)||
626 IS_APPEND(victim->d_inode)||
627 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
628 return -EPERM;
629 if (isdir) {
630 if (!S_ISDIR(victim->d_inode->i_mode))
631 return -ENOTDIR;
632 if (IS_ROOT(victim))
633 return -EBUSY;
634 } else if (S_ISDIR(victim->d_inode->i_mode))
635 return -EISDIR;
636 if (IS_DEADDIR(dir))
637 return -ENOENT;
638 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
639 return -EBUSY;
640 return 0;
641}
642
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400643/* copy of may_create in fs/namei.c() */
644static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
645{
646 if (child->d_inode)
647 return -EEXIST;
648 if (IS_DEADDIR(dir))
649 return -ENOENT;
650 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
651}
652
653/*
654 * Create a new subvolume below @parent. This is largely modeled after
655 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
656 * inside this filesystem so it's quite a bit simpler.
657 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400658static noinline int btrfs_mksubvol(struct path *parent,
659 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400660 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200661 u64 *async_transid, bool readonly,
662 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400663{
Yan, Zheng76dda932009-09-21 16:00:26 -0400664 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400665 struct dentry *dentry;
666 int error;
667
Yan, Zheng76dda932009-09-21 16:00:26 -0400668 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400669
670 dentry = lookup_one_len(name, parent->dentry, namelen);
671 error = PTR_ERR(dentry);
672 if (IS_ERR(dentry))
673 goto out_unlock;
674
675 error = -EEXIST;
676 if (dentry->d_inode)
677 goto out_dput;
678
Yan, Zheng76dda932009-09-21 16:00:26 -0400679 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400680 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600681 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400682
Yan, Zheng76dda932009-09-21 16:00:26 -0400683 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
684
685 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
686 goto out_up_read;
687
Chris Mason3de45862008-11-17 21:02:50 -0500688 if (snap_src) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200689 error = create_snapshot(snap_src, dentry, name, namelen,
690 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500691 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400692 error = create_subvol(BTRFS_I(dir)->root, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200693 name, namelen, async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500694 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400695 if (!error)
696 fsnotify_mkdir(dir, dentry);
697out_up_read:
698 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400699out_dput:
700 dput(dentry);
701out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400702 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400703 return error;
704}
705
Chris Mason4cb53002011-05-24 15:35:30 -0400706/*
707 * When we're defragging a range, we don't want to kick it off again
708 * if it is really just waiting for delalloc to send it down.
709 * If we find a nice big extent or delalloc range for the bytes in the
710 * file you want to defrag, we return 0 to let you know to skip this
711 * part of the file
712 */
713static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
714{
715 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
716 struct extent_map *em = NULL;
717 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
718 u64 end;
719
720 read_lock(&em_tree->lock);
721 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
722 read_unlock(&em_tree->lock);
723
724 if (em) {
725 end = extent_map_end(em);
726 free_extent_map(em);
727 if (end - offset > thresh)
728 return 0;
729 }
730 /* if we already have a nice delalloc here, just stop */
731 thresh /= 2;
732 end = count_range_bits(io_tree, &offset, offset + thresh,
733 thresh, EXTENT_DELALLOC, 1);
734 if (end >= thresh)
735 return 0;
736 return 1;
737}
738
739/*
740 * helper function to walk through a file and find extents
741 * newer than a specific transid, and smaller than thresh.
742 *
743 * This is used by the defragging code to find new and small
744 * extents
745 */
746static int find_new_extents(struct btrfs_root *root,
747 struct inode *inode, u64 newer_than,
748 u64 *off, int thresh)
749{
750 struct btrfs_path *path;
751 struct btrfs_key min_key;
752 struct btrfs_key max_key;
753 struct extent_buffer *leaf;
754 struct btrfs_file_extent_item *extent;
755 int type;
756 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000757 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400758
759 path = btrfs_alloc_path();
760 if (!path)
761 return -ENOMEM;
762
David Sterbaa4689d22011-05-31 17:08:14 +0000763 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400764 min_key.type = BTRFS_EXTENT_DATA_KEY;
765 min_key.offset = *off;
766
David Sterbaa4689d22011-05-31 17:08:14 +0000767 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400768 max_key.type = (u8)-1;
769 max_key.offset = (u64)-1;
770
771 path->keep_locks = 1;
772
773 while(1) {
774 ret = btrfs_search_forward(root, &min_key, &max_key,
775 path, 0, newer_than);
776 if (ret != 0)
777 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000778 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400779 goto none;
780 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
781 goto none;
782
783 leaf = path->nodes[0];
784 extent = btrfs_item_ptr(leaf, path->slots[0],
785 struct btrfs_file_extent_item);
786
787 type = btrfs_file_extent_type(leaf, extent);
788 if (type == BTRFS_FILE_EXTENT_REG &&
789 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
790 check_defrag_in_cache(inode, min_key.offset, thresh)) {
791 *off = min_key.offset;
792 btrfs_free_path(path);
793 return 0;
794 }
795
796 if (min_key.offset == (u64)-1)
797 goto none;
798
799 min_key.offset++;
800 btrfs_release_path(path);
801 }
802none:
803 btrfs_free_path(path);
804 return -ENOENT;
805}
806
Li Zefan6c282eb2012-06-11 16:03:35 +0800807static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400808{
809 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500810 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800811 struct extent_map *em;
812 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500813
814 /*
815 * hopefully we have this extent in the tree already, try without
816 * the full extent lock
817 */
818 read_lock(&em_tree->lock);
819 em = lookup_extent_mapping(em_tree, start, len);
820 read_unlock(&em_tree->lock);
821
822 if (!em) {
823 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100824 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500825 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100826 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500827
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000828 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800829 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500830 }
831
Li Zefan6c282eb2012-06-11 16:03:35 +0800832 return em;
833}
834
835static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
836{
837 struct extent_map *next;
838 bool ret = true;
839
840 /* this is the last extent */
841 if (em->start + em->len >= i_size_read(inode))
842 return false;
843
844 next = defrag_lookup_extent(inode, em->start + em->len);
845 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
846 ret = false;
847
848 free_extent_map(next);
849 return ret;
850}
851
852static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400853 u64 *last_len, u64 *skip, u64 *defrag_end,
854 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800855{
856 struct extent_map *em;
857 int ret = 1;
858 bool next_mergeable = true;
859
860 /*
861 * make sure that once we start defragging an extent, we keep on
862 * defragging it
863 */
864 if (start < *defrag_end)
865 return 1;
866
867 *skip = 0;
868
869 em = defrag_lookup_extent(inode, start);
870 if (!em)
871 return 0;
872
Chris Mason940100a2010-03-10 10:52:59 -0500873 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400874 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500875 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400876 goto out;
877 }
878
Li Zefan6c282eb2012-06-11 16:03:35 +0800879 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500880
881 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800882 * we hit a real extent, if it is big or the next extent is not a
883 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500884 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400885 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800886 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500887 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400888out:
Chris Mason940100a2010-03-10 10:52:59 -0500889 /*
890 * last_len ends up being a counter of how many bytes we've defragged.
891 * every time we choose not to defrag an extent, we reset *last_len
892 * so that the next tiny extent will force a defrag.
893 *
894 * The end result of this is that tiny extents before a single big
895 * extent will force at least part of that big extent to be defragged.
896 */
897 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500898 *defrag_end = extent_map_end(em);
899 } else {
900 *last_len = 0;
901 *skip = extent_map_end(em);
902 *defrag_end = 0;
903 }
904
905 free_extent_map(em);
906 return ret;
907}
908
Chris Mason4cb53002011-05-24 15:35:30 -0400909/*
910 * it doesn't do much good to defrag one or two pages
911 * at a time. This pulls in a nice chunk of pages
912 * to COW and defrag.
913 *
914 * It also makes sure the delalloc code has enough
915 * dirty data to avoid making new small extents as part
916 * of the defrag
917 *
918 * It's a good idea to start RA on this range
919 * before calling this.
920 */
921static int cluster_pages_for_defrag(struct inode *inode,
922 struct page **pages,
923 unsigned long start_index,
924 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400925{
Chris Mason4cb53002011-05-24 15:35:30 -0400926 unsigned long file_end;
927 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400928 u64 page_start;
929 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400930 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400931 int ret;
932 int i;
933 int i_done;
934 struct btrfs_ordered_extent *ordered;
935 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800936 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400937 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400938
Chris Mason4cb53002011-05-24 15:35:30 -0400939 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400940 if (!isize || start_index > file_end)
941 return 0;
942
943 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400944
945 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400946 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400947 if (ret)
948 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400949 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800950 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400951
952 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400953 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400954 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800955again:
Josef Bacika94733d2011-07-11 10:47:06 -0400956 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800957 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400958 if (!page)
959 break;
960
Miao Xie600a45e2012-02-16 15:01:24 +0800961 page_start = page_offset(page);
962 page_end = page_start + PAGE_CACHE_SIZE - 1;
963 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100964 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800965 ordered = btrfs_lookup_ordered_extent(inode,
966 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100967 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800968 if (!ordered)
969 break;
970
971 unlock_page(page);
972 btrfs_start_ordered_extent(inode, ordered, 1);
973 btrfs_put_ordered_extent(ordered);
974 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -0400975 /*
976 * we unlocked the page above, so we need check if
977 * it was released or not.
978 */
979 if (page->mapping != inode->i_mapping) {
980 unlock_page(page);
981 page_cache_release(page);
982 goto again;
983 }
Miao Xie600a45e2012-02-16 15:01:24 +0800984 }
985
Chris Mason4cb53002011-05-24 15:35:30 -0400986 if (!PageUptodate(page)) {
987 btrfs_readpage(NULL, page);
988 lock_page(page);
989 if (!PageUptodate(page)) {
990 unlock_page(page);
991 page_cache_release(page);
992 ret = -EIO;
993 break;
994 }
995 }
Miao Xie600a45e2012-02-16 15:01:24 +0800996
Miao Xie600a45e2012-02-16 15:01:24 +0800997 if (page->mapping != inode->i_mapping) {
998 unlock_page(page);
999 page_cache_release(page);
1000 goto again;
1001 }
1002
Chris Mason4cb53002011-05-24 15:35:30 -04001003 pages[i] = page;
1004 i_done++;
1005 }
1006 if (!i_done || ret)
1007 goto out;
1008
1009 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1010 goto out;
1011
1012 /*
1013 * so now we have a nice long stream of locked
1014 * and up to date pages, lets wait on them
1015 */
1016 for (i = 0; i < i_done; i++)
1017 wait_on_page_writeback(pages[i]);
1018
1019 page_start = page_offset(pages[0]);
1020 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1021
1022 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001023 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001024 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1025 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001026 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1027 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001028
Liu Bo1f12bd02012-03-29 09:57:44 -04001029 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001030 spin_lock(&BTRFS_I(inode)->lock);
1031 BTRFS_I(inode)->outstanding_extents++;
1032 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001033 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001034 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001035 }
1036
1037
Liu Bo9e8a4a82012-09-05 19:10:51 -06001038 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1039 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001040
1041 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1042 page_start, page_end - 1, &cached_state,
1043 GFP_NOFS);
1044
1045 for (i = 0; i < i_done; i++) {
1046 clear_page_dirty_for_io(pages[i]);
1047 ClearPageChecked(pages[i]);
1048 set_page_extent_mapped(pages[i]);
1049 set_page_dirty(pages[i]);
1050 unlock_page(pages[i]);
1051 page_cache_release(pages[i]);
1052 }
1053 return i_done;
1054out:
1055 for (i = 0; i < i_done; i++) {
1056 unlock_page(pages[i]);
1057 page_cache_release(pages[i]);
1058 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001059 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001060 return ret;
1061
1062}
1063
1064int btrfs_defrag_file(struct inode *inode, struct file *file,
1065 struct btrfs_ioctl_defrag_range_args *range,
1066 u64 newer_than, unsigned long max_to_defrag)
1067{
1068 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001069 struct file_ra_state *ra = NULL;
1070 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001071 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001072 u64 last_len = 0;
1073 u64 skip = 0;
1074 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001075 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001076 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001077 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001078 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001079 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001080 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001081 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001082 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1083 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001084 u64 new_align = ~((u64)128 * 1024 - 1);
1085 struct page **pages = NULL;
1086
1087 if (extent_thresh == 0)
1088 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001089
1090 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1091 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1092 return -EINVAL;
1093 if (range->compress_type)
1094 compress_type = range->compress_type;
1095 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001096
Li Zefan151a31b2011-09-02 15:56:39 +08001097 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001098 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001099
Chris Mason4cb53002011-05-24 15:35:30 -04001100 /*
1101 * if we were not given a file, allocate a readahead
1102 * context
1103 */
1104 if (!file) {
1105 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1106 if (!ra)
1107 return -ENOMEM;
1108 file_ra_state_init(ra, inode->i_mapping);
1109 } else {
1110 ra = &file->f_ra;
1111 }
1112
Li Zefan008873e2011-09-02 15:57:07 +08001113 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001114 GFP_NOFS);
1115 if (!pages) {
1116 ret = -ENOMEM;
1117 goto out_ra;
1118 }
1119
1120 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001121 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001122 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001123 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1124 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001125 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001126 }
1127
Chris Mason4cb53002011-05-24 15:35:30 -04001128 if (newer_than) {
1129 ret = find_new_extents(root, inode, newer_than,
1130 &newer_off, 64 * 1024);
1131 if (!ret) {
1132 range->start = newer_off;
1133 /*
1134 * we always align our defrag to help keep
1135 * the extents in the file evenly spaced
1136 */
1137 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001138 } else
1139 goto out_ra;
1140 } else {
1141 i = range->start >> PAGE_CACHE_SHIFT;
1142 }
1143 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001144 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001145
Li Zefan2a0f7f52011-10-10 15:43:34 -04001146 /*
1147 * make writeback starts from i, so the defrag range can be
1148 * written sequentially.
1149 */
1150 if (i < inode->i_mapping->writeback_index)
1151 inode->i_mapping->writeback_index = i;
1152
Chris Masonf7f43cc2011-10-11 11:41:40 -04001153 while (i <= last_index && defrag_count < max_to_defrag &&
1154 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1155 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001156 /*
1157 * make sure we stop running if someone unmounts
1158 * the FS
1159 */
1160 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1161 break;
1162
Liu Bo66c26892012-03-29 09:57:45 -04001163 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001164 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001165 &defrag_end, range->flags &
1166 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001167 unsigned long next;
1168 /*
1169 * the should_defrag function tells us how much to skip
1170 * bump our counter by the suggested amount
1171 */
1172 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1173 i = max(i + 1, next);
1174 continue;
1175 }
Li Zefan008873e2011-09-02 15:57:07 +08001176
1177 if (!newer_than) {
1178 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1179 PAGE_CACHE_SHIFT) - i;
1180 cluster = min(cluster, max_cluster);
1181 } else {
1182 cluster = max_cluster;
1183 }
1184
Chris Mason1e701a32010-03-11 09:42:04 -05001185 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001186 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001187
Li Zefan008873e2011-09-02 15:57:07 +08001188 if (i + cluster > ra_index) {
1189 ra_index = max(i, ra_index);
1190 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1191 cluster);
1192 ra_index += max_cluster;
1193 }
Chris Mason940100a2010-03-10 10:52:59 -05001194
Liu Boecb8bea2012-03-29 09:57:44 -04001195 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001196 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001197 if (ret < 0) {
1198 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001199 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001200 }
Chris Mason940100a2010-03-10 10:52:59 -05001201
Chris Mason4cb53002011-05-24 15:35:30 -04001202 defrag_count += ret;
1203 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001204 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001205
1206 if (newer_than) {
1207 if (newer_off == (u64)-1)
1208 break;
1209
Liu Boe1f041e2012-03-29 09:57:45 -04001210 if (ret > 0)
1211 i += ret;
1212
Chris Mason4cb53002011-05-24 15:35:30 -04001213 newer_off = max(newer_off + 1,
1214 (u64)i << PAGE_CACHE_SHIFT);
1215
1216 ret = find_new_extents(root, inode,
1217 newer_than, &newer_off,
1218 64 * 1024);
1219 if (!ret) {
1220 range->start = newer_off;
1221 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001222 } else {
1223 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001224 }
Chris Mason4cb53002011-05-24 15:35:30 -04001225 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001226 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001227 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001228 last_len += ret << PAGE_CACHE_SHIFT;
1229 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001230 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001231 last_len = 0;
1232 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001233 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001234 }
1235
Chris Mason1e701a32010-03-11 09:42:04 -05001236 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1237 filemap_flush(inode->i_mapping);
1238
1239 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1240 /* the filemap_flush will queue IO into the worker threads, but
1241 * we have to make sure the IO is actually started and that
1242 * ordered extents get created before we return
1243 */
1244 atomic_inc(&root->fs_info->async_submit_draining);
1245 while (atomic_read(&root->fs_info->nr_async_submits) ||
1246 atomic_read(&root->fs_info->async_delalloc_pages)) {
1247 wait_event(root->fs_info->async_submit_wait,
1248 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1249 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1250 }
1251 atomic_dec(&root->fs_info->async_submit_draining);
1252
1253 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001254 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001255 mutex_unlock(&inode->i_mutex);
1256 }
1257
Li Zefan1a419d82010-10-25 15:12:50 +08001258 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001259 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001260 }
1261
Diego Calleja60ccf822011-09-01 16:33:57 +02001262 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001263
Chris Mason4cb53002011-05-24 15:35:30 -04001264out_ra:
1265 if (!file)
1266 kfree(ra);
1267 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001268 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001269}
1270
Yan, Zheng76dda932009-09-21 16:00:26 -04001271static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1272 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001273{
1274 u64 new_size;
1275 u64 old_size;
1276 u64 devid = 1;
1277 struct btrfs_ioctl_vol_args *vol_args;
1278 struct btrfs_trans_handle *trans;
1279 struct btrfs_device *device = NULL;
1280 char *sizestr;
1281 char *devstr = NULL;
1282 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001283 int mod = 0;
1284
Yan Zhengc146afa2008-11-12 14:34:12 -05001285 if (root->fs_info->sb->s_flags & MS_RDONLY)
1286 return -EROFS;
1287
Chris Masone441d542009-01-05 16:57:23 -05001288 if (!capable(CAP_SYS_ADMIN))
1289 return -EPERM;
1290
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001291 mutex_lock(&root->fs_info->volume_mutex);
1292 if (root->fs_info->balance_ctl) {
1293 printk(KERN_INFO "btrfs: balance in progress\n");
1294 ret = -EINVAL;
1295 goto out;
1296 }
1297
Li Zefandae7b662009-04-08 15:06:54 +08001298 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001299 if (IS_ERR(vol_args)) {
1300 ret = PTR_ERR(vol_args);
1301 goto out;
1302 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001303
1304 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001305
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001306 sizestr = vol_args->name;
1307 devstr = strchr(sizestr, ':');
1308 if (devstr) {
1309 char *end;
1310 sizestr = devstr + 1;
1311 *devstr = '\0';
1312 devstr = vol_args->name;
1313 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001314 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001315 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001316 }
Yan Zheng2b820322008-11-17 21:11:30 -05001317 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001318 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001319 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001320 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001321 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001322 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001323 }
Liu Bo4e42ae12012-06-14 02:23:19 -06001324 if (device->fs_devices && device->fs_devices->seeding) {
1325 printk(KERN_INFO "btrfs: resizer unable to apply on "
Chris Masona8c4a332012-06-15 20:07:17 -04001326 "seeding device %llu\n",
1327 (unsigned long long)devid);
Liu Bo4e42ae12012-06-14 02:23:19 -06001328 ret = -EINVAL;
1329 goto out_free;
1330 }
1331
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001332 if (!strcmp(sizestr, "max"))
1333 new_size = device->bdev->bd_inode->i_size;
1334 else {
1335 if (sizestr[0] == '-') {
1336 mod = -1;
1337 sizestr++;
1338 } else if (sizestr[0] == '+') {
1339 mod = 1;
1340 sizestr++;
1341 }
Akinobu Mita91748462010-02-28 10:59:11 +00001342 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001343 if (new_size == 0) {
1344 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001345 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001346 }
1347 }
1348
1349 old_size = device->total_bytes;
1350
1351 if (mod < 0) {
1352 if (new_size > old_size) {
1353 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001354 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001355 }
1356 new_size = old_size - new_size;
1357 } else if (mod > 0) {
1358 new_size = old_size + new_size;
1359 }
1360
1361 if (new_size < 256 * 1024 * 1024) {
1362 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001363 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001364 }
1365 if (new_size > device->bdev->bd_inode->i_size) {
1366 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001367 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001368 }
1369
1370 do_div(new_size, root->sectorsize);
1371 new_size *= root->sectorsize;
1372
Josef Bacik606686e2012-06-04 14:03:51 -04001373 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1374 rcu_str_deref(device->name),
1375 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001376
1377 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001378 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001379 if (IS_ERR(trans)) {
1380 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001381 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001382 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001383 ret = btrfs_grow_device(trans, device, new_size);
1384 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001385 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001386 ret = btrfs_shrink_device(device, new_size);
1387 }
1388
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001389out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001390 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001391out:
1392 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001393 return ret;
1394}
1395
Sage Weil72fd0322010-10-29 15:41:32 -04001396static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001397 char *name, unsigned long fd, int subvol,
1398 u64 *transid, bool readonly,
1399 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001400{
Chris Mason3de45862008-11-17 21:02:50 -05001401 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001402 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001403 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001404
Liu Boa874a632012-06-29 03:58:46 -06001405 ret = mnt_want_write_file(file);
1406 if (ret)
1407 goto out;
1408
Sage Weil72fd0322010-10-29 15:41:32 -04001409 namelen = strlen(name);
1410 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001411 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001412 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001413 }
1414
Chris Mason16780ca2012-02-20 22:14:55 -05001415 if (name[0] == '.' &&
1416 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1417 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001418 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001419 }
1420
Chris Mason3de45862008-11-17 21:02:50 -05001421 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001422 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001423 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001424 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001425 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001426 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001427 if (!src_file) {
1428 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001429 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001430 }
1431
1432 src_inode = src_file->f_path.dentry->d_inode;
1433 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001434 printk(KERN_INFO "btrfs: Snapshot src from "
1435 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001436 ret = -EINVAL;
1437 fput(src_file);
Liu Boa874a632012-06-29 03:58:46 -06001438 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001439 }
Sage Weil72fd0322010-10-29 15:41:32 -04001440 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1441 BTRFS_I(src_inode)->root,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001442 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001443 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001444 }
Liu Boa874a632012-06-29 03:58:46 -06001445out_drop_write:
1446 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001447out:
Sage Weil72fd0322010-10-29 15:41:32 -04001448 return ret;
1449}
1450
1451static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001452 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001453{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001454 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001455 int ret;
1456
Li Zefanfa0d2b92010-12-20 15:53:28 +08001457 vol_args = memdup_user(arg, sizeof(*vol_args));
1458 if (IS_ERR(vol_args))
1459 return PTR_ERR(vol_args);
1460 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001461
Li Zefanfa0d2b92010-12-20 15:53:28 +08001462 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001463 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001464 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001465
Li Zefanfa0d2b92010-12-20 15:53:28 +08001466 kfree(vol_args);
1467 return ret;
1468}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001469
Li Zefanfa0d2b92010-12-20 15:53:28 +08001470static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1471 void __user *arg, int subvol)
1472{
1473 struct btrfs_ioctl_vol_args_v2 *vol_args;
1474 int ret;
1475 u64 transid = 0;
1476 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001477 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001478 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001479
Li Zefanfa0d2b92010-12-20 15:53:28 +08001480 vol_args = memdup_user(arg, sizeof(*vol_args));
1481 if (IS_ERR(vol_args))
1482 return PTR_ERR(vol_args);
1483 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001484
Li Zefanb83cc962010-12-20 16:04:08 +08001485 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001486 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1487 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001488 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001489 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001490 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001491
1492 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1493 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001494 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1495 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001496 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1497 if (vol_args->size > PAGE_CACHE_SIZE) {
1498 ret = -EINVAL;
1499 goto out;
1500 }
1501 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1502 if (IS_ERR(inherit)) {
1503 ret = PTR_ERR(inherit);
1504 goto out;
1505 }
1506 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001507
1508 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001509 vol_args->fd, subvol, ptr,
1510 readonly, &inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001511
1512 if (ret == 0 && ptr &&
1513 copy_to_user(arg +
1514 offsetof(struct btrfs_ioctl_vol_args_v2,
1515 transid), ptr, sizeof(*ptr)))
1516 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001517out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001518 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001519 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520 return ret;
1521}
1522
Li Zefan0caa1022010-12-20 16:30:25 +08001523static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1524 void __user *arg)
1525{
1526 struct inode *inode = fdentry(file)->d_inode;
1527 struct btrfs_root *root = BTRFS_I(inode)->root;
1528 int ret = 0;
1529 u64 flags = 0;
1530
Li Zefan33345d012011-04-20 10:31:50 +08001531 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001532 return -EINVAL;
1533
1534 down_read(&root->fs_info->subvol_sem);
1535 if (btrfs_root_readonly(root))
1536 flags |= BTRFS_SUBVOL_RDONLY;
1537 up_read(&root->fs_info->subvol_sem);
1538
1539 if (copy_to_user(arg, &flags, sizeof(flags)))
1540 ret = -EFAULT;
1541
1542 return ret;
1543}
1544
1545static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1546 void __user *arg)
1547{
1548 struct inode *inode = fdentry(file)->d_inode;
1549 struct btrfs_root *root = BTRFS_I(inode)->root;
1550 struct btrfs_trans_handle *trans;
1551 u64 root_flags;
1552 u64 flags;
1553 int ret = 0;
1554
Liu Bob9ca0662012-06-29 03:58:49 -06001555 ret = mnt_want_write_file(file);
1556 if (ret)
1557 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001558
Liu Bob9ca0662012-06-29 03:58:49 -06001559 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1560 ret = -EINVAL;
1561 goto out_drop_write;
1562 }
Li Zefan0caa1022010-12-20 16:30:25 +08001563
Liu Bob9ca0662012-06-29 03:58:49 -06001564 if (copy_from_user(&flags, arg, sizeof(flags))) {
1565 ret = -EFAULT;
1566 goto out_drop_write;
1567 }
Li Zefan0caa1022010-12-20 16:30:25 +08001568
Liu Bob9ca0662012-06-29 03:58:49 -06001569 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1570 ret = -EINVAL;
1571 goto out_drop_write;
1572 }
Li Zefan0caa1022010-12-20 16:30:25 +08001573
Liu Bob9ca0662012-06-29 03:58:49 -06001574 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1575 ret = -EOPNOTSUPP;
1576 goto out_drop_write;
1577 }
Li Zefan0caa1022010-12-20 16:30:25 +08001578
Liu Bob9ca0662012-06-29 03:58:49 -06001579 if (!inode_owner_or_capable(inode)) {
1580 ret = -EACCES;
1581 goto out_drop_write;
1582 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001583
Li Zefan0caa1022010-12-20 16:30:25 +08001584 down_write(&root->fs_info->subvol_sem);
1585
1586 /* nothing to do */
1587 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001588 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001589
1590 root_flags = btrfs_root_flags(&root->root_item);
1591 if (flags & BTRFS_SUBVOL_RDONLY)
1592 btrfs_set_root_flags(&root->root_item,
1593 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1594 else
1595 btrfs_set_root_flags(&root->root_item,
1596 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1597
1598 trans = btrfs_start_transaction(root, 1);
1599 if (IS_ERR(trans)) {
1600 ret = PTR_ERR(trans);
1601 goto out_reset;
1602 }
1603
Li Zefanb4dc2b82011-02-16 06:06:34 +00001604 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001605 &root->root_key, &root->root_item);
1606
1607 btrfs_commit_transaction(trans, root);
1608out_reset:
1609 if (ret)
1610 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001611out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001612 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001613out_drop_write:
1614 mnt_drop_write_file(file);
1615out:
Li Zefan0caa1022010-12-20 16:30:25 +08001616 return ret;
1617}
1618
Yan, Zheng76dda932009-09-21 16:00:26 -04001619/*
1620 * helper to check if the subvolume references other subvolumes
1621 */
1622static noinline int may_destroy_subvol(struct btrfs_root *root)
1623{
1624 struct btrfs_path *path;
1625 struct btrfs_key key;
1626 int ret;
1627
1628 path = btrfs_alloc_path();
1629 if (!path)
1630 return -ENOMEM;
1631
1632 key.objectid = root->root_key.objectid;
1633 key.type = BTRFS_ROOT_REF_KEY;
1634 key.offset = (u64)-1;
1635
1636 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1637 &key, path, 0, 0);
1638 if (ret < 0)
1639 goto out;
1640 BUG_ON(ret == 0);
1641
1642 ret = 0;
1643 if (path->slots[0] > 0) {
1644 path->slots[0]--;
1645 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1646 if (key.objectid == root->root_key.objectid &&
1647 key.type == BTRFS_ROOT_REF_KEY)
1648 ret = -ENOTEMPTY;
1649 }
1650out:
1651 btrfs_free_path(path);
1652 return ret;
1653}
1654
Chris Masonac8e9812010-02-28 15:39:26 -05001655static noinline int key_in_sk(struct btrfs_key *key,
1656 struct btrfs_ioctl_search_key *sk)
1657{
Chris Masonabc6e132010-03-18 12:10:08 -04001658 struct btrfs_key test;
1659 int ret;
1660
1661 test.objectid = sk->min_objectid;
1662 test.type = sk->min_type;
1663 test.offset = sk->min_offset;
1664
1665 ret = btrfs_comp_cpu_keys(key, &test);
1666 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001667 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001668
1669 test.objectid = sk->max_objectid;
1670 test.type = sk->max_type;
1671 test.offset = sk->max_offset;
1672
1673 ret = btrfs_comp_cpu_keys(key, &test);
1674 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001675 return 0;
1676 return 1;
1677}
1678
1679static noinline int copy_to_sk(struct btrfs_root *root,
1680 struct btrfs_path *path,
1681 struct btrfs_key *key,
1682 struct btrfs_ioctl_search_key *sk,
1683 char *buf,
1684 unsigned long *sk_offset,
1685 int *num_found)
1686{
1687 u64 found_transid;
1688 struct extent_buffer *leaf;
1689 struct btrfs_ioctl_search_header sh;
1690 unsigned long item_off;
1691 unsigned long item_len;
1692 int nritems;
1693 int i;
1694 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001695 int ret = 0;
1696
1697 leaf = path->nodes[0];
1698 slot = path->slots[0];
1699 nritems = btrfs_header_nritems(leaf);
1700
1701 if (btrfs_header_generation(leaf) > sk->max_transid) {
1702 i = nritems;
1703 goto advance_key;
1704 }
1705 found_transid = btrfs_header_generation(leaf);
1706
1707 for (i = slot; i < nritems; i++) {
1708 item_off = btrfs_item_ptr_offset(leaf, i);
1709 item_len = btrfs_item_size_nr(leaf, i);
1710
1711 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1712 item_len = 0;
1713
1714 if (sizeof(sh) + item_len + *sk_offset >
1715 BTRFS_SEARCH_ARGS_BUFSIZE) {
1716 ret = 1;
1717 goto overflow;
1718 }
1719
1720 btrfs_item_key_to_cpu(leaf, key, i);
1721 if (!key_in_sk(key, sk))
1722 continue;
1723
1724 sh.objectid = key->objectid;
1725 sh.offset = key->offset;
1726 sh.type = key->type;
1727 sh.len = item_len;
1728 sh.transid = found_transid;
1729
1730 /* copy search result header */
1731 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1732 *sk_offset += sizeof(sh);
1733
1734 if (item_len) {
1735 char *p = buf + *sk_offset;
1736 /* copy the item */
1737 read_extent_buffer(leaf, p,
1738 item_off, item_len);
1739 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001740 }
Hugo Millse2156862011-05-14 17:43:41 +00001741 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001742
1743 if (*num_found >= sk->nr_items)
1744 break;
1745 }
1746advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001747 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001748 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1749 key->offset++;
1750 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1751 key->offset = 0;
1752 key->type++;
1753 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1754 key->offset = 0;
1755 key->type = 0;
1756 key->objectid++;
1757 } else
1758 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001759overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001760 return ret;
1761}
1762
1763static noinline int search_ioctl(struct inode *inode,
1764 struct btrfs_ioctl_search_args *args)
1765{
1766 struct btrfs_root *root;
1767 struct btrfs_key key;
1768 struct btrfs_key max_key;
1769 struct btrfs_path *path;
1770 struct btrfs_ioctl_search_key *sk = &args->key;
1771 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1772 int ret;
1773 int num_found = 0;
1774 unsigned long sk_offset = 0;
1775
1776 path = btrfs_alloc_path();
1777 if (!path)
1778 return -ENOMEM;
1779
1780 if (sk->tree_id == 0) {
1781 /* search the root of the inode that was passed */
1782 root = BTRFS_I(inode)->root;
1783 } else {
1784 key.objectid = sk->tree_id;
1785 key.type = BTRFS_ROOT_ITEM_KEY;
1786 key.offset = (u64)-1;
1787 root = btrfs_read_fs_root_no_name(info, &key);
1788 if (IS_ERR(root)) {
1789 printk(KERN_ERR "could not find root %llu\n",
1790 sk->tree_id);
1791 btrfs_free_path(path);
1792 return -ENOENT;
1793 }
1794 }
1795
1796 key.objectid = sk->min_objectid;
1797 key.type = sk->min_type;
1798 key.offset = sk->min_offset;
1799
1800 max_key.objectid = sk->max_objectid;
1801 max_key.type = sk->max_type;
1802 max_key.offset = sk->max_offset;
1803
1804 path->keep_locks = 1;
1805
1806 while(1) {
1807 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1808 sk->min_transid);
1809 if (ret != 0) {
1810 if (ret > 0)
1811 ret = 0;
1812 goto err;
1813 }
1814 ret = copy_to_sk(root, path, &key, sk, args->buf,
1815 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001816 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001817 if (ret || num_found >= sk->nr_items)
1818 break;
1819
1820 }
1821 ret = 0;
1822err:
1823 sk->nr_items = num_found;
1824 btrfs_free_path(path);
1825 return ret;
1826}
1827
1828static noinline int btrfs_ioctl_tree_search(struct file *file,
1829 void __user *argp)
1830{
1831 struct btrfs_ioctl_search_args *args;
1832 struct inode *inode;
1833 int ret;
1834
1835 if (!capable(CAP_SYS_ADMIN))
1836 return -EPERM;
1837
Julia Lawall2354d082010-10-29 15:14:18 -04001838 args = memdup_user(argp, sizeof(*args));
1839 if (IS_ERR(args))
1840 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001841
Chris Masonac8e9812010-02-28 15:39:26 -05001842 inode = fdentry(file)->d_inode;
1843 ret = search_ioctl(inode, args);
1844 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1845 ret = -EFAULT;
1846 kfree(args);
1847 return ret;
1848}
1849
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001850/*
Chris Masonac8e9812010-02-28 15:39:26 -05001851 * Search INODE_REFs to identify path name of 'dirid' directory
1852 * in a 'tree_id' tree. and sets path name to 'name'.
1853 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001854static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1855 u64 tree_id, u64 dirid, char *name)
1856{
1857 struct btrfs_root *root;
1858 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001859 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001860 int ret = -1;
1861 int slot;
1862 int len;
1863 int total_len = 0;
1864 struct btrfs_inode_ref *iref;
1865 struct extent_buffer *l;
1866 struct btrfs_path *path;
1867
1868 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1869 name[0]='\0';
1870 return 0;
1871 }
1872
1873 path = btrfs_alloc_path();
1874 if (!path)
1875 return -ENOMEM;
1876
Chris Masonac8e9812010-02-28 15:39:26 -05001877 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001878
1879 key.objectid = tree_id;
1880 key.type = BTRFS_ROOT_ITEM_KEY;
1881 key.offset = (u64)-1;
1882 root = btrfs_read_fs_root_no_name(info, &key);
1883 if (IS_ERR(root)) {
1884 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001885 ret = -ENOENT;
1886 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001887 }
1888
1889 key.objectid = dirid;
1890 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001891 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001892
1893 while(1) {
1894 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1895 if (ret < 0)
1896 goto out;
1897
1898 l = path->nodes[0];
1899 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001900 if (ret > 0 && slot > 0)
1901 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001902 btrfs_item_key_to_cpu(l, &key, slot);
1903
1904 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001905 key.type != BTRFS_INODE_REF_KEY)) {
1906 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001907 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001908 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001909
1910 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1911 len = btrfs_inode_ref_name_len(l, iref);
1912 ptr -= len + 1;
1913 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001914 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001915 goto out;
1916
1917 *(ptr + len) = '/';
1918 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1919
1920 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1921 break;
1922
David Sterbab3b4aa72011-04-21 01:20:15 +02001923 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001924 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001925 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001926 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001927 }
Chris Masonac8e9812010-02-28 15:39:26 -05001928 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001929 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001930 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001931 name[total_len]='\0';
1932 ret = 0;
1933out:
1934 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001935 return ret;
1936}
1937
1938static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1939 void __user *argp)
1940{
1941 struct btrfs_ioctl_ino_lookup_args *args;
1942 struct inode *inode;
1943 int ret;
1944
1945 if (!capable(CAP_SYS_ADMIN))
1946 return -EPERM;
1947
Julia Lawall2354d082010-10-29 15:14:18 -04001948 args = memdup_user(argp, sizeof(*args));
1949 if (IS_ERR(args))
1950 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001951
Chris Masonac8e9812010-02-28 15:39:26 -05001952 inode = fdentry(file)->d_inode;
1953
Chris Mason1b53ac42010-03-18 12:17:05 -04001954 if (args->treeid == 0)
1955 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1956
Chris Masonac8e9812010-02-28 15:39:26 -05001957 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1958 args->treeid, args->objectid,
1959 args->name);
1960
1961 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1962 ret = -EFAULT;
1963
1964 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001965 return ret;
1966}
1967
Yan, Zheng76dda932009-09-21 16:00:26 -04001968static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1969 void __user *arg)
1970{
1971 struct dentry *parent = fdentry(file);
1972 struct dentry *dentry;
1973 struct inode *dir = parent->d_inode;
1974 struct inode *inode;
1975 struct btrfs_root *root = BTRFS_I(dir)->root;
1976 struct btrfs_root *dest = NULL;
1977 struct btrfs_ioctl_vol_args *vol_args;
1978 struct btrfs_trans_handle *trans;
1979 int namelen;
1980 int ret;
1981 int err = 0;
1982
Yan, Zheng76dda932009-09-21 16:00:26 -04001983 vol_args = memdup_user(arg, sizeof(*vol_args));
1984 if (IS_ERR(vol_args))
1985 return PTR_ERR(vol_args);
1986
1987 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1988 namelen = strlen(vol_args->name);
1989 if (strchr(vol_args->name, '/') ||
1990 strncmp(vol_args->name, "..", namelen) == 0) {
1991 err = -EINVAL;
1992 goto out;
1993 }
1994
Al Viroa561be72011-11-23 11:57:51 -05001995 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001996 if (err)
1997 goto out;
1998
1999 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2000 dentry = lookup_one_len(vol_args->name, parent, namelen);
2001 if (IS_ERR(dentry)) {
2002 err = PTR_ERR(dentry);
2003 goto out_unlock_dir;
2004 }
2005
2006 if (!dentry->d_inode) {
2007 err = -ENOENT;
2008 goto out_dput;
2009 }
2010
2011 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002012 dest = BTRFS_I(inode)->root;
2013 if (!capable(CAP_SYS_ADMIN)){
2014 /*
2015 * Regular user. Only allow this with a special mount
2016 * option, when the user has write+exec access to the
2017 * subvol root, and when rmdir(2) would have been
2018 * allowed.
2019 *
2020 * Note that this is _not_ check that the subvol is
2021 * empty or doesn't contain data that we wouldn't
2022 * otherwise be able to delete.
2023 *
2024 * Users who want to delete empty subvols should try
2025 * rmdir(2).
2026 */
2027 err = -EPERM;
2028 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2029 goto out_dput;
2030
2031 /*
2032 * Do not allow deletion if the parent dir is the same
2033 * as the dir to be deleted. That means the ioctl
2034 * must be called on the dentry referencing the root
2035 * of the subvol, not a random directory contained
2036 * within it.
2037 */
2038 err = -EINVAL;
2039 if (root == dest)
2040 goto out_dput;
2041
2042 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2043 if (err)
2044 goto out_dput;
2045
2046 /* check if subvolume may be deleted by a non-root user */
2047 err = btrfs_may_delete(dir, dentry, 1);
2048 if (err)
2049 goto out_dput;
2050 }
2051
Li Zefan33345d012011-04-20 10:31:50 +08002052 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002053 err = -EINVAL;
2054 goto out_dput;
2055 }
2056
Yan, Zheng76dda932009-09-21 16:00:26 -04002057 mutex_lock(&inode->i_mutex);
2058 err = d_invalidate(dentry);
2059 if (err)
2060 goto out_unlock;
2061
2062 down_write(&root->fs_info->subvol_sem);
2063
2064 err = may_destroy_subvol(dest);
2065 if (err)
2066 goto out_up_write;
2067
Yan, Zhenga22285a2010-05-16 10:48:46 -04002068 trans = btrfs_start_transaction(root, 0);
2069 if (IS_ERR(trans)) {
2070 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002071 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002072 }
2073 trans->block_rsv = &root->fs_info->global_block_rsv;
2074
Yan, Zheng76dda932009-09-21 16:00:26 -04002075 ret = btrfs_unlink_subvol(trans, root, dir,
2076 dest->root_key.objectid,
2077 dentry->d_name.name,
2078 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002079 if (ret) {
2080 err = ret;
2081 btrfs_abort_transaction(trans, root, ret);
2082 goto out_end_trans;
2083 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002084
2085 btrfs_record_root_in_trans(trans, dest);
2086
2087 memset(&dest->root_item.drop_progress, 0,
2088 sizeof(dest->root_item.drop_progress));
2089 dest->root_item.drop_level = 0;
2090 btrfs_set_root_refs(&dest->root_item, 0);
2091
Yan, Zhengd68fc572010-05-16 10:49:58 -04002092 if (!xchg(&dest->orphan_item_inserted, 1)) {
2093 ret = btrfs_insert_orphan_item(trans,
2094 root->fs_info->tree_root,
2095 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002096 if (ret) {
2097 btrfs_abort_transaction(trans, root, ret);
2098 err = ret;
2099 goto out_end_trans;
2100 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002101 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002102out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002103 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002104 if (ret && !err)
2105 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002106 inode->i_flags |= S_DEAD;
2107out_up_write:
2108 up_write(&root->fs_info->subvol_sem);
2109out_unlock:
2110 mutex_unlock(&inode->i_mutex);
2111 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002112 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002113 btrfs_invalidate_inodes(dest);
2114 d_delete(dentry);
2115 }
2116out_dput:
2117 dput(dentry);
2118out_unlock_dir:
2119 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002120 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002121out:
2122 kfree(vol_args);
2123 return err;
2124}
2125
Chris Mason1e701a32010-03-11 09:42:04 -05002126static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002127{
2128 struct inode *inode = fdentry(file)->d_inode;
2129 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002130 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002131 int ret;
2132
Li Zefanb83cc962010-12-20 16:04:08 +08002133 if (btrfs_root_readonly(root))
2134 return -EROFS;
2135
Al Viroa561be72011-11-23 11:57:51 -05002136 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002137 if (ret)
2138 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002139
2140 switch (inode->i_mode & S_IFMT) {
2141 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002142 if (!capable(CAP_SYS_ADMIN)) {
2143 ret = -EPERM;
2144 goto out;
2145 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002146 ret = btrfs_defrag_root(root, 0);
2147 if (ret)
2148 goto out;
2149 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002150 break;
2151 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002152 if (!(file->f_mode & FMODE_WRITE)) {
2153 ret = -EINVAL;
2154 goto out;
2155 }
Chris Mason1e701a32010-03-11 09:42:04 -05002156
2157 range = kzalloc(sizeof(*range), GFP_KERNEL);
2158 if (!range) {
2159 ret = -ENOMEM;
2160 goto out;
2161 }
2162
2163 if (argp) {
2164 if (copy_from_user(range, argp,
2165 sizeof(*range))) {
2166 ret = -EFAULT;
2167 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002168 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002169 }
2170 /* compression requires us to start the IO */
2171 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2172 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2173 range->extent_thresh = (u32)-1;
2174 }
2175 } else {
2176 /* the rest are all set to zero by kzalloc */
2177 range->len = (u64)-1;
2178 }
Chris Mason4cb53002011-05-24 15:35:30 -04002179 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2180 range, 0, 0);
2181 if (ret > 0)
2182 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002183 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002184 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002185 default:
2186 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002187 }
Chris Masone441d542009-01-05 16:57:23 -05002188out:
Al Viro2a79f172011-12-09 08:06:57 -05002189 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002190 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002191}
2192
Christoph Hellwigb2950862008-12-02 09:54:17 -05002193static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002194{
2195 struct btrfs_ioctl_vol_args *vol_args;
2196 int ret;
2197
Chris Masone441d542009-01-05 16:57:23 -05002198 if (!capable(CAP_SYS_ADMIN))
2199 return -EPERM;
2200
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002201 mutex_lock(&root->fs_info->volume_mutex);
2202 if (root->fs_info->balance_ctl) {
2203 printk(KERN_INFO "btrfs: balance in progress\n");
2204 ret = -EINVAL;
2205 goto out;
2206 }
2207
Li Zefandae7b662009-04-08 15:06:54 +08002208 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002209 if (IS_ERR(vol_args)) {
2210 ret = PTR_ERR(vol_args);
2211 goto out;
2212 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002213
Mark Fasheh5516e592008-07-24 12:20:14 -04002214 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002215 ret = btrfs_init_new_device(root, vol_args->name);
2216
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002217 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002218out:
2219 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002220 return ret;
2221}
2222
Christoph Hellwigb2950862008-12-02 09:54:17 -05002223static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002224{
2225 struct btrfs_ioctl_vol_args *vol_args;
2226 int ret;
2227
Chris Masone441d542009-01-05 16:57:23 -05002228 if (!capable(CAP_SYS_ADMIN))
2229 return -EPERM;
2230
Yan Zhengc146afa2008-11-12 14:34:12 -05002231 if (root->fs_info->sb->s_flags & MS_RDONLY)
2232 return -EROFS;
2233
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002234 mutex_lock(&root->fs_info->volume_mutex);
2235 if (root->fs_info->balance_ctl) {
2236 printk(KERN_INFO "btrfs: balance in progress\n");
2237 ret = -EINVAL;
2238 goto out;
2239 }
2240
Li Zefandae7b662009-04-08 15:06:54 +08002241 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002242 if (IS_ERR(vol_args)) {
2243 ret = PTR_ERR(vol_args);
2244 goto out;
2245 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002246
Mark Fasheh5516e592008-07-24 12:20:14 -04002247 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002248 ret = btrfs_rm_device(root, vol_args->name);
2249
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002250 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002251out:
2252 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002253 return ret;
2254}
2255
Jan Schmidt475f6382011-03-11 15:41:01 +01002256static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2257{
Li Zefan027ed2f2011-06-08 08:27:56 +00002258 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002259 struct btrfs_device *device;
2260 struct btrfs_device *next;
2261 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002262 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002263
2264 if (!capable(CAP_SYS_ADMIN))
2265 return -EPERM;
2266
Li Zefan027ed2f2011-06-08 08:27:56 +00002267 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2268 if (!fi_args)
2269 return -ENOMEM;
2270
2271 fi_args->num_devices = fs_devices->num_devices;
2272 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002273
2274 mutex_lock(&fs_devices->device_list_mutex);
2275 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002276 if (device->devid > fi_args->max_id)
2277 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002278 }
2279 mutex_unlock(&fs_devices->device_list_mutex);
2280
Li Zefan027ed2f2011-06-08 08:27:56 +00002281 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2282 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002283
Li Zefan027ed2f2011-06-08 08:27:56 +00002284 kfree(fi_args);
2285 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002286}
2287
2288static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2289{
2290 struct btrfs_ioctl_dev_info_args *di_args;
2291 struct btrfs_device *dev;
2292 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2293 int ret = 0;
2294 char *s_uuid = NULL;
2295 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2296
2297 if (!capable(CAP_SYS_ADMIN))
2298 return -EPERM;
2299
2300 di_args = memdup_user(arg, sizeof(*di_args));
2301 if (IS_ERR(di_args))
2302 return PTR_ERR(di_args);
2303
2304 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2305 s_uuid = di_args->uuid;
2306
2307 mutex_lock(&fs_devices->device_list_mutex);
2308 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2309 mutex_unlock(&fs_devices->device_list_mutex);
2310
2311 if (!dev) {
2312 ret = -ENODEV;
2313 goto out;
2314 }
2315
2316 di_args->devid = dev->devid;
2317 di_args->bytes_used = dev->bytes_used;
2318 di_args->total_bytes = dev->total_bytes;
2319 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002320 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002321 struct rcu_string *name;
2322
2323 rcu_read_lock();
2324 name = rcu_dereference(dev->name);
2325 strncpy(di_args->path, name->str, sizeof(di_args->path));
2326 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002327 di_args->path[sizeof(di_args->path) - 1] = 0;
2328 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002329 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002330 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002331
2332out:
2333 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2334 ret = -EFAULT;
2335
2336 kfree(di_args);
2337 return ret;
2338}
2339
Yan, Zheng76dda932009-09-21 16:00:26 -04002340static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2341 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002342{
2343 struct inode *inode = fdentry(file)->d_inode;
2344 struct btrfs_root *root = BTRFS_I(inode)->root;
2345 struct file *src_file;
2346 struct inode *src;
2347 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002348 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002349 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002350 char *buf;
2351 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002352 u32 nritems;
2353 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002354 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002355 u64 len = olen;
2356 u64 bs = root->fs_info->sb->s_blocksize;
Chris Masond20f7042008-12-08 16:58:54 -05002357
Sage Weilc5c9cd42008-11-12 14:32:25 -05002358 /*
2359 * TODO:
2360 * - split compressed inline extents. annoying: we need to
2361 * decompress into destination's address_space (the file offset
2362 * may change, so source mapping won't do), then recompress (or
2363 * otherwise reinsert) a subrange.
2364 * - allow ranges within the same file to be cloned (provided
2365 * they don't overlap)?
2366 */
2367
Chris Masone441d542009-01-05 16:57:23 -05002368 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002369 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002370 return -EINVAL;
2371
Li Zefanb83cc962010-12-20 16:04:08 +08002372 if (btrfs_root_readonly(root))
2373 return -EROFS;
2374
Al Viroa561be72011-11-23 11:57:51 -05002375 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002376 if (ret)
2377 return ret;
2378
Sage Weilc5c9cd42008-11-12 14:32:25 -05002379 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002380 if (!src_file) {
2381 ret = -EBADF;
2382 goto out_drop_write;
2383 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002384
David Sterba362a20c2011-08-01 18:11:57 +02002385 ret = -EXDEV;
2386 if (src_file->f_path.mnt != file->f_path.mnt)
2387 goto out_fput;
2388
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002389 src = src_file->f_dentry->d_inode;
2390
Sage Weilc5c9cd42008-11-12 14:32:25 -05002391 ret = -EINVAL;
2392 if (src == inode)
2393 goto out_fput;
2394
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002395 /* the src must be open for reading */
2396 if (!(src_file->f_mode & FMODE_READ))
2397 goto out_fput;
2398
Li Zefan0e7b8242011-09-18 10:20:46 -04002399 /* don't make the dst file partly checksummed */
2400 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2401 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2402 goto out_fput;
2403
Yan Zhengae01a0a2008-08-04 23:23:47 -04002404 ret = -EISDIR;
2405 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002406 goto out_fput;
2407
Yan Zhengae01a0a2008-08-04 23:23:47 -04002408 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002409 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002410 goto out_fput;
2411
2412 ret = -ENOMEM;
2413 buf = vmalloc(btrfs_level_size(root, 0));
2414 if (!buf)
2415 goto out_fput;
2416
2417 path = btrfs_alloc_path();
2418 if (!path) {
2419 vfree(buf);
2420 goto out_fput;
2421 }
2422 path->reada = 2;
2423
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002424 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002425 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2426 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002427 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002428 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2429 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002430 }
2431
Sage Weilc5c9cd42008-11-12 14:32:25 -05002432 /* determine range to clone */
2433 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002434 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002435 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002436 if (len == 0)
2437 olen = len = src->i_size - off;
2438 /* if we extend to eof, continue to block boundary */
2439 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002440 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002441
2442 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002443 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2444 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002445 goto out_unlock;
2446
Li Zefand525e8a2011-09-11 10:52:25 -04002447 if (destoff > inode->i_size) {
2448 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2449 if (ret)
2450 goto out_unlock;
2451 }
2452
Li Zefan71ef0782011-09-18 10:20:46 -04002453 /* truncate page cache pages from target inode range */
2454 truncate_inode_pages_range(&inode->i_data, destoff,
2455 PAGE_CACHE_ALIGN(destoff + len) - 1);
2456
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002457 /* do any pending delalloc/csum calc on src, one way or
2458 another, and lock file content */
2459 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002460 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002461 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002462 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2463 if (!ordered &&
2464 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2465 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002466 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002467 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002468 if (ordered)
2469 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002470 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002471 }
2472
Sage Weilc5c9cd42008-11-12 14:32:25 -05002473 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002474 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002475 key.type = BTRFS_EXTENT_DATA_KEY;
2476 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002477
2478 while (1) {
2479 /*
2480 * note the key will change type as we walk through the
2481 * tree.
2482 */
David Sterba362a20c2011-08-01 18:11:57 +02002483 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2484 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002485 if (ret < 0)
2486 goto out;
2487
Yan Zhengae01a0a2008-08-04 23:23:47 -04002488 nritems = btrfs_header_nritems(path->nodes[0]);
2489 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002490 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002491 if (ret < 0)
2492 goto out;
2493 if (ret > 0)
2494 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002495 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002496 }
2497 leaf = path->nodes[0];
2498 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002499
Yan Zhengae01a0a2008-08-04 23:23:47 -04002500 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002501 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002502 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002503 break;
2504
Sage Weilc5c9cd42008-11-12 14:32:25 -05002505 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2506 struct btrfs_file_extent_item *extent;
2507 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002508 u32 size;
2509 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002510 u64 disko = 0, diskl = 0;
2511 u64 datao = 0, datal = 0;
2512 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002513 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002514
2515 size = btrfs_item_size_nr(leaf, slot);
2516 read_extent_buffer(leaf, buf,
2517 btrfs_item_ptr_offset(leaf, slot),
2518 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002519
2520 extent = btrfs_item_ptr(leaf, slot,
2521 struct btrfs_file_extent_item);
2522 comp = btrfs_file_extent_compression(leaf, extent);
2523 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002524 if (type == BTRFS_FILE_EXTENT_REG ||
2525 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002526 disko = btrfs_file_extent_disk_bytenr(leaf,
2527 extent);
2528 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2529 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002530 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002531 datal = btrfs_file_extent_num_bytes(leaf,
2532 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002533 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2534 /* take upper bound, may be compressed */
2535 datal = btrfs_file_extent_ram_bytes(leaf,
2536 extent);
2537 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002538 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002539
Sage Weil050006a2010-10-29 15:37:33 -04002540 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002541 key.offset >= off+len)
2542 goto next;
2543
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002545 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002546 if (off <= key.offset)
2547 new_key.offset = key.offset + destoff - off;
2548 else
2549 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002550
Sage Weilb6f34092011-09-20 14:48:51 -04002551 /*
2552 * 1 - adjusting old extent (we may have to split it)
2553 * 1 - add new extent
2554 * 1 - inode update
2555 */
2556 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002557 if (IS_ERR(trans)) {
2558 ret = PTR_ERR(trans);
2559 goto out;
2560 }
2561
Chris Masonc8a894d2009-06-27 21:07:03 -04002562 if (type == BTRFS_FILE_EXTENT_REG ||
2563 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002564 /*
2565 * a | --- range to clone ---| b
2566 * | ------------- extent ------------- |
2567 */
2568
2569 /* substract range b */
2570 if (key.offset + datal > off + len)
2571 datal = off + len - key.offset;
2572
2573 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002574 if (off > key.offset) {
2575 datao += off - key.offset;
2576 datal -= off - key.offset;
2577 }
2578
Josef Bacik5dc562c2012-08-17 13:14:17 -04002579 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002580 new_key.offset,
2581 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002582 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002583 if (ret) {
2584 btrfs_abort_transaction(trans, root,
2585 ret);
2586 btrfs_end_transaction(trans, root);
2587 goto out;
2588 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002589
Sage Weilc5c9cd42008-11-12 14:32:25 -05002590 ret = btrfs_insert_empty_item(trans, root, path,
2591 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002592 if (ret) {
2593 btrfs_abort_transaction(trans, root,
2594 ret);
2595 btrfs_end_transaction(trans, root);
2596 goto out;
2597 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002598
2599 leaf = path->nodes[0];
2600 slot = path->slots[0];
2601 write_extent_buffer(leaf, buf,
2602 btrfs_item_ptr_offset(leaf, slot),
2603 size);
2604
2605 extent = btrfs_item_ptr(leaf, slot,
2606 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002607
Sage Weilc5c9cd42008-11-12 14:32:25 -05002608 /* disko == 0 means it's a hole */
2609 if (!disko)
2610 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002611
2612 btrfs_set_file_extent_offset(leaf, extent,
2613 datao);
2614 btrfs_set_file_extent_num_bytes(leaf, extent,
2615 datal);
2616 if (disko) {
2617 inode_add_bytes(inode, datal);
2618 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002619 disko, diskl, 0,
2620 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002621 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002622 new_key.offset - datao,
2623 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002624 if (ret) {
2625 btrfs_abort_transaction(trans,
2626 root,
2627 ret);
2628 btrfs_end_transaction(trans,
2629 root);
2630 goto out;
2631
2632 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002633 }
2634 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2635 u64 skip = 0;
2636 u64 trim = 0;
2637 if (off > key.offset) {
2638 skip = off - key.offset;
2639 new_key.offset += skip;
2640 }
Chris Masond3977122009-01-05 21:25:51 -05002641
Sage Weilc5c9cd42008-11-12 14:32:25 -05002642 if (key.offset + datal > off+len)
2643 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002644
Sage Weilc5c9cd42008-11-12 14:32:25 -05002645 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002646 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002647 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002648 goto out;
2649 }
2650 size -= skip + trim;
2651 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002652
Josef Bacik5dc562c2012-08-17 13:14:17 -04002653 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002654 new_key.offset,
2655 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002656 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657 if (ret) {
2658 btrfs_abort_transaction(trans, root,
2659 ret);
2660 btrfs_end_transaction(trans, root);
2661 goto out;
2662 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002663
Sage Weilc5c9cd42008-11-12 14:32:25 -05002664 ret = btrfs_insert_empty_item(trans, root, path,
2665 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002666 if (ret) {
2667 btrfs_abort_transaction(trans, root,
2668 ret);
2669 btrfs_end_transaction(trans, root);
2670 goto out;
2671 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002672
2673 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002674 u32 start =
2675 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002676 memmove(buf+start, buf+start+skip,
2677 datal);
2678 }
2679
2680 leaf = path->nodes[0];
2681 slot = path->slots[0];
2682 write_extent_buffer(leaf, buf,
2683 btrfs_item_ptr_offset(leaf, slot),
2684 size);
2685 inode_add_bytes(inode, datal);
2686 }
2687
2688 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002689 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002690
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002691 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002692 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002693
2694 /*
2695 * we round up to the block size at eof when
2696 * determining which extents to clone above,
2697 * but shouldn't round up the file size
2698 */
2699 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002700 if (endoff > destoff+olen)
2701 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002702 if (endoff > inode->i_size)
2703 btrfs_i_size_write(inode, endoff);
2704
Yan, Zhenga22285a2010-05-16 10:48:46 -04002705 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002706 if (ret) {
2707 btrfs_abort_transaction(trans, root, ret);
2708 btrfs_end_transaction(trans, root);
2709 goto out;
2710 }
2711 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002712 }
Chris Masond3977122009-01-05 21:25:51 -05002713next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002714 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002715 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002716 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002717 ret = 0;
2718out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002719 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002720 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002721out_unlock:
2722 mutex_unlock(&src->i_mutex);
2723 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002724 vfree(buf);
2725 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002726out_fput:
2727 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002728out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002729 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002730 return ret;
2731}
2732
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002733static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002734{
2735 struct btrfs_ioctl_clone_range_args args;
2736
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002737 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002738 return -EFAULT;
2739 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2740 args.src_length, args.dest_offset);
2741}
2742
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002743/*
2744 * there are many ways the trans_start and trans_end ioctls can lead
2745 * to deadlocks. They should only be used by applications that
2746 * basically own the machine, and have a very in depth understanding
2747 * of all the possible deadlocks and enospc problems.
2748 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002749static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002750{
2751 struct inode *inode = fdentry(file)->d_inode;
2752 struct btrfs_root *root = BTRFS_I(inode)->root;
2753 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002754 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002755
Sage Weil1ab86ae2009-09-29 18:38:44 -04002756 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002757 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002758 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002759
2760 ret = -EINPROGRESS;
2761 if (file->private_data)
2762 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002763
Li Zefanb83cc962010-12-20 16:04:08 +08002764 ret = -EROFS;
2765 if (btrfs_root_readonly(root))
2766 goto out;
2767
Al Viroa561be72011-11-23 11:57:51 -05002768 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002769 if (ret)
2770 goto out;
2771
Josef Bacika4abeea2011-04-11 17:25:13 -04002772 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002773
Sage Weil1ab86ae2009-09-29 18:38:44 -04002774 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002775 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002776 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002777 goto out_drop;
2778
2779 file->private_data = trans;
2780 return 0;
2781
2782out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002783 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002784 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002785out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002786 return ret;
2787}
2788
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002789static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2790{
2791 struct inode *inode = fdentry(file)->d_inode;
2792 struct btrfs_root *root = BTRFS_I(inode)->root;
2793 struct btrfs_root *new_root;
2794 struct btrfs_dir_item *di;
2795 struct btrfs_trans_handle *trans;
2796 struct btrfs_path *path;
2797 struct btrfs_key location;
2798 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002799 u64 objectid = 0;
2800 u64 dir_id;
2801
2802 if (!capable(CAP_SYS_ADMIN))
2803 return -EPERM;
2804
2805 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2806 return -EFAULT;
2807
2808 if (!objectid)
2809 objectid = root->root_key.objectid;
2810
2811 location.objectid = objectid;
2812 location.type = BTRFS_ROOT_ITEM_KEY;
2813 location.offset = (u64)-1;
2814
2815 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2816 if (IS_ERR(new_root))
2817 return PTR_ERR(new_root);
2818
2819 if (btrfs_root_refs(&new_root->root_item) == 0)
2820 return -ENOENT;
2821
2822 path = btrfs_alloc_path();
2823 if (!path)
2824 return -ENOMEM;
2825 path->leave_spinning = 1;
2826
2827 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002828 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002829 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002830 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002831 }
2832
David Sterba6c417612011-04-13 15:41:04 +02002833 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002834 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2835 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002836 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002837 btrfs_free_path(path);
2838 btrfs_end_transaction(trans, root);
2839 printk(KERN_ERR "Umm, you don't have the default dir item, "
2840 "this isn't going to work\n");
2841 return -ENOENT;
2842 }
2843
2844 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2845 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2846 btrfs_mark_buffer_dirty(path->nodes[0]);
2847 btrfs_free_path(path);
2848
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06002849 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002850 btrfs_end_transaction(trans, root);
2851
2852 return 0;
2853}
2854
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002855static void get_block_group_info(struct list_head *groups_list,
2856 struct btrfs_ioctl_space_info *space)
2857{
2858 struct btrfs_block_group_cache *block_group;
2859
2860 space->total_bytes = 0;
2861 space->used_bytes = 0;
2862 space->flags = 0;
2863 list_for_each_entry(block_group, groups_list, list) {
2864 space->flags = block_group->flags;
2865 space->total_bytes += block_group->key.offset;
2866 space->used_bytes +=
2867 btrfs_block_group_used(&block_group->item);
2868 }
2869}
2870
Josef Bacik1406e432010-01-13 18:19:06 +00002871long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2872{
2873 struct btrfs_ioctl_space_args space_args;
2874 struct btrfs_ioctl_space_info space;
2875 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002876 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002877 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002878 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002879 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2880 BTRFS_BLOCK_GROUP_SYSTEM,
2881 BTRFS_BLOCK_GROUP_METADATA,
2882 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2883 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002884 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002885 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002886 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002887 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002888
2889 if (copy_from_user(&space_args,
2890 (struct btrfs_ioctl_space_args __user *)arg,
2891 sizeof(space_args)))
2892 return -EFAULT;
2893
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002894 for (i = 0; i < num_types; i++) {
2895 struct btrfs_space_info *tmp;
2896
2897 info = NULL;
2898 rcu_read_lock();
2899 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2900 list) {
2901 if (tmp->flags == types[i]) {
2902 info = tmp;
2903 break;
2904 }
2905 }
2906 rcu_read_unlock();
2907
2908 if (!info)
2909 continue;
2910
2911 down_read(&info->groups_sem);
2912 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2913 if (!list_empty(&info->block_groups[c]))
2914 slot_count++;
2915 }
2916 up_read(&info->groups_sem);
2917 }
Josef Bacik1406e432010-01-13 18:19:06 +00002918
Chris Mason7fde62b2010-03-16 15:40:10 -04002919 /* space_slots == 0 means they are asking for a count */
2920 if (space_args.space_slots == 0) {
2921 space_args.total_spaces = slot_count;
2922 goto out;
2923 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002924
Dan Rosenberg51788b12011-02-14 16:04:23 -05002925 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002926
Chris Mason7fde62b2010-03-16 15:40:10 -04002927 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002928
Chris Mason7fde62b2010-03-16 15:40:10 -04002929 /* we generally have at most 6 or so space infos, one for each raid
2930 * level. So, a whole page should be more than enough for everyone
2931 */
2932 if (alloc_size > PAGE_CACHE_SIZE)
2933 return -ENOMEM;
2934
2935 space_args.total_spaces = 0;
2936 dest = kmalloc(alloc_size, GFP_NOFS);
2937 if (!dest)
2938 return -ENOMEM;
2939 dest_orig = dest;
2940
2941 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002942 for (i = 0; i < num_types; i++) {
2943 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002944
Dan Rosenberg51788b12011-02-14 16:04:23 -05002945 if (!slot_count)
2946 break;
2947
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002948 info = NULL;
2949 rcu_read_lock();
2950 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2951 list) {
2952 if (tmp->flags == types[i]) {
2953 info = tmp;
2954 break;
2955 }
2956 }
2957 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002958
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002959 if (!info)
2960 continue;
2961 down_read(&info->groups_sem);
2962 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2963 if (!list_empty(&info->block_groups[c])) {
2964 get_block_group_info(&info->block_groups[c],
2965 &space);
2966 memcpy(dest, &space, sizeof(space));
2967 dest++;
2968 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002969 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002970 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002971 if (!slot_count)
2972 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002973 }
2974 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002975 }
Josef Bacik1406e432010-01-13 18:19:06 +00002976
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08002977 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04002978 (arg + sizeof(struct btrfs_ioctl_space_args));
2979
2980 if (copy_to_user(user_dest, dest_orig, alloc_size))
2981 ret = -EFAULT;
2982
2983 kfree(dest_orig);
2984out:
2985 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002986 ret = -EFAULT;
2987
2988 return ret;
2989}
2990
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002991/*
2992 * there are many ways the trans_start and trans_end ioctls can lead
2993 * to deadlocks. They should only be used by applications that
2994 * basically own the machine, and have a very in depth understanding
2995 * of all the possible deadlocks and enospc problems.
2996 */
2997long btrfs_ioctl_trans_end(struct file *file)
2998{
2999 struct inode *inode = fdentry(file)->d_inode;
3000 struct btrfs_root *root = BTRFS_I(inode)->root;
3001 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003002
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003003 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003004 if (!trans)
3005 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003006 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003007
Sage Weil1ab86ae2009-09-29 18:38:44 -04003008 btrfs_end_transaction(trans, root);
3009
Josef Bacika4abeea2011-04-11 17:25:13 -04003010 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003011
Al Viro2a79f172011-12-09 08:06:57 -05003012 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003013 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003014}
3015
Sage Weil46204592010-10-29 15:41:32 -04003016static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
3017{
3018 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3019 struct btrfs_trans_handle *trans;
3020 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003021 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003022
3023 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003024 if (IS_ERR(trans))
3025 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04003026 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003027 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003028 if (ret) {
3029 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003030 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003031 }
Sage Weil46204592010-10-29 15:41:32 -04003032
3033 if (argp)
3034 if (copy_to_user(argp, &transid, sizeof(transid)))
3035 return -EFAULT;
3036 return 0;
3037}
3038
3039static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
3040{
3041 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3042 u64 transid;
3043
3044 if (argp) {
3045 if (copy_from_user(&transid, argp, sizeof(transid)))
3046 return -EFAULT;
3047 } else {
3048 transid = 0; /* current trans */
3049 }
3050 return btrfs_wait_for_commit(root, transid);
3051}
3052
Jan Schmidt475f6382011-03-11 15:41:01 +01003053static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3054{
3055 int ret;
3056 struct btrfs_ioctl_scrub_args *sa;
3057
3058 if (!capable(CAP_SYS_ADMIN))
3059 return -EPERM;
3060
3061 sa = memdup_user(arg, sizeof(*sa));
3062 if (IS_ERR(sa))
3063 return PTR_ERR(sa);
3064
3065 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01003066 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01003067
3068 if (copy_to_user(arg, sa, sizeof(*sa)))
3069 ret = -EFAULT;
3070
3071 kfree(sa);
3072 return ret;
3073}
3074
3075static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3076{
3077 if (!capable(CAP_SYS_ADMIN))
3078 return -EPERM;
3079
3080 return btrfs_scrub_cancel(root);
3081}
3082
3083static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3084 void __user *arg)
3085{
3086 struct btrfs_ioctl_scrub_args *sa;
3087 int ret;
3088
3089 if (!capable(CAP_SYS_ADMIN))
3090 return -EPERM;
3091
3092 sa = memdup_user(arg, sizeof(*sa));
3093 if (IS_ERR(sa))
3094 return PTR_ERR(sa);
3095
3096 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3097
3098 if (copy_to_user(arg, sa, sizeof(*sa)))
3099 ret = -EFAULT;
3100
3101 kfree(sa);
3102 return ret;
3103}
3104
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003105static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003106 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003107{
3108 struct btrfs_ioctl_get_dev_stats *sa;
3109 int ret;
3110
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003111 sa = memdup_user(arg, sizeof(*sa));
3112 if (IS_ERR(sa))
3113 return PTR_ERR(sa);
3114
David Sterbab27f7c02012-06-22 06:30:39 -06003115 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3116 kfree(sa);
3117 return -EPERM;
3118 }
3119
3120 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003121
3122 if (copy_to_user(arg, sa, sizeof(*sa)))
3123 ret = -EFAULT;
3124
3125 kfree(sa);
3126 return ret;
3127}
3128
Jan Schmidtd7728c92011-07-07 16:48:38 +02003129static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3130{
3131 int ret = 0;
3132 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003133 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003134 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003135 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003136 struct inode_fs_paths *ipath = NULL;
3137 struct btrfs_path *path;
3138
3139 if (!capable(CAP_SYS_ADMIN))
3140 return -EPERM;
3141
3142 path = btrfs_alloc_path();
3143 if (!path) {
3144 ret = -ENOMEM;
3145 goto out;
3146 }
3147
3148 ipa = memdup_user(arg, sizeof(*ipa));
3149 if (IS_ERR(ipa)) {
3150 ret = PTR_ERR(ipa);
3151 ipa = NULL;
3152 goto out;
3153 }
3154
3155 size = min_t(u32, ipa->size, 4096);
3156 ipath = init_ipath(size, root, path);
3157 if (IS_ERR(ipath)) {
3158 ret = PTR_ERR(ipath);
3159 ipath = NULL;
3160 goto out;
3161 }
3162
3163 ret = paths_from_inode(ipa->inum, ipath);
3164 if (ret < 0)
3165 goto out;
3166
3167 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003168 rel_ptr = ipath->fspath->val[i] -
3169 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003170 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003171 }
3172
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003173 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3174 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003175 if (ret) {
3176 ret = -EFAULT;
3177 goto out;
3178 }
3179
3180out:
3181 btrfs_free_path(path);
3182 free_ipath(ipath);
3183 kfree(ipa);
3184
3185 return ret;
3186}
3187
3188static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3189{
3190 struct btrfs_data_container *inodes = ctx;
3191 const size_t c = 3 * sizeof(u64);
3192
3193 if (inodes->bytes_left >= c) {
3194 inodes->bytes_left -= c;
3195 inodes->val[inodes->elem_cnt] = inum;
3196 inodes->val[inodes->elem_cnt + 1] = offset;
3197 inodes->val[inodes->elem_cnt + 2] = root;
3198 inodes->elem_cnt += 3;
3199 } else {
3200 inodes->bytes_missing += c - inodes->bytes_left;
3201 inodes->bytes_left = 0;
3202 inodes->elem_missed += 3;
3203 }
3204
3205 return 0;
3206}
3207
3208static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3209 void __user *arg)
3210{
3211 int ret = 0;
3212 int size;
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;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003216
3217 if (!capable(CAP_SYS_ADMIN))
3218 return -EPERM;
3219
3220 loi = memdup_user(arg, sizeof(*loi));
3221 if (IS_ERR(loi)) {
3222 ret = PTR_ERR(loi);
3223 loi = NULL;
3224 goto out;
3225 }
3226
3227 path = btrfs_alloc_path();
3228 if (!path) {
3229 ret = -ENOMEM;
3230 goto out;
3231 }
3232
Liu Bo425d17a2012-09-07 20:01:30 -06003233 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003234 inodes = init_data_container(size);
3235 if (IS_ERR(inodes)) {
3236 ret = PTR_ERR(inodes);
3237 inodes = NULL;
3238 goto out;
3239 }
3240
Liu Bodf031f02012-09-07 20:01:29 -06003241 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3242 build_ino_list, inodes);
3243 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003244 ret = -ENOENT;
3245 if (ret < 0)
3246 goto out;
3247
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003248 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3249 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003250 if (ret)
3251 ret = -EFAULT;
3252
3253out:
3254 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003255 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003256 kfree(loi);
3257
3258 return ret;
3259}
3260
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003261void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003262 struct btrfs_ioctl_balance_args *bargs)
3263{
3264 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3265
3266 bargs->flags = bctl->flags;
3267
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003268 if (atomic_read(&fs_info->balance_running))
3269 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3270 if (atomic_read(&fs_info->balance_pause_req))
3271 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003272 if (atomic_read(&fs_info->balance_cancel_req))
3273 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003274
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003275 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3276 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3277 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003278
3279 if (lock) {
3280 spin_lock(&fs_info->balance_lock);
3281 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3282 spin_unlock(&fs_info->balance_lock);
3283 } else {
3284 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3285 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003286}
3287
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003288static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003289{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003290 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003291 struct btrfs_fs_info *fs_info = root->fs_info;
3292 struct btrfs_ioctl_balance_args *bargs;
3293 struct btrfs_balance_control *bctl;
3294 int ret;
3295
3296 if (!capable(CAP_SYS_ADMIN))
3297 return -EPERM;
3298
Liu Boe54bfa32012-06-29 03:58:48 -06003299 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003300 if (ret)
3301 return ret;
3302
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003303 mutex_lock(&fs_info->volume_mutex);
3304 mutex_lock(&fs_info->balance_mutex);
3305
3306 if (arg) {
3307 bargs = memdup_user(arg, sizeof(*bargs));
3308 if (IS_ERR(bargs)) {
3309 ret = PTR_ERR(bargs);
3310 goto out;
3311 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003312
3313 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3314 if (!fs_info->balance_ctl) {
3315 ret = -ENOTCONN;
3316 goto out_bargs;
3317 }
3318
3319 bctl = fs_info->balance_ctl;
3320 spin_lock(&fs_info->balance_lock);
3321 bctl->flags |= BTRFS_BALANCE_RESUME;
3322 spin_unlock(&fs_info->balance_lock);
3323
3324 goto do_balance;
3325 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003326 } else {
3327 bargs = NULL;
3328 }
3329
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003330 if (fs_info->balance_ctl) {
3331 ret = -EINPROGRESS;
3332 goto out_bargs;
3333 }
3334
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003335 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3336 if (!bctl) {
3337 ret = -ENOMEM;
3338 goto out_bargs;
3339 }
3340
3341 bctl->fs_info = fs_info;
3342 if (arg) {
3343 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3344 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3345 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3346
3347 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003348 } else {
3349 /* balance everything - no filters */
3350 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003351 }
3352
Ilya Dryomovde322262012-01-16 22:04:49 +02003353do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003354 ret = btrfs_balance(bctl, bargs);
3355 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003356 * bctl is freed in __cancel_balance or in free_fs_info if
3357 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003358 */
3359 if (arg) {
3360 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3361 ret = -EFAULT;
3362 }
3363
3364out_bargs:
3365 kfree(bargs);
3366out:
3367 mutex_unlock(&fs_info->balance_mutex);
3368 mutex_unlock(&fs_info->volume_mutex);
Liu Boe54bfa32012-06-29 03:58:48 -06003369 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003370 return ret;
3371}
3372
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003373static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3374{
3375 if (!capable(CAP_SYS_ADMIN))
3376 return -EPERM;
3377
3378 switch (cmd) {
3379 case BTRFS_BALANCE_CTL_PAUSE:
3380 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003381 case BTRFS_BALANCE_CTL_CANCEL:
3382 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003383 }
3384
3385 return -EINVAL;
3386}
3387
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003388static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3389 void __user *arg)
3390{
3391 struct btrfs_fs_info *fs_info = root->fs_info;
3392 struct btrfs_ioctl_balance_args *bargs;
3393 int ret = 0;
3394
3395 if (!capable(CAP_SYS_ADMIN))
3396 return -EPERM;
3397
3398 mutex_lock(&fs_info->balance_mutex);
3399 if (!fs_info->balance_ctl) {
3400 ret = -ENOTCONN;
3401 goto out;
3402 }
3403
3404 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3405 if (!bargs) {
3406 ret = -ENOMEM;
3407 goto out;
3408 }
3409
3410 update_ioctl_balance_args(fs_info, 1, bargs);
3411
3412 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3413 ret = -EFAULT;
3414
3415 kfree(bargs);
3416out:
3417 mutex_unlock(&fs_info->balance_mutex);
3418 return ret;
3419}
3420
Arne Jansen5d13a372011-09-14 15:53:51 +02003421static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3422{
3423 struct btrfs_ioctl_quota_ctl_args *sa;
3424 struct btrfs_trans_handle *trans = NULL;
3425 int ret;
3426 int err;
3427
3428 if (!capable(CAP_SYS_ADMIN))
3429 return -EPERM;
3430
3431 if (root->fs_info->sb->s_flags & MS_RDONLY)
3432 return -EROFS;
3433
3434 sa = memdup_user(arg, sizeof(*sa));
3435 if (IS_ERR(sa))
3436 return PTR_ERR(sa);
3437
3438 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3439 trans = btrfs_start_transaction(root, 2);
3440 if (IS_ERR(trans)) {
3441 ret = PTR_ERR(trans);
3442 goto out;
3443 }
3444 }
3445
3446 switch (sa->cmd) {
3447 case BTRFS_QUOTA_CTL_ENABLE:
3448 ret = btrfs_quota_enable(trans, root->fs_info);
3449 break;
3450 case BTRFS_QUOTA_CTL_DISABLE:
3451 ret = btrfs_quota_disable(trans, root->fs_info);
3452 break;
3453 case BTRFS_QUOTA_CTL_RESCAN:
3454 ret = btrfs_quota_rescan(root->fs_info);
3455 break;
3456 default:
3457 ret = -EINVAL;
3458 break;
3459 }
3460
3461 if (copy_to_user(arg, sa, sizeof(*sa)))
3462 ret = -EFAULT;
3463
3464 if (trans) {
3465 err = btrfs_commit_transaction(trans, root);
3466 if (err && !ret)
3467 ret = err;
3468 }
3469
3470out:
3471 kfree(sa);
3472 return ret;
3473}
3474
3475static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3476{
3477 struct btrfs_ioctl_qgroup_assign_args *sa;
3478 struct btrfs_trans_handle *trans;
3479 int ret;
3480 int err;
3481
3482 if (!capable(CAP_SYS_ADMIN))
3483 return -EPERM;
3484
3485 if (root->fs_info->sb->s_flags & MS_RDONLY)
3486 return -EROFS;
3487
3488 sa = memdup_user(arg, sizeof(*sa));
3489 if (IS_ERR(sa))
3490 return PTR_ERR(sa);
3491
3492 trans = btrfs_join_transaction(root);
3493 if (IS_ERR(trans)) {
3494 ret = PTR_ERR(trans);
3495 goto out;
3496 }
3497
3498 /* FIXME: check if the IDs really exist */
3499 if (sa->assign) {
3500 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3501 sa->src, sa->dst);
3502 } else {
3503 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3504 sa->src, sa->dst);
3505 }
3506
3507 err = btrfs_end_transaction(trans, root);
3508 if (err && !ret)
3509 ret = err;
3510
3511out:
3512 kfree(sa);
3513 return ret;
3514}
3515
3516static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3517{
3518 struct btrfs_ioctl_qgroup_create_args *sa;
3519 struct btrfs_trans_handle *trans;
3520 int ret;
3521 int err;
3522
3523 if (!capable(CAP_SYS_ADMIN))
3524 return -EPERM;
3525
3526 if (root->fs_info->sb->s_flags & MS_RDONLY)
3527 return -EROFS;
3528
3529 sa = memdup_user(arg, sizeof(*sa));
3530 if (IS_ERR(sa))
3531 return PTR_ERR(sa);
3532
3533 trans = btrfs_join_transaction(root);
3534 if (IS_ERR(trans)) {
3535 ret = PTR_ERR(trans);
3536 goto out;
3537 }
3538
3539 /* FIXME: check if the IDs really exist */
3540 if (sa->create) {
3541 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3542 NULL);
3543 } else {
3544 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3545 }
3546
3547 err = btrfs_end_transaction(trans, root);
3548 if (err && !ret)
3549 ret = err;
3550
3551out:
3552 kfree(sa);
3553 return ret;
3554}
3555
3556static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3557{
3558 struct btrfs_ioctl_qgroup_limit_args *sa;
3559 struct btrfs_trans_handle *trans;
3560 int ret;
3561 int err;
3562 u64 qgroupid;
3563
3564 if (!capable(CAP_SYS_ADMIN))
3565 return -EPERM;
3566
3567 if (root->fs_info->sb->s_flags & MS_RDONLY)
3568 return -EROFS;
3569
3570 sa = memdup_user(arg, sizeof(*sa));
3571 if (IS_ERR(sa))
3572 return PTR_ERR(sa);
3573
3574 trans = btrfs_join_transaction(root);
3575 if (IS_ERR(trans)) {
3576 ret = PTR_ERR(trans);
3577 goto out;
3578 }
3579
3580 qgroupid = sa->qgroupid;
3581 if (!qgroupid) {
3582 /* take the current subvol as qgroup */
3583 qgroupid = root->root_key.objectid;
3584 }
3585
3586 /* FIXME: check if the IDs really exist */
3587 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3588
3589 err = btrfs_end_transaction(trans, root);
3590 if (err && !ret)
3591 ret = err;
3592
3593out:
3594 kfree(sa);
3595 return ret;
3596}
3597
Alexander Block8ea05e32012-07-25 17:35:53 +02003598static long btrfs_ioctl_set_received_subvol(struct file *file,
3599 void __user *arg)
3600{
3601 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3602 struct inode *inode = fdentry(file)->d_inode;
3603 struct btrfs_root *root = BTRFS_I(inode)->root;
3604 struct btrfs_root_item *root_item = &root->root_item;
3605 struct btrfs_trans_handle *trans;
3606 struct timespec ct = CURRENT_TIME;
3607 int ret = 0;
3608
3609 ret = mnt_want_write_file(file);
3610 if (ret < 0)
3611 return ret;
3612
3613 down_write(&root->fs_info->subvol_sem);
3614
3615 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3616 ret = -EINVAL;
3617 goto out;
3618 }
3619
3620 if (btrfs_root_readonly(root)) {
3621 ret = -EROFS;
3622 goto out;
3623 }
3624
3625 if (!inode_owner_or_capable(inode)) {
3626 ret = -EACCES;
3627 goto out;
3628 }
3629
3630 sa = memdup_user(arg, sizeof(*sa));
3631 if (IS_ERR(sa)) {
3632 ret = PTR_ERR(sa);
3633 sa = NULL;
3634 goto out;
3635 }
3636
3637 trans = btrfs_start_transaction(root, 1);
3638 if (IS_ERR(trans)) {
3639 ret = PTR_ERR(trans);
3640 trans = NULL;
3641 goto out;
3642 }
3643
3644 sa->rtransid = trans->transid;
3645 sa->rtime.sec = ct.tv_sec;
3646 sa->rtime.nsec = ct.tv_nsec;
3647
3648 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3649 btrfs_set_root_stransid(root_item, sa->stransid);
3650 btrfs_set_root_rtransid(root_item, sa->rtransid);
3651 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3652 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3653 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3654 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3655
3656 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3657 &root->root_key, &root->root_item);
3658 if (ret < 0) {
3659 btrfs_end_transaction(trans, root);
3660 trans = NULL;
3661 goto out;
3662 } else {
3663 ret = btrfs_commit_transaction(trans, root);
3664 if (ret < 0)
3665 goto out;
3666 }
3667
3668 ret = copy_to_user(arg, sa, sizeof(*sa));
3669 if (ret)
3670 ret = -EFAULT;
3671
3672out:
3673 kfree(sa);
3674 up_write(&root->fs_info->subvol_sem);
3675 mnt_drop_write_file(file);
3676 return ret;
3677}
3678
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003679long btrfs_ioctl(struct file *file, unsigned int
3680 cmd, unsigned long arg)
3681{
3682 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003683 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003684
3685 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003686 case FS_IOC_GETFLAGS:
3687 return btrfs_ioctl_getflags(file, argp);
3688 case FS_IOC_SETFLAGS:
3689 return btrfs_ioctl_setflags(file, argp);
3690 case FS_IOC_GETVERSION:
3691 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003692 case FITRIM:
3693 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003694 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003695 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003696 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003697 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003698 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003699 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02003700 case BTRFS_IOC_SUBVOL_CREATE_V2:
3701 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003702 case BTRFS_IOC_SNAP_DESTROY:
3703 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003704 case BTRFS_IOC_SUBVOL_GETFLAGS:
3705 return btrfs_ioctl_subvol_getflags(file, argp);
3706 case BTRFS_IOC_SUBVOL_SETFLAGS:
3707 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003708 case BTRFS_IOC_DEFAULT_SUBVOL:
3709 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003710 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003711 return btrfs_ioctl_defrag(file, NULL);
3712 case BTRFS_IOC_DEFRAG_RANGE:
3713 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003714 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003715 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003716 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003717 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003718 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003719 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003720 case BTRFS_IOC_FS_INFO:
3721 return btrfs_ioctl_fs_info(root, argp);
3722 case BTRFS_IOC_DEV_INFO:
3723 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003724 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003725 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003726 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003727 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3728 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003729 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003730 case BTRFS_IOC_TRANS_START:
3731 return btrfs_ioctl_trans_start(file);
3732 case BTRFS_IOC_TRANS_END:
3733 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003734 case BTRFS_IOC_TREE_SEARCH:
3735 return btrfs_ioctl_tree_search(file, argp);
3736 case BTRFS_IOC_INO_LOOKUP:
3737 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003738 case BTRFS_IOC_INO_PATHS:
3739 return btrfs_ioctl_ino_to_path(root, argp);
3740 case BTRFS_IOC_LOGICAL_INO:
3741 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003742 case BTRFS_IOC_SPACE_INFO:
3743 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003744 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003745 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3746 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003747 case BTRFS_IOC_START_SYNC:
3748 return btrfs_ioctl_start_sync(file, argp);
3749 case BTRFS_IOC_WAIT_SYNC:
3750 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003751 case BTRFS_IOC_SCRUB:
3752 return btrfs_ioctl_scrub(root, argp);
3753 case BTRFS_IOC_SCRUB_CANCEL:
3754 return btrfs_ioctl_scrub_cancel(root, argp);
3755 case BTRFS_IOC_SCRUB_PROGRESS:
3756 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003757 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003758 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003759 case BTRFS_IOC_BALANCE_CTL:
3760 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003761 case BTRFS_IOC_BALANCE_PROGRESS:
3762 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02003763 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3764 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02003765 case BTRFS_IOC_SEND:
3766 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003767 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06003768 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003769 case BTRFS_IOC_QUOTA_CTL:
3770 return btrfs_ioctl_quota_ctl(root, argp);
3771 case BTRFS_IOC_QGROUP_ASSIGN:
3772 return btrfs_ioctl_qgroup_assign(root, argp);
3773 case BTRFS_IOC_QGROUP_CREATE:
3774 return btrfs_ioctl_qgroup_create(root, argp);
3775 case BTRFS_IOC_QGROUP_LIMIT:
3776 return btrfs_ioctl_qgroup_limit(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003777 }
3778
3779 return -ENOTTY;
3780}