blob: afde837644e55f3e7c7db58ea693d37680797ca3 [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040052#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080053#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020054#include "backref.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040055
Christoph Hellwig6cbff002009-04-17 10:37:41 +020056/* Mask out flags that are inappropriate for the given type of inode. */
57static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
58{
59 if (S_ISDIR(mode))
60 return flags;
61 else if (S_ISREG(mode))
62 return flags & ~FS_DIRSYNC_FL;
63 else
64 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040066
Christoph Hellwig6cbff002009-04-17 10:37:41 +020067/*
68 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
69 */
70static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
71{
72 unsigned int iflags = 0;
73
74 if (flags & BTRFS_INODE_SYNC)
75 iflags |= FS_SYNC_FL;
76 if (flags & BTRFS_INODE_IMMUTABLE)
77 iflags |= FS_IMMUTABLE_FL;
78 if (flags & BTRFS_INODE_APPEND)
79 iflags |= FS_APPEND_FL;
80 if (flags & BTRFS_INODE_NODUMP)
81 iflags |= FS_NODUMP_FL;
82 if (flags & BTRFS_INODE_NOATIME)
83 iflags |= FS_NOATIME_FL;
84 if (flags & BTRFS_INODE_DIRSYNC)
85 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000086 if (flags & BTRFS_INODE_NODATACOW)
87 iflags |= FS_NOCOW_FL;
88
89 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
90 iflags |= FS_COMPR_FL;
91 else if (flags & BTRFS_INODE_NOCOMPRESS)
92 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093
94 return iflags;
95}
96
97/*
98 * Update inode->i_flags based on the btrfs internal flags.
99 */
100void btrfs_update_iflags(struct inode *inode)
101{
102 struct btrfs_inode *ip = BTRFS_I(inode);
103
104 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
105
106 if (ip->flags & BTRFS_INODE_SYNC)
107 inode->i_flags |= S_SYNC;
108 if (ip->flags & BTRFS_INODE_IMMUTABLE)
109 inode->i_flags |= S_IMMUTABLE;
110 if (ip->flags & BTRFS_INODE_APPEND)
111 inode->i_flags |= S_APPEND;
112 if (ip->flags & BTRFS_INODE_NOATIME)
113 inode->i_flags |= S_NOATIME;
114 if (ip->flags & BTRFS_INODE_DIRSYNC)
115 inode->i_flags |= S_DIRSYNC;
116}
117
118/*
119 * Inherit flags from the parent inode.
120 *
Josef Bacike27425d2011-09-27 11:01:30 -0400121 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200122 */
123void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
124{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400125 unsigned int flags;
126
127 if (!dir)
128 return;
129
130 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200131
Josef Bacike27425d2011-09-27 11:01:30 -0400132 if (flags & BTRFS_INODE_NOCOMPRESS) {
133 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
134 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
135 } else if (flags & BTRFS_INODE_COMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
138 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200139
Josef Bacike27425d2011-09-27 11:01:30 -0400140 if (flags & BTRFS_INODE_NODATACOW)
141 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
142
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143 btrfs_update_iflags(inode);
144}
145
146static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
147{
148 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
149 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
150
151 if (copy_to_user(arg, &flags, sizeof(flags)))
152 return -EFAULT;
153 return 0;
154}
155
Liu Bo75e7cb72011-03-22 10:12:20 +0000156static int check_flags(unsigned int flags)
157{
158 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
159 FS_NOATIME_FL | FS_NODUMP_FL | \
160 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000161 FS_NOCOMP_FL | FS_COMPR_FL |
162 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000163 return -EOPNOTSUPP;
164
165 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
166 return -EINVAL;
167
Liu Bo75e7cb72011-03-22 10:12:20 +0000168 return 0;
169}
170
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200171static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
172{
173 struct inode *inode = file->f_path.dentry->d_inode;
174 struct btrfs_inode *ip = BTRFS_I(inode);
175 struct btrfs_root *root = ip->root;
176 struct btrfs_trans_handle *trans;
177 unsigned int flags, oldflags;
178 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800179 u64 ip_oldflags;
180 unsigned int i_oldflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181
Li Zefanb83cc962010-12-20 16:04:08 +0800182 if (btrfs_root_readonly(root))
183 return -EROFS;
184
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 if (copy_from_user(&flags, arg, sizeof(flags)))
186 return -EFAULT;
187
Liu Bo75e7cb72011-03-22 10:12:20 +0000188 ret = check_flags(flags);
189 if (ret)
190 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700192 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193 return -EACCES;
194
195 mutex_lock(&inode->i_mutex);
196
Li Zefanf062abf2011-12-29 13:36:45 +0800197 ip_oldflags = ip->flags;
198 i_oldflags = inode->i_flags;
199
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200 flags = btrfs_mask_flags(inode->i_mode, flags);
201 oldflags = btrfs_flags_to_ioctl(ip->flags);
202 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
203 if (!capable(CAP_LINUX_IMMUTABLE)) {
204 ret = -EPERM;
205 goto out_unlock;
206 }
207 }
208
Al Viroa561be72011-11-23 11:57:51 -0500209 ret = mnt_want_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200210 if (ret)
211 goto out_unlock;
212
213 if (flags & FS_SYNC_FL)
214 ip->flags |= BTRFS_INODE_SYNC;
215 else
216 ip->flags &= ~BTRFS_INODE_SYNC;
217 if (flags & FS_IMMUTABLE_FL)
218 ip->flags |= BTRFS_INODE_IMMUTABLE;
219 else
220 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
221 if (flags & FS_APPEND_FL)
222 ip->flags |= BTRFS_INODE_APPEND;
223 else
224 ip->flags &= ~BTRFS_INODE_APPEND;
225 if (flags & FS_NODUMP_FL)
226 ip->flags |= BTRFS_INODE_NODUMP;
227 else
228 ip->flags &= ~BTRFS_INODE_NODUMP;
229 if (flags & FS_NOATIME_FL)
230 ip->flags |= BTRFS_INODE_NOATIME;
231 else
232 ip->flags &= ~BTRFS_INODE_NOATIME;
233 if (flags & FS_DIRSYNC_FL)
234 ip->flags |= BTRFS_INODE_DIRSYNC;
235 else
236 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000237 if (flags & FS_NOCOW_FL)
238 ip->flags |= BTRFS_INODE_NODATACOW;
239 else
240 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200241
Liu Bo75e7cb72011-03-22 10:12:20 +0000242 /*
243 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
244 * flag may be changed automatically if compression code won't make
245 * things smaller.
246 */
247 if (flags & FS_NOCOMP_FL) {
248 ip->flags &= ~BTRFS_INODE_COMPRESS;
249 ip->flags |= BTRFS_INODE_NOCOMPRESS;
250 } else if (flags & FS_COMPR_FL) {
251 ip->flags |= BTRFS_INODE_COMPRESS;
252 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000253 } else {
254 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000255 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200256
Li Zefan4da6f1a2011-12-29 13:39:50 +0800257 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800258 if (IS_ERR(trans)) {
259 ret = PTR_ERR(trans);
260 goto out_drop;
261 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200262
Li Zefan306424c2011-12-14 20:12:02 -0500263 btrfs_update_iflags(inode);
264 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200265 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200266
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200267 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800268 out_drop:
269 if (ret) {
270 ip->flags = ip_oldflags;
271 inode->i_flags = i_oldflags;
272 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200273
Al Viro2a79f172011-12-09 08:06:57 -0500274 mnt_drop_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200275 out_unlock:
276 mutex_unlock(&inode->i_mutex);
liubo2d4e6f6a2011-02-24 09:38:16 +0000277 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200278}
279
280static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
281{
282 struct inode *inode = file->f_path.dentry->d_inode;
283
284 return put_user(inode->i_generation, arg);
285}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400286
Li Dongyangf7039b12011-03-24 10:24:28 +0000287static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
288{
Al Viro815745c2011-11-17 15:40:49 -0500289 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000290 struct btrfs_device *device;
291 struct request_queue *q;
292 struct fstrim_range range;
293 u64 minlen = ULLONG_MAX;
294 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500295 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000296 int ret;
297
298 if (!capable(CAP_SYS_ADMIN))
299 return -EPERM;
300
Xiao Guangrong1f781602011-04-20 10:09:16 +0000301 rcu_read_lock();
302 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
303 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000304 if (!device->bdev)
305 continue;
306 q = bdev_get_queue(device->bdev);
307 if (blk_queue_discard(q)) {
308 num_devices++;
309 minlen = min((u64)q->limits.discard_granularity,
310 minlen);
311 }
312 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000313 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200314
Li Dongyangf7039b12011-03-24 10:24:28 +0000315 if (!num_devices)
316 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000317 if (copy_from_user(&range, arg, sizeof(range)))
318 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200319 if (range.start > total_bytes)
320 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000321
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200322 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000323 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500324 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000325 if (ret < 0)
326 return ret;
327
328 if (copy_to_user(arg, &range, sizeof(range)))
329 return -EFAULT;
330
331 return 0;
332}
333
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400334static noinline int create_subvol(struct btrfs_root *root,
335 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400336 char *name, int namelen,
337 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400338{
339 struct btrfs_trans_handle *trans;
340 struct btrfs_key key;
341 struct btrfs_root_item root_item;
342 struct btrfs_inode_item *inode_item;
343 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400344 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400345 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000346 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400347 int ret;
348 int err;
349 u64 objectid;
350 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500351 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400352
Li Zefan581bb052011-04-20 10:06:11 +0800353 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400354 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400355 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000356
357 dir = parent->d_inode;
358
Josef Bacik9ed74f22009-09-11 16:12:44 -0400359 /*
360 * 1 - inode item
361 * 2 - refs
362 * 1 - root item
363 * 2 - dir items
364 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400365 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400366 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400367 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400368
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400369 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200370 0, objectid, NULL, 0, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400371 if (IS_ERR(leaf)) {
372 ret = PTR_ERR(leaf);
373 goto fail;
374 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400375
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400376 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400377 btrfs_set_header_bytenr(leaf, leaf->start);
378 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400379 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380 btrfs_set_header_owner(leaf, objectid);
381
382 write_extent_buffer(leaf, root->fs_info->fsid,
383 (unsigned long)btrfs_header_fsid(leaf),
384 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400385 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
386 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
387 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400388 btrfs_mark_buffer_dirty(leaf);
389
390 inode_item = &root_item.inode;
391 memset(inode_item, 0, sizeof(*inode_item));
392 inode_item->generation = cpu_to_le64(1);
393 inode_item->size = cpu_to_le64(3);
394 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400395 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400396 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
397
Li Zefan08fe4db2011-03-28 02:01:25 +0000398 root_item.flags = 0;
399 root_item.byte_limit = 0;
400 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
401
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400402 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400403 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404 btrfs_set_root_level(&root_item, 0);
405 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000406 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400407 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400408
409 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
410 root_item.drop_level = 0;
411
Chris Mason925baed2008-06-25 16:01:30 -0400412 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400413 free_extent_buffer(leaf);
414 leaf = NULL;
415
416 btrfs_set_root_dirid(&root_item, new_dirid);
417
418 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400419 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400420 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
421 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
422 &root_item);
423 if (ret)
424 goto fail;
425
Yan, Zheng76dda932009-09-21 16:00:26 -0400426 key.offset = (u64)-1;
427 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
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
Liu Bo17ce6ef2012-03-29 09:57:45 -0400787/*
788 * Validaty check of prev em and next em:
789 * 1) no prev/next em
790 * 2) prev/next em is an hole/inline extent
791 */
792static int check_adjacent_extents(struct inode *inode, struct extent_map *em)
793{
794 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
795 struct extent_map *prev = NULL, *next = NULL;
796 int ret = 0;
797
798 read_lock(&em_tree->lock);
799 prev = lookup_extent_mapping(em_tree, em->start - 1, (u64)-1);
800 next = lookup_extent_mapping(em_tree, em->start + em->len, (u64)-1);
801 read_unlock(&em_tree->lock);
802
803 if ((!prev || prev->block_start >= EXTENT_MAP_LAST_BYTE) &&
804 (!next || next->block_start >= EXTENT_MAP_LAST_BYTE))
805 ret = 1;
806 free_extent_map(prev);
807 free_extent_map(next);
808
809 return ret;
810}
811
Chris Mason940100a2010-03-10 10:52:59 -0500812static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500813 int thresh, u64 *last_len, u64 *skip,
814 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500815{
816 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
817 struct extent_map *em = NULL;
818 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
819 int ret = 1;
820
821 /*
Li Zefan008873e2011-09-02 15:57:07 +0800822 * make sure that once we start defragging an extent, we keep on
Chris Mason940100a2010-03-10 10:52:59 -0500823 * defragging it
824 */
825 if (start < *defrag_end)
826 return 1;
827
828 *skip = 0;
829
830 /*
831 * hopefully we have this extent in the tree already, try without
832 * the full extent lock
833 */
834 read_lock(&em_tree->lock);
835 em = lookup_extent_mapping(em_tree, start, len);
836 read_unlock(&em_tree->lock);
837
838 if (!em) {
839 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100840 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500841 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100842 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500843
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000844 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500845 return 0;
846 }
847
848 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400849 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500850 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400851 goto out;
852 }
853
854 /* If we have nothing to merge with us, just skip. */
855 if (check_adjacent_extents(inode, em)) {
856 ret = 0;
857 goto out;
858 }
Chris Mason940100a2010-03-10 10:52:59 -0500859
860 /*
861 * we hit a real extent, if it is big don't bother defragging it again
862 */
Chris Mason1e701a32010-03-11 09:42:04 -0500863 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500864 ret = 0;
865
Liu Bo17ce6ef2012-03-29 09:57:45 -0400866out:
Chris Mason940100a2010-03-10 10:52:59 -0500867 /*
868 * last_len ends up being a counter of how many bytes we've defragged.
869 * every time we choose not to defrag an extent, we reset *last_len
870 * so that the next tiny extent will force a defrag.
871 *
872 * The end result of this is that tiny extents before a single big
873 * extent will force at least part of that big extent to be defragged.
874 */
875 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500876 *defrag_end = extent_map_end(em);
877 } else {
878 *last_len = 0;
879 *skip = extent_map_end(em);
880 *defrag_end = 0;
881 }
882
883 free_extent_map(em);
884 return ret;
885}
886
Chris Mason4cb53002011-05-24 15:35:30 -0400887/*
888 * it doesn't do much good to defrag one or two pages
889 * at a time. This pulls in a nice chunk of pages
890 * to COW and defrag.
891 *
892 * It also makes sure the delalloc code has enough
893 * dirty data to avoid making new small extents as part
894 * of the defrag
895 *
896 * It's a good idea to start RA on this range
897 * before calling this.
898 */
899static int cluster_pages_for_defrag(struct inode *inode,
900 struct page **pages,
901 unsigned long start_index,
902 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400903{
Chris Mason4cb53002011-05-24 15:35:30 -0400904 unsigned long file_end;
905 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400906 u64 page_start;
907 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400908 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400909 int ret;
910 int i;
911 int i_done;
912 struct btrfs_ordered_extent *ordered;
913 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800914 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400915 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400916
Chris Mason4cb53002011-05-24 15:35:30 -0400917 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400918 if (!isize || start_index > file_end)
919 return 0;
920
921 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400922
923 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400924 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400925 if (ret)
926 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400927 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800928 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400929
930 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400931 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400932 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800933again:
Josef Bacika94733d2011-07-11 10:47:06 -0400934 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800935 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400936 if (!page)
937 break;
938
Miao Xie600a45e2012-02-16 15:01:24 +0800939 page_start = page_offset(page);
940 page_end = page_start + PAGE_CACHE_SIZE - 1;
941 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100942 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800943 ordered = btrfs_lookup_ordered_extent(inode,
944 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100945 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800946 if (!ordered)
947 break;
948
949 unlock_page(page);
950 btrfs_start_ordered_extent(inode, ordered, 1);
951 btrfs_put_ordered_extent(ordered);
952 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -0400953 /*
954 * we unlocked the page above, so we need check if
955 * it was released or not.
956 */
957 if (page->mapping != inode->i_mapping) {
958 unlock_page(page);
959 page_cache_release(page);
960 goto again;
961 }
Miao Xie600a45e2012-02-16 15:01:24 +0800962 }
963
Chris Mason4cb53002011-05-24 15:35:30 -0400964 if (!PageUptodate(page)) {
965 btrfs_readpage(NULL, page);
966 lock_page(page);
967 if (!PageUptodate(page)) {
968 unlock_page(page);
969 page_cache_release(page);
970 ret = -EIO;
971 break;
972 }
973 }
Miao Xie600a45e2012-02-16 15:01:24 +0800974
Miao Xie600a45e2012-02-16 15:01:24 +0800975 if (page->mapping != inode->i_mapping) {
976 unlock_page(page);
977 page_cache_release(page);
978 goto again;
979 }
980
Chris Mason4cb53002011-05-24 15:35:30 -0400981 pages[i] = page;
982 i_done++;
983 }
984 if (!i_done || ret)
985 goto out;
986
987 if (!(inode->i_sb->s_flags & MS_ACTIVE))
988 goto out;
989
990 /*
991 * so now we have a nice long stream of locked
992 * and up to date pages, lets wait on them
993 */
994 for (i = 0; i < i_done; i++)
995 wait_on_page_writeback(pages[i]);
996
997 page_start = page_offset(pages[0]);
998 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
999
1000 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001001 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001002 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1003 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1004 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
1005 GFP_NOFS);
1006
Liu Bo1f12bd02012-03-29 09:57:44 -04001007 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001008 spin_lock(&BTRFS_I(inode)->lock);
1009 BTRFS_I(inode)->outstanding_extents++;
1010 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001011 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001012 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001013 }
1014
1015
1016 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
1017 &cached_state);
1018
1019 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1020 page_start, page_end - 1, &cached_state,
1021 GFP_NOFS);
1022
1023 for (i = 0; i < i_done; i++) {
1024 clear_page_dirty_for_io(pages[i]);
1025 ClearPageChecked(pages[i]);
1026 set_page_extent_mapped(pages[i]);
1027 set_page_dirty(pages[i]);
1028 unlock_page(pages[i]);
1029 page_cache_release(pages[i]);
1030 }
1031 return i_done;
1032out:
1033 for (i = 0; i < i_done; i++) {
1034 unlock_page(pages[i]);
1035 page_cache_release(pages[i]);
1036 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001037 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001038 return ret;
1039
1040}
1041
1042int btrfs_defrag_file(struct inode *inode, struct file *file,
1043 struct btrfs_ioctl_defrag_range_args *range,
1044 u64 newer_than, unsigned long max_to_defrag)
1045{
1046 struct btrfs_root *root = BTRFS_I(inode)->root;
1047 struct btrfs_super_block *disk_super;
1048 struct file_ra_state *ra = NULL;
1049 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001050 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001051 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -05001052 u64 last_len = 0;
1053 u64 skip = 0;
1054 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001055 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001056 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001057 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001058 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001059 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001060 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001061 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001062 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1063 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001064 u64 new_align = ~((u64)128 * 1024 - 1);
1065 struct page **pages = NULL;
1066
1067 if (extent_thresh == 0)
1068 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001069
1070 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1071 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1072 return -EINVAL;
1073 if (range->compress_type)
1074 compress_type = range->compress_type;
1075 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001076
Li Zefan151a31b2011-09-02 15:56:39 +08001077 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001078 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001079
Chris Mason4cb53002011-05-24 15:35:30 -04001080 /*
1081 * if we were not given a file, allocate a readahead
1082 * context
1083 */
1084 if (!file) {
1085 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1086 if (!ra)
1087 return -ENOMEM;
1088 file_ra_state_init(ra, inode->i_mapping);
1089 } else {
1090 ra = &file->f_ra;
1091 }
1092
Li Zefan008873e2011-09-02 15:57:07 +08001093 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001094 GFP_NOFS);
1095 if (!pages) {
1096 ret = -ENOMEM;
1097 goto out_ra;
1098 }
1099
1100 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001101 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001102 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001103 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1104 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001105 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001106 }
1107
Chris Mason4cb53002011-05-24 15:35:30 -04001108 if (newer_than) {
1109 ret = find_new_extents(root, inode, newer_than,
1110 &newer_off, 64 * 1024);
1111 if (!ret) {
1112 range->start = newer_off;
1113 /*
1114 * we always align our defrag to help keep
1115 * the extents in the file evenly spaced
1116 */
1117 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001118 } else
1119 goto out_ra;
1120 } else {
1121 i = range->start >> PAGE_CACHE_SHIFT;
1122 }
1123 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001124 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001125
Li Zefan2a0f7f52011-10-10 15:43:34 -04001126 /*
1127 * make writeback starts from i, so the defrag range can be
1128 * written sequentially.
1129 */
1130 if (i < inode->i_mapping->writeback_index)
1131 inode->i_mapping->writeback_index = i;
1132
Chris Masonf7f43cc2011-10-11 11:41:40 -04001133 while (i <= last_index && defrag_count < max_to_defrag &&
1134 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1135 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001136 /*
1137 * make sure we stop running if someone unmounts
1138 * the FS
1139 */
1140 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1141 break;
1142
Liu Bo66c26892012-03-29 09:57:45 -04001143 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1144 PAGE_CACHE_SIZE, extent_thresh,
1145 &last_len, &skip, &defrag_end)) {
Chris Mason940100a2010-03-10 10:52:59 -05001146 unsigned long next;
1147 /*
1148 * the should_defrag function tells us how much to skip
1149 * bump our counter by the suggested amount
1150 */
1151 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1152 i = max(i + 1, next);
1153 continue;
1154 }
Li Zefan008873e2011-09-02 15:57:07 +08001155
1156 if (!newer_than) {
1157 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1158 PAGE_CACHE_SHIFT) - i;
1159 cluster = min(cluster, max_cluster);
1160 } else {
1161 cluster = max_cluster;
1162 }
1163
Chris Mason1e701a32010-03-11 09:42:04 -05001164 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001165 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001166
Li Zefan008873e2011-09-02 15:57:07 +08001167 if (i + cluster > ra_index) {
1168 ra_index = max(i, ra_index);
1169 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1170 cluster);
1171 ra_index += max_cluster;
1172 }
Chris Mason940100a2010-03-10 10:52:59 -05001173
Liu Boecb8bea2012-03-29 09:57:44 -04001174 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001175 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001176 if (ret < 0) {
1177 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001178 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001179 }
Chris Mason940100a2010-03-10 10:52:59 -05001180
Chris Mason4cb53002011-05-24 15:35:30 -04001181 defrag_count += ret;
1182 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001183 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001184
1185 if (newer_than) {
1186 if (newer_off == (u64)-1)
1187 break;
1188
1189 newer_off = max(newer_off + 1,
1190 (u64)i << PAGE_CACHE_SHIFT);
1191
1192 ret = find_new_extents(root, inode,
1193 newer_than, &newer_off,
1194 64 * 1024);
1195 if (!ret) {
1196 range->start = newer_off;
1197 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001198 } else {
1199 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001200 }
Chris Mason4cb53002011-05-24 15:35:30 -04001201 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001202 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001203 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001204 last_len += ret << PAGE_CACHE_SHIFT;
1205 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001206 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001207 last_len = 0;
1208 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001209 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001210 }
1211
Chris Mason1e701a32010-03-11 09:42:04 -05001212 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1213 filemap_flush(inode->i_mapping);
1214
1215 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1216 /* the filemap_flush will queue IO into the worker threads, but
1217 * we have to make sure the IO is actually started and that
1218 * ordered extents get created before we return
1219 */
1220 atomic_inc(&root->fs_info->async_submit_draining);
1221 while (atomic_read(&root->fs_info->nr_async_submits) ||
1222 atomic_read(&root->fs_info->async_delalloc_pages)) {
1223 wait_event(root->fs_info->async_submit_wait,
1224 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1225 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1226 }
1227 atomic_dec(&root->fs_info->async_submit_draining);
1228
1229 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001230 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001231 mutex_unlock(&inode->i_mutex);
1232 }
1233
David Sterba6c417612011-04-13 15:41:04 +02001234 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001235 features = btrfs_super_incompat_flags(disk_super);
1236 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1237 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1238 btrfs_set_super_incompat_flags(disk_super, features);
1239 }
1240
Diego Calleja60ccf822011-09-01 16:33:57 +02001241 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001242
Chris Mason4cb53002011-05-24 15:35:30 -04001243out_ra:
1244 if (!file)
1245 kfree(ra);
1246 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001247 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001248}
1249
Yan, Zheng76dda932009-09-21 16:00:26 -04001250static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1251 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001252{
1253 u64 new_size;
1254 u64 old_size;
1255 u64 devid = 1;
1256 struct btrfs_ioctl_vol_args *vol_args;
1257 struct btrfs_trans_handle *trans;
1258 struct btrfs_device *device = NULL;
1259 char *sizestr;
1260 char *devstr = NULL;
1261 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001262 int mod = 0;
1263
Yan Zhengc146afa2008-11-12 14:34:12 -05001264 if (root->fs_info->sb->s_flags & MS_RDONLY)
1265 return -EROFS;
1266
Chris Masone441d542009-01-05 16:57:23 -05001267 if (!capable(CAP_SYS_ADMIN))
1268 return -EPERM;
1269
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001270 mutex_lock(&root->fs_info->volume_mutex);
1271 if (root->fs_info->balance_ctl) {
1272 printk(KERN_INFO "btrfs: balance in progress\n");
1273 ret = -EINVAL;
1274 goto out;
1275 }
1276
Li Zefandae7b662009-04-08 15:06:54 +08001277 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001278 if (IS_ERR(vol_args)) {
1279 ret = PTR_ERR(vol_args);
1280 goto out;
1281 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001282
1283 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001284
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001285 sizestr = vol_args->name;
1286 devstr = strchr(sizestr, ':');
1287 if (devstr) {
1288 char *end;
1289 sizestr = devstr + 1;
1290 *devstr = '\0';
1291 devstr = vol_args->name;
1292 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001293 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001294 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001295 }
Yan Zheng2b820322008-11-17 21:11:30 -05001296 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001297 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001298 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001299 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001300 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001301 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001302 }
1303 if (!strcmp(sizestr, "max"))
1304 new_size = device->bdev->bd_inode->i_size;
1305 else {
1306 if (sizestr[0] == '-') {
1307 mod = -1;
1308 sizestr++;
1309 } else if (sizestr[0] == '+') {
1310 mod = 1;
1311 sizestr++;
1312 }
Akinobu Mita91748462010-02-28 10:59:11 +00001313 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001314 if (new_size == 0) {
1315 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001316 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001317 }
1318 }
1319
1320 old_size = device->total_bytes;
1321
1322 if (mod < 0) {
1323 if (new_size > old_size) {
1324 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001325 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001326 }
1327 new_size = old_size - new_size;
1328 } else if (mod > 0) {
1329 new_size = old_size + new_size;
1330 }
1331
1332 if (new_size < 256 * 1024 * 1024) {
1333 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001334 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001335 }
1336 if (new_size > device->bdev->bd_inode->i_size) {
1337 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001338 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001339 }
1340
1341 do_div(new_size, root->sectorsize);
1342 new_size *= root->sectorsize;
1343
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001344 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001345 device->name, (unsigned long long)new_size);
1346
1347 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001348 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001349 if (IS_ERR(trans)) {
1350 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001351 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001352 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001353 ret = btrfs_grow_device(trans, device, new_size);
1354 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001355 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001356 ret = btrfs_shrink_device(device, new_size);
1357 }
1358
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001359out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001360 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001361out:
1362 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001363 return ret;
1364}
1365
Sage Weil72fd0322010-10-29 15:41:32 -04001366static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1367 char *name,
1368 unsigned long fd,
1369 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001370 u64 *transid,
1371 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001372{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001373 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001374 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001375 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001376 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001377
Yan Zhengc146afa2008-11-12 14:34:12 -05001378 if (root->fs_info->sb->s_flags & MS_RDONLY)
1379 return -EROFS;
1380
Sage Weil72fd0322010-10-29 15:41:32 -04001381 namelen = strlen(name);
1382 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001383 ret = -EINVAL;
1384 goto out;
1385 }
1386
Chris Mason16780ca2012-02-20 22:14:55 -05001387 if (name[0] == '.' &&
1388 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1389 ret = -EEXIST;
1390 goto out;
1391 }
1392
Chris Mason3de45862008-11-17 21:02:50 -05001393 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001394 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001395 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001396 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001397 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001398 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001399 if (!src_file) {
1400 ret = -EINVAL;
1401 goto out;
1402 }
1403
1404 src_inode = src_file->f_path.dentry->d_inode;
1405 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001406 printk(KERN_INFO "btrfs: Snapshot src from "
1407 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001408 ret = -EINVAL;
1409 fput(src_file);
1410 goto out;
1411 }
Sage Weil72fd0322010-10-29 15:41:32 -04001412 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1413 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001414 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001415 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001416 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001417out:
Sage Weil72fd0322010-10-29 15:41:32 -04001418 return ret;
1419}
1420
1421static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001422 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001423{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001424 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001425 int ret;
1426
Li Zefanfa0d2b92010-12-20 15:53:28 +08001427 vol_args = memdup_user(arg, sizeof(*vol_args));
1428 if (IS_ERR(vol_args))
1429 return PTR_ERR(vol_args);
1430 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001431
Li Zefanfa0d2b92010-12-20 15:53:28 +08001432 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001433 vol_args->fd, subvol,
1434 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001435
Li Zefanfa0d2b92010-12-20 15:53:28 +08001436 kfree(vol_args);
1437 return ret;
1438}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001439
Li Zefanfa0d2b92010-12-20 15:53:28 +08001440static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1441 void __user *arg, int subvol)
1442{
1443 struct btrfs_ioctl_vol_args_v2 *vol_args;
1444 int ret;
1445 u64 transid = 0;
1446 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001447 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001448
Li Zefanfa0d2b92010-12-20 15:53:28 +08001449 vol_args = memdup_user(arg, sizeof(*vol_args));
1450 if (IS_ERR(vol_args))
1451 return PTR_ERR(vol_args);
1452 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001453
Li Zefanb83cc962010-12-20 16:04:08 +08001454 if (vol_args->flags &
1455 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1456 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001457 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001458 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001459
1460 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1461 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001462 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1463 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001464
1465 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001466 vol_args->fd, subvol,
1467 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001468
1469 if (ret == 0 && ptr &&
1470 copy_to_user(arg +
1471 offsetof(struct btrfs_ioctl_vol_args_v2,
1472 transid), ptr, sizeof(*ptr)))
1473 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001474out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001475 kfree(vol_args);
1476 return ret;
1477}
1478
Li Zefan0caa1022010-12-20 16:30:25 +08001479static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1480 void __user *arg)
1481{
1482 struct inode *inode = fdentry(file)->d_inode;
1483 struct btrfs_root *root = BTRFS_I(inode)->root;
1484 int ret = 0;
1485 u64 flags = 0;
1486
Li Zefan33345d012011-04-20 10:31:50 +08001487 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001488 return -EINVAL;
1489
1490 down_read(&root->fs_info->subvol_sem);
1491 if (btrfs_root_readonly(root))
1492 flags |= BTRFS_SUBVOL_RDONLY;
1493 up_read(&root->fs_info->subvol_sem);
1494
1495 if (copy_to_user(arg, &flags, sizeof(flags)))
1496 ret = -EFAULT;
1497
1498 return ret;
1499}
1500
1501static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1502 void __user *arg)
1503{
1504 struct inode *inode = fdentry(file)->d_inode;
1505 struct btrfs_root *root = BTRFS_I(inode)->root;
1506 struct btrfs_trans_handle *trans;
1507 u64 root_flags;
1508 u64 flags;
1509 int ret = 0;
1510
1511 if (root->fs_info->sb->s_flags & MS_RDONLY)
1512 return -EROFS;
1513
Li Zefan33345d012011-04-20 10:31:50 +08001514 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001515 return -EINVAL;
1516
1517 if (copy_from_user(&flags, arg, sizeof(flags)))
1518 return -EFAULT;
1519
Li Zefanb4dc2b82011-02-16 06:06:34 +00001520 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001521 return -EINVAL;
1522
1523 if (flags & ~BTRFS_SUBVOL_RDONLY)
1524 return -EOPNOTSUPP;
1525
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001526 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001527 return -EACCES;
1528
Li Zefan0caa1022010-12-20 16:30:25 +08001529 down_write(&root->fs_info->subvol_sem);
1530
1531 /* nothing to do */
1532 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1533 goto out;
1534
1535 root_flags = btrfs_root_flags(&root->root_item);
1536 if (flags & BTRFS_SUBVOL_RDONLY)
1537 btrfs_set_root_flags(&root->root_item,
1538 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1539 else
1540 btrfs_set_root_flags(&root->root_item,
1541 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1542
1543 trans = btrfs_start_transaction(root, 1);
1544 if (IS_ERR(trans)) {
1545 ret = PTR_ERR(trans);
1546 goto out_reset;
1547 }
1548
Li Zefanb4dc2b82011-02-16 06:06:34 +00001549 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001550 &root->root_key, &root->root_item);
1551
1552 btrfs_commit_transaction(trans, root);
1553out_reset:
1554 if (ret)
1555 btrfs_set_root_flags(&root->root_item, root_flags);
1556out:
1557 up_write(&root->fs_info->subvol_sem);
1558 return ret;
1559}
1560
Yan, Zheng76dda932009-09-21 16:00:26 -04001561/*
1562 * helper to check if the subvolume references other subvolumes
1563 */
1564static noinline int may_destroy_subvol(struct btrfs_root *root)
1565{
1566 struct btrfs_path *path;
1567 struct btrfs_key key;
1568 int ret;
1569
1570 path = btrfs_alloc_path();
1571 if (!path)
1572 return -ENOMEM;
1573
1574 key.objectid = root->root_key.objectid;
1575 key.type = BTRFS_ROOT_REF_KEY;
1576 key.offset = (u64)-1;
1577
1578 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1579 &key, path, 0, 0);
1580 if (ret < 0)
1581 goto out;
1582 BUG_ON(ret == 0);
1583
1584 ret = 0;
1585 if (path->slots[0] > 0) {
1586 path->slots[0]--;
1587 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1588 if (key.objectid == root->root_key.objectid &&
1589 key.type == BTRFS_ROOT_REF_KEY)
1590 ret = -ENOTEMPTY;
1591 }
1592out:
1593 btrfs_free_path(path);
1594 return ret;
1595}
1596
Chris Masonac8e9812010-02-28 15:39:26 -05001597static noinline int key_in_sk(struct btrfs_key *key,
1598 struct btrfs_ioctl_search_key *sk)
1599{
Chris Masonabc6e132010-03-18 12:10:08 -04001600 struct btrfs_key test;
1601 int ret;
1602
1603 test.objectid = sk->min_objectid;
1604 test.type = sk->min_type;
1605 test.offset = sk->min_offset;
1606
1607 ret = btrfs_comp_cpu_keys(key, &test);
1608 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001609 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001610
1611 test.objectid = sk->max_objectid;
1612 test.type = sk->max_type;
1613 test.offset = sk->max_offset;
1614
1615 ret = btrfs_comp_cpu_keys(key, &test);
1616 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001617 return 0;
1618 return 1;
1619}
1620
1621static noinline int copy_to_sk(struct btrfs_root *root,
1622 struct btrfs_path *path,
1623 struct btrfs_key *key,
1624 struct btrfs_ioctl_search_key *sk,
1625 char *buf,
1626 unsigned long *sk_offset,
1627 int *num_found)
1628{
1629 u64 found_transid;
1630 struct extent_buffer *leaf;
1631 struct btrfs_ioctl_search_header sh;
1632 unsigned long item_off;
1633 unsigned long item_len;
1634 int nritems;
1635 int i;
1636 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001637 int ret = 0;
1638
1639 leaf = path->nodes[0];
1640 slot = path->slots[0];
1641 nritems = btrfs_header_nritems(leaf);
1642
1643 if (btrfs_header_generation(leaf) > sk->max_transid) {
1644 i = nritems;
1645 goto advance_key;
1646 }
1647 found_transid = btrfs_header_generation(leaf);
1648
1649 for (i = slot; i < nritems; i++) {
1650 item_off = btrfs_item_ptr_offset(leaf, i);
1651 item_len = btrfs_item_size_nr(leaf, i);
1652
1653 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1654 item_len = 0;
1655
1656 if (sizeof(sh) + item_len + *sk_offset >
1657 BTRFS_SEARCH_ARGS_BUFSIZE) {
1658 ret = 1;
1659 goto overflow;
1660 }
1661
1662 btrfs_item_key_to_cpu(leaf, key, i);
1663 if (!key_in_sk(key, sk))
1664 continue;
1665
1666 sh.objectid = key->objectid;
1667 sh.offset = key->offset;
1668 sh.type = key->type;
1669 sh.len = item_len;
1670 sh.transid = found_transid;
1671
1672 /* copy search result header */
1673 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1674 *sk_offset += sizeof(sh);
1675
1676 if (item_len) {
1677 char *p = buf + *sk_offset;
1678 /* copy the item */
1679 read_extent_buffer(leaf, p,
1680 item_off, item_len);
1681 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001682 }
Hugo Millse2156862011-05-14 17:43:41 +00001683 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001684
1685 if (*num_found >= sk->nr_items)
1686 break;
1687 }
1688advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001689 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001690 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1691 key->offset++;
1692 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1693 key->offset = 0;
1694 key->type++;
1695 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1696 key->offset = 0;
1697 key->type = 0;
1698 key->objectid++;
1699 } else
1700 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001701overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001702 return ret;
1703}
1704
1705static noinline int search_ioctl(struct inode *inode,
1706 struct btrfs_ioctl_search_args *args)
1707{
1708 struct btrfs_root *root;
1709 struct btrfs_key key;
1710 struct btrfs_key max_key;
1711 struct btrfs_path *path;
1712 struct btrfs_ioctl_search_key *sk = &args->key;
1713 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1714 int ret;
1715 int num_found = 0;
1716 unsigned long sk_offset = 0;
1717
1718 path = btrfs_alloc_path();
1719 if (!path)
1720 return -ENOMEM;
1721
1722 if (sk->tree_id == 0) {
1723 /* search the root of the inode that was passed */
1724 root = BTRFS_I(inode)->root;
1725 } else {
1726 key.objectid = sk->tree_id;
1727 key.type = BTRFS_ROOT_ITEM_KEY;
1728 key.offset = (u64)-1;
1729 root = btrfs_read_fs_root_no_name(info, &key);
1730 if (IS_ERR(root)) {
1731 printk(KERN_ERR "could not find root %llu\n",
1732 sk->tree_id);
1733 btrfs_free_path(path);
1734 return -ENOENT;
1735 }
1736 }
1737
1738 key.objectid = sk->min_objectid;
1739 key.type = sk->min_type;
1740 key.offset = sk->min_offset;
1741
1742 max_key.objectid = sk->max_objectid;
1743 max_key.type = sk->max_type;
1744 max_key.offset = sk->max_offset;
1745
1746 path->keep_locks = 1;
1747
1748 while(1) {
1749 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1750 sk->min_transid);
1751 if (ret != 0) {
1752 if (ret > 0)
1753 ret = 0;
1754 goto err;
1755 }
1756 ret = copy_to_sk(root, path, &key, sk, args->buf,
1757 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001758 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001759 if (ret || num_found >= sk->nr_items)
1760 break;
1761
1762 }
1763 ret = 0;
1764err:
1765 sk->nr_items = num_found;
1766 btrfs_free_path(path);
1767 return ret;
1768}
1769
1770static noinline int btrfs_ioctl_tree_search(struct file *file,
1771 void __user *argp)
1772{
1773 struct btrfs_ioctl_search_args *args;
1774 struct inode *inode;
1775 int ret;
1776
1777 if (!capable(CAP_SYS_ADMIN))
1778 return -EPERM;
1779
Julia Lawall2354d082010-10-29 15:14:18 -04001780 args = memdup_user(argp, sizeof(*args));
1781 if (IS_ERR(args))
1782 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001783
Chris Masonac8e9812010-02-28 15:39:26 -05001784 inode = fdentry(file)->d_inode;
1785 ret = search_ioctl(inode, args);
1786 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1787 ret = -EFAULT;
1788 kfree(args);
1789 return ret;
1790}
1791
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001792/*
Chris Masonac8e9812010-02-28 15:39:26 -05001793 * Search INODE_REFs to identify path name of 'dirid' directory
1794 * in a 'tree_id' tree. and sets path name to 'name'.
1795 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001796static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1797 u64 tree_id, u64 dirid, char *name)
1798{
1799 struct btrfs_root *root;
1800 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001801 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001802 int ret = -1;
1803 int slot;
1804 int len;
1805 int total_len = 0;
1806 struct btrfs_inode_ref *iref;
1807 struct extent_buffer *l;
1808 struct btrfs_path *path;
1809
1810 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1811 name[0]='\0';
1812 return 0;
1813 }
1814
1815 path = btrfs_alloc_path();
1816 if (!path)
1817 return -ENOMEM;
1818
Chris Masonac8e9812010-02-28 15:39:26 -05001819 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001820
1821 key.objectid = tree_id;
1822 key.type = BTRFS_ROOT_ITEM_KEY;
1823 key.offset = (u64)-1;
1824 root = btrfs_read_fs_root_no_name(info, &key);
1825 if (IS_ERR(root)) {
1826 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001827 ret = -ENOENT;
1828 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001829 }
1830
1831 key.objectid = dirid;
1832 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001833 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001834
1835 while(1) {
1836 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1837 if (ret < 0)
1838 goto out;
1839
1840 l = path->nodes[0];
1841 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001842 if (ret > 0 && slot > 0)
1843 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001844 btrfs_item_key_to_cpu(l, &key, slot);
1845
1846 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001847 key.type != BTRFS_INODE_REF_KEY)) {
1848 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001849 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001850 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001851
1852 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1853 len = btrfs_inode_ref_name_len(l, iref);
1854 ptr -= len + 1;
1855 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001856 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001857 goto out;
1858
1859 *(ptr + len) = '/';
1860 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1861
1862 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1863 break;
1864
David Sterbab3b4aa72011-04-21 01:20:15 +02001865 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001866 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001867 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001868 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001869 }
Chris Masonac8e9812010-02-28 15:39:26 -05001870 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001871 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001872 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001873 name[total_len]='\0';
1874 ret = 0;
1875out:
1876 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001877 return ret;
1878}
1879
1880static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1881 void __user *argp)
1882{
1883 struct btrfs_ioctl_ino_lookup_args *args;
1884 struct inode *inode;
1885 int ret;
1886
1887 if (!capable(CAP_SYS_ADMIN))
1888 return -EPERM;
1889
Julia Lawall2354d082010-10-29 15:14:18 -04001890 args = memdup_user(argp, sizeof(*args));
1891 if (IS_ERR(args))
1892 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001893
Chris Masonac8e9812010-02-28 15:39:26 -05001894 inode = fdentry(file)->d_inode;
1895
Chris Mason1b53ac42010-03-18 12:17:05 -04001896 if (args->treeid == 0)
1897 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1898
Chris Masonac8e9812010-02-28 15:39:26 -05001899 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1900 args->treeid, args->objectid,
1901 args->name);
1902
1903 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1904 ret = -EFAULT;
1905
1906 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001907 return ret;
1908}
1909
Yan, Zheng76dda932009-09-21 16:00:26 -04001910static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1911 void __user *arg)
1912{
1913 struct dentry *parent = fdentry(file);
1914 struct dentry *dentry;
1915 struct inode *dir = parent->d_inode;
1916 struct inode *inode;
1917 struct btrfs_root *root = BTRFS_I(dir)->root;
1918 struct btrfs_root *dest = NULL;
1919 struct btrfs_ioctl_vol_args *vol_args;
1920 struct btrfs_trans_handle *trans;
1921 int namelen;
1922 int ret;
1923 int err = 0;
1924
Yan, Zheng76dda932009-09-21 16:00:26 -04001925 vol_args = memdup_user(arg, sizeof(*vol_args));
1926 if (IS_ERR(vol_args))
1927 return PTR_ERR(vol_args);
1928
1929 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1930 namelen = strlen(vol_args->name);
1931 if (strchr(vol_args->name, '/') ||
1932 strncmp(vol_args->name, "..", namelen) == 0) {
1933 err = -EINVAL;
1934 goto out;
1935 }
1936
Al Viroa561be72011-11-23 11:57:51 -05001937 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001938 if (err)
1939 goto out;
1940
1941 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1942 dentry = lookup_one_len(vol_args->name, parent, namelen);
1943 if (IS_ERR(dentry)) {
1944 err = PTR_ERR(dentry);
1945 goto out_unlock_dir;
1946 }
1947
1948 if (!dentry->d_inode) {
1949 err = -ENOENT;
1950 goto out_dput;
1951 }
1952
1953 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001954 dest = BTRFS_I(inode)->root;
1955 if (!capable(CAP_SYS_ADMIN)){
1956 /*
1957 * Regular user. Only allow this with a special mount
1958 * option, when the user has write+exec access to the
1959 * subvol root, and when rmdir(2) would have been
1960 * allowed.
1961 *
1962 * Note that this is _not_ check that the subvol is
1963 * empty or doesn't contain data that we wouldn't
1964 * otherwise be able to delete.
1965 *
1966 * Users who want to delete empty subvols should try
1967 * rmdir(2).
1968 */
1969 err = -EPERM;
1970 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1971 goto out_dput;
1972
1973 /*
1974 * Do not allow deletion if the parent dir is the same
1975 * as the dir to be deleted. That means the ioctl
1976 * must be called on the dentry referencing the root
1977 * of the subvol, not a random directory contained
1978 * within it.
1979 */
1980 err = -EINVAL;
1981 if (root == dest)
1982 goto out_dput;
1983
1984 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1985 if (err)
1986 goto out_dput;
1987
1988 /* check if subvolume may be deleted by a non-root user */
1989 err = btrfs_may_delete(dir, dentry, 1);
1990 if (err)
1991 goto out_dput;
1992 }
1993
Li Zefan33345d012011-04-20 10:31:50 +08001994 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04001995 err = -EINVAL;
1996 goto out_dput;
1997 }
1998
Yan, Zheng76dda932009-09-21 16:00:26 -04001999 mutex_lock(&inode->i_mutex);
2000 err = d_invalidate(dentry);
2001 if (err)
2002 goto out_unlock;
2003
2004 down_write(&root->fs_info->subvol_sem);
2005
2006 err = may_destroy_subvol(dest);
2007 if (err)
2008 goto out_up_write;
2009
Yan, Zhenga22285a2010-05-16 10:48:46 -04002010 trans = btrfs_start_transaction(root, 0);
2011 if (IS_ERR(trans)) {
2012 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002013 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002014 }
2015 trans->block_rsv = &root->fs_info->global_block_rsv;
2016
Yan, Zheng76dda932009-09-21 16:00:26 -04002017 ret = btrfs_unlink_subvol(trans, root, dir,
2018 dest->root_key.objectid,
2019 dentry->d_name.name,
2020 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002021 if (ret) {
2022 err = ret;
2023 btrfs_abort_transaction(trans, root, ret);
2024 goto out_end_trans;
2025 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002026
2027 btrfs_record_root_in_trans(trans, dest);
2028
2029 memset(&dest->root_item.drop_progress, 0,
2030 sizeof(dest->root_item.drop_progress));
2031 dest->root_item.drop_level = 0;
2032 btrfs_set_root_refs(&dest->root_item, 0);
2033
Yan, Zhengd68fc572010-05-16 10:49:58 -04002034 if (!xchg(&dest->orphan_item_inserted, 1)) {
2035 ret = btrfs_insert_orphan_item(trans,
2036 root->fs_info->tree_root,
2037 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002038 if (ret) {
2039 btrfs_abort_transaction(trans, root, ret);
2040 err = ret;
2041 goto out_end_trans;
2042 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002043 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002044out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002045 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002046 if (ret && !err)
2047 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002048 inode->i_flags |= S_DEAD;
2049out_up_write:
2050 up_write(&root->fs_info->subvol_sem);
2051out_unlock:
2052 mutex_unlock(&inode->i_mutex);
2053 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002054 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002055 btrfs_invalidate_inodes(dest);
2056 d_delete(dentry);
2057 }
2058out_dput:
2059 dput(dentry);
2060out_unlock_dir:
2061 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002062 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002063out:
2064 kfree(vol_args);
2065 return err;
2066}
2067
Chris Mason1e701a32010-03-11 09:42:04 -05002068static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002069{
2070 struct inode *inode = fdentry(file)->d_inode;
2071 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002072 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002073 int ret;
2074
Li Zefanb83cc962010-12-20 16:04:08 +08002075 if (btrfs_root_readonly(root))
2076 return -EROFS;
2077
Al Viroa561be72011-11-23 11:57:51 -05002078 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002079 if (ret)
2080 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002081
2082 switch (inode->i_mode & S_IFMT) {
2083 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002084 if (!capable(CAP_SYS_ADMIN)) {
2085 ret = -EPERM;
2086 goto out;
2087 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002088 ret = btrfs_defrag_root(root, 0);
2089 if (ret)
2090 goto out;
2091 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002092 break;
2093 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002094 if (!(file->f_mode & FMODE_WRITE)) {
2095 ret = -EINVAL;
2096 goto out;
2097 }
Chris Mason1e701a32010-03-11 09:42:04 -05002098
2099 range = kzalloc(sizeof(*range), GFP_KERNEL);
2100 if (!range) {
2101 ret = -ENOMEM;
2102 goto out;
2103 }
2104
2105 if (argp) {
2106 if (copy_from_user(range, argp,
2107 sizeof(*range))) {
2108 ret = -EFAULT;
2109 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002110 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002111 }
2112 /* compression requires us to start the IO */
2113 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2114 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2115 range->extent_thresh = (u32)-1;
2116 }
2117 } else {
2118 /* the rest are all set to zero by kzalloc */
2119 range->len = (u64)-1;
2120 }
Chris Mason4cb53002011-05-24 15:35:30 -04002121 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2122 range, 0, 0);
2123 if (ret > 0)
2124 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002125 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002126 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002127 default:
2128 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002129 }
Chris Masone441d542009-01-05 16:57:23 -05002130out:
Al Viro2a79f172011-12-09 08:06:57 -05002131 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002132 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002133}
2134
Christoph Hellwigb2950862008-12-02 09:54:17 -05002135static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002136{
2137 struct btrfs_ioctl_vol_args *vol_args;
2138 int ret;
2139
Chris Masone441d542009-01-05 16:57:23 -05002140 if (!capable(CAP_SYS_ADMIN))
2141 return -EPERM;
2142
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002143 mutex_lock(&root->fs_info->volume_mutex);
2144 if (root->fs_info->balance_ctl) {
2145 printk(KERN_INFO "btrfs: balance in progress\n");
2146 ret = -EINVAL;
2147 goto out;
2148 }
2149
Li Zefandae7b662009-04-08 15:06:54 +08002150 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002151 if (IS_ERR(vol_args)) {
2152 ret = PTR_ERR(vol_args);
2153 goto out;
2154 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002155
Mark Fasheh5516e592008-07-24 12:20:14 -04002156 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002157 ret = btrfs_init_new_device(root, vol_args->name);
2158
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002159 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002160out:
2161 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002162 return ret;
2163}
2164
Christoph Hellwigb2950862008-12-02 09:54:17 -05002165static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002166{
2167 struct btrfs_ioctl_vol_args *vol_args;
2168 int ret;
2169
Chris Masone441d542009-01-05 16:57:23 -05002170 if (!capable(CAP_SYS_ADMIN))
2171 return -EPERM;
2172
Yan Zhengc146afa2008-11-12 14:34:12 -05002173 if (root->fs_info->sb->s_flags & MS_RDONLY)
2174 return -EROFS;
2175
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002176 mutex_lock(&root->fs_info->volume_mutex);
2177 if (root->fs_info->balance_ctl) {
2178 printk(KERN_INFO "btrfs: balance in progress\n");
2179 ret = -EINVAL;
2180 goto out;
2181 }
2182
Li Zefandae7b662009-04-08 15:06:54 +08002183 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002184 if (IS_ERR(vol_args)) {
2185 ret = PTR_ERR(vol_args);
2186 goto out;
2187 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002188
Mark Fasheh5516e592008-07-24 12:20:14 -04002189 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002190 ret = btrfs_rm_device(root, vol_args->name);
2191
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002192 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002193out:
2194 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002195 return ret;
2196}
2197
Jan Schmidt475f6382011-03-11 15:41:01 +01002198static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2199{
Li Zefan027ed2f2011-06-08 08:27:56 +00002200 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002201 struct btrfs_device *device;
2202 struct btrfs_device *next;
2203 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002204 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002205
2206 if (!capable(CAP_SYS_ADMIN))
2207 return -EPERM;
2208
Li Zefan027ed2f2011-06-08 08:27:56 +00002209 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2210 if (!fi_args)
2211 return -ENOMEM;
2212
2213 fi_args->num_devices = fs_devices->num_devices;
2214 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002215
2216 mutex_lock(&fs_devices->device_list_mutex);
2217 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002218 if (device->devid > fi_args->max_id)
2219 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002220 }
2221 mutex_unlock(&fs_devices->device_list_mutex);
2222
Li Zefan027ed2f2011-06-08 08:27:56 +00002223 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2224 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002225
Li Zefan027ed2f2011-06-08 08:27:56 +00002226 kfree(fi_args);
2227 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002228}
2229
2230static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2231{
2232 struct btrfs_ioctl_dev_info_args *di_args;
2233 struct btrfs_device *dev;
2234 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2235 int ret = 0;
2236 char *s_uuid = NULL;
2237 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2238
2239 if (!capable(CAP_SYS_ADMIN))
2240 return -EPERM;
2241
2242 di_args = memdup_user(arg, sizeof(*di_args));
2243 if (IS_ERR(di_args))
2244 return PTR_ERR(di_args);
2245
2246 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2247 s_uuid = di_args->uuid;
2248
2249 mutex_lock(&fs_devices->device_list_mutex);
2250 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2251 mutex_unlock(&fs_devices->device_list_mutex);
2252
2253 if (!dev) {
2254 ret = -ENODEV;
2255 goto out;
2256 }
2257
2258 di_args->devid = dev->devid;
2259 di_args->bytes_used = dev->bytes_used;
2260 di_args->total_bytes = dev->total_bytes;
2261 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2262 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2263
2264out:
2265 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2266 ret = -EFAULT;
2267
2268 kfree(di_args);
2269 return ret;
2270}
2271
Yan, Zheng76dda932009-09-21 16:00:26 -04002272static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2273 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002274{
2275 struct inode *inode = fdentry(file)->d_inode;
2276 struct btrfs_root *root = BTRFS_I(inode)->root;
2277 struct file *src_file;
2278 struct inode *src;
2279 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002280 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002281 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002282 char *buf;
2283 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002284 u32 nritems;
2285 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002286 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002287 u64 len = olen;
2288 u64 bs = root->fs_info->sb->s_blocksize;
2289 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002290
Sage Weilc5c9cd42008-11-12 14:32:25 -05002291 /*
2292 * TODO:
2293 * - split compressed inline extents. annoying: we need to
2294 * decompress into destination's address_space (the file offset
2295 * may change, so source mapping won't do), then recompress (or
2296 * otherwise reinsert) a subrange.
2297 * - allow ranges within the same file to be cloned (provided
2298 * they don't overlap)?
2299 */
2300
Chris Masone441d542009-01-05 16:57:23 -05002301 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002302 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002303 return -EINVAL;
2304
Li Zefanb83cc962010-12-20 16:04:08 +08002305 if (btrfs_root_readonly(root))
2306 return -EROFS;
2307
Al Viroa561be72011-11-23 11:57:51 -05002308 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002309 if (ret)
2310 return ret;
2311
Sage Weilc5c9cd42008-11-12 14:32:25 -05002312 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002313 if (!src_file) {
2314 ret = -EBADF;
2315 goto out_drop_write;
2316 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002317
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002318 src = src_file->f_dentry->d_inode;
2319
Sage Weilc5c9cd42008-11-12 14:32:25 -05002320 ret = -EINVAL;
2321 if (src == inode)
2322 goto out_fput;
2323
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002324 /* the src must be open for reading */
2325 if (!(src_file->f_mode & FMODE_READ))
2326 goto out_fput;
2327
Li Zefan0e7b8242011-09-18 10:20:46 -04002328 /* don't make the dst file partly checksummed */
2329 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2330 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2331 goto out_fput;
2332
Yan Zhengae01a0a2008-08-04 23:23:47 -04002333 ret = -EISDIR;
2334 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002335 goto out_fput;
2336
Yan Zhengae01a0a2008-08-04 23:23:47 -04002337 ret = -EXDEV;
2338 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2339 goto out_fput;
2340
2341 ret = -ENOMEM;
2342 buf = vmalloc(btrfs_level_size(root, 0));
2343 if (!buf)
2344 goto out_fput;
2345
2346 path = btrfs_alloc_path();
2347 if (!path) {
2348 vfree(buf);
2349 goto out_fput;
2350 }
2351 path->reada = 2;
2352
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002353 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002354 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2355 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002356 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002357 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2358 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002359 }
2360
Sage Weilc5c9cd42008-11-12 14:32:25 -05002361 /* determine range to clone */
2362 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002363 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002364 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002365 if (len == 0)
2366 olen = len = src->i_size - off;
2367 /* if we extend to eof, continue to block boundary */
2368 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002369 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002370
2371 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002372 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2373 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002374 goto out_unlock;
2375
Li Zefand525e8a2011-09-11 10:52:25 -04002376 if (destoff > inode->i_size) {
2377 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2378 if (ret)
2379 goto out_unlock;
2380 }
2381
Li Zefan71ef0782011-09-18 10:20:46 -04002382 /* truncate page cache pages from target inode range */
2383 truncate_inode_pages_range(&inode->i_data, destoff,
2384 PAGE_CACHE_ALIGN(destoff + len) - 1);
2385
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002386 /* do any pending delalloc/csum calc on src, one way or
2387 another, and lock file content */
2388 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002389 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002390 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002391 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2392 if (!ordered &&
2393 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2394 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002395 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002396 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002397 if (ordered)
2398 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002399 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002400 }
2401
Sage Weilc5c9cd42008-11-12 14:32:25 -05002402 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002403 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002404 key.type = BTRFS_EXTENT_DATA_KEY;
2405 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002406
2407 while (1) {
2408 /*
2409 * note the key will change type as we walk through the
2410 * tree.
2411 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002412 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002413 if (ret < 0)
2414 goto out;
2415
Yan Zhengae01a0a2008-08-04 23:23:47 -04002416 nritems = btrfs_header_nritems(path->nodes[0]);
2417 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002418 ret = btrfs_next_leaf(root, path);
2419 if (ret < 0)
2420 goto out;
2421 if (ret > 0)
2422 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002423 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002424 }
2425 leaf = path->nodes[0];
2426 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002427
Yan Zhengae01a0a2008-08-04 23:23:47 -04002428 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002429 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002430 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002431 break;
2432
Sage Weilc5c9cd42008-11-12 14:32:25 -05002433 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2434 struct btrfs_file_extent_item *extent;
2435 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002436 u32 size;
2437 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002438 u64 disko = 0, diskl = 0;
2439 u64 datao = 0, datal = 0;
2440 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002441 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002442
2443 size = btrfs_item_size_nr(leaf, slot);
2444 read_extent_buffer(leaf, buf,
2445 btrfs_item_ptr_offset(leaf, slot),
2446 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002447
2448 extent = btrfs_item_ptr(leaf, slot,
2449 struct btrfs_file_extent_item);
2450 comp = btrfs_file_extent_compression(leaf, extent);
2451 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002452 if (type == BTRFS_FILE_EXTENT_REG ||
2453 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002454 disko = btrfs_file_extent_disk_bytenr(leaf,
2455 extent);
2456 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2457 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002458 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002459 datal = btrfs_file_extent_num_bytes(leaf,
2460 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002461 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2462 /* take upper bound, may be compressed */
2463 datal = btrfs_file_extent_ram_bytes(leaf,
2464 extent);
2465 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002466 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002467
Sage Weil050006a2010-10-29 15:37:33 -04002468 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002469 key.offset >= off+len)
2470 goto next;
2471
Zheng Yan31840ae2008-09-23 13:14:14 -04002472 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002473 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002474 if (off <= key.offset)
2475 new_key.offset = key.offset + destoff - off;
2476 else
2477 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002478
Sage Weilb6f34092011-09-20 14:48:51 -04002479 /*
2480 * 1 - adjusting old extent (we may have to split it)
2481 * 1 - add new extent
2482 * 1 - inode update
2483 */
2484 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002485 if (IS_ERR(trans)) {
2486 ret = PTR_ERR(trans);
2487 goto out;
2488 }
2489
Chris Masonc8a894d2009-06-27 21:07:03 -04002490 if (type == BTRFS_FILE_EXTENT_REG ||
2491 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002492 /*
2493 * a | --- range to clone ---| b
2494 * | ------------- extent ------------- |
2495 */
2496
2497 /* substract range b */
2498 if (key.offset + datal > off + len)
2499 datal = off + len - key.offset;
2500
2501 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002502 if (off > key.offset) {
2503 datao += off - key.offset;
2504 datal -= off - key.offset;
2505 }
2506
Yan, Zhenga22285a2010-05-16 10:48:46 -04002507 ret = btrfs_drop_extents(trans, inode,
2508 new_key.offset,
2509 new_key.offset + datal,
2510 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002511 if (ret) {
2512 btrfs_abort_transaction(trans, root,
2513 ret);
2514 btrfs_end_transaction(trans, root);
2515 goto out;
2516 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002517
Sage Weilc5c9cd42008-11-12 14:32:25 -05002518 ret = btrfs_insert_empty_item(trans, root, path,
2519 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002520 if (ret) {
2521 btrfs_abort_transaction(trans, root,
2522 ret);
2523 btrfs_end_transaction(trans, root);
2524 goto out;
2525 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002526
2527 leaf = path->nodes[0];
2528 slot = path->slots[0];
2529 write_extent_buffer(leaf, buf,
2530 btrfs_item_ptr_offset(leaf, slot),
2531 size);
2532
2533 extent = btrfs_item_ptr(leaf, slot,
2534 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002535
Sage Weilc5c9cd42008-11-12 14:32:25 -05002536 /* disko == 0 means it's a hole */
2537 if (!disko)
2538 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002539
2540 btrfs_set_file_extent_offset(leaf, extent,
2541 datao);
2542 btrfs_set_file_extent_num_bytes(leaf, extent,
2543 datal);
2544 if (disko) {
2545 inode_add_bytes(inode, datal);
2546 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002547 disko, diskl, 0,
2548 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002549 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002550 new_key.offset - datao,
2551 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002552 if (ret) {
2553 btrfs_abort_transaction(trans,
2554 root,
2555 ret);
2556 btrfs_end_transaction(trans,
2557 root);
2558 goto out;
2559
2560 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002561 }
2562 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2563 u64 skip = 0;
2564 u64 trim = 0;
2565 if (off > key.offset) {
2566 skip = off - key.offset;
2567 new_key.offset += skip;
2568 }
Chris Masond3977122009-01-05 21:25:51 -05002569
Sage Weilc5c9cd42008-11-12 14:32:25 -05002570 if (key.offset + datal > off+len)
2571 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002572
Sage Weilc5c9cd42008-11-12 14:32:25 -05002573 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002574 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002575 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002576 goto out;
2577 }
2578 size -= skip + trim;
2579 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002580
2581 ret = btrfs_drop_extents(trans, inode,
2582 new_key.offset,
2583 new_key.offset + datal,
2584 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002585 if (ret) {
2586 btrfs_abort_transaction(trans, root,
2587 ret);
2588 btrfs_end_transaction(trans, root);
2589 goto out;
2590 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002591
Sage Weilc5c9cd42008-11-12 14:32:25 -05002592 ret = btrfs_insert_empty_item(trans, root, path,
2593 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002594 if (ret) {
2595 btrfs_abort_transaction(trans, root,
2596 ret);
2597 btrfs_end_transaction(trans, root);
2598 goto out;
2599 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002600
2601 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002602 u32 start =
2603 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002604 memmove(buf+start, buf+start+skip,
2605 datal);
2606 }
2607
2608 leaf = path->nodes[0];
2609 slot = path->slots[0];
2610 write_extent_buffer(leaf, buf,
2611 btrfs_item_ptr_offset(leaf, slot),
2612 size);
2613 inode_add_bytes(inode, datal);
2614 }
2615
2616 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002617 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002618
Yan, Zhenga22285a2010-05-16 10:48:46 -04002619 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002620
2621 /*
2622 * we round up to the block size at eof when
2623 * determining which extents to clone above,
2624 * but shouldn't round up the file size
2625 */
2626 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002627 if (endoff > destoff+olen)
2628 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002629 if (endoff > inode->i_size)
2630 btrfs_i_size_write(inode, endoff);
2631
Yan, Zhenga22285a2010-05-16 10:48:46 -04002632 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002633 if (ret) {
2634 btrfs_abort_transaction(trans, root, ret);
2635 btrfs_end_transaction(trans, root);
2636 goto out;
2637 }
2638 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002639 }
Chris Masond3977122009-01-05 21:25:51 -05002640next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002641 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002642 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002643 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002644 ret = 0;
2645out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002646 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002647 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002648out_unlock:
2649 mutex_unlock(&src->i_mutex);
2650 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002651 vfree(buf);
2652 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002653out_fput:
2654 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002655out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002656 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002657 return ret;
2658}
2659
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002660static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002661{
2662 struct btrfs_ioctl_clone_range_args args;
2663
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002664 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002665 return -EFAULT;
2666 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2667 args.src_length, args.dest_offset);
2668}
2669
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002670/*
2671 * there are many ways the trans_start and trans_end ioctls can lead
2672 * to deadlocks. They should only be used by applications that
2673 * basically own the machine, and have a very in depth understanding
2674 * of all the possible deadlocks and enospc problems.
2675 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002676static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002677{
2678 struct inode *inode = fdentry(file)->d_inode;
2679 struct btrfs_root *root = BTRFS_I(inode)->root;
2680 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002681 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002682
Sage Weil1ab86ae2009-09-29 18:38:44 -04002683 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002684 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002685 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002686
2687 ret = -EINPROGRESS;
2688 if (file->private_data)
2689 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002690
Li Zefanb83cc962010-12-20 16:04:08 +08002691 ret = -EROFS;
2692 if (btrfs_root_readonly(root))
2693 goto out;
2694
Al Viroa561be72011-11-23 11:57:51 -05002695 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002696 if (ret)
2697 goto out;
2698
Josef Bacika4abeea2011-04-11 17:25:13 -04002699 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002700
Sage Weil1ab86ae2009-09-29 18:38:44 -04002701 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002702 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002703 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002704 goto out_drop;
2705
2706 file->private_data = trans;
2707 return 0;
2708
2709out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002710 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002711 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002712out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002713 return ret;
2714}
2715
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002716static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2717{
2718 struct inode *inode = fdentry(file)->d_inode;
2719 struct btrfs_root *root = BTRFS_I(inode)->root;
2720 struct btrfs_root *new_root;
2721 struct btrfs_dir_item *di;
2722 struct btrfs_trans_handle *trans;
2723 struct btrfs_path *path;
2724 struct btrfs_key location;
2725 struct btrfs_disk_key disk_key;
2726 struct btrfs_super_block *disk_super;
2727 u64 features;
2728 u64 objectid = 0;
2729 u64 dir_id;
2730
2731 if (!capable(CAP_SYS_ADMIN))
2732 return -EPERM;
2733
2734 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2735 return -EFAULT;
2736
2737 if (!objectid)
2738 objectid = root->root_key.objectid;
2739
2740 location.objectid = objectid;
2741 location.type = BTRFS_ROOT_ITEM_KEY;
2742 location.offset = (u64)-1;
2743
2744 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2745 if (IS_ERR(new_root))
2746 return PTR_ERR(new_root);
2747
2748 if (btrfs_root_refs(&new_root->root_item) == 0)
2749 return -ENOENT;
2750
2751 path = btrfs_alloc_path();
2752 if (!path)
2753 return -ENOMEM;
2754 path->leave_spinning = 1;
2755
2756 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002757 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002758 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002759 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002760 }
2761
David Sterba6c417612011-04-13 15:41:04 +02002762 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002763 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2764 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002765 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002766 btrfs_free_path(path);
2767 btrfs_end_transaction(trans, root);
2768 printk(KERN_ERR "Umm, you don't have the default dir item, "
2769 "this isn't going to work\n");
2770 return -ENOENT;
2771 }
2772
2773 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2774 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2775 btrfs_mark_buffer_dirty(path->nodes[0]);
2776 btrfs_free_path(path);
2777
David Sterba6c417612011-04-13 15:41:04 +02002778 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002779 features = btrfs_super_incompat_flags(disk_super);
2780 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2781 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2782 btrfs_set_super_incompat_flags(disk_super, features);
2783 }
2784 btrfs_end_transaction(trans, root);
2785
2786 return 0;
2787}
2788
Josef Bacikbf5fc092010-09-29 11:22:36 -04002789static void get_block_group_info(struct list_head *groups_list,
2790 struct btrfs_ioctl_space_info *space)
2791{
2792 struct btrfs_block_group_cache *block_group;
2793
2794 space->total_bytes = 0;
2795 space->used_bytes = 0;
2796 space->flags = 0;
2797 list_for_each_entry(block_group, groups_list, list) {
2798 space->flags = block_group->flags;
2799 space->total_bytes += block_group->key.offset;
2800 space->used_bytes +=
2801 btrfs_block_group_used(&block_group->item);
2802 }
2803}
2804
Josef Bacik1406e432010-01-13 18:19:06 +00002805long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2806{
2807 struct btrfs_ioctl_space_args space_args;
2808 struct btrfs_ioctl_space_info space;
2809 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002810 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002811 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002812 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002813 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2814 BTRFS_BLOCK_GROUP_SYSTEM,
2815 BTRFS_BLOCK_GROUP_METADATA,
2816 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2817 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002818 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002819 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002820 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002821 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002822
2823 if (copy_from_user(&space_args,
2824 (struct btrfs_ioctl_space_args __user *)arg,
2825 sizeof(space_args)))
2826 return -EFAULT;
2827
Josef Bacikbf5fc092010-09-29 11:22:36 -04002828 for (i = 0; i < num_types; i++) {
2829 struct btrfs_space_info *tmp;
2830
2831 info = NULL;
2832 rcu_read_lock();
2833 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2834 list) {
2835 if (tmp->flags == types[i]) {
2836 info = tmp;
2837 break;
2838 }
2839 }
2840 rcu_read_unlock();
2841
2842 if (!info)
2843 continue;
2844
2845 down_read(&info->groups_sem);
2846 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2847 if (!list_empty(&info->block_groups[c]))
2848 slot_count++;
2849 }
2850 up_read(&info->groups_sem);
2851 }
Josef Bacik1406e432010-01-13 18:19:06 +00002852
Chris Mason7fde62b2010-03-16 15:40:10 -04002853 /* space_slots == 0 means they are asking for a count */
2854 if (space_args.space_slots == 0) {
2855 space_args.total_spaces = slot_count;
2856 goto out;
2857 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002858
Dan Rosenberg51788b12011-02-14 16:04:23 -05002859 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002860
Chris Mason7fde62b2010-03-16 15:40:10 -04002861 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002862
Chris Mason7fde62b2010-03-16 15:40:10 -04002863 /* we generally have at most 6 or so space infos, one for each raid
2864 * level. So, a whole page should be more than enough for everyone
2865 */
2866 if (alloc_size > PAGE_CACHE_SIZE)
2867 return -ENOMEM;
2868
2869 space_args.total_spaces = 0;
2870 dest = kmalloc(alloc_size, GFP_NOFS);
2871 if (!dest)
2872 return -ENOMEM;
2873 dest_orig = dest;
2874
2875 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002876 for (i = 0; i < num_types; i++) {
2877 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002878
Dan Rosenberg51788b12011-02-14 16:04:23 -05002879 if (!slot_count)
2880 break;
2881
Josef Bacikbf5fc092010-09-29 11:22:36 -04002882 info = NULL;
2883 rcu_read_lock();
2884 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2885 list) {
2886 if (tmp->flags == types[i]) {
2887 info = tmp;
2888 break;
2889 }
2890 }
2891 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002892
Josef Bacikbf5fc092010-09-29 11:22:36 -04002893 if (!info)
2894 continue;
2895 down_read(&info->groups_sem);
2896 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2897 if (!list_empty(&info->block_groups[c])) {
2898 get_block_group_info(&info->block_groups[c],
2899 &space);
2900 memcpy(dest, &space, sizeof(space));
2901 dest++;
2902 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002903 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002904 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002905 if (!slot_count)
2906 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002907 }
2908 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002909 }
Josef Bacik1406e432010-01-13 18:19:06 +00002910
Chris Mason7fde62b2010-03-16 15:40:10 -04002911 user_dest = (struct btrfs_ioctl_space_info *)
2912 (arg + sizeof(struct btrfs_ioctl_space_args));
2913
2914 if (copy_to_user(user_dest, dest_orig, alloc_size))
2915 ret = -EFAULT;
2916
2917 kfree(dest_orig);
2918out:
2919 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002920 ret = -EFAULT;
2921
2922 return ret;
2923}
2924
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002925/*
2926 * there are many ways the trans_start and trans_end ioctls can lead
2927 * to deadlocks. They should only be used by applications that
2928 * basically own the machine, and have a very in depth understanding
2929 * of all the possible deadlocks and enospc problems.
2930 */
2931long btrfs_ioctl_trans_end(struct file *file)
2932{
2933 struct inode *inode = fdentry(file)->d_inode;
2934 struct btrfs_root *root = BTRFS_I(inode)->root;
2935 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002936
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002937 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002938 if (!trans)
2939 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002940 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002941
Sage Weil1ab86ae2009-09-29 18:38:44 -04002942 btrfs_end_transaction(trans, root);
2943
Josef Bacika4abeea2011-04-11 17:25:13 -04002944 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002945
Al Viro2a79f172011-12-09 08:06:57 -05002946 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002947 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002948}
2949
Sage Weil46204592010-10-29 15:41:32 -04002950static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2951{
2952 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2953 struct btrfs_trans_handle *trans;
2954 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002955 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002956
2957 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002958 if (IS_ERR(trans))
2959 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002960 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002961 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002962 if (ret) {
2963 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002964 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002965 }
Sage Weil46204592010-10-29 15:41:32 -04002966
2967 if (argp)
2968 if (copy_to_user(argp, &transid, sizeof(transid)))
2969 return -EFAULT;
2970 return 0;
2971}
2972
2973static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2974{
2975 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2976 u64 transid;
2977
2978 if (argp) {
2979 if (copy_from_user(&transid, argp, sizeof(transid)))
2980 return -EFAULT;
2981 } else {
2982 transid = 0; /* current trans */
2983 }
2984 return btrfs_wait_for_commit(root, transid);
2985}
2986
Jan Schmidt475f6382011-03-11 15:41:01 +01002987static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2988{
2989 int ret;
2990 struct btrfs_ioctl_scrub_args *sa;
2991
2992 if (!capable(CAP_SYS_ADMIN))
2993 return -EPERM;
2994
2995 sa = memdup_user(arg, sizeof(*sa));
2996 if (IS_ERR(sa))
2997 return PTR_ERR(sa);
2998
2999 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01003000 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01003001
3002 if (copy_to_user(arg, sa, sizeof(*sa)))
3003 ret = -EFAULT;
3004
3005 kfree(sa);
3006 return ret;
3007}
3008
3009static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3010{
3011 if (!capable(CAP_SYS_ADMIN))
3012 return -EPERM;
3013
3014 return btrfs_scrub_cancel(root);
3015}
3016
3017static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3018 void __user *arg)
3019{
3020 struct btrfs_ioctl_scrub_args *sa;
3021 int ret;
3022
3023 if (!capable(CAP_SYS_ADMIN))
3024 return -EPERM;
3025
3026 sa = memdup_user(arg, sizeof(*sa));
3027 if (IS_ERR(sa))
3028 return PTR_ERR(sa);
3029
3030 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3031
3032 if (copy_to_user(arg, sa, sizeof(*sa)))
3033 ret = -EFAULT;
3034
3035 kfree(sa);
3036 return ret;
3037}
3038
Jan Schmidtd7728c92011-07-07 16:48:38 +02003039static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3040{
3041 int ret = 0;
3042 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003043 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003044 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003045 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003046 struct inode_fs_paths *ipath = NULL;
3047 struct btrfs_path *path;
3048
3049 if (!capable(CAP_SYS_ADMIN))
3050 return -EPERM;
3051
3052 path = btrfs_alloc_path();
3053 if (!path) {
3054 ret = -ENOMEM;
3055 goto out;
3056 }
3057
3058 ipa = memdup_user(arg, sizeof(*ipa));
3059 if (IS_ERR(ipa)) {
3060 ret = PTR_ERR(ipa);
3061 ipa = NULL;
3062 goto out;
3063 }
3064
3065 size = min_t(u32, ipa->size, 4096);
3066 ipath = init_ipath(size, root, path);
3067 if (IS_ERR(ipath)) {
3068 ret = PTR_ERR(ipath);
3069 ipath = NULL;
3070 goto out;
3071 }
3072
3073 ret = paths_from_inode(ipa->inum, ipath);
3074 if (ret < 0)
3075 goto out;
3076
3077 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003078 rel_ptr = ipath->fspath->val[i] -
3079 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003080 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003081 }
3082
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003083 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3084 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003085 if (ret) {
3086 ret = -EFAULT;
3087 goto out;
3088 }
3089
3090out:
3091 btrfs_free_path(path);
3092 free_ipath(ipath);
3093 kfree(ipa);
3094
3095 return ret;
3096}
3097
3098static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3099{
3100 struct btrfs_data_container *inodes = ctx;
3101 const size_t c = 3 * sizeof(u64);
3102
3103 if (inodes->bytes_left >= c) {
3104 inodes->bytes_left -= c;
3105 inodes->val[inodes->elem_cnt] = inum;
3106 inodes->val[inodes->elem_cnt + 1] = offset;
3107 inodes->val[inodes->elem_cnt + 2] = root;
3108 inodes->elem_cnt += 3;
3109 } else {
3110 inodes->bytes_missing += c - inodes->bytes_left;
3111 inodes->bytes_left = 0;
3112 inodes->elem_missed += 3;
3113 }
3114
3115 return 0;
3116}
3117
3118static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3119 void __user *arg)
3120{
3121 int ret = 0;
3122 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003123 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003124 struct btrfs_ioctl_logical_ino_args *loi;
3125 struct btrfs_data_container *inodes = NULL;
3126 struct btrfs_path *path = NULL;
3127 struct btrfs_key key;
3128
3129 if (!capable(CAP_SYS_ADMIN))
3130 return -EPERM;
3131
3132 loi = memdup_user(arg, sizeof(*loi));
3133 if (IS_ERR(loi)) {
3134 ret = PTR_ERR(loi);
3135 loi = NULL;
3136 goto out;
3137 }
3138
3139 path = btrfs_alloc_path();
3140 if (!path) {
3141 ret = -ENOMEM;
3142 goto out;
3143 }
3144
3145 size = min_t(u32, loi->size, 4096);
3146 inodes = init_data_container(size);
3147 if (IS_ERR(inodes)) {
3148 ret = PTR_ERR(inodes);
3149 inodes = NULL;
3150 goto out;
3151 }
3152
3153 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003154 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003155
3156 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3157 ret = -ENOENT;
3158 if (ret < 0)
3159 goto out;
3160
Jan Schmidt4692cf52011-12-02 14:56:41 +01003161 extent_item_pos = loi->logical - key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01003162 ret = iterate_extent_inodes(root->fs_info, key.objectid,
3163 extent_item_pos, 0, build_ino_list,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003164 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003165
3166 if (ret < 0)
3167 goto out;
3168
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003169 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3170 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003171 if (ret)
3172 ret = -EFAULT;
3173
3174out:
3175 btrfs_free_path(path);
3176 kfree(inodes);
3177 kfree(loi);
3178
3179 return ret;
3180}
3181
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003182void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003183 struct btrfs_ioctl_balance_args *bargs)
3184{
3185 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3186
3187 bargs->flags = bctl->flags;
3188
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003189 if (atomic_read(&fs_info->balance_running))
3190 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3191 if (atomic_read(&fs_info->balance_pause_req))
3192 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003193 if (atomic_read(&fs_info->balance_cancel_req))
3194 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003195
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003196 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3197 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3198 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003199
3200 if (lock) {
3201 spin_lock(&fs_info->balance_lock);
3202 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3203 spin_unlock(&fs_info->balance_lock);
3204 } else {
3205 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3206 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003207}
3208
3209static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3210{
3211 struct btrfs_fs_info *fs_info = root->fs_info;
3212 struct btrfs_ioctl_balance_args *bargs;
3213 struct btrfs_balance_control *bctl;
3214 int ret;
3215
3216 if (!capable(CAP_SYS_ADMIN))
3217 return -EPERM;
3218
3219 if (fs_info->sb->s_flags & MS_RDONLY)
3220 return -EROFS;
3221
3222 mutex_lock(&fs_info->volume_mutex);
3223 mutex_lock(&fs_info->balance_mutex);
3224
3225 if (arg) {
3226 bargs = memdup_user(arg, sizeof(*bargs));
3227 if (IS_ERR(bargs)) {
3228 ret = PTR_ERR(bargs);
3229 goto out;
3230 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003231
3232 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3233 if (!fs_info->balance_ctl) {
3234 ret = -ENOTCONN;
3235 goto out_bargs;
3236 }
3237
3238 bctl = fs_info->balance_ctl;
3239 spin_lock(&fs_info->balance_lock);
3240 bctl->flags |= BTRFS_BALANCE_RESUME;
3241 spin_unlock(&fs_info->balance_lock);
3242
3243 goto do_balance;
3244 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003245 } else {
3246 bargs = NULL;
3247 }
3248
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003249 if (fs_info->balance_ctl) {
3250 ret = -EINPROGRESS;
3251 goto out_bargs;
3252 }
3253
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003254 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3255 if (!bctl) {
3256 ret = -ENOMEM;
3257 goto out_bargs;
3258 }
3259
3260 bctl->fs_info = fs_info;
3261 if (arg) {
3262 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3263 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3264 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3265
3266 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003267 } else {
3268 /* balance everything - no filters */
3269 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003270 }
3271
Ilya Dryomovde322262012-01-16 22:04:49 +02003272do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003273 ret = btrfs_balance(bctl, bargs);
3274 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003275 * bctl is freed in __cancel_balance or in free_fs_info if
3276 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003277 */
3278 if (arg) {
3279 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3280 ret = -EFAULT;
3281 }
3282
3283out_bargs:
3284 kfree(bargs);
3285out:
3286 mutex_unlock(&fs_info->balance_mutex);
3287 mutex_unlock(&fs_info->volume_mutex);
3288 return ret;
3289}
3290
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003291static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3292{
3293 if (!capable(CAP_SYS_ADMIN))
3294 return -EPERM;
3295
3296 switch (cmd) {
3297 case BTRFS_BALANCE_CTL_PAUSE:
3298 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003299 case BTRFS_BALANCE_CTL_CANCEL:
3300 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003301 }
3302
3303 return -EINVAL;
3304}
3305
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003306static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3307 void __user *arg)
3308{
3309 struct btrfs_fs_info *fs_info = root->fs_info;
3310 struct btrfs_ioctl_balance_args *bargs;
3311 int ret = 0;
3312
3313 if (!capable(CAP_SYS_ADMIN))
3314 return -EPERM;
3315
3316 mutex_lock(&fs_info->balance_mutex);
3317 if (!fs_info->balance_ctl) {
3318 ret = -ENOTCONN;
3319 goto out;
3320 }
3321
3322 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3323 if (!bargs) {
3324 ret = -ENOMEM;
3325 goto out;
3326 }
3327
3328 update_ioctl_balance_args(fs_info, 1, bargs);
3329
3330 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3331 ret = -EFAULT;
3332
3333 kfree(bargs);
3334out:
3335 mutex_unlock(&fs_info->balance_mutex);
3336 return ret;
3337}
3338
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003339long btrfs_ioctl(struct file *file, unsigned int
3340 cmd, unsigned long arg)
3341{
3342 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003343 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003344
3345 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003346 case FS_IOC_GETFLAGS:
3347 return btrfs_ioctl_getflags(file, argp);
3348 case FS_IOC_SETFLAGS:
3349 return btrfs_ioctl_setflags(file, argp);
3350 case FS_IOC_GETVERSION:
3351 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003352 case FITRIM:
3353 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003354 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003355 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003356 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003357 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003358 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003359 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003360 case BTRFS_IOC_SNAP_DESTROY:
3361 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003362 case BTRFS_IOC_SUBVOL_GETFLAGS:
3363 return btrfs_ioctl_subvol_getflags(file, argp);
3364 case BTRFS_IOC_SUBVOL_SETFLAGS:
3365 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003366 case BTRFS_IOC_DEFAULT_SUBVOL:
3367 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003368 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003369 return btrfs_ioctl_defrag(file, NULL);
3370 case BTRFS_IOC_DEFRAG_RANGE:
3371 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003372 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003373 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003374 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003375 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003376 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003377 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003378 case BTRFS_IOC_FS_INFO:
3379 return btrfs_ioctl_fs_info(root, argp);
3380 case BTRFS_IOC_DEV_INFO:
3381 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003382 case BTRFS_IOC_BALANCE:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003383 return btrfs_ioctl_balance(root, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003384 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003385 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3386 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003387 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003388 case BTRFS_IOC_TRANS_START:
3389 return btrfs_ioctl_trans_start(file);
3390 case BTRFS_IOC_TRANS_END:
3391 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003392 case BTRFS_IOC_TREE_SEARCH:
3393 return btrfs_ioctl_tree_search(file, argp);
3394 case BTRFS_IOC_INO_LOOKUP:
3395 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003396 case BTRFS_IOC_INO_PATHS:
3397 return btrfs_ioctl_ino_to_path(root, argp);
3398 case BTRFS_IOC_LOGICAL_INO:
3399 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003400 case BTRFS_IOC_SPACE_INFO:
3401 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003402 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003403 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3404 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003405 case BTRFS_IOC_START_SYNC:
3406 return btrfs_ioctl_start_sync(file, argp);
3407 case BTRFS_IOC_WAIT_SYNC:
3408 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003409 case BTRFS_IOC_SCRUB:
3410 return btrfs_ioctl_scrub(root, argp);
3411 case BTRFS_IOC_SCRUB_CANCEL:
3412 return btrfs_ioctl_scrub_cancel(root, argp);
3413 case BTRFS_IOC_SCRUB_PROGRESS:
3414 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003415 case BTRFS_IOC_BALANCE_V2:
3416 return btrfs_ioctl_balance(root, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003417 case BTRFS_IOC_BALANCE_CTL:
3418 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003419 case BTRFS_IOC_BALANCE_PROGRESS:
3420 return btrfs_ioctl_balance_progress(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003421 }
3422
3423 return -ENOTTY;
3424}