blob: e9bdb8b783e568263ef140dc75fe92693213972b [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040052#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080053#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020054#include "backref.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040055
Christoph Hellwig6cbff002009-04-17 10:37:41 +020056/* Mask out flags that are inappropriate for the given type of inode. */
57static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
58{
59 if (S_ISDIR(mode))
60 return flags;
61 else if (S_ISREG(mode))
62 return flags & ~FS_DIRSYNC_FL;
63 else
64 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040066
Christoph Hellwig6cbff002009-04-17 10:37:41 +020067/*
68 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
69 */
70static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
71{
72 unsigned int iflags = 0;
73
74 if (flags & BTRFS_INODE_SYNC)
75 iflags |= FS_SYNC_FL;
76 if (flags & BTRFS_INODE_IMMUTABLE)
77 iflags |= FS_IMMUTABLE_FL;
78 if (flags & BTRFS_INODE_APPEND)
79 iflags |= FS_APPEND_FL;
80 if (flags & BTRFS_INODE_NODUMP)
81 iflags |= FS_NODUMP_FL;
82 if (flags & BTRFS_INODE_NOATIME)
83 iflags |= FS_NOATIME_FL;
84 if (flags & BTRFS_INODE_DIRSYNC)
85 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000086 if (flags & BTRFS_INODE_NODATACOW)
87 iflags |= FS_NOCOW_FL;
88
89 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
90 iflags |= FS_COMPR_FL;
91 else if (flags & BTRFS_INODE_NOCOMPRESS)
92 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093
94 return iflags;
95}
96
97/*
98 * Update inode->i_flags based on the btrfs internal flags.
99 */
100void btrfs_update_iflags(struct inode *inode)
101{
102 struct btrfs_inode *ip = BTRFS_I(inode);
103
104 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
105
106 if (ip->flags & BTRFS_INODE_SYNC)
107 inode->i_flags |= S_SYNC;
108 if (ip->flags & BTRFS_INODE_IMMUTABLE)
109 inode->i_flags |= S_IMMUTABLE;
110 if (ip->flags & BTRFS_INODE_APPEND)
111 inode->i_flags |= S_APPEND;
112 if (ip->flags & BTRFS_INODE_NOATIME)
113 inode->i_flags |= S_NOATIME;
114 if (ip->flags & BTRFS_INODE_DIRSYNC)
115 inode->i_flags |= S_DIRSYNC;
116}
117
118/*
119 * Inherit flags from the parent inode.
120 *
Josef Bacike27425d2011-09-27 11:01:30 -0400121 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200122 */
123void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
124{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400125 unsigned int flags;
126
127 if (!dir)
128 return;
129
130 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200131
Josef Bacike27425d2011-09-27 11:01:30 -0400132 if (flags & BTRFS_INODE_NOCOMPRESS) {
133 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
134 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
135 } else if (flags & BTRFS_INODE_COMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
138 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200139
Josef Bacike27425d2011-09-27 11:01:30 -0400140 if (flags & BTRFS_INODE_NODATACOW)
141 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
142
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143 btrfs_update_iflags(inode);
144}
145
146static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
147{
148 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
149 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
150
151 if (copy_to_user(arg, &flags, sizeof(flags)))
152 return -EFAULT;
153 return 0;
154}
155
Liu Bo75e7cb72011-03-22 10:12:20 +0000156static int check_flags(unsigned int flags)
157{
158 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
159 FS_NOATIME_FL | FS_NODUMP_FL | \
160 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000161 FS_NOCOMP_FL | FS_COMPR_FL |
162 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000163 return -EOPNOTSUPP;
164
165 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
166 return -EINVAL;
167
Liu Bo75e7cb72011-03-22 10:12:20 +0000168 return 0;
169}
170
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200171static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
172{
173 struct inode *inode = file->f_path.dentry->d_inode;
174 struct btrfs_inode *ip = BTRFS_I(inode);
175 struct btrfs_root *root = ip->root;
176 struct btrfs_trans_handle *trans;
177 unsigned int flags, oldflags;
178 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800179 u64 ip_oldflags;
180 unsigned int i_oldflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181
Li Zefanb83cc962010-12-20 16:04:08 +0800182 if (btrfs_root_readonly(root))
183 return -EROFS;
184
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 if (copy_from_user(&flags, arg, sizeof(flags)))
186 return -EFAULT;
187
Liu Bo75e7cb72011-03-22 10:12:20 +0000188 ret = check_flags(flags);
189 if (ret)
190 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700192 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193 return -EACCES;
194
195 mutex_lock(&inode->i_mutex);
196
Li Zefanf062abf2011-12-29 13:36:45 +0800197 ip_oldflags = ip->flags;
198 i_oldflags = inode->i_flags;
199
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200 flags = btrfs_mask_flags(inode->i_mode, flags);
201 oldflags = btrfs_flags_to_ioctl(ip->flags);
202 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
203 if (!capable(CAP_LINUX_IMMUTABLE)) {
204 ret = -EPERM;
205 goto out_unlock;
206 }
207 }
208
209 ret = mnt_want_write(file->f_path.mnt);
210 if (ret)
211 goto out_unlock;
212
213 if (flags & FS_SYNC_FL)
214 ip->flags |= BTRFS_INODE_SYNC;
215 else
216 ip->flags &= ~BTRFS_INODE_SYNC;
217 if (flags & FS_IMMUTABLE_FL)
218 ip->flags |= BTRFS_INODE_IMMUTABLE;
219 else
220 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
221 if (flags & FS_APPEND_FL)
222 ip->flags |= BTRFS_INODE_APPEND;
223 else
224 ip->flags &= ~BTRFS_INODE_APPEND;
225 if (flags & FS_NODUMP_FL)
226 ip->flags |= BTRFS_INODE_NODUMP;
227 else
228 ip->flags &= ~BTRFS_INODE_NODUMP;
229 if (flags & FS_NOATIME_FL)
230 ip->flags |= BTRFS_INODE_NOATIME;
231 else
232 ip->flags &= ~BTRFS_INODE_NOATIME;
233 if (flags & FS_DIRSYNC_FL)
234 ip->flags |= BTRFS_INODE_DIRSYNC;
235 else
236 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000237 if (flags & FS_NOCOW_FL)
238 ip->flags |= BTRFS_INODE_NODATACOW;
239 else
240 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200241
Liu Bo75e7cb72011-03-22 10:12:20 +0000242 /*
243 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
244 * flag may be changed automatically if compression code won't make
245 * things smaller.
246 */
247 if (flags & FS_NOCOMP_FL) {
248 ip->flags &= ~BTRFS_INODE_COMPRESS;
249 ip->flags |= BTRFS_INODE_NOCOMPRESS;
250 } else if (flags & FS_COMPR_FL) {
251 ip->flags |= BTRFS_INODE_COMPRESS;
252 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000253 } else {
254 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000255 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200256
Li Zefan4da6f1a2011-12-29 13:39:50 +0800257 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800258 if (IS_ERR(trans)) {
259 ret = PTR_ERR(trans);
260 goto out_drop;
261 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200262
Li Zefan306424c2011-12-14 20:12:02 -0500263 btrfs_update_iflags(inode);
264 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200265 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200266
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200267 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800268 out_drop:
269 if (ret) {
270 ip->flags = ip_oldflags;
271 inode->i_flags = i_oldflags;
272 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200273
274 mnt_drop_write(file->f_path.mnt);
275 out_unlock:
276 mutex_unlock(&inode->i_mutex);
liubo2d4e6f6a2011-02-24 09:38:16 +0000277 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200278}
279
280static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
281{
282 struct inode *inode = file->f_path.dentry->d_inode;
283
284 return put_user(inode->i_generation, arg);
285}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400286
Li Dongyangf7039b12011-03-24 10:24:28 +0000287static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
288{
289 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
290 struct btrfs_fs_info *fs_info = root->fs_info;
291 struct btrfs_device *device;
292 struct request_queue *q;
293 struct fstrim_range range;
294 u64 minlen = ULLONG_MAX;
295 u64 num_devices = 0;
David Sterba6c417612011-04-13 15:41:04 +0200296 u64 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000297 int ret;
298
299 if (!capable(CAP_SYS_ADMIN))
300 return -EPERM;
301
Xiao Guangrong1f781602011-04-20 10:09:16 +0000302 rcu_read_lock();
303 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
304 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000305 if (!device->bdev)
306 continue;
307 q = bdev_get_queue(device->bdev);
308 if (blk_queue_discard(q)) {
309 num_devices++;
310 minlen = min((u64)q->limits.discard_granularity,
311 minlen);
312 }
313 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000314 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200315
Li Dongyangf7039b12011-03-24 10:24:28 +0000316 if (!num_devices)
317 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000318 if (copy_from_user(&range, arg, sizeof(range)))
319 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200320 if (range.start > total_bytes)
321 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000322
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200323 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000324 range.minlen = max(range.minlen, minlen);
325 ret = btrfs_trim_fs(root, &range);
326 if (ret < 0)
327 return ret;
328
329 if (copy_to_user(arg, &range, sizeof(range)))
330 return -EFAULT;
331
332 return 0;
333}
334
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400335static noinline int create_subvol(struct btrfs_root *root,
336 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400337 char *name, int namelen,
338 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400339{
340 struct btrfs_trans_handle *trans;
341 struct btrfs_key key;
342 struct btrfs_root_item root_item;
343 struct btrfs_inode_item *inode_item;
344 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400345 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400346 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000347 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400348 int ret;
349 int err;
350 u64 objectid;
351 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500352 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353
Li Zefan581bb052011-04-20 10:06:11 +0800354 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400355 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400356 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000357
358 dir = parent->d_inode;
359
Josef Bacik9ed74f22009-09-11 16:12:44 -0400360 /*
361 * 1 - inode item
362 * 2 - refs
363 * 1 - root item
364 * 2 - dir items
365 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400366 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400367 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400368 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400369
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400370 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200371 0, objectid, NULL, 0, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400372 if (IS_ERR(leaf)) {
373 ret = PTR_ERR(leaf);
374 goto fail;
375 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400376
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400377 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400378 btrfs_set_header_bytenr(leaf, leaf->start);
379 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400380 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400381 btrfs_set_header_owner(leaf, objectid);
382
383 write_extent_buffer(leaf, root->fs_info->fsid,
384 (unsigned long)btrfs_header_fsid(leaf),
385 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400386 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
387 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
388 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400389 btrfs_mark_buffer_dirty(leaf);
390
391 inode_item = &root_item.inode;
392 memset(inode_item, 0, sizeof(*inode_item));
393 inode_item->generation = cpu_to_le64(1);
394 inode_item->size = cpu_to_le64(3);
395 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400396 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
398
Li Zefan08fe4db2011-03-28 02:01:25 +0000399 root_item.flags = 0;
400 root_item.byte_limit = 0;
401 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
402
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400403 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400404 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400405 btrfs_set_root_level(&root_item, 0);
406 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000407 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400408 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409
410 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
411 root_item.drop_level = 0;
412
Chris Mason925baed2008-06-25 16:01:30 -0400413 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414 free_extent_buffer(leaf);
415 leaf = NULL;
416
417 btrfs_set_root_dirid(&root_item, new_dirid);
418
419 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400420 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
422 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
423 &root_item);
424 if (ret)
425 goto fail;
426
Yan, Zheng76dda932009-09-21 16:00:26 -0400427 key.offset = (u64)-1;
428 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
429 BUG_ON(IS_ERR(new_root));
430
431 btrfs_record_root_in_trans(trans, new_root);
432
Josef Bacikd82a6f12011-05-11 15:26:06 -0400433 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400434 /*
435 * insert the directory item
436 */
Chris Mason3de45862008-11-17 21:02:50 -0500437 ret = btrfs_set_inode_index(dir, &index);
438 BUG_ON(ret);
439
440 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800441 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500442 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400443 if (ret)
444 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500445
Yan Zheng52c26172009-01-05 15:43:43 -0500446 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
447 ret = btrfs_update_inode(trans, root, dir);
448 BUG_ON(ret);
449
Chris Mason0660b5a2008-11-17 20:37:39 -0500450 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400451 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800452 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400453
Chris Mason0660b5a2008-11-17 20:37:39 -0500454 BUG_ON(ret);
455
Yan, Zheng76dda932009-09-21 16:00:26 -0400456 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400458 if (async_transid) {
459 *async_transid = trans->transid;
460 err = btrfs_commit_transaction_async(trans, root, 1);
461 } else {
462 err = btrfs_commit_transaction(trans, root);
463 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464 if (err && !ret)
465 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400466 return ret;
467}
468
Sage Weil72fd0322010-10-29 15:41:32 -0400469static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800470 char *name, int namelen, u64 *async_transid,
471 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400472{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000473 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400474 struct btrfs_pending_snapshot *pending_snapshot;
475 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000476 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400477
478 if (!root->ref_cows)
479 return -EINVAL;
480
Yan, Zhenga22285a2010-05-16 10:48:46 -0400481 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
482 if (!pending_snapshot)
483 return -ENOMEM;
484
485 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
486 pending_snapshot->dentry = dentry;
487 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800488 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400489
490 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
491 if (IS_ERR(trans)) {
492 ret = PTR_ERR(trans);
493 goto fail;
494 }
495
496 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
497 BUG_ON(ret);
498
Josef Bacik83515832011-06-14 15:16:14 -0400499 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400500 list_add(&pending_snapshot->list,
501 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400502 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400503 if (async_transid) {
504 *async_transid = trans->transid;
505 ret = btrfs_commit_transaction_async(trans,
506 root->fs_info->extent_root, 1);
507 } else {
508 ret = btrfs_commit_transaction(trans,
509 root->fs_info->extent_root);
510 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400511 BUG_ON(ret);
512
513 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400514 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000515 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400516
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500517 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
518 if (ret)
519 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400520
Al Viro2fbe8c82011-07-16 21:38:06 -0400521 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000522 if (IS_ERR(inode)) {
523 ret = PTR_ERR(inode);
524 goto fail;
525 }
526 BUG_ON(!inode);
527 d_instantiate(dentry, inode);
528 ret = 0;
529fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400530 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400531 return ret;
532}
533
Sage Weil4260f7c2010-10-29 15:46:43 -0400534/* copy of check_sticky in fs/namei.c()
535* It's inline, so penalty for filesystems that don't use sticky bit is
536* minimal.
537*/
538static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
539{
540 uid_t fsuid = current_fsuid();
541
542 if (!(dir->i_mode & S_ISVTX))
543 return 0;
544 if (inode->i_uid == fsuid)
545 return 0;
546 if (dir->i_uid == fsuid)
547 return 0;
548 return !capable(CAP_FOWNER);
549}
550
551/* copy of may_delete in fs/namei.c()
552 * Check whether we can remove a link victim from directory dir, check
553 * whether the type of victim is right.
554 * 1. We can't do it if dir is read-only (done in permission())
555 * 2. We should have write and exec permissions on dir
556 * 3. We can't remove anything from append-only dir
557 * 4. We can't do anything with immutable dir (done in permission())
558 * 5. If the sticky bit on dir is set we should either
559 * a. be owner of dir, or
560 * b. be owner of victim, or
561 * c. have CAP_FOWNER capability
562 * 6. If the victim is append-only or immutable we can't do antyhing with
563 * links pointing to it.
564 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
565 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
566 * 9. We can't remove a root or mountpoint.
567 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
568 * nfs_async_unlink().
569 */
570
571static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
572{
573 int error;
574
575 if (!victim->d_inode)
576 return -ENOENT;
577
578 BUG_ON(victim->d_parent->d_inode != dir);
579 audit_inode_child(victim, dir);
580
581 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
582 if (error)
583 return error;
584 if (IS_APPEND(dir))
585 return -EPERM;
586 if (btrfs_check_sticky(dir, victim->d_inode)||
587 IS_APPEND(victim->d_inode)||
588 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
589 return -EPERM;
590 if (isdir) {
591 if (!S_ISDIR(victim->d_inode->i_mode))
592 return -ENOTDIR;
593 if (IS_ROOT(victim))
594 return -EBUSY;
595 } else if (S_ISDIR(victim->d_inode->i_mode))
596 return -EISDIR;
597 if (IS_DEADDIR(dir))
598 return -ENOENT;
599 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
600 return -EBUSY;
601 return 0;
602}
603
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400604/* copy of may_create in fs/namei.c() */
605static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
606{
607 if (child->d_inode)
608 return -EEXIST;
609 if (IS_DEADDIR(dir))
610 return -ENOENT;
611 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
612}
613
614/*
615 * Create a new subvolume below @parent. This is largely modeled after
616 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
617 * inside this filesystem so it's quite a bit simpler.
618 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400619static noinline int btrfs_mksubvol(struct path *parent,
620 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400621 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800622 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400623{
Yan, Zheng76dda932009-09-21 16:00:26 -0400624 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400625 struct dentry *dentry;
626 int error;
627
Yan, Zheng76dda932009-09-21 16:00:26 -0400628 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400629
630 dentry = lookup_one_len(name, parent->dentry, namelen);
631 error = PTR_ERR(dentry);
632 if (IS_ERR(dentry))
633 goto out_unlock;
634
635 error = -EEXIST;
636 if (dentry->d_inode)
637 goto out_dput;
638
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400639 error = mnt_want_write(parent->mnt);
640 if (error)
641 goto out_dput;
642
Yan, Zheng76dda932009-09-21 16:00:26 -0400643 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400644 if (error)
645 goto out_drop_write;
646
Yan, Zheng76dda932009-09-21 16:00:26 -0400647 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
648
649 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
650 goto out_up_read;
651
Chris Mason3de45862008-11-17 21:02:50 -0500652 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400653 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800654 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500655 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400656 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400657 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500658 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400659 if (!error)
660 fsnotify_mkdir(dir, dentry);
661out_up_read:
662 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400663out_drop_write:
664 mnt_drop_write(parent->mnt);
665out_dput:
666 dput(dentry);
667out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400668 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400669 return error;
670}
671
Chris Mason4cb53002011-05-24 15:35:30 -0400672/*
673 * When we're defragging a range, we don't want to kick it off again
674 * if it is really just waiting for delalloc to send it down.
675 * If we find a nice big extent or delalloc range for the bytes in the
676 * file you want to defrag, we return 0 to let you know to skip this
677 * part of the file
678 */
679static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
680{
681 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
682 struct extent_map *em = NULL;
683 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
684 u64 end;
685
686 read_lock(&em_tree->lock);
687 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
688 read_unlock(&em_tree->lock);
689
690 if (em) {
691 end = extent_map_end(em);
692 free_extent_map(em);
693 if (end - offset > thresh)
694 return 0;
695 }
696 /* if we already have a nice delalloc here, just stop */
697 thresh /= 2;
698 end = count_range_bits(io_tree, &offset, offset + thresh,
699 thresh, EXTENT_DELALLOC, 1);
700 if (end >= thresh)
701 return 0;
702 return 1;
703}
704
705/*
706 * helper function to walk through a file and find extents
707 * newer than a specific transid, and smaller than thresh.
708 *
709 * This is used by the defragging code to find new and small
710 * extents
711 */
712static int find_new_extents(struct btrfs_root *root,
713 struct inode *inode, u64 newer_than,
714 u64 *off, int thresh)
715{
716 struct btrfs_path *path;
717 struct btrfs_key min_key;
718 struct btrfs_key max_key;
719 struct extent_buffer *leaf;
720 struct btrfs_file_extent_item *extent;
721 int type;
722 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000723 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400724
725 path = btrfs_alloc_path();
726 if (!path)
727 return -ENOMEM;
728
David Sterbaa4689d22011-05-31 17:08:14 +0000729 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400730 min_key.type = BTRFS_EXTENT_DATA_KEY;
731 min_key.offset = *off;
732
David Sterbaa4689d22011-05-31 17:08:14 +0000733 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400734 max_key.type = (u8)-1;
735 max_key.offset = (u64)-1;
736
737 path->keep_locks = 1;
738
739 while(1) {
740 ret = btrfs_search_forward(root, &min_key, &max_key,
741 path, 0, newer_than);
742 if (ret != 0)
743 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000744 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400745 goto none;
746 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
747 goto none;
748
749 leaf = path->nodes[0];
750 extent = btrfs_item_ptr(leaf, path->slots[0],
751 struct btrfs_file_extent_item);
752
753 type = btrfs_file_extent_type(leaf, extent);
754 if (type == BTRFS_FILE_EXTENT_REG &&
755 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
756 check_defrag_in_cache(inode, min_key.offset, thresh)) {
757 *off = min_key.offset;
758 btrfs_free_path(path);
759 return 0;
760 }
761
762 if (min_key.offset == (u64)-1)
763 goto none;
764
765 min_key.offset++;
766 btrfs_release_path(path);
767 }
768none:
769 btrfs_free_path(path);
770 return -ENOENT;
771}
772
Chris Mason940100a2010-03-10 10:52:59 -0500773static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500774 int thresh, u64 *last_len, u64 *skip,
775 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500776{
777 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
778 struct extent_map *em = NULL;
779 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
780 int ret = 1;
781
782 /*
Li Zefan008873e2011-09-02 15:57:07 +0800783 * make sure that once we start defragging an extent, we keep on
Chris Mason940100a2010-03-10 10:52:59 -0500784 * defragging it
785 */
786 if (start < *defrag_end)
787 return 1;
788
789 *skip = 0;
790
791 /*
792 * hopefully we have this extent in the tree already, try without
793 * the full extent lock
794 */
795 read_lock(&em_tree->lock);
796 em = lookup_extent_mapping(em_tree, start, len);
797 read_unlock(&em_tree->lock);
798
799 if (!em) {
800 /* get the big lock and read metadata off disk */
801 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
802 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
803 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
804
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000805 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500806 return 0;
807 }
808
809 /* this will cover holes, and inline extents */
810 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
811 ret = 0;
812
813 /*
814 * we hit a real extent, if it is big don't bother defragging it again
815 */
Chris Mason1e701a32010-03-11 09:42:04 -0500816 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500817 ret = 0;
818
819 /*
820 * last_len ends up being a counter of how many bytes we've defragged.
821 * every time we choose not to defrag an extent, we reset *last_len
822 * so that the next tiny extent will force a defrag.
823 *
824 * The end result of this is that tiny extents before a single big
825 * extent will force at least part of that big extent to be defragged.
826 */
827 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500828 *defrag_end = extent_map_end(em);
829 } else {
830 *last_len = 0;
831 *skip = extent_map_end(em);
832 *defrag_end = 0;
833 }
834
835 free_extent_map(em);
836 return ret;
837}
838
Chris Mason4cb53002011-05-24 15:35:30 -0400839/*
840 * it doesn't do much good to defrag one or two pages
841 * at a time. This pulls in a nice chunk of pages
842 * to COW and defrag.
843 *
844 * It also makes sure the delalloc code has enough
845 * dirty data to avoid making new small extents as part
846 * of the defrag
847 *
848 * It's a good idea to start RA on this range
849 * before calling this.
850 */
851static int cluster_pages_for_defrag(struct inode *inode,
852 struct page **pages,
853 unsigned long start_index,
854 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400855{
Chris Mason4cb53002011-05-24 15:35:30 -0400856 unsigned long file_end;
857 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 u64 page_start;
859 u64 page_end;
Chris Mason4cb53002011-05-24 15:35:30 -0400860 int ret;
861 int i;
862 int i_done;
863 struct btrfs_ordered_extent *ordered;
864 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800865 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400866 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400867
868 if (isize == 0)
869 return 0;
870 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
871
872 ret = btrfs_delalloc_reserve_space(inode,
873 num_pages << PAGE_CACHE_SHIFT);
874 if (ret)
875 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400876 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800877 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400878
879 /* step one, lock all the pages */
880 for (i = 0; i < num_pages; i++) {
881 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800882again:
Josef Bacika94733d2011-07-11 10:47:06 -0400883 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800884 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400885 if (!page)
886 break;
887
Miao Xie600a45e2012-02-16 15:01:24 +0800888 page_start = page_offset(page);
889 page_end = page_start + PAGE_CACHE_SIZE - 1;
890 while (1) {
891 lock_extent(tree, page_start, page_end, GFP_NOFS);
892 ordered = btrfs_lookup_ordered_extent(inode,
893 page_start);
894 unlock_extent(tree, page_start, page_end, GFP_NOFS);
895 if (!ordered)
896 break;
897
898 unlock_page(page);
899 btrfs_start_ordered_extent(inode, ordered, 1);
900 btrfs_put_ordered_extent(ordered);
901 lock_page(page);
902 }
903
Chris Mason4cb53002011-05-24 15:35:30 -0400904 if (!PageUptodate(page)) {
905 btrfs_readpage(NULL, page);
906 lock_page(page);
907 if (!PageUptodate(page)) {
908 unlock_page(page);
909 page_cache_release(page);
910 ret = -EIO;
911 break;
912 }
913 }
Miao Xie600a45e2012-02-16 15:01:24 +0800914
Chris Mason4cb53002011-05-24 15:35:30 -0400915 isize = i_size_read(inode);
916 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Miao Xie600a45e2012-02-16 15:01:24 +0800917 if (!isize || page->index > file_end) {
Chris Mason4cb53002011-05-24 15:35:30 -0400918 /* whoops, we blew past eof, skip this page */
919 unlock_page(page);
920 page_cache_release(page);
921 break;
922 }
Miao Xie600a45e2012-02-16 15:01:24 +0800923
924 if (page->mapping != inode->i_mapping) {
925 unlock_page(page);
926 page_cache_release(page);
927 goto again;
928 }
929
Chris Mason4cb53002011-05-24 15:35:30 -0400930 pages[i] = page;
931 i_done++;
932 }
933 if (!i_done || ret)
934 goto out;
935
936 if (!(inode->i_sb->s_flags & MS_ACTIVE))
937 goto out;
938
939 /*
940 * so now we have a nice long stream of locked
941 * and up to date pages, lets wait on them
942 */
943 for (i = 0; i < i_done; i++)
944 wait_on_page_writeback(pages[i]);
945
946 page_start = page_offset(pages[0]);
947 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
948
949 lock_extent_bits(&BTRFS_I(inode)->io_tree,
950 page_start, page_end - 1, 0, &cached_state,
951 GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -0400952 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
953 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
954 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
955 GFP_NOFS);
956
957 if (i_done != num_pages) {
Josef Bacik9e0baf62011-07-15 15:16:44 +0000958 spin_lock(&BTRFS_I(inode)->lock);
959 BTRFS_I(inode)->outstanding_extents++;
960 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -0400961 btrfs_delalloc_release_space(inode,
962 (num_pages - i_done) << PAGE_CACHE_SHIFT);
963 }
964
965
966 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
967 &cached_state);
968
969 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
970 page_start, page_end - 1, &cached_state,
971 GFP_NOFS);
972
973 for (i = 0; i < i_done; i++) {
974 clear_page_dirty_for_io(pages[i]);
975 ClearPageChecked(pages[i]);
976 set_page_extent_mapped(pages[i]);
977 set_page_dirty(pages[i]);
978 unlock_page(pages[i]);
979 page_cache_release(pages[i]);
980 }
981 return i_done;
982out:
983 for (i = 0; i < i_done; i++) {
984 unlock_page(pages[i]);
985 page_cache_release(pages[i]);
986 }
987 btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
988 return ret;
989
990}
991
992int btrfs_defrag_file(struct inode *inode, struct file *file,
993 struct btrfs_ioctl_defrag_range_args *range,
994 u64 newer_than, unsigned long max_to_defrag)
995{
996 struct btrfs_root *root = BTRFS_I(inode)->root;
997 struct btrfs_super_block *disk_super;
998 struct file_ra_state *ra = NULL;
999 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001000 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001001 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -05001002 u64 last_len = 0;
1003 u64 skip = 0;
1004 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001005 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001006 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001007 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001008 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001009 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001010 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001011 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001012 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1013 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001014 u64 new_align = ~((u64)128 * 1024 - 1);
1015 struct page **pages = NULL;
1016
1017 if (extent_thresh == 0)
1018 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001019
1020 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1021 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1022 return -EINVAL;
1023 if (range->compress_type)
1024 compress_type = range->compress_type;
1025 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001026
Li Zefan151a31b2011-09-02 15:56:39 +08001027 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001028 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001029
Chris Mason4cb53002011-05-24 15:35:30 -04001030 /*
1031 * if we were not given a file, allocate a readahead
1032 * context
1033 */
1034 if (!file) {
1035 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1036 if (!ra)
1037 return -ENOMEM;
1038 file_ra_state_init(ra, inode->i_mapping);
1039 } else {
1040 ra = &file->f_ra;
1041 }
1042
Li Zefan008873e2011-09-02 15:57:07 +08001043 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001044 GFP_NOFS);
1045 if (!pages) {
1046 ret = -ENOMEM;
1047 goto out_ra;
1048 }
1049
1050 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001051 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001052 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001053 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1054 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001055 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001056 }
1057
Chris Mason4cb53002011-05-24 15:35:30 -04001058 if (newer_than) {
1059 ret = find_new_extents(root, inode, newer_than,
1060 &newer_off, 64 * 1024);
1061 if (!ret) {
1062 range->start = newer_off;
1063 /*
1064 * we always align our defrag to help keep
1065 * the extents in the file evenly spaced
1066 */
1067 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001068 } else
1069 goto out_ra;
1070 } else {
1071 i = range->start >> PAGE_CACHE_SHIFT;
1072 }
1073 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001074 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001075
Li Zefan2a0f7f52011-10-10 15:43:34 -04001076 /*
1077 * make writeback starts from i, so the defrag range can be
1078 * written sequentially.
1079 */
1080 if (i < inode->i_mapping->writeback_index)
1081 inode->i_mapping->writeback_index = i;
1082
Chris Masonf7f43cc2011-10-11 11:41:40 -04001083 while (i <= last_index && defrag_count < max_to_defrag &&
1084 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1085 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001086 /*
1087 * make sure we stop running if someone unmounts
1088 * the FS
1089 */
1090 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1091 break;
1092
1093 if (!newer_than &&
1094 !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -05001095 PAGE_CACHE_SIZE,
Chris Mason4cb53002011-05-24 15:35:30 -04001096 extent_thresh,
Chris Mason1e701a32010-03-11 09:42:04 -05001097 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -05001098 &defrag_end)) {
1099 unsigned long next;
1100 /*
1101 * the should_defrag function tells us how much to skip
1102 * bump our counter by the suggested amount
1103 */
1104 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1105 i = max(i + 1, next);
1106 continue;
1107 }
Li Zefan008873e2011-09-02 15:57:07 +08001108
1109 if (!newer_than) {
1110 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1111 PAGE_CACHE_SHIFT) - i;
1112 cluster = min(cluster, max_cluster);
1113 } else {
1114 cluster = max_cluster;
1115 }
1116
Chris Mason1e701a32010-03-11 09:42:04 -05001117 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001118 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001119
Li Zefan008873e2011-09-02 15:57:07 +08001120 if (i + cluster > ra_index) {
1121 ra_index = max(i, ra_index);
1122 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1123 cluster);
1124 ra_index += max_cluster;
1125 }
Chris Mason940100a2010-03-10 10:52:59 -05001126
Li Zefan008873e2011-09-02 15:57:07 +08001127 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Chris Mason4cb53002011-05-24 15:35:30 -04001128 if (ret < 0)
1129 goto out_ra;
Chris Mason940100a2010-03-10 10:52:59 -05001130
Chris Mason4cb53002011-05-24 15:35:30 -04001131 defrag_count += ret;
1132 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Chris Mason4cb53002011-05-24 15:35:30 -04001133
1134 if (newer_than) {
1135 if (newer_off == (u64)-1)
1136 break;
1137
1138 newer_off = max(newer_off + 1,
1139 (u64)i << PAGE_CACHE_SHIFT);
1140
1141 ret = find_new_extents(root, inode,
1142 newer_than, &newer_off,
1143 64 * 1024);
1144 if (!ret) {
1145 range->start = newer_off;
1146 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001147 } else {
1148 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001149 }
Chris Mason4cb53002011-05-24 15:35:30 -04001150 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001151 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001152 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001153 last_len += ret << PAGE_CACHE_SHIFT;
1154 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001155 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001156 last_len = 0;
1157 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001158 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001159 }
1160
Chris Mason1e701a32010-03-11 09:42:04 -05001161 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1162 filemap_flush(inode->i_mapping);
1163
1164 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1165 /* the filemap_flush will queue IO into the worker threads, but
1166 * we have to make sure the IO is actually started and that
1167 * ordered extents get created before we return
1168 */
1169 atomic_inc(&root->fs_info->async_submit_draining);
1170 while (atomic_read(&root->fs_info->nr_async_submits) ||
1171 atomic_read(&root->fs_info->async_delalloc_pages)) {
1172 wait_event(root->fs_info->async_submit_wait,
1173 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1174 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1175 }
1176 atomic_dec(&root->fs_info->async_submit_draining);
1177
1178 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001179 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001180 mutex_unlock(&inode->i_mutex);
1181 }
1182
David Sterba6c417612011-04-13 15:41:04 +02001183 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001184 features = btrfs_super_incompat_flags(disk_super);
1185 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1186 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1187 btrfs_set_super_incompat_flags(disk_super, features);
1188 }
1189
Diego Calleja60ccf822011-09-01 16:33:57 +02001190 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001191
Chris Mason4cb53002011-05-24 15:35:30 -04001192out_ra:
1193 if (!file)
1194 kfree(ra);
1195 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001196 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001197}
1198
Yan, Zheng76dda932009-09-21 16:00:26 -04001199static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1200 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001201{
1202 u64 new_size;
1203 u64 old_size;
1204 u64 devid = 1;
1205 struct btrfs_ioctl_vol_args *vol_args;
1206 struct btrfs_trans_handle *trans;
1207 struct btrfs_device *device = NULL;
1208 char *sizestr;
1209 char *devstr = NULL;
1210 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001211 int mod = 0;
1212
Yan Zhengc146afa2008-11-12 14:34:12 -05001213 if (root->fs_info->sb->s_flags & MS_RDONLY)
1214 return -EROFS;
1215
Chris Masone441d542009-01-05 16:57:23 -05001216 if (!capable(CAP_SYS_ADMIN))
1217 return -EPERM;
1218
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001219 mutex_lock(&root->fs_info->volume_mutex);
1220 if (root->fs_info->balance_ctl) {
1221 printk(KERN_INFO "btrfs: balance in progress\n");
1222 ret = -EINVAL;
1223 goto out;
1224 }
1225
Li Zefandae7b662009-04-08 15:06:54 +08001226 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001227 if (IS_ERR(vol_args)) {
1228 ret = PTR_ERR(vol_args);
1229 goto out;
1230 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001231
1232 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001233
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001234 sizestr = vol_args->name;
1235 devstr = strchr(sizestr, ':');
1236 if (devstr) {
1237 char *end;
1238 sizestr = devstr + 1;
1239 *devstr = '\0';
1240 devstr = vol_args->name;
1241 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001242 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001243 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001244 }
Yan Zheng2b820322008-11-17 21:11:30 -05001245 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001246 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001247 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001248 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001249 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001250 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001251 }
1252 if (!strcmp(sizestr, "max"))
1253 new_size = device->bdev->bd_inode->i_size;
1254 else {
1255 if (sizestr[0] == '-') {
1256 mod = -1;
1257 sizestr++;
1258 } else if (sizestr[0] == '+') {
1259 mod = 1;
1260 sizestr++;
1261 }
Akinobu Mita91748462010-02-28 10:59:11 +00001262 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001263 if (new_size == 0) {
1264 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001265 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001266 }
1267 }
1268
1269 old_size = device->total_bytes;
1270
1271 if (mod < 0) {
1272 if (new_size > old_size) {
1273 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001274 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001275 }
1276 new_size = old_size - new_size;
1277 } else if (mod > 0) {
1278 new_size = old_size + new_size;
1279 }
1280
1281 if (new_size < 256 * 1024 * 1024) {
1282 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001283 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001284 }
1285 if (new_size > device->bdev->bd_inode->i_size) {
1286 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001287 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001288 }
1289
1290 do_div(new_size, root->sectorsize);
1291 new_size *= root->sectorsize;
1292
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001293 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001294 device->name, (unsigned long long)new_size);
1295
1296 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001297 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001298 if (IS_ERR(trans)) {
1299 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001300 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001301 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001302 ret = btrfs_grow_device(trans, device, new_size);
1303 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001304 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001305 ret = btrfs_shrink_device(device, new_size);
1306 }
1307
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001308out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001309 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001310out:
1311 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001312 return ret;
1313}
1314
Sage Weil72fd0322010-10-29 15:41:32 -04001315static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1316 char *name,
1317 unsigned long fd,
1318 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001319 u64 *transid,
1320 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001321{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001322 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001323 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001324 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001325 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001326
Yan Zhengc146afa2008-11-12 14:34:12 -05001327 if (root->fs_info->sb->s_flags & MS_RDONLY)
1328 return -EROFS;
1329
Sage Weil72fd0322010-10-29 15:41:32 -04001330 namelen = strlen(name);
1331 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001332 ret = -EINVAL;
1333 goto out;
1334 }
1335
Chris Mason3de45862008-11-17 21:02:50 -05001336 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001337 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001338 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001339 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001340 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001341 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001342 if (!src_file) {
1343 ret = -EINVAL;
1344 goto out;
1345 }
1346
1347 src_inode = src_file->f_path.dentry->d_inode;
1348 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001349 printk(KERN_INFO "btrfs: Snapshot src from "
1350 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001351 ret = -EINVAL;
1352 fput(src_file);
1353 goto out;
1354 }
Sage Weil72fd0322010-10-29 15:41:32 -04001355 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1356 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001357 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001358 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001359 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001360out:
Sage Weil72fd0322010-10-29 15:41:32 -04001361 return ret;
1362}
1363
1364static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001365 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001366{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001367 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001368 int ret;
1369
Li Zefanfa0d2b92010-12-20 15:53:28 +08001370 vol_args = memdup_user(arg, sizeof(*vol_args));
1371 if (IS_ERR(vol_args))
1372 return PTR_ERR(vol_args);
1373 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001374
Li Zefanfa0d2b92010-12-20 15:53:28 +08001375 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001376 vol_args->fd, subvol,
1377 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001378
Li Zefanfa0d2b92010-12-20 15:53:28 +08001379 kfree(vol_args);
1380 return ret;
1381}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001382
Li Zefanfa0d2b92010-12-20 15:53:28 +08001383static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1384 void __user *arg, int subvol)
1385{
1386 struct btrfs_ioctl_vol_args_v2 *vol_args;
1387 int ret;
1388 u64 transid = 0;
1389 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001390 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001391
Li Zefanfa0d2b92010-12-20 15:53:28 +08001392 vol_args = memdup_user(arg, sizeof(*vol_args));
1393 if (IS_ERR(vol_args))
1394 return PTR_ERR(vol_args);
1395 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001396
Li Zefanb83cc962010-12-20 16:04:08 +08001397 if (vol_args->flags &
1398 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1399 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001400 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001401 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001402
1403 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1404 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001405 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1406 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001407
1408 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001409 vol_args->fd, subvol,
1410 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001411
1412 if (ret == 0 && ptr &&
1413 copy_to_user(arg +
1414 offsetof(struct btrfs_ioctl_vol_args_v2,
1415 transid), ptr, sizeof(*ptr)))
1416 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001417out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001418 kfree(vol_args);
1419 return ret;
1420}
1421
Li Zefan0caa1022010-12-20 16:30:25 +08001422static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1423 void __user *arg)
1424{
1425 struct inode *inode = fdentry(file)->d_inode;
1426 struct btrfs_root *root = BTRFS_I(inode)->root;
1427 int ret = 0;
1428 u64 flags = 0;
1429
Li Zefan33345d012011-04-20 10:31:50 +08001430 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001431 return -EINVAL;
1432
1433 down_read(&root->fs_info->subvol_sem);
1434 if (btrfs_root_readonly(root))
1435 flags |= BTRFS_SUBVOL_RDONLY;
1436 up_read(&root->fs_info->subvol_sem);
1437
1438 if (copy_to_user(arg, &flags, sizeof(flags)))
1439 ret = -EFAULT;
1440
1441 return ret;
1442}
1443
1444static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1445 void __user *arg)
1446{
1447 struct inode *inode = fdentry(file)->d_inode;
1448 struct btrfs_root *root = BTRFS_I(inode)->root;
1449 struct btrfs_trans_handle *trans;
1450 u64 root_flags;
1451 u64 flags;
1452 int ret = 0;
1453
1454 if (root->fs_info->sb->s_flags & MS_RDONLY)
1455 return -EROFS;
1456
Li Zefan33345d012011-04-20 10:31:50 +08001457 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001458 return -EINVAL;
1459
1460 if (copy_from_user(&flags, arg, sizeof(flags)))
1461 return -EFAULT;
1462
Li Zefanb4dc2b82011-02-16 06:06:34 +00001463 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001464 return -EINVAL;
1465
1466 if (flags & ~BTRFS_SUBVOL_RDONLY)
1467 return -EOPNOTSUPP;
1468
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001469 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001470 return -EACCES;
1471
Li Zefan0caa1022010-12-20 16:30:25 +08001472 down_write(&root->fs_info->subvol_sem);
1473
1474 /* nothing to do */
1475 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1476 goto out;
1477
1478 root_flags = btrfs_root_flags(&root->root_item);
1479 if (flags & BTRFS_SUBVOL_RDONLY)
1480 btrfs_set_root_flags(&root->root_item,
1481 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1482 else
1483 btrfs_set_root_flags(&root->root_item,
1484 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1485
1486 trans = btrfs_start_transaction(root, 1);
1487 if (IS_ERR(trans)) {
1488 ret = PTR_ERR(trans);
1489 goto out_reset;
1490 }
1491
Li Zefanb4dc2b82011-02-16 06:06:34 +00001492 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001493 &root->root_key, &root->root_item);
1494
1495 btrfs_commit_transaction(trans, root);
1496out_reset:
1497 if (ret)
1498 btrfs_set_root_flags(&root->root_item, root_flags);
1499out:
1500 up_write(&root->fs_info->subvol_sem);
1501 return ret;
1502}
1503
Yan, Zheng76dda932009-09-21 16:00:26 -04001504/*
1505 * helper to check if the subvolume references other subvolumes
1506 */
1507static noinline int may_destroy_subvol(struct btrfs_root *root)
1508{
1509 struct btrfs_path *path;
1510 struct btrfs_key key;
1511 int ret;
1512
1513 path = btrfs_alloc_path();
1514 if (!path)
1515 return -ENOMEM;
1516
1517 key.objectid = root->root_key.objectid;
1518 key.type = BTRFS_ROOT_REF_KEY;
1519 key.offset = (u64)-1;
1520
1521 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1522 &key, path, 0, 0);
1523 if (ret < 0)
1524 goto out;
1525 BUG_ON(ret == 0);
1526
1527 ret = 0;
1528 if (path->slots[0] > 0) {
1529 path->slots[0]--;
1530 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1531 if (key.objectid == root->root_key.objectid &&
1532 key.type == BTRFS_ROOT_REF_KEY)
1533 ret = -ENOTEMPTY;
1534 }
1535out:
1536 btrfs_free_path(path);
1537 return ret;
1538}
1539
Chris Masonac8e9812010-02-28 15:39:26 -05001540static noinline int key_in_sk(struct btrfs_key *key,
1541 struct btrfs_ioctl_search_key *sk)
1542{
Chris Masonabc6e132010-03-18 12:10:08 -04001543 struct btrfs_key test;
1544 int ret;
1545
1546 test.objectid = sk->min_objectid;
1547 test.type = sk->min_type;
1548 test.offset = sk->min_offset;
1549
1550 ret = btrfs_comp_cpu_keys(key, &test);
1551 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001552 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001553
1554 test.objectid = sk->max_objectid;
1555 test.type = sk->max_type;
1556 test.offset = sk->max_offset;
1557
1558 ret = btrfs_comp_cpu_keys(key, &test);
1559 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001560 return 0;
1561 return 1;
1562}
1563
1564static noinline int copy_to_sk(struct btrfs_root *root,
1565 struct btrfs_path *path,
1566 struct btrfs_key *key,
1567 struct btrfs_ioctl_search_key *sk,
1568 char *buf,
1569 unsigned long *sk_offset,
1570 int *num_found)
1571{
1572 u64 found_transid;
1573 struct extent_buffer *leaf;
1574 struct btrfs_ioctl_search_header sh;
1575 unsigned long item_off;
1576 unsigned long item_len;
1577 int nritems;
1578 int i;
1579 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001580 int ret = 0;
1581
1582 leaf = path->nodes[0];
1583 slot = path->slots[0];
1584 nritems = btrfs_header_nritems(leaf);
1585
1586 if (btrfs_header_generation(leaf) > sk->max_transid) {
1587 i = nritems;
1588 goto advance_key;
1589 }
1590 found_transid = btrfs_header_generation(leaf);
1591
1592 for (i = slot; i < nritems; i++) {
1593 item_off = btrfs_item_ptr_offset(leaf, i);
1594 item_len = btrfs_item_size_nr(leaf, i);
1595
1596 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1597 item_len = 0;
1598
1599 if (sizeof(sh) + item_len + *sk_offset >
1600 BTRFS_SEARCH_ARGS_BUFSIZE) {
1601 ret = 1;
1602 goto overflow;
1603 }
1604
1605 btrfs_item_key_to_cpu(leaf, key, i);
1606 if (!key_in_sk(key, sk))
1607 continue;
1608
1609 sh.objectid = key->objectid;
1610 sh.offset = key->offset;
1611 sh.type = key->type;
1612 sh.len = item_len;
1613 sh.transid = found_transid;
1614
1615 /* copy search result header */
1616 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1617 *sk_offset += sizeof(sh);
1618
1619 if (item_len) {
1620 char *p = buf + *sk_offset;
1621 /* copy the item */
1622 read_extent_buffer(leaf, p,
1623 item_off, item_len);
1624 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001625 }
Hugo Millse2156862011-05-14 17:43:41 +00001626 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001627
1628 if (*num_found >= sk->nr_items)
1629 break;
1630 }
1631advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001632 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001633 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1634 key->offset++;
1635 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1636 key->offset = 0;
1637 key->type++;
1638 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1639 key->offset = 0;
1640 key->type = 0;
1641 key->objectid++;
1642 } else
1643 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001644overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001645 return ret;
1646}
1647
1648static noinline int search_ioctl(struct inode *inode,
1649 struct btrfs_ioctl_search_args *args)
1650{
1651 struct btrfs_root *root;
1652 struct btrfs_key key;
1653 struct btrfs_key max_key;
1654 struct btrfs_path *path;
1655 struct btrfs_ioctl_search_key *sk = &args->key;
1656 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1657 int ret;
1658 int num_found = 0;
1659 unsigned long sk_offset = 0;
1660
1661 path = btrfs_alloc_path();
1662 if (!path)
1663 return -ENOMEM;
1664
1665 if (sk->tree_id == 0) {
1666 /* search the root of the inode that was passed */
1667 root = BTRFS_I(inode)->root;
1668 } else {
1669 key.objectid = sk->tree_id;
1670 key.type = BTRFS_ROOT_ITEM_KEY;
1671 key.offset = (u64)-1;
1672 root = btrfs_read_fs_root_no_name(info, &key);
1673 if (IS_ERR(root)) {
1674 printk(KERN_ERR "could not find root %llu\n",
1675 sk->tree_id);
1676 btrfs_free_path(path);
1677 return -ENOENT;
1678 }
1679 }
1680
1681 key.objectid = sk->min_objectid;
1682 key.type = sk->min_type;
1683 key.offset = sk->min_offset;
1684
1685 max_key.objectid = sk->max_objectid;
1686 max_key.type = sk->max_type;
1687 max_key.offset = sk->max_offset;
1688
1689 path->keep_locks = 1;
1690
1691 while(1) {
1692 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1693 sk->min_transid);
1694 if (ret != 0) {
1695 if (ret > 0)
1696 ret = 0;
1697 goto err;
1698 }
1699 ret = copy_to_sk(root, path, &key, sk, args->buf,
1700 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001701 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001702 if (ret || num_found >= sk->nr_items)
1703 break;
1704
1705 }
1706 ret = 0;
1707err:
1708 sk->nr_items = num_found;
1709 btrfs_free_path(path);
1710 return ret;
1711}
1712
1713static noinline int btrfs_ioctl_tree_search(struct file *file,
1714 void __user *argp)
1715{
1716 struct btrfs_ioctl_search_args *args;
1717 struct inode *inode;
1718 int ret;
1719
1720 if (!capable(CAP_SYS_ADMIN))
1721 return -EPERM;
1722
Julia Lawall2354d082010-10-29 15:14:18 -04001723 args = memdup_user(argp, sizeof(*args));
1724 if (IS_ERR(args))
1725 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001726
Chris Masonac8e9812010-02-28 15:39:26 -05001727 inode = fdentry(file)->d_inode;
1728 ret = search_ioctl(inode, args);
1729 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1730 ret = -EFAULT;
1731 kfree(args);
1732 return ret;
1733}
1734
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001735/*
Chris Masonac8e9812010-02-28 15:39:26 -05001736 * Search INODE_REFs to identify path name of 'dirid' directory
1737 * in a 'tree_id' tree. and sets path name to 'name'.
1738 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001739static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1740 u64 tree_id, u64 dirid, char *name)
1741{
1742 struct btrfs_root *root;
1743 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001744 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001745 int ret = -1;
1746 int slot;
1747 int len;
1748 int total_len = 0;
1749 struct btrfs_inode_ref *iref;
1750 struct extent_buffer *l;
1751 struct btrfs_path *path;
1752
1753 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1754 name[0]='\0';
1755 return 0;
1756 }
1757
1758 path = btrfs_alloc_path();
1759 if (!path)
1760 return -ENOMEM;
1761
Chris Masonac8e9812010-02-28 15:39:26 -05001762 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001763
1764 key.objectid = tree_id;
1765 key.type = BTRFS_ROOT_ITEM_KEY;
1766 key.offset = (u64)-1;
1767 root = btrfs_read_fs_root_no_name(info, &key);
1768 if (IS_ERR(root)) {
1769 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001770 ret = -ENOENT;
1771 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001772 }
1773
1774 key.objectid = dirid;
1775 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001776 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001777
1778 while(1) {
1779 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1780 if (ret < 0)
1781 goto out;
1782
1783 l = path->nodes[0];
1784 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001785 if (ret > 0 && slot > 0)
1786 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001787 btrfs_item_key_to_cpu(l, &key, slot);
1788
1789 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001790 key.type != BTRFS_INODE_REF_KEY)) {
1791 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001792 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001793 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001794
1795 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1796 len = btrfs_inode_ref_name_len(l, iref);
1797 ptr -= len + 1;
1798 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001799 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001800 goto out;
1801
1802 *(ptr + len) = '/';
1803 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1804
1805 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1806 break;
1807
David Sterbab3b4aa72011-04-21 01:20:15 +02001808 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001809 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001810 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001811 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001812 }
Chris Masonac8e9812010-02-28 15:39:26 -05001813 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001814 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001815 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001816 name[total_len]='\0';
1817 ret = 0;
1818out:
1819 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001820 return ret;
1821}
1822
1823static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1824 void __user *argp)
1825{
1826 struct btrfs_ioctl_ino_lookup_args *args;
1827 struct inode *inode;
1828 int ret;
1829
1830 if (!capable(CAP_SYS_ADMIN))
1831 return -EPERM;
1832
Julia Lawall2354d082010-10-29 15:14:18 -04001833 args = memdup_user(argp, sizeof(*args));
1834 if (IS_ERR(args))
1835 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001836
Chris Masonac8e9812010-02-28 15:39:26 -05001837 inode = fdentry(file)->d_inode;
1838
Chris Mason1b53ac42010-03-18 12:17:05 -04001839 if (args->treeid == 0)
1840 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1841
Chris Masonac8e9812010-02-28 15:39:26 -05001842 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1843 args->treeid, args->objectid,
1844 args->name);
1845
1846 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1847 ret = -EFAULT;
1848
1849 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001850 return ret;
1851}
1852
Yan, Zheng76dda932009-09-21 16:00:26 -04001853static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1854 void __user *arg)
1855{
1856 struct dentry *parent = fdentry(file);
1857 struct dentry *dentry;
1858 struct inode *dir = parent->d_inode;
1859 struct inode *inode;
1860 struct btrfs_root *root = BTRFS_I(dir)->root;
1861 struct btrfs_root *dest = NULL;
1862 struct btrfs_ioctl_vol_args *vol_args;
1863 struct btrfs_trans_handle *trans;
1864 int namelen;
1865 int ret;
1866 int err = 0;
1867
Yan, Zheng76dda932009-09-21 16:00:26 -04001868 vol_args = memdup_user(arg, sizeof(*vol_args));
1869 if (IS_ERR(vol_args))
1870 return PTR_ERR(vol_args);
1871
1872 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1873 namelen = strlen(vol_args->name);
1874 if (strchr(vol_args->name, '/') ||
1875 strncmp(vol_args->name, "..", namelen) == 0) {
1876 err = -EINVAL;
1877 goto out;
1878 }
1879
1880 err = mnt_want_write(file->f_path.mnt);
1881 if (err)
1882 goto out;
1883
1884 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1885 dentry = lookup_one_len(vol_args->name, parent, namelen);
1886 if (IS_ERR(dentry)) {
1887 err = PTR_ERR(dentry);
1888 goto out_unlock_dir;
1889 }
1890
1891 if (!dentry->d_inode) {
1892 err = -ENOENT;
1893 goto out_dput;
1894 }
1895
1896 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001897 dest = BTRFS_I(inode)->root;
1898 if (!capable(CAP_SYS_ADMIN)){
1899 /*
1900 * Regular user. Only allow this with a special mount
1901 * option, when the user has write+exec access to the
1902 * subvol root, and when rmdir(2) would have been
1903 * allowed.
1904 *
1905 * Note that this is _not_ check that the subvol is
1906 * empty or doesn't contain data that we wouldn't
1907 * otherwise be able to delete.
1908 *
1909 * Users who want to delete empty subvols should try
1910 * rmdir(2).
1911 */
1912 err = -EPERM;
1913 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1914 goto out_dput;
1915
1916 /*
1917 * Do not allow deletion if the parent dir is the same
1918 * as the dir to be deleted. That means the ioctl
1919 * must be called on the dentry referencing the root
1920 * of the subvol, not a random directory contained
1921 * within it.
1922 */
1923 err = -EINVAL;
1924 if (root == dest)
1925 goto out_dput;
1926
1927 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1928 if (err)
1929 goto out_dput;
1930
1931 /* check if subvolume may be deleted by a non-root user */
1932 err = btrfs_may_delete(dir, dentry, 1);
1933 if (err)
1934 goto out_dput;
1935 }
1936
Li Zefan33345d012011-04-20 10:31:50 +08001937 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04001938 err = -EINVAL;
1939 goto out_dput;
1940 }
1941
Yan, Zheng76dda932009-09-21 16:00:26 -04001942 mutex_lock(&inode->i_mutex);
1943 err = d_invalidate(dentry);
1944 if (err)
1945 goto out_unlock;
1946
1947 down_write(&root->fs_info->subvol_sem);
1948
1949 err = may_destroy_subvol(dest);
1950 if (err)
1951 goto out_up_write;
1952
Yan, Zhenga22285a2010-05-16 10:48:46 -04001953 trans = btrfs_start_transaction(root, 0);
1954 if (IS_ERR(trans)) {
1955 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001956 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001957 }
1958 trans->block_rsv = &root->fs_info->global_block_rsv;
1959
Yan, Zheng76dda932009-09-21 16:00:26 -04001960 ret = btrfs_unlink_subvol(trans, root, dir,
1961 dest->root_key.objectid,
1962 dentry->d_name.name,
1963 dentry->d_name.len);
1964 BUG_ON(ret);
1965
1966 btrfs_record_root_in_trans(trans, dest);
1967
1968 memset(&dest->root_item.drop_progress, 0,
1969 sizeof(dest->root_item.drop_progress));
1970 dest->root_item.drop_level = 0;
1971 btrfs_set_root_refs(&dest->root_item, 0);
1972
Yan, Zhengd68fc572010-05-16 10:49:58 -04001973 if (!xchg(&dest->orphan_item_inserted, 1)) {
1974 ret = btrfs_insert_orphan_item(trans,
1975 root->fs_info->tree_root,
1976 dest->root_key.objectid);
1977 BUG_ON(ret);
1978 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001979
Sage Weil531cb132010-10-29 15:41:32 -04001980 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001981 BUG_ON(ret);
1982 inode->i_flags |= S_DEAD;
1983out_up_write:
1984 up_write(&root->fs_info->subvol_sem);
1985out_unlock:
1986 mutex_unlock(&inode->i_mutex);
1987 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001988 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001989 btrfs_invalidate_inodes(dest);
1990 d_delete(dentry);
1991 }
1992out_dput:
1993 dput(dentry);
1994out_unlock_dir:
1995 mutex_unlock(&dir->i_mutex);
1996 mnt_drop_write(file->f_path.mnt);
1997out:
1998 kfree(vol_args);
1999 return err;
2000}
2001
Chris Mason1e701a32010-03-11 09:42:04 -05002002static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002003{
2004 struct inode *inode = fdentry(file)->d_inode;
2005 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002006 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002007 int ret;
2008
Li Zefanb83cc962010-12-20 16:04:08 +08002009 if (btrfs_root_readonly(root))
2010 return -EROFS;
2011
Yan Zhengc146afa2008-11-12 14:34:12 -05002012 ret = mnt_want_write(file->f_path.mnt);
2013 if (ret)
2014 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002015
2016 switch (inode->i_mode & S_IFMT) {
2017 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002018 if (!capable(CAP_SYS_ADMIN)) {
2019 ret = -EPERM;
2020 goto out;
2021 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002022 ret = btrfs_defrag_root(root, 0);
2023 if (ret)
2024 goto out;
2025 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002026 break;
2027 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002028 if (!(file->f_mode & FMODE_WRITE)) {
2029 ret = -EINVAL;
2030 goto out;
2031 }
Chris Mason1e701a32010-03-11 09:42:04 -05002032
2033 range = kzalloc(sizeof(*range), GFP_KERNEL);
2034 if (!range) {
2035 ret = -ENOMEM;
2036 goto out;
2037 }
2038
2039 if (argp) {
2040 if (copy_from_user(range, argp,
2041 sizeof(*range))) {
2042 ret = -EFAULT;
2043 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002044 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002045 }
2046 /* compression requires us to start the IO */
2047 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2048 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2049 range->extent_thresh = (u32)-1;
2050 }
2051 } else {
2052 /* the rest are all set to zero by kzalloc */
2053 range->len = (u64)-1;
2054 }
Chris Mason4cb53002011-05-24 15:35:30 -04002055 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2056 range, 0, 0);
2057 if (ret > 0)
2058 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002059 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002060 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002061 default:
2062 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002063 }
Chris Masone441d542009-01-05 16:57:23 -05002064out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05002065 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05002066 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002067}
2068
Christoph Hellwigb2950862008-12-02 09:54:17 -05002069static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002070{
2071 struct btrfs_ioctl_vol_args *vol_args;
2072 int ret;
2073
Chris Masone441d542009-01-05 16:57:23 -05002074 if (!capable(CAP_SYS_ADMIN))
2075 return -EPERM;
2076
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002077 mutex_lock(&root->fs_info->volume_mutex);
2078 if (root->fs_info->balance_ctl) {
2079 printk(KERN_INFO "btrfs: balance in progress\n");
2080 ret = -EINVAL;
2081 goto out;
2082 }
2083
Li Zefandae7b662009-04-08 15:06:54 +08002084 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002085 if (IS_ERR(vol_args)) {
2086 ret = PTR_ERR(vol_args);
2087 goto out;
2088 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002089
Mark Fasheh5516e592008-07-24 12:20:14 -04002090 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002091 ret = btrfs_init_new_device(root, vol_args->name);
2092
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002093 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002094out:
2095 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002096 return ret;
2097}
2098
Christoph Hellwigb2950862008-12-02 09:54:17 -05002099static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002100{
2101 struct btrfs_ioctl_vol_args *vol_args;
2102 int ret;
2103
Chris Masone441d542009-01-05 16:57:23 -05002104 if (!capable(CAP_SYS_ADMIN))
2105 return -EPERM;
2106
Yan Zhengc146afa2008-11-12 14:34:12 -05002107 if (root->fs_info->sb->s_flags & MS_RDONLY)
2108 return -EROFS;
2109
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002110 mutex_lock(&root->fs_info->volume_mutex);
2111 if (root->fs_info->balance_ctl) {
2112 printk(KERN_INFO "btrfs: balance in progress\n");
2113 ret = -EINVAL;
2114 goto out;
2115 }
2116
Li Zefandae7b662009-04-08 15:06:54 +08002117 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002118 if (IS_ERR(vol_args)) {
2119 ret = PTR_ERR(vol_args);
2120 goto out;
2121 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002122
Mark Fasheh5516e592008-07-24 12:20:14 -04002123 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002124 ret = btrfs_rm_device(root, vol_args->name);
2125
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002126 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002127out:
2128 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002129 return ret;
2130}
2131
Jan Schmidt475f6382011-03-11 15:41:01 +01002132static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2133{
Li Zefan027ed2f2011-06-08 08:27:56 +00002134 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002135 struct btrfs_device *device;
2136 struct btrfs_device *next;
2137 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002138 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002139
2140 if (!capable(CAP_SYS_ADMIN))
2141 return -EPERM;
2142
Li Zefan027ed2f2011-06-08 08:27:56 +00002143 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2144 if (!fi_args)
2145 return -ENOMEM;
2146
2147 fi_args->num_devices = fs_devices->num_devices;
2148 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002149
2150 mutex_lock(&fs_devices->device_list_mutex);
2151 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002152 if (device->devid > fi_args->max_id)
2153 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002154 }
2155 mutex_unlock(&fs_devices->device_list_mutex);
2156
Li Zefan027ed2f2011-06-08 08:27:56 +00002157 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2158 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002159
Li Zefan027ed2f2011-06-08 08:27:56 +00002160 kfree(fi_args);
2161 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002162}
2163
2164static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2165{
2166 struct btrfs_ioctl_dev_info_args *di_args;
2167 struct btrfs_device *dev;
2168 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2169 int ret = 0;
2170 char *s_uuid = NULL;
2171 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2172
2173 if (!capable(CAP_SYS_ADMIN))
2174 return -EPERM;
2175
2176 di_args = memdup_user(arg, sizeof(*di_args));
2177 if (IS_ERR(di_args))
2178 return PTR_ERR(di_args);
2179
2180 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2181 s_uuid = di_args->uuid;
2182
2183 mutex_lock(&fs_devices->device_list_mutex);
2184 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2185 mutex_unlock(&fs_devices->device_list_mutex);
2186
2187 if (!dev) {
2188 ret = -ENODEV;
2189 goto out;
2190 }
2191
2192 di_args->devid = dev->devid;
2193 di_args->bytes_used = dev->bytes_used;
2194 di_args->total_bytes = dev->total_bytes;
2195 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2196 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2197
2198out:
2199 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2200 ret = -EFAULT;
2201
2202 kfree(di_args);
2203 return ret;
2204}
2205
Yan, Zheng76dda932009-09-21 16:00:26 -04002206static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2207 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002208{
2209 struct inode *inode = fdentry(file)->d_inode;
2210 struct btrfs_root *root = BTRFS_I(inode)->root;
2211 struct file *src_file;
2212 struct inode *src;
2213 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002214 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002215 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002216 char *buf;
2217 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002218 u32 nritems;
2219 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002220 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002221 u64 len = olen;
2222 u64 bs = root->fs_info->sb->s_blocksize;
2223 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002224
Sage Weilc5c9cd42008-11-12 14:32:25 -05002225 /*
2226 * TODO:
2227 * - split compressed inline extents. annoying: we need to
2228 * decompress into destination's address_space (the file offset
2229 * may change, so source mapping won't do), then recompress (or
2230 * otherwise reinsert) a subrange.
2231 * - allow ranges within the same file to be cloned (provided
2232 * they don't overlap)?
2233 */
2234
Chris Masone441d542009-01-05 16:57:23 -05002235 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002236 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002237 return -EINVAL;
2238
Li Zefanb83cc962010-12-20 16:04:08 +08002239 if (btrfs_root_readonly(root))
2240 return -EROFS;
2241
Yan Zhengc146afa2008-11-12 14:34:12 -05002242 ret = mnt_want_write(file->f_path.mnt);
2243 if (ret)
2244 return ret;
2245
Sage Weilc5c9cd42008-11-12 14:32:25 -05002246 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002247 if (!src_file) {
2248 ret = -EBADF;
2249 goto out_drop_write;
2250 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002251
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002252 src = src_file->f_dentry->d_inode;
2253
Sage Weilc5c9cd42008-11-12 14:32:25 -05002254 ret = -EINVAL;
2255 if (src == inode)
2256 goto out_fput;
2257
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002258 /* the src must be open for reading */
2259 if (!(src_file->f_mode & FMODE_READ))
2260 goto out_fput;
2261
Li Zefan0e7b8242011-09-18 10:20:46 -04002262 /* don't make the dst file partly checksummed */
2263 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2264 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2265 goto out_fput;
2266
Yan Zhengae01a0a2008-08-04 23:23:47 -04002267 ret = -EISDIR;
2268 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002269 goto out_fput;
2270
Yan Zhengae01a0a2008-08-04 23:23:47 -04002271 ret = -EXDEV;
2272 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2273 goto out_fput;
2274
2275 ret = -ENOMEM;
2276 buf = vmalloc(btrfs_level_size(root, 0));
2277 if (!buf)
2278 goto out_fput;
2279
2280 path = btrfs_alloc_path();
2281 if (!path) {
2282 vfree(buf);
2283 goto out_fput;
2284 }
2285 path->reada = 2;
2286
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002287 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002288 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2289 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002290 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002291 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2292 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002293 }
2294
Sage Weilc5c9cd42008-11-12 14:32:25 -05002295 /* determine range to clone */
2296 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002297 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002298 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002299 if (len == 0)
2300 olen = len = src->i_size - off;
2301 /* if we extend to eof, continue to block boundary */
2302 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002303 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002304
2305 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002306 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2307 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002308 goto out_unlock;
2309
Li Zefand525e8a2011-09-11 10:52:25 -04002310 if (destoff > inode->i_size) {
2311 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2312 if (ret)
2313 goto out_unlock;
2314 }
2315
Li Zefan71ef0782011-09-18 10:20:46 -04002316 /* truncate page cache pages from target inode range */
2317 truncate_inode_pages_range(&inode->i_data, destoff,
2318 PAGE_CACHE_ALIGN(destoff + len) - 1);
2319
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002320 /* do any pending delalloc/csum calc on src, one way or
2321 another, and lock file content */
2322 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002323 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002324 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04002325 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2326 if (!ordered &&
2327 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2328 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002329 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002330 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002331 if (ordered)
2332 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002333 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002334 }
2335
Sage Weilc5c9cd42008-11-12 14:32:25 -05002336 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002337 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002338 key.type = BTRFS_EXTENT_DATA_KEY;
2339 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002340
2341 while (1) {
2342 /*
2343 * note the key will change type as we walk through the
2344 * tree.
2345 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002346 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002347 if (ret < 0)
2348 goto out;
2349
Yan Zhengae01a0a2008-08-04 23:23:47 -04002350 nritems = btrfs_header_nritems(path->nodes[0]);
2351 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002352 ret = btrfs_next_leaf(root, path);
2353 if (ret < 0)
2354 goto out;
2355 if (ret > 0)
2356 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002357 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002358 }
2359 leaf = path->nodes[0];
2360 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002361
Yan Zhengae01a0a2008-08-04 23:23:47 -04002362 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002363 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002364 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002365 break;
2366
Sage Weilc5c9cd42008-11-12 14:32:25 -05002367 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2368 struct btrfs_file_extent_item *extent;
2369 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002370 u32 size;
2371 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002372 u64 disko = 0, diskl = 0;
2373 u64 datao = 0, datal = 0;
2374 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002375 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002376
2377 size = btrfs_item_size_nr(leaf, slot);
2378 read_extent_buffer(leaf, buf,
2379 btrfs_item_ptr_offset(leaf, slot),
2380 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002381
2382 extent = btrfs_item_ptr(leaf, slot,
2383 struct btrfs_file_extent_item);
2384 comp = btrfs_file_extent_compression(leaf, extent);
2385 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002386 if (type == BTRFS_FILE_EXTENT_REG ||
2387 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002388 disko = btrfs_file_extent_disk_bytenr(leaf,
2389 extent);
2390 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2391 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002392 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002393 datal = btrfs_file_extent_num_bytes(leaf,
2394 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002395 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2396 /* take upper bound, may be compressed */
2397 datal = btrfs_file_extent_ram_bytes(leaf,
2398 extent);
2399 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002400 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002401
Sage Weil050006a2010-10-29 15:37:33 -04002402 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002403 key.offset >= off+len)
2404 goto next;
2405
Zheng Yan31840ae2008-09-23 13:14:14 -04002406 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002407 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002408 if (off <= key.offset)
2409 new_key.offset = key.offset + destoff - off;
2410 else
2411 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002412
Sage Weilb6f34092011-09-20 14:48:51 -04002413 /*
2414 * 1 - adjusting old extent (we may have to split it)
2415 * 1 - add new extent
2416 * 1 - inode update
2417 */
2418 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002419 if (IS_ERR(trans)) {
2420 ret = PTR_ERR(trans);
2421 goto out;
2422 }
2423
Chris Masonc8a894d2009-06-27 21:07:03 -04002424 if (type == BTRFS_FILE_EXTENT_REG ||
2425 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002426 /*
2427 * a | --- range to clone ---| b
2428 * | ------------- extent ------------- |
2429 */
2430
2431 /* substract range b */
2432 if (key.offset + datal > off + len)
2433 datal = off + len - key.offset;
2434
2435 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002436 if (off > key.offset) {
2437 datao += off - key.offset;
2438 datal -= off - key.offset;
2439 }
2440
Yan, Zhenga22285a2010-05-16 10:48:46 -04002441 ret = btrfs_drop_extents(trans, inode,
2442 new_key.offset,
2443 new_key.offset + datal,
2444 &hint_byte, 1);
2445 BUG_ON(ret);
2446
Sage Weilc5c9cd42008-11-12 14:32:25 -05002447 ret = btrfs_insert_empty_item(trans, root, path,
2448 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002449 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002450
2451 leaf = path->nodes[0];
2452 slot = path->slots[0];
2453 write_extent_buffer(leaf, buf,
2454 btrfs_item_ptr_offset(leaf, slot),
2455 size);
2456
2457 extent = btrfs_item_ptr(leaf, slot,
2458 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002459
Sage Weilc5c9cd42008-11-12 14:32:25 -05002460 /* disko == 0 means it's a hole */
2461 if (!disko)
2462 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002463
2464 btrfs_set_file_extent_offset(leaf, extent,
2465 datao);
2466 btrfs_set_file_extent_num_bytes(leaf, extent,
2467 datal);
2468 if (disko) {
2469 inode_add_bytes(inode, datal);
2470 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002471 disko, diskl, 0,
2472 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002473 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002474 new_key.offset - datao,
2475 0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002476 BUG_ON(ret);
2477 }
2478 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2479 u64 skip = 0;
2480 u64 trim = 0;
2481 if (off > key.offset) {
2482 skip = off - key.offset;
2483 new_key.offset += skip;
2484 }
Chris Masond3977122009-01-05 21:25:51 -05002485
Sage Weilc5c9cd42008-11-12 14:32:25 -05002486 if (key.offset + datal > off+len)
2487 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002488
Sage Weilc5c9cd42008-11-12 14:32:25 -05002489 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002490 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002491 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002492 goto out;
2493 }
2494 size -= skip + trim;
2495 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002496
2497 ret = btrfs_drop_extents(trans, inode,
2498 new_key.offset,
2499 new_key.offset + datal,
2500 &hint_byte, 1);
2501 BUG_ON(ret);
2502
Sage Weilc5c9cd42008-11-12 14:32:25 -05002503 ret = btrfs_insert_empty_item(trans, root, path,
2504 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002505 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002506
2507 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002508 u32 start =
2509 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002510 memmove(buf+start, buf+start+skip,
2511 datal);
2512 }
2513
2514 leaf = path->nodes[0];
2515 slot = path->slots[0];
2516 write_extent_buffer(leaf, buf,
2517 btrfs_item_ptr_offset(leaf, slot),
2518 size);
2519 inode_add_bytes(inode, datal);
2520 }
2521
2522 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002523 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002524
Yan, Zhenga22285a2010-05-16 10:48:46 -04002525 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002526
2527 /*
2528 * we round up to the block size at eof when
2529 * determining which extents to clone above,
2530 * but shouldn't round up the file size
2531 */
2532 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002533 if (endoff > destoff+olen)
2534 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002535 if (endoff > inode->i_size)
2536 btrfs_i_size_write(inode, endoff);
2537
Yan, Zhenga22285a2010-05-16 10:48:46 -04002538 ret = btrfs_update_inode(trans, root, inode);
2539 BUG_ON(ret);
2540 btrfs_end_transaction(trans, root);
2541 }
Chris Masond3977122009-01-05 21:25:51 -05002542next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002543 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002544 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002545 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002546 ret = 0;
2547out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002548 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002549 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002550out_unlock:
2551 mutex_unlock(&src->i_mutex);
2552 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002553 vfree(buf);
2554 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002555out_fput:
2556 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002557out_drop_write:
2558 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002559 return ret;
2560}
2561
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002562static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002563{
2564 struct btrfs_ioctl_clone_range_args args;
2565
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002566 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002567 return -EFAULT;
2568 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2569 args.src_length, args.dest_offset);
2570}
2571
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002572/*
2573 * there are many ways the trans_start and trans_end ioctls can lead
2574 * to deadlocks. They should only be used by applications that
2575 * basically own the machine, and have a very in depth understanding
2576 * of all the possible deadlocks and enospc problems.
2577 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002578static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002579{
2580 struct inode *inode = fdentry(file)->d_inode;
2581 struct btrfs_root *root = BTRFS_I(inode)->root;
2582 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002583 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002584
Sage Weil1ab86ae2009-09-29 18:38:44 -04002585 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002586 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002587 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002588
2589 ret = -EINPROGRESS;
2590 if (file->private_data)
2591 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002592
Li Zefanb83cc962010-12-20 16:04:08 +08002593 ret = -EROFS;
2594 if (btrfs_root_readonly(root))
2595 goto out;
2596
Yan Zhengc146afa2008-11-12 14:34:12 -05002597 ret = mnt_want_write(file->f_path.mnt);
2598 if (ret)
2599 goto out;
2600
Josef Bacika4abeea2011-04-11 17:25:13 -04002601 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002602
Sage Weil1ab86ae2009-09-29 18:38:44 -04002603 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002604 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002605 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002606 goto out_drop;
2607
2608 file->private_data = trans;
2609 return 0;
2610
2611out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002612 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002613 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002614out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002615 return ret;
2616}
2617
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002618static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2619{
2620 struct inode *inode = fdentry(file)->d_inode;
2621 struct btrfs_root *root = BTRFS_I(inode)->root;
2622 struct btrfs_root *new_root;
2623 struct btrfs_dir_item *di;
2624 struct btrfs_trans_handle *trans;
2625 struct btrfs_path *path;
2626 struct btrfs_key location;
2627 struct btrfs_disk_key disk_key;
2628 struct btrfs_super_block *disk_super;
2629 u64 features;
2630 u64 objectid = 0;
2631 u64 dir_id;
2632
2633 if (!capable(CAP_SYS_ADMIN))
2634 return -EPERM;
2635
2636 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2637 return -EFAULT;
2638
2639 if (!objectid)
2640 objectid = root->root_key.objectid;
2641
2642 location.objectid = objectid;
2643 location.type = BTRFS_ROOT_ITEM_KEY;
2644 location.offset = (u64)-1;
2645
2646 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2647 if (IS_ERR(new_root))
2648 return PTR_ERR(new_root);
2649
2650 if (btrfs_root_refs(&new_root->root_item) == 0)
2651 return -ENOENT;
2652
2653 path = btrfs_alloc_path();
2654 if (!path)
2655 return -ENOMEM;
2656 path->leave_spinning = 1;
2657
2658 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002659 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002660 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002661 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002662 }
2663
David Sterba6c417612011-04-13 15:41:04 +02002664 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002665 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2666 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002667 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002668 btrfs_free_path(path);
2669 btrfs_end_transaction(trans, root);
2670 printk(KERN_ERR "Umm, you don't have the default dir item, "
2671 "this isn't going to work\n");
2672 return -ENOENT;
2673 }
2674
2675 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2676 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2677 btrfs_mark_buffer_dirty(path->nodes[0]);
2678 btrfs_free_path(path);
2679
David Sterba6c417612011-04-13 15:41:04 +02002680 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002681 features = btrfs_super_incompat_flags(disk_super);
2682 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2683 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2684 btrfs_set_super_incompat_flags(disk_super, features);
2685 }
2686 btrfs_end_transaction(trans, root);
2687
2688 return 0;
2689}
2690
Josef Bacikbf5fc092010-09-29 11:22:36 -04002691static void get_block_group_info(struct list_head *groups_list,
2692 struct btrfs_ioctl_space_info *space)
2693{
2694 struct btrfs_block_group_cache *block_group;
2695
2696 space->total_bytes = 0;
2697 space->used_bytes = 0;
2698 space->flags = 0;
2699 list_for_each_entry(block_group, groups_list, list) {
2700 space->flags = block_group->flags;
2701 space->total_bytes += block_group->key.offset;
2702 space->used_bytes +=
2703 btrfs_block_group_used(&block_group->item);
2704 }
2705}
2706
Josef Bacik1406e432010-01-13 18:19:06 +00002707long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2708{
2709 struct btrfs_ioctl_space_args space_args;
2710 struct btrfs_ioctl_space_info space;
2711 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002712 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002713 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002714 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002715 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2716 BTRFS_BLOCK_GROUP_SYSTEM,
2717 BTRFS_BLOCK_GROUP_METADATA,
2718 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2719 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002720 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002721 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002722 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002723 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002724
2725 if (copy_from_user(&space_args,
2726 (struct btrfs_ioctl_space_args __user *)arg,
2727 sizeof(space_args)))
2728 return -EFAULT;
2729
Josef Bacikbf5fc092010-09-29 11:22:36 -04002730 for (i = 0; i < num_types; i++) {
2731 struct btrfs_space_info *tmp;
2732
2733 info = NULL;
2734 rcu_read_lock();
2735 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2736 list) {
2737 if (tmp->flags == types[i]) {
2738 info = tmp;
2739 break;
2740 }
2741 }
2742 rcu_read_unlock();
2743
2744 if (!info)
2745 continue;
2746
2747 down_read(&info->groups_sem);
2748 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2749 if (!list_empty(&info->block_groups[c]))
2750 slot_count++;
2751 }
2752 up_read(&info->groups_sem);
2753 }
Josef Bacik1406e432010-01-13 18:19:06 +00002754
Chris Mason7fde62b2010-03-16 15:40:10 -04002755 /* space_slots == 0 means they are asking for a count */
2756 if (space_args.space_slots == 0) {
2757 space_args.total_spaces = slot_count;
2758 goto out;
2759 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002760
Dan Rosenberg51788b12011-02-14 16:04:23 -05002761 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002762
Chris Mason7fde62b2010-03-16 15:40:10 -04002763 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002764
Chris Mason7fde62b2010-03-16 15:40:10 -04002765 /* we generally have at most 6 or so space infos, one for each raid
2766 * level. So, a whole page should be more than enough for everyone
2767 */
2768 if (alloc_size > PAGE_CACHE_SIZE)
2769 return -ENOMEM;
2770
2771 space_args.total_spaces = 0;
2772 dest = kmalloc(alloc_size, GFP_NOFS);
2773 if (!dest)
2774 return -ENOMEM;
2775 dest_orig = dest;
2776
2777 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002778 for (i = 0; i < num_types; i++) {
2779 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002780
Dan Rosenberg51788b12011-02-14 16:04:23 -05002781 if (!slot_count)
2782 break;
2783
Josef Bacikbf5fc092010-09-29 11:22:36 -04002784 info = NULL;
2785 rcu_read_lock();
2786 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2787 list) {
2788 if (tmp->flags == types[i]) {
2789 info = tmp;
2790 break;
2791 }
2792 }
2793 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002794
Josef Bacikbf5fc092010-09-29 11:22:36 -04002795 if (!info)
2796 continue;
2797 down_read(&info->groups_sem);
2798 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2799 if (!list_empty(&info->block_groups[c])) {
2800 get_block_group_info(&info->block_groups[c],
2801 &space);
2802 memcpy(dest, &space, sizeof(space));
2803 dest++;
2804 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002805 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002806 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002807 if (!slot_count)
2808 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002809 }
2810 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002811 }
Josef Bacik1406e432010-01-13 18:19:06 +00002812
Chris Mason7fde62b2010-03-16 15:40:10 -04002813 user_dest = (struct btrfs_ioctl_space_info *)
2814 (arg + sizeof(struct btrfs_ioctl_space_args));
2815
2816 if (copy_to_user(user_dest, dest_orig, alloc_size))
2817 ret = -EFAULT;
2818
2819 kfree(dest_orig);
2820out:
2821 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002822 ret = -EFAULT;
2823
2824 return ret;
2825}
2826
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002827/*
2828 * there are many ways the trans_start and trans_end ioctls can lead
2829 * to deadlocks. They should only be used by applications that
2830 * basically own the machine, and have a very in depth understanding
2831 * of all the possible deadlocks and enospc problems.
2832 */
2833long btrfs_ioctl_trans_end(struct file *file)
2834{
2835 struct inode *inode = fdentry(file)->d_inode;
2836 struct btrfs_root *root = BTRFS_I(inode)->root;
2837 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002838
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002839 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002840 if (!trans)
2841 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002842 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002843
Sage Weil1ab86ae2009-09-29 18:38:44 -04002844 btrfs_end_transaction(trans, root);
2845
Josef Bacika4abeea2011-04-11 17:25:13 -04002846 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002847
Sage Weilcfc8ea82008-12-11 16:30:06 -05002848 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002849 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002850}
2851
Sage Weil46204592010-10-29 15:41:32 -04002852static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2853{
2854 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2855 struct btrfs_trans_handle *trans;
2856 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002857 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002858
2859 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002860 if (IS_ERR(trans))
2861 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002862 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002863 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002864 if (ret) {
2865 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002866 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002867 }
Sage Weil46204592010-10-29 15:41:32 -04002868
2869 if (argp)
2870 if (copy_to_user(argp, &transid, sizeof(transid)))
2871 return -EFAULT;
2872 return 0;
2873}
2874
2875static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2876{
2877 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2878 u64 transid;
2879
2880 if (argp) {
2881 if (copy_from_user(&transid, argp, sizeof(transid)))
2882 return -EFAULT;
2883 } else {
2884 transid = 0; /* current trans */
2885 }
2886 return btrfs_wait_for_commit(root, transid);
2887}
2888
Jan Schmidt475f6382011-03-11 15:41:01 +01002889static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2890{
2891 int ret;
2892 struct btrfs_ioctl_scrub_args *sa;
2893
2894 if (!capable(CAP_SYS_ADMIN))
2895 return -EPERM;
2896
2897 sa = memdup_user(arg, sizeof(*sa));
2898 if (IS_ERR(sa))
2899 return PTR_ERR(sa);
2900
2901 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01002902 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01002903
2904 if (copy_to_user(arg, sa, sizeof(*sa)))
2905 ret = -EFAULT;
2906
2907 kfree(sa);
2908 return ret;
2909}
2910
2911static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2912{
2913 if (!capable(CAP_SYS_ADMIN))
2914 return -EPERM;
2915
2916 return btrfs_scrub_cancel(root);
2917}
2918
2919static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2920 void __user *arg)
2921{
2922 struct btrfs_ioctl_scrub_args *sa;
2923 int ret;
2924
2925 if (!capable(CAP_SYS_ADMIN))
2926 return -EPERM;
2927
2928 sa = memdup_user(arg, sizeof(*sa));
2929 if (IS_ERR(sa))
2930 return PTR_ERR(sa);
2931
2932 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2933
2934 if (copy_to_user(arg, sa, sizeof(*sa)))
2935 ret = -EFAULT;
2936
2937 kfree(sa);
2938 return ret;
2939}
2940
Jan Schmidtd7728c92011-07-07 16:48:38 +02002941static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
2942{
2943 int ret = 0;
2944 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04002945 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002946 int size;
Chris Mason806468f2011-11-06 03:07:10 -05002947 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002948 struct inode_fs_paths *ipath = NULL;
2949 struct btrfs_path *path;
2950
2951 if (!capable(CAP_SYS_ADMIN))
2952 return -EPERM;
2953
2954 path = btrfs_alloc_path();
2955 if (!path) {
2956 ret = -ENOMEM;
2957 goto out;
2958 }
2959
2960 ipa = memdup_user(arg, sizeof(*ipa));
2961 if (IS_ERR(ipa)) {
2962 ret = PTR_ERR(ipa);
2963 ipa = NULL;
2964 goto out;
2965 }
2966
2967 size = min_t(u32, ipa->size, 4096);
2968 ipath = init_ipath(size, root, path);
2969 if (IS_ERR(ipath)) {
2970 ret = PTR_ERR(ipath);
2971 ipath = NULL;
2972 goto out;
2973 }
2974
2975 ret = paths_from_inode(ipa->inum, ipath);
2976 if (ret < 0)
2977 goto out;
2978
2979 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002980 rel_ptr = ipath->fspath->val[i] -
2981 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04002982 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002983 }
2984
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002985 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
2986 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02002987 if (ret) {
2988 ret = -EFAULT;
2989 goto out;
2990 }
2991
2992out:
2993 btrfs_free_path(path);
2994 free_ipath(ipath);
2995 kfree(ipa);
2996
2997 return ret;
2998}
2999
3000static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3001{
3002 struct btrfs_data_container *inodes = ctx;
3003 const size_t c = 3 * sizeof(u64);
3004
3005 if (inodes->bytes_left >= c) {
3006 inodes->bytes_left -= c;
3007 inodes->val[inodes->elem_cnt] = inum;
3008 inodes->val[inodes->elem_cnt + 1] = offset;
3009 inodes->val[inodes->elem_cnt + 2] = root;
3010 inodes->elem_cnt += 3;
3011 } else {
3012 inodes->bytes_missing += c - inodes->bytes_left;
3013 inodes->bytes_left = 0;
3014 inodes->elem_missed += 3;
3015 }
3016
3017 return 0;
3018}
3019
3020static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3021 void __user *arg)
3022{
3023 int ret = 0;
3024 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003025 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003026 struct btrfs_ioctl_logical_ino_args *loi;
3027 struct btrfs_data_container *inodes = NULL;
3028 struct btrfs_path *path = NULL;
3029 struct btrfs_key key;
3030
3031 if (!capable(CAP_SYS_ADMIN))
3032 return -EPERM;
3033
3034 loi = memdup_user(arg, sizeof(*loi));
3035 if (IS_ERR(loi)) {
3036 ret = PTR_ERR(loi);
3037 loi = NULL;
3038 goto out;
3039 }
3040
3041 path = btrfs_alloc_path();
3042 if (!path) {
3043 ret = -ENOMEM;
3044 goto out;
3045 }
3046
3047 size = min_t(u32, loi->size, 4096);
3048 inodes = init_data_container(size);
3049 if (IS_ERR(inodes)) {
3050 ret = PTR_ERR(inodes);
3051 inodes = NULL;
3052 goto out;
3053 }
3054
3055 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003056 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003057
3058 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3059 ret = -ENOENT;
3060 if (ret < 0)
3061 goto out;
3062
Jan Schmidt4692cf52011-12-02 14:56:41 +01003063 extent_item_pos = loi->logical - key.objectid;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003064 ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003065 extent_item_pos, build_ino_list,
3066 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003067
3068 if (ret < 0)
3069 goto out;
3070
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003071 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3072 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003073 if (ret)
3074 ret = -EFAULT;
3075
3076out:
3077 btrfs_free_path(path);
3078 kfree(inodes);
3079 kfree(loi);
3080
3081 return ret;
3082}
3083
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003084void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003085 struct btrfs_ioctl_balance_args *bargs)
3086{
3087 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3088
3089 bargs->flags = bctl->flags;
3090
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003091 if (atomic_read(&fs_info->balance_running))
3092 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3093 if (atomic_read(&fs_info->balance_pause_req))
3094 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003095 if (atomic_read(&fs_info->balance_cancel_req))
3096 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003097
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003098 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3099 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3100 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003101
3102 if (lock) {
3103 spin_lock(&fs_info->balance_lock);
3104 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3105 spin_unlock(&fs_info->balance_lock);
3106 } else {
3107 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3108 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003109}
3110
3111static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3112{
3113 struct btrfs_fs_info *fs_info = root->fs_info;
3114 struct btrfs_ioctl_balance_args *bargs;
3115 struct btrfs_balance_control *bctl;
3116 int ret;
3117
3118 if (!capable(CAP_SYS_ADMIN))
3119 return -EPERM;
3120
3121 if (fs_info->sb->s_flags & MS_RDONLY)
3122 return -EROFS;
3123
3124 mutex_lock(&fs_info->volume_mutex);
3125 mutex_lock(&fs_info->balance_mutex);
3126
3127 if (arg) {
3128 bargs = memdup_user(arg, sizeof(*bargs));
3129 if (IS_ERR(bargs)) {
3130 ret = PTR_ERR(bargs);
3131 goto out;
3132 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003133
3134 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3135 if (!fs_info->balance_ctl) {
3136 ret = -ENOTCONN;
3137 goto out_bargs;
3138 }
3139
3140 bctl = fs_info->balance_ctl;
3141 spin_lock(&fs_info->balance_lock);
3142 bctl->flags |= BTRFS_BALANCE_RESUME;
3143 spin_unlock(&fs_info->balance_lock);
3144
3145 goto do_balance;
3146 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003147 } else {
3148 bargs = NULL;
3149 }
3150
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003151 if (fs_info->balance_ctl) {
3152 ret = -EINPROGRESS;
3153 goto out_bargs;
3154 }
3155
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003156 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3157 if (!bctl) {
3158 ret = -ENOMEM;
3159 goto out_bargs;
3160 }
3161
3162 bctl->fs_info = fs_info;
3163 if (arg) {
3164 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3165 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3166 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3167
3168 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003169 } else {
3170 /* balance everything - no filters */
3171 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003172 }
3173
Ilya Dryomovde322262012-01-16 22:04:49 +02003174do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003175 ret = btrfs_balance(bctl, bargs);
3176 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003177 * bctl is freed in __cancel_balance or in free_fs_info if
3178 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003179 */
3180 if (arg) {
3181 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3182 ret = -EFAULT;
3183 }
3184
3185out_bargs:
3186 kfree(bargs);
3187out:
3188 mutex_unlock(&fs_info->balance_mutex);
3189 mutex_unlock(&fs_info->volume_mutex);
3190 return ret;
3191}
3192
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003193static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3194{
3195 if (!capable(CAP_SYS_ADMIN))
3196 return -EPERM;
3197
3198 switch (cmd) {
3199 case BTRFS_BALANCE_CTL_PAUSE:
3200 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003201 case BTRFS_BALANCE_CTL_CANCEL:
3202 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003203 }
3204
3205 return -EINVAL;
3206}
3207
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003208static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3209 void __user *arg)
3210{
3211 struct btrfs_fs_info *fs_info = root->fs_info;
3212 struct btrfs_ioctl_balance_args *bargs;
3213 int ret = 0;
3214
3215 if (!capable(CAP_SYS_ADMIN))
3216 return -EPERM;
3217
3218 mutex_lock(&fs_info->balance_mutex);
3219 if (!fs_info->balance_ctl) {
3220 ret = -ENOTCONN;
3221 goto out;
3222 }
3223
3224 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3225 if (!bargs) {
3226 ret = -ENOMEM;
3227 goto out;
3228 }
3229
3230 update_ioctl_balance_args(fs_info, 1, bargs);
3231
3232 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3233 ret = -EFAULT;
3234
3235 kfree(bargs);
3236out:
3237 mutex_unlock(&fs_info->balance_mutex);
3238 return ret;
3239}
3240
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003241long btrfs_ioctl(struct file *file, unsigned int
3242 cmd, unsigned long arg)
3243{
3244 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003245 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003246
3247 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003248 case FS_IOC_GETFLAGS:
3249 return btrfs_ioctl_getflags(file, argp);
3250 case FS_IOC_SETFLAGS:
3251 return btrfs_ioctl_setflags(file, argp);
3252 case FS_IOC_GETVERSION:
3253 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003254 case FITRIM:
3255 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003256 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003257 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003258 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003259 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003260 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003261 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003262 case BTRFS_IOC_SNAP_DESTROY:
3263 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003264 case BTRFS_IOC_SUBVOL_GETFLAGS:
3265 return btrfs_ioctl_subvol_getflags(file, argp);
3266 case BTRFS_IOC_SUBVOL_SETFLAGS:
3267 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003268 case BTRFS_IOC_DEFAULT_SUBVOL:
3269 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003270 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003271 return btrfs_ioctl_defrag(file, NULL);
3272 case BTRFS_IOC_DEFRAG_RANGE:
3273 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003274 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003275 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003276 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003277 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003278 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003279 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003280 case BTRFS_IOC_FS_INFO:
3281 return btrfs_ioctl_fs_info(root, argp);
3282 case BTRFS_IOC_DEV_INFO:
3283 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003284 case BTRFS_IOC_BALANCE:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003285 return btrfs_ioctl_balance(root, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003286 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003287 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3288 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003289 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003290 case BTRFS_IOC_TRANS_START:
3291 return btrfs_ioctl_trans_start(file);
3292 case BTRFS_IOC_TRANS_END:
3293 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003294 case BTRFS_IOC_TREE_SEARCH:
3295 return btrfs_ioctl_tree_search(file, argp);
3296 case BTRFS_IOC_INO_LOOKUP:
3297 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003298 case BTRFS_IOC_INO_PATHS:
3299 return btrfs_ioctl_ino_to_path(root, argp);
3300 case BTRFS_IOC_LOGICAL_INO:
3301 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003302 case BTRFS_IOC_SPACE_INFO:
3303 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003304 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003305 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3306 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003307 case BTRFS_IOC_START_SYNC:
3308 return btrfs_ioctl_start_sync(file, argp);
3309 case BTRFS_IOC_WAIT_SYNC:
3310 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003311 case BTRFS_IOC_SCRUB:
3312 return btrfs_ioctl_scrub(root, argp);
3313 case BTRFS_IOC_SCRUB_CANCEL:
3314 return btrfs_ioctl_scrub_cancel(root, argp);
3315 case BTRFS_IOC_SCRUB_PROGRESS:
3316 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003317 case BTRFS_IOC_BALANCE_V2:
3318 return btrfs_ioctl_balance(root, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003319 case BTRFS_IOC_BALANCE_CTL:
3320 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003321 case BTRFS_IOC_BALANCE_PROGRESS:
3322 return btrfs_ioctl_balance_progress(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003323 }
3324
3325 return -ENOTTY;
3326}