blob: 45910d4b8f659ddbc4bcc69696478dc28864cc38 [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 Zefan306424cc2011-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);
liubo2d4e6f62011-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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100428 if (IS_ERR(new_root)) {
429 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
430 ret = PTR_ERR(new_root);
431 goto fail;
432 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400433
434 btrfs_record_root_in_trans(trans, new_root);
435
Josef Bacikd82a6f12011-05-11 15:26:06 -0400436 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700437 if (ret) {
438 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100439 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700440 goto fail;
441 }
442
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400443 /*
444 * insert the directory item
445 */
Chris Mason3de45862008-11-17 21:02:50 -0500446 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100447 if (ret) {
448 btrfs_abort_transaction(trans, root, ret);
449 goto fail;
450 }
Chris Mason3de45862008-11-17 21:02:50 -0500451
452 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800453 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500454 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100455 if (ret) {
456 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100458 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500459
Yan Zheng52c26172009-01-05 15:43:43 -0500460 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
461 ret = btrfs_update_inode(trans, root, dir);
462 BUG_ON(ret);
463
Chris Mason0660b5a2008-11-17 20:37:39 -0500464 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400465 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800466 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400467
Chris Mason0660b5a2008-11-17 20:37:39 -0500468 BUG_ON(ret);
469
Yan, Zheng76dda932009-09-21 16:00:26 -0400470 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400471fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400472 if (async_transid) {
473 *async_transid = trans->transid;
474 err = btrfs_commit_transaction_async(trans, root, 1);
475 } else {
476 err = btrfs_commit_transaction(trans, root);
477 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400478 if (err && !ret)
479 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400480 return ret;
481}
482
Sage Weil72fd0322010-10-29 15:41:32 -0400483static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800484 char *name, int namelen, u64 *async_transid,
485 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400486{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000487 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400488 struct btrfs_pending_snapshot *pending_snapshot;
489 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000490 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491
492 if (!root->ref_cows)
493 return -EINVAL;
494
Yan, Zhenga22285a2010-05-16 10:48:46 -0400495 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
496 if (!pending_snapshot)
497 return -ENOMEM;
498
499 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
500 pending_snapshot->dentry = dentry;
501 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800502 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400503
504 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
505 if (IS_ERR(trans)) {
506 ret = PTR_ERR(trans);
507 goto fail;
508 }
509
510 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
511 BUG_ON(ret);
512
Josef Bacik83515832011-06-14 15:16:14 -0400513 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400514 list_add(&pending_snapshot->list,
515 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400516 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400517 if (async_transid) {
518 *async_transid = trans->transid;
519 ret = btrfs_commit_transaction_async(trans,
520 root->fs_info->extent_root, 1);
521 } else {
522 ret = btrfs_commit_transaction(trans,
523 root->fs_info->extent_root);
524 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400525 BUG_ON(ret);
526
527 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400528 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000529 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400530
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500531 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
532 if (ret)
533 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400534
Al Viro2fbe8c82011-07-16 21:38:06 -0400535 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000536 if (IS_ERR(inode)) {
537 ret = PTR_ERR(inode);
538 goto fail;
539 }
540 BUG_ON(!inode);
541 d_instantiate(dentry, inode);
542 ret = 0;
543fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400544 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400545 return ret;
546}
547
Sage Weil4260f7c2010-10-29 15:46:43 -0400548/* copy of check_sticky in fs/namei.c()
549* It's inline, so penalty for filesystems that don't use sticky bit is
550* minimal.
551*/
552static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
553{
554 uid_t fsuid = current_fsuid();
555
556 if (!(dir->i_mode & S_ISVTX))
557 return 0;
558 if (inode->i_uid == fsuid)
559 return 0;
560 if (dir->i_uid == fsuid)
561 return 0;
562 return !capable(CAP_FOWNER);
563}
564
565/* copy of may_delete in fs/namei.c()
566 * Check whether we can remove a link victim from directory dir, check
567 * whether the type of victim is right.
568 * 1. We can't do it if dir is read-only (done in permission())
569 * 2. We should have write and exec permissions on dir
570 * 3. We can't remove anything from append-only dir
571 * 4. We can't do anything with immutable dir (done in permission())
572 * 5. If the sticky bit on dir is set we should either
573 * a. be owner of dir, or
574 * b. be owner of victim, or
575 * c. have CAP_FOWNER capability
576 * 6. If the victim is append-only or immutable we can't do antyhing with
577 * links pointing to it.
578 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
579 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
580 * 9. We can't remove a root or mountpoint.
581 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
582 * nfs_async_unlink().
583 */
584
585static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
586{
587 int error;
588
589 if (!victim->d_inode)
590 return -ENOENT;
591
592 BUG_ON(victim->d_parent->d_inode != dir);
593 audit_inode_child(victim, dir);
594
595 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
596 if (error)
597 return error;
598 if (IS_APPEND(dir))
599 return -EPERM;
600 if (btrfs_check_sticky(dir, victim->d_inode)||
601 IS_APPEND(victim->d_inode)||
602 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
603 return -EPERM;
604 if (isdir) {
605 if (!S_ISDIR(victim->d_inode->i_mode))
606 return -ENOTDIR;
607 if (IS_ROOT(victim))
608 return -EBUSY;
609 } else if (S_ISDIR(victim->d_inode->i_mode))
610 return -EISDIR;
611 if (IS_DEADDIR(dir))
612 return -ENOENT;
613 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
614 return -EBUSY;
615 return 0;
616}
617
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400618/* copy of may_create in fs/namei.c() */
619static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
620{
621 if (child->d_inode)
622 return -EEXIST;
623 if (IS_DEADDIR(dir))
624 return -ENOENT;
625 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
626}
627
628/*
629 * Create a new subvolume below @parent. This is largely modeled after
630 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
631 * inside this filesystem so it's quite a bit simpler.
632 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400633static noinline int btrfs_mksubvol(struct path *parent,
634 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400635 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800636 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400637{
Yan, Zheng76dda932009-09-21 16:00:26 -0400638 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400639 struct dentry *dentry;
640 int error;
641
Yan, Zheng76dda932009-09-21 16:00:26 -0400642 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400643
644 dentry = lookup_one_len(name, parent->dentry, namelen);
645 error = PTR_ERR(dentry);
646 if (IS_ERR(dentry))
647 goto out_unlock;
648
649 error = -EEXIST;
650 if (dentry->d_inode)
651 goto out_dput;
652
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400653 error = mnt_want_write(parent->mnt);
654 if (error)
655 goto out_dput;
656
Yan, Zheng76dda932009-09-21 16:00:26 -0400657 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400658 if (error)
659 goto out_drop_write;
660
Yan, Zheng76dda932009-09-21 16:00:26 -0400661 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
662
663 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
664 goto out_up_read;
665
Chris Mason3de45862008-11-17 21:02:50 -0500666 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400667 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800668 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500669 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400670 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400671 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500672 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400673 if (!error)
674 fsnotify_mkdir(dir, dentry);
675out_up_read:
676 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400677out_drop_write:
678 mnt_drop_write(parent->mnt);
679out_dput:
680 dput(dentry);
681out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400682 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400683 return error;
684}
685
Chris Mason4cb53002011-05-24 15:35:30 -0400686/*
687 * When we're defragging a range, we don't want to kick it off again
688 * if it is really just waiting for delalloc to send it down.
689 * If we find a nice big extent or delalloc range for the bytes in the
690 * file you want to defrag, we return 0 to let you know to skip this
691 * part of the file
692 */
693static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
694{
695 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
696 struct extent_map *em = NULL;
697 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
698 u64 end;
699
700 read_lock(&em_tree->lock);
701 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
702 read_unlock(&em_tree->lock);
703
704 if (em) {
705 end = extent_map_end(em);
706 free_extent_map(em);
707 if (end - offset > thresh)
708 return 0;
709 }
710 /* if we already have a nice delalloc here, just stop */
711 thresh /= 2;
712 end = count_range_bits(io_tree, &offset, offset + thresh,
713 thresh, EXTENT_DELALLOC, 1);
714 if (end >= thresh)
715 return 0;
716 return 1;
717}
718
719/*
720 * helper function to walk through a file and find extents
721 * newer than a specific transid, and smaller than thresh.
722 *
723 * This is used by the defragging code to find new and small
724 * extents
725 */
726static int find_new_extents(struct btrfs_root *root,
727 struct inode *inode, u64 newer_than,
728 u64 *off, int thresh)
729{
730 struct btrfs_path *path;
731 struct btrfs_key min_key;
732 struct btrfs_key max_key;
733 struct extent_buffer *leaf;
734 struct btrfs_file_extent_item *extent;
735 int type;
736 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000737 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400738
739 path = btrfs_alloc_path();
740 if (!path)
741 return -ENOMEM;
742
David Sterbaa4689d22011-05-31 17:08:14 +0000743 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400744 min_key.type = BTRFS_EXTENT_DATA_KEY;
745 min_key.offset = *off;
746
David Sterbaa4689d22011-05-31 17:08:14 +0000747 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400748 max_key.type = (u8)-1;
749 max_key.offset = (u64)-1;
750
751 path->keep_locks = 1;
752
753 while(1) {
754 ret = btrfs_search_forward(root, &min_key, &max_key,
755 path, 0, newer_than);
756 if (ret != 0)
757 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000758 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400759 goto none;
760 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
761 goto none;
762
763 leaf = path->nodes[0];
764 extent = btrfs_item_ptr(leaf, path->slots[0],
765 struct btrfs_file_extent_item);
766
767 type = btrfs_file_extent_type(leaf, extent);
768 if (type == BTRFS_FILE_EXTENT_REG &&
769 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
770 check_defrag_in_cache(inode, min_key.offset, thresh)) {
771 *off = min_key.offset;
772 btrfs_free_path(path);
773 return 0;
774 }
775
776 if (min_key.offset == (u64)-1)
777 goto none;
778
779 min_key.offset++;
780 btrfs_release_path(path);
781 }
782none:
783 btrfs_free_path(path);
784 return -ENOENT;
785}
786
Chris Mason940100a2010-03-10 10:52:59 -0500787static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500788 int thresh, u64 *last_len, u64 *skip,
789 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500790{
791 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
792 struct extent_map *em = NULL;
793 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
794 int ret = 1;
795
796 /*
Li Zefan008873e2011-09-02 15:57:07 +0800797 * make sure that once we start defragging an extent, we keep on
Chris Mason940100a2010-03-10 10:52:59 -0500798 * defragging it
799 */
800 if (start < *defrag_end)
801 return 1;
802
803 *skip = 0;
804
805 /*
806 * hopefully we have this extent in the tree already, try without
807 * the full extent lock
808 */
809 read_lock(&em_tree->lock);
810 em = lookup_extent_mapping(em_tree, start, len);
811 read_unlock(&em_tree->lock);
812
813 if (!em) {
814 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100815 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500816 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100817 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500818
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000819 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500820 return 0;
821 }
822
823 /* this will cover holes, and inline extents */
824 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
825 ret = 0;
826
827 /*
828 * we hit a real extent, if it is big don't bother defragging it again
829 */
Chris Mason1e701a32010-03-11 09:42:04 -0500830 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500831 ret = 0;
832
833 /*
834 * last_len ends up being a counter of how many bytes we've defragged.
835 * every time we choose not to defrag an extent, we reset *last_len
836 * so that the next tiny extent will force a defrag.
837 *
838 * The end result of this is that tiny extents before a single big
839 * extent will force at least part of that big extent to be defragged.
840 */
841 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500842 *defrag_end = extent_map_end(em);
843 } else {
844 *last_len = 0;
845 *skip = extent_map_end(em);
846 *defrag_end = 0;
847 }
848
849 free_extent_map(em);
850 return ret;
851}
852
Chris Mason4cb53002011-05-24 15:35:30 -0400853/*
854 * it doesn't do much good to defrag one or two pages
855 * at a time. This pulls in a nice chunk of pages
856 * to COW and defrag.
857 *
858 * It also makes sure the delalloc code has enough
859 * dirty data to avoid making new small extents as part
860 * of the defrag
861 *
862 * It's a good idea to start RA on this range
863 * before calling this.
864 */
865static int cluster_pages_for_defrag(struct inode *inode,
866 struct page **pages,
867 unsigned long start_index,
868 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400869{
Chris Mason4cb53002011-05-24 15:35:30 -0400870 unsigned long file_end;
871 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400872 u64 page_start;
873 u64 page_end;
Chris Mason4cb53002011-05-24 15:35:30 -0400874 int ret;
875 int i;
876 int i_done;
877 struct btrfs_ordered_extent *ordered;
878 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800879 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400880 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400881
882 if (isize == 0)
883 return 0;
884 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
885
886 ret = btrfs_delalloc_reserve_space(inode,
887 num_pages << PAGE_CACHE_SHIFT);
888 if (ret)
889 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400890 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800891 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400892
893 /* step one, lock all the pages */
894 for (i = 0; i < num_pages; i++) {
895 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800896again:
Josef Bacika94733d2011-07-11 10:47:06 -0400897 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800898 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400899 if (!page)
900 break;
901
Miao Xie600a45e2012-02-16 15:01:24 +0800902 page_start = page_offset(page);
903 page_end = page_start + PAGE_CACHE_SIZE - 1;
904 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100905 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800906 ordered = btrfs_lookup_ordered_extent(inode,
907 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100908 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800909 if (!ordered)
910 break;
911
912 unlock_page(page);
913 btrfs_start_ordered_extent(inode, ordered, 1);
914 btrfs_put_ordered_extent(ordered);
915 lock_page(page);
916 }
917
Chris Mason4cb53002011-05-24 15:35:30 -0400918 if (!PageUptodate(page)) {
919 btrfs_readpage(NULL, page);
920 lock_page(page);
921 if (!PageUptodate(page)) {
922 unlock_page(page);
923 page_cache_release(page);
924 ret = -EIO;
925 break;
926 }
927 }
Miao Xie600a45e2012-02-16 15:01:24 +0800928
Chris Mason4cb53002011-05-24 15:35:30 -0400929 isize = i_size_read(inode);
930 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Miao Xie600a45e2012-02-16 15:01:24 +0800931 if (!isize || page->index > file_end) {
Chris Mason4cb53002011-05-24 15:35:30 -0400932 /* whoops, we blew past eof, skip this page */
933 unlock_page(page);
934 page_cache_release(page);
935 break;
936 }
Miao Xie600a45e2012-02-16 15:01:24 +0800937
938 if (page->mapping != inode->i_mapping) {
939 unlock_page(page);
940 page_cache_release(page);
941 goto again;
942 }
943
Chris Mason4cb53002011-05-24 15:35:30 -0400944 pages[i] = page;
945 i_done++;
946 }
947 if (!i_done || ret)
948 goto out;
949
950 if (!(inode->i_sb->s_flags & MS_ACTIVE))
951 goto out;
952
953 /*
954 * so now we have a nice long stream of locked
955 * and up to date pages, lets wait on them
956 */
957 for (i = 0; i < i_done; i++)
958 wait_on_page_writeback(pages[i]);
959
960 page_start = page_offset(pages[0]);
961 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
962
963 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100964 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -0400965 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
966 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
967 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
968 GFP_NOFS);
969
970 if (i_done != num_pages) {
Josef Bacik9e0baf62011-07-15 15:16:44 +0000971 spin_lock(&BTRFS_I(inode)->lock);
972 BTRFS_I(inode)->outstanding_extents++;
973 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -0400974 btrfs_delalloc_release_space(inode,
975 (num_pages - i_done) << PAGE_CACHE_SHIFT);
976 }
977
978
979 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
980 &cached_state);
981
982 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
983 page_start, page_end - 1, &cached_state,
984 GFP_NOFS);
985
986 for (i = 0; i < i_done; i++) {
987 clear_page_dirty_for_io(pages[i]);
988 ClearPageChecked(pages[i]);
989 set_page_extent_mapped(pages[i]);
990 set_page_dirty(pages[i]);
991 unlock_page(pages[i]);
992 page_cache_release(pages[i]);
993 }
994 return i_done;
995out:
996 for (i = 0; i < i_done; i++) {
997 unlock_page(pages[i]);
998 page_cache_release(pages[i]);
999 }
1000 btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
1001 return ret;
1002
1003}
1004
1005int btrfs_defrag_file(struct inode *inode, struct file *file,
1006 struct btrfs_ioctl_defrag_range_args *range,
1007 u64 newer_than, unsigned long max_to_defrag)
1008{
1009 struct btrfs_root *root = BTRFS_I(inode)->root;
1010 struct btrfs_super_block *disk_super;
1011 struct file_ra_state *ra = NULL;
1012 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001013 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001014 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -05001015 u64 last_len = 0;
1016 u64 skip = 0;
1017 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001018 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001019 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001020 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001021 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001022 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001023 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001024 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001025 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1026 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001027 u64 new_align = ~((u64)128 * 1024 - 1);
1028 struct page **pages = NULL;
1029
1030 if (extent_thresh == 0)
1031 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001032
1033 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1034 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1035 return -EINVAL;
1036 if (range->compress_type)
1037 compress_type = range->compress_type;
1038 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001039
Li Zefan151a31b2011-09-02 15:56:39 +08001040 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001041 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001042
Chris Mason4cb53002011-05-24 15:35:30 -04001043 /*
1044 * if we were not given a file, allocate a readahead
1045 * context
1046 */
1047 if (!file) {
1048 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1049 if (!ra)
1050 return -ENOMEM;
1051 file_ra_state_init(ra, inode->i_mapping);
1052 } else {
1053 ra = &file->f_ra;
1054 }
1055
Li Zefan008873e2011-09-02 15:57:07 +08001056 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001057 GFP_NOFS);
1058 if (!pages) {
1059 ret = -ENOMEM;
1060 goto out_ra;
1061 }
1062
1063 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001064 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001065 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001066 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1067 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001068 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001069 }
1070
Chris Mason4cb53002011-05-24 15:35:30 -04001071 if (newer_than) {
1072 ret = find_new_extents(root, inode, newer_than,
1073 &newer_off, 64 * 1024);
1074 if (!ret) {
1075 range->start = newer_off;
1076 /*
1077 * we always align our defrag to help keep
1078 * the extents in the file evenly spaced
1079 */
1080 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001081 } else
1082 goto out_ra;
1083 } else {
1084 i = range->start >> PAGE_CACHE_SHIFT;
1085 }
1086 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001087 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001088
Li Zefan2a0f7f52011-10-10 15:43:34 -04001089 /*
1090 * make writeback starts from i, so the defrag range can be
1091 * written sequentially.
1092 */
1093 if (i < inode->i_mapping->writeback_index)
1094 inode->i_mapping->writeback_index = i;
1095
Chris Masonf7f43cc2011-10-11 11:41:40 -04001096 while (i <= last_index && defrag_count < max_to_defrag &&
1097 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1098 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001099 /*
1100 * make sure we stop running if someone unmounts
1101 * the FS
1102 */
1103 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1104 break;
1105
1106 if (!newer_than &&
1107 !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -05001108 PAGE_CACHE_SIZE,
Chris Mason4cb53002011-05-24 15:35:30 -04001109 extent_thresh,
Chris Mason1e701a32010-03-11 09:42:04 -05001110 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -05001111 &defrag_end)) {
1112 unsigned long next;
1113 /*
1114 * the should_defrag function tells us how much to skip
1115 * bump our counter by the suggested amount
1116 */
1117 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1118 i = max(i + 1, next);
1119 continue;
1120 }
Li Zefan008873e2011-09-02 15:57:07 +08001121
1122 if (!newer_than) {
1123 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1124 PAGE_CACHE_SHIFT) - i;
1125 cluster = min(cluster, max_cluster);
1126 } else {
1127 cluster = max_cluster;
1128 }
1129
Chris Mason1e701a32010-03-11 09:42:04 -05001130 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001131 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001132
Li Zefan008873e2011-09-02 15:57:07 +08001133 if (i + cluster > ra_index) {
1134 ra_index = max(i, ra_index);
1135 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1136 cluster);
1137 ra_index += max_cluster;
1138 }
Chris Mason940100a2010-03-10 10:52:59 -05001139
Liu Boecb8bea2012-03-29 09:57:44 -04001140 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001141 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001142 if (ret < 0) {
1143 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001144 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001145 }
Chris Mason940100a2010-03-10 10:52:59 -05001146
Chris Mason4cb53002011-05-24 15:35:30 -04001147 defrag_count += ret;
1148 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001149 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001150
1151 if (newer_than) {
1152 if (newer_off == (u64)-1)
1153 break;
1154
1155 newer_off = max(newer_off + 1,
1156 (u64)i << PAGE_CACHE_SHIFT);
1157
1158 ret = find_new_extents(root, inode,
1159 newer_than, &newer_off,
1160 64 * 1024);
1161 if (!ret) {
1162 range->start = newer_off;
1163 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001164 } else {
1165 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001166 }
Chris Mason4cb53002011-05-24 15:35:30 -04001167 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001168 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001169 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001170 last_len += ret << PAGE_CACHE_SHIFT;
1171 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001172 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001173 last_len = 0;
1174 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001175 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001176 }
1177
Chris Mason1e701a32010-03-11 09:42:04 -05001178 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1179 filemap_flush(inode->i_mapping);
1180
1181 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1182 /* the filemap_flush will queue IO into the worker threads, but
1183 * we have to make sure the IO is actually started and that
1184 * ordered extents get created before we return
1185 */
1186 atomic_inc(&root->fs_info->async_submit_draining);
1187 while (atomic_read(&root->fs_info->nr_async_submits) ||
1188 atomic_read(&root->fs_info->async_delalloc_pages)) {
1189 wait_event(root->fs_info->async_submit_wait,
1190 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1191 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1192 }
1193 atomic_dec(&root->fs_info->async_submit_draining);
1194
1195 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001196 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001197 mutex_unlock(&inode->i_mutex);
1198 }
1199
David Sterba6c417612011-04-13 15:41:04 +02001200 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001201 features = btrfs_super_incompat_flags(disk_super);
1202 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1203 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1204 btrfs_set_super_incompat_flags(disk_super, features);
1205 }
1206
Diego Calleja60ccf822011-09-01 16:33:57 +02001207 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001208
Chris Mason4cb53002011-05-24 15:35:30 -04001209out_ra:
1210 if (!file)
1211 kfree(ra);
1212 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001213 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001214}
1215
Yan, Zheng76dda932009-09-21 16:00:26 -04001216static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1217 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001218{
1219 u64 new_size;
1220 u64 old_size;
1221 u64 devid = 1;
1222 struct btrfs_ioctl_vol_args *vol_args;
1223 struct btrfs_trans_handle *trans;
1224 struct btrfs_device *device = NULL;
1225 char *sizestr;
1226 char *devstr = NULL;
1227 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001228 int mod = 0;
1229
Yan Zhengc146afa2008-11-12 14:34:12 -05001230 if (root->fs_info->sb->s_flags & MS_RDONLY)
1231 return -EROFS;
1232
Chris Masone441d542009-01-05 16:57:23 -05001233 if (!capable(CAP_SYS_ADMIN))
1234 return -EPERM;
1235
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001236 mutex_lock(&root->fs_info->volume_mutex);
1237 if (root->fs_info->balance_ctl) {
1238 printk(KERN_INFO "btrfs: balance in progress\n");
1239 ret = -EINVAL;
1240 goto out;
1241 }
1242
Li Zefandae7b662009-04-08 15:06:54 +08001243 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001244 if (IS_ERR(vol_args)) {
1245 ret = PTR_ERR(vol_args);
1246 goto out;
1247 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001248
1249 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001250
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001251 sizestr = vol_args->name;
1252 devstr = strchr(sizestr, ':');
1253 if (devstr) {
1254 char *end;
1255 sizestr = devstr + 1;
1256 *devstr = '\0';
1257 devstr = vol_args->name;
1258 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001259 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001260 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001261 }
Yan Zheng2b820322008-11-17 21:11:30 -05001262 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001263 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001264 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001265 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001266 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001267 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001268 }
1269 if (!strcmp(sizestr, "max"))
1270 new_size = device->bdev->bd_inode->i_size;
1271 else {
1272 if (sizestr[0] == '-') {
1273 mod = -1;
1274 sizestr++;
1275 } else if (sizestr[0] == '+') {
1276 mod = 1;
1277 sizestr++;
1278 }
Akinobu Mita91748462010-02-28 10:59:11 +00001279 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001280 if (new_size == 0) {
1281 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001282 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001283 }
1284 }
1285
1286 old_size = device->total_bytes;
1287
1288 if (mod < 0) {
1289 if (new_size > old_size) {
1290 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001291 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001292 }
1293 new_size = old_size - new_size;
1294 } else if (mod > 0) {
1295 new_size = old_size + new_size;
1296 }
1297
1298 if (new_size < 256 * 1024 * 1024) {
1299 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001300 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001301 }
1302 if (new_size > device->bdev->bd_inode->i_size) {
1303 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001304 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001305 }
1306
1307 do_div(new_size, root->sectorsize);
1308 new_size *= root->sectorsize;
1309
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001310 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001311 device->name, (unsigned long long)new_size);
1312
1313 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001314 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001315 if (IS_ERR(trans)) {
1316 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001317 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001318 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001319 ret = btrfs_grow_device(trans, device, new_size);
1320 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001321 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001322 ret = btrfs_shrink_device(device, new_size);
1323 }
1324
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001325out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001326 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001327out:
1328 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001329 return ret;
1330}
1331
Sage Weil72fd0322010-10-29 15:41:32 -04001332static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1333 char *name,
1334 unsigned long fd,
1335 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001336 u64 *transid,
1337 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001338{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001339 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001340 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001341 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001342 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001343
Yan Zhengc146afa2008-11-12 14:34:12 -05001344 if (root->fs_info->sb->s_flags & MS_RDONLY)
1345 return -EROFS;
1346
Sage Weil72fd0322010-10-29 15:41:32 -04001347 namelen = strlen(name);
1348 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001349 ret = -EINVAL;
1350 goto out;
1351 }
1352
Chris Mason16780ca2012-02-20 22:14:55 -05001353 if (name[0] == '.' &&
1354 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1355 ret = -EEXIST;
1356 goto out;
1357 }
1358
Chris Mason3de45862008-11-17 21:02:50 -05001359 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001360 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001361 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001362 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001363 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001364 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001365 if (!src_file) {
1366 ret = -EINVAL;
1367 goto out;
1368 }
1369
1370 src_inode = src_file->f_path.dentry->d_inode;
1371 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001372 printk(KERN_INFO "btrfs: Snapshot src from "
1373 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001374 ret = -EINVAL;
1375 fput(src_file);
1376 goto out;
1377 }
Sage Weil72fd0322010-10-29 15:41:32 -04001378 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1379 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001380 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001381 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001382 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001383out:
Sage Weil72fd0322010-10-29 15:41:32 -04001384 return ret;
1385}
1386
1387static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001388 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001389{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001390 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001391 int ret;
1392
Li Zefanfa0d2b92010-12-20 15:53:28 +08001393 vol_args = memdup_user(arg, sizeof(*vol_args));
1394 if (IS_ERR(vol_args))
1395 return PTR_ERR(vol_args);
1396 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001397
Li Zefanfa0d2b92010-12-20 15:53:28 +08001398 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001399 vol_args->fd, subvol,
1400 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001401
Li Zefanfa0d2b92010-12-20 15:53:28 +08001402 kfree(vol_args);
1403 return ret;
1404}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001405
Li Zefanfa0d2b92010-12-20 15:53:28 +08001406static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1407 void __user *arg, int subvol)
1408{
1409 struct btrfs_ioctl_vol_args_v2 *vol_args;
1410 int ret;
1411 u64 transid = 0;
1412 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001413 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001414
Li Zefanfa0d2b92010-12-20 15:53:28 +08001415 vol_args = memdup_user(arg, sizeof(*vol_args));
1416 if (IS_ERR(vol_args))
1417 return PTR_ERR(vol_args);
1418 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001419
Li Zefanb83cc962010-12-20 16:04:08 +08001420 if (vol_args->flags &
1421 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1422 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001423 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001424 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001425
1426 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1427 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001428 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1429 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001430
1431 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001432 vol_args->fd, subvol,
1433 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001434
1435 if (ret == 0 && ptr &&
1436 copy_to_user(arg +
1437 offsetof(struct btrfs_ioctl_vol_args_v2,
1438 transid), ptr, sizeof(*ptr)))
1439 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001440out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001441 kfree(vol_args);
1442 return ret;
1443}
1444
Li Zefan0caa1022010-12-20 16:30:25 +08001445static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1446 void __user *arg)
1447{
1448 struct inode *inode = fdentry(file)->d_inode;
1449 struct btrfs_root *root = BTRFS_I(inode)->root;
1450 int ret = 0;
1451 u64 flags = 0;
1452
Li Zefan33345d012011-04-20 10:31:50 +08001453 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001454 return -EINVAL;
1455
1456 down_read(&root->fs_info->subvol_sem);
1457 if (btrfs_root_readonly(root))
1458 flags |= BTRFS_SUBVOL_RDONLY;
1459 up_read(&root->fs_info->subvol_sem);
1460
1461 if (copy_to_user(arg, &flags, sizeof(flags)))
1462 ret = -EFAULT;
1463
1464 return ret;
1465}
1466
1467static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1468 void __user *arg)
1469{
1470 struct inode *inode = fdentry(file)->d_inode;
1471 struct btrfs_root *root = BTRFS_I(inode)->root;
1472 struct btrfs_trans_handle *trans;
1473 u64 root_flags;
1474 u64 flags;
1475 int ret = 0;
1476
1477 if (root->fs_info->sb->s_flags & MS_RDONLY)
1478 return -EROFS;
1479
Li Zefan33345d012011-04-20 10:31:50 +08001480 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001481 return -EINVAL;
1482
1483 if (copy_from_user(&flags, arg, sizeof(flags)))
1484 return -EFAULT;
1485
Li Zefanb4dc2b82011-02-16 06:06:34 +00001486 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001487 return -EINVAL;
1488
1489 if (flags & ~BTRFS_SUBVOL_RDONLY)
1490 return -EOPNOTSUPP;
1491
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001492 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001493 return -EACCES;
1494
Li Zefan0caa1022010-12-20 16:30:25 +08001495 down_write(&root->fs_info->subvol_sem);
1496
1497 /* nothing to do */
1498 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1499 goto out;
1500
1501 root_flags = btrfs_root_flags(&root->root_item);
1502 if (flags & BTRFS_SUBVOL_RDONLY)
1503 btrfs_set_root_flags(&root->root_item,
1504 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1505 else
1506 btrfs_set_root_flags(&root->root_item,
1507 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1508
1509 trans = btrfs_start_transaction(root, 1);
1510 if (IS_ERR(trans)) {
1511 ret = PTR_ERR(trans);
1512 goto out_reset;
1513 }
1514
Li Zefanb4dc2b82011-02-16 06:06:34 +00001515 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001516 &root->root_key, &root->root_item);
1517
1518 btrfs_commit_transaction(trans, root);
1519out_reset:
1520 if (ret)
1521 btrfs_set_root_flags(&root->root_item, root_flags);
1522out:
1523 up_write(&root->fs_info->subvol_sem);
1524 return ret;
1525}
1526
Yan, Zheng76dda932009-09-21 16:00:26 -04001527/*
1528 * helper to check if the subvolume references other subvolumes
1529 */
1530static noinline int may_destroy_subvol(struct btrfs_root *root)
1531{
1532 struct btrfs_path *path;
1533 struct btrfs_key key;
1534 int ret;
1535
1536 path = btrfs_alloc_path();
1537 if (!path)
1538 return -ENOMEM;
1539
1540 key.objectid = root->root_key.objectid;
1541 key.type = BTRFS_ROOT_REF_KEY;
1542 key.offset = (u64)-1;
1543
1544 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1545 &key, path, 0, 0);
1546 if (ret < 0)
1547 goto out;
1548 BUG_ON(ret == 0);
1549
1550 ret = 0;
1551 if (path->slots[0] > 0) {
1552 path->slots[0]--;
1553 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1554 if (key.objectid == root->root_key.objectid &&
1555 key.type == BTRFS_ROOT_REF_KEY)
1556 ret = -ENOTEMPTY;
1557 }
1558out:
1559 btrfs_free_path(path);
1560 return ret;
1561}
1562
Chris Masonac8e9812010-02-28 15:39:26 -05001563static noinline int key_in_sk(struct btrfs_key *key,
1564 struct btrfs_ioctl_search_key *sk)
1565{
Chris Masonabc6e132010-03-18 12:10:08 -04001566 struct btrfs_key test;
1567 int ret;
1568
1569 test.objectid = sk->min_objectid;
1570 test.type = sk->min_type;
1571 test.offset = sk->min_offset;
1572
1573 ret = btrfs_comp_cpu_keys(key, &test);
1574 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001575 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001576
1577 test.objectid = sk->max_objectid;
1578 test.type = sk->max_type;
1579 test.offset = sk->max_offset;
1580
1581 ret = btrfs_comp_cpu_keys(key, &test);
1582 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001583 return 0;
1584 return 1;
1585}
1586
1587static noinline int copy_to_sk(struct btrfs_root *root,
1588 struct btrfs_path *path,
1589 struct btrfs_key *key,
1590 struct btrfs_ioctl_search_key *sk,
1591 char *buf,
1592 unsigned long *sk_offset,
1593 int *num_found)
1594{
1595 u64 found_transid;
1596 struct extent_buffer *leaf;
1597 struct btrfs_ioctl_search_header sh;
1598 unsigned long item_off;
1599 unsigned long item_len;
1600 int nritems;
1601 int i;
1602 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001603 int ret = 0;
1604
1605 leaf = path->nodes[0];
1606 slot = path->slots[0];
1607 nritems = btrfs_header_nritems(leaf);
1608
1609 if (btrfs_header_generation(leaf) > sk->max_transid) {
1610 i = nritems;
1611 goto advance_key;
1612 }
1613 found_transid = btrfs_header_generation(leaf);
1614
1615 for (i = slot; i < nritems; i++) {
1616 item_off = btrfs_item_ptr_offset(leaf, i);
1617 item_len = btrfs_item_size_nr(leaf, i);
1618
1619 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1620 item_len = 0;
1621
1622 if (sizeof(sh) + item_len + *sk_offset >
1623 BTRFS_SEARCH_ARGS_BUFSIZE) {
1624 ret = 1;
1625 goto overflow;
1626 }
1627
1628 btrfs_item_key_to_cpu(leaf, key, i);
1629 if (!key_in_sk(key, sk))
1630 continue;
1631
1632 sh.objectid = key->objectid;
1633 sh.offset = key->offset;
1634 sh.type = key->type;
1635 sh.len = item_len;
1636 sh.transid = found_transid;
1637
1638 /* copy search result header */
1639 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1640 *sk_offset += sizeof(sh);
1641
1642 if (item_len) {
1643 char *p = buf + *sk_offset;
1644 /* copy the item */
1645 read_extent_buffer(leaf, p,
1646 item_off, item_len);
1647 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001648 }
Hugo Millse2156862011-05-14 17:43:41 +00001649 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001650
1651 if (*num_found >= sk->nr_items)
1652 break;
1653 }
1654advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001655 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001656 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1657 key->offset++;
1658 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1659 key->offset = 0;
1660 key->type++;
1661 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1662 key->offset = 0;
1663 key->type = 0;
1664 key->objectid++;
1665 } else
1666 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001667overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001668 return ret;
1669}
1670
1671static noinline int search_ioctl(struct inode *inode,
1672 struct btrfs_ioctl_search_args *args)
1673{
1674 struct btrfs_root *root;
1675 struct btrfs_key key;
1676 struct btrfs_key max_key;
1677 struct btrfs_path *path;
1678 struct btrfs_ioctl_search_key *sk = &args->key;
1679 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1680 int ret;
1681 int num_found = 0;
1682 unsigned long sk_offset = 0;
1683
1684 path = btrfs_alloc_path();
1685 if (!path)
1686 return -ENOMEM;
1687
1688 if (sk->tree_id == 0) {
1689 /* search the root of the inode that was passed */
1690 root = BTRFS_I(inode)->root;
1691 } else {
1692 key.objectid = sk->tree_id;
1693 key.type = BTRFS_ROOT_ITEM_KEY;
1694 key.offset = (u64)-1;
1695 root = btrfs_read_fs_root_no_name(info, &key);
1696 if (IS_ERR(root)) {
1697 printk(KERN_ERR "could not find root %llu\n",
1698 sk->tree_id);
1699 btrfs_free_path(path);
1700 return -ENOENT;
1701 }
1702 }
1703
1704 key.objectid = sk->min_objectid;
1705 key.type = sk->min_type;
1706 key.offset = sk->min_offset;
1707
1708 max_key.objectid = sk->max_objectid;
1709 max_key.type = sk->max_type;
1710 max_key.offset = sk->max_offset;
1711
1712 path->keep_locks = 1;
1713
1714 while(1) {
1715 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1716 sk->min_transid);
1717 if (ret != 0) {
1718 if (ret > 0)
1719 ret = 0;
1720 goto err;
1721 }
1722 ret = copy_to_sk(root, path, &key, sk, args->buf,
1723 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001724 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001725 if (ret || num_found >= sk->nr_items)
1726 break;
1727
1728 }
1729 ret = 0;
1730err:
1731 sk->nr_items = num_found;
1732 btrfs_free_path(path);
1733 return ret;
1734}
1735
1736static noinline int btrfs_ioctl_tree_search(struct file *file,
1737 void __user *argp)
1738{
1739 struct btrfs_ioctl_search_args *args;
1740 struct inode *inode;
1741 int ret;
1742
1743 if (!capable(CAP_SYS_ADMIN))
1744 return -EPERM;
1745
Julia Lawall2354d082010-10-29 15:14:18 -04001746 args = memdup_user(argp, sizeof(*args));
1747 if (IS_ERR(args))
1748 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001749
Chris Masonac8e9812010-02-28 15:39:26 -05001750 inode = fdentry(file)->d_inode;
1751 ret = search_ioctl(inode, args);
1752 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1753 ret = -EFAULT;
1754 kfree(args);
1755 return ret;
1756}
1757
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001758/*
Chris Masonac8e9812010-02-28 15:39:26 -05001759 * Search INODE_REFs to identify path name of 'dirid' directory
1760 * in a 'tree_id' tree. and sets path name to 'name'.
1761 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001762static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1763 u64 tree_id, u64 dirid, char *name)
1764{
1765 struct btrfs_root *root;
1766 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001767 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001768 int ret = -1;
1769 int slot;
1770 int len;
1771 int total_len = 0;
1772 struct btrfs_inode_ref *iref;
1773 struct extent_buffer *l;
1774 struct btrfs_path *path;
1775
1776 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1777 name[0]='\0';
1778 return 0;
1779 }
1780
1781 path = btrfs_alloc_path();
1782 if (!path)
1783 return -ENOMEM;
1784
Chris Masonac8e9812010-02-28 15:39:26 -05001785 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001786
1787 key.objectid = tree_id;
1788 key.type = BTRFS_ROOT_ITEM_KEY;
1789 key.offset = (u64)-1;
1790 root = btrfs_read_fs_root_no_name(info, &key);
1791 if (IS_ERR(root)) {
1792 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001793 ret = -ENOENT;
1794 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001795 }
1796
1797 key.objectid = dirid;
1798 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001799 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001800
1801 while(1) {
1802 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1803 if (ret < 0)
1804 goto out;
1805
1806 l = path->nodes[0];
1807 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001808 if (ret > 0 && slot > 0)
1809 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001810 btrfs_item_key_to_cpu(l, &key, slot);
1811
1812 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001813 key.type != BTRFS_INODE_REF_KEY)) {
1814 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001815 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001816 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001817
1818 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1819 len = btrfs_inode_ref_name_len(l, iref);
1820 ptr -= len + 1;
1821 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001822 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001823 goto out;
1824
1825 *(ptr + len) = '/';
1826 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1827
1828 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1829 break;
1830
David Sterbab3b4aa72011-04-21 01:20:15 +02001831 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001832 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001833 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001834 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001835 }
Chris Masonac8e9812010-02-28 15:39:26 -05001836 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001837 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001838 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001839 name[total_len]='\0';
1840 ret = 0;
1841out:
1842 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001843 return ret;
1844}
1845
1846static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1847 void __user *argp)
1848{
1849 struct btrfs_ioctl_ino_lookup_args *args;
1850 struct inode *inode;
1851 int ret;
1852
1853 if (!capable(CAP_SYS_ADMIN))
1854 return -EPERM;
1855
Julia Lawall2354d082010-10-29 15:14:18 -04001856 args = memdup_user(argp, sizeof(*args));
1857 if (IS_ERR(args))
1858 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001859
Chris Masonac8e9812010-02-28 15:39:26 -05001860 inode = fdentry(file)->d_inode;
1861
Chris Mason1b53ac42010-03-18 12:17:05 -04001862 if (args->treeid == 0)
1863 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1864
Chris Masonac8e9812010-02-28 15:39:26 -05001865 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1866 args->treeid, args->objectid,
1867 args->name);
1868
1869 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1870 ret = -EFAULT;
1871
1872 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001873 return ret;
1874}
1875
Yan, Zheng76dda932009-09-21 16:00:26 -04001876static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1877 void __user *arg)
1878{
1879 struct dentry *parent = fdentry(file);
1880 struct dentry *dentry;
1881 struct inode *dir = parent->d_inode;
1882 struct inode *inode;
1883 struct btrfs_root *root = BTRFS_I(dir)->root;
1884 struct btrfs_root *dest = NULL;
1885 struct btrfs_ioctl_vol_args *vol_args;
1886 struct btrfs_trans_handle *trans;
1887 int namelen;
1888 int ret;
1889 int err = 0;
1890
Yan, Zheng76dda932009-09-21 16:00:26 -04001891 vol_args = memdup_user(arg, sizeof(*vol_args));
1892 if (IS_ERR(vol_args))
1893 return PTR_ERR(vol_args);
1894
1895 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1896 namelen = strlen(vol_args->name);
1897 if (strchr(vol_args->name, '/') ||
1898 strncmp(vol_args->name, "..", namelen) == 0) {
1899 err = -EINVAL;
1900 goto out;
1901 }
1902
Al Viroa561be72011-11-23 11:57:51 -05001903 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001904 if (err)
1905 goto out;
1906
1907 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1908 dentry = lookup_one_len(vol_args->name, parent, namelen);
1909 if (IS_ERR(dentry)) {
1910 err = PTR_ERR(dentry);
1911 goto out_unlock_dir;
1912 }
1913
1914 if (!dentry->d_inode) {
1915 err = -ENOENT;
1916 goto out_dput;
1917 }
1918
1919 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001920 dest = BTRFS_I(inode)->root;
1921 if (!capable(CAP_SYS_ADMIN)){
1922 /*
1923 * Regular user. Only allow this with a special mount
1924 * option, when the user has write+exec access to the
1925 * subvol root, and when rmdir(2) would have been
1926 * allowed.
1927 *
1928 * Note that this is _not_ check that the subvol is
1929 * empty or doesn't contain data that we wouldn't
1930 * otherwise be able to delete.
1931 *
1932 * Users who want to delete empty subvols should try
1933 * rmdir(2).
1934 */
1935 err = -EPERM;
1936 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1937 goto out_dput;
1938
1939 /*
1940 * Do not allow deletion if the parent dir is the same
1941 * as the dir to be deleted. That means the ioctl
1942 * must be called on the dentry referencing the root
1943 * of the subvol, not a random directory contained
1944 * within it.
1945 */
1946 err = -EINVAL;
1947 if (root == dest)
1948 goto out_dput;
1949
1950 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1951 if (err)
1952 goto out_dput;
1953
1954 /* check if subvolume may be deleted by a non-root user */
1955 err = btrfs_may_delete(dir, dentry, 1);
1956 if (err)
1957 goto out_dput;
1958 }
1959
Li Zefan33345d012011-04-20 10:31:50 +08001960 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04001961 err = -EINVAL;
1962 goto out_dput;
1963 }
1964
Yan, Zheng76dda932009-09-21 16:00:26 -04001965 mutex_lock(&inode->i_mutex);
1966 err = d_invalidate(dentry);
1967 if (err)
1968 goto out_unlock;
1969
1970 down_write(&root->fs_info->subvol_sem);
1971
1972 err = may_destroy_subvol(dest);
1973 if (err)
1974 goto out_up_write;
1975
Yan, Zhenga22285a2010-05-16 10:48:46 -04001976 trans = btrfs_start_transaction(root, 0);
1977 if (IS_ERR(trans)) {
1978 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001979 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001980 }
1981 trans->block_rsv = &root->fs_info->global_block_rsv;
1982
Yan, Zheng76dda932009-09-21 16:00:26 -04001983 ret = btrfs_unlink_subvol(trans, root, dir,
1984 dest->root_key.objectid,
1985 dentry->d_name.name,
1986 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001987 if (ret) {
1988 err = ret;
1989 btrfs_abort_transaction(trans, root, ret);
1990 goto out_end_trans;
1991 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001992
1993 btrfs_record_root_in_trans(trans, dest);
1994
1995 memset(&dest->root_item.drop_progress, 0,
1996 sizeof(dest->root_item.drop_progress));
1997 dest->root_item.drop_level = 0;
1998 btrfs_set_root_refs(&dest->root_item, 0);
1999
Yan, Zhengd68fc572010-05-16 10:49:58 -04002000 if (!xchg(&dest->orphan_item_inserted, 1)) {
2001 ret = btrfs_insert_orphan_item(trans,
2002 root->fs_info->tree_root,
2003 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002004 if (ret) {
2005 btrfs_abort_transaction(trans, root, ret);
2006 err = ret;
2007 goto out_end_trans;
2008 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002009 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002010out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002011 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002012 if (ret && !err)
2013 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002014 inode->i_flags |= S_DEAD;
2015out_up_write:
2016 up_write(&root->fs_info->subvol_sem);
2017out_unlock:
2018 mutex_unlock(&inode->i_mutex);
2019 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002020 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002021 btrfs_invalidate_inodes(dest);
2022 d_delete(dentry);
2023 }
2024out_dput:
2025 dput(dentry);
2026out_unlock_dir:
2027 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002028 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002029out:
2030 kfree(vol_args);
2031 return err;
2032}
2033
Chris Mason1e701a32010-03-11 09:42:04 -05002034static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002035{
2036 struct inode *inode = fdentry(file)->d_inode;
2037 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002038 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002039 int ret;
2040
Li Zefanb83cc962010-12-20 16:04:08 +08002041 if (btrfs_root_readonly(root))
2042 return -EROFS;
2043
Al Viroa561be72011-11-23 11:57:51 -05002044 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002045 if (ret)
2046 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002047
2048 switch (inode->i_mode & S_IFMT) {
2049 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002050 if (!capable(CAP_SYS_ADMIN)) {
2051 ret = -EPERM;
2052 goto out;
2053 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002054 ret = btrfs_defrag_root(root, 0);
2055 if (ret)
2056 goto out;
2057 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002058 break;
2059 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002060 if (!(file->f_mode & FMODE_WRITE)) {
2061 ret = -EINVAL;
2062 goto out;
2063 }
Chris Mason1e701a32010-03-11 09:42:04 -05002064
2065 range = kzalloc(sizeof(*range), GFP_KERNEL);
2066 if (!range) {
2067 ret = -ENOMEM;
2068 goto out;
2069 }
2070
2071 if (argp) {
2072 if (copy_from_user(range, argp,
2073 sizeof(*range))) {
2074 ret = -EFAULT;
2075 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002076 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002077 }
2078 /* compression requires us to start the IO */
2079 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2080 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2081 range->extent_thresh = (u32)-1;
2082 }
2083 } else {
2084 /* the rest are all set to zero by kzalloc */
2085 range->len = (u64)-1;
2086 }
Chris Mason4cb53002011-05-24 15:35:30 -04002087 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2088 range, 0, 0);
2089 if (ret > 0)
2090 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002091 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002092 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002093 default:
2094 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002095 }
Chris Masone441d542009-01-05 16:57:23 -05002096out:
Al Viro2a79f172011-12-09 08:06:57 -05002097 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002098 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002099}
2100
Christoph Hellwigb2950862008-12-02 09:54:17 -05002101static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002102{
2103 struct btrfs_ioctl_vol_args *vol_args;
2104 int ret;
2105
Chris Masone441d542009-01-05 16:57:23 -05002106 if (!capable(CAP_SYS_ADMIN))
2107 return -EPERM;
2108
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002109 mutex_lock(&root->fs_info->volume_mutex);
2110 if (root->fs_info->balance_ctl) {
2111 printk(KERN_INFO "btrfs: balance in progress\n");
2112 ret = -EINVAL;
2113 goto out;
2114 }
2115
Li Zefandae7b662009-04-08 15:06:54 +08002116 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002117 if (IS_ERR(vol_args)) {
2118 ret = PTR_ERR(vol_args);
2119 goto out;
2120 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002121
Mark Fasheh5516e592008-07-24 12:20:14 -04002122 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002123 ret = btrfs_init_new_device(root, vol_args->name);
2124
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002125 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002126out:
2127 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002128 return ret;
2129}
2130
Christoph Hellwigb2950862008-12-02 09:54:17 -05002131static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002132{
2133 struct btrfs_ioctl_vol_args *vol_args;
2134 int ret;
2135
Chris Masone441d542009-01-05 16:57:23 -05002136 if (!capable(CAP_SYS_ADMIN))
2137 return -EPERM;
2138
Yan Zhengc146afa2008-11-12 14:34:12 -05002139 if (root->fs_info->sb->s_flags & MS_RDONLY)
2140 return -EROFS;
2141
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002142 mutex_lock(&root->fs_info->volume_mutex);
2143 if (root->fs_info->balance_ctl) {
2144 printk(KERN_INFO "btrfs: balance in progress\n");
2145 ret = -EINVAL;
2146 goto out;
2147 }
2148
Li Zefandae7b662009-04-08 15:06:54 +08002149 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002150 if (IS_ERR(vol_args)) {
2151 ret = PTR_ERR(vol_args);
2152 goto out;
2153 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002154
Mark Fasheh5516e592008-07-24 12:20:14 -04002155 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002156 ret = btrfs_rm_device(root, vol_args->name);
2157
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002158 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002159out:
2160 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002161 return ret;
2162}
2163
Jan Schmidt475f6382011-03-11 15:41:01 +01002164static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2165{
Li Zefan027ed2f2011-06-08 08:27:56 +00002166 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002167 struct btrfs_device *device;
2168 struct btrfs_device *next;
2169 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002170 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002171
2172 if (!capable(CAP_SYS_ADMIN))
2173 return -EPERM;
2174
Li Zefan027ed2f2011-06-08 08:27:56 +00002175 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2176 if (!fi_args)
2177 return -ENOMEM;
2178
2179 fi_args->num_devices = fs_devices->num_devices;
2180 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002181
2182 mutex_lock(&fs_devices->device_list_mutex);
2183 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002184 if (device->devid > fi_args->max_id)
2185 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002186 }
2187 mutex_unlock(&fs_devices->device_list_mutex);
2188
Li Zefan027ed2f2011-06-08 08:27:56 +00002189 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2190 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002191
Li Zefan027ed2f2011-06-08 08:27:56 +00002192 kfree(fi_args);
2193 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002194}
2195
2196static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2197{
2198 struct btrfs_ioctl_dev_info_args *di_args;
2199 struct btrfs_device *dev;
2200 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2201 int ret = 0;
2202 char *s_uuid = NULL;
2203 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2204
2205 if (!capable(CAP_SYS_ADMIN))
2206 return -EPERM;
2207
2208 di_args = memdup_user(arg, sizeof(*di_args));
2209 if (IS_ERR(di_args))
2210 return PTR_ERR(di_args);
2211
2212 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2213 s_uuid = di_args->uuid;
2214
2215 mutex_lock(&fs_devices->device_list_mutex);
2216 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2217 mutex_unlock(&fs_devices->device_list_mutex);
2218
2219 if (!dev) {
2220 ret = -ENODEV;
2221 goto out;
2222 }
2223
2224 di_args->devid = dev->devid;
2225 di_args->bytes_used = dev->bytes_used;
2226 di_args->total_bytes = dev->total_bytes;
2227 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2228 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2229
2230out:
2231 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2232 ret = -EFAULT;
2233
2234 kfree(di_args);
2235 return ret;
2236}
2237
Yan, Zheng76dda932009-09-21 16:00:26 -04002238static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2239 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002240{
2241 struct inode *inode = fdentry(file)->d_inode;
2242 struct btrfs_root *root = BTRFS_I(inode)->root;
2243 struct file *src_file;
2244 struct inode *src;
2245 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002246 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002247 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002248 char *buf;
2249 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002250 u32 nritems;
2251 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002252 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002253 u64 len = olen;
2254 u64 bs = root->fs_info->sb->s_blocksize;
2255 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002256
Sage Weilc5c9cd42008-11-12 14:32:25 -05002257 /*
2258 * TODO:
2259 * - split compressed inline extents. annoying: we need to
2260 * decompress into destination's address_space (the file offset
2261 * may change, so source mapping won't do), then recompress (or
2262 * otherwise reinsert) a subrange.
2263 * - allow ranges within the same file to be cloned (provided
2264 * they don't overlap)?
2265 */
2266
Chris Masone441d542009-01-05 16:57:23 -05002267 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002268 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002269 return -EINVAL;
2270
Li Zefanb83cc962010-12-20 16:04:08 +08002271 if (btrfs_root_readonly(root))
2272 return -EROFS;
2273
Al Viroa561be72011-11-23 11:57:51 -05002274 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002275 if (ret)
2276 return ret;
2277
Sage Weilc5c9cd42008-11-12 14:32:25 -05002278 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002279 if (!src_file) {
2280 ret = -EBADF;
2281 goto out_drop_write;
2282 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002283
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002284 src = src_file->f_dentry->d_inode;
2285
Sage Weilc5c9cd42008-11-12 14:32:25 -05002286 ret = -EINVAL;
2287 if (src == inode)
2288 goto out_fput;
2289
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002290 /* the src must be open for reading */
2291 if (!(src_file->f_mode & FMODE_READ))
2292 goto out_fput;
2293
Li Zefan0e7b8242011-09-18 10:20:46 -04002294 /* don't make the dst file partly checksummed */
2295 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2296 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2297 goto out_fput;
2298
Yan Zhengae01a0a2008-08-04 23:23:47 -04002299 ret = -EISDIR;
2300 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002301 goto out_fput;
2302
Yan Zhengae01a0a2008-08-04 23:23:47 -04002303 ret = -EXDEV;
2304 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2305 goto out_fput;
2306
2307 ret = -ENOMEM;
2308 buf = vmalloc(btrfs_level_size(root, 0));
2309 if (!buf)
2310 goto out_fput;
2311
2312 path = btrfs_alloc_path();
2313 if (!path) {
2314 vfree(buf);
2315 goto out_fput;
2316 }
2317 path->reada = 2;
2318
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002319 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002320 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2321 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002322 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002323 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2324 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002325 }
2326
Sage Weilc5c9cd42008-11-12 14:32:25 -05002327 /* determine range to clone */
2328 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002329 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002330 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002331 if (len == 0)
2332 olen = len = src->i_size - off;
2333 /* if we extend to eof, continue to block boundary */
2334 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002335 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002336
2337 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002338 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2339 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002340 goto out_unlock;
2341
Li Zefand525e8a2011-09-11 10:52:25 -04002342 if (destoff > inode->i_size) {
2343 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2344 if (ret)
2345 goto out_unlock;
2346 }
2347
Li Zefan71ef0782011-09-18 10:20:46 -04002348 /* truncate page cache pages from target inode range */
2349 truncate_inode_pages_range(&inode->i_data, destoff,
2350 PAGE_CACHE_ALIGN(destoff + len) - 1);
2351
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002352 /* do any pending delalloc/csum calc on src, one way or
2353 another, and lock file content */
2354 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002355 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002356 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002357 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2358 if (!ordered &&
2359 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2360 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002361 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002362 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002363 if (ordered)
2364 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002365 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002366 }
2367
Sage Weilc5c9cd42008-11-12 14:32:25 -05002368 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002369 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002370 key.type = BTRFS_EXTENT_DATA_KEY;
2371 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002372
2373 while (1) {
2374 /*
2375 * note the key will change type as we walk through the
2376 * tree.
2377 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002378 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002379 if (ret < 0)
2380 goto out;
2381
Yan Zhengae01a0a2008-08-04 23:23:47 -04002382 nritems = btrfs_header_nritems(path->nodes[0]);
2383 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002384 ret = btrfs_next_leaf(root, path);
2385 if (ret < 0)
2386 goto out;
2387 if (ret > 0)
2388 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002389 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002390 }
2391 leaf = path->nodes[0];
2392 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002393
Yan Zhengae01a0a2008-08-04 23:23:47 -04002394 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002395 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002396 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002397 break;
2398
Sage Weilc5c9cd42008-11-12 14:32:25 -05002399 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2400 struct btrfs_file_extent_item *extent;
2401 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002402 u32 size;
2403 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002404 u64 disko = 0, diskl = 0;
2405 u64 datao = 0, datal = 0;
2406 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002407 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002408
2409 size = btrfs_item_size_nr(leaf, slot);
2410 read_extent_buffer(leaf, buf,
2411 btrfs_item_ptr_offset(leaf, slot),
2412 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002413
2414 extent = btrfs_item_ptr(leaf, slot,
2415 struct btrfs_file_extent_item);
2416 comp = btrfs_file_extent_compression(leaf, extent);
2417 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002418 if (type == BTRFS_FILE_EXTENT_REG ||
2419 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002420 disko = btrfs_file_extent_disk_bytenr(leaf,
2421 extent);
2422 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2423 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002424 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002425 datal = btrfs_file_extent_num_bytes(leaf,
2426 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002427 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2428 /* take upper bound, may be compressed */
2429 datal = btrfs_file_extent_ram_bytes(leaf,
2430 extent);
2431 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002432 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002433
Sage Weil050006a2010-10-29 15:37:33 -04002434 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002435 key.offset >= off+len)
2436 goto next;
2437
Zheng Yan31840ae2008-09-23 13:14:14 -04002438 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002439 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002440 if (off <= key.offset)
2441 new_key.offset = key.offset + destoff - off;
2442 else
2443 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002444
Sage Weilb6f34092011-09-20 14:48:51 -04002445 /*
2446 * 1 - adjusting old extent (we may have to split it)
2447 * 1 - add new extent
2448 * 1 - inode update
2449 */
2450 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002451 if (IS_ERR(trans)) {
2452 ret = PTR_ERR(trans);
2453 goto out;
2454 }
2455
Chris Masonc8a894d2009-06-27 21:07:03 -04002456 if (type == BTRFS_FILE_EXTENT_REG ||
2457 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002458 /*
2459 * a | --- range to clone ---| b
2460 * | ------------- extent ------------- |
2461 */
2462
2463 /* substract range b */
2464 if (key.offset + datal > off + len)
2465 datal = off + len - key.offset;
2466
2467 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002468 if (off > key.offset) {
2469 datao += off - key.offset;
2470 datal -= off - key.offset;
2471 }
2472
Yan, Zhenga22285a2010-05-16 10:48:46 -04002473 ret = btrfs_drop_extents(trans, inode,
2474 new_key.offset,
2475 new_key.offset + datal,
2476 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002477 if (ret) {
2478 btrfs_abort_transaction(trans, root,
2479 ret);
2480 btrfs_end_transaction(trans, root);
2481 goto out;
2482 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002483
Sage Weilc5c9cd42008-11-12 14:32:25 -05002484 ret = btrfs_insert_empty_item(trans, root, path,
2485 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002486 if (ret) {
2487 btrfs_abort_transaction(trans, root,
2488 ret);
2489 btrfs_end_transaction(trans, root);
2490 goto out;
2491 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002492
2493 leaf = path->nodes[0];
2494 slot = path->slots[0];
2495 write_extent_buffer(leaf, buf,
2496 btrfs_item_ptr_offset(leaf, slot),
2497 size);
2498
2499 extent = btrfs_item_ptr(leaf, slot,
2500 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002501
Sage Weilc5c9cd42008-11-12 14:32:25 -05002502 /* disko == 0 means it's a hole */
2503 if (!disko)
2504 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002505
2506 btrfs_set_file_extent_offset(leaf, extent,
2507 datao);
2508 btrfs_set_file_extent_num_bytes(leaf, extent,
2509 datal);
2510 if (disko) {
2511 inode_add_bytes(inode, datal);
2512 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002513 disko, diskl, 0,
2514 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002515 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002516 new_key.offset - datao,
2517 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002518 if (ret) {
2519 btrfs_abort_transaction(trans,
2520 root,
2521 ret);
2522 btrfs_end_transaction(trans,
2523 root);
2524 goto out;
2525
2526 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002527 }
2528 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2529 u64 skip = 0;
2530 u64 trim = 0;
2531 if (off > key.offset) {
2532 skip = off - key.offset;
2533 new_key.offset += skip;
2534 }
Chris Masond3977122009-01-05 21:25:51 -05002535
Sage Weilc5c9cd42008-11-12 14:32:25 -05002536 if (key.offset + datal > off+len)
2537 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002538
Sage Weilc5c9cd42008-11-12 14:32:25 -05002539 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002540 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002541 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002542 goto out;
2543 }
2544 size -= skip + trim;
2545 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002546
2547 ret = btrfs_drop_extents(trans, inode,
2548 new_key.offset,
2549 new_key.offset + datal,
2550 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002551 if (ret) {
2552 btrfs_abort_transaction(trans, root,
2553 ret);
2554 btrfs_end_transaction(trans, root);
2555 goto out;
2556 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002557
Sage Weilc5c9cd42008-11-12 14:32:25 -05002558 ret = btrfs_insert_empty_item(trans, root, path,
2559 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002560 if (ret) {
2561 btrfs_abort_transaction(trans, root,
2562 ret);
2563 btrfs_end_transaction(trans, root);
2564 goto out;
2565 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002566
2567 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002568 u32 start =
2569 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002570 memmove(buf+start, buf+start+skip,
2571 datal);
2572 }
2573
2574 leaf = path->nodes[0];
2575 slot = path->slots[0];
2576 write_extent_buffer(leaf, buf,
2577 btrfs_item_ptr_offset(leaf, slot),
2578 size);
2579 inode_add_bytes(inode, datal);
2580 }
2581
2582 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002583 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002584
Yan, Zhenga22285a2010-05-16 10:48:46 -04002585 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002586
2587 /*
2588 * we round up to the block size at eof when
2589 * determining which extents to clone above,
2590 * but shouldn't round up the file size
2591 */
2592 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002593 if (endoff > destoff+olen)
2594 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002595 if (endoff > inode->i_size)
2596 btrfs_i_size_write(inode, endoff);
2597
Yan, Zhenga22285a2010-05-16 10:48:46 -04002598 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002599 if (ret) {
2600 btrfs_abort_transaction(trans, root, ret);
2601 btrfs_end_transaction(trans, root);
2602 goto out;
2603 }
2604 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002605 }
Chris Masond3977122009-01-05 21:25:51 -05002606next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002607 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002608 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002609 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002610 ret = 0;
2611out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002612 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002613 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002614out_unlock:
2615 mutex_unlock(&src->i_mutex);
2616 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002617 vfree(buf);
2618 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002619out_fput:
2620 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002621out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002622 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002623 return ret;
2624}
2625
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002626static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002627{
2628 struct btrfs_ioctl_clone_range_args args;
2629
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002630 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002631 return -EFAULT;
2632 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2633 args.src_length, args.dest_offset);
2634}
2635
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002636/*
2637 * there are many ways the trans_start and trans_end ioctls can lead
2638 * to deadlocks. They should only be used by applications that
2639 * basically own the machine, and have a very in depth understanding
2640 * of all the possible deadlocks and enospc problems.
2641 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002642static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002643{
2644 struct inode *inode = fdentry(file)->d_inode;
2645 struct btrfs_root *root = BTRFS_I(inode)->root;
2646 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002647 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002648
Sage Weil1ab86ae2009-09-29 18:38:44 -04002649 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002650 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002651 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002652
2653 ret = -EINPROGRESS;
2654 if (file->private_data)
2655 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002656
Li Zefanb83cc962010-12-20 16:04:08 +08002657 ret = -EROFS;
2658 if (btrfs_root_readonly(root))
2659 goto out;
2660
Al Viroa561be72011-11-23 11:57:51 -05002661 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002662 if (ret)
2663 goto out;
2664
Josef Bacika4abeea2011-04-11 17:25:13 -04002665 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002666
Sage Weil1ab86ae2009-09-29 18:38:44 -04002667 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002668 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002669 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002670 goto out_drop;
2671
2672 file->private_data = trans;
2673 return 0;
2674
2675out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002676 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002677 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002678out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002679 return ret;
2680}
2681
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002682static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2683{
2684 struct inode *inode = fdentry(file)->d_inode;
2685 struct btrfs_root *root = BTRFS_I(inode)->root;
2686 struct btrfs_root *new_root;
2687 struct btrfs_dir_item *di;
2688 struct btrfs_trans_handle *trans;
2689 struct btrfs_path *path;
2690 struct btrfs_key location;
2691 struct btrfs_disk_key disk_key;
2692 struct btrfs_super_block *disk_super;
2693 u64 features;
2694 u64 objectid = 0;
2695 u64 dir_id;
2696
2697 if (!capable(CAP_SYS_ADMIN))
2698 return -EPERM;
2699
2700 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2701 return -EFAULT;
2702
2703 if (!objectid)
2704 objectid = root->root_key.objectid;
2705
2706 location.objectid = objectid;
2707 location.type = BTRFS_ROOT_ITEM_KEY;
2708 location.offset = (u64)-1;
2709
2710 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2711 if (IS_ERR(new_root))
2712 return PTR_ERR(new_root);
2713
2714 if (btrfs_root_refs(&new_root->root_item) == 0)
2715 return -ENOENT;
2716
2717 path = btrfs_alloc_path();
2718 if (!path)
2719 return -ENOMEM;
2720 path->leave_spinning = 1;
2721
2722 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002723 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002724 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002725 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002726 }
2727
David Sterba6c417612011-04-13 15:41:04 +02002728 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002729 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2730 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002731 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002732 btrfs_free_path(path);
2733 btrfs_end_transaction(trans, root);
2734 printk(KERN_ERR "Umm, you don't have the default dir item, "
2735 "this isn't going to work\n");
2736 return -ENOENT;
2737 }
2738
2739 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2740 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2741 btrfs_mark_buffer_dirty(path->nodes[0]);
2742 btrfs_free_path(path);
2743
David Sterba6c417612011-04-13 15:41:04 +02002744 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002745 features = btrfs_super_incompat_flags(disk_super);
2746 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2747 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2748 btrfs_set_super_incompat_flags(disk_super, features);
2749 }
2750 btrfs_end_transaction(trans, root);
2751
2752 return 0;
2753}
2754
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002755static void get_block_group_info(struct list_head *groups_list,
2756 struct btrfs_ioctl_space_info *space)
2757{
2758 struct btrfs_block_group_cache *block_group;
2759
2760 space->total_bytes = 0;
2761 space->used_bytes = 0;
2762 space->flags = 0;
2763 list_for_each_entry(block_group, groups_list, list) {
2764 space->flags = block_group->flags;
2765 space->total_bytes += block_group->key.offset;
2766 space->used_bytes +=
2767 btrfs_block_group_used(&block_group->item);
2768 }
2769}
2770
Josef Bacik1406e432010-01-13 18:19:06 +00002771long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2772{
2773 struct btrfs_ioctl_space_args space_args;
2774 struct btrfs_ioctl_space_info space;
2775 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002776 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002777 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002778 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002779 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2780 BTRFS_BLOCK_GROUP_SYSTEM,
2781 BTRFS_BLOCK_GROUP_METADATA,
2782 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2783 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002784 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002785 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002786 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002787 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002788
2789 if (copy_from_user(&space_args,
2790 (struct btrfs_ioctl_space_args __user *)arg,
2791 sizeof(space_args)))
2792 return -EFAULT;
2793
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002794 for (i = 0; i < num_types; i++) {
2795 struct btrfs_space_info *tmp;
2796
2797 info = NULL;
2798 rcu_read_lock();
2799 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2800 list) {
2801 if (tmp->flags == types[i]) {
2802 info = tmp;
2803 break;
2804 }
2805 }
2806 rcu_read_unlock();
2807
2808 if (!info)
2809 continue;
2810
2811 down_read(&info->groups_sem);
2812 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2813 if (!list_empty(&info->block_groups[c]))
2814 slot_count++;
2815 }
2816 up_read(&info->groups_sem);
2817 }
Josef Bacik1406e432010-01-13 18:19:06 +00002818
Chris Mason7fde62b2010-03-16 15:40:10 -04002819 /* space_slots == 0 means they are asking for a count */
2820 if (space_args.space_slots == 0) {
2821 space_args.total_spaces = slot_count;
2822 goto out;
2823 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002824
Dan Rosenberg51788b12011-02-14 16:04:23 -05002825 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002826
Chris Mason7fde62b2010-03-16 15:40:10 -04002827 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002828
Chris Mason7fde62b2010-03-16 15:40:10 -04002829 /* we generally have at most 6 or so space infos, one for each raid
2830 * level. So, a whole page should be more than enough for everyone
2831 */
2832 if (alloc_size > PAGE_CACHE_SIZE)
2833 return -ENOMEM;
2834
2835 space_args.total_spaces = 0;
2836 dest = kmalloc(alloc_size, GFP_NOFS);
2837 if (!dest)
2838 return -ENOMEM;
2839 dest_orig = dest;
2840
2841 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002842 for (i = 0; i < num_types; i++) {
2843 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002844
Dan Rosenberg51788b12011-02-14 16:04:23 -05002845 if (!slot_count)
2846 break;
2847
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002848 info = NULL;
2849 rcu_read_lock();
2850 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2851 list) {
2852 if (tmp->flags == types[i]) {
2853 info = tmp;
2854 break;
2855 }
2856 }
2857 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002858
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002859 if (!info)
2860 continue;
2861 down_read(&info->groups_sem);
2862 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2863 if (!list_empty(&info->block_groups[c])) {
2864 get_block_group_info(&info->block_groups[c],
2865 &space);
2866 memcpy(dest, &space, sizeof(space));
2867 dest++;
2868 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002869 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002870 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002871 if (!slot_count)
2872 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002873 }
2874 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002875 }
Josef Bacik1406e432010-01-13 18:19:06 +00002876
Chris Mason7fde62b2010-03-16 15:40:10 -04002877 user_dest = (struct btrfs_ioctl_space_info *)
2878 (arg + sizeof(struct btrfs_ioctl_space_args));
2879
2880 if (copy_to_user(user_dest, dest_orig, alloc_size))
2881 ret = -EFAULT;
2882
2883 kfree(dest_orig);
2884out:
2885 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002886 ret = -EFAULT;
2887
2888 return ret;
2889}
2890
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002891/*
2892 * there are many ways the trans_start and trans_end ioctls can lead
2893 * to deadlocks. They should only be used by applications that
2894 * basically own the machine, and have a very in depth understanding
2895 * of all the possible deadlocks and enospc problems.
2896 */
2897long btrfs_ioctl_trans_end(struct file *file)
2898{
2899 struct inode *inode = fdentry(file)->d_inode;
2900 struct btrfs_root *root = BTRFS_I(inode)->root;
2901 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002902
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002903 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002904 if (!trans)
2905 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002906 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002907
Sage Weil1ab86ae2009-09-29 18:38:44 -04002908 btrfs_end_transaction(trans, root);
2909
Josef Bacika4abeea2011-04-11 17:25:13 -04002910 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002911
Al Viro2a79f172011-12-09 08:06:57 -05002912 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002913 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002914}
2915
Sage Weil46204592010-10-29 15:41:32 -04002916static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2917{
2918 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2919 struct btrfs_trans_handle *trans;
2920 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002921 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002922
2923 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002924 if (IS_ERR(trans))
2925 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002926 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002927 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002928 if (ret) {
2929 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002930 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002931 }
Sage Weil46204592010-10-29 15:41:32 -04002932
2933 if (argp)
2934 if (copy_to_user(argp, &transid, sizeof(transid)))
2935 return -EFAULT;
2936 return 0;
2937}
2938
2939static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2940{
2941 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2942 u64 transid;
2943
2944 if (argp) {
2945 if (copy_from_user(&transid, argp, sizeof(transid)))
2946 return -EFAULT;
2947 } else {
2948 transid = 0; /* current trans */
2949 }
2950 return btrfs_wait_for_commit(root, transid);
2951}
2952
Jan Schmidt475f6382011-03-11 15:41:01 +01002953static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2954{
2955 int ret;
2956 struct btrfs_ioctl_scrub_args *sa;
2957
2958 if (!capable(CAP_SYS_ADMIN))
2959 return -EPERM;
2960
2961 sa = memdup_user(arg, sizeof(*sa));
2962 if (IS_ERR(sa))
2963 return PTR_ERR(sa);
2964
2965 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01002966 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01002967
2968 if (copy_to_user(arg, sa, sizeof(*sa)))
2969 ret = -EFAULT;
2970
2971 kfree(sa);
2972 return ret;
2973}
2974
2975static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2976{
2977 if (!capable(CAP_SYS_ADMIN))
2978 return -EPERM;
2979
2980 return btrfs_scrub_cancel(root);
2981}
2982
2983static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2984 void __user *arg)
2985{
2986 struct btrfs_ioctl_scrub_args *sa;
2987 int ret;
2988
2989 if (!capable(CAP_SYS_ADMIN))
2990 return -EPERM;
2991
2992 sa = memdup_user(arg, sizeof(*sa));
2993 if (IS_ERR(sa))
2994 return PTR_ERR(sa);
2995
2996 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2997
2998 if (copy_to_user(arg, sa, sizeof(*sa)))
2999 ret = -EFAULT;
3000
3001 kfree(sa);
3002 return ret;
3003}
3004
Jan Schmidtd7728c92011-07-07 16:48:38 +02003005static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3006{
3007 int ret = 0;
3008 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003009 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003010 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003011 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003012 struct inode_fs_paths *ipath = NULL;
3013 struct btrfs_path *path;
3014
3015 if (!capable(CAP_SYS_ADMIN))
3016 return -EPERM;
3017
3018 path = btrfs_alloc_path();
3019 if (!path) {
3020 ret = -ENOMEM;
3021 goto out;
3022 }
3023
3024 ipa = memdup_user(arg, sizeof(*ipa));
3025 if (IS_ERR(ipa)) {
3026 ret = PTR_ERR(ipa);
3027 ipa = NULL;
3028 goto out;
3029 }
3030
3031 size = min_t(u32, ipa->size, 4096);
3032 ipath = init_ipath(size, root, path);
3033 if (IS_ERR(ipath)) {
3034 ret = PTR_ERR(ipath);
3035 ipath = NULL;
3036 goto out;
3037 }
3038
3039 ret = paths_from_inode(ipa->inum, ipath);
3040 if (ret < 0)
3041 goto out;
3042
3043 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003044 rel_ptr = ipath->fspath->val[i] -
3045 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003046 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003047 }
3048
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003049 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3050 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003051 if (ret) {
3052 ret = -EFAULT;
3053 goto out;
3054 }
3055
3056out:
3057 btrfs_free_path(path);
3058 free_ipath(ipath);
3059 kfree(ipa);
3060
3061 return ret;
3062}
3063
3064static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3065{
3066 struct btrfs_data_container *inodes = ctx;
3067 const size_t c = 3 * sizeof(u64);
3068
3069 if (inodes->bytes_left >= c) {
3070 inodes->bytes_left -= c;
3071 inodes->val[inodes->elem_cnt] = inum;
3072 inodes->val[inodes->elem_cnt + 1] = offset;
3073 inodes->val[inodes->elem_cnt + 2] = root;
3074 inodes->elem_cnt += 3;
3075 } else {
3076 inodes->bytes_missing += c - inodes->bytes_left;
3077 inodes->bytes_left = 0;
3078 inodes->elem_missed += 3;
3079 }
3080
3081 return 0;
3082}
3083
3084static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3085 void __user *arg)
3086{
3087 int ret = 0;
3088 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003089 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003090 struct btrfs_ioctl_logical_ino_args *loi;
3091 struct btrfs_data_container *inodes = NULL;
3092 struct btrfs_path *path = NULL;
3093 struct btrfs_key key;
3094
3095 if (!capable(CAP_SYS_ADMIN))
3096 return -EPERM;
3097
3098 loi = memdup_user(arg, sizeof(*loi));
3099 if (IS_ERR(loi)) {
3100 ret = PTR_ERR(loi);
3101 loi = NULL;
3102 goto out;
3103 }
3104
3105 path = btrfs_alloc_path();
3106 if (!path) {
3107 ret = -ENOMEM;
3108 goto out;
3109 }
3110
3111 size = min_t(u32, loi->size, 4096);
3112 inodes = init_data_container(size);
3113 if (IS_ERR(inodes)) {
3114 ret = PTR_ERR(inodes);
3115 inodes = NULL;
3116 goto out;
3117 }
3118
3119 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003120 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003121
3122 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3123 ret = -ENOENT;
3124 if (ret < 0)
3125 goto out;
3126
Jan Schmidt4692cf52011-12-02 14:56:41 +01003127 extent_item_pos = loi->logical - key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01003128 ret = iterate_extent_inodes(root->fs_info, key.objectid,
3129 extent_item_pos, 0, build_ino_list,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003130 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003131
3132 if (ret < 0)
3133 goto out;
3134
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003135 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3136 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003137 if (ret)
3138 ret = -EFAULT;
3139
3140out:
3141 btrfs_free_path(path);
3142 kfree(inodes);
3143 kfree(loi);
3144
3145 return ret;
3146}
3147
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003148void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003149 struct btrfs_ioctl_balance_args *bargs)
3150{
3151 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3152
3153 bargs->flags = bctl->flags;
3154
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003155 if (atomic_read(&fs_info->balance_running))
3156 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3157 if (atomic_read(&fs_info->balance_pause_req))
3158 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003159 if (atomic_read(&fs_info->balance_cancel_req))
3160 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003161
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003162 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3163 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3164 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003165
3166 if (lock) {
3167 spin_lock(&fs_info->balance_lock);
3168 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3169 spin_unlock(&fs_info->balance_lock);
3170 } else {
3171 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3172 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003173}
3174
3175static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3176{
3177 struct btrfs_fs_info *fs_info = root->fs_info;
3178 struct btrfs_ioctl_balance_args *bargs;
3179 struct btrfs_balance_control *bctl;
3180 int ret;
3181
3182 if (!capable(CAP_SYS_ADMIN))
3183 return -EPERM;
3184
3185 if (fs_info->sb->s_flags & MS_RDONLY)
3186 return -EROFS;
3187
3188 mutex_lock(&fs_info->volume_mutex);
3189 mutex_lock(&fs_info->balance_mutex);
3190
3191 if (arg) {
3192 bargs = memdup_user(arg, sizeof(*bargs));
3193 if (IS_ERR(bargs)) {
3194 ret = PTR_ERR(bargs);
3195 goto out;
3196 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003197
3198 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3199 if (!fs_info->balance_ctl) {
3200 ret = -ENOTCONN;
3201 goto out_bargs;
3202 }
3203
3204 bctl = fs_info->balance_ctl;
3205 spin_lock(&fs_info->balance_lock);
3206 bctl->flags |= BTRFS_BALANCE_RESUME;
3207 spin_unlock(&fs_info->balance_lock);
3208
3209 goto do_balance;
3210 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003211 } else {
3212 bargs = NULL;
3213 }
3214
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003215 if (fs_info->balance_ctl) {
3216 ret = -EINPROGRESS;
3217 goto out_bargs;
3218 }
3219
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003220 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3221 if (!bctl) {
3222 ret = -ENOMEM;
3223 goto out_bargs;
3224 }
3225
3226 bctl->fs_info = fs_info;
3227 if (arg) {
3228 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3229 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3230 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3231
3232 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003233 } else {
3234 /* balance everything - no filters */
3235 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003236 }
3237
Ilya Dryomovde322262012-01-16 22:04:49 +02003238do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003239 ret = btrfs_balance(bctl, bargs);
3240 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003241 * bctl is freed in __cancel_balance or in free_fs_info if
3242 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003243 */
3244 if (arg) {
3245 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3246 ret = -EFAULT;
3247 }
3248
3249out_bargs:
3250 kfree(bargs);
3251out:
3252 mutex_unlock(&fs_info->balance_mutex);
3253 mutex_unlock(&fs_info->volume_mutex);
3254 return ret;
3255}
3256
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003257static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3258{
3259 if (!capable(CAP_SYS_ADMIN))
3260 return -EPERM;
3261
3262 switch (cmd) {
3263 case BTRFS_BALANCE_CTL_PAUSE:
3264 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003265 case BTRFS_BALANCE_CTL_CANCEL:
3266 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003267 }
3268
3269 return -EINVAL;
3270}
3271
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003272static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3273 void __user *arg)
3274{
3275 struct btrfs_fs_info *fs_info = root->fs_info;
3276 struct btrfs_ioctl_balance_args *bargs;
3277 int ret = 0;
3278
3279 if (!capable(CAP_SYS_ADMIN))
3280 return -EPERM;
3281
3282 mutex_lock(&fs_info->balance_mutex);
3283 if (!fs_info->balance_ctl) {
3284 ret = -ENOTCONN;
3285 goto out;
3286 }
3287
3288 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3289 if (!bargs) {
3290 ret = -ENOMEM;
3291 goto out;
3292 }
3293
3294 update_ioctl_balance_args(fs_info, 1, bargs);
3295
3296 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3297 ret = -EFAULT;
3298
3299 kfree(bargs);
3300out:
3301 mutex_unlock(&fs_info->balance_mutex);
3302 return ret;
3303}
3304
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003305long btrfs_ioctl(struct file *file, unsigned int
3306 cmd, unsigned long arg)
3307{
3308 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003309 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003310
3311 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003312 case FS_IOC_GETFLAGS:
3313 return btrfs_ioctl_getflags(file, argp);
3314 case FS_IOC_SETFLAGS:
3315 return btrfs_ioctl_setflags(file, argp);
3316 case FS_IOC_GETVERSION:
3317 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003318 case FITRIM:
3319 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003320 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003321 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003322 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003323 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003324 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003325 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003326 case BTRFS_IOC_SNAP_DESTROY:
3327 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003328 case BTRFS_IOC_SUBVOL_GETFLAGS:
3329 return btrfs_ioctl_subvol_getflags(file, argp);
3330 case BTRFS_IOC_SUBVOL_SETFLAGS:
3331 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003332 case BTRFS_IOC_DEFAULT_SUBVOL:
3333 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003334 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003335 return btrfs_ioctl_defrag(file, NULL);
3336 case BTRFS_IOC_DEFRAG_RANGE:
3337 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003338 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003339 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003340 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003341 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003342 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003343 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003344 case BTRFS_IOC_FS_INFO:
3345 return btrfs_ioctl_fs_info(root, argp);
3346 case BTRFS_IOC_DEV_INFO:
3347 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003348 case BTRFS_IOC_BALANCE:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003349 return btrfs_ioctl_balance(root, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003350 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003351 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3352 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003353 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003354 case BTRFS_IOC_TRANS_START:
3355 return btrfs_ioctl_trans_start(file);
3356 case BTRFS_IOC_TRANS_END:
3357 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003358 case BTRFS_IOC_TREE_SEARCH:
3359 return btrfs_ioctl_tree_search(file, argp);
3360 case BTRFS_IOC_INO_LOOKUP:
3361 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003362 case BTRFS_IOC_INO_PATHS:
3363 return btrfs_ioctl_ino_to_path(root, argp);
3364 case BTRFS_IOC_LOGICAL_INO:
3365 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003366 case BTRFS_IOC_SPACE_INFO:
3367 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003368 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003369 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3370 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003371 case BTRFS_IOC_START_SYNC:
3372 return btrfs_ioctl_start_sync(file, argp);
3373 case BTRFS_IOC_WAIT_SYNC:
3374 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003375 case BTRFS_IOC_SCRUB:
3376 return btrfs_ioctl_scrub(root, argp);
3377 case BTRFS_IOC_SCRUB_CANCEL:
3378 return btrfs_ioctl_scrub_cancel(root, argp);
3379 case BTRFS_IOC_SCRUB_PROGRESS:
3380 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003381 case BTRFS_IOC_BALANCE_V2:
3382 return btrfs_ioctl_balance(root, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003383 case BTRFS_IOC_BALANCE_CTL:
3384 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003385 case BTRFS_IOC_BALANCE_PROGRESS:
3386 return btrfs_ioctl_balance_progress(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003387 }
3388
3389 return -ENOTTY;
3390}