blob: 10bc65ed736c638db6d80b8b6123bc875d7e3828 [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000043#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020044#include <linux/uuid.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050045#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
50#include "ioctl.h"
51#include "print-tree.h"
52#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040053#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020055#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040056#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020057#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010058#include "dev-replace.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040059
Christoph Hellwig6cbff002009-04-17 10:37:41 +020060/* Mask out flags that are inappropriate for the given type of inode. */
61static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
62{
63 if (S_ISDIR(mode))
64 return flags;
65 else if (S_ISREG(mode))
66 return flags & ~FS_DIRSYNC_FL;
67 else
68 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
69}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040070
Christoph Hellwig6cbff002009-04-17 10:37:41 +020071/*
72 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
73 */
74static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
75{
76 unsigned int iflags = 0;
77
78 if (flags & BTRFS_INODE_SYNC)
79 iflags |= FS_SYNC_FL;
80 if (flags & BTRFS_INODE_IMMUTABLE)
81 iflags |= FS_IMMUTABLE_FL;
82 if (flags & BTRFS_INODE_APPEND)
83 iflags |= FS_APPEND_FL;
84 if (flags & BTRFS_INODE_NODUMP)
85 iflags |= FS_NODUMP_FL;
86 if (flags & BTRFS_INODE_NOATIME)
87 iflags |= FS_NOATIME_FL;
88 if (flags & BTRFS_INODE_DIRSYNC)
89 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000090 if (flags & BTRFS_INODE_NODATACOW)
91 iflags |= FS_NOCOW_FL;
92
93 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
94 iflags |= FS_COMPR_FL;
95 else if (flags & BTRFS_INODE_NOCOMPRESS)
96 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097
98 return iflags;
99}
100
101/*
102 * Update inode->i_flags based on the btrfs internal flags.
103 */
104void btrfs_update_iflags(struct inode *inode)
105{
106 struct btrfs_inode *ip = BTRFS_I(inode);
107
108 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
109
110 if (ip->flags & BTRFS_INODE_SYNC)
111 inode->i_flags |= S_SYNC;
112 if (ip->flags & BTRFS_INODE_IMMUTABLE)
113 inode->i_flags |= S_IMMUTABLE;
114 if (ip->flags & BTRFS_INODE_APPEND)
115 inode->i_flags |= S_APPEND;
116 if (ip->flags & BTRFS_INODE_NOATIME)
117 inode->i_flags |= S_NOATIME;
118 if (ip->flags & BTRFS_INODE_DIRSYNC)
119 inode->i_flags |= S_DIRSYNC;
120}
121
122/*
123 * Inherit flags from the parent inode.
124 *
Josef Bacike27425d2011-09-27 11:01:30 -0400125 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200126 */
127void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
128{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400129 unsigned int flags;
130
131 if (!dir)
132 return;
133
134 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200135
Josef Bacike27425d2011-09-27 11:01:30 -0400136 if (flags & BTRFS_INODE_NOCOMPRESS) {
137 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
138 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
139 } else if (flags & BTRFS_INODE_COMPRESS) {
140 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
141 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
142 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143
Josef Bacike27425d2011-09-27 11:01:30 -0400144 if (flags & BTRFS_INODE_NODATACOW)
145 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
146
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147 btrfs_update_iflags(inode);
148}
149
150static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
151{
152 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
153 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
154
155 if (copy_to_user(arg, &flags, sizeof(flags)))
156 return -EFAULT;
157 return 0;
158}
159
Liu Bo75e7cb72011-03-22 10:12:20 +0000160static int check_flags(unsigned int flags)
161{
162 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
163 FS_NOATIME_FL | FS_NODUMP_FL | \
164 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000165 FS_NOCOMP_FL | FS_COMPR_FL |
166 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000167 return -EOPNOTSUPP;
168
169 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
170 return -EINVAL;
171
Liu Bo75e7cb72011-03-22 10:12:20 +0000172 return 0;
173}
174
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200175static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
176{
177 struct inode *inode = file->f_path.dentry->d_inode;
178 struct btrfs_inode *ip = BTRFS_I(inode);
179 struct btrfs_root *root = ip->root;
180 struct btrfs_trans_handle *trans;
181 unsigned int flags, oldflags;
182 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800183 u64 ip_oldflags;
184 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600185 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200186
Li Zefanb83cc962010-12-20 16:04:08 +0800187 if (btrfs_root_readonly(root))
188 return -EROFS;
189
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200190 if (copy_from_user(&flags, arg, sizeof(flags)))
191 return -EFAULT;
192
Liu Bo75e7cb72011-03-22 10:12:20 +0000193 ret = check_flags(flags);
194 if (ret)
195 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200196
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700197 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200198 return -EACCES;
199
Jan Karae7848682012-06-12 16:20:32 +0200200 ret = mnt_want_write_file(file);
201 if (ret)
202 return ret;
203
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200204 mutex_lock(&inode->i_mutex);
205
Li Zefanf062abf2011-12-29 13:36:45 +0800206 ip_oldflags = ip->flags;
207 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600208 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800209
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200210 flags = btrfs_mask_flags(inode->i_mode, flags);
211 oldflags = btrfs_flags_to_ioctl(ip->flags);
212 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
213 if (!capable(CAP_LINUX_IMMUTABLE)) {
214 ret = -EPERM;
215 goto out_unlock;
216 }
217 }
218
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200219 if (flags & FS_SYNC_FL)
220 ip->flags |= BTRFS_INODE_SYNC;
221 else
222 ip->flags &= ~BTRFS_INODE_SYNC;
223 if (flags & FS_IMMUTABLE_FL)
224 ip->flags |= BTRFS_INODE_IMMUTABLE;
225 else
226 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
227 if (flags & FS_APPEND_FL)
228 ip->flags |= BTRFS_INODE_APPEND;
229 else
230 ip->flags &= ~BTRFS_INODE_APPEND;
231 if (flags & FS_NODUMP_FL)
232 ip->flags |= BTRFS_INODE_NODUMP;
233 else
234 ip->flags &= ~BTRFS_INODE_NODUMP;
235 if (flags & FS_NOATIME_FL)
236 ip->flags |= BTRFS_INODE_NOATIME;
237 else
238 ip->flags &= ~BTRFS_INODE_NOATIME;
239 if (flags & FS_DIRSYNC_FL)
240 ip->flags |= BTRFS_INODE_DIRSYNC;
241 else
242 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600243 if (flags & FS_NOCOW_FL) {
244 if (S_ISREG(mode)) {
245 /*
246 * It's safe to turn csums off here, no extents exist.
247 * Otherwise we want the flag to reflect the real COW
248 * status of the file and will not set it.
249 */
250 if (inode->i_size == 0)
251 ip->flags |= BTRFS_INODE_NODATACOW
252 | BTRFS_INODE_NODATASUM;
253 } else {
254 ip->flags |= BTRFS_INODE_NODATACOW;
255 }
256 } else {
257 /*
258 * Revert back under same assuptions as above
259 */
260 if (S_ISREG(mode)) {
261 if (inode->i_size == 0)
262 ip->flags &= ~(BTRFS_INODE_NODATACOW
263 | BTRFS_INODE_NODATASUM);
264 } else {
265 ip->flags &= ~BTRFS_INODE_NODATACOW;
266 }
267 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200268
Liu Bo75e7cb72011-03-22 10:12:20 +0000269 /*
270 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
271 * flag may be changed automatically if compression code won't make
272 * things smaller.
273 */
274 if (flags & FS_NOCOMP_FL) {
275 ip->flags &= ~BTRFS_INODE_COMPRESS;
276 ip->flags |= BTRFS_INODE_NOCOMPRESS;
277 } else if (flags & FS_COMPR_FL) {
278 ip->flags |= BTRFS_INODE_COMPRESS;
279 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000280 } else {
281 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000282 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200283
Li Zefan4da6f1a2011-12-29 13:39:50 +0800284 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800285 if (IS_ERR(trans)) {
286 ret = PTR_ERR(trans);
287 goto out_drop;
288 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200289
Li Zefan306424cc2011-12-14 20:12:02 -0500290 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400291 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500292 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200293 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200294
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200295 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800296 out_drop:
297 if (ret) {
298 ip->flags = ip_oldflags;
299 inode->i_flags = i_oldflags;
300 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200302 out_unlock:
303 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200304 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000305 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200306}
307
308static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
309{
310 struct inode *inode = file->f_path.dentry->d_inode;
311
312 return put_user(inode->i_generation, arg);
313}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400314
Li Dongyangf7039b12011-03-24 10:24:28 +0000315static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
316{
Al Viro815745c2011-11-17 15:40:49 -0500317 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000318 struct btrfs_device *device;
319 struct request_queue *q;
320 struct fstrim_range range;
321 u64 minlen = ULLONG_MAX;
322 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500323 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000324 int ret;
325
326 if (!capable(CAP_SYS_ADMIN))
327 return -EPERM;
328
Xiao Guangrong1f781602011-04-20 10:09:16 +0000329 rcu_read_lock();
330 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
331 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000332 if (!device->bdev)
333 continue;
334 q = bdev_get_queue(device->bdev);
335 if (blk_queue_discard(q)) {
336 num_devices++;
337 minlen = min((u64)q->limits.discard_granularity,
338 minlen);
339 }
340 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000341 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200342
Li Dongyangf7039b12011-03-24 10:24:28 +0000343 if (!num_devices)
344 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000345 if (copy_from_user(&range, arg, sizeof(range)))
346 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000347 if (range.start > total_bytes ||
348 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200349 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000350
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200351 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000352 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500353 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000354 if (ret < 0)
355 return ret;
356
357 if (copy_to_user(arg, &range, sizeof(range)))
358 return -EFAULT;
359
360 return 0;
361}
362
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400363static noinline int create_subvol(struct btrfs_root *root,
364 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400365 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200366 u64 *async_transid,
367 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400368{
369 struct btrfs_trans_handle *trans;
370 struct btrfs_key key;
371 struct btrfs_root_item root_item;
372 struct btrfs_inode_item *inode_item;
373 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400374 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400375 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000376 struct inode *dir;
Alexander Block8ea05e32012-07-25 17:35:53 +0200377 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400378 int ret;
379 int err;
380 u64 objectid;
381 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500382 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200383 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400384
Li Zefan581bb052011-04-20 10:06:11 +0800385 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400386 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400387 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000388
389 dir = parent->d_inode;
390
Josef Bacik9ed74f22009-09-11 16:12:44 -0400391 /*
392 * 1 - inode item
393 * 2 - refs
394 * 1 - root item
395 * 2 - dir items
396 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400397 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400398 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400399 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400400
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200401 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
402 inherit ? *inherit : NULL);
403 if (ret)
404 goto fail;
405
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400406 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200407 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400408 if (IS_ERR(leaf)) {
409 ret = PTR_ERR(leaf);
410 goto fail;
411 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400412
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400413 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414 btrfs_set_header_bytenr(leaf, leaf->start);
415 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400416 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400417 btrfs_set_header_owner(leaf, objectid);
418
419 write_extent_buffer(leaf, root->fs_info->fsid,
420 (unsigned long)btrfs_header_fsid(leaf),
421 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400422 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
423 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
424 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400425 btrfs_mark_buffer_dirty(leaf);
426
Alexander Block8ea05e32012-07-25 17:35:53 +0200427 memset(&root_item, 0, sizeof(root_item));
428
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400429 inode_item = &root_item.inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400430 inode_item->generation = cpu_to_le64(1);
431 inode_item->size = cpu_to_le64(3);
432 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400433 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400434 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
435
Li Zefan08fe4db2011-03-28 02:01:25 +0000436 root_item.flags = 0;
437 root_item.byte_limit = 0;
438 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
439
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400440 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400441 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442 btrfs_set_root_level(&root_item, 0);
443 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000444 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400445 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400446
Alexander Block8ea05e32012-07-25 17:35:53 +0200447 btrfs_set_root_generation_v2(&root_item,
448 btrfs_root_generation(&root_item));
449 uuid_le_gen(&new_uuid);
450 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
451 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600452 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200453 root_item.ctime = root_item.otime;
454 btrfs_set_root_ctransid(&root_item, trans->transid);
455 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400456
Chris Mason925baed2008-06-25 16:01:30 -0400457 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400458 free_extent_buffer(leaf);
459 leaf = NULL;
460
461 btrfs_set_root_dirid(&root_item, new_dirid);
462
463 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400464 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400465 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
466 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
467 &root_item);
468 if (ret)
469 goto fail;
470
Yan, Zheng76dda932009-09-21 16:00:26 -0400471 key.offset = (u64)-1;
472 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100473 if (IS_ERR(new_root)) {
474 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
475 ret = PTR_ERR(new_root);
476 goto fail;
477 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400478
479 btrfs_record_root_in_trans(trans, new_root);
480
Josef Bacikd82a6f12011-05-11 15:26:06 -0400481 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700482 if (ret) {
483 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100484 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700485 goto fail;
486 }
487
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400488 /*
489 * insert the directory item
490 */
Chris Mason3de45862008-11-17 21:02:50 -0500491 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100492 if (ret) {
493 btrfs_abort_transaction(trans, root, ret);
494 goto fail;
495 }
Chris Mason3de45862008-11-17 21:02:50 -0500496
497 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800498 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500499 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100500 if (ret) {
501 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400502 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100503 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500504
Yan Zheng52c26172009-01-05 15:43:43 -0500505 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
506 ret = btrfs_update_inode(trans, root, dir);
507 BUG_ON(ret);
508
Chris Mason0660b5a2008-11-17 20:37:39 -0500509 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400510 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800511 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400512
Chris Mason0660b5a2008-11-17 20:37:39 -0500513 BUG_ON(ret);
514
Yan, Zheng76dda932009-09-21 16:00:26 -0400515 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400516fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400517 if (async_transid) {
518 *async_transid = trans->transid;
519 err = btrfs_commit_transaction_async(trans, root, 1);
520 } else {
521 err = btrfs_commit_transaction(trans, root);
522 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400523 if (err && !ret)
524 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525 return ret;
526}
527
Sage Weil72fd0322010-10-29 15:41:32 -0400528static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800529 char *name, int namelen, u64 *async_transid,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200530 bool readonly, struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400531{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000532 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400533 struct btrfs_pending_snapshot *pending_snapshot;
534 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000535 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400536
537 if (!root->ref_cows)
538 return -EINVAL;
539
Yan, Zhenga22285a2010-05-16 10:48:46 -0400540 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
541 if (!pending_snapshot)
542 return -ENOMEM;
543
Miao Xie66d8f3d2012-09-06 04:02:28 -0600544 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
545 BTRFS_BLOCK_RSV_TEMP);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400546 pending_snapshot->dentry = dentry;
547 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800548 pending_snapshot->readonly = readonly;
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200549 if (inherit) {
550 pending_snapshot->inherit = *inherit;
551 *inherit = NULL; /* take responsibility to free it */
552 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400553
Miao Xie48c03c42012-09-06 04:03:56 -0600554 trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400555 if (IS_ERR(trans)) {
556 ret = PTR_ERR(trans);
557 goto fail;
558 }
559
560 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
561 BUG_ON(ret);
562
Josef Bacik83515832011-06-14 15:16:14 -0400563 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400564 list_add(&pending_snapshot->list,
565 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400566 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400567 if (async_transid) {
568 *async_transid = trans->transid;
569 ret = btrfs_commit_transaction_async(trans,
570 root->fs_info->extent_root, 1);
571 } else {
572 ret = btrfs_commit_transaction(trans,
573 root->fs_info->extent_root);
574 }
Liu Bo109f2362012-11-05 12:42:09 +0000575 if (ret) {
576 /* cleanup_transaction has freed this for us */
577 if (trans->aborted)
578 pending_snapshot = NULL;
Josef Bacikc37b2b62012-10-22 15:51:44 -0400579 goto fail;
Liu Bo109f2362012-11-05 12:42:09 +0000580 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400581
582 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400583 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000584 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400585
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500586 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
587 if (ret)
588 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400589
Al Viro2fbe8c82011-07-16 21:38:06 -0400590 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000591 if (IS_ERR(inode)) {
592 ret = PTR_ERR(inode);
593 goto fail;
594 }
595 BUG_ON(!inode);
596 d_instantiate(dentry, inode);
597 ret = 0;
598fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400599 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400600 return ret;
601}
602
Sage Weil4260f7c2010-10-29 15:46:43 -0400603/* copy of check_sticky in fs/namei.c()
604* It's inline, so penalty for filesystems that don't use sticky bit is
605* minimal.
606*/
607static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
608{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800609 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400610
611 if (!(dir->i_mode & S_ISVTX))
612 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800613 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400614 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800615 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400616 return 0;
617 return !capable(CAP_FOWNER);
618}
619
620/* copy of may_delete in fs/namei.c()
621 * Check whether we can remove a link victim from directory dir, check
622 * whether the type of victim is right.
623 * 1. We can't do it if dir is read-only (done in permission())
624 * 2. We should have write and exec permissions on dir
625 * 3. We can't remove anything from append-only dir
626 * 4. We can't do anything with immutable dir (done in permission())
627 * 5. If the sticky bit on dir is set we should either
628 * a. be owner of dir, or
629 * b. be owner of victim, or
630 * c. have CAP_FOWNER capability
631 * 6. If the victim is append-only or immutable we can't do antyhing with
632 * links pointing to it.
633 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
634 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
635 * 9. We can't remove a root or mountpoint.
636 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
637 * nfs_async_unlink().
638 */
639
640static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
641{
642 int error;
643
644 if (!victim->d_inode)
645 return -ENOENT;
646
647 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400648 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400649
650 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
651 if (error)
652 return error;
653 if (IS_APPEND(dir))
654 return -EPERM;
655 if (btrfs_check_sticky(dir, victim->d_inode)||
656 IS_APPEND(victim->d_inode)||
657 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
658 return -EPERM;
659 if (isdir) {
660 if (!S_ISDIR(victim->d_inode->i_mode))
661 return -ENOTDIR;
662 if (IS_ROOT(victim))
663 return -EBUSY;
664 } else if (S_ISDIR(victim->d_inode->i_mode))
665 return -EISDIR;
666 if (IS_DEADDIR(dir))
667 return -ENOENT;
668 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
669 return -EBUSY;
670 return 0;
671}
672
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400673/* copy of may_create in fs/namei.c() */
674static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
675{
676 if (child->d_inode)
677 return -EEXIST;
678 if (IS_DEADDIR(dir))
679 return -ENOENT;
680 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
681}
682
683/*
684 * Create a new subvolume below @parent. This is largely modeled after
685 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
686 * inside this filesystem so it's quite a bit simpler.
687 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400688static noinline int btrfs_mksubvol(struct path *parent,
689 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400690 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200691 u64 *async_transid, bool readonly,
692 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400693{
Yan, Zheng76dda932009-09-21 16:00:26 -0400694 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400695 struct dentry *dentry;
696 int error;
697
Yan, Zheng76dda932009-09-21 16:00:26 -0400698 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400699
700 dentry = lookup_one_len(name, parent->dentry, namelen);
701 error = PTR_ERR(dentry);
702 if (IS_ERR(dentry))
703 goto out_unlock;
704
705 error = -EEXIST;
706 if (dentry->d_inode)
707 goto out_dput;
708
Yan, Zheng76dda932009-09-21 16:00:26 -0400709 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400710 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600711 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400712
Yan, Zheng76dda932009-09-21 16:00:26 -0400713 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
714
715 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
716 goto out_up_read;
717
Chris Mason3de45862008-11-17 21:02:50 -0500718 if (snap_src) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200719 error = create_snapshot(snap_src, dentry, name, namelen,
720 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500721 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400722 error = create_subvol(BTRFS_I(dir)->root, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200723 name, namelen, async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500724 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400725 if (!error)
726 fsnotify_mkdir(dir, dentry);
727out_up_read:
728 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400729out_dput:
730 dput(dentry);
731out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400732 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400733 return error;
734}
735
Chris Mason4cb53002011-05-24 15:35:30 -0400736/*
737 * When we're defragging a range, we don't want to kick it off again
738 * if it is really just waiting for delalloc to send it down.
739 * If we find a nice big extent or delalloc range for the bytes in the
740 * file you want to defrag, we return 0 to let you know to skip this
741 * part of the file
742 */
743static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
744{
745 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
746 struct extent_map *em = NULL;
747 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
748 u64 end;
749
750 read_lock(&em_tree->lock);
751 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
752 read_unlock(&em_tree->lock);
753
754 if (em) {
755 end = extent_map_end(em);
756 free_extent_map(em);
757 if (end - offset > thresh)
758 return 0;
759 }
760 /* if we already have a nice delalloc here, just stop */
761 thresh /= 2;
762 end = count_range_bits(io_tree, &offset, offset + thresh,
763 thresh, EXTENT_DELALLOC, 1);
764 if (end >= thresh)
765 return 0;
766 return 1;
767}
768
769/*
770 * helper function to walk through a file and find extents
771 * newer than a specific transid, and smaller than thresh.
772 *
773 * This is used by the defragging code to find new and small
774 * extents
775 */
776static int find_new_extents(struct btrfs_root *root,
777 struct inode *inode, u64 newer_than,
778 u64 *off, int thresh)
779{
780 struct btrfs_path *path;
781 struct btrfs_key min_key;
782 struct btrfs_key max_key;
783 struct extent_buffer *leaf;
784 struct btrfs_file_extent_item *extent;
785 int type;
786 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000787 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400788
789 path = btrfs_alloc_path();
790 if (!path)
791 return -ENOMEM;
792
David Sterbaa4689d22011-05-31 17:08:14 +0000793 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400794 min_key.type = BTRFS_EXTENT_DATA_KEY;
795 min_key.offset = *off;
796
David Sterbaa4689d22011-05-31 17:08:14 +0000797 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400798 max_key.type = (u8)-1;
799 max_key.offset = (u64)-1;
800
801 path->keep_locks = 1;
802
803 while(1) {
804 ret = btrfs_search_forward(root, &min_key, &max_key,
805 path, 0, newer_than);
806 if (ret != 0)
807 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000808 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400809 goto none;
810 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
811 goto none;
812
813 leaf = path->nodes[0];
814 extent = btrfs_item_ptr(leaf, path->slots[0],
815 struct btrfs_file_extent_item);
816
817 type = btrfs_file_extent_type(leaf, extent);
818 if (type == BTRFS_FILE_EXTENT_REG &&
819 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
820 check_defrag_in_cache(inode, min_key.offset, thresh)) {
821 *off = min_key.offset;
822 btrfs_free_path(path);
823 return 0;
824 }
825
826 if (min_key.offset == (u64)-1)
827 goto none;
828
829 min_key.offset++;
830 btrfs_release_path(path);
831 }
832none:
833 btrfs_free_path(path);
834 return -ENOENT;
835}
836
Li Zefan6c282eb2012-06-11 16:03:35 +0800837static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400838{
839 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500840 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800841 struct extent_map *em;
842 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500843
844 /*
845 * hopefully we have this extent in the tree already, try without
846 * the full extent lock
847 */
848 read_lock(&em_tree->lock);
849 em = lookup_extent_mapping(em_tree, start, len);
850 read_unlock(&em_tree->lock);
851
852 if (!em) {
853 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100854 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500855 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100856 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500857
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000858 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800859 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500860 }
861
Li Zefan6c282eb2012-06-11 16:03:35 +0800862 return em;
863}
864
865static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
866{
867 struct extent_map *next;
868 bool ret = true;
869
870 /* this is the last extent */
871 if (em->start + em->len >= i_size_read(inode))
872 return false;
873
874 next = defrag_lookup_extent(inode, em->start + em->len);
875 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
876 ret = false;
877
878 free_extent_map(next);
879 return ret;
880}
881
882static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400883 u64 *last_len, u64 *skip, u64 *defrag_end,
884 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800885{
886 struct extent_map *em;
887 int ret = 1;
888 bool next_mergeable = true;
889
890 /*
891 * make sure that once we start defragging an extent, we keep on
892 * defragging it
893 */
894 if (start < *defrag_end)
895 return 1;
896
897 *skip = 0;
898
899 em = defrag_lookup_extent(inode, start);
900 if (!em)
901 return 0;
902
Chris Mason940100a2010-03-10 10:52:59 -0500903 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400904 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500905 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400906 goto out;
907 }
908
Li Zefan6c282eb2012-06-11 16:03:35 +0800909 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500910
911 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800912 * we hit a real extent, if it is big or the next extent is not a
913 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500914 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400915 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800916 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500917 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400918out:
Chris Mason940100a2010-03-10 10:52:59 -0500919 /*
920 * last_len ends up being a counter of how many bytes we've defragged.
921 * every time we choose not to defrag an extent, we reset *last_len
922 * so that the next tiny extent will force a defrag.
923 *
924 * The end result of this is that tiny extents before a single big
925 * extent will force at least part of that big extent to be defragged.
926 */
927 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500928 *defrag_end = extent_map_end(em);
929 } else {
930 *last_len = 0;
931 *skip = extent_map_end(em);
932 *defrag_end = 0;
933 }
934
935 free_extent_map(em);
936 return ret;
937}
938
Chris Mason4cb53002011-05-24 15:35:30 -0400939/*
940 * it doesn't do much good to defrag one or two pages
941 * at a time. This pulls in a nice chunk of pages
942 * to COW and defrag.
943 *
944 * It also makes sure the delalloc code has enough
945 * dirty data to avoid making new small extents as part
946 * of the defrag
947 *
948 * It's a good idea to start RA on this range
949 * before calling this.
950 */
951static int cluster_pages_for_defrag(struct inode *inode,
952 struct page **pages,
953 unsigned long start_index,
954 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400955{
Chris Mason4cb53002011-05-24 15:35:30 -0400956 unsigned long file_end;
957 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400958 u64 page_start;
959 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400960 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400961 int ret;
962 int i;
963 int i_done;
964 struct btrfs_ordered_extent *ordered;
965 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800966 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400967 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400968
Chris Mason4cb53002011-05-24 15:35:30 -0400969 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400970 if (!isize || start_index > file_end)
971 return 0;
972
973 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400974
975 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400976 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400977 if (ret)
978 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400979 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800980 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400981
982 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400983 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400984 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800985again:
Josef Bacika94733d2011-07-11 10:47:06 -0400986 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800987 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400988 if (!page)
989 break;
990
Miao Xie600a45e2012-02-16 15:01:24 +0800991 page_start = page_offset(page);
992 page_end = page_start + PAGE_CACHE_SIZE - 1;
993 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100994 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800995 ordered = btrfs_lookup_ordered_extent(inode,
996 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100997 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800998 if (!ordered)
999 break;
1000
1001 unlock_page(page);
1002 btrfs_start_ordered_extent(inode, ordered, 1);
1003 btrfs_put_ordered_extent(ordered);
1004 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001005 /*
1006 * we unlocked the page above, so we need check if
1007 * it was released or not.
1008 */
1009 if (page->mapping != inode->i_mapping) {
1010 unlock_page(page);
1011 page_cache_release(page);
1012 goto again;
1013 }
Miao Xie600a45e2012-02-16 15:01:24 +08001014 }
1015
Chris Mason4cb53002011-05-24 15:35:30 -04001016 if (!PageUptodate(page)) {
1017 btrfs_readpage(NULL, page);
1018 lock_page(page);
1019 if (!PageUptodate(page)) {
1020 unlock_page(page);
1021 page_cache_release(page);
1022 ret = -EIO;
1023 break;
1024 }
1025 }
Miao Xie600a45e2012-02-16 15:01:24 +08001026
Miao Xie600a45e2012-02-16 15:01:24 +08001027 if (page->mapping != inode->i_mapping) {
1028 unlock_page(page);
1029 page_cache_release(page);
1030 goto again;
1031 }
1032
Chris Mason4cb53002011-05-24 15:35:30 -04001033 pages[i] = page;
1034 i_done++;
1035 }
1036 if (!i_done || ret)
1037 goto out;
1038
1039 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1040 goto out;
1041
1042 /*
1043 * so now we have a nice long stream of locked
1044 * and up to date pages, lets wait on them
1045 */
1046 for (i = 0; i < i_done; i++)
1047 wait_on_page_writeback(pages[i]);
1048
1049 page_start = page_offset(pages[0]);
1050 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1051
1052 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001053 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001054 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1055 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001056 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1057 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001058
Liu Bo1f12bd02012-03-29 09:57:44 -04001059 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001060 spin_lock(&BTRFS_I(inode)->lock);
1061 BTRFS_I(inode)->outstanding_extents++;
1062 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001063 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001064 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001065 }
1066
1067
Liu Bo9e8a4a82012-09-05 19:10:51 -06001068 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1069 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001070
1071 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1072 page_start, page_end - 1, &cached_state,
1073 GFP_NOFS);
1074
1075 for (i = 0; i < i_done; i++) {
1076 clear_page_dirty_for_io(pages[i]);
1077 ClearPageChecked(pages[i]);
1078 set_page_extent_mapped(pages[i]);
1079 set_page_dirty(pages[i]);
1080 unlock_page(pages[i]);
1081 page_cache_release(pages[i]);
1082 }
1083 return i_done;
1084out:
1085 for (i = 0; i < i_done; i++) {
1086 unlock_page(pages[i]);
1087 page_cache_release(pages[i]);
1088 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001089 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001090 return ret;
1091
1092}
1093
1094int btrfs_defrag_file(struct inode *inode, struct file *file,
1095 struct btrfs_ioctl_defrag_range_args *range,
1096 u64 newer_than, unsigned long max_to_defrag)
1097{
1098 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001099 struct file_ra_state *ra = NULL;
1100 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001101 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001102 u64 last_len = 0;
1103 u64 skip = 0;
1104 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001105 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001106 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001107 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001108 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001109 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001110 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001111 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001112 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1113 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001114 u64 new_align = ~((u64)128 * 1024 - 1);
1115 struct page **pages = NULL;
1116
1117 if (extent_thresh == 0)
1118 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001119
1120 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1121 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1122 return -EINVAL;
1123 if (range->compress_type)
1124 compress_type = range->compress_type;
1125 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001126
Li Zefan151a31b2011-09-02 15:56:39 +08001127 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001128 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001129
Chris Mason4cb53002011-05-24 15:35:30 -04001130 /*
1131 * if we were not given a file, allocate a readahead
1132 * context
1133 */
1134 if (!file) {
1135 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1136 if (!ra)
1137 return -ENOMEM;
1138 file_ra_state_init(ra, inode->i_mapping);
1139 } else {
1140 ra = &file->f_ra;
1141 }
1142
Li Zefan008873e2011-09-02 15:57:07 +08001143 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001144 GFP_NOFS);
1145 if (!pages) {
1146 ret = -ENOMEM;
1147 goto out_ra;
1148 }
1149
1150 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001151 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001152 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001153 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1154 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001155 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001156 }
1157
Chris Mason4cb53002011-05-24 15:35:30 -04001158 if (newer_than) {
1159 ret = find_new_extents(root, inode, newer_than,
1160 &newer_off, 64 * 1024);
1161 if (!ret) {
1162 range->start = newer_off;
1163 /*
1164 * we always align our defrag to help keep
1165 * the extents in the file evenly spaced
1166 */
1167 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001168 } else
1169 goto out_ra;
1170 } else {
1171 i = range->start >> PAGE_CACHE_SHIFT;
1172 }
1173 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001174 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001175
Li Zefan2a0f7f52011-10-10 15:43:34 -04001176 /*
1177 * make writeback starts from i, so the defrag range can be
1178 * written sequentially.
1179 */
1180 if (i < inode->i_mapping->writeback_index)
1181 inode->i_mapping->writeback_index = i;
1182
Chris Masonf7f43cc2011-10-11 11:41:40 -04001183 while (i <= last_index && defrag_count < max_to_defrag &&
1184 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1185 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001186 /*
1187 * make sure we stop running if someone unmounts
1188 * the FS
1189 */
1190 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1191 break;
1192
Liu Bo66c26892012-03-29 09:57:45 -04001193 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001194 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001195 &defrag_end, range->flags &
1196 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001197 unsigned long next;
1198 /*
1199 * the should_defrag function tells us how much to skip
1200 * bump our counter by the suggested amount
1201 */
1202 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1203 i = max(i + 1, next);
1204 continue;
1205 }
Li Zefan008873e2011-09-02 15:57:07 +08001206
1207 if (!newer_than) {
1208 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1209 PAGE_CACHE_SHIFT) - i;
1210 cluster = min(cluster, max_cluster);
1211 } else {
1212 cluster = max_cluster;
1213 }
1214
Chris Mason1e701a32010-03-11 09:42:04 -05001215 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001216 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001217
Li Zefan008873e2011-09-02 15:57:07 +08001218 if (i + cluster > ra_index) {
1219 ra_index = max(i, ra_index);
1220 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1221 cluster);
1222 ra_index += max_cluster;
1223 }
Chris Mason940100a2010-03-10 10:52:59 -05001224
Liu Boecb8bea2012-03-29 09:57:44 -04001225 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001226 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001227 if (ret < 0) {
1228 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001229 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001230 }
Chris Mason940100a2010-03-10 10:52:59 -05001231
Chris Mason4cb53002011-05-24 15:35:30 -04001232 defrag_count += ret;
1233 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001234 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001235
1236 if (newer_than) {
1237 if (newer_off == (u64)-1)
1238 break;
1239
Liu Boe1f041e2012-03-29 09:57:45 -04001240 if (ret > 0)
1241 i += ret;
1242
Chris Mason4cb53002011-05-24 15:35:30 -04001243 newer_off = max(newer_off + 1,
1244 (u64)i << PAGE_CACHE_SHIFT);
1245
1246 ret = find_new_extents(root, inode,
1247 newer_than, &newer_off,
1248 64 * 1024);
1249 if (!ret) {
1250 range->start = newer_off;
1251 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001252 } else {
1253 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001254 }
Chris Mason4cb53002011-05-24 15:35:30 -04001255 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001256 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001257 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001258 last_len += ret << PAGE_CACHE_SHIFT;
1259 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001260 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001261 last_len = 0;
1262 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001263 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001264 }
1265
Chris Mason1e701a32010-03-11 09:42:04 -05001266 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1267 filemap_flush(inode->i_mapping);
1268
1269 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1270 /* the filemap_flush will queue IO into the worker threads, but
1271 * we have to make sure the IO is actually started and that
1272 * ordered extents get created before we return
1273 */
1274 atomic_inc(&root->fs_info->async_submit_draining);
1275 while (atomic_read(&root->fs_info->nr_async_submits) ||
1276 atomic_read(&root->fs_info->async_delalloc_pages)) {
1277 wait_event(root->fs_info->async_submit_wait,
1278 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1279 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1280 }
1281 atomic_dec(&root->fs_info->async_submit_draining);
1282
1283 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001284 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001285 mutex_unlock(&inode->i_mutex);
1286 }
1287
Li Zefan1a419d82010-10-25 15:12:50 +08001288 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001289 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001290 }
1291
Diego Calleja60ccf822011-09-01 16:33:57 +02001292 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001293
Chris Mason4cb53002011-05-24 15:35:30 -04001294out_ra:
1295 if (!file)
1296 kfree(ra);
1297 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001298 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001299}
1300
Yan, Zheng76dda932009-09-21 16:00:26 -04001301static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1302 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001303{
1304 u64 new_size;
1305 u64 old_size;
1306 u64 devid = 1;
1307 struct btrfs_ioctl_vol_args *vol_args;
1308 struct btrfs_trans_handle *trans;
1309 struct btrfs_device *device = NULL;
1310 char *sizestr;
1311 char *devstr = NULL;
1312 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001313 int mod = 0;
1314
Yan Zhengc146afa2008-11-12 14:34:12 -05001315 if (root->fs_info->sb->s_flags & MS_RDONLY)
1316 return -EROFS;
1317
Chris Masone441d542009-01-05 16:57:23 -05001318 if (!capable(CAP_SYS_ADMIN))
1319 return -EPERM;
1320
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001321 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1322 1)) {
1323 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1324 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001325 }
1326
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001327 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001328 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001329 if (IS_ERR(vol_args)) {
1330 ret = PTR_ERR(vol_args);
1331 goto out;
1332 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001333
1334 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001335
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001336 sizestr = vol_args->name;
1337 devstr = strchr(sizestr, ':');
1338 if (devstr) {
1339 char *end;
1340 sizestr = devstr + 1;
1341 *devstr = '\0';
1342 devstr = vol_args->name;
1343 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001344 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001345 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001346 }
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001347 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001348 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001349 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001350 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001351 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001352 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001353 }
Liu Bo4e42ae12012-06-14 02:23:19 -06001354 if (device->fs_devices && device->fs_devices->seeding) {
1355 printk(KERN_INFO "btrfs: resizer unable to apply on "
Chris Masona8c4a332012-06-15 20:07:17 -04001356 "seeding device %llu\n",
1357 (unsigned long long)devid);
Liu Bo4e42ae12012-06-14 02:23:19 -06001358 ret = -EINVAL;
1359 goto out_free;
1360 }
1361
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001362 if (!strcmp(sizestr, "max"))
1363 new_size = device->bdev->bd_inode->i_size;
1364 else {
1365 if (sizestr[0] == '-') {
1366 mod = -1;
1367 sizestr++;
1368 } else if (sizestr[0] == '+') {
1369 mod = 1;
1370 sizestr++;
1371 }
Akinobu Mita91748462010-02-28 10:59:11 +00001372 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001373 if (new_size == 0) {
1374 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001375 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001376 }
1377 }
1378
Stefan Behrens63a212a2012-11-05 18:29:28 +01001379 if (device->is_tgtdev_for_dev_replace) {
1380 ret = -EINVAL;
1381 goto out_free;
1382 }
1383
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001384 old_size = device->total_bytes;
1385
1386 if (mod < 0) {
1387 if (new_size > old_size) {
1388 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001389 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001390 }
1391 new_size = old_size - new_size;
1392 } else if (mod > 0) {
1393 new_size = old_size + new_size;
1394 }
1395
1396 if (new_size < 256 * 1024 * 1024) {
1397 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001398 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001399 }
1400 if (new_size > device->bdev->bd_inode->i_size) {
1401 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001402 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001403 }
1404
1405 do_div(new_size, root->sectorsize);
1406 new_size *= root->sectorsize;
1407
Josef Bacik606686e2012-06-04 14:03:51 -04001408 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1409 rcu_str_deref(device->name),
1410 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001411
1412 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001413 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001414 if (IS_ERR(trans)) {
1415 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001416 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001417 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001418 ret = btrfs_grow_device(trans, device, new_size);
1419 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001420 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001421 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001422 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001423
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001424out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001425 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001426out:
1427 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001428 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001429 return ret;
1430}
1431
Sage Weil72fd0322010-10-29 15:41:32 -04001432static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001433 char *name, unsigned long fd, int subvol,
1434 u64 *transid, bool readonly,
1435 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001436{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001437 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001438 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001439
Liu Boa874a632012-06-29 03:58:46 -06001440 ret = mnt_want_write_file(file);
1441 if (ret)
1442 goto out;
1443
Sage Weil72fd0322010-10-29 15:41:32 -04001444 namelen = strlen(name);
1445 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001446 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001447 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001448 }
1449
Chris Mason16780ca2012-02-20 22:14:55 -05001450 if (name[0] == '.' &&
1451 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1452 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001453 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001454 }
1455
Chris Mason3de45862008-11-17 21:02:50 -05001456 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001457 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001458 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001459 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001460 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001461 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001462 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001463 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001464 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001465 }
1466
Al Viro2903ff02012-08-28 12:52:22 -04001467 src_inode = src.file->f_path.dentry->d_inode;
Chris Mason3de45862008-11-17 21:02:50 -05001468 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001469 printk(KERN_INFO "btrfs: Snapshot src from "
1470 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001471 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001472 } else {
1473 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1474 BTRFS_I(src_inode)->root,
1475 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001476 }
Al Viro2903ff02012-08-28 12:52:22 -04001477 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001478 }
Liu Boa874a632012-06-29 03:58:46 -06001479out_drop_write:
1480 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001481out:
Sage Weil72fd0322010-10-29 15:41:32 -04001482 return ret;
1483}
1484
1485static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001486 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001487{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001488 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001489 int ret;
1490
Li Zefanfa0d2b92010-12-20 15:53:28 +08001491 vol_args = memdup_user(arg, sizeof(*vol_args));
1492 if (IS_ERR(vol_args))
1493 return PTR_ERR(vol_args);
1494 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001495
Li Zefanfa0d2b92010-12-20 15:53:28 +08001496 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001497 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001498 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001499
Li Zefanfa0d2b92010-12-20 15:53:28 +08001500 kfree(vol_args);
1501 return ret;
1502}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001503
Li Zefanfa0d2b92010-12-20 15:53:28 +08001504static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1505 void __user *arg, int subvol)
1506{
1507 struct btrfs_ioctl_vol_args_v2 *vol_args;
1508 int ret;
1509 u64 transid = 0;
1510 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001511 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001512 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001513
Li Zefanfa0d2b92010-12-20 15:53:28 +08001514 vol_args = memdup_user(arg, sizeof(*vol_args));
1515 if (IS_ERR(vol_args))
1516 return PTR_ERR(vol_args);
1517 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001518
Li Zefanb83cc962010-12-20 16:04:08 +08001519 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001520 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1521 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001522 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001523 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001524 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001525
1526 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1527 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001528 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1529 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001530 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1531 if (vol_args->size > PAGE_CACHE_SIZE) {
1532 ret = -EINVAL;
1533 goto out;
1534 }
1535 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1536 if (IS_ERR(inherit)) {
1537 ret = PTR_ERR(inherit);
1538 goto out;
1539 }
1540 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001541
1542 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001543 vol_args->fd, subvol, ptr,
1544 readonly, &inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001545
1546 if (ret == 0 && ptr &&
1547 copy_to_user(arg +
1548 offsetof(struct btrfs_ioctl_vol_args_v2,
1549 transid), ptr, sizeof(*ptr)))
1550 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001551out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001552 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001553 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001554 return ret;
1555}
1556
Li Zefan0caa1022010-12-20 16:30:25 +08001557static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1558 void __user *arg)
1559{
1560 struct inode *inode = fdentry(file)->d_inode;
1561 struct btrfs_root *root = BTRFS_I(inode)->root;
1562 int ret = 0;
1563 u64 flags = 0;
1564
Li Zefan33345d012011-04-20 10:31:50 +08001565 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001566 return -EINVAL;
1567
1568 down_read(&root->fs_info->subvol_sem);
1569 if (btrfs_root_readonly(root))
1570 flags |= BTRFS_SUBVOL_RDONLY;
1571 up_read(&root->fs_info->subvol_sem);
1572
1573 if (copy_to_user(arg, &flags, sizeof(flags)))
1574 ret = -EFAULT;
1575
1576 return ret;
1577}
1578
1579static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1580 void __user *arg)
1581{
1582 struct inode *inode = fdentry(file)->d_inode;
1583 struct btrfs_root *root = BTRFS_I(inode)->root;
1584 struct btrfs_trans_handle *trans;
1585 u64 root_flags;
1586 u64 flags;
1587 int ret = 0;
1588
Liu Bob9ca0662012-06-29 03:58:49 -06001589 ret = mnt_want_write_file(file);
1590 if (ret)
1591 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001592
Liu Bob9ca0662012-06-29 03:58:49 -06001593 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1594 ret = -EINVAL;
1595 goto out_drop_write;
1596 }
Li Zefan0caa1022010-12-20 16:30:25 +08001597
Liu Bob9ca0662012-06-29 03:58:49 -06001598 if (copy_from_user(&flags, arg, sizeof(flags))) {
1599 ret = -EFAULT;
1600 goto out_drop_write;
1601 }
Li Zefan0caa1022010-12-20 16:30:25 +08001602
Liu Bob9ca0662012-06-29 03:58:49 -06001603 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1604 ret = -EINVAL;
1605 goto out_drop_write;
1606 }
Li Zefan0caa1022010-12-20 16:30:25 +08001607
Liu Bob9ca0662012-06-29 03:58:49 -06001608 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1609 ret = -EOPNOTSUPP;
1610 goto out_drop_write;
1611 }
Li Zefan0caa1022010-12-20 16:30:25 +08001612
Liu Bob9ca0662012-06-29 03:58:49 -06001613 if (!inode_owner_or_capable(inode)) {
1614 ret = -EACCES;
1615 goto out_drop_write;
1616 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001617
Li Zefan0caa1022010-12-20 16:30:25 +08001618 down_write(&root->fs_info->subvol_sem);
1619
1620 /* nothing to do */
1621 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001622 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001623
1624 root_flags = btrfs_root_flags(&root->root_item);
1625 if (flags & BTRFS_SUBVOL_RDONLY)
1626 btrfs_set_root_flags(&root->root_item,
1627 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1628 else
1629 btrfs_set_root_flags(&root->root_item,
1630 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1631
1632 trans = btrfs_start_transaction(root, 1);
1633 if (IS_ERR(trans)) {
1634 ret = PTR_ERR(trans);
1635 goto out_reset;
1636 }
1637
Li Zefanb4dc2b82011-02-16 06:06:34 +00001638 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001639 &root->root_key, &root->root_item);
1640
1641 btrfs_commit_transaction(trans, root);
1642out_reset:
1643 if (ret)
1644 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001645out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001646 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001647out_drop_write:
1648 mnt_drop_write_file(file);
1649out:
Li Zefan0caa1022010-12-20 16:30:25 +08001650 return ret;
1651}
1652
Yan, Zheng76dda932009-09-21 16:00:26 -04001653/*
1654 * helper to check if the subvolume references other subvolumes
1655 */
1656static noinline int may_destroy_subvol(struct btrfs_root *root)
1657{
1658 struct btrfs_path *path;
1659 struct btrfs_key key;
1660 int ret;
1661
1662 path = btrfs_alloc_path();
1663 if (!path)
1664 return -ENOMEM;
1665
1666 key.objectid = root->root_key.objectid;
1667 key.type = BTRFS_ROOT_REF_KEY;
1668 key.offset = (u64)-1;
1669
1670 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1671 &key, path, 0, 0);
1672 if (ret < 0)
1673 goto out;
1674 BUG_ON(ret == 0);
1675
1676 ret = 0;
1677 if (path->slots[0] > 0) {
1678 path->slots[0]--;
1679 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1680 if (key.objectid == root->root_key.objectid &&
1681 key.type == BTRFS_ROOT_REF_KEY)
1682 ret = -ENOTEMPTY;
1683 }
1684out:
1685 btrfs_free_path(path);
1686 return ret;
1687}
1688
Chris Masonac8e9812010-02-28 15:39:26 -05001689static noinline int key_in_sk(struct btrfs_key *key,
1690 struct btrfs_ioctl_search_key *sk)
1691{
Chris Masonabc6e132010-03-18 12:10:08 -04001692 struct btrfs_key test;
1693 int ret;
1694
1695 test.objectid = sk->min_objectid;
1696 test.type = sk->min_type;
1697 test.offset = sk->min_offset;
1698
1699 ret = btrfs_comp_cpu_keys(key, &test);
1700 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001701 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001702
1703 test.objectid = sk->max_objectid;
1704 test.type = sk->max_type;
1705 test.offset = sk->max_offset;
1706
1707 ret = btrfs_comp_cpu_keys(key, &test);
1708 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001709 return 0;
1710 return 1;
1711}
1712
1713static noinline int copy_to_sk(struct btrfs_root *root,
1714 struct btrfs_path *path,
1715 struct btrfs_key *key,
1716 struct btrfs_ioctl_search_key *sk,
1717 char *buf,
1718 unsigned long *sk_offset,
1719 int *num_found)
1720{
1721 u64 found_transid;
1722 struct extent_buffer *leaf;
1723 struct btrfs_ioctl_search_header sh;
1724 unsigned long item_off;
1725 unsigned long item_len;
1726 int nritems;
1727 int i;
1728 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001729 int ret = 0;
1730
1731 leaf = path->nodes[0];
1732 slot = path->slots[0];
1733 nritems = btrfs_header_nritems(leaf);
1734
1735 if (btrfs_header_generation(leaf) > sk->max_transid) {
1736 i = nritems;
1737 goto advance_key;
1738 }
1739 found_transid = btrfs_header_generation(leaf);
1740
1741 for (i = slot; i < nritems; i++) {
1742 item_off = btrfs_item_ptr_offset(leaf, i);
1743 item_len = btrfs_item_size_nr(leaf, i);
1744
1745 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1746 item_len = 0;
1747
1748 if (sizeof(sh) + item_len + *sk_offset >
1749 BTRFS_SEARCH_ARGS_BUFSIZE) {
1750 ret = 1;
1751 goto overflow;
1752 }
1753
1754 btrfs_item_key_to_cpu(leaf, key, i);
1755 if (!key_in_sk(key, sk))
1756 continue;
1757
1758 sh.objectid = key->objectid;
1759 sh.offset = key->offset;
1760 sh.type = key->type;
1761 sh.len = item_len;
1762 sh.transid = found_transid;
1763
1764 /* copy search result header */
1765 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1766 *sk_offset += sizeof(sh);
1767
1768 if (item_len) {
1769 char *p = buf + *sk_offset;
1770 /* copy the item */
1771 read_extent_buffer(leaf, p,
1772 item_off, item_len);
1773 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001774 }
Hugo Millse2156862011-05-14 17:43:41 +00001775 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001776
1777 if (*num_found >= sk->nr_items)
1778 break;
1779 }
1780advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001781 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001782 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1783 key->offset++;
1784 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1785 key->offset = 0;
1786 key->type++;
1787 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1788 key->offset = 0;
1789 key->type = 0;
1790 key->objectid++;
1791 } else
1792 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001793overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001794 return ret;
1795}
1796
1797static noinline int search_ioctl(struct inode *inode,
1798 struct btrfs_ioctl_search_args *args)
1799{
1800 struct btrfs_root *root;
1801 struct btrfs_key key;
1802 struct btrfs_key max_key;
1803 struct btrfs_path *path;
1804 struct btrfs_ioctl_search_key *sk = &args->key;
1805 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1806 int ret;
1807 int num_found = 0;
1808 unsigned long sk_offset = 0;
1809
1810 path = btrfs_alloc_path();
1811 if (!path)
1812 return -ENOMEM;
1813
1814 if (sk->tree_id == 0) {
1815 /* search the root of the inode that was passed */
1816 root = BTRFS_I(inode)->root;
1817 } else {
1818 key.objectid = sk->tree_id;
1819 key.type = BTRFS_ROOT_ITEM_KEY;
1820 key.offset = (u64)-1;
1821 root = btrfs_read_fs_root_no_name(info, &key);
1822 if (IS_ERR(root)) {
1823 printk(KERN_ERR "could not find root %llu\n",
1824 sk->tree_id);
1825 btrfs_free_path(path);
1826 return -ENOENT;
1827 }
1828 }
1829
1830 key.objectid = sk->min_objectid;
1831 key.type = sk->min_type;
1832 key.offset = sk->min_offset;
1833
1834 max_key.objectid = sk->max_objectid;
1835 max_key.type = sk->max_type;
1836 max_key.offset = sk->max_offset;
1837
1838 path->keep_locks = 1;
1839
1840 while(1) {
1841 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1842 sk->min_transid);
1843 if (ret != 0) {
1844 if (ret > 0)
1845 ret = 0;
1846 goto err;
1847 }
1848 ret = copy_to_sk(root, path, &key, sk, args->buf,
1849 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001850 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001851 if (ret || num_found >= sk->nr_items)
1852 break;
1853
1854 }
1855 ret = 0;
1856err:
1857 sk->nr_items = num_found;
1858 btrfs_free_path(path);
1859 return ret;
1860}
1861
1862static noinline int btrfs_ioctl_tree_search(struct file *file,
1863 void __user *argp)
1864{
1865 struct btrfs_ioctl_search_args *args;
1866 struct inode *inode;
1867 int ret;
1868
1869 if (!capable(CAP_SYS_ADMIN))
1870 return -EPERM;
1871
Julia Lawall2354d082010-10-29 15:14:18 -04001872 args = memdup_user(argp, sizeof(*args));
1873 if (IS_ERR(args))
1874 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001875
Chris Masonac8e9812010-02-28 15:39:26 -05001876 inode = fdentry(file)->d_inode;
1877 ret = search_ioctl(inode, args);
1878 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1879 ret = -EFAULT;
1880 kfree(args);
1881 return ret;
1882}
1883
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001884/*
Chris Masonac8e9812010-02-28 15:39:26 -05001885 * Search INODE_REFs to identify path name of 'dirid' directory
1886 * in a 'tree_id' tree. and sets path name to 'name'.
1887 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001888static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1889 u64 tree_id, u64 dirid, char *name)
1890{
1891 struct btrfs_root *root;
1892 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001893 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001894 int ret = -1;
1895 int slot;
1896 int len;
1897 int total_len = 0;
1898 struct btrfs_inode_ref *iref;
1899 struct extent_buffer *l;
1900 struct btrfs_path *path;
1901
1902 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1903 name[0]='\0';
1904 return 0;
1905 }
1906
1907 path = btrfs_alloc_path();
1908 if (!path)
1909 return -ENOMEM;
1910
Chris Masonac8e9812010-02-28 15:39:26 -05001911 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001912
1913 key.objectid = tree_id;
1914 key.type = BTRFS_ROOT_ITEM_KEY;
1915 key.offset = (u64)-1;
1916 root = btrfs_read_fs_root_no_name(info, &key);
1917 if (IS_ERR(root)) {
1918 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001919 ret = -ENOENT;
1920 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001921 }
1922
1923 key.objectid = dirid;
1924 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001925 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001926
1927 while(1) {
1928 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1929 if (ret < 0)
1930 goto out;
1931
1932 l = path->nodes[0];
1933 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001934 if (ret > 0 && slot > 0)
1935 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001936 btrfs_item_key_to_cpu(l, &key, slot);
1937
1938 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001939 key.type != BTRFS_INODE_REF_KEY)) {
1940 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001941 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001942 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001943
1944 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1945 len = btrfs_inode_ref_name_len(l, iref);
1946 ptr -= len + 1;
1947 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001948 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001949 goto out;
1950
1951 *(ptr + len) = '/';
1952 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1953
1954 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1955 break;
1956
David Sterbab3b4aa72011-04-21 01:20:15 +02001957 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001958 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001959 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001960 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001961 }
Chris Masonac8e9812010-02-28 15:39:26 -05001962 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001963 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001964 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001965 name[total_len]='\0';
1966 ret = 0;
1967out:
1968 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001969 return ret;
1970}
1971
1972static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1973 void __user *argp)
1974{
1975 struct btrfs_ioctl_ino_lookup_args *args;
1976 struct inode *inode;
1977 int ret;
1978
1979 if (!capable(CAP_SYS_ADMIN))
1980 return -EPERM;
1981
Julia Lawall2354d082010-10-29 15:14:18 -04001982 args = memdup_user(argp, sizeof(*args));
1983 if (IS_ERR(args))
1984 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001985
Chris Masonac8e9812010-02-28 15:39:26 -05001986 inode = fdentry(file)->d_inode;
1987
Chris Mason1b53ac42010-03-18 12:17:05 -04001988 if (args->treeid == 0)
1989 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1990
Chris Masonac8e9812010-02-28 15:39:26 -05001991 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1992 args->treeid, args->objectid,
1993 args->name);
1994
1995 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1996 ret = -EFAULT;
1997
1998 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001999 return ret;
2000}
2001
Yan, Zheng76dda932009-09-21 16:00:26 -04002002static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2003 void __user *arg)
2004{
2005 struct dentry *parent = fdentry(file);
2006 struct dentry *dentry;
2007 struct inode *dir = parent->d_inode;
2008 struct inode *inode;
2009 struct btrfs_root *root = BTRFS_I(dir)->root;
2010 struct btrfs_root *dest = NULL;
2011 struct btrfs_ioctl_vol_args *vol_args;
2012 struct btrfs_trans_handle *trans;
2013 int namelen;
2014 int ret;
2015 int err = 0;
2016
Yan, Zheng76dda932009-09-21 16:00:26 -04002017 vol_args = memdup_user(arg, sizeof(*vol_args));
2018 if (IS_ERR(vol_args))
2019 return PTR_ERR(vol_args);
2020
2021 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2022 namelen = strlen(vol_args->name);
2023 if (strchr(vol_args->name, '/') ||
2024 strncmp(vol_args->name, "..", namelen) == 0) {
2025 err = -EINVAL;
2026 goto out;
2027 }
2028
Al Viroa561be72011-11-23 11:57:51 -05002029 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002030 if (err)
2031 goto out;
2032
2033 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2034 dentry = lookup_one_len(vol_args->name, parent, namelen);
2035 if (IS_ERR(dentry)) {
2036 err = PTR_ERR(dentry);
2037 goto out_unlock_dir;
2038 }
2039
2040 if (!dentry->d_inode) {
2041 err = -ENOENT;
2042 goto out_dput;
2043 }
2044
2045 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002046 dest = BTRFS_I(inode)->root;
2047 if (!capable(CAP_SYS_ADMIN)){
2048 /*
2049 * Regular user. Only allow this with a special mount
2050 * option, when the user has write+exec access to the
2051 * subvol root, and when rmdir(2) would have been
2052 * allowed.
2053 *
2054 * Note that this is _not_ check that the subvol is
2055 * empty or doesn't contain data that we wouldn't
2056 * otherwise be able to delete.
2057 *
2058 * Users who want to delete empty subvols should try
2059 * rmdir(2).
2060 */
2061 err = -EPERM;
2062 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2063 goto out_dput;
2064
2065 /*
2066 * Do not allow deletion if the parent dir is the same
2067 * as the dir to be deleted. That means the ioctl
2068 * must be called on the dentry referencing the root
2069 * of the subvol, not a random directory contained
2070 * within it.
2071 */
2072 err = -EINVAL;
2073 if (root == dest)
2074 goto out_dput;
2075
2076 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2077 if (err)
2078 goto out_dput;
2079
2080 /* check if subvolume may be deleted by a non-root user */
2081 err = btrfs_may_delete(dir, dentry, 1);
2082 if (err)
2083 goto out_dput;
2084 }
2085
Li Zefan33345d012011-04-20 10:31:50 +08002086 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002087 err = -EINVAL;
2088 goto out_dput;
2089 }
2090
Yan, Zheng76dda932009-09-21 16:00:26 -04002091 mutex_lock(&inode->i_mutex);
2092 err = d_invalidate(dentry);
2093 if (err)
2094 goto out_unlock;
2095
2096 down_write(&root->fs_info->subvol_sem);
2097
2098 err = may_destroy_subvol(dest);
2099 if (err)
2100 goto out_up_write;
2101
Yan, Zhenga22285a2010-05-16 10:48:46 -04002102 trans = btrfs_start_transaction(root, 0);
2103 if (IS_ERR(trans)) {
2104 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002105 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002106 }
2107 trans->block_rsv = &root->fs_info->global_block_rsv;
2108
Yan, Zheng76dda932009-09-21 16:00:26 -04002109 ret = btrfs_unlink_subvol(trans, root, dir,
2110 dest->root_key.objectid,
2111 dentry->d_name.name,
2112 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002113 if (ret) {
2114 err = ret;
2115 btrfs_abort_transaction(trans, root, ret);
2116 goto out_end_trans;
2117 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002118
2119 btrfs_record_root_in_trans(trans, dest);
2120
2121 memset(&dest->root_item.drop_progress, 0,
2122 sizeof(dest->root_item.drop_progress));
2123 dest->root_item.drop_level = 0;
2124 btrfs_set_root_refs(&dest->root_item, 0);
2125
Yan, Zhengd68fc572010-05-16 10:49:58 -04002126 if (!xchg(&dest->orphan_item_inserted, 1)) {
2127 ret = btrfs_insert_orphan_item(trans,
2128 root->fs_info->tree_root,
2129 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002130 if (ret) {
2131 btrfs_abort_transaction(trans, root, ret);
2132 err = ret;
2133 goto out_end_trans;
2134 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002135 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002136out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002137 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002138 if (ret && !err)
2139 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002140 inode->i_flags |= S_DEAD;
2141out_up_write:
2142 up_write(&root->fs_info->subvol_sem);
2143out_unlock:
2144 mutex_unlock(&inode->i_mutex);
2145 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002146 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002147 btrfs_invalidate_inodes(dest);
2148 d_delete(dentry);
2149 }
2150out_dput:
2151 dput(dentry);
2152out_unlock_dir:
2153 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002154 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002155out:
2156 kfree(vol_args);
2157 return err;
2158}
2159
Chris Mason1e701a32010-03-11 09:42:04 -05002160static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002161{
2162 struct inode *inode = fdentry(file)->d_inode;
2163 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002164 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002165 int ret;
2166
Li Zefanb83cc962010-12-20 16:04:08 +08002167 if (btrfs_root_readonly(root))
2168 return -EROFS;
2169
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002170 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2171 1)) {
2172 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2173 return -EINPROGRESS;
2174 }
Al Viroa561be72011-11-23 11:57:51 -05002175 ret = mnt_want_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002176 if (ret) {
2177 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
2178 0);
Yan Zhengc146afa2008-11-12 14:34:12 -05002179 return ret;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002180 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002181
2182 switch (inode->i_mode & S_IFMT) {
2183 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002184 if (!capable(CAP_SYS_ADMIN)) {
2185 ret = -EPERM;
2186 goto out;
2187 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002188 ret = btrfs_defrag_root(root, 0);
2189 if (ret)
2190 goto out;
2191 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002192 break;
2193 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002194 if (!(file->f_mode & FMODE_WRITE)) {
2195 ret = -EINVAL;
2196 goto out;
2197 }
Chris Mason1e701a32010-03-11 09:42:04 -05002198
2199 range = kzalloc(sizeof(*range), GFP_KERNEL);
2200 if (!range) {
2201 ret = -ENOMEM;
2202 goto out;
2203 }
2204
2205 if (argp) {
2206 if (copy_from_user(range, argp,
2207 sizeof(*range))) {
2208 ret = -EFAULT;
2209 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002210 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002211 }
2212 /* compression requires us to start the IO */
2213 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2214 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2215 range->extent_thresh = (u32)-1;
2216 }
2217 } else {
2218 /* the rest are all set to zero by kzalloc */
2219 range->len = (u64)-1;
2220 }
Chris Mason4cb53002011-05-24 15:35:30 -04002221 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2222 range, 0, 0);
2223 if (ret > 0)
2224 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002225 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002226 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002227 default:
2228 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002229 }
Chris Masone441d542009-01-05 16:57:23 -05002230out:
Al Viro2a79f172011-12-09 08:06:57 -05002231 mnt_drop_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002232 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Chris Masone441d542009-01-05 16:57:23 -05002233 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002234}
2235
Christoph Hellwigb2950862008-12-02 09:54:17 -05002236static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002237{
2238 struct btrfs_ioctl_vol_args *vol_args;
2239 int ret;
2240
Chris Masone441d542009-01-05 16:57:23 -05002241 if (!capable(CAP_SYS_ADMIN))
2242 return -EPERM;
2243
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002244 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2245 1)) {
2246 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2247 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002248 }
2249
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002250 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002251 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002252 if (IS_ERR(vol_args)) {
2253 ret = PTR_ERR(vol_args);
2254 goto out;
2255 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002256
Mark Fasheh5516e592008-07-24 12:20:14 -04002257 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002258 ret = btrfs_init_new_device(root, vol_args->name);
2259
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002260 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002261out:
2262 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002263 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002264 return ret;
2265}
2266
Christoph Hellwigb2950862008-12-02 09:54:17 -05002267static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002268{
2269 struct btrfs_ioctl_vol_args *vol_args;
2270 int ret;
2271
Chris Masone441d542009-01-05 16:57:23 -05002272 if (!capable(CAP_SYS_ADMIN))
2273 return -EPERM;
2274
Yan Zhengc146afa2008-11-12 14:34:12 -05002275 if (root->fs_info->sb->s_flags & MS_RDONLY)
2276 return -EROFS;
2277
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002278 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2279 1)) {
2280 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2281 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002282 }
2283
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002284 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002285 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002286 if (IS_ERR(vol_args)) {
2287 ret = PTR_ERR(vol_args);
2288 goto out;
2289 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002290
Mark Fasheh5516e592008-07-24 12:20:14 -04002291 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002292 ret = btrfs_rm_device(root, vol_args->name);
2293
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002294 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002295out:
2296 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002297 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002298 return ret;
2299}
2300
Jan Schmidt475f6382011-03-11 15:41:01 +01002301static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2302{
Li Zefan027ed2f2011-06-08 08:27:56 +00002303 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002304 struct btrfs_device *device;
2305 struct btrfs_device *next;
2306 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002307 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002308
2309 if (!capable(CAP_SYS_ADMIN))
2310 return -EPERM;
2311
Li Zefan027ed2f2011-06-08 08:27:56 +00002312 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2313 if (!fi_args)
2314 return -ENOMEM;
2315
2316 fi_args->num_devices = fs_devices->num_devices;
2317 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002318
2319 mutex_lock(&fs_devices->device_list_mutex);
2320 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002321 if (device->devid > fi_args->max_id)
2322 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002323 }
2324 mutex_unlock(&fs_devices->device_list_mutex);
2325
Li Zefan027ed2f2011-06-08 08:27:56 +00002326 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2327 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002328
Li Zefan027ed2f2011-06-08 08:27:56 +00002329 kfree(fi_args);
2330 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002331}
2332
2333static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2334{
2335 struct btrfs_ioctl_dev_info_args *di_args;
2336 struct btrfs_device *dev;
2337 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2338 int ret = 0;
2339 char *s_uuid = NULL;
2340 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2341
2342 if (!capable(CAP_SYS_ADMIN))
2343 return -EPERM;
2344
2345 di_args = memdup_user(arg, sizeof(*di_args));
2346 if (IS_ERR(di_args))
2347 return PTR_ERR(di_args);
2348
2349 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2350 s_uuid = di_args->uuid;
2351
2352 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002353 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002354 mutex_unlock(&fs_devices->device_list_mutex);
2355
2356 if (!dev) {
2357 ret = -ENODEV;
2358 goto out;
2359 }
2360
2361 di_args->devid = dev->devid;
2362 di_args->bytes_used = dev->bytes_used;
2363 di_args->total_bytes = dev->total_bytes;
2364 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002365 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002366 struct rcu_string *name;
2367
2368 rcu_read_lock();
2369 name = rcu_dereference(dev->name);
2370 strncpy(di_args->path, name->str, sizeof(di_args->path));
2371 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002372 di_args->path[sizeof(di_args->path) - 1] = 0;
2373 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002374 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002375 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002376
2377out:
2378 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2379 ret = -EFAULT;
2380
2381 kfree(di_args);
2382 return ret;
2383}
2384
Yan, Zheng76dda932009-09-21 16:00:26 -04002385static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2386 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002387{
2388 struct inode *inode = fdentry(file)->d_inode;
2389 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro2903ff02012-08-28 12:52:22 -04002390 struct fd src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002391 struct inode *src;
2392 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002393 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002394 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002395 char *buf;
2396 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002397 u32 nritems;
2398 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002399 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002400 u64 len = olen;
2401 u64 bs = root->fs_info->sb->s_blocksize;
Chris Masond20f7042008-12-08 16:58:54 -05002402
Sage Weilc5c9cd42008-11-12 14:32:25 -05002403 /*
2404 * TODO:
2405 * - split compressed inline extents. annoying: we need to
2406 * decompress into destination's address_space (the file offset
2407 * may change, so source mapping won't do), then recompress (or
2408 * otherwise reinsert) a subrange.
2409 * - allow ranges within the same file to be cloned (provided
2410 * they don't overlap)?
2411 */
2412
Chris Masone441d542009-01-05 16:57:23 -05002413 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002414 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002415 return -EINVAL;
2416
Li Zefanb83cc962010-12-20 16:04:08 +08002417 if (btrfs_root_readonly(root))
2418 return -EROFS;
2419
Al Viroa561be72011-11-23 11:57:51 -05002420 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002421 if (ret)
2422 return ret;
2423
Al Viro2903ff02012-08-28 12:52:22 -04002424 src_file = fdget(srcfd);
2425 if (!src_file.file) {
Yan Zhengab67b7c2008-12-19 10:58:39 -05002426 ret = -EBADF;
2427 goto out_drop_write;
2428 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002429
David Sterba362a20c2011-08-01 18:11:57 +02002430 ret = -EXDEV;
Al Viro2903ff02012-08-28 12:52:22 -04002431 if (src_file.file->f_path.mnt != file->f_path.mnt)
David Sterba362a20c2011-08-01 18:11:57 +02002432 goto out_fput;
2433
Al Viro2903ff02012-08-28 12:52:22 -04002434 src = src_file.file->f_dentry->d_inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002435
Sage Weilc5c9cd42008-11-12 14:32:25 -05002436 ret = -EINVAL;
2437 if (src == inode)
2438 goto out_fput;
2439
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002440 /* the src must be open for reading */
Al Viro2903ff02012-08-28 12:52:22 -04002441 if (!(src_file.file->f_mode & FMODE_READ))
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002442 goto out_fput;
2443
Li Zefan0e7b8242011-09-18 10:20:46 -04002444 /* don't make the dst file partly checksummed */
2445 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2446 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2447 goto out_fput;
2448
Yan Zhengae01a0a2008-08-04 23:23:47 -04002449 ret = -EISDIR;
2450 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002451 goto out_fput;
2452
Yan Zhengae01a0a2008-08-04 23:23:47 -04002453 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002454 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002455 goto out_fput;
2456
2457 ret = -ENOMEM;
2458 buf = vmalloc(btrfs_level_size(root, 0));
2459 if (!buf)
2460 goto out_fput;
2461
2462 path = btrfs_alloc_path();
2463 if (!path) {
2464 vfree(buf);
2465 goto out_fput;
2466 }
2467 path->reada = 2;
2468
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002469 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002470 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2471 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002472 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002473 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2474 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002475 }
2476
Sage Weilc5c9cd42008-11-12 14:32:25 -05002477 /* determine range to clone */
2478 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002479 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002480 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002481 if (len == 0)
2482 olen = len = src->i_size - off;
2483 /* if we extend to eof, continue to block boundary */
2484 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002485 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002486
2487 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002488 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2489 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002490 goto out_unlock;
2491
Li Zefand525e8a2011-09-11 10:52:25 -04002492 if (destoff > inode->i_size) {
2493 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2494 if (ret)
2495 goto out_unlock;
2496 }
2497
Li Zefan71ef0782011-09-18 10:20:46 -04002498 /* truncate page cache pages from target inode range */
2499 truncate_inode_pages_range(&inode->i_data, destoff,
2500 PAGE_CACHE_ALIGN(destoff + len) - 1);
2501
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002502 /* do any pending delalloc/csum calc on src, one way or
2503 another, and lock file content */
2504 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002505 struct btrfs_ordered_extent *ordered;
Liu Boaa42ffd2012-09-18 03:52:23 -06002506 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2507 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
Sage Weil9a019192010-10-29 15:37:33 -04002508 if (!ordered &&
Liu Boaa42ffd2012-09-18 03:52:23 -06002509 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2510 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002511 break;
Liu Boaa42ffd2012-09-18 03:52:23 -06002512 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002513 if (ordered)
2514 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002515 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002516 }
2517
Sage Weilc5c9cd42008-11-12 14:32:25 -05002518 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002519 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002520 key.type = BTRFS_EXTENT_DATA_KEY;
2521 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002522
2523 while (1) {
2524 /*
2525 * note the key will change type as we walk through the
2526 * tree.
2527 */
David Sterba362a20c2011-08-01 18:11:57 +02002528 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2529 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002530 if (ret < 0)
2531 goto out;
2532
Yan Zhengae01a0a2008-08-04 23:23:47 -04002533 nritems = btrfs_header_nritems(path->nodes[0]);
2534 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002535 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002536 if (ret < 0)
2537 goto out;
2538 if (ret > 0)
2539 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002540 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002541 }
2542 leaf = path->nodes[0];
2543 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002544
Yan Zhengae01a0a2008-08-04 23:23:47 -04002545 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002546 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002547 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002548 break;
2549
Sage Weilc5c9cd42008-11-12 14:32:25 -05002550 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2551 struct btrfs_file_extent_item *extent;
2552 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002553 u32 size;
2554 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002555 u64 disko = 0, diskl = 0;
2556 u64 datao = 0, datal = 0;
2557 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002558 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002559
2560 size = btrfs_item_size_nr(leaf, slot);
2561 read_extent_buffer(leaf, buf,
2562 btrfs_item_ptr_offset(leaf, slot),
2563 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002564
2565 extent = btrfs_item_ptr(leaf, slot,
2566 struct btrfs_file_extent_item);
2567 comp = btrfs_file_extent_compression(leaf, extent);
2568 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002569 if (type == BTRFS_FILE_EXTENT_REG ||
2570 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002571 disko = btrfs_file_extent_disk_bytenr(leaf,
2572 extent);
2573 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2574 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002575 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002576 datal = btrfs_file_extent_num_bytes(leaf,
2577 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002578 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2579 /* take upper bound, may be compressed */
2580 datal = btrfs_file_extent_ram_bytes(leaf,
2581 extent);
2582 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002583 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002584
Sage Weil050006a2010-10-29 15:37:33 -04002585 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002586 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002587 goto next;
2588
Zheng Yan31840ae2008-09-23 13:14:14 -04002589 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002590 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002591 if (off <= key.offset)
2592 new_key.offset = key.offset + destoff - off;
2593 else
2594 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002595
Sage Weilb6f34092011-09-20 14:48:51 -04002596 /*
2597 * 1 - adjusting old extent (we may have to split it)
2598 * 1 - add new extent
2599 * 1 - inode update
2600 */
2601 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002602 if (IS_ERR(trans)) {
2603 ret = PTR_ERR(trans);
2604 goto out;
2605 }
2606
Chris Masonc8a894d2009-06-27 21:07:03 -04002607 if (type == BTRFS_FILE_EXTENT_REG ||
2608 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002609 /*
2610 * a | --- range to clone ---| b
2611 * | ------------- extent ------------- |
2612 */
2613
2614 /* substract range b */
2615 if (key.offset + datal > off + len)
2616 datal = off + len - key.offset;
2617
2618 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002619 if (off > key.offset) {
2620 datao += off - key.offset;
2621 datal -= off - key.offset;
2622 }
2623
Josef Bacik5dc562c2012-08-17 13:14:17 -04002624 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002625 new_key.offset,
2626 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002627 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002628 if (ret) {
2629 btrfs_abort_transaction(trans, root,
2630 ret);
2631 btrfs_end_transaction(trans, root);
2632 goto out;
2633 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002634
Sage Weilc5c9cd42008-11-12 14:32:25 -05002635 ret = btrfs_insert_empty_item(trans, root, path,
2636 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002637 if (ret) {
2638 btrfs_abort_transaction(trans, root,
2639 ret);
2640 btrfs_end_transaction(trans, root);
2641 goto out;
2642 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002643
2644 leaf = path->nodes[0];
2645 slot = path->slots[0];
2646 write_extent_buffer(leaf, buf,
2647 btrfs_item_ptr_offset(leaf, slot),
2648 size);
2649
2650 extent = btrfs_item_ptr(leaf, slot,
2651 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002652
Sage Weilc5c9cd42008-11-12 14:32:25 -05002653 /* disko == 0 means it's a hole */
2654 if (!disko)
2655 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002656
2657 btrfs_set_file_extent_offset(leaf, extent,
2658 datao);
2659 btrfs_set_file_extent_num_bytes(leaf, extent,
2660 datal);
2661 if (disko) {
2662 inode_add_bytes(inode, datal);
2663 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002664 disko, diskl, 0,
2665 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002666 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002667 new_key.offset - datao,
2668 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002669 if (ret) {
2670 btrfs_abort_transaction(trans,
2671 root,
2672 ret);
2673 btrfs_end_transaction(trans,
2674 root);
2675 goto out;
2676
2677 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002678 }
2679 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2680 u64 skip = 0;
2681 u64 trim = 0;
2682 if (off > key.offset) {
2683 skip = off - key.offset;
2684 new_key.offset += skip;
2685 }
Chris Masond3977122009-01-05 21:25:51 -05002686
Liu Boaa42ffd2012-09-18 03:52:23 -06002687 if (key.offset + datal > off + len)
2688 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05002689
Sage Weilc5c9cd42008-11-12 14:32:25 -05002690 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002691 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002692 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002693 goto out;
2694 }
2695 size -= skip + trim;
2696 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002697
Josef Bacik5dc562c2012-08-17 13:14:17 -04002698 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002699 new_key.offset,
2700 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002701 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002702 if (ret) {
2703 btrfs_abort_transaction(trans, root,
2704 ret);
2705 btrfs_end_transaction(trans, root);
2706 goto out;
2707 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002708
Sage Weilc5c9cd42008-11-12 14:32:25 -05002709 ret = btrfs_insert_empty_item(trans, root, path,
2710 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002711 if (ret) {
2712 btrfs_abort_transaction(trans, root,
2713 ret);
2714 btrfs_end_transaction(trans, root);
2715 goto out;
2716 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002717
2718 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002719 u32 start =
2720 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002721 memmove(buf+start, buf+start+skip,
2722 datal);
2723 }
2724
2725 leaf = path->nodes[0];
2726 slot = path->slots[0];
2727 write_extent_buffer(leaf, buf,
2728 btrfs_item_ptr_offset(leaf, slot),
2729 size);
2730 inode_add_bytes(inode, datal);
2731 }
2732
2733 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002734 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002735
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002736 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002737 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002738
2739 /*
2740 * we round up to the block size at eof when
2741 * determining which extents to clone above,
2742 * but shouldn't round up the file size
2743 */
2744 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002745 if (endoff > destoff+olen)
2746 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002747 if (endoff > inode->i_size)
2748 btrfs_i_size_write(inode, endoff);
2749
Yan, Zhenga22285a2010-05-16 10:48:46 -04002750 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002751 if (ret) {
2752 btrfs_abort_transaction(trans, root, ret);
2753 btrfs_end_transaction(trans, root);
2754 goto out;
2755 }
2756 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002757 }
Chris Masond3977122009-01-05 21:25:51 -05002758next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002759 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002760 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002761 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002762 ret = 0;
2763out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002764 btrfs_release_path(path);
Liu Boaa42ffd2012-09-18 03:52:23 -06002765 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002766out_unlock:
2767 mutex_unlock(&src->i_mutex);
2768 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002769 vfree(buf);
2770 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002771out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04002772 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002773out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002774 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002775 return ret;
2776}
2777
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002778static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002779{
2780 struct btrfs_ioctl_clone_range_args args;
2781
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002782 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002783 return -EFAULT;
2784 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2785 args.src_length, args.dest_offset);
2786}
2787
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002788/*
2789 * there are many ways the trans_start and trans_end ioctls can lead
2790 * to deadlocks. They should only be used by applications that
2791 * basically own the machine, and have a very in depth understanding
2792 * of all the possible deadlocks and enospc problems.
2793 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002794static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002795{
2796 struct inode *inode = fdentry(file)->d_inode;
2797 struct btrfs_root *root = BTRFS_I(inode)->root;
2798 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002799 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002800
Sage Weil1ab86ae2009-09-29 18:38:44 -04002801 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002802 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002803 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002804
2805 ret = -EINPROGRESS;
2806 if (file->private_data)
2807 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002808
Li Zefanb83cc962010-12-20 16:04:08 +08002809 ret = -EROFS;
2810 if (btrfs_root_readonly(root))
2811 goto out;
2812
Al Viroa561be72011-11-23 11:57:51 -05002813 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002814 if (ret)
2815 goto out;
2816
Josef Bacika4abeea2011-04-11 17:25:13 -04002817 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002818
Sage Weil1ab86ae2009-09-29 18:38:44 -04002819 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002820 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002821 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002822 goto out_drop;
2823
2824 file->private_data = trans;
2825 return 0;
2826
2827out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002828 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002829 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002830out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002831 return ret;
2832}
2833
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002834static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2835{
2836 struct inode *inode = fdentry(file)->d_inode;
2837 struct btrfs_root *root = BTRFS_I(inode)->root;
2838 struct btrfs_root *new_root;
2839 struct btrfs_dir_item *di;
2840 struct btrfs_trans_handle *trans;
2841 struct btrfs_path *path;
2842 struct btrfs_key location;
2843 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002844 u64 objectid = 0;
2845 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00002846 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002847
2848 if (!capable(CAP_SYS_ADMIN))
2849 return -EPERM;
2850
Miao Xie3c04ce02012-11-26 08:43:07 +00002851 ret = mnt_want_write_file(file);
2852 if (ret)
2853 return ret;
2854
2855 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2856 ret = -EFAULT;
2857 goto out;
2858 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002859
2860 if (!objectid)
2861 objectid = root->root_key.objectid;
2862
2863 location.objectid = objectid;
2864 location.type = BTRFS_ROOT_ITEM_KEY;
2865 location.offset = (u64)-1;
2866
2867 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00002868 if (IS_ERR(new_root)) {
2869 ret = PTR_ERR(new_root);
2870 goto out;
2871 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002872
Miao Xie3c04ce02012-11-26 08:43:07 +00002873 if (btrfs_root_refs(&new_root->root_item) == 0) {
2874 ret = -ENOENT;
2875 goto out;
2876 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002877
2878 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00002879 if (!path) {
2880 ret = -ENOMEM;
2881 goto out;
2882 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002883 path->leave_spinning = 1;
2884
2885 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002886 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002887 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00002888 ret = PTR_ERR(trans);
2889 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002890 }
2891
David Sterba6c417612011-04-13 15:41:04 +02002892 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002893 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2894 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002895 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002896 btrfs_free_path(path);
2897 btrfs_end_transaction(trans, root);
2898 printk(KERN_ERR "Umm, you don't have the default dir item, "
2899 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00002900 ret = -ENOENT;
2901 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002902 }
2903
2904 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2905 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2906 btrfs_mark_buffer_dirty(path->nodes[0]);
2907 btrfs_free_path(path);
2908
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06002909 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002910 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00002911out:
2912 mnt_drop_write_file(file);
2913 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002914}
2915
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002916void btrfs_get_block_group_info(struct list_head *groups_list,
2917 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002918{
2919 struct btrfs_block_group_cache *block_group;
2920
2921 space->total_bytes = 0;
2922 space->used_bytes = 0;
2923 space->flags = 0;
2924 list_for_each_entry(block_group, groups_list, list) {
2925 space->flags = block_group->flags;
2926 space->total_bytes += block_group->key.offset;
2927 space->used_bytes +=
2928 btrfs_block_group_used(&block_group->item);
2929 }
2930}
2931
Josef Bacik1406e432010-01-13 18:19:06 +00002932long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2933{
2934 struct btrfs_ioctl_space_args space_args;
2935 struct btrfs_ioctl_space_info space;
2936 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002937 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002938 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002939 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002940 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2941 BTRFS_BLOCK_GROUP_SYSTEM,
2942 BTRFS_BLOCK_GROUP_METADATA,
2943 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2944 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002945 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002946 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002947 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002948 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002949
2950 if (copy_from_user(&space_args,
2951 (struct btrfs_ioctl_space_args __user *)arg,
2952 sizeof(space_args)))
2953 return -EFAULT;
2954
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002955 for (i = 0; i < num_types; i++) {
2956 struct btrfs_space_info *tmp;
2957
2958 info = NULL;
2959 rcu_read_lock();
2960 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2961 list) {
2962 if (tmp->flags == types[i]) {
2963 info = tmp;
2964 break;
2965 }
2966 }
2967 rcu_read_unlock();
2968
2969 if (!info)
2970 continue;
2971
2972 down_read(&info->groups_sem);
2973 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2974 if (!list_empty(&info->block_groups[c]))
2975 slot_count++;
2976 }
2977 up_read(&info->groups_sem);
2978 }
Josef Bacik1406e432010-01-13 18:19:06 +00002979
Chris Mason7fde62b2010-03-16 15:40:10 -04002980 /* space_slots == 0 means they are asking for a count */
2981 if (space_args.space_slots == 0) {
2982 space_args.total_spaces = slot_count;
2983 goto out;
2984 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002985
Dan Rosenberg51788b12011-02-14 16:04:23 -05002986 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002987
Chris Mason7fde62b2010-03-16 15:40:10 -04002988 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002989
Chris Mason7fde62b2010-03-16 15:40:10 -04002990 /* we generally have at most 6 or so space infos, one for each raid
2991 * level. So, a whole page should be more than enough for everyone
2992 */
2993 if (alloc_size > PAGE_CACHE_SIZE)
2994 return -ENOMEM;
2995
2996 space_args.total_spaces = 0;
2997 dest = kmalloc(alloc_size, GFP_NOFS);
2998 if (!dest)
2999 return -ENOMEM;
3000 dest_orig = dest;
3001
3002 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003003 for (i = 0; i < num_types; i++) {
3004 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003005
Dan Rosenberg51788b12011-02-14 16:04:23 -05003006 if (!slot_count)
3007 break;
3008
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003009 info = NULL;
3010 rcu_read_lock();
3011 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3012 list) {
3013 if (tmp->flags == types[i]) {
3014 info = tmp;
3015 break;
3016 }
3017 }
3018 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003019
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003020 if (!info)
3021 continue;
3022 down_read(&info->groups_sem);
3023 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3024 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003025 btrfs_get_block_group_info(
3026 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003027 memcpy(dest, &space, sizeof(space));
3028 dest++;
3029 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003030 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003031 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003032 if (!slot_count)
3033 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003034 }
3035 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003036 }
Josef Bacik1406e432010-01-13 18:19:06 +00003037
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003038 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003039 (arg + sizeof(struct btrfs_ioctl_space_args));
3040
3041 if (copy_to_user(user_dest, dest_orig, alloc_size))
3042 ret = -EFAULT;
3043
3044 kfree(dest_orig);
3045out:
3046 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003047 ret = -EFAULT;
3048
3049 return ret;
3050}
3051
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003052/*
3053 * there are many ways the trans_start and trans_end ioctls can lead
3054 * to deadlocks. They should only be used by applications that
3055 * basically own the machine, and have a very in depth understanding
3056 * of all the possible deadlocks and enospc problems.
3057 */
3058long btrfs_ioctl_trans_end(struct file *file)
3059{
3060 struct inode *inode = fdentry(file)->d_inode;
3061 struct btrfs_root *root = BTRFS_I(inode)->root;
3062 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003063
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003064 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003065 if (!trans)
3066 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003067 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003068
Sage Weil1ab86ae2009-09-29 18:38:44 -04003069 btrfs_end_transaction(trans, root);
3070
Josef Bacika4abeea2011-04-11 17:25:13 -04003071 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003072
Al Viro2a79f172011-12-09 08:06:57 -05003073 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003074 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003075}
3076
Miao Xie9a8c28b2012-11-26 08:40:43 +00003077static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3078 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003079{
Sage Weil46204592010-10-29 15:41:32 -04003080 struct btrfs_trans_handle *trans;
3081 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003082 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003083
Miao Xieff7c1d32012-11-26 08:41:29 +00003084 trans = btrfs_attach_transaction(root);
3085 if (IS_ERR(trans)) {
3086 if (PTR_ERR(trans) != -ENOENT)
3087 return PTR_ERR(trans);
3088
3089 /* No running transaction, don't bother */
3090 transid = root->fs_info->last_trans_committed;
3091 goto out;
3092 }
Sage Weil46204592010-10-29 15:41:32 -04003093 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003094 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003095 if (ret) {
3096 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003097 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003098 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003099out:
Sage Weil46204592010-10-29 15:41:32 -04003100 if (argp)
3101 if (copy_to_user(argp, &transid, sizeof(transid)))
3102 return -EFAULT;
3103 return 0;
3104}
3105
Miao Xie9a8c28b2012-11-26 08:40:43 +00003106static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3107 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003108{
Sage Weil46204592010-10-29 15:41:32 -04003109 u64 transid;
3110
3111 if (argp) {
3112 if (copy_from_user(&transid, argp, sizeof(transid)))
3113 return -EFAULT;
3114 } else {
3115 transid = 0; /* current trans */
3116 }
3117 return btrfs_wait_for_commit(root, transid);
3118}
3119
Jan Schmidt475f6382011-03-11 15:41:01 +01003120static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3121{
3122 int ret;
3123 struct btrfs_ioctl_scrub_args *sa;
3124
3125 if (!capable(CAP_SYS_ADMIN))
3126 return -EPERM;
3127
3128 sa = memdup_user(arg, sizeof(*sa));
3129 if (IS_ERR(sa))
3130 return PTR_ERR(sa);
3131
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003132 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003133 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3134 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003135
3136 if (copy_to_user(arg, sa, sizeof(*sa)))
3137 ret = -EFAULT;
3138
3139 kfree(sa);
3140 return ret;
3141}
3142
3143static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3144{
3145 if (!capable(CAP_SYS_ADMIN))
3146 return -EPERM;
3147
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003148 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003149}
3150
3151static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3152 void __user *arg)
3153{
3154 struct btrfs_ioctl_scrub_args *sa;
3155 int ret;
3156
3157 if (!capable(CAP_SYS_ADMIN))
3158 return -EPERM;
3159
3160 sa = memdup_user(arg, sizeof(*sa));
3161 if (IS_ERR(sa))
3162 return PTR_ERR(sa);
3163
3164 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3165
3166 if (copy_to_user(arg, sa, sizeof(*sa)))
3167 ret = -EFAULT;
3168
3169 kfree(sa);
3170 return ret;
3171}
3172
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003173static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003174 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003175{
3176 struct btrfs_ioctl_get_dev_stats *sa;
3177 int ret;
3178
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003179 sa = memdup_user(arg, sizeof(*sa));
3180 if (IS_ERR(sa))
3181 return PTR_ERR(sa);
3182
David Sterbab27f7c02012-06-22 06:30:39 -06003183 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3184 kfree(sa);
3185 return -EPERM;
3186 }
3187
3188 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003189
3190 if (copy_to_user(arg, sa, sizeof(*sa)))
3191 ret = -EFAULT;
3192
3193 kfree(sa);
3194 return ret;
3195}
3196
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003197static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3198{
3199 struct btrfs_ioctl_dev_replace_args *p;
3200 int ret;
3201
3202 if (!capable(CAP_SYS_ADMIN))
3203 return -EPERM;
3204
3205 p = memdup_user(arg, sizeof(*p));
3206 if (IS_ERR(p))
3207 return PTR_ERR(p);
3208
3209 switch (p->cmd) {
3210 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3211 if (atomic_xchg(
3212 &root->fs_info->mutually_exclusive_operation_running,
3213 1)) {
3214 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3215 ret = -EINPROGRESS;
3216 } else {
3217 ret = btrfs_dev_replace_start(root, p);
3218 atomic_set(
3219 &root->fs_info->mutually_exclusive_operation_running,
3220 0);
3221 }
3222 break;
3223 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3224 btrfs_dev_replace_status(root->fs_info, p);
3225 ret = 0;
3226 break;
3227 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3228 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3229 break;
3230 default:
3231 ret = -EINVAL;
3232 break;
3233 }
3234
3235 if (copy_to_user(arg, p, sizeof(*p)))
3236 ret = -EFAULT;
3237
3238 kfree(p);
3239 return ret;
3240}
3241
Jan Schmidtd7728c92011-07-07 16:48:38 +02003242static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3243{
3244 int ret = 0;
3245 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003246 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003247 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003248 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003249 struct inode_fs_paths *ipath = NULL;
3250 struct btrfs_path *path;
3251
3252 if (!capable(CAP_SYS_ADMIN))
3253 return -EPERM;
3254
3255 path = btrfs_alloc_path();
3256 if (!path) {
3257 ret = -ENOMEM;
3258 goto out;
3259 }
3260
3261 ipa = memdup_user(arg, sizeof(*ipa));
3262 if (IS_ERR(ipa)) {
3263 ret = PTR_ERR(ipa);
3264 ipa = NULL;
3265 goto out;
3266 }
3267
3268 size = min_t(u32, ipa->size, 4096);
3269 ipath = init_ipath(size, root, path);
3270 if (IS_ERR(ipath)) {
3271 ret = PTR_ERR(ipath);
3272 ipath = NULL;
3273 goto out;
3274 }
3275
3276 ret = paths_from_inode(ipa->inum, ipath);
3277 if (ret < 0)
3278 goto out;
3279
3280 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003281 rel_ptr = ipath->fspath->val[i] -
3282 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003283 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003284 }
3285
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003286 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3287 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003288 if (ret) {
3289 ret = -EFAULT;
3290 goto out;
3291 }
3292
3293out:
3294 btrfs_free_path(path);
3295 free_ipath(ipath);
3296 kfree(ipa);
3297
3298 return ret;
3299}
3300
3301static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3302{
3303 struct btrfs_data_container *inodes = ctx;
3304 const size_t c = 3 * sizeof(u64);
3305
3306 if (inodes->bytes_left >= c) {
3307 inodes->bytes_left -= c;
3308 inodes->val[inodes->elem_cnt] = inum;
3309 inodes->val[inodes->elem_cnt + 1] = offset;
3310 inodes->val[inodes->elem_cnt + 2] = root;
3311 inodes->elem_cnt += 3;
3312 } else {
3313 inodes->bytes_missing += c - inodes->bytes_left;
3314 inodes->bytes_left = 0;
3315 inodes->elem_missed += 3;
3316 }
3317
3318 return 0;
3319}
3320
3321static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3322 void __user *arg)
3323{
3324 int ret = 0;
3325 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003326 struct btrfs_ioctl_logical_ino_args *loi;
3327 struct btrfs_data_container *inodes = NULL;
3328 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003329
3330 if (!capable(CAP_SYS_ADMIN))
3331 return -EPERM;
3332
3333 loi = memdup_user(arg, sizeof(*loi));
3334 if (IS_ERR(loi)) {
3335 ret = PTR_ERR(loi);
3336 loi = NULL;
3337 goto out;
3338 }
3339
3340 path = btrfs_alloc_path();
3341 if (!path) {
3342 ret = -ENOMEM;
3343 goto out;
3344 }
3345
Liu Bo425d17a2012-09-07 20:01:30 -06003346 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003347 inodes = init_data_container(size);
3348 if (IS_ERR(inodes)) {
3349 ret = PTR_ERR(inodes);
3350 inodes = NULL;
3351 goto out;
3352 }
3353
Liu Bodf031f02012-09-07 20:01:29 -06003354 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3355 build_ino_list, inodes);
3356 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003357 ret = -ENOENT;
3358 if (ret < 0)
3359 goto out;
3360
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003361 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3362 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003363 if (ret)
3364 ret = -EFAULT;
3365
3366out:
3367 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003368 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003369 kfree(loi);
3370
3371 return ret;
3372}
3373
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003374void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003375 struct btrfs_ioctl_balance_args *bargs)
3376{
3377 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3378
3379 bargs->flags = bctl->flags;
3380
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003381 if (atomic_read(&fs_info->balance_running))
3382 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3383 if (atomic_read(&fs_info->balance_pause_req))
3384 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003385 if (atomic_read(&fs_info->balance_cancel_req))
3386 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003387
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003388 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3389 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3390 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003391
3392 if (lock) {
3393 spin_lock(&fs_info->balance_lock);
3394 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3395 spin_unlock(&fs_info->balance_lock);
3396 } else {
3397 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3398 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003399}
3400
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003401static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003402{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003403 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003404 struct btrfs_fs_info *fs_info = root->fs_info;
3405 struct btrfs_ioctl_balance_args *bargs;
3406 struct btrfs_balance_control *bctl;
3407 int ret;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003408 int need_to_clear_lock = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003409
3410 if (!capable(CAP_SYS_ADMIN))
3411 return -EPERM;
3412
Liu Boe54bfa32012-06-29 03:58:48 -06003413 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003414 if (ret)
3415 return ret;
3416
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003417 mutex_lock(&fs_info->volume_mutex);
3418 mutex_lock(&fs_info->balance_mutex);
3419
3420 if (arg) {
3421 bargs = memdup_user(arg, sizeof(*bargs));
3422 if (IS_ERR(bargs)) {
3423 ret = PTR_ERR(bargs);
3424 goto out;
3425 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003426
3427 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3428 if (!fs_info->balance_ctl) {
3429 ret = -ENOTCONN;
3430 goto out_bargs;
3431 }
3432
3433 bctl = fs_info->balance_ctl;
3434 spin_lock(&fs_info->balance_lock);
3435 bctl->flags |= BTRFS_BALANCE_RESUME;
3436 spin_unlock(&fs_info->balance_lock);
3437
3438 goto do_balance;
3439 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003440 } else {
3441 bargs = NULL;
3442 }
3443
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003444 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
3445 1)) {
3446 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003447 ret = -EINPROGRESS;
3448 goto out_bargs;
3449 }
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003450 need_to_clear_lock = 1;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003451
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003452 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3453 if (!bctl) {
3454 ret = -ENOMEM;
3455 goto out_bargs;
3456 }
3457
3458 bctl->fs_info = fs_info;
3459 if (arg) {
3460 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3461 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3462 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3463
3464 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003465 } else {
3466 /* balance everything - no filters */
3467 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003468 }
3469
Ilya Dryomovde322262012-01-16 22:04:49 +02003470do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003471 ret = btrfs_balance(bctl, bargs);
3472 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003473 * bctl is freed in __cancel_balance or in free_fs_info if
3474 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003475 */
3476 if (arg) {
3477 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3478 ret = -EFAULT;
3479 }
3480
3481out_bargs:
3482 kfree(bargs);
3483out:
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003484 if (need_to_clear_lock)
3485 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
3486 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003487 mutex_unlock(&fs_info->balance_mutex);
3488 mutex_unlock(&fs_info->volume_mutex);
Liu Boe54bfa32012-06-29 03:58:48 -06003489 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003490 return ret;
3491}
3492
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003493static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3494{
3495 if (!capable(CAP_SYS_ADMIN))
3496 return -EPERM;
3497
3498 switch (cmd) {
3499 case BTRFS_BALANCE_CTL_PAUSE:
3500 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003501 case BTRFS_BALANCE_CTL_CANCEL:
3502 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003503 }
3504
3505 return -EINVAL;
3506}
3507
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003508static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3509 void __user *arg)
3510{
3511 struct btrfs_fs_info *fs_info = root->fs_info;
3512 struct btrfs_ioctl_balance_args *bargs;
3513 int ret = 0;
3514
3515 if (!capable(CAP_SYS_ADMIN))
3516 return -EPERM;
3517
3518 mutex_lock(&fs_info->balance_mutex);
3519 if (!fs_info->balance_ctl) {
3520 ret = -ENOTCONN;
3521 goto out;
3522 }
3523
3524 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3525 if (!bargs) {
3526 ret = -ENOMEM;
3527 goto out;
3528 }
3529
3530 update_ioctl_balance_args(fs_info, 1, bargs);
3531
3532 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3533 ret = -EFAULT;
3534
3535 kfree(bargs);
3536out:
3537 mutex_unlock(&fs_info->balance_mutex);
3538 return ret;
3539}
3540
Arne Jansen5d13a372011-09-14 15:53:51 +02003541static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3542{
3543 struct btrfs_ioctl_quota_ctl_args *sa;
3544 struct btrfs_trans_handle *trans = NULL;
3545 int ret;
3546 int err;
3547
3548 if (!capable(CAP_SYS_ADMIN))
3549 return -EPERM;
3550
3551 if (root->fs_info->sb->s_flags & MS_RDONLY)
3552 return -EROFS;
3553
3554 sa = memdup_user(arg, sizeof(*sa));
3555 if (IS_ERR(sa))
3556 return PTR_ERR(sa);
3557
3558 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3559 trans = btrfs_start_transaction(root, 2);
3560 if (IS_ERR(trans)) {
3561 ret = PTR_ERR(trans);
3562 goto out;
3563 }
3564 }
3565
3566 switch (sa->cmd) {
3567 case BTRFS_QUOTA_CTL_ENABLE:
3568 ret = btrfs_quota_enable(trans, root->fs_info);
3569 break;
3570 case BTRFS_QUOTA_CTL_DISABLE:
3571 ret = btrfs_quota_disable(trans, root->fs_info);
3572 break;
3573 case BTRFS_QUOTA_CTL_RESCAN:
3574 ret = btrfs_quota_rescan(root->fs_info);
3575 break;
3576 default:
3577 ret = -EINVAL;
3578 break;
3579 }
3580
3581 if (copy_to_user(arg, sa, sizeof(*sa)))
3582 ret = -EFAULT;
3583
3584 if (trans) {
3585 err = btrfs_commit_transaction(trans, root);
3586 if (err && !ret)
3587 ret = err;
3588 }
3589
3590out:
3591 kfree(sa);
3592 return ret;
3593}
3594
3595static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3596{
3597 struct btrfs_ioctl_qgroup_assign_args *sa;
3598 struct btrfs_trans_handle *trans;
3599 int ret;
3600 int err;
3601
3602 if (!capable(CAP_SYS_ADMIN))
3603 return -EPERM;
3604
3605 if (root->fs_info->sb->s_flags & MS_RDONLY)
3606 return -EROFS;
3607
3608 sa = memdup_user(arg, sizeof(*sa));
3609 if (IS_ERR(sa))
3610 return PTR_ERR(sa);
3611
3612 trans = btrfs_join_transaction(root);
3613 if (IS_ERR(trans)) {
3614 ret = PTR_ERR(trans);
3615 goto out;
3616 }
3617
3618 /* FIXME: check if the IDs really exist */
3619 if (sa->assign) {
3620 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3621 sa->src, sa->dst);
3622 } else {
3623 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3624 sa->src, sa->dst);
3625 }
3626
3627 err = btrfs_end_transaction(trans, root);
3628 if (err && !ret)
3629 ret = err;
3630
3631out:
3632 kfree(sa);
3633 return ret;
3634}
3635
3636static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3637{
3638 struct btrfs_ioctl_qgroup_create_args *sa;
3639 struct btrfs_trans_handle *trans;
3640 int ret;
3641 int err;
3642
3643 if (!capable(CAP_SYS_ADMIN))
3644 return -EPERM;
3645
3646 if (root->fs_info->sb->s_flags & MS_RDONLY)
3647 return -EROFS;
3648
3649 sa = memdup_user(arg, sizeof(*sa));
3650 if (IS_ERR(sa))
3651 return PTR_ERR(sa);
3652
3653 trans = btrfs_join_transaction(root);
3654 if (IS_ERR(trans)) {
3655 ret = PTR_ERR(trans);
3656 goto out;
3657 }
3658
3659 /* FIXME: check if the IDs really exist */
3660 if (sa->create) {
3661 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3662 NULL);
3663 } else {
3664 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3665 }
3666
3667 err = btrfs_end_transaction(trans, root);
3668 if (err && !ret)
3669 ret = err;
3670
3671out:
3672 kfree(sa);
3673 return ret;
3674}
3675
3676static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3677{
3678 struct btrfs_ioctl_qgroup_limit_args *sa;
3679 struct btrfs_trans_handle *trans;
3680 int ret;
3681 int err;
3682 u64 qgroupid;
3683
3684 if (!capable(CAP_SYS_ADMIN))
3685 return -EPERM;
3686
3687 if (root->fs_info->sb->s_flags & MS_RDONLY)
3688 return -EROFS;
3689
3690 sa = memdup_user(arg, sizeof(*sa));
3691 if (IS_ERR(sa))
3692 return PTR_ERR(sa);
3693
3694 trans = btrfs_join_transaction(root);
3695 if (IS_ERR(trans)) {
3696 ret = PTR_ERR(trans);
3697 goto out;
3698 }
3699
3700 qgroupid = sa->qgroupid;
3701 if (!qgroupid) {
3702 /* take the current subvol as qgroup */
3703 qgroupid = root->root_key.objectid;
3704 }
3705
3706 /* FIXME: check if the IDs really exist */
3707 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3708
3709 err = btrfs_end_transaction(trans, root);
3710 if (err && !ret)
3711 ret = err;
3712
3713out:
3714 kfree(sa);
3715 return ret;
3716}
3717
Alexander Block8ea05e32012-07-25 17:35:53 +02003718static long btrfs_ioctl_set_received_subvol(struct file *file,
3719 void __user *arg)
3720{
3721 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3722 struct inode *inode = fdentry(file)->d_inode;
3723 struct btrfs_root *root = BTRFS_I(inode)->root;
3724 struct btrfs_root_item *root_item = &root->root_item;
3725 struct btrfs_trans_handle *trans;
3726 struct timespec ct = CURRENT_TIME;
3727 int ret = 0;
3728
3729 ret = mnt_want_write_file(file);
3730 if (ret < 0)
3731 return ret;
3732
3733 down_write(&root->fs_info->subvol_sem);
3734
3735 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3736 ret = -EINVAL;
3737 goto out;
3738 }
3739
3740 if (btrfs_root_readonly(root)) {
3741 ret = -EROFS;
3742 goto out;
3743 }
3744
3745 if (!inode_owner_or_capable(inode)) {
3746 ret = -EACCES;
3747 goto out;
3748 }
3749
3750 sa = memdup_user(arg, sizeof(*sa));
3751 if (IS_ERR(sa)) {
3752 ret = PTR_ERR(sa);
3753 sa = NULL;
3754 goto out;
3755 }
3756
3757 trans = btrfs_start_transaction(root, 1);
3758 if (IS_ERR(trans)) {
3759 ret = PTR_ERR(trans);
3760 trans = NULL;
3761 goto out;
3762 }
3763
3764 sa->rtransid = trans->transid;
3765 sa->rtime.sec = ct.tv_sec;
3766 sa->rtime.nsec = ct.tv_nsec;
3767
3768 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3769 btrfs_set_root_stransid(root_item, sa->stransid);
3770 btrfs_set_root_rtransid(root_item, sa->rtransid);
3771 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3772 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3773 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3774 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3775
3776 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3777 &root->root_key, &root->root_item);
3778 if (ret < 0) {
3779 btrfs_end_transaction(trans, root);
3780 trans = NULL;
3781 goto out;
3782 } else {
3783 ret = btrfs_commit_transaction(trans, root);
3784 if (ret < 0)
3785 goto out;
3786 }
3787
3788 ret = copy_to_user(arg, sa, sizeof(*sa));
3789 if (ret)
3790 ret = -EFAULT;
3791
3792out:
3793 kfree(sa);
3794 up_write(&root->fs_info->subvol_sem);
3795 mnt_drop_write_file(file);
3796 return ret;
3797}
3798
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003799long btrfs_ioctl(struct file *file, unsigned int
3800 cmd, unsigned long arg)
3801{
3802 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003803 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003804
3805 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003806 case FS_IOC_GETFLAGS:
3807 return btrfs_ioctl_getflags(file, argp);
3808 case FS_IOC_SETFLAGS:
3809 return btrfs_ioctl_setflags(file, argp);
3810 case FS_IOC_GETVERSION:
3811 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003812 case FITRIM:
3813 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003814 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003815 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003816 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003817 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003818 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003819 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02003820 case BTRFS_IOC_SUBVOL_CREATE_V2:
3821 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003822 case BTRFS_IOC_SNAP_DESTROY:
3823 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003824 case BTRFS_IOC_SUBVOL_GETFLAGS:
3825 return btrfs_ioctl_subvol_getflags(file, argp);
3826 case BTRFS_IOC_SUBVOL_SETFLAGS:
3827 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003828 case BTRFS_IOC_DEFAULT_SUBVOL:
3829 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003830 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003831 return btrfs_ioctl_defrag(file, NULL);
3832 case BTRFS_IOC_DEFRAG_RANGE:
3833 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003834 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003835 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003836 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003837 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003838 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003839 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003840 case BTRFS_IOC_FS_INFO:
3841 return btrfs_ioctl_fs_info(root, argp);
3842 case BTRFS_IOC_DEV_INFO:
3843 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003844 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003845 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003846 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003847 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3848 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003849 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003850 case BTRFS_IOC_TRANS_START:
3851 return btrfs_ioctl_trans_start(file);
3852 case BTRFS_IOC_TRANS_END:
3853 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003854 case BTRFS_IOC_TREE_SEARCH:
3855 return btrfs_ioctl_tree_search(file, argp);
3856 case BTRFS_IOC_INO_LOOKUP:
3857 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003858 case BTRFS_IOC_INO_PATHS:
3859 return btrfs_ioctl_ino_to_path(root, argp);
3860 case BTRFS_IOC_LOGICAL_INO:
3861 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003862 case BTRFS_IOC_SPACE_INFO:
3863 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003864 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003865 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3866 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003867 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00003868 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04003869 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00003870 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003871 case BTRFS_IOC_SCRUB:
3872 return btrfs_ioctl_scrub(root, argp);
3873 case BTRFS_IOC_SCRUB_CANCEL:
3874 return btrfs_ioctl_scrub_cancel(root, argp);
3875 case BTRFS_IOC_SCRUB_PROGRESS:
3876 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003877 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003878 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003879 case BTRFS_IOC_BALANCE_CTL:
3880 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003881 case BTRFS_IOC_BALANCE_PROGRESS:
3882 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02003883 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3884 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02003885 case BTRFS_IOC_SEND:
3886 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003887 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06003888 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003889 case BTRFS_IOC_QUOTA_CTL:
3890 return btrfs_ioctl_quota_ctl(root, argp);
3891 case BTRFS_IOC_QGROUP_ASSIGN:
3892 return btrfs_ioctl_qgroup_assign(root, argp);
3893 case BTRFS_IOC_QGROUP_CREATE:
3894 return btrfs_ioctl_qgroup_create(root, argp);
3895 case BTRFS_IOC_QGROUP_LIMIT:
3896 return btrfs_ioctl_qgroup_limit(root, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003897 case BTRFS_IOC_DEV_REPLACE:
3898 return btrfs_ioctl_dev_replace(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003899 }
3900
3901 return -ENOTTY;
3902}