blob: 4e51494614357f7c03656a1f6fdcc59c96b4e3ce [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
Al Viroa561be72011-11-23 11:57:51 -0500209 ret = mnt_want_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200210 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
Al Viro2a79f172011-12-09 08:06:57 -0500274 mnt_drop_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200275 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{
Al Viro815745c2011-11-17 15:40:49 -0500289 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000290 struct btrfs_device *device;
291 struct request_queue *q;
292 struct fstrim_range range;
293 u64 minlen = ULLONG_MAX;
294 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500295 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000296 int ret;
297
298 if (!capable(CAP_SYS_ADMIN))
299 return -EPERM;
300
Xiao Guangrong1f781602011-04-20 10:09:16 +0000301 rcu_read_lock();
302 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
303 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000304 if (!device->bdev)
305 continue;
306 q = bdev_get_queue(device->bdev);
307 if (blk_queue_discard(q)) {
308 num_devices++;
309 minlen = min((u64)q->limits.discard_granularity,
310 minlen);
311 }
312 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000313 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200314
Li Dongyangf7039b12011-03-24 10:24:28 +0000315 if (!num_devices)
316 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000317 if (copy_from_user(&range, arg, sizeof(range)))
318 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200319 if (range.start > total_bytes)
320 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000321
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200322 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000323 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500324 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000325 if (ret < 0)
326 return ret;
327
328 if (copy_to_user(arg, &range, sizeof(range)))
329 return -EFAULT;
330
331 return 0;
332}
333
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400334static noinline int create_subvol(struct btrfs_root *root,
335 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400336 char *name, int namelen,
337 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400338{
339 struct btrfs_trans_handle *trans;
340 struct btrfs_key key;
341 struct btrfs_root_item root_item;
342 struct btrfs_inode_item *inode_item;
343 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400344 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400345 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000346 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400347 int ret;
348 int err;
349 u64 objectid;
350 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500351 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400352
Li Zefan581bb052011-04-20 10:06:11 +0800353 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400354 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400355 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000356
357 dir = parent->d_inode;
358
Josef Bacik9ed74f22009-09-11 16:12:44 -0400359 /*
360 * 1 - inode item
361 * 2 - refs
362 * 1 - root item
363 * 2 - dir items
364 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400365 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400366 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400367 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400368
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400369 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200370 0, objectid, NULL, 0, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400371 if (IS_ERR(leaf)) {
372 ret = PTR_ERR(leaf);
373 goto fail;
374 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400375
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400376 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400377 btrfs_set_header_bytenr(leaf, leaf->start);
378 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400379 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380 btrfs_set_header_owner(leaf, objectid);
381
382 write_extent_buffer(leaf, root->fs_info->fsid,
383 (unsigned long)btrfs_header_fsid(leaf),
384 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400385 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
386 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
387 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400388 btrfs_mark_buffer_dirty(leaf);
389
390 inode_item = &root_item.inode;
391 memset(inode_item, 0, sizeof(*inode_item));
392 inode_item->generation = cpu_to_le64(1);
393 inode_item->size = cpu_to_le64(3);
394 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400395 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400396 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
397
Li Zefan08fe4db2011-03-28 02:01:25 +0000398 root_item.flags = 0;
399 root_item.byte_limit = 0;
400 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
401
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400402 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400403 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404 btrfs_set_root_level(&root_item, 0);
405 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000406 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400407 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400408
409 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
410 root_item.drop_level = 0;
411
Chris Mason925baed2008-06-25 16:01:30 -0400412 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400413 free_extent_buffer(leaf);
414 leaf = NULL;
415
416 btrfs_set_root_dirid(&root_item, new_dirid);
417
418 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400419 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400420 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
421 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
422 &root_item);
423 if (ret)
424 goto fail;
425
Yan, Zheng76dda932009-09-21 16:00:26 -0400426 key.offset = (u64)-1;
427 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
428 BUG_ON(IS_ERR(new_root));
429
430 btrfs_record_root_in_trans(trans, new_root);
431
Josef Bacikd82a6f12011-05-11 15:26:06 -0400432 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400433 /*
434 * insert the directory item
435 */
Chris Mason3de45862008-11-17 21:02:50 -0500436 ret = btrfs_set_inode_index(dir, &index);
437 BUG_ON(ret);
438
439 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800440 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500441 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442 if (ret)
443 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500444
Yan Zheng52c26172009-01-05 15:43:43 -0500445 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
446 ret = btrfs_update_inode(trans, root, dir);
447 BUG_ON(ret);
448
Chris Mason0660b5a2008-11-17 20:37:39 -0500449 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400450 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800451 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400452
Chris Mason0660b5a2008-11-17 20:37:39 -0500453 BUG_ON(ret);
454
Yan, Zheng76dda932009-09-21 16:00:26 -0400455 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400456fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400457 if (async_transid) {
458 *async_transid = trans->transid;
459 err = btrfs_commit_transaction_async(trans, root, 1);
460 } else {
461 err = btrfs_commit_transaction(trans, root);
462 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400463 if (err && !ret)
464 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400465 return ret;
466}
467
Sage Weil72fd0322010-10-29 15:41:32 -0400468static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800469 char *name, int namelen, u64 *async_transid,
470 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400471{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000472 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400473 struct btrfs_pending_snapshot *pending_snapshot;
474 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000475 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400476
477 if (!root->ref_cows)
478 return -EINVAL;
479
Yan, Zhenga22285a2010-05-16 10:48:46 -0400480 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
481 if (!pending_snapshot)
482 return -ENOMEM;
483
484 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
485 pending_snapshot->dentry = dentry;
486 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800487 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400488
489 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
490 if (IS_ERR(trans)) {
491 ret = PTR_ERR(trans);
492 goto fail;
493 }
494
495 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
496 BUG_ON(ret);
497
Josef Bacik83515832011-06-14 15:16:14 -0400498 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400499 list_add(&pending_snapshot->list,
500 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400501 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400502 if (async_transid) {
503 *async_transid = trans->transid;
504 ret = btrfs_commit_transaction_async(trans,
505 root->fs_info->extent_root, 1);
506 } else {
507 ret = btrfs_commit_transaction(trans,
508 root->fs_info->extent_root);
509 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400510 BUG_ON(ret);
511
512 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400513 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000514 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500516 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
517 if (ret)
518 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519
Al Viro2fbe8c82011-07-16 21:38:06 -0400520 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000521 if (IS_ERR(inode)) {
522 ret = PTR_ERR(inode);
523 goto fail;
524 }
525 BUG_ON(!inode);
526 d_instantiate(dentry, inode);
527 ret = 0;
528fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400529 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400530 return ret;
531}
532
Sage Weil4260f7c2010-10-29 15:46:43 -0400533/* copy of check_sticky in fs/namei.c()
534* It's inline, so penalty for filesystems that don't use sticky bit is
535* minimal.
536*/
537static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
538{
539 uid_t fsuid = current_fsuid();
540
541 if (!(dir->i_mode & S_ISVTX))
542 return 0;
543 if (inode->i_uid == fsuid)
544 return 0;
545 if (dir->i_uid == fsuid)
546 return 0;
547 return !capable(CAP_FOWNER);
548}
549
550/* copy of may_delete in fs/namei.c()
551 * Check whether we can remove a link victim from directory dir, check
552 * whether the type of victim is right.
553 * 1. We can't do it if dir is read-only (done in permission())
554 * 2. We should have write and exec permissions on dir
555 * 3. We can't remove anything from append-only dir
556 * 4. We can't do anything with immutable dir (done in permission())
557 * 5. If the sticky bit on dir is set we should either
558 * a. be owner of dir, or
559 * b. be owner of victim, or
560 * c. have CAP_FOWNER capability
561 * 6. If the victim is append-only or immutable we can't do antyhing with
562 * links pointing to it.
563 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
564 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
565 * 9. We can't remove a root or mountpoint.
566 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
567 * nfs_async_unlink().
568 */
569
570static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
571{
572 int error;
573
574 if (!victim->d_inode)
575 return -ENOENT;
576
577 BUG_ON(victim->d_parent->d_inode != dir);
578 audit_inode_child(victim, dir);
579
580 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
581 if (error)
582 return error;
583 if (IS_APPEND(dir))
584 return -EPERM;
585 if (btrfs_check_sticky(dir, victim->d_inode)||
586 IS_APPEND(victim->d_inode)||
587 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
588 return -EPERM;
589 if (isdir) {
590 if (!S_ISDIR(victim->d_inode->i_mode))
591 return -ENOTDIR;
592 if (IS_ROOT(victim))
593 return -EBUSY;
594 } else if (S_ISDIR(victim->d_inode->i_mode))
595 return -EISDIR;
596 if (IS_DEADDIR(dir))
597 return -ENOENT;
598 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
599 return -EBUSY;
600 return 0;
601}
602
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400603/* copy of may_create in fs/namei.c() */
604static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
605{
606 if (child->d_inode)
607 return -EEXIST;
608 if (IS_DEADDIR(dir))
609 return -ENOENT;
610 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
611}
612
613/*
614 * Create a new subvolume below @parent. This is largely modeled after
615 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
616 * inside this filesystem so it's quite a bit simpler.
617 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400618static noinline int btrfs_mksubvol(struct path *parent,
619 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400620 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800621 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400622{
Yan, Zheng76dda932009-09-21 16:00:26 -0400623 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400624 struct dentry *dentry;
625 int error;
626
Yan, Zheng76dda932009-09-21 16:00:26 -0400627 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400628
629 dentry = lookup_one_len(name, parent->dentry, namelen);
630 error = PTR_ERR(dentry);
631 if (IS_ERR(dentry))
632 goto out_unlock;
633
634 error = -EEXIST;
635 if (dentry->d_inode)
636 goto out_dput;
637
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400638 error = mnt_want_write(parent->mnt);
639 if (error)
640 goto out_dput;
641
Yan, Zheng76dda932009-09-21 16:00:26 -0400642 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400643 if (error)
644 goto out_drop_write;
645
Yan, Zheng76dda932009-09-21 16:00:26 -0400646 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
647
648 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
649 goto out_up_read;
650
Chris Mason3de45862008-11-17 21:02:50 -0500651 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400652 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800653 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500654 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400655 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400656 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500657 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400658 if (!error)
659 fsnotify_mkdir(dir, dentry);
660out_up_read:
661 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400662out_drop_write:
663 mnt_drop_write(parent->mnt);
664out_dput:
665 dput(dentry);
666out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400667 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400668 return error;
669}
670
Chris Mason4cb53002011-05-24 15:35:30 -0400671/*
672 * When we're defragging a range, we don't want to kick it off again
673 * if it is really just waiting for delalloc to send it down.
674 * If we find a nice big extent or delalloc range for the bytes in the
675 * file you want to defrag, we return 0 to let you know to skip this
676 * part of the file
677 */
678static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
679{
680 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
681 struct extent_map *em = NULL;
682 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
683 u64 end;
684
685 read_lock(&em_tree->lock);
686 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
687 read_unlock(&em_tree->lock);
688
689 if (em) {
690 end = extent_map_end(em);
691 free_extent_map(em);
692 if (end - offset > thresh)
693 return 0;
694 }
695 /* if we already have a nice delalloc here, just stop */
696 thresh /= 2;
697 end = count_range_bits(io_tree, &offset, offset + thresh,
698 thresh, EXTENT_DELALLOC, 1);
699 if (end >= thresh)
700 return 0;
701 return 1;
702}
703
704/*
705 * helper function to walk through a file and find extents
706 * newer than a specific transid, and smaller than thresh.
707 *
708 * This is used by the defragging code to find new and small
709 * extents
710 */
711static int find_new_extents(struct btrfs_root *root,
712 struct inode *inode, u64 newer_than,
713 u64 *off, int thresh)
714{
715 struct btrfs_path *path;
716 struct btrfs_key min_key;
717 struct btrfs_key max_key;
718 struct extent_buffer *leaf;
719 struct btrfs_file_extent_item *extent;
720 int type;
721 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000722 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400723
724 path = btrfs_alloc_path();
725 if (!path)
726 return -ENOMEM;
727
David Sterbaa4689d22011-05-31 17:08:14 +0000728 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400729 min_key.type = BTRFS_EXTENT_DATA_KEY;
730 min_key.offset = *off;
731
David Sterbaa4689d22011-05-31 17:08:14 +0000732 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400733 max_key.type = (u8)-1;
734 max_key.offset = (u64)-1;
735
736 path->keep_locks = 1;
737
738 while(1) {
739 ret = btrfs_search_forward(root, &min_key, &max_key,
740 path, 0, newer_than);
741 if (ret != 0)
742 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000743 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400744 goto none;
745 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
746 goto none;
747
748 leaf = path->nodes[0];
749 extent = btrfs_item_ptr(leaf, path->slots[0],
750 struct btrfs_file_extent_item);
751
752 type = btrfs_file_extent_type(leaf, extent);
753 if (type == BTRFS_FILE_EXTENT_REG &&
754 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
755 check_defrag_in_cache(inode, min_key.offset, thresh)) {
756 *off = min_key.offset;
757 btrfs_free_path(path);
758 return 0;
759 }
760
761 if (min_key.offset == (u64)-1)
762 goto none;
763
764 min_key.offset++;
765 btrfs_release_path(path);
766 }
767none:
768 btrfs_free_path(path);
769 return -ENOENT;
770}
771
Chris Mason940100a2010-03-10 10:52:59 -0500772static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500773 int thresh, u64 *last_len, u64 *skip,
774 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500775{
776 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
777 struct extent_map *em = NULL;
778 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
779 int ret = 1;
780
781 /*
Li Zefan008873e2011-09-02 15:57:07 +0800782 * make sure that once we start defragging an extent, we keep on
Chris Mason940100a2010-03-10 10:52:59 -0500783 * defragging it
784 */
785 if (start < *defrag_end)
786 return 1;
787
788 *skip = 0;
789
790 /*
791 * hopefully we have this extent in the tree already, try without
792 * the full extent lock
793 */
794 read_lock(&em_tree->lock);
795 em = lookup_extent_mapping(em_tree, start, len);
796 read_unlock(&em_tree->lock);
797
798 if (!em) {
799 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100800 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500801 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100802 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500803
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000804 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500805 return 0;
806 }
807
808 /* this will cover holes, and inline extents */
809 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
810 ret = 0;
811
812 /*
813 * we hit a real extent, if it is big don't bother defragging it again
814 */
Chris Mason1e701a32010-03-11 09:42:04 -0500815 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500816 ret = 0;
817
818 /*
819 * last_len ends up being a counter of how many bytes we've defragged.
820 * every time we choose not to defrag an extent, we reset *last_len
821 * so that the next tiny extent will force a defrag.
822 *
823 * The end result of this is that tiny extents before a single big
824 * extent will force at least part of that big extent to be defragged.
825 */
826 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500827 *defrag_end = extent_map_end(em);
828 } else {
829 *last_len = 0;
830 *skip = extent_map_end(em);
831 *defrag_end = 0;
832 }
833
834 free_extent_map(em);
835 return ret;
836}
837
Chris Mason4cb53002011-05-24 15:35:30 -0400838/*
839 * it doesn't do much good to defrag one or two pages
840 * at a time. This pulls in a nice chunk of pages
841 * to COW and defrag.
842 *
843 * It also makes sure the delalloc code has enough
844 * dirty data to avoid making new small extents as part
845 * of the defrag
846 *
847 * It's a good idea to start RA on this range
848 * before calling this.
849 */
850static int cluster_pages_for_defrag(struct inode *inode,
851 struct page **pages,
852 unsigned long start_index,
853 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400854{
Chris Mason4cb53002011-05-24 15:35:30 -0400855 unsigned long file_end;
856 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400857 u64 page_start;
858 u64 page_end;
Chris Mason4cb53002011-05-24 15:35:30 -0400859 int ret;
860 int i;
861 int i_done;
862 struct btrfs_ordered_extent *ordered;
863 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800864 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400865 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400866
867 if (isize == 0)
868 return 0;
869 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
870
871 ret = btrfs_delalloc_reserve_space(inode,
872 num_pages << PAGE_CACHE_SHIFT);
873 if (ret)
874 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400875 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800876 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400877
878 /* step one, lock all the pages */
879 for (i = 0; i < num_pages; i++) {
880 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800881again:
Josef Bacika94733d2011-07-11 10:47:06 -0400882 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800883 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400884 if (!page)
885 break;
886
Miao Xie600a45e2012-02-16 15:01:24 +0800887 page_start = page_offset(page);
888 page_end = page_start + PAGE_CACHE_SIZE - 1;
889 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100890 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800891 ordered = btrfs_lookup_ordered_extent(inode,
892 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100893 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800894 if (!ordered)
895 break;
896
897 unlock_page(page);
898 btrfs_start_ordered_extent(inode, ordered, 1);
899 btrfs_put_ordered_extent(ordered);
900 lock_page(page);
901 }
902
Chris Mason4cb53002011-05-24 15:35:30 -0400903 if (!PageUptodate(page)) {
904 btrfs_readpage(NULL, page);
905 lock_page(page);
906 if (!PageUptodate(page)) {
907 unlock_page(page);
908 page_cache_release(page);
909 ret = -EIO;
910 break;
911 }
912 }
Miao Xie600a45e2012-02-16 15:01:24 +0800913
Chris Mason4cb53002011-05-24 15:35:30 -0400914 isize = i_size_read(inode);
915 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Miao Xie600a45e2012-02-16 15:01:24 +0800916 if (!isize || page->index > file_end) {
Chris Mason4cb53002011-05-24 15:35:30 -0400917 /* whoops, we blew past eof, skip this page */
918 unlock_page(page);
919 page_cache_release(page);
920 break;
921 }
Miao Xie600a45e2012-02-16 15:01:24 +0800922
923 if (page->mapping != inode->i_mapping) {
924 unlock_page(page);
925 page_cache_release(page);
926 goto again;
927 }
928
Chris Mason4cb53002011-05-24 15:35:30 -0400929 pages[i] = page;
930 i_done++;
931 }
932 if (!i_done || ret)
933 goto out;
934
935 if (!(inode->i_sb->s_flags & MS_ACTIVE))
936 goto out;
937
938 /*
939 * so now we have a nice long stream of locked
940 * and up to date pages, lets wait on them
941 */
942 for (i = 0; i < i_done; i++)
943 wait_on_page_writeback(pages[i]);
944
945 page_start = page_offset(pages[0]);
946 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
947
948 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100949 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -0400950 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
951 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
952 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
953 GFP_NOFS);
954
955 if (i_done != num_pages) {
Josef Bacik9e0baf62011-07-15 15:16:44 +0000956 spin_lock(&BTRFS_I(inode)->lock);
957 BTRFS_I(inode)->outstanding_extents++;
958 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -0400959 btrfs_delalloc_release_space(inode,
960 (num_pages - i_done) << PAGE_CACHE_SHIFT);
961 }
962
963
964 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
965 &cached_state);
966
967 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
968 page_start, page_end - 1, &cached_state,
969 GFP_NOFS);
970
971 for (i = 0; i < i_done; i++) {
972 clear_page_dirty_for_io(pages[i]);
973 ClearPageChecked(pages[i]);
974 set_page_extent_mapped(pages[i]);
975 set_page_dirty(pages[i]);
976 unlock_page(pages[i]);
977 page_cache_release(pages[i]);
978 }
979 return i_done;
980out:
981 for (i = 0; i < i_done; i++) {
982 unlock_page(pages[i]);
983 page_cache_release(pages[i]);
984 }
985 btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
986 return ret;
987
988}
989
990int btrfs_defrag_file(struct inode *inode, struct file *file,
991 struct btrfs_ioctl_defrag_range_args *range,
992 u64 newer_than, unsigned long max_to_defrag)
993{
994 struct btrfs_root *root = BTRFS_I(inode)->root;
995 struct btrfs_super_block *disk_super;
996 struct file_ra_state *ra = NULL;
997 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +0800998 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400999 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -05001000 u64 last_len = 0;
1001 u64 skip = 0;
1002 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001003 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001004 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001005 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001006 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001007 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001008 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001009 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001010 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1011 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001012 u64 new_align = ~((u64)128 * 1024 - 1);
1013 struct page **pages = NULL;
1014
1015 if (extent_thresh == 0)
1016 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001017
1018 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1019 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1020 return -EINVAL;
1021 if (range->compress_type)
1022 compress_type = range->compress_type;
1023 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001024
Li Zefan151a31b2011-09-02 15:56:39 +08001025 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001026 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001027
Chris Mason4cb53002011-05-24 15:35:30 -04001028 /*
1029 * if we were not given a file, allocate a readahead
1030 * context
1031 */
1032 if (!file) {
1033 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1034 if (!ra)
1035 return -ENOMEM;
1036 file_ra_state_init(ra, inode->i_mapping);
1037 } else {
1038 ra = &file->f_ra;
1039 }
1040
Li Zefan008873e2011-09-02 15:57:07 +08001041 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001042 GFP_NOFS);
1043 if (!pages) {
1044 ret = -ENOMEM;
1045 goto out_ra;
1046 }
1047
1048 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001049 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001050 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001051 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1052 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001053 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001054 }
1055
Chris Mason4cb53002011-05-24 15:35:30 -04001056 if (newer_than) {
1057 ret = find_new_extents(root, inode, newer_than,
1058 &newer_off, 64 * 1024);
1059 if (!ret) {
1060 range->start = newer_off;
1061 /*
1062 * we always align our defrag to help keep
1063 * the extents in the file evenly spaced
1064 */
1065 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001066 } else
1067 goto out_ra;
1068 } else {
1069 i = range->start >> PAGE_CACHE_SHIFT;
1070 }
1071 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001072 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001073
Li Zefan2a0f7f52011-10-10 15:43:34 -04001074 /*
1075 * make writeback starts from i, so the defrag range can be
1076 * written sequentially.
1077 */
1078 if (i < inode->i_mapping->writeback_index)
1079 inode->i_mapping->writeback_index = i;
1080
Chris Masonf7f43cc2011-10-11 11:41:40 -04001081 while (i <= last_index && defrag_count < max_to_defrag &&
1082 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1083 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001084 /*
1085 * make sure we stop running if someone unmounts
1086 * the FS
1087 */
1088 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1089 break;
1090
1091 if (!newer_than &&
1092 !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -05001093 PAGE_CACHE_SIZE,
Chris Mason4cb53002011-05-24 15:35:30 -04001094 extent_thresh,
Chris Mason1e701a32010-03-11 09:42:04 -05001095 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -05001096 &defrag_end)) {
1097 unsigned long next;
1098 /*
1099 * the should_defrag function tells us how much to skip
1100 * bump our counter by the suggested amount
1101 */
1102 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1103 i = max(i + 1, next);
1104 continue;
1105 }
Li Zefan008873e2011-09-02 15:57:07 +08001106
1107 if (!newer_than) {
1108 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1109 PAGE_CACHE_SHIFT) - i;
1110 cluster = min(cluster, max_cluster);
1111 } else {
1112 cluster = max_cluster;
1113 }
1114
Chris Mason1e701a32010-03-11 09:42:04 -05001115 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001116 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001117
Li Zefan008873e2011-09-02 15:57:07 +08001118 if (i + cluster > ra_index) {
1119 ra_index = max(i, ra_index);
1120 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1121 cluster);
1122 ra_index += max_cluster;
1123 }
Chris Mason940100a2010-03-10 10:52:59 -05001124
Li Zefan008873e2011-09-02 15:57:07 +08001125 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Chris Mason4cb53002011-05-24 15:35:30 -04001126 if (ret < 0)
1127 goto out_ra;
Chris Mason940100a2010-03-10 10:52:59 -05001128
Chris Mason4cb53002011-05-24 15:35:30 -04001129 defrag_count += ret;
1130 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Chris Mason4cb53002011-05-24 15:35:30 -04001131
1132 if (newer_than) {
1133 if (newer_off == (u64)-1)
1134 break;
1135
1136 newer_off = max(newer_off + 1,
1137 (u64)i << PAGE_CACHE_SHIFT);
1138
1139 ret = find_new_extents(root, inode,
1140 newer_than, &newer_off,
1141 64 * 1024);
1142 if (!ret) {
1143 range->start = newer_off;
1144 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001145 } else {
1146 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001147 }
Chris Mason4cb53002011-05-24 15:35:30 -04001148 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001149 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001150 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001151 last_len += ret << PAGE_CACHE_SHIFT;
1152 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001153 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001154 last_len = 0;
1155 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001156 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001157 }
1158
Chris Mason1e701a32010-03-11 09:42:04 -05001159 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1160 filemap_flush(inode->i_mapping);
1161
1162 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1163 /* the filemap_flush will queue IO into the worker threads, but
1164 * we have to make sure the IO is actually started and that
1165 * ordered extents get created before we return
1166 */
1167 atomic_inc(&root->fs_info->async_submit_draining);
1168 while (atomic_read(&root->fs_info->nr_async_submits) ||
1169 atomic_read(&root->fs_info->async_delalloc_pages)) {
1170 wait_event(root->fs_info->async_submit_wait,
1171 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1172 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1173 }
1174 atomic_dec(&root->fs_info->async_submit_draining);
1175
1176 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001177 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001178 mutex_unlock(&inode->i_mutex);
1179 }
1180
David Sterba6c417612011-04-13 15:41:04 +02001181 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001182 features = btrfs_super_incompat_flags(disk_super);
1183 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1184 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1185 btrfs_set_super_incompat_flags(disk_super, features);
1186 }
1187
Diego Calleja60ccf822011-09-01 16:33:57 +02001188 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001189
Chris Mason4cb53002011-05-24 15:35:30 -04001190out_ra:
1191 if (!file)
1192 kfree(ra);
1193 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001194 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001195}
1196
Yan, Zheng76dda932009-09-21 16:00:26 -04001197static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1198 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001199{
1200 u64 new_size;
1201 u64 old_size;
1202 u64 devid = 1;
1203 struct btrfs_ioctl_vol_args *vol_args;
1204 struct btrfs_trans_handle *trans;
1205 struct btrfs_device *device = NULL;
1206 char *sizestr;
1207 char *devstr = NULL;
1208 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001209 int mod = 0;
1210
Yan Zhengc146afa2008-11-12 14:34:12 -05001211 if (root->fs_info->sb->s_flags & MS_RDONLY)
1212 return -EROFS;
1213
Chris Masone441d542009-01-05 16:57:23 -05001214 if (!capable(CAP_SYS_ADMIN))
1215 return -EPERM;
1216
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001217 mutex_lock(&root->fs_info->volume_mutex);
1218 if (root->fs_info->balance_ctl) {
1219 printk(KERN_INFO "btrfs: balance in progress\n");
1220 ret = -EINVAL;
1221 goto out;
1222 }
1223
Li Zefandae7b662009-04-08 15:06:54 +08001224 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001225 if (IS_ERR(vol_args)) {
1226 ret = PTR_ERR(vol_args);
1227 goto out;
1228 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001229
1230 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001231
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001232 sizestr = vol_args->name;
1233 devstr = strchr(sizestr, ':');
1234 if (devstr) {
1235 char *end;
1236 sizestr = devstr + 1;
1237 *devstr = '\0';
1238 devstr = vol_args->name;
1239 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001240 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001241 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001242 }
Yan Zheng2b820322008-11-17 21:11:30 -05001243 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001244 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001245 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001246 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001247 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001248 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001249 }
1250 if (!strcmp(sizestr, "max"))
1251 new_size = device->bdev->bd_inode->i_size;
1252 else {
1253 if (sizestr[0] == '-') {
1254 mod = -1;
1255 sizestr++;
1256 } else if (sizestr[0] == '+') {
1257 mod = 1;
1258 sizestr++;
1259 }
Akinobu Mita91748462010-02-28 10:59:11 +00001260 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001261 if (new_size == 0) {
1262 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001263 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001264 }
1265 }
1266
1267 old_size = device->total_bytes;
1268
1269 if (mod < 0) {
1270 if (new_size > old_size) {
1271 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001272 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001273 }
1274 new_size = old_size - new_size;
1275 } else if (mod > 0) {
1276 new_size = old_size + new_size;
1277 }
1278
1279 if (new_size < 256 * 1024 * 1024) {
1280 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001281 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001282 }
1283 if (new_size > device->bdev->bd_inode->i_size) {
1284 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001285 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001286 }
1287
1288 do_div(new_size, root->sectorsize);
1289 new_size *= root->sectorsize;
1290
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001291 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001292 device->name, (unsigned long long)new_size);
1293
1294 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001295 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001296 if (IS_ERR(trans)) {
1297 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001298 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001299 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001300 ret = btrfs_grow_device(trans, device, new_size);
1301 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001302 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001303 ret = btrfs_shrink_device(device, new_size);
1304 }
1305
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001306out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001307 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001308out:
1309 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001310 return ret;
1311}
1312
Sage Weil72fd0322010-10-29 15:41:32 -04001313static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1314 char *name,
1315 unsigned long fd,
1316 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001317 u64 *transid,
1318 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001319{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001320 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001321 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001322 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001323 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001324
Yan Zhengc146afa2008-11-12 14:34:12 -05001325 if (root->fs_info->sb->s_flags & MS_RDONLY)
1326 return -EROFS;
1327
Sage Weil72fd0322010-10-29 15:41:32 -04001328 namelen = strlen(name);
1329 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001330 ret = -EINVAL;
1331 goto out;
1332 }
1333
Chris Mason16780ca2012-02-20 22:14:55 -05001334 if (name[0] == '.' &&
1335 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1336 ret = -EEXIST;
1337 goto out;
1338 }
1339
Chris Mason3de45862008-11-17 21:02:50 -05001340 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001341 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001342 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001343 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001344 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001345 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001346 if (!src_file) {
1347 ret = -EINVAL;
1348 goto out;
1349 }
1350
1351 src_inode = src_file->f_path.dentry->d_inode;
1352 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001353 printk(KERN_INFO "btrfs: Snapshot src from "
1354 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001355 ret = -EINVAL;
1356 fput(src_file);
1357 goto out;
1358 }
Sage Weil72fd0322010-10-29 15:41:32 -04001359 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1360 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001361 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001362 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001363 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001364out:
Sage Weil72fd0322010-10-29 15:41:32 -04001365 return ret;
1366}
1367
1368static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001369 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001370{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001371 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001372 int ret;
1373
Li Zefanfa0d2b92010-12-20 15:53:28 +08001374 vol_args = memdup_user(arg, sizeof(*vol_args));
1375 if (IS_ERR(vol_args))
1376 return PTR_ERR(vol_args);
1377 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001378
Li Zefanfa0d2b92010-12-20 15:53:28 +08001379 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001380 vol_args->fd, subvol,
1381 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001382
Li Zefanfa0d2b92010-12-20 15:53:28 +08001383 kfree(vol_args);
1384 return ret;
1385}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001386
Li Zefanfa0d2b92010-12-20 15:53:28 +08001387static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1388 void __user *arg, int subvol)
1389{
1390 struct btrfs_ioctl_vol_args_v2 *vol_args;
1391 int ret;
1392 u64 transid = 0;
1393 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001394 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001395
Li Zefanfa0d2b92010-12-20 15:53:28 +08001396 vol_args = memdup_user(arg, sizeof(*vol_args));
1397 if (IS_ERR(vol_args))
1398 return PTR_ERR(vol_args);
1399 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001400
Li Zefanb83cc962010-12-20 16:04:08 +08001401 if (vol_args->flags &
1402 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1403 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001404 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001405 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001406
1407 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1408 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001409 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1410 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001411
1412 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001413 vol_args->fd, subvol,
1414 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001415
1416 if (ret == 0 && ptr &&
1417 copy_to_user(arg +
1418 offsetof(struct btrfs_ioctl_vol_args_v2,
1419 transid), ptr, sizeof(*ptr)))
1420 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001421out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001422 kfree(vol_args);
1423 return ret;
1424}
1425
Li Zefan0caa1022010-12-20 16:30:25 +08001426static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1427 void __user *arg)
1428{
1429 struct inode *inode = fdentry(file)->d_inode;
1430 struct btrfs_root *root = BTRFS_I(inode)->root;
1431 int ret = 0;
1432 u64 flags = 0;
1433
Li Zefan33345d012011-04-20 10:31:50 +08001434 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001435 return -EINVAL;
1436
1437 down_read(&root->fs_info->subvol_sem);
1438 if (btrfs_root_readonly(root))
1439 flags |= BTRFS_SUBVOL_RDONLY;
1440 up_read(&root->fs_info->subvol_sem);
1441
1442 if (copy_to_user(arg, &flags, sizeof(flags)))
1443 ret = -EFAULT;
1444
1445 return ret;
1446}
1447
1448static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1449 void __user *arg)
1450{
1451 struct inode *inode = fdentry(file)->d_inode;
1452 struct btrfs_root *root = BTRFS_I(inode)->root;
1453 struct btrfs_trans_handle *trans;
1454 u64 root_flags;
1455 u64 flags;
1456 int ret = 0;
1457
1458 if (root->fs_info->sb->s_flags & MS_RDONLY)
1459 return -EROFS;
1460
Li Zefan33345d012011-04-20 10:31:50 +08001461 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001462 return -EINVAL;
1463
1464 if (copy_from_user(&flags, arg, sizeof(flags)))
1465 return -EFAULT;
1466
Li Zefanb4dc2b82011-02-16 06:06:34 +00001467 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001468 return -EINVAL;
1469
1470 if (flags & ~BTRFS_SUBVOL_RDONLY)
1471 return -EOPNOTSUPP;
1472
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001473 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001474 return -EACCES;
1475
Li Zefan0caa1022010-12-20 16:30:25 +08001476 down_write(&root->fs_info->subvol_sem);
1477
1478 /* nothing to do */
1479 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1480 goto out;
1481
1482 root_flags = btrfs_root_flags(&root->root_item);
1483 if (flags & BTRFS_SUBVOL_RDONLY)
1484 btrfs_set_root_flags(&root->root_item,
1485 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1486 else
1487 btrfs_set_root_flags(&root->root_item,
1488 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1489
1490 trans = btrfs_start_transaction(root, 1);
1491 if (IS_ERR(trans)) {
1492 ret = PTR_ERR(trans);
1493 goto out_reset;
1494 }
1495
Li Zefanb4dc2b82011-02-16 06:06:34 +00001496 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001497 &root->root_key, &root->root_item);
1498
1499 btrfs_commit_transaction(trans, root);
1500out_reset:
1501 if (ret)
1502 btrfs_set_root_flags(&root->root_item, root_flags);
1503out:
1504 up_write(&root->fs_info->subvol_sem);
1505 return ret;
1506}
1507
Yan, Zheng76dda932009-09-21 16:00:26 -04001508/*
1509 * helper to check if the subvolume references other subvolumes
1510 */
1511static noinline int may_destroy_subvol(struct btrfs_root *root)
1512{
1513 struct btrfs_path *path;
1514 struct btrfs_key key;
1515 int ret;
1516
1517 path = btrfs_alloc_path();
1518 if (!path)
1519 return -ENOMEM;
1520
1521 key.objectid = root->root_key.objectid;
1522 key.type = BTRFS_ROOT_REF_KEY;
1523 key.offset = (u64)-1;
1524
1525 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1526 &key, path, 0, 0);
1527 if (ret < 0)
1528 goto out;
1529 BUG_ON(ret == 0);
1530
1531 ret = 0;
1532 if (path->slots[0] > 0) {
1533 path->slots[0]--;
1534 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1535 if (key.objectid == root->root_key.objectid &&
1536 key.type == BTRFS_ROOT_REF_KEY)
1537 ret = -ENOTEMPTY;
1538 }
1539out:
1540 btrfs_free_path(path);
1541 return ret;
1542}
1543
Chris Masonac8e9812010-02-28 15:39:26 -05001544static noinline int key_in_sk(struct btrfs_key *key,
1545 struct btrfs_ioctl_search_key *sk)
1546{
Chris Masonabc6e132010-03-18 12:10:08 -04001547 struct btrfs_key test;
1548 int ret;
1549
1550 test.objectid = sk->min_objectid;
1551 test.type = sk->min_type;
1552 test.offset = sk->min_offset;
1553
1554 ret = btrfs_comp_cpu_keys(key, &test);
1555 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001556 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001557
1558 test.objectid = sk->max_objectid;
1559 test.type = sk->max_type;
1560 test.offset = sk->max_offset;
1561
1562 ret = btrfs_comp_cpu_keys(key, &test);
1563 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001564 return 0;
1565 return 1;
1566}
1567
1568static noinline int copy_to_sk(struct btrfs_root *root,
1569 struct btrfs_path *path,
1570 struct btrfs_key *key,
1571 struct btrfs_ioctl_search_key *sk,
1572 char *buf,
1573 unsigned long *sk_offset,
1574 int *num_found)
1575{
1576 u64 found_transid;
1577 struct extent_buffer *leaf;
1578 struct btrfs_ioctl_search_header sh;
1579 unsigned long item_off;
1580 unsigned long item_len;
1581 int nritems;
1582 int i;
1583 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001584 int ret = 0;
1585
1586 leaf = path->nodes[0];
1587 slot = path->slots[0];
1588 nritems = btrfs_header_nritems(leaf);
1589
1590 if (btrfs_header_generation(leaf) > sk->max_transid) {
1591 i = nritems;
1592 goto advance_key;
1593 }
1594 found_transid = btrfs_header_generation(leaf);
1595
1596 for (i = slot; i < nritems; i++) {
1597 item_off = btrfs_item_ptr_offset(leaf, i);
1598 item_len = btrfs_item_size_nr(leaf, i);
1599
1600 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1601 item_len = 0;
1602
1603 if (sizeof(sh) + item_len + *sk_offset >
1604 BTRFS_SEARCH_ARGS_BUFSIZE) {
1605 ret = 1;
1606 goto overflow;
1607 }
1608
1609 btrfs_item_key_to_cpu(leaf, key, i);
1610 if (!key_in_sk(key, sk))
1611 continue;
1612
1613 sh.objectid = key->objectid;
1614 sh.offset = key->offset;
1615 sh.type = key->type;
1616 sh.len = item_len;
1617 sh.transid = found_transid;
1618
1619 /* copy search result header */
1620 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1621 *sk_offset += sizeof(sh);
1622
1623 if (item_len) {
1624 char *p = buf + *sk_offset;
1625 /* copy the item */
1626 read_extent_buffer(leaf, p,
1627 item_off, item_len);
1628 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001629 }
Hugo Millse2156862011-05-14 17:43:41 +00001630 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001631
1632 if (*num_found >= sk->nr_items)
1633 break;
1634 }
1635advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001636 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001637 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1638 key->offset++;
1639 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1640 key->offset = 0;
1641 key->type++;
1642 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1643 key->offset = 0;
1644 key->type = 0;
1645 key->objectid++;
1646 } else
1647 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001648overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001649 return ret;
1650}
1651
1652static noinline int search_ioctl(struct inode *inode,
1653 struct btrfs_ioctl_search_args *args)
1654{
1655 struct btrfs_root *root;
1656 struct btrfs_key key;
1657 struct btrfs_key max_key;
1658 struct btrfs_path *path;
1659 struct btrfs_ioctl_search_key *sk = &args->key;
1660 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1661 int ret;
1662 int num_found = 0;
1663 unsigned long sk_offset = 0;
1664
1665 path = btrfs_alloc_path();
1666 if (!path)
1667 return -ENOMEM;
1668
1669 if (sk->tree_id == 0) {
1670 /* search the root of the inode that was passed */
1671 root = BTRFS_I(inode)->root;
1672 } else {
1673 key.objectid = sk->tree_id;
1674 key.type = BTRFS_ROOT_ITEM_KEY;
1675 key.offset = (u64)-1;
1676 root = btrfs_read_fs_root_no_name(info, &key);
1677 if (IS_ERR(root)) {
1678 printk(KERN_ERR "could not find root %llu\n",
1679 sk->tree_id);
1680 btrfs_free_path(path);
1681 return -ENOENT;
1682 }
1683 }
1684
1685 key.objectid = sk->min_objectid;
1686 key.type = sk->min_type;
1687 key.offset = sk->min_offset;
1688
1689 max_key.objectid = sk->max_objectid;
1690 max_key.type = sk->max_type;
1691 max_key.offset = sk->max_offset;
1692
1693 path->keep_locks = 1;
1694
1695 while(1) {
1696 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1697 sk->min_transid);
1698 if (ret != 0) {
1699 if (ret > 0)
1700 ret = 0;
1701 goto err;
1702 }
1703 ret = copy_to_sk(root, path, &key, sk, args->buf,
1704 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001705 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001706 if (ret || num_found >= sk->nr_items)
1707 break;
1708
1709 }
1710 ret = 0;
1711err:
1712 sk->nr_items = num_found;
1713 btrfs_free_path(path);
1714 return ret;
1715}
1716
1717static noinline int btrfs_ioctl_tree_search(struct file *file,
1718 void __user *argp)
1719{
1720 struct btrfs_ioctl_search_args *args;
1721 struct inode *inode;
1722 int ret;
1723
1724 if (!capable(CAP_SYS_ADMIN))
1725 return -EPERM;
1726
Julia Lawall2354d082010-10-29 15:14:18 -04001727 args = memdup_user(argp, sizeof(*args));
1728 if (IS_ERR(args))
1729 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001730
Chris Masonac8e9812010-02-28 15:39:26 -05001731 inode = fdentry(file)->d_inode;
1732 ret = search_ioctl(inode, args);
1733 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1734 ret = -EFAULT;
1735 kfree(args);
1736 return ret;
1737}
1738
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001739/*
Chris Masonac8e9812010-02-28 15:39:26 -05001740 * Search INODE_REFs to identify path name of 'dirid' directory
1741 * in a 'tree_id' tree. and sets path name to 'name'.
1742 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001743static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1744 u64 tree_id, u64 dirid, char *name)
1745{
1746 struct btrfs_root *root;
1747 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001748 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001749 int ret = -1;
1750 int slot;
1751 int len;
1752 int total_len = 0;
1753 struct btrfs_inode_ref *iref;
1754 struct extent_buffer *l;
1755 struct btrfs_path *path;
1756
1757 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1758 name[0]='\0';
1759 return 0;
1760 }
1761
1762 path = btrfs_alloc_path();
1763 if (!path)
1764 return -ENOMEM;
1765
Chris Masonac8e9812010-02-28 15:39:26 -05001766 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001767
1768 key.objectid = tree_id;
1769 key.type = BTRFS_ROOT_ITEM_KEY;
1770 key.offset = (u64)-1;
1771 root = btrfs_read_fs_root_no_name(info, &key);
1772 if (IS_ERR(root)) {
1773 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001774 ret = -ENOENT;
1775 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001776 }
1777
1778 key.objectid = dirid;
1779 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001780 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001781
1782 while(1) {
1783 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1784 if (ret < 0)
1785 goto out;
1786
1787 l = path->nodes[0];
1788 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001789 if (ret > 0 && slot > 0)
1790 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001791 btrfs_item_key_to_cpu(l, &key, slot);
1792
1793 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001794 key.type != BTRFS_INODE_REF_KEY)) {
1795 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001796 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001797 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001798
1799 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1800 len = btrfs_inode_ref_name_len(l, iref);
1801 ptr -= len + 1;
1802 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001803 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001804 goto out;
1805
1806 *(ptr + len) = '/';
1807 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1808
1809 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1810 break;
1811
David Sterbab3b4aa72011-04-21 01:20:15 +02001812 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001813 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001814 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001815 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001816 }
Chris Masonac8e9812010-02-28 15:39:26 -05001817 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001818 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001819 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001820 name[total_len]='\0';
1821 ret = 0;
1822out:
1823 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001824 return ret;
1825}
1826
1827static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1828 void __user *argp)
1829{
1830 struct btrfs_ioctl_ino_lookup_args *args;
1831 struct inode *inode;
1832 int ret;
1833
1834 if (!capable(CAP_SYS_ADMIN))
1835 return -EPERM;
1836
Julia Lawall2354d082010-10-29 15:14:18 -04001837 args = memdup_user(argp, sizeof(*args));
1838 if (IS_ERR(args))
1839 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001840
Chris Masonac8e9812010-02-28 15:39:26 -05001841 inode = fdentry(file)->d_inode;
1842
Chris Mason1b53ac42010-03-18 12:17:05 -04001843 if (args->treeid == 0)
1844 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1845
Chris Masonac8e9812010-02-28 15:39:26 -05001846 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1847 args->treeid, args->objectid,
1848 args->name);
1849
1850 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1851 ret = -EFAULT;
1852
1853 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001854 return ret;
1855}
1856
Yan, Zheng76dda932009-09-21 16:00:26 -04001857static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1858 void __user *arg)
1859{
1860 struct dentry *parent = fdentry(file);
1861 struct dentry *dentry;
1862 struct inode *dir = parent->d_inode;
1863 struct inode *inode;
1864 struct btrfs_root *root = BTRFS_I(dir)->root;
1865 struct btrfs_root *dest = NULL;
1866 struct btrfs_ioctl_vol_args *vol_args;
1867 struct btrfs_trans_handle *trans;
1868 int namelen;
1869 int ret;
1870 int err = 0;
1871
Yan, Zheng76dda932009-09-21 16:00:26 -04001872 vol_args = memdup_user(arg, sizeof(*vol_args));
1873 if (IS_ERR(vol_args))
1874 return PTR_ERR(vol_args);
1875
1876 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1877 namelen = strlen(vol_args->name);
1878 if (strchr(vol_args->name, '/') ||
1879 strncmp(vol_args->name, "..", namelen) == 0) {
1880 err = -EINVAL;
1881 goto out;
1882 }
1883
Al Viroa561be72011-11-23 11:57:51 -05001884 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001885 if (err)
1886 goto out;
1887
1888 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1889 dentry = lookup_one_len(vol_args->name, parent, namelen);
1890 if (IS_ERR(dentry)) {
1891 err = PTR_ERR(dentry);
1892 goto out_unlock_dir;
1893 }
1894
1895 if (!dentry->d_inode) {
1896 err = -ENOENT;
1897 goto out_dput;
1898 }
1899
1900 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001901 dest = BTRFS_I(inode)->root;
1902 if (!capable(CAP_SYS_ADMIN)){
1903 /*
1904 * Regular user. Only allow this with a special mount
1905 * option, when the user has write+exec access to the
1906 * subvol root, and when rmdir(2) would have been
1907 * allowed.
1908 *
1909 * Note that this is _not_ check that the subvol is
1910 * empty or doesn't contain data that we wouldn't
1911 * otherwise be able to delete.
1912 *
1913 * Users who want to delete empty subvols should try
1914 * rmdir(2).
1915 */
1916 err = -EPERM;
1917 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1918 goto out_dput;
1919
1920 /*
1921 * Do not allow deletion if the parent dir is the same
1922 * as the dir to be deleted. That means the ioctl
1923 * must be called on the dentry referencing the root
1924 * of the subvol, not a random directory contained
1925 * within it.
1926 */
1927 err = -EINVAL;
1928 if (root == dest)
1929 goto out_dput;
1930
1931 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1932 if (err)
1933 goto out_dput;
1934
1935 /* check if subvolume may be deleted by a non-root user */
1936 err = btrfs_may_delete(dir, dentry, 1);
1937 if (err)
1938 goto out_dput;
1939 }
1940
Li Zefan33345d012011-04-20 10:31:50 +08001941 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04001942 err = -EINVAL;
1943 goto out_dput;
1944 }
1945
Yan, Zheng76dda932009-09-21 16:00:26 -04001946 mutex_lock(&inode->i_mutex);
1947 err = d_invalidate(dentry);
1948 if (err)
1949 goto out_unlock;
1950
1951 down_write(&root->fs_info->subvol_sem);
1952
1953 err = may_destroy_subvol(dest);
1954 if (err)
1955 goto out_up_write;
1956
Yan, Zhenga22285a2010-05-16 10:48:46 -04001957 trans = btrfs_start_transaction(root, 0);
1958 if (IS_ERR(trans)) {
1959 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001960 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001961 }
1962 trans->block_rsv = &root->fs_info->global_block_rsv;
1963
Yan, Zheng76dda932009-09-21 16:00:26 -04001964 ret = btrfs_unlink_subvol(trans, root, dir,
1965 dest->root_key.objectid,
1966 dentry->d_name.name,
1967 dentry->d_name.len);
1968 BUG_ON(ret);
1969
1970 btrfs_record_root_in_trans(trans, dest);
1971
1972 memset(&dest->root_item.drop_progress, 0,
1973 sizeof(dest->root_item.drop_progress));
1974 dest->root_item.drop_level = 0;
1975 btrfs_set_root_refs(&dest->root_item, 0);
1976
Yan, Zhengd68fc572010-05-16 10:49:58 -04001977 if (!xchg(&dest->orphan_item_inserted, 1)) {
1978 ret = btrfs_insert_orphan_item(trans,
1979 root->fs_info->tree_root,
1980 dest->root_key.objectid);
1981 BUG_ON(ret);
1982 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001983
Sage Weil531cb132010-10-29 15:41:32 -04001984 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001985 BUG_ON(ret);
1986 inode->i_flags |= S_DEAD;
1987out_up_write:
1988 up_write(&root->fs_info->subvol_sem);
1989out_unlock:
1990 mutex_unlock(&inode->i_mutex);
1991 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001992 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001993 btrfs_invalidate_inodes(dest);
1994 d_delete(dentry);
1995 }
1996out_dput:
1997 dput(dentry);
1998out_unlock_dir:
1999 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002000 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002001out:
2002 kfree(vol_args);
2003 return err;
2004}
2005
Chris Mason1e701a32010-03-11 09:42:04 -05002006static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002007{
2008 struct inode *inode = fdentry(file)->d_inode;
2009 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002010 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002011 int ret;
2012
Li Zefanb83cc962010-12-20 16:04:08 +08002013 if (btrfs_root_readonly(root))
2014 return -EROFS;
2015
Al Viroa561be72011-11-23 11:57:51 -05002016 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002017 if (ret)
2018 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002019
2020 switch (inode->i_mode & S_IFMT) {
2021 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002022 if (!capable(CAP_SYS_ADMIN)) {
2023 ret = -EPERM;
2024 goto out;
2025 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002026 ret = btrfs_defrag_root(root, 0);
2027 if (ret)
2028 goto out;
2029 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002030 break;
2031 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002032 if (!(file->f_mode & FMODE_WRITE)) {
2033 ret = -EINVAL;
2034 goto out;
2035 }
Chris Mason1e701a32010-03-11 09:42:04 -05002036
2037 range = kzalloc(sizeof(*range), GFP_KERNEL);
2038 if (!range) {
2039 ret = -ENOMEM;
2040 goto out;
2041 }
2042
2043 if (argp) {
2044 if (copy_from_user(range, argp,
2045 sizeof(*range))) {
2046 ret = -EFAULT;
2047 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002048 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002049 }
2050 /* compression requires us to start the IO */
2051 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2052 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2053 range->extent_thresh = (u32)-1;
2054 }
2055 } else {
2056 /* the rest are all set to zero by kzalloc */
2057 range->len = (u64)-1;
2058 }
Chris Mason4cb53002011-05-24 15:35:30 -04002059 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2060 range, 0, 0);
2061 if (ret > 0)
2062 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002063 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002064 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002065 default:
2066 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002067 }
Chris Masone441d542009-01-05 16:57:23 -05002068out:
Al Viro2a79f172011-12-09 08:06:57 -05002069 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002070 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002071}
2072
Christoph Hellwigb2950862008-12-02 09:54:17 -05002073static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002074{
2075 struct btrfs_ioctl_vol_args *vol_args;
2076 int ret;
2077
Chris Masone441d542009-01-05 16:57:23 -05002078 if (!capable(CAP_SYS_ADMIN))
2079 return -EPERM;
2080
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002081 mutex_lock(&root->fs_info->volume_mutex);
2082 if (root->fs_info->balance_ctl) {
2083 printk(KERN_INFO "btrfs: balance in progress\n");
2084 ret = -EINVAL;
2085 goto out;
2086 }
2087
Li Zefandae7b662009-04-08 15:06:54 +08002088 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002089 if (IS_ERR(vol_args)) {
2090 ret = PTR_ERR(vol_args);
2091 goto out;
2092 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002093
Mark Fasheh5516e592008-07-24 12:20:14 -04002094 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002095 ret = btrfs_init_new_device(root, vol_args->name);
2096
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002097 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002098out:
2099 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002100 return ret;
2101}
2102
Christoph Hellwigb2950862008-12-02 09:54:17 -05002103static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002104{
2105 struct btrfs_ioctl_vol_args *vol_args;
2106 int ret;
2107
Chris Masone441d542009-01-05 16:57:23 -05002108 if (!capable(CAP_SYS_ADMIN))
2109 return -EPERM;
2110
Yan Zhengc146afa2008-11-12 14:34:12 -05002111 if (root->fs_info->sb->s_flags & MS_RDONLY)
2112 return -EROFS;
2113
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002114 mutex_lock(&root->fs_info->volume_mutex);
2115 if (root->fs_info->balance_ctl) {
2116 printk(KERN_INFO "btrfs: balance in progress\n");
2117 ret = -EINVAL;
2118 goto out;
2119 }
2120
Li Zefandae7b662009-04-08 15:06:54 +08002121 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002122 if (IS_ERR(vol_args)) {
2123 ret = PTR_ERR(vol_args);
2124 goto out;
2125 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002126
Mark Fasheh5516e592008-07-24 12:20:14 -04002127 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002128 ret = btrfs_rm_device(root, vol_args->name);
2129
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002130 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002131out:
2132 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002133 return ret;
2134}
2135
Jan Schmidt475f6382011-03-11 15:41:01 +01002136static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2137{
Li Zefan027ed2f2011-06-08 08:27:56 +00002138 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002139 struct btrfs_device *device;
2140 struct btrfs_device *next;
2141 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002142 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002143
2144 if (!capable(CAP_SYS_ADMIN))
2145 return -EPERM;
2146
Li Zefan027ed2f2011-06-08 08:27:56 +00002147 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2148 if (!fi_args)
2149 return -ENOMEM;
2150
2151 fi_args->num_devices = fs_devices->num_devices;
2152 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002153
2154 mutex_lock(&fs_devices->device_list_mutex);
2155 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002156 if (device->devid > fi_args->max_id)
2157 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002158 }
2159 mutex_unlock(&fs_devices->device_list_mutex);
2160
Li Zefan027ed2f2011-06-08 08:27:56 +00002161 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2162 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002163
Li Zefan027ed2f2011-06-08 08:27:56 +00002164 kfree(fi_args);
2165 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002166}
2167
2168static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2169{
2170 struct btrfs_ioctl_dev_info_args *di_args;
2171 struct btrfs_device *dev;
2172 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2173 int ret = 0;
2174 char *s_uuid = NULL;
2175 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2176
2177 if (!capable(CAP_SYS_ADMIN))
2178 return -EPERM;
2179
2180 di_args = memdup_user(arg, sizeof(*di_args));
2181 if (IS_ERR(di_args))
2182 return PTR_ERR(di_args);
2183
2184 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2185 s_uuid = di_args->uuid;
2186
2187 mutex_lock(&fs_devices->device_list_mutex);
2188 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2189 mutex_unlock(&fs_devices->device_list_mutex);
2190
2191 if (!dev) {
2192 ret = -ENODEV;
2193 goto out;
2194 }
2195
2196 di_args->devid = dev->devid;
2197 di_args->bytes_used = dev->bytes_used;
2198 di_args->total_bytes = dev->total_bytes;
2199 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2200 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2201
2202out:
2203 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2204 ret = -EFAULT;
2205
2206 kfree(di_args);
2207 return ret;
2208}
2209
Yan, Zheng76dda932009-09-21 16:00:26 -04002210static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2211 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002212{
2213 struct inode *inode = fdentry(file)->d_inode;
2214 struct btrfs_root *root = BTRFS_I(inode)->root;
2215 struct file *src_file;
2216 struct inode *src;
2217 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002218 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002219 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002220 char *buf;
2221 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002222 u32 nritems;
2223 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002224 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002225 u64 len = olen;
2226 u64 bs = root->fs_info->sb->s_blocksize;
2227 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002228
Sage Weilc5c9cd42008-11-12 14:32:25 -05002229 /*
2230 * TODO:
2231 * - split compressed inline extents. annoying: we need to
2232 * decompress into destination's address_space (the file offset
2233 * may change, so source mapping won't do), then recompress (or
2234 * otherwise reinsert) a subrange.
2235 * - allow ranges within the same file to be cloned (provided
2236 * they don't overlap)?
2237 */
2238
Chris Masone441d542009-01-05 16:57:23 -05002239 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002240 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002241 return -EINVAL;
2242
Li Zefanb83cc962010-12-20 16:04:08 +08002243 if (btrfs_root_readonly(root))
2244 return -EROFS;
2245
Al Viroa561be72011-11-23 11:57:51 -05002246 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002247 if (ret)
2248 return ret;
2249
Sage Weilc5c9cd42008-11-12 14:32:25 -05002250 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002251 if (!src_file) {
2252 ret = -EBADF;
2253 goto out_drop_write;
2254 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002255
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002256 src = src_file->f_dentry->d_inode;
2257
Sage Weilc5c9cd42008-11-12 14:32:25 -05002258 ret = -EINVAL;
2259 if (src == inode)
2260 goto out_fput;
2261
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002262 /* the src must be open for reading */
2263 if (!(src_file->f_mode & FMODE_READ))
2264 goto out_fput;
2265
Li Zefan0e7b8242011-09-18 10:20:46 -04002266 /* don't make the dst file partly checksummed */
2267 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2268 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2269 goto out_fput;
2270
Yan Zhengae01a0a2008-08-04 23:23:47 -04002271 ret = -EISDIR;
2272 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002273 goto out_fput;
2274
Yan Zhengae01a0a2008-08-04 23:23:47 -04002275 ret = -EXDEV;
2276 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2277 goto out_fput;
2278
2279 ret = -ENOMEM;
2280 buf = vmalloc(btrfs_level_size(root, 0));
2281 if (!buf)
2282 goto out_fput;
2283
2284 path = btrfs_alloc_path();
2285 if (!path) {
2286 vfree(buf);
2287 goto out_fput;
2288 }
2289 path->reada = 2;
2290
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002291 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002292 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2293 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002294 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002295 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2296 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002297 }
2298
Sage Weilc5c9cd42008-11-12 14:32:25 -05002299 /* determine range to clone */
2300 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002301 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002302 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002303 if (len == 0)
2304 olen = len = src->i_size - off;
2305 /* if we extend to eof, continue to block boundary */
2306 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002307 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002308
2309 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002310 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2311 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002312 goto out_unlock;
2313
Li Zefand525e8a2011-09-11 10:52:25 -04002314 if (destoff > inode->i_size) {
2315 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2316 if (ret)
2317 goto out_unlock;
2318 }
2319
Li Zefan71ef0782011-09-18 10:20:46 -04002320 /* truncate page cache pages from target inode range */
2321 truncate_inode_pages_range(&inode->i_data, destoff,
2322 PAGE_CACHE_ALIGN(destoff + len) - 1);
2323
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002324 /* do any pending delalloc/csum calc on src, one way or
2325 another, and lock file content */
2326 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002327 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002328 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002329 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2330 if (!ordered &&
2331 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2332 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002333 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002334 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002335 if (ordered)
2336 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002337 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002338 }
2339
Sage Weilc5c9cd42008-11-12 14:32:25 -05002340 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002341 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002342 key.type = BTRFS_EXTENT_DATA_KEY;
2343 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002344
2345 while (1) {
2346 /*
2347 * note the key will change type as we walk through the
2348 * tree.
2349 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002350 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002351 if (ret < 0)
2352 goto out;
2353
Yan Zhengae01a0a2008-08-04 23:23:47 -04002354 nritems = btrfs_header_nritems(path->nodes[0]);
2355 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002356 ret = btrfs_next_leaf(root, path);
2357 if (ret < 0)
2358 goto out;
2359 if (ret > 0)
2360 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002361 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002362 }
2363 leaf = path->nodes[0];
2364 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002365
Yan Zhengae01a0a2008-08-04 23:23:47 -04002366 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002367 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002368 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002369 break;
2370
Sage Weilc5c9cd42008-11-12 14:32:25 -05002371 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2372 struct btrfs_file_extent_item *extent;
2373 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002374 u32 size;
2375 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002376 u64 disko = 0, diskl = 0;
2377 u64 datao = 0, datal = 0;
2378 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002379 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002380
2381 size = btrfs_item_size_nr(leaf, slot);
2382 read_extent_buffer(leaf, buf,
2383 btrfs_item_ptr_offset(leaf, slot),
2384 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002385
2386 extent = btrfs_item_ptr(leaf, slot,
2387 struct btrfs_file_extent_item);
2388 comp = btrfs_file_extent_compression(leaf, extent);
2389 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002390 if (type == BTRFS_FILE_EXTENT_REG ||
2391 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002392 disko = btrfs_file_extent_disk_bytenr(leaf,
2393 extent);
2394 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2395 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002396 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002397 datal = btrfs_file_extent_num_bytes(leaf,
2398 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002399 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2400 /* take upper bound, may be compressed */
2401 datal = btrfs_file_extent_ram_bytes(leaf,
2402 extent);
2403 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002404 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002405
Sage Weil050006a2010-10-29 15:37:33 -04002406 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002407 key.offset >= off+len)
2408 goto next;
2409
Zheng Yan31840ae2008-09-23 13:14:14 -04002410 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002411 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002412 if (off <= key.offset)
2413 new_key.offset = key.offset + destoff - off;
2414 else
2415 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002416
Sage Weilb6f34092011-09-20 14:48:51 -04002417 /*
2418 * 1 - adjusting old extent (we may have to split it)
2419 * 1 - add new extent
2420 * 1 - inode update
2421 */
2422 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002423 if (IS_ERR(trans)) {
2424 ret = PTR_ERR(trans);
2425 goto out;
2426 }
2427
Chris Masonc8a894d2009-06-27 21:07:03 -04002428 if (type == BTRFS_FILE_EXTENT_REG ||
2429 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002430 /*
2431 * a | --- range to clone ---| b
2432 * | ------------- extent ------------- |
2433 */
2434
2435 /* substract range b */
2436 if (key.offset + datal > off + len)
2437 datal = off + len - key.offset;
2438
2439 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002440 if (off > key.offset) {
2441 datao += off - key.offset;
2442 datal -= off - key.offset;
2443 }
2444
Yan, Zhenga22285a2010-05-16 10:48:46 -04002445 ret = btrfs_drop_extents(trans, inode,
2446 new_key.offset,
2447 new_key.offset + datal,
2448 &hint_byte, 1);
2449 BUG_ON(ret);
2450
Sage Weilc5c9cd42008-11-12 14:32:25 -05002451 ret = btrfs_insert_empty_item(trans, root, path,
2452 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002453 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002454
2455 leaf = path->nodes[0];
2456 slot = path->slots[0];
2457 write_extent_buffer(leaf, buf,
2458 btrfs_item_ptr_offset(leaf, slot),
2459 size);
2460
2461 extent = btrfs_item_ptr(leaf, slot,
2462 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002463
Sage Weilc5c9cd42008-11-12 14:32:25 -05002464 /* disko == 0 means it's a hole */
2465 if (!disko)
2466 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002467
2468 btrfs_set_file_extent_offset(leaf, extent,
2469 datao);
2470 btrfs_set_file_extent_num_bytes(leaf, extent,
2471 datal);
2472 if (disko) {
2473 inode_add_bytes(inode, datal);
2474 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002475 disko, diskl, 0,
2476 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002477 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002478 new_key.offset - datao,
2479 0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002480 BUG_ON(ret);
2481 }
2482 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2483 u64 skip = 0;
2484 u64 trim = 0;
2485 if (off > key.offset) {
2486 skip = off - key.offset;
2487 new_key.offset += skip;
2488 }
Chris Masond3977122009-01-05 21:25:51 -05002489
Sage Weilc5c9cd42008-11-12 14:32:25 -05002490 if (key.offset + datal > off+len)
2491 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002492
Sage Weilc5c9cd42008-11-12 14:32:25 -05002493 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002494 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002495 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002496 goto out;
2497 }
2498 size -= skip + trim;
2499 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002500
2501 ret = btrfs_drop_extents(trans, inode,
2502 new_key.offset,
2503 new_key.offset + datal,
2504 &hint_byte, 1);
2505 BUG_ON(ret);
2506
Sage Weilc5c9cd42008-11-12 14:32:25 -05002507 ret = btrfs_insert_empty_item(trans, root, path,
2508 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002509 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002510
2511 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002512 u32 start =
2513 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002514 memmove(buf+start, buf+start+skip,
2515 datal);
2516 }
2517
2518 leaf = path->nodes[0];
2519 slot = path->slots[0];
2520 write_extent_buffer(leaf, buf,
2521 btrfs_item_ptr_offset(leaf, slot),
2522 size);
2523 inode_add_bytes(inode, datal);
2524 }
2525
2526 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002527 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002528
Yan, Zhenga22285a2010-05-16 10:48:46 -04002529 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002530
2531 /*
2532 * we round up to the block size at eof when
2533 * determining which extents to clone above,
2534 * but shouldn't round up the file size
2535 */
2536 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002537 if (endoff > destoff+olen)
2538 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002539 if (endoff > inode->i_size)
2540 btrfs_i_size_write(inode, endoff);
2541
Yan, Zhenga22285a2010-05-16 10:48:46 -04002542 ret = btrfs_update_inode(trans, root, inode);
2543 BUG_ON(ret);
2544 btrfs_end_transaction(trans, root);
2545 }
Chris Masond3977122009-01-05 21:25:51 -05002546next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002547 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002548 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002549 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002550 ret = 0;
2551out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002552 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002553 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002554out_unlock:
2555 mutex_unlock(&src->i_mutex);
2556 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002557 vfree(buf);
2558 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002559out_fput:
2560 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002561out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002562 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002563 return ret;
2564}
2565
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002566static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002567{
2568 struct btrfs_ioctl_clone_range_args args;
2569
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002570 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002571 return -EFAULT;
2572 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2573 args.src_length, args.dest_offset);
2574}
2575
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002576/*
2577 * there are many ways the trans_start and trans_end ioctls can lead
2578 * to deadlocks. They should only be used by applications that
2579 * basically own the machine, and have a very in depth understanding
2580 * of all the possible deadlocks and enospc problems.
2581 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002582static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002583{
2584 struct inode *inode = fdentry(file)->d_inode;
2585 struct btrfs_root *root = BTRFS_I(inode)->root;
2586 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002587 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002588
Sage Weil1ab86ae2009-09-29 18:38:44 -04002589 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002590 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002591 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002592
2593 ret = -EINPROGRESS;
2594 if (file->private_data)
2595 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002596
Li Zefanb83cc962010-12-20 16:04:08 +08002597 ret = -EROFS;
2598 if (btrfs_root_readonly(root))
2599 goto out;
2600
Al Viroa561be72011-11-23 11:57:51 -05002601 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002602 if (ret)
2603 goto out;
2604
Josef Bacika4abeea2011-04-11 17:25:13 -04002605 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002606
Sage Weil1ab86ae2009-09-29 18:38:44 -04002607 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002608 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002609 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002610 goto out_drop;
2611
2612 file->private_data = trans;
2613 return 0;
2614
2615out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002616 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002617 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002618out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002619 return ret;
2620}
2621
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002622static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2623{
2624 struct inode *inode = fdentry(file)->d_inode;
2625 struct btrfs_root *root = BTRFS_I(inode)->root;
2626 struct btrfs_root *new_root;
2627 struct btrfs_dir_item *di;
2628 struct btrfs_trans_handle *trans;
2629 struct btrfs_path *path;
2630 struct btrfs_key location;
2631 struct btrfs_disk_key disk_key;
2632 struct btrfs_super_block *disk_super;
2633 u64 features;
2634 u64 objectid = 0;
2635 u64 dir_id;
2636
2637 if (!capable(CAP_SYS_ADMIN))
2638 return -EPERM;
2639
2640 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2641 return -EFAULT;
2642
2643 if (!objectid)
2644 objectid = root->root_key.objectid;
2645
2646 location.objectid = objectid;
2647 location.type = BTRFS_ROOT_ITEM_KEY;
2648 location.offset = (u64)-1;
2649
2650 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2651 if (IS_ERR(new_root))
2652 return PTR_ERR(new_root);
2653
2654 if (btrfs_root_refs(&new_root->root_item) == 0)
2655 return -ENOENT;
2656
2657 path = btrfs_alloc_path();
2658 if (!path)
2659 return -ENOMEM;
2660 path->leave_spinning = 1;
2661
2662 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002663 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002664 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002665 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002666 }
2667
David Sterba6c417612011-04-13 15:41:04 +02002668 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002669 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2670 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002671 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002672 btrfs_free_path(path);
2673 btrfs_end_transaction(trans, root);
2674 printk(KERN_ERR "Umm, you don't have the default dir item, "
2675 "this isn't going to work\n");
2676 return -ENOENT;
2677 }
2678
2679 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2680 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2681 btrfs_mark_buffer_dirty(path->nodes[0]);
2682 btrfs_free_path(path);
2683
David Sterba6c417612011-04-13 15:41:04 +02002684 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002685 features = btrfs_super_incompat_flags(disk_super);
2686 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2687 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2688 btrfs_set_super_incompat_flags(disk_super, features);
2689 }
2690 btrfs_end_transaction(trans, root);
2691
2692 return 0;
2693}
2694
Josef Bacikbf5fc092010-09-29 11:22:36 -04002695static void get_block_group_info(struct list_head *groups_list,
2696 struct btrfs_ioctl_space_info *space)
2697{
2698 struct btrfs_block_group_cache *block_group;
2699
2700 space->total_bytes = 0;
2701 space->used_bytes = 0;
2702 space->flags = 0;
2703 list_for_each_entry(block_group, groups_list, list) {
2704 space->flags = block_group->flags;
2705 space->total_bytes += block_group->key.offset;
2706 space->used_bytes +=
2707 btrfs_block_group_used(&block_group->item);
2708 }
2709}
2710
Josef Bacik1406e432010-01-13 18:19:06 +00002711long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2712{
2713 struct btrfs_ioctl_space_args space_args;
2714 struct btrfs_ioctl_space_info space;
2715 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002716 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002717 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002718 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002719 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2720 BTRFS_BLOCK_GROUP_SYSTEM,
2721 BTRFS_BLOCK_GROUP_METADATA,
2722 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2723 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002724 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002725 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002726 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002727 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002728
2729 if (copy_from_user(&space_args,
2730 (struct btrfs_ioctl_space_args __user *)arg,
2731 sizeof(space_args)))
2732 return -EFAULT;
2733
Josef Bacikbf5fc092010-09-29 11:22:36 -04002734 for (i = 0; i < num_types; i++) {
2735 struct btrfs_space_info *tmp;
2736
2737 info = NULL;
2738 rcu_read_lock();
2739 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2740 list) {
2741 if (tmp->flags == types[i]) {
2742 info = tmp;
2743 break;
2744 }
2745 }
2746 rcu_read_unlock();
2747
2748 if (!info)
2749 continue;
2750
2751 down_read(&info->groups_sem);
2752 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2753 if (!list_empty(&info->block_groups[c]))
2754 slot_count++;
2755 }
2756 up_read(&info->groups_sem);
2757 }
Josef Bacik1406e432010-01-13 18:19:06 +00002758
Chris Mason7fde62b2010-03-16 15:40:10 -04002759 /* space_slots == 0 means they are asking for a count */
2760 if (space_args.space_slots == 0) {
2761 space_args.total_spaces = slot_count;
2762 goto out;
2763 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002764
Dan Rosenberg51788b12011-02-14 16:04:23 -05002765 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002766
Chris Mason7fde62b2010-03-16 15:40:10 -04002767 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002768
Chris Mason7fde62b2010-03-16 15:40:10 -04002769 /* we generally have at most 6 or so space infos, one for each raid
2770 * level. So, a whole page should be more than enough for everyone
2771 */
2772 if (alloc_size > PAGE_CACHE_SIZE)
2773 return -ENOMEM;
2774
2775 space_args.total_spaces = 0;
2776 dest = kmalloc(alloc_size, GFP_NOFS);
2777 if (!dest)
2778 return -ENOMEM;
2779 dest_orig = dest;
2780
2781 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002782 for (i = 0; i < num_types; i++) {
2783 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002784
Dan Rosenberg51788b12011-02-14 16:04:23 -05002785 if (!slot_count)
2786 break;
2787
Josef Bacikbf5fc092010-09-29 11:22:36 -04002788 info = NULL;
2789 rcu_read_lock();
2790 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2791 list) {
2792 if (tmp->flags == types[i]) {
2793 info = tmp;
2794 break;
2795 }
2796 }
2797 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002798
Josef Bacikbf5fc092010-09-29 11:22:36 -04002799 if (!info)
2800 continue;
2801 down_read(&info->groups_sem);
2802 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2803 if (!list_empty(&info->block_groups[c])) {
2804 get_block_group_info(&info->block_groups[c],
2805 &space);
2806 memcpy(dest, &space, sizeof(space));
2807 dest++;
2808 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002809 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002810 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002811 if (!slot_count)
2812 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002813 }
2814 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002815 }
Josef Bacik1406e432010-01-13 18:19:06 +00002816
Chris Mason7fde62b2010-03-16 15:40:10 -04002817 user_dest = (struct btrfs_ioctl_space_info *)
2818 (arg + sizeof(struct btrfs_ioctl_space_args));
2819
2820 if (copy_to_user(user_dest, dest_orig, alloc_size))
2821 ret = -EFAULT;
2822
2823 kfree(dest_orig);
2824out:
2825 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002826 ret = -EFAULT;
2827
2828 return ret;
2829}
2830
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002831/*
2832 * there are many ways the trans_start and trans_end ioctls can lead
2833 * to deadlocks. They should only be used by applications that
2834 * basically own the machine, and have a very in depth understanding
2835 * of all the possible deadlocks and enospc problems.
2836 */
2837long btrfs_ioctl_trans_end(struct file *file)
2838{
2839 struct inode *inode = fdentry(file)->d_inode;
2840 struct btrfs_root *root = BTRFS_I(inode)->root;
2841 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002842
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002843 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002844 if (!trans)
2845 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002846 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002847
Sage Weil1ab86ae2009-09-29 18:38:44 -04002848 btrfs_end_transaction(trans, root);
2849
Josef Bacika4abeea2011-04-11 17:25:13 -04002850 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002851
Al Viro2a79f172011-12-09 08:06:57 -05002852 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002853 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002854}
2855
Sage Weil46204592010-10-29 15:41:32 -04002856static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2857{
2858 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2859 struct btrfs_trans_handle *trans;
2860 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002861 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002862
2863 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002864 if (IS_ERR(trans))
2865 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002866 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002867 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002868 if (ret) {
2869 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002870 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002871 }
Sage Weil46204592010-10-29 15:41:32 -04002872
2873 if (argp)
2874 if (copy_to_user(argp, &transid, sizeof(transid)))
2875 return -EFAULT;
2876 return 0;
2877}
2878
2879static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2880{
2881 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2882 u64 transid;
2883
2884 if (argp) {
2885 if (copy_from_user(&transid, argp, sizeof(transid)))
2886 return -EFAULT;
2887 } else {
2888 transid = 0; /* current trans */
2889 }
2890 return btrfs_wait_for_commit(root, transid);
2891}
2892
Jan Schmidt475f6382011-03-11 15:41:01 +01002893static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2894{
2895 int ret;
2896 struct btrfs_ioctl_scrub_args *sa;
2897
2898 if (!capable(CAP_SYS_ADMIN))
2899 return -EPERM;
2900
2901 sa = memdup_user(arg, sizeof(*sa));
2902 if (IS_ERR(sa))
2903 return PTR_ERR(sa);
2904
2905 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01002906 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01002907
2908 if (copy_to_user(arg, sa, sizeof(*sa)))
2909 ret = -EFAULT;
2910
2911 kfree(sa);
2912 return ret;
2913}
2914
2915static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2916{
2917 if (!capable(CAP_SYS_ADMIN))
2918 return -EPERM;
2919
2920 return btrfs_scrub_cancel(root);
2921}
2922
2923static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2924 void __user *arg)
2925{
2926 struct btrfs_ioctl_scrub_args *sa;
2927 int ret;
2928
2929 if (!capable(CAP_SYS_ADMIN))
2930 return -EPERM;
2931
2932 sa = memdup_user(arg, sizeof(*sa));
2933 if (IS_ERR(sa))
2934 return PTR_ERR(sa);
2935
2936 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2937
2938 if (copy_to_user(arg, sa, sizeof(*sa)))
2939 ret = -EFAULT;
2940
2941 kfree(sa);
2942 return ret;
2943}
2944
Jan Schmidtd7728c92011-07-07 16:48:38 +02002945static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
2946{
2947 int ret = 0;
2948 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04002949 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002950 int size;
Chris Mason806468f2011-11-06 03:07:10 -05002951 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002952 struct inode_fs_paths *ipath = NULL;
2953 struct btrfs_path *path;
2954
2955 if (!capable(CAP_SYS_ADMIN))
2956 return -EPERM;
2957
2958 path = btrfs_alloc_path();
2959 if (!path) {
2960 ret = -ENOMEM;
2961 goto out;
2962 }
2963
2964 ipa = memdup_user(arg, sizeof(*ipa));
2965 if (IS_ERR(ipa)) {
2966 ret = PTR_ERR(ipa);
2967 ipa = NULL;
2968 goto out;
2969 }
2970
2971 size = min_t(u32, ipa->size, 4096);
2972 ipath = init_ipath(size, root, path);
2973 if (IS_ERR(ipath)) {
2974 ret = PTR_ERR(ipath);
2975 ipath = NULL;
2976 goto out;
2977 }
2978
2979 ret = paths_from_inode(ipa->inum, ipath);
2980 if (ret < 0)
2981 goto out;
2982
2983 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002984 rel_ptr = ipath->fspath->val[i] -
2985 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04002986 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002987 }
2988
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002989 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
2990 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02002991 if (ret) {
2992 ret = -EFAULT;
2993 goto out;
2994 }
2995
2996out:
2997 btrfs_free_path(path);
2998 free_ipath(ipath);
2999 kfree(ipa);
3000
3001 return ret;
3002}
3003
3004static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3005{
3006 struct btrfs_data_container *inodes = ctx;
3007 const size_t c = 3 * sizeof(u64);
3008
3009 if (inodes->bytes_left >= c) {
3010 inodes->bytes_left -= c;
3011 inodes->val[inodes->elem_cnt] = inum;
3012 inodes->val[inodes->elem_cnt + 1] = offset;
3013 inodes->val[inodes->elem_cnt + 2] = root;
3014 inodes->elem_cnt += 3;
3015 } else {
3016 inodes->bytes_missing += c - inodes->bytes_left;
3017 inodes->bytes_left = 0;
3018 inodes->elem_missed += 3;
3019 }
3020
3021 return 0;
3022}
3023
3024static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3025 void __user *arg)
3026{
3027 int ret = 0;
3028 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003029 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003030 struct btrfs_ioctl_logical_ino_args *loi;
3031 struct btrfs_data_container *inodes = NULL;
3032 struct btrfs_path *path = NULL;
3033 struct btrfs_key key;
3034
3035 if (!capable(CAP_SYS_ADMIN))
3036 return -EPERM;
3037
3038 loi = memdup_user(arg, sizeof(*loi));
3039 if (IS_ERR(loi)) {
3040 ret = PTR_ERR(loi);
3041 loi = NULL;
3042 goto out;
3043 }
3044
3045 path = btrfs_alloc_path();
3046 if (!path) {
3047 ret = -ENOMEM;
3048 goto out;
3049 }
3050
3051 size = min_t(u32, loi->size, 4096);
3052 inodes = init_data_container(size);
3053 if (IS_ERR(inodes)) {
3054 ret = PTR_ERR(inodes);
3055 inodes = NULL;
3056 goto out;
3057 }
3058
3059 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003060 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003061
3062 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3063 ret = -ENOENT;
3064 if (ret < 0)
3065 goto out;
3066
Jan Schmidt4692cf52011-12-02 14:56:41 +01003067 extent_item_pos = loi->logical - key.objectid;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003068 ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003069 extent_item_pos, build_ino_list,
3070 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003071
3072 if (ret < 0)
3073 goto out;
3074
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003075 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3076 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003077 if (ret)
3078 ret = -EFAULT;
3079
3080out:
3081 btrfs_free_path(path);
3082 kfree(inodes);
3083 kfree(loi);
3084
3085 return ret;
3086}
3087
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003088void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003089 struct btrfs_ioctl_balance_args *bargs)
3090{
3091 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3092
3093 bargs->flags = bctl->flags;
3094
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003095 if (atomic_read(&fs_info->balance_running))
3096 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3097 if (atomic_read(&fs_info->balance_pause_req))
3098 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003099 if (atomic_read(&fs_info->balance_cancel_req))
3100 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003101
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003102 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3103 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3104 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003105
3106 if (lock) {
3107 spin_lock(&fs_info->balance_lock);
3108 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3109 spin_unlock(&fs_info->balance_lock);
3110 } else {
3111 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3112 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003113}
3114
3115static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3116{
3117 struct btrfs_fs_info *fs_info = root->fs_info;
3118 struct btrfs_ioctl_balance_args *bargs;
3119 struct btrfs_balance_control *bctl;
3120 int ret;
3121
3122 if (!capable(CAP_SYS_ADMIN))
3123 return -EPERM;
3124
3125 if (fs_info->sb->s_flags & MS_RDONLY)
3126 return -EROFS;
3127
3128 mutex_lock(&fs_info->volume_mutex);
3129 mutex_lock(&fs_info->balance_mutex);
3130
3131 if (arg) {
3132 bargs = memdup_user(arg, sizeof(*bargs));
3133 if (IS_ERR(bargs)) {
3134 ret = PTR_ERR(bargs);
3135 goto out;
3136 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003137
3138 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3139 if (!fs_info->balance_ctl) {
3140 ret = -ENOTCONN;
3141 goto out_bargs;
3142 }
3143
3144 bctl = fs_info->balance_ctl;
3145 spin_lock(&fs_info->balance_lock);
3146 bctl->flags |= BTRFS_BALANCE_RESUME;
3147 spin_unlock(&fs_info->balance_lock);
3148
3149 goto do_balance;
3150 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003151 } else {
3152 bargs = NULL;
3153 }
3154
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003155 if (fs_info->balance_ctl) {
3156 ret = -EINPROGRESS;
3157 goto out_bargs;
3158 }
3159
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003160 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3161 if (!bctl) {
3162 ret = -ENOMEM;
3163 goto out_bargs;
3164 }
3165
3166 bctl->fs_info = fs_info;
3167 if (arg) {
3168 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3169 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3170 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3171
3172 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003173 } else {
3174 /* balance everything - no filters */
3175 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003176 }
3177
Ilya Dryomovde322262012-01-16 22:04:49 +02003178do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003179 ret = btrfs_balance(bctl, bargs);
3180 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003181 * bctl is freed in __cancel_balance or in free_fs_info if
3182 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003183 */
3184 if (arg) {
3185 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3186 ret = -EFAULT;
3187 }
3188
3189out_bargs:
3190 kfree(bargs);
3191out:
3192 mutex_unlock(&fs_info->balance_mutex);
3193 mutex_unlock(&fs_info->volume_mutex);
3194 return ret;
3195}
3196
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003197static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3198{
3199 if (!capable(CAP_SYS_ADMIN))
3200 return -EPERM;
3201
3202 switch (cmd) {
3203 case BTRFS_BALANCE_CTL_PAUSE:
3204 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003205 case BTRFS_BALANCE_CTL_CANCEL:
3206 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003207 }
3208
3209 return -EINVAL;
3210}
3211
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003212static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3213 void __user *arg)
3214{
3215 struct btrfs_fs_info *fs_info = root->fs_info;
3216 struct btrfs_ioctl_balance_args *bargs;
3217 int ret = 0;
3218
3219 if (!capable(CAP_SYS_ADMIN))
3220 return -EPERM;
3221
3222 mutex_lock(&fs_info->balance_mutex);
3223 if (!fs_info->balance_ctl) {
3224 ret = -ENOTCONN;
3225 goto out;
3226 }
3227
3228 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3229 if (!bargs) {
3230 ret = -ENOMEM;
3231 goto out;
3232 }
3233
3234 update_ioctl_balance_args(fs_info, 1, bargs);
3235
3236 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3237 ret = -EFAULT;
3238
3239 kfree(bargs);
3240out:
3241 mutex_unlock(&fs_info->balance_mutex);
3242 return ret;
3243}
3244
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003245long btrfs_ioctl(struct file *file, unsigned int
3246 cmd, unsigned long arg)
3247{
3248 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003249 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003250
3251 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003252 case FS_IOC_GETFLAGS:
3253 return btrfs_ioctl_getflags(file, argp);
3254 case FS_IOC_SETFLAGS:
3255 return btrfs_ioctl_setflags(file, argp);
3256 case FS_IOC_GETVERSION:
3257 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003258 case FITRIM:
3259 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003260 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003261 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003262 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003263 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003264 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003265 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003266 case BTRFS_IOC_SNAP_DESTROY:
3267 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003268 case BTRFS_IOC_SUBVOL_GETFLAGS:
3269 return btrfs_ioctl_subvol_getflags(file, argp);
3270 case BTRFS_IOC_SUBVOL_SETFLAGS:
3271 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003272 case BTRFS_IOC_DEFAULT_SUBVOL:
3273 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003274 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003275 return btrfs_ioctl_defrag(file, NULL);
3276 case BTRFS_IOC_DEFRAG_RANGE:
3277 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003278 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003279 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003280 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003281 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003282 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003283 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003284 case BTRFS_IOC_FS_INFO:
3285 return btrfs_ioctl_fs_info(root, argp);
3286 case BTRFS_IOC_DEV_INFO:
3287 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003288 case BTRFS_IOC_BALANCE:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003289 return btrfs_ioctl_balance(root, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003290 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003291 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3292 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003293 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003294 case BTRFS_IOC_TRANS_START:
3295 return btrfs_ioctl_trans_start(file);
3296 case BTRFS_IOC_TRANS_END:
3297 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003298 case BTRFS_IOC_TREE_SEARCH:
3299 return btrfs_ioctl_tree_search(file, argp);
3300 case BTRFS_IOC_INO_LOOKUP:
3301 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003302 case BTRFS_IOC_INO_PATHS:
3303 return btrfs_ioctl_ino_to_path(root, argp);
3304 case BTRFS_IOC_LOGICAL_INO:
3305 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003306 case BTRFS_IOC_SPACE_INFO:
3307 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003308 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003309 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3310 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003311 case BTRFS_IOC_START_SYNC:
3312 return btrfs_ioctl_start_sync(file, argp);
3313 case BTRFS_IOC_WAIT_SYNC:
3314 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003315 case BTRFS_IOC_SCRUB:
3316 return btrfs_ioctl_scrub(root, argp);
3317 case BTRFS_IOC_SCRUB_CANCEL:
3318 return btrfs_ioctl_scrub_cancel(root, argp);
3319 case BTRFS_IOC_SCRUB_PROGRESS:
3320 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003321 case BTRFS_IOC_BALANCE_V2:
3322 return btrfs_ioctl_balance(root, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003323 case BTRFS_IOC_BALANCE_CTL:
3324 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003325 case BTRFS_IOC_BALANCE_PROGRESS:
3326 return btrfs_ioctl_balance_progress(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003327 }
3328
3329 return -ENOTTY;
3330}