blob: d4608ab72b791682803da6761e52335cb367f31e [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 Zefanf062abf02011-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 Zefanf062abf02011-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 Zefanf062abf02011-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 Zefanf062abf02011-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 Zefanf062abf02011-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);
liubo2d4e6f6ad2011-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 Bacikd82a6f1d2011-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
Chris Mason9c520572012-12-17 14:26:57 -0500713 /*
714 * even if this name doesn't exist, we may get hash collisions.
715 * check for them now when we can safely fail
716 */
717 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
718 dir->i_ino, name,
719 namelen);
720 if (error)
721 goto out_dput;
722
Yan, Zheng76dda932009-09-21 16:00:26 -0400723 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
724
725 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
726 goto out_up_read;
727
Chris Mason3de45862008-11-17 21:02:50 -0500728 if (snap_src) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200729 error = create_snapshot(snap_src, dentry, name, namelen,
730 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500731 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400732 error = create_subvol(BTRFS_I(dir)->root, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200733 name, namelen, async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500734 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400735 if (!error)
736 fsnotify_mkdir(dir, dentry);
737out_up_read:
738 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400739out_dput:
740 dput(dentry);
741out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400742 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400743 return error;
744}
745
Chris Mason4cb53002011-05-24 15:35:30 -0400746/*
747 * When we're defragging a range, we don't want to kick it off again
748 * if it is really just waiting for delalloc to send it down.
749 * If we find a nice big extent or delalloc range for the bytes in the
750 * file you want to defrag, we return 0 to let you know to skip this
751 * part of the file
752 */
753static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
754{
755 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
756 struct extent_map *em = NULL;
757 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
758 u64 end;
759
760 read_lock(&em_tree->lock);
761 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
762 read_unlock(&em_tree->lock);
763
764 if (em) {
765 end = extent_map_end(em);
766 free_extent_map(em);
767 if (end - offset > thresh)
768 return 0;
769 }
770 /* if we already have a nice delalloc here, just stop */
771 thresh /= 2;
772 end = count_range_bits(io_tree, &offset, offset + thresh,
773 thresh, EXTENT_DELALLOC, 1);
774 if (end >= thresh)
775 return 0;
776 return 1;
777}
778
779/*
780 * helper function to walk through a file and find extents
781 * newer than a specific transid, and smaller than thresh.
782 *
783 * This is used by the defragging code to find new and small
784 * extents
785 */
786static int find_new_extents(struct btrfs_root *root,
787 struct inode *inode, u64 newer_than,
788 u64 *off, int thresh)
789{
790 struct btrfs_path *path;
791 struct btrfs_key min_key;
792 struct btrfs_key max_key;
793 struct extent_buffer *leaf;
794 struct btrfs_file_extent_item *extent;
795 int type;
796 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000797 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400798
799 path = btrfs_alloc_path();
800 if (!path)
801 return -ENOMEM;
802
David Sterbaa4689d22011-05-31 17:08:14 +0000803 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400804 min_key.type = BTRFS_EXTENT_DATA_KEY;
805 min_key.offset = *off;
806
David Sterbaa4689d22011-05-31 17:08:14 +0000807 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400808 max_key.type = (u8)-1;
809 max_key.offset = (u64)-1;
810
811 path->keep_locks = 1;
812
813 while(1) {
814 ret = btrfs_search_forward(root, &min_key, &max_key,
815 path, 0, newer_than);
816 if (ret != 0)
817 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000818 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400819 goto none;
820 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
821 goto none;
822
823 leaf = path->nodes[0];
824 extent = btrfs_item_ptr(leaf, path->slots[0],
825 struct btrfs_file_extent_item);
826
827 type = btrfs_file_extent_type(leaf, extent);
828 if (type == BTRFS_FILE_EXTENT_REG &&
829 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
830 check_defrag_in_cache(inode, min_key.offset, thresh)) {
831 *off = min_key.offset;
832 btrfs_free_path(path);
833 return 0;
834 }
835
836 if (min_key.offset == (u64)-1)
837 goto none;
838
839 min_key.offset++;
840 btrfs_release_path(path);
841 }
842none:
843 btrfs_free_path(path);
844 return -ENOENT;
845}
846
Li Zefan6c282eb2012-06-11 16:03:35 +0800847static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400848{
849 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500850 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800851 struct extent_map *em;
852 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500853
854 /*
855 * hopefully we have this extent in the tree already, try without
856 * the full extent lock
857 */
858 read_lock(&em_tree->lock);
859 em = lookup_extent_mapping(em_tree, start, len);
860 read_unlock(&em_tree->lock);
861
862 if (!em) {
863 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100864 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500865 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100866 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500867
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000868 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800869 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500870 }
871
Li Zefan6c282eb2012-06-11 16:03:35 +0800872 return em;
873}
874
875static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
876{
877 struct extent_map *next;
878 bool ret = true;
879
880 /* this is the last extent */
881 if (em->start + em->len >= i_size_read(inode))
882 return false;
883
884 next = defrag_lookup_extent(inode, em->start + em->len);
885 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
886 ret = false;
887
888 free_extent_map(next);
889 return ret;
890}
891
892static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400893 u64 *last_len, u64 *skip, u64 *defrag_end,
894 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800895{
896 struct extent_map *em;
897 int ret = 1;
898 bool next_mergeable = true;
899
900 /*
901 * make sure that once we start defragging an extent, we keep on
902 * defragging it
903 */
904 if (start < *defrag_end)
905 return 1;
906
907 *skip = 0;
908
909 em = defrag_lookup_extent(inode, start);
910 if (!em)
911 return 0;
912
Chris Mason940100a2010-03-10 10:52:59 -0500913 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400914 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500915 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400916 goto out;
917 }
918
Li Zefan6c282eb2012-06-11 16:03:35 +0800919 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500920
921 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800922 * we hit a real extent, if it is big or the next extent is not a
923 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500924 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400925 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800926 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500927 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400928out:
Chris Mason940100a2010-03-10 10:52:59 -0500929 /*
930 * last_len ends up being a counter of how many bytes we've defragged.
931 * every time we choose not to defrag an extent, we reset *last_len
932 * so that the next tiny extent will force a defrag.
933 *
934 * The end result of this is that tiny extents before a single big
935 * extent will force at least part of that big extent to be defragged.
936 */
937 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500938 *defrag_end = extent_map_end(em);
939 } else {
940 *last_len = 0;
941 *skip = extent_map_end(em);
942 *defrag_end = 0;
943 }
944
945 free_extent_map(em);
946 return ret;
947}
948
Chris Mason4cb53002011-05-24 15:35:30 -0400949/*
950 * it doesn't do much good to defrag one or two pages
951 * at a time. This pulls in a nice chunk of pages
952 * to COW and defrag.
953 *
954 * It also makes sure the delalloc code has enough
955 * dirty data to avoid making new small extents as part
956 * of the defrag
957 *
958 * It's a good idea to start RA on this range
959 * before calling this.
960 */
961static int cluster_pages_for_defrag(struct inode *inode,
962 struct page **pages,
963 unsigned long start_index,
964 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400965{
Chris Mason4cb53002011-05-24 15:35:30 -0400966 unsigned long file_end;
967 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400968 u64 page_start;
969 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400970 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400971 int ret;
972 int i;
973 int i_done;
974 struct btrfs_ordered_extent *ordered;
975 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800976 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400977 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400978
Chris Mason4cb53002011-05-24 15:35:30 -0400979 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400980 if (!isize || start_index > file_end)
981 return 0;
982
983 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400984
985 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400986 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400987 if (ret)
988 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400989 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800990 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400991
992 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400993 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400994 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800995again:
Josef Bacika94733d2011-07-11 10:47:06 -0400996 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800997 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400998 if (!page)
999 break;
1000
Miao Xie600a45e2012-02-16 15:01:24 +08001001 page_start = page_offset(page);
1002 page_end = page_start + PAGE_CACHE_SIZE - 1;
1003 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001004 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001005 ordered = btrfs_lookup_ordered_extent(inode,
1006 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001007 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001008 if (!ordered)
1009 break;
1010
1011 unlock_page(page);
1012 btrfs_start_ordered_extent(inode, ordered, 1);
1013 btrfs_put_ordered_extent(ordered);
1014 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001015 /*
1016 * we unlocked the page above, so we need check if
1017 * it was released or not.
1018 */
1019 if (page->mapping != inode->i_mapping) {
1020 unlock_page(page);
1021 page_cache_release(page);
1022 goto again;
1023 }
Miao Xie600a45e2012-02-16 15:01:24 +08001024 }
1025
Chris Mason4cb53002011-05-24 15:35:30 -04001026 if (!PageUptodate(page)) {
1027 btrfs_readpage(NULL, page);
1028 lock_page(page);
1029 if (!PageUptodate(page)) {
1030 unlock_page(page);
1031 page_cache_release(page);
1032 ret = -EIO;
1033 break;
1034 }
1035 }
Miao Xie600a45e2012-02-16 15:01:24 +08001036
Miao Xie600a45e2012-02-16 15:01:24 +08001037 if (page->mapping != inode->i_mapping) {
1038 unlock_page(page);
1039 page_cache_release(page);
1040 goto again;
1041 }
1042
Chris Mason4cb53002011-05-24 15:35:30 -04001043 pages[i] = page;
1044 i_done++;
1045 }
1046 if (!i_done || ret)
1047 goto out;
1048
1049 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1050 goto out;
1051
1052 /*
1053 * so now we have a nice long stream of locked
1054 * and up to date pages, lets wait on them
1055 */
1056 for (i = 0; i < i_done; i++)
1057 wait_on_page_writeback(pages[i]);
1058
1059 page_start = page_offset(pages[0]);
1060 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1061
1062 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001063 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001064 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1065 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001066 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1067 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001068
Liu Bo1f12bd02012-03-29 09:57:44 -04001069 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001070 spin_lock(&BTRFS_I(inode)->lock);
1071 BTRFS_I(inode)->outstanding_extents++;
1072 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001073 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001074 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001075 }
1076
1077
Liu Bo9e8a4a82012-09-05 19:10:51 -06001078 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1079 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001080
1081 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1082 page_start, page_end - 1, &cached_state,
1083 GFP_NOFS);
1084
1085 for (i = 0; i < i_done; i++) {
1086 clear_page_dirty_for_io(pages[i]);
1087 ClearPageChecked(pages[i]);
1088 set_page_extent_mapped(pages[i]);
1089 set_page_dirty(pages[i]);
1090 unlock_page(pages[i]);
1091 page_cache_release(pages[i]);
1092 }
1093 return i_done;
1094out:
1095 for (i = 0; i < i_done; i++) {
1096 unlock_page(pages[i]);
1097 page_cache_release(pages[i]);
1098 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001099 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001100 return ret;
1101
1102}
1103
1104int btrfs_defrag_file(struct inode *inode, struct file *file,
1105 struct btrfs_ioctl_defrag_range_args *range,
1106 u64 newer_than, unsigned long max_to_defrag)
1107{
1108 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001109 struct file_ra_state *ra = NULL;
1110 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001111 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001112 u64 last_len = 0;
1113 u64 skip = 0;
1114 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001115 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001116 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001117 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001118 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001119 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001120 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001121 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001122 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1123 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001124 u64 new_align = ~((u64)128 * 1024 - 1);
1125 struct page **pages = NULL;
1126
1127 if (extent_thresh == 0)
1128 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001129
1130 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1131 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1132 return -EINVAL;
1133 if (range->compress_type)
1134 compress_type = range->compress_type;
1135 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001136
Li Zefan151a31b2011-09-02 15:56:39 +08001137 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001138 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001139
Chris Mason4cb53002011-05-24 15:35:30 -04001140 /*
1141 * if we were not given a file, allocate a readahead
1142 * context
1143 */
1144 if (!file) {
1145 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1146 if (!ra)
1147 return -ENOMEM;
1148 file_ra_state_init(ra, inode->i_mapping);
1149 } else {
1150 ra = &file->f_ra;
1151 }
1152
Li Zefan008873e2011-09-02 15:57:07 +08001153 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001154 GFP_NOFS);
1155 if (!pages) {
1156 ret = -ENOMEM;
1157 goto out_ra;
1158 }
1159
1160 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001161 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001162 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001163 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1164 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001165 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001166 }
1167
Chris Mason4cb53002011-05-24 15:35:30 -04001168 if (newer_than) {
1169 ret = find_new_extents(root, inode, newer_than,
1170 &newer_off, 64 * 1024);
1171 if (!ret) {
1172 range->start = newer_off;
1173 /*
1174 * we always align our defrag to help keep
1175 * the extents in the file evenly spaced
1176 */
1177 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001178 } else
1179 goto out_ra;
1180 } else {
1181 i = range->start >> PAGE_CACHE_SHIFT;
1182 }
1183 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001184 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001185
Li Zefan2a0f7f52011-10-10 15:43:34 -04001186 /*
1187 * make writeback starts from i, so the defrag range can be
1188 * written sequentially.
1189 */
1190 if (i < inode->i_mapping->writeback_index)
1191 inode->i_mapping->writeback_index = i;
1192
Chris Masonf7f43cc2011-10-11 11:41:40 -04001193 while (i <= last_index && defrag_count < max_to_defrag &&
1194 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1195 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001196 /*
1197 * make sure we stop running if someone unmounts
1198 * the FS
1199 */
1200 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1201 break;
1202
Liu Bo66c26892012-03-29 09:57:45 -04001203 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001204 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001205 &defrag_end, range->flags &
1206 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001207 unsigned long next;
1208 /*
1209 * the should_defrag function tells us how much to skip
1210 * bump our counter by the suggested amount
1211 */
1212 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1213 i = max(i + 1, next);
1214 continue;
1215 }
Li Zefan008873e2011-09-02 15:57:07 +08001216
1217 if (!newer_than) {
1218 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1219 PAGE_CACHE_SHIFT) - i;
1220 cluster = min(cluster, max_cluster);
1221 } else {
1222 cluster = max_cluster;
1223 }
1224
Chris Mason1e701a32010-03-11 09:42:04 -05001225 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001226 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001227
Li Zefan008873e2011-09-02 15:57:07 +08001228 if (i + cluster > ra_index) {
1229 ra_index = max(i, ra_index);
1230 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1231 cluster);
1232 ra_index += max_cluster;
1233 }
Chris Mason940100a2010-03-10 10:52:59 -05001234
Liu Boecb8bea2012-03-29 09:57:44 -04001235 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001236 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001237 if (ret < 0) {
1238 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001239 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001240 }
Chris Mason940100a2010-03-10 10:52:59 -05001241
Chris Mason4cb53002011-05-24 15:35:30 -04001242 defrag_count += ret;
1243 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001244 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001245
1246 if (newer_than) {
1247 if (newer_off == (u64)-1)
1248 break;
1249
Liu Boe1f041e2012-03-29 09:57:45 -04001250 if (ret > 0)
1251 i += ret;
1252
Chris Mason4cb53002011-05-24 15:35:30 -04001253 newer_off = max(newer_off + 1,
1254 (u64)i << PAGE_CACHE_SHIFT);
1255
1256 ret = find_new_extents(root, inode,
1257 newer_than, &newer_off,
1258 64 * 1024);
1259 if (!ret) {
1260 range->start = newer_off;
1261 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001262 } else {
1263 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001264 }
Chris Mason4cb53002011-05-24 15:35:30 -04001265 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001266 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001267 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001268 last_len += ret << PAGE_CACHE_SHIFT;
1269 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001270 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001271 last_len = 0;
1272 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001273 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001274 }
1275
Chris Mason1e701a32010-03-11 09:42:04 -05001276 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1277 filemap_flush(inode->i_mapping);
1278
1279 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1280 /* the filemap_flush will queue IO into the worker threads, but
1281 * we have to make sure the IO is actually started and that
1282 * ordered extents get created before we return
1283 */
1284 atomic_inc(&root->fs_info->async_submit_draining);
1285 while (atomic_read(&root->fs_info->nr_async_submits) ||
1286 atomic_read(&root->fs_info->async_delalloc_pages)) {
1287 wait_event(root->fs_info->async_submit_wait,
1288 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1289 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1290 }
1291 atomic_dec(&root->fs_info->async_submit_draining);
1292
1293 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001294 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001295 mutex_unlock(&inode->i_mutex);
1296 }
1297
Li Zefan1a419d82010-10-25 15:12:50 +08001298 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001299 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001300 }
1301
Diego Calleja60ccf822011-09-01 16:33:57 +02001302 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001303
Chris Mason4cb53002011-05-24 15:35:30 -04001304out_ra:
1305 if (!file)
1306 kfree(ra);
1307 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001308 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001309}
1310
Miao Xie198605a2012-11-26 08:43:45 +00001311static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001312 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001313{
1314 u64 new_size;
1315 u64 old_size;
1316 u64 devid = 1;
Miao Xie198605a2012-11-26 08:43:45 +00001317 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001318 struct btrfs_ioctl_vol_args *vol_args;
1319 struct btrfs_trans_handle *trans;
1320 struct btrfs_device *device = NULL;
1321 char *sizestr;
1322 char *devstr = NULL;
1323 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001324 int mod = 0;
1325
Yan Zhengc146afa2008-11-12 14:34:12 -05001326 if (root->fs_info->sb->s_flags & MS_RDONLY)
1327 return -EROFS;
1328
Chris Masone441d542009-01-05 16:57:23 -05001329 if (!capable(CAP_SYS_ADMIN))
1330 return -EPERM;
1331
Miao Xie198605a2012-11-26 08:43:45 +00001332 ret = mnt_want_write_file(file);
1333 if (ret)
1334 return ret;
1335
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001336 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1337 1)) {
1338 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1339 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001340 }
1341
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001342 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001343 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001344 if (IS_ERR(vol_args)) {
1345 ret = PTR_ERR(vol_args);
1346 goto out;
1347 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001348
1349 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001350
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001351 sizestr = vol_args->name;
1352 devstr = strchr(sizestr, ':');
1353 if (devstr) {
1354 char *end;
1355 sizestr = devstr + 1;
1356 *devstr = '\0';
1357 devstr = vol_args->name;
1358 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001359 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001360 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001361 }
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001362 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001363 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001364 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001365 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001366 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001367 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001368 }
Liu Bo4e42ae12012-06-14 02:23:19 -06001369 if (device->fs_devices && device->fs_devices->seeding) {
1370 printk(KERN_INFO "btrfs: resizer unable to apply on "
Chris Masona8c4a332012-06-15 20:07:17 -04001371 "seeding device %llu\n",
1372 (unsigned long long)devid);
Liu Bo4e42ae12012-06-14 02:23:19 -06001373 ret = -EINVAL;
1374 goto out_free;
1375 }
1376
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001377 if (!strcmp(sizestr, "max"))
1378 new_size = device->bdev->bd_inode->i_size;
1379 else {
1380 if (sizestr[0] == '-') {
1381 mod = -1;
1382 sizestr++;
1383 } else if (sizestr[0] == '+') {
1384 mod = 1;
1385 sizestr++;
1386 }
Akinobu Mita91748462010-02-28 10:59:11 +00001387 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001388 if (new_size == 0) {
1389 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001390 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001391 }
1392 }
1393
Stefan Behrens63a212a2012-11-05 18:29:28 +01001394 if (device->is_tgtdev_for_dev_replace) {
1395 ret = -EINVAL;
1396 goto out_free;
1397 }
1398
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001399 old_size = device->total_bytes;
1400
1401 if (mod < 0) {
1402 if (new_size > old_size) {
1403 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001404 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001405 }
1406 new_size = old_size - new_size;
1407 } else if (mod > 0) {
1408 new_size = old_size + new_size;
1409 }
1410
1411 if (new_size < 256 * 1024 * 1024) {
1412 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001413 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001414 }
1415 if (new_size > device->bdev->bd_inode->i_size) {
1416 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001417 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001418 }
1419
1420 do_div(new_size, root->sectorsize);
1421 new_size *= root->sectorsize;
1422
Josef Bacik606686e2012-06-04 14:03:51 -04001423 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1424 rcu_str_deref(device->name),
1425 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001426
1427 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001428 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001429 if (IS_ERR(trans)) {
1430 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001431 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001432 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001433 ret = btrfs_grow_device(trans, device, new_size);
1434 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001435 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001436 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001437 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001438
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001439out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001440 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001441out:
1442 mutex_unlock(&root->fs_info->volume_mutex);
Miao Xie198605a2012-11-26 08:43:45 +00001443 mnt_drop_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001444 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001445 return ret;
1446}
1447
Sage Weil72fd0322010-10-29 15:41:32 -04001448static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001449 char *name, unsigned long fd, int subvol,
1450 u64 *transid, bool readonly,
1451 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001452{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001453 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001454 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001455
Liu Boa874a632012-06-29 03:58:46 -06001456 ret = mnt_want_write_file(file);
1457 if (ret)
1458 goto out;
1459
Sage Weil72fd0322010-10-29 15:41:32 -04001460 namelen = strlen(name);
1461 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001462 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001463 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001464 }
1465
Chris Mason16780ca2012-02-20 22:14:55 -05001466 if (name[0] == '.' &&
1467 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1468 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001469 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001470 }
1471
Chris Mason3de45862008-11-17 21:02:50 -05001472 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001473 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001474 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001475 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001476 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001477 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001478 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001479 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001480 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001481 }
1482
Al Viro2903ff02012-08-28 12:52:22 -04001483 src_inode = src.file->f_path.dentry->d_inode;
Chris Mason3de45862008-11-17 21:02:50 -05001484 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001485 printk(KERN_INFO "btrfs: Snapshot src from "
1486 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001487 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001488 } else {
1489 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1490 BTRFS_I(src_inode)->root,
1491 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001492 }
Al Viro2903ff02012-08-28 12:52:22 -04001493 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001494 }
Liu Boa874a632012-06-29 03:58:46 -06001495out_drop_write:
1496 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001497out:
Sage Weil72fd0322010-10-29 15:41:32 -04001498 return ret;
1499}
1500
1501static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001502 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001503{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001504 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001505 int ret;
1506
Li Zefanfa0d2b92010-12-20 15:53:28 +08001507 vol_args = memdup_user(arg, sizeof(*vol_args));
1508 if (IS_ERR(vol_args))
1509 return PTR_ERR(vol_args);
1510 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001511
Li Zefanfa0d2b92010-12-20 15:53:28 +08001512 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001513 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001514 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001515
Li Zefanfa0d2b92010-12-20 15:53:28 +08001516 kfree(vol_args);
1517 return ret;
1518}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001519
Li Zefanfa0d2b92010-12-20 15:53:28 +08001520static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1521 void __user *arg, int subvol)
1522{
1523 struct btrfs_ioctl_vol_args_v2 *vol_args;
1524 int ret;
1525 u64 transid = 0;
1526 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001527 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001528 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001529
Li Zefanfa0d2b92010-12-20 15:53:28 +08001530 vol_args = memdup_user(arg, sizeof(*vol_args));
1531 if (IS_ERR(vol_args))
1532 return PTR_ERR(vol_args);
1533 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001534
Li Zefanb83cc962010-12-20 16:04:08 +08001535 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001536 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1537 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001538 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001539 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001540 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001541
1542 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1543 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001544 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1545 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001546 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1547 if (vol_args->size > PAGE_CACHE_SIZE) {
1548 ret = -EINVAL;
1549 goto out;
1550 }
1551 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1552 if (IS_ERR(inherit)) {
1553 ret = PTR_ERR(inherit);
1554 goto out;
1555 }
1556 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001557
1558 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001559 vol_args->fd, subvol, ptr,
1560 readonly, &inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001561
1562 if (ret == 0 && ptr &&
1563 copy_to_user(arg +
1564 offsetof(struct btrfs_ioctl_vol_args_v2,
1565 transid), ptr, sizeof(*ptr)))
1566 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001567out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001568 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001569 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001570 return ret;
1571}
1572
Li Zefan0caa1022010-12-20 16:30:25 +08001573static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1574 void __user *arg)
1575{
1576 struct inode *inode = fdentry(file)->d_inode;
1577 struct btrfs_root *root = BTRFS_I(inode)->root;
1578 int ret = 0;
1579 u64 flags = 0;
1580
Li Zefan33345d012011-04-20 10:31:50 +08001581 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001582 return -EINVAL;
1583
1584 down_read(&root->fs_info->subvol_sem);
1585 if (btrfs_root_readonly(root))
1586 flags |= BTRFS_SUBVOL_RDONLY;
1587 up_read(&root->fs_info->subvol_sem);
1588
1589 if (copy_to_user(arg, &flags, sizeof(flags)))
1590 ret = -EFAULT;
1591
1592 return ret;
1593}
1594
1595static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1596 void __user *arg)
1597{
1598 struct inode *inode = fdentry(file)->d_inode;
1599 struct btrfs_root *root = BTRFS_I(inode)->root;
1600 struct btrfs_trans_handle *trans;
1601 u64 root_flags;
1602 u64 flags;
1603 int ret = 0;
1604
Liu Bob9ca0662012-06-29 03:58:49 -06001605 ret = mnt_want_write_file(file);
1606 if (ret)
1607 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001608
Liu Bob9ca0662012-06-29 03:58:49 -06001609 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1610 ret = -EINVAL;
1611 goto out_drop_write;
1612 }
Li Zefan0caa1022010-12-20 16:30:25 +08001613
Liu Bob9ca0662012-06-29 03:58:49 -06001614 if (copy_from_user(&flags, arg, sizeof(flags))) {
1615 ret = -EFAULT;
1616 goto out_drop_write;
1617 }
Li Zefan0caa1022010-12-20 16:30:25 +08001618
Liu Bob9ca0662012-06-29 03:58:49 -06001619 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1620 ret = -EINVAL;
1621 goto out_drop_write;
1622 }
Li Zefan0caa1022010-12-20 16:30:25 +08001623
Liu Bob9ca0662012-06-29 03:58:49 -06001624 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1625 ret = -EOPNOTSUPP;
1626 goto out_drop_write;
1627 }
Li Zefan0caa1022010-12-20 16:30:25 +08001628
Liu Bob9ca0662012-06-29 03:58:49 -06001629 if (!inode_owner_or_capable(inode)) {
1630 ret = -EACCES;
1631 goto out_drop_write;
1632 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001633
Li Zefan0caa1022010-12-20 16:30:25 +08001634 down_write(&root->fs_info->subvol_sem);
1635
1636 /* nothing to do */
1637 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001638 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001639
1640 root_flags = btrfs_root_flags(&root->root_item);
1641 if (flags & BTRFS_SUBVOL_RDONLY)
1642 btrfs_set_root_flags(&root->root_item,
1643 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1644 else
1645 btrfs_set_root_flags(&root->root_item,
1646 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1647
1648 trans = btrfs_start_transaction(root, 1);
1649 if (IS_ERR(trans)) {
1650 ret = PTR_ERR(trans);
1651 goto out_reset;
1652 }
1653
Li Zefanb4dc2b82011-02-16 06:06:34 +00001654 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001655 &root->root_key, &root->root_item);
1656
1657 btrfs_commit_transaction(trans, root);
1658out_reset:
1659 if (ret)
1660 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001661out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001662 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001663out_drop_write:
1664 mnt_drop_write_file(file);
1665out:
Li Zefan0caa1022010-12-20 16:30:25 +08001666 return ret;
1667}
1668
Yan, Zheng76dda932009-09-21 16:00:26 -04001669/*
1670 * helper to check if the subvolume references other subvolumes
1671 */
1672static noinline int may_destroy_subvol(struct btrfs_root *root)
1673{
1674 struct btrfs_path *path;
1675 struct btrfs_key key;
1676 int ret;
1677
1678 path = btrfs_alloc_path();
1679 if (!path)
1680 return -ENOMEM;
1681
1682 key.objectid = root->root_key.objectid;
1683 key.type = BTRFS_ROOT_REF_KEY;
1684 key.offset = (u64)-1;
1685
1686 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1687 &key, path, 0, 0);
1688 if (ret < 0)
1689 goto out;
1690 BUG_ON(ret == 0);
1691
1692 ret = 0;
1693 if (path->slots[0] > 0) {
1694 path->slots[0]--;
1695 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1696 if (key.objectid == root->root_key.objectid &&
1697 key.type == BTRFS_ROOT_REF_KEY)
1698 ret = -ENOTEMPTY;
1699 }
1700out:
1701 btrfs_free_path(path);
1702 return ret;
1703}
1704
Chris Masonac8e9812010-02-28 15:39:26 -05001705static noinline int key_in_sk(struct btrfs_key *key,
1706 struct btrfs_ioctl_search_key *sk)
1707{
Chris Masonabc6e132010-03-18 12:10:08 -04001708 struct btrfs_key test;
1709 int ret;
1710
1711 test.objectid = sk->min_objectid;
1712 test.type = sk->min_type;
1713 test.offset = sk->min_offset;
1714
1715 ret = btrfs_comp_cpu_keys(key, &test);
1716 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001717 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001718
1719 test.objectid = sk->max_objectid;
1720 test.type = sk->max_type;
1721 test.offset = sk->max_offset;
1722
1723 ret = btrfs_comp_cpu_keys(key, &test);
1724 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001725 return 0;
1726 return 1;
1727}
1728
1729static noinline int copy_to_sk(struct btrfs_root *root,
1730 struct btrfs_path *path,
1731 struct btrfs_key *key,
1732 struct btrfs_ioctl_search_key *sk,
1733 char *buf,
1734 unsigned long *sk_offset,
1735 int *num_found)
1736{
1737 u64 found_transid;
1738 struct extent_buffer *leaf;
1739 struct btrfs_ioctl_search_header sh;
1740 unsigned long item_off;
1741 unsigned long item_len;
1742 int nritems;
1743 int i;
1744 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001745 int ret = 0;
1746
1747 leaf = path->nodes[0];
1748 slot = path->slots[0];
1749 nritems = btrfs_header_nritems(leaf);
1750
1751 if (btrfs_header_generation(leaf) > sk->max_transid) {
1752 i = nritems;
1753 goto advance_key;
1754 }
1755 found_transid = btrfs_header_generation(leaf);
1756
1757 for (i = slot; i < nritems; i++) {
1758 item_off = btrfs_item_ptr_offset(leaf, i);
1759 item_len = btrfs_item_size_nr(leaf, i);
1760
1761 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1762 item_len = 0;
1763
1764 if (sizeof(sh) + item_len + *sk_offset >
1765 BTRFS_SEARCH_ARGS_BUFSIZE) {
1766 ret = 1;
1767 goto overflow;
1768 }
1769
1770 btrfs_item_key_to_cpu(leaf, key, i);
1771 if (!key_in_sk(key, sk))
1772 continue;
1773
1774 sh.objectid = key->objectid;
1775 sh.offset = key->offset;
1776 sh.type = key->type;
1777 sh.len = item_len;
1778 sh.transid = found_transid;
1779
1780 /* copy search result header */
1781 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1782 *sk_offset += sizeof(sh);
1783
1784 if (item_len) {
1785 char *p = buf + *sk_offset;
1786 /* copy the item */
1787 read_extent_buffer(leaf, p,
1788 item_off, item_len);
1789 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001790 }
Hugo Millse2156862011-05-14 17:43:41 +00001791 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001792
1793 if (*num_found >= sk->nr_items)
1794 break;
1795 }
1796advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001797 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001798 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1799 key->offset++;
1800 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1801 key->offset = 0;
1802 key->type++;
1803 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1804 key->offset = 0;
1805 key->type = 0;
1806 key->objectid++;
1807 } else
1808 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001809overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001810 return ret;
1811}
1812
1813static noinline int search_ioctl(struct inode *inode,
1814 struct btrfs_ioctl_search_args *args)
1815{
1816 struct btrfs_root *root;
1817 struct btrfs_key key;
1818 struct btrfs_key max_key;
1819 struct btrfs_path *path;
1820 struct btrfs_ioctl_search_key *sk = &args->key;
1821 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1822 int ret;
1823 int num_found = 0;
1824 unsigned long sk_offset = 0;
1825
1826 path = btrfs_alloc_path();
1827 if (!path)
1828 return -ENOMEM;
1829
1830 if (sk->tree_id == 0) {
1831 /* search the root of the inode that was passed */
1832 root = BTRFS_I(inode)->root;
1833 } else {
1834 key.objectid = sk->tree_id;
1835 key.type = BTRFS_ROOT_ITEM_KEY;
1836 key.offset = (u64)-1;
1837 root = btrfs_read_fs_root_no_name(info, &key);
1838 if (IS_ERR(root)) {
1839 printk(KERN_ERR "could not find root %llu\n",
1840 sk->tree_id);
1841 btrfs_free_path(path);
1842 return -ENOENT;
1843 }
1844 }
1845
1846 key.objectid = sk->min_objectid;
1847 key.type = sk->min_type;
1848 key.offset = sk->min_offset;
1849
1850 max_key.objectid = sk->max_objectid;
1851 max_key.type = sk->max_type;
1852 max_key.offset = sk->max_offset;
1853
1854 path->keep_locks = 1;
1855
1856 while(1) {
1857 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1858 sk->min_transid);
1859 if (ret != 0) {
1860 if (ret > 0)
1861 ret = 0;
1862 goto err;
1863 }
1864 ret = copy_to_sk(root, path, &key, sk, args->buf,
1865 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001866 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001867 if (ret || num_found >= sk->nr_items)
1868 break;
1869
1870 }
1871 ret = 0;
1872err:
1873 sk->nr_items = num_found;
1874 btrfs_free_path(path);
1875 return ret;
1876}
1877
1878static noinline int btrfs_ioctl_tree_search(struct file *file,
1879 void __user *argp)
1880{
1881 struct btrfs_ioctl_search_args *args;
1882 struct inode *inode;
1883 int ret;
1884
1885 if (!capable(CAP_SYS_ADMIN))
1886 return -EPERM;
1887
Julia Lawall2354d08f2010-10-29 15:14:18 -04001888 args = memdup_user(argp, sizeof(*args));
1889 if (IS_ERR(args))
1890 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001891
Chris Masonac8e9812010-02-28 15:39:26 -05001892 inode = fdentry(file)->d_inode;
1893 ret = search_ioctl(inode, args);
1894 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1895 ret = -EFAULT;
1896 kfree(args);
1897 return ret;
1898}
1899
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001900/*
Chris Masonac8e9812010-02-28 15:39:26 -05001901 * Search INODE_REFs to identify path name of 'dirid' directory
1902 * in a 'tree_id' tree. and sets path name to 'name'.
1903 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001904static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1905 u64 tree_id, u64 dirid, char *name)
1906{
1907 struct btrfs_root *root;
1908 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001909 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001910 int ret = -1;
1911 int slot;
1912 int len;
1913 int total_len = 0;
1914 struct btrfs_inode_ref *iref;
1915 struct extent_buffer *l;
1916 struct btrfs_path *path;
1917
1918 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1919 name[0]='\0';
1920 return 0;
1921 }
1922
1923 path = btrfs_alloc_path();
1924 if (!path)
1925 return -ENOMEM;
1926
Chris Masonac8e9812010-02-28 15:39:26 -05001927 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001928
1929 key.objectid = tree_id;
1930 key.type = BTRFS_ROOT_ITEM_KEY;
1931 key.offset = (u64)-1;
1932 root = btrfs_read_fs_root_no_name(info, &key);
1933 if (IS_ERR(root)) {
1934 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001935 ret = -ENOENT;
1936 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001937 }
1938
1939 key.objectid = dirid;
1940 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001941 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001942
1943 while(1) {
1944 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1945 if (ret < 0)
1946 goto out;
1947
1948 l = path->nodes[0];
1949 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001950 if (ret > 0 && slot > 0)
1951 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001952 btrfs_item_key_to_cpu(l, &key, slot);
1953
1954 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001955 key.type != BTRFS_INODE_REF_KEY)) {
1956 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001957 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001958 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001959
1960 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1961 len = btrfs_inode_ref_name_len(l, iref);
1962 ptr -= len + 1;
1963 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001964 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001965 goto out;
1966
1967 *(ptr + len) = '/';
1968 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1969
1970 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1971 break;
1972
David Sterbab3b4aa72011-04-21 01:20:15 +02001973 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001974 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001975 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001976 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001977 }
Chris Masonac8e9812010-02-28 15:39:26 -05001978 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001979 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001980 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001981 name[total_len]='\0';
1982 ret = 0;
1983out:
1984 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001985 return ret;
1986}
1987
1988static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1989 void __user *argp)
1990{
1991 struct btrfs_ioctl_ino_lookup_args *args;
1992 struct inode *inode;
1993 int ret;
1994
1995 if (!capable(CAP_SYS_ADMIN))
1996 return -EPERM;
1997
Julia Lawall2354d08f2010-10-29 15:14:18 -04001998 args = memdup_user(argp, sizeof(*args));
1999 if (IS_ERR(args))
2000 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002001
Chris Masonac8e9812010-02-28 15:39:26 -05002002 inode = fdentry(file)->d_inode;
2003
Chris Mason1b53ac42010-03-18 12:17:05 -04002004 if (args->treeid == 0)
2005 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2006
Chris Masonac8e9812010-02-28 15:39:26 -05002007 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2008 args->treeid, args->objectid,
2009 args->name);
2010
2011 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2012 ret = -EFAULT;
2013
2014 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002015 return ret;
2016}
2017
Yan, Zheng76dda932009-09-21 16:00:26 -04002018static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2019 void __user *arg)
2020{
2021 struct dentry *parent = fdentry(file);
2022 struct dentry *dentry;
2023 struct inode *dir = parent->d_inode;
2024 struct inode *inode;
2025 struct btrfs_root *root = BTRFS_I(dir)->root;
2026 struct btrfs_root *dest = NULL;
2027 struct btrfs_ioctl_vol_args *vol_args;
2028 struct btrfs_trans_handle *trans;
2029 int namelen;
2030 int ret;
2031 int err = 0;
2032
Yan, Zheng76dda932009-09-21 16:00:26 -04002033 vol_args = memdup_user(arg, sizeof(*vol_args));
2034 if (IS_ERR(vol_args))
2035 return PTR_ERR(vol_args);
2036
2037 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2038 namelen = strlen(vol_args->name);
2039 if (strchr(vol_args->name, '/') ||
2040 strncmp(vol_args->name, "..", namelen) == 0) {
2041 err = -EINVAL;
2042 goto out;
2043 }
2044
Al Viroa561be72011-11-23 11:57:51 -05002045 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002046 if (err)
2047 goto out;
2048
2049 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2050 dentry = lookup_one_len(vol_args->name, parent, namelen);
2051 if (IS_ERR(dentry)) {
2052 err = PTR_ERR(dentry);
2053 goto out_unlock_dir;
2054 }
2055
2056 if (!dentry->d_inode) {
2057 err = -ENOENT;
2058 goto out_dput;
2059 }
2060
2061 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002062 dest = BTRFS_I(inode)->root;
2063 if (!capable(CAP_SYS_ADMIN)){
2064 /*
2065 * Regular user. Only allow this with a special mount
2066 * option, when the user has write+exec access to the
2067 * subvol root, and when rmdir(2) would have been
2068 * allowed.
2069 *
2070 * Note that this is _not_ check that the subvol is
2071 * empty or doesn't contain data that we wouldn't
2072 * otherwise be able to delete.
2073 *
2074 * Users who want to delete empty subvols should try
2075 * rmdir(2).
2076 */
2077 err = -EPERM;
2078 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2079 goto out_dput;
2080
2081 /*
2082 * Do not allow deletion if the parent dir is the same
2083 * as the dir to be deleted. That means the ioctl
2084 * must be called on the dentry referencing the root
2085 * of the subvol, not a random directory contained
2086 * within it.
2087 */
2088 err = -EINVAL;
2089 if (root == dest)
2090 goto out_dput;
2091
2092 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2093 if (err)
2094 goto out_dput;
2095
2096 /* check if subvolume may be deleted by a non-root user */
2097 err = btrfs_may_delete(dir, dentry, 1);
2098 if (err)
2099 goto out_dput;
2100 }
2101
Li Zefan33345d012011-04-20 10:31:50 +08002102 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002103 err = -EINVAL;
2104 goto out_dput;
2105 }
2106
Yan, Zheng76dda932009-09-21 16:00:26 -04002107 mutex_lock(&inode->i_mutex);
2108 err = d_invalidate(dentry);
2109 if (err)
2110 goto out_unlock;
2111
2112 down_write(&root->fs_info->subvol_sem);
2113
2114 err = may_destroy_subvol(dest);
2115 if (err)
2116 goto out_up_write;
2117
Yan, Zhenga22285a2010-05-16 10:48:46 -04002118 trans = btrfs_start_transaction(root, 0);
2119 if (IS_ERR(trans)) {
2120 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002121 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002122 }
2123 trans->block_rsv = &root->fs_info->global_block_rsv;
2124
Yan, Zheng76dda932009-09-21 16:00:26 -04002125 ret = btrfs_unlink_subvol(trans, root, dir,
2126 dest->root_key.objectid,
2127 dentry->d_name.name,
2128 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002129 if (ret) {
2130 err = ret;
2131 btrfs_abort_transaction(trans, root, ret);
2132 goto out_end_trans;
2133 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002134
2135 btrfs_record_root_in_trans(trans, dest);
2136
2137 memset(&dest->root_item.drop_progress, 0,
2138 sizeof(dest->root_item.drop_progress));
2139 dest->root_item.drop_level = 0;
2140 btrfs_set_root_refs(&dest->root_item, 0);
2141
Yan, Zhengd68fc572010-05-16 10:49:58 -04002142 if (!xchg(&dest->orphan_item_inserted, 1)) {
2143 ret = btrfs_insert_orphan_item(trans,
2144 root->fs_info->tree_root,
2145 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002146 if (ret) {
2147 btrfs_abort_transaction(trans, root, ret);
2148 err = ret;
2149 goto out_end_trans;
2150 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002151 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002152out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002153 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002154 if (ret && !err)
2155 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002156 inode->i_flags |= S_DEAD;
2157out_up_write:
2158 up_write(&root->fs_info->subvol_sem);
2159out_unlock:
2160 mutex_unlock(&inode->i_mutex);
2161 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002162 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002163 btrfs_invalidate_inodes(dest);
2164 d_delete(dentry);
2165 }
2166out_dput:
2167 dput(dentry);
2168out_unlock_dir:
2169 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002170 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002171out:
2172 kfree(vol_args);
2173 return err;
2174}
2175
Chris Mason1e701a32010-03-11 09:42:04 -05002176static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002177{
2178 struct inode *inode = fdentry(file)->d_inode;
2179 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002180 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002181 int ret;
2182
Li Zefanb83cc962010-12-20 16:04:08 +08002183 if (btrfs_root_readonly(root))
2184 return -EROFS;
2185
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002186 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2187 1)) {
2188 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2189 return -EINPROGRESS;
2190 }
Al Viroa561be72011-11-23 11:57:51 -05002191 ret = mnt_want_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002192 if (ret) {
2193 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
2194 0);
Yan Zhengc146afa2008-11-12 14:34:12 -05002195 return ret;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002196 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002197
2198 switch (inode->i_mode & S_IFMT) {
2199 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002200 if (!capable(CAP_SYS_ADMIN)) {
2201 ret = -EPERM;
2202 goto out;
2203 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002204 ret = btrfs_defrag_root(root, 0);
2205 if (ret)
2206 goto out;
2207 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002208 break;
2209 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002210 if (!(file->f_mode & FMODE_WRITE)) {
2211 ret = -EINVAL;
2212 goto out;
2213 }
Chris Mason1e701a32010-03-11 09:42:04 -05002214
2215 range = kzalloc(sizeof(*range), GFP_KERNEL);
2216 if (!range) {
2217 ret = -ENOMEM;
2218 goto out;
2219 }
2220
2221 if (argp) {
2222 if (copy_from_user(range, argp,
2223 sizeof(*range))) {
2224 ret = -EFAULT;
2225 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002226 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002227 }
2228 /* compression requires us to start the IO */
2229 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2230 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2231 range->extent_thresh = (u32)-1;
2232 }
2233 } else {
2234 /* the rest are all set to zero by kzalloc */
2235 range->len = (u64)-1;
2236 }
Chris Mason4cb53002011-05-24 15:35:30 -04002237 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2238 range, 0, 0);
2239 if (ret > 0)
2240 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002241 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002242 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002243 default:
2244 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002245 }
Chris Masone441d542009-01-05 16:57:23 -05002246out:
Al Viro2a79f172011-12-09 08:06:57 -05002247 mnt_drop_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002248 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Chris Masone441d542009-01-05 16:57:23 -05002249 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002250}
2251
Christoph Hellwigb2950862008-12-02 09:54:17 -05002252static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002253{
2254 struct btrfs_ioctl_vol_args *vol_args;
2255 int ret;
2256
Chris Masone441d542009-01-05 16:57:23 -05002257 if (!capable(CAP_SYS_ADMIN))
2258 return -EPERM;
2259
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002260 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2261 1)) {
2262 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2263 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002264 }
2265
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002266 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002267 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002268 if (IS_ERR(vol_args)) {
2269 ret = PTR_ERR(vol_args);
2270 goto out;
2271 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002272
Mark Fasheh5516e592008-07-24 12:20:14 -04002273 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002274 ret = btrfs_init_new_device(root, vol_args->name);
2275
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002276 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002277out:
2278 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002279 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002280 return ret;
2281}
2282
Miao Xieda249272012-11-26 08:44:50 +00002283static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002284{
Miao Xieda249272012-11-26 08:44:50 +00002285 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002286 struct btrfs_ioctl_vol_args *vol_args;
2287 int ret;
2288
Chris Masone441d542009-01-05 16:57:23 -05002289 if (!capable(CAP_SYS_ADMIN))
2290 return -EPERM;
2291
Miao Xieda249272012-11-26 08:44:50 +00002292 ret = mnt_want_write_file(file);
2293 if (ret)
2294 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002295
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002296 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2297 1)) {
2298 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Miao Xieda249272012-11-26 08:44:50 +00002299 mnt_drop_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002300 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002301 }
2302
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002303 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002304 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002305 if (IS_ERR(vol_args)) {
2306 ret = PTR_ERR(vol_args);
2307 goto out;
2308 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002309
Mark Fasheh5516e592008-07-24 12:20:14 -04002310 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002311 ret = btrfs_rm_device(root, vol_args->name);
2312
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002313 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002314out:
2315 mutex_unlock(&root->fs_info->volume_mutex);
Miao Xieda249272012-11-26 08:44:50 +00002316 mnt_drop_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002317 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002318 return ret;
2319}
2320
Jan Schmidt475f6382011-03-11 15:41:01 +01002321static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2322{
Li Zefan027ed2f2011-06-08 08:27:56 +00002323 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002324 struct btrfs_device *device;
2325 struct btrfs_device *next;
2326 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002327 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002328
2329 if (!capable(CAP_SYS_ADMIN))
2330 return -EPERM;
2331
Li Zefan027ed2f2011-06-08 08:27:56 +00002332 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2333 if (!fi_args)
2334 return -ENOMEM;
2335
2336 fi_args->num_devices = fs_devices->num_devices;
2337 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002338
2339 mutex_lock(&fs_devices->device_list_mutex);
2340 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002341 if (device->devid > fi_args->max_id)
2342 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002343 }
2344 mutex_unlock(&fs_devices->device_list_mutex);
2345
Li Zefan027ed2f2011-06-08 08:27:56 +00002346 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2347 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002348
Li Zefan027ed2f2011-06-08 08:27:56 +00002349 kfree(fi_args);
2350 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002351}
2352
2353static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2354{
2355 struct btrfs_ioctl_dev_info_args *di_args;
2356 struct btrfs_device *dev;
2357 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2358 int ret = 0;
2359 char *s_uuid = NULL;
2360 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2361
2362 if (!capable(CAP_SYS_ADMIN))
2363 return -EPERM;
2364
2365 di_args = memdup_user(arg, sizeof(*di_args));
2366 if (IS_ERR(di_args))
2367 return PTR_ERR(di_args);
2368
2369 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2370 s_uuid = di_args->uuid;
2371
2372 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002373 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002374 mutex_unlock(&fs_devices->device_list_mutex);
2375
2376 if (!dev) {
2377 ret = -ENODEV;
2378 goto out;
2379 }
2380
2381 di_args->devid = dev->devid;
2382 di_args->bytes_used = dev->bytes_used;
2383 di_args->total_bytes = dev->total_bytes;
2384 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002385 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002386 struct rcu_string *name;
2387
2388 rcu_read_lock();
2389 name = rcu_dereference(dev->name);
2390 strncpy(di_args->path, name->str, sizeof(di_args->path));
2391 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002392 di_args->path[sizeof(di_args->path) - 1] = 0;
2393 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002394 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002395 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002396
2397out:
2398 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2399 ret = -EFAULT;
2400
2401 kfree(di_args);
2402 return ret;
2403}
2404
Yan, Zheng76dda932009-09-21 16:00:26 -04002405static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2406 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002407{
2408 struct inode *inode = fdentry(file)->d_inode;
2409 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro2903ff02012-08-28 12:52:22 -04002410 struct fd src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002411 struct inode *src;
2412 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002413 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002414 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002415 char *buf;
2416 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002417 u32 nritems;
2418 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002419 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002420 u64 len = olen;
2421 u64 bs = root->fs_info->sb->s_blocksize;
Chris Masond20f7042008-12-08 16:58:54 -05002422
Sage Weilc5c9cd42008-11-12 14:32:25 -05002423 /*
2424 * TODO:
2425 * - split compressed inline extents. annoying: we need to
2426 * decompress into destination's address_space (the file offset
2427 * may change, so source mapping won't do), then recompress (or
2428 * otherwise reinsert) a subrange.
2429 * - allow ranges within the same file to be cloned (provided
2430 * they don't overlap)?
2431 */
2432
Chris Masone441d542009-01-05 16:57:23 -05002433 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002434 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002435 return -EINVAL;
2436
Li Zefanb83cc962010-12-20 16:04:08 +08002437 if (btrfs_root_readonly(root))
2438 return -EROFS;
2439
Al Viroa561be72011-11-23 11:57:51 -05002440 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002441 if (ret)
2442 return ret;
2443
Al Viro2903ff02012-08-28 12:52:22 -04002444 src_file = fdget(srcfd);
2445 if (!src_file.file) {
Yan Zhengab67b7c2008-12-19 10:58:39 -05002446 ret = -EBADF;
2447 goto out_drop_write;
2448 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002449
David Sterba362a20c2011-08-01 18:11:57 +02002450 ret = -EXDEV;
Al Viro2903ff02012-08-28 12:52:22 -04002451 if (src_file.file->f_path.mnt != file->f_path.mnt)
David Sterba362a20c2011-08-01 18:11:57 +02002452 goto out_fput;
2453
Al Viro2903ff02012-08-28 12:52:22 -04002454 src = src_file.file->f_dentry->d_inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002455
Sage Weilc5c9cd42008-11-12 14:32:25 -05002456 ret = -EINVAL;
2457 if (src == inode)
2458 goto out_fput;
2459
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002460 /* the src must be open for reading */
Al Viro2903ff02012-08-28 12:52:22 -04002461 if (!(src_file.file->f_mode & FMODE_READ))
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002462 goto out_fput;
2463
Li Zefan0e7b8242011-09-18 10:20:46 -04002464 /* don't make the dst file partly checksummed */
2465 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2466 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2467 goto out_fput;
2468
Yan Zhengae01a0a2008-08-04 23:23:47 -04002469 ret = -EISDIR;
2470 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002471 goto out_fput;
2472
Yan Zhengae01a0a2008-08-04 23:23:47 -04002473 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002474 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002475 goto out_fput;
2476
2477 ret = -ENOMEM;
2478 buf = vmalloc(btrfs_level_size(root, 0));
2479 if (!buf)
2480 goto out_fput;
2481
2482 path = btrfs_alloc_path();
2483 if (!path) {
2484 vfree(buf);
2485 goto out_fput;
2486 }
2487 path->reada = 2;
2488
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002489 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002490 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2491 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002492 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002493 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2494 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002495 }
2496
Sage Weilc5c9cd42008-11-12 14:32:25 -05002497 /* determine range to clone */
2498 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002499 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002500 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002501 if (len == 0)
2502 olen = len = src->i_size - off;
2503 /* if we extend to eof, continue to block boundary */
2504 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002505 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002506
2507 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002508 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2509 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002510 goto out_unlock;
2511
Li Zefand525e8a2011-09-11 10:52:25 -04002512 if (destoff > inode->i_size) {
2513 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2514 if (ret)
2515 goto out_unlock;
2516 }
2517
Li Zefan71ef0782011-09-18 10:20:46 -04002518 /* truncate page cache pages from target inode range */
2519 truncate_inode_pages_range(&inode->i_data, destoff,
2520 PAGE_CACHE_ALIGN(destoff + len) - 1);
2521
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002522 /* do any pending delalloc/csum calc on src, one way or
2523 another, and lock file content */
2524 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002525 struct btrfs_ordered_extent *ordered;
Liu Boaa42ffd2012-09-18 03:52:23 -06002526 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2527 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
Sage Weil9a019192010-10-29 15:37:33 -04002528 if (!ordered &&
Liu Boaa42ffd2012-09-18 03:52:23 -06002529 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2530 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002531 break;
Liu Boaa42ffd2012-09-18 03:52:23 -06002532 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002533 if (ordered)
2534 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002535 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002536 }
2537
Sage Weilc5c9cd42008-11-12 14:32:25 -05002538 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002539 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002540 key.type = BTRFS_EXTENT_DATA_KEY;
2541 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002542
2543 while (1) {
2544 /*
2545 * note the key will change type as we walk through the
2546 * tree.
2547 */
David Sterba362a20c2011-08-01 18:11:57 +02002548 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2549 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002550 if (ret < 0)
2551 goto out;
2552
Yan Zhengae01a0a2008-08-04 23:23:47 -04002553 nritems = btrfs_header_nritems(path->nodes[0]);
2554 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002555 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002556 if (ret < 0)
2557 goto out;
2558 if (ret > 0)
2559 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002560 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002561 }
2562 leaf = path->nodes[0];
2563 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002564
Yan Zhengae01a0a2008-08-04 23:23:47 -04002565 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002566 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002567 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002568 break;
2569
Sage Weilc5c9cd42008-11-12 14:32:25 -05002570 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2571 struct btrfs_file_extent_item *extent;
2572 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002573 u32 size;
2574 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002575 u64 disko = 0, diskl = 0;
2576 u64 datao = 0, datal = 0;
2577 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002578 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002579
2580 size = btrfs_item_size_nr(leaf, slot);
2581 read_extent_buffer(leaf, buf,
2582 btrfs_item_ptr_offset(leaf, slot),
2583 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002584
2585 extent = btrfs_item_ptr(leaf, slot,
2586 struct btrfs_file_extent_item);
2587 comp = btrfs_file_extent_compression(leaf, extent);
2588 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002589 if (type == BTRFS_FILE_EXTENT_REG ||
2590 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002591 disko = btrfs_file_extent_disk_bytenr(leaf,
2592 extent);
2593 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2594 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002595 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002596 datal = btrfs_file_extent_num_bytes(leaf,
2597 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002598 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2599 /* take upper bound, may be compressed */
2600 datal = btrfs_file_extent_ram_bytes(leaf,
2601 extent);
2602 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002603 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002604
Sage Weil050006a2010-10-29 15:37:33 -04002605 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002606 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002607 goto next;
2608
Zheng Yan31840ae2008-09-23 13:14:14 -04002609 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002610 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002611 if (off <= key.offset)
2612 new_key.offset = key.offset + destoff - off;
2613 else
2614 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002615
Sage Weilb6f34092011-09-20 14:48:51 -04002616 /*
2617 * 1 - adjusting old extent (we may have to split it)
2618 * 1 - add new extent
2619 * 1 - inode update
2620 */
2621 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002622 if (IS_ERR(trans)) {
2623 ret = PTR_ERR(trans);
2624 goto out;
2625 }
2626
Chris Masonc8a894d2009-06-27 21:07:03 -04002627 if (type == BTRFS_FILE_EXTENT_REG ||
2628 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002629 /*
2630 * a | --- range to clone ---| b
2631 * | ------------- extent ------------- |
2632 */
2633
2634 /* substract range b */
2635 if (key.offset + datal > off + len)
2636 datal = off + len - key.offset;
2637
2638 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002639 if (off > key.offset) {
2640 datao += off - key.offset;
2641 datal -= off - key.offset;
2642 }
2643
Josef Bacik5dc562c2012-08-17 13:14:17 -04002644 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002645 new_key.offset,
2646 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002647 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002648 if (ret) {
2649 btrfs_abort_transaction(trans, root,
2650 ret);
2651 btrfs_end_transaction(trans, root);
2652 goto out;
2653 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002654
Sage Weilc5c9cd42008-11-12 14:32:25 -05002655 ret = btrfs_insert_empty_item(trans, root, path,
2656 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657 if (ret) {
2658 btrfs_abort_transaction(trans, root,
2659 ret);
2660 btrfs_end_transaction(trans, root);
2661 goto out;
2662 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002663
2664 leaf = path->nodes[0];
2665 slot = path->slots[0];
2666 write_extent_buffer(leaf, buf,
2667 btrfs_item_ptr_offset(leaf, slot),
2668 size);
2669
2670 extent = btrfs_item_ptr(leaf, slot,
2671 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002672
Sage Weilc5c9cd42008-11-12 14:32:25 -05002673 /* disko == 0 means it's a hole */
2674 if (!disko)
2675 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002676
2677 btrfs_set_file_extent_offset(leaf, extent,
2678 datao);
2679 btrfs_set_file_extent_num_bytes(leaf, extent,
2680 datal);
2681 if (disko) {
2682 inode_add_bytes(inode, datal);
2683 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002684 disko, diskl, 0,
2685 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002686 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002687 new_key.offset - datao,
2688 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002689 if (ret) {
2690 btrfs_abort_transaction(trans,
2691 root,
2692 ret);
2693 btrfs_end_transaction(trans,
2694 root);
2695 goto out;
2696
2697 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002698 }
2699 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2700 u64 skip = 0;
2701 u64 trim = 0;
2702 if (off > key.offset) {
2703 skip = off - key.offset;
2704 new_key.offset += skip;
2705 }
Chris Masond3977122009-01-05 21:25:51 -05002706
Liu Boaa42ffd2012-09-18 03:52:23 -06002707 if (key.offset + datal > off + len)
2708 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05002709
Sage Weilc5c9cd42008-11-12 14:32:25 -05002710 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002711 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002712 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002713 goto out;
2714 }
2715 size -= skip + trim;
2716 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002717
Josef Bacik5dc562c2012-08-17 13:14:17 -04002718 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002719 new_key.offset,
2720 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002721 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002722 if (ret) {
2723 btrfs_abort_transaction(trans, root,
2724 ret);
2725 btrfs_end_transaction(trans, root);
2726 goto out;
2727 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002728
Sage Weilc5c9cd42008-11-12 14:32:25 -05002729 ret = btrfs_insert_empty_item(trans, root, path,
2730 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002731 if (ret) {
2732 btrfs_abort_transaction(trans, root,
2733 ret);
2734 btrfs_end_transaction(trans, root);
2735 goto out;
2736 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002737
2738 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002739 u32 start =
2740 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002741 memmove(buf+start, buf+start+skip,
2742 datal);
2743 }
2744
2745 leaf = path->nodes[0];
2746 slot = path->slots[0];
2747 write_extent_buffer(leaf, buf,
2748 btrfs_item_ptr_offset(leaf, slot),
2749 size);
2750 inode_add_bytes(inode, datal);
2751 }
2752
2753 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002754 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002755
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002756 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002757 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002758
2759 /*
2760 * we round up to the block size at eof when
2761 * determining which extents to clone above,
2762 * but shouldn't round up the file size
2763 */
2764 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002765 if (endoff > destoff+olen)
2766 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002767 if (endoff > inode->i_size)
2768 btrfs_i_size_write(inode, endoff);
2769
Yan, Zhenga22285a2010-05-16 10:48:46 -04002770 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002771 if (ret) {
2772 btrfs_abort_transaction(trans, root, ret);
2773 btrfs_end_transaction(trans, root);
2774 goto out;
2775 }
2776 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002777 }
Chris Masond3977122009-01-05 21:25:51 -05002778next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002779 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002780 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002781 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002782 ret = 0;
2783out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002784 btrfs_release_path(path);
Liu Boaa42ffd2012-09-18 03:52:23 -06002785 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002786out_unlock:
2787 mutex_unlock(&src->i_mutex);
2788 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002789 vfree(buf);
2790 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002791out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04002792 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002793out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002794 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002795 return ret;
2796}
2797
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002798static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002799{
2800 struct btrfs_ioctl_clone_range_args args;
2801
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002802 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002803 return -EFAULT;
2804 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2805 args.src_length, args.dest_offset);
2806}
2807
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002808/*
2809 * there are many ways the trans_start and trans_end ioctls can lead
2810 * to deadlocks. They should only be used by applications that
2811 * basically own the machine, and have a very in depth understanding
2812 * of all the possible deadlocks and enospc problems.
2813 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002814static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002815{
2816 struct inode *inode = fdentry(file)->d_inode;
2817 struct btrfs_root *root = BTRFS_I(inode)->root;
2818 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002819 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002820
Sage Weil1ab86ae2009-09-29 18:38:44 -04002821 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002822 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002823 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002824
2825 ret = -EINPROGRESS;
2826 if (file->private_data)
2827 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002828
Li Zefanb83cc962010-12-20 16:04:08 +08002829 ret = -EROFS;
2830 if (btrfs_root_readonly(root))
2831 goto out;
2832
Al Viroa561be72011-11-23 11:57:51 -05002833 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002834 if (ret)
2835 goto out;
2836
Josef Bacika4abeea2011-04-11 17:25:13 -04002837 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002838
Sage Weil1ab86ae2009-09-29 18:38:44 -04002839 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002840 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002841 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002842 goto out_drop;
2843
2844 file->private_data = trans;
2845 return 0;
2846
2847out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002848 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002849 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002850out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002851 return ret;
2852}
2853
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002854static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2855{
2856 struct inode *inode = fdentry(file)->d_inode;
2857 struct btrfs_root *root = BTRFS_I(inode)->root;
2858 struct btrfs_root *new_root;
2859 struct btrfs_dir_item *di;
2860 struct btrfs_trans_handle *trans;
2861 struct btrfs_path *path;
2862 struct btrfs_key location;
2863 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002864 u64 objectid = 0;
2865 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00002866 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002867
2868 if (!capable(CAP_SYS_ADMIN))
2869 return -EPERM;
2870
Miao Xie3c04ce02012-11-26 08:43:07 +00002871 ret = mnt_want_write_file(file);
2872 if (ret)
2873 return ret;
2874
2875 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2876 ret = -EFAULT;
2877 goto out;
2878 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002879
2880 if (!objectid)
2881 objectid = root->root_key.objectid;
2882
2883 location.objectid = objectid;
2884 location.type = BTRFS_ROOT_ITEM_KEY;
2885 location.offset = (u64)-1;
2886
2887 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00002888 if (IS_ERR(new_root)) {
2889 ret = PTR_ERR(new_root);
2890 goto out;
2891 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002892
Miao Xie3c04ce02012-11-26 08:43:07 +00002893 if (btrfs_root_refs(&new_root->root_item) == 0) {
2894 ret = -ENOENT;
2895 goto out;
2896 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002897
2898 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00002899 if (!path) {
2900 ret = -ENOMEM;
2901 goto out;
2902 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002903 path->leave_spinning = 1;
2904
2905 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002906 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002907 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00002908 ret = PTR_ERR(trans);
2909 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002910 }
2911
David Sterba6c417612011-04-13 15:41:04 +02002912 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002913 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2914 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002915 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002916 btrfs_free_path(path);
2917 btrfs_end_transaction(trans, root);
2918 printk(KERN_ERR "Umm, you don't have the default dir item, "
2919 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00002920 ret = -ENOENT;
2921 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002922 }
2923
2924 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2925 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2926 btrfs_mark_buffer_dirty(path->nodes[0]);
2927 btrfs_free_path(path);
2928
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06002929 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002930 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00002931out:
2932 mnt_drop_write_file(file);
2933 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002934}
2935
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002936void btrfs_get_block_group_info(struct list_head *groups_list,
2937 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002938{
2939 struct btrfs_block_group_cache *block_group;
2940
2941 space->total_bytes = 0;
2942 space->used_bytes = 0;
2943 space->flags = 0;
2944 list_for_each_entry(block_group, groups_list, list) {
2945 space->flags = block_group->flags;
2946 space->total_bytes += block_group->key.offset;
2947 space->used_bytes +=
2948 btrfs_block_group_used(&block_group->item);
2949 }
2950}
2951
Josef Bacik1406e432010-01-13 18:19:06 +00002952long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2953{
2954 struct btrfs_ioctl_space_args space_args;
2955 struct btrfs_ioctl_space_info space;
2956 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002957 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002958 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002959 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002960 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2961 BTRFS_BLOCK_GROUP_SYSTEM,
2962 BTRFS_BLOCK_GROUP_METADATA,
2963 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2964 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002965 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002966 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002967 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002968 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002969
2970 if (copy_from_user(&space_args,
2971 (struct btrfs_ioctl_space_args __user *)arg,
2972 sizeof(space_args)))
2973 return -EFAULT;
2974
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002975 for (i = 0; i < num_types; i++) {
2976 struct btrfs_space_info *tmp;
2977
2978 info = NULL;
2979 rcu_read_lock();
2980 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2981 list) {
2982 if (tmp->flags == types[i]) {
2983 info = tmp;
2984 break;
2985 }
2986 }
2987 rcu_read_unlock();
2988
2989 if (!info)
2990 continue;
2991
2992 down_read(&info->groups_sem);
2993 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2994 if (!list_empty(&info->block_groups[c]))
2995 slot_count++;
2996 }
2997 up_read(&info->groups_sem);
2998 }
Josef Bacik1406e432010-01-13 18:19:06 +00002999
Chris Mason7fde62b2010-03-16 15:40:10 -04003000 /* space_slots == 0 means they are asking for a count */
3001 if (space_args.space_slots == 0) {
3002 space_args.total_spaces = slot_count;
3003 goto out;
3004 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003005
Dan Rosenberg51788b12011-02-14 16:04:23 -05003006 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003007
Chris Mason7fde62b2010-03-16 15:40:10 -04003008 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003009
Chris Mason7fde62b2010-03-16 15:40:10 -04003010 /* we generally have at most 6 or so space infos, one for each raid
3011 * level. So, a whole page should be more than enough for everyone
3012 */
3013 if (alloc_size > PAGE_CACHE_SIZE)
3014 return -ENOMEM;
3015
3016 space_args.total_spaces = 0;
3017 dest = kmalloc(alloc_size, GFP_NOFS);
3018 if (!dest)
3019 return -ENOMEM;
3020 dest_orig = dest;
3021
3022 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003023 for (i = 0; i < num_types; i++) {
3024 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003025
Dan Rosenberg51788b12011-02-14 16:04:23 -05003026 if (!slot_count)
3027 break;
3028
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003029 info = NULL;
3030 rcu_read_lock();
3031 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3032 list) {
3033 if (tmp->flags == types[i]) {
3034 info = tmp;
3035 break;
3036 }
3037 }
3038 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003039
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003040 if (!info)
3041 continue;
3042 down_read(&info->groups_sem);
3043 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3044 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003045 btrfs_get_block_group_info(
3046 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003047 memcpy(dest, &space, sizeof(space));
3048 dest++;
3049 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003050 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003051 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003052 if (!slot_count)
3053 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003054 }
3055 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003056 }
Josef Bacik1406e432010-01-13 18:19:06 +00003057
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003058 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003059 (arg + sizeof(struct btrfs_ioctl_space_args));
3060
3061 if (copy_to_user(user_dest, dest_orig, alloc_size))
3062 ret = -EFAULT;
3063
3064 kfree(dest_orig);
3065out:
3066 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003067 ret = -EFAULT;
3068
3069 return ret;
3070}
3071
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003072/*
3073 * there are many ways the trans_start and trans_end ioctls can lead
3074 * to deadlocks. They should only be used by applications that
3075 * basically own the machine, and have a very in depth understanding
3076 * of all the possible deadlocks and enospc problems.
3077 */
3078long btrfs_ioctl_trans_end(struct file *file)
3079{
3080 struct inode *inode = fdentry(file)->d_inode;
3081 struct btrfs_root *root = BTRFS_I(inode)->root;
3082 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003083
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003084 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003085 if (!trans)
3086 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003087 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003088
Sage Weil1ab86ae2009-09-29 18:38:44 -04003089 btrfs_end_transaction(trans, root);
3090
Josef Bacika4abeea2011-04-11 17:25:13 -04003091 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003092
Al Viro2a79f172011-12-09 08:06:57 -05003093 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003094 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003095}
3096
Miao Xie9a8c28b2012-11-26 08:40:43 +00003097static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3098 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003099{
Sage Weil46204592010-10-29 15:41:32 -04003100 struct btrfs_trans_handle *trans;
3101 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003102 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003103
Miao Xieff7c1d32012-11-26 08:41:29 +00003104 trans = btrfs_attach_transaction(root);
3105 if (IS_ERR(trans)) {
3106 if (PTR_ERR(trans) != -ENOENT)
3107 return PTR_ERR(trans);
3108
3109 /* No running transaction, don't bother */
3110 transid = root->fs_info->last_trans_committed;
3111 goto out;
3112 }
Sage Weil46204592010-10-29 15:41:32 -04003113 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003114 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003115 if (ret) {
3116 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003117 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003118 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003119out:
Sage Weil46204592010-10-29 15:41:32 -04003120 if (argp)
3121 if (copy_to_user(argp, &transid, sizeof(transid)))
3122 return -EFAULT;
3123 return 0;
3124}
3125
Miao Xie9a8c28b2012-11-26 08:40:43 +00003126static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3127 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003128{
Sage Weil46204592010-10-29 15:41:32 -04003129 u64 transid;
3130
3131 if (argp) {
3132 if (copy_from_user(&transid, argp, sizeof(transid)))
3133 return -EFAULT;
3134 } else {
3135 transid = 0; /* current trans */
3136 }
3137 return btrfs_wait_for_commit(root, transid);
3138}
3139
Miao Xieb8e95482012-11-26 08:48:01 +00003140static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003141{
Miao Xieb8e95482012-11-26 08:48:01 +00003142 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003143 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003144 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003145
3146 if (!capable(CAP_SYS_ADMIN))
3147 return -EPERM;
3148
3149 sa = memdup_user(arg, sizeof(*sa));
3150 if (IS_ERR(sa))
3151 return PTR_ERR(sa);
3152
Miao Xieb8e95482012-11-26 08:48:01 +00003153 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3154 ret = mnt_want_write_file(file);
3155 if (ret)
3156 goto out;
3157 }
3158
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003159 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003160 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3161 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003162
3163 if (copy_to_user(arg, sa, sizeof(*sa)))
3164 ret = -EFAULT;
3165
Miao Xieb8e95482012-11-26 08:48:01 +00003166 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3167 mnt_drop_write_file(file);
3168out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003169 kfree(sa);
3170 return ret;
3171}
3172
3173static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3174{
3175 if (!capable(CAP_SYS_ADMIN))
3176 return -EPERM;
3177
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003178 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003179}
3180
3181static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3182 void __user *arg)
3183{
3184 struct btrfs_ioctl_scrub_args *sa;
3185 int ret;
3186
3187 if (!capable(CAP_SYS_ADMIN))
3188 return -EPERM;
3189
3190 sa = memdup_user(arg, sizeof(*sa));
3191 if (IS_ERR(sa))
3192 return PTR_ERR(sa);
3193
3194 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3195
3196 if (copy_to_user(arg, sa, sizeof(*sa)))
3197 ret = -EFAULT;
3198
3199 kfree(sa);
3200 return ret;
3201}
3202
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003203static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003204 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003205{
3206 struct btrfs_ioctl_get_dev_stats *sa;
3207 int ret;
3208
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003209 sa = memdup_user(arg, sizeof(*sa));
3210 if (IS_ERR(sa))
3211 return PTR_ERR(sa);
3212
David Sterbab27f7c02012-06-22 06:30:39 -06003213 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3214 kfree(sa);
3215 return -EPERM;
3216 }
3217
3218 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003219
3220 if (copy_to_user(arg, sa, sizeof(*sa)))
3221 ret = -EFAULT;
3222
3223 kfree(sa);
3224 return ret;
3225}
3226
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003227static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3228{
3229 struct btrfs_ioctl_dev_replace_args *p;
3230 int ret;
3231
3232 if (!capable(CAP_SYS_ADMIN))
3233 return -EPERM;
3234
3235 p = memdup_user(arg, sizeof(*p));
3236 if (IS_ERR(p))
3237 return PTR_ERR(p);
3238
3239 switch (p->cmd) {
3240 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3241 if (atomic_xchg(
3242 &root->fs_info->mutually_exclusive_operation_running,
3243 1)) {
3244 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3245 ret = -EINPROGRESS;
3246 } else {
3247 ret = btrfs_dev_replace_start(root, p);
3248 atomic_set(
3249 &root->fs_info->mutually_exclusive_operation_running,
3250 0);
3251 }
3252 break;
3253 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3254 btrfs_dev_replace_status(root->fs_info, p);
3255 ret = 0;
3256 break;
3257 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3258 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3259 break;
3260 default:
3261 ret = -EINVAL;
3262 break;
3263 }
3264
3265 if (copy_to_user(arg, p, sizeof(*p)))
3266 ret = -EFAULT;
3267
3268 kfree(p);
3269 return ret;
3270}
3271
Jan Schmidtd7728c92011-07-07 16:48:38 +02003272static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3273{
3274 int ret = 0;
3275 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003276 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003277 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003278 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003279 struct inode_fs_paths *ipath = NULL;
3280 struct btrfs_path *path;
3281
3282 if (!capable(CAP_SYS_ADMIN))
3283 return -EPERM;
3284
3285 path = btrfs_alloc_path();
3286 if (!path) {
3287 ret = -ENOMEM;
3288 goto out;
3289 }
3290
3291 ipa = memdup_user(arg, sizeof(*ipa));
3292 if (IS_ERR(ipa)) {
3293 ret = PTR_ERR(ipa);
3294 ipa = NULL;
3295 goto out;
3296 }
3297
3298 size = min_t(u32, ipa->size, 4096);
3299 ipath = init_ipath(size, root, path);
3300 if (IS_ERR(ipath)) {
3301 ret = PTR_ERR(ipath);
3302 ipath = NULL;
3303 goto out;
3304 }
3305
3306 ret = paths_from_inode(ipa->inum, ipath);
3307 if (ret < 0)
3308 goto out;
3309
3310 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003311 rel_ptr = ipath->fspath->val[i] -
3312 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003313 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003314 }
3315
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003316 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3317 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003318 if (ret) {
3319 ret = -EFAULT;
3320 goto out;
3321 }
3322
3323out:
3324 btrfs_free_path(path);
3325 free_ipath(ipath);
3326 kfree(ipa);
3327
3328 return ret;
3329}
3330
3331static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3332{
3333 struct btrfs_data_container *inodes = ctx;
3334 const size_t c = 3 * sizeof(u64);
3335
3336 if (inodes->bytes_left >= c) {
3337 inodes->bytes_left -= c;
3338 inodes->val[inodes->elem_cnt] = inum;
3339 inodes->val[inodes->elem_cnt + 1] = offset;
3340 inodes->val[inodes->elem_cnt + 2] = root;
3341 inodes->elem_cnt += 3;
3342 } else {
3343 inodes->bytes_missing += c - inodes->bytes_left;
3344 inodes->bytes_left = 0;
3345 inodes->elem_missed += 3;
3346 }
3347
3348 return 0;
3349}
3350
3351static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3352 void __user *arg)
3353{
3354 int ret = 0;
3355 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003356 struct btrfs_ioctl_logical_ino_args *loi;
3357 struct btrfs_data_container *inodes = NULL;
3358 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003359
3360 if (!capable(CAP_SYS_ADMIN))
3361 return -EPERM;
3362
3363 loi = memdup_user(arg, sizeof(*loi));
3364 if (IS_ERR(loi)) {
3365 ret = PTR_ERR(loi);
3366 loi = NULL;
3367 goto out;
3368 }
3369
3370 path = btrfs_alloc_path();
3371 if (!path) {
3372 ret = -ENOMEM;
3373 goto out;
3374 }
3375
Liu Bo425d17a2012-09-07 20:01:30 -06003376 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003377 inodes = init_data_container(size);
3378 if (IS_ERR(inodes)) {
3379 ret = PTR_ERR(inodes);
3380 inodes = NULL;
3381 goto out;
3382 }
3383
Liu Bodf031f02012-09-07 20:01:29 -06003384 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3385 build_ino_list, inodes);
3386 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003387 ret = -ENOENT;
3388 if (ret < 0)
3389 goto out;
3390
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003391 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3392 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003393 if (ret)
3394 ret = -EFAULT;
3395
3396out:
3397 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003398 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003399 kfree(loi);
3400
3401 return ret;
3402}
3403
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003404void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003405 struct btrfs_ioctl_balance_args *bargs)
3406{
3407 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3408
3409 bargs->flags = bctl->flags;
3410
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003411 if (atomic_read(&fs_info->balance_running))
3412 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3413 if (atomic_read(&fs_info->balance_pause_req))
3414 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003415 if (atomic_read(&fs_info->balance_cancel_req))
3416 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003417
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003418 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3419 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3420 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003421
3422 if (lock) {
3423 spin_lock(&fs_info->balance_lock);
3424 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3425 spin_unlock(&fs_info->balance_lock);
3426 } else {
3427 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3428 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003429}
3430
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003431static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003432{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003433 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003434 struct btrfs_fs_info *fs_info = root->fs_info;
3435 struct btrfs_ioctl_balance_args *bargs;
3436 struct btrfs_balance_control *bctl;
3437 int ret;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003438 int need_to_clear_lock = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003439
3440 if (!capable(CAP_SYS_ADMIN))
3441 return -EPERM;
3442
Liu Boe54bfa312012-06-29 03:58:48 -06003443 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003444 if (ret)
3445 return ret;
3446
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003447 mutex_lock(&fs_info->volume_mutex);
3448 mutex_lock(&fs_info->balance_mutex);
3449
3450 if (arg) {
3451 bargs = memdup_user(arg, sizeof(*bargs));
3452 if (IS_ERR(bargs)) {
3453 ret = PTR_ERR(bargs);
3454 goto out;
3455 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003456
3457 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3458 if (!fs_info->balance_ctl) {
3459 ret = -ENOTCONN;
3460 goto out_bargs;
3461 }
3462
3463 bctl = fs_info->balance_ctl;
3464 spin_lock(&fs_info->balance_lock);
3465 bctl->flags |= BTRFS_BALANCE_RESUME;
3466 spin_unlock(&fs_info->balance_lock);
3467
3468 goto do_balance;
3469 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003470 } else {
3471 bargs = NULL;
3472 }
3473
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003474 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
3475 1)) {
3476 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003477 ret = -EINPROGRESS;
3478 goto out_bargs;
3479 }
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003480 need_to_clear_lock = 1;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003481
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003482 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3483 if (!bctl) {
3484 ret = -ENOMEM;
3485 goto out_bargs;
3486 }
3487
3488 bctl->fs_info = fs_info;
3489 if (arg) {
3490 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3491 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3492 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3493
3494 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003495 } else {
3496 /* balance everything - no filters */
3497 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003498 }
3499
Ilya Dryomovde322262012-01-16 22:04:49 +02003500do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003501 ret = btrfs_balance(bctl, bargs);
3502 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003503 * bctl is freed in __cancel_balance or in free_fs_info if
3504 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003505 */
3506 if (arg) {
3507 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3508 ret = -EFAULT;
3509 }
3510
3511out_bargs:
3512 kfree(bargs);
3513out:
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003514 if (need_to_clear_lock)
3515 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
3516 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003517 mutex_unlock(&fs_info->balance_mutex);
3518 mutex_unlock(&fs_info->volume_mutex);
Liu Boe54bfa312012-06-29 03:58:48 -06003519 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003520 return ret;
3521}
3522
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003523static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3524{
3525 if (!capable(CAP_SYS_ADMIN))
3526 return -EPERM;
3527
3528 switch (cmd) {
3529 case BTRFS_BALANCE_CTL_PAUSE:
3530 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003531 case BTRFS_BALANCE_CTL_CANCEL:
3532 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003533 }
3534
3535 return -EINVAL;
3536}
3537
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003538static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3539 void __user *arg)
3540{
3541 struct btrfs_fs_info *fs_info = root->fs_info;
3542 struct btrfs_ioctl_balance_args *bargs;
3543 int ret = 0;
3544
3545 if (!capable(CAP_SYS_ADMIN))
3546 return -EPERM;
3547
3548 mutex_lock(&fs_info->balance_mutex);
3549 if (!fs_info->balance_ctl) {
3550 ret = -ENOTCONN;
3551 goto out;
3552 }
3553
3554 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3555 if (!bargs) {
3556 ret = -ENOMEM;
3557 goto out;
3558 }
3559
3560 update_ioctl_balance_args(fs_info, 1, bargs);
3561
3562 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3563 ret = -EFAULT;
3564
3565 kfree(bargs);
3566out:
3567 mutex_unlock(&fs_info->balance_mutex);
3568 return ret;
3569}
3570
Miao Xie905b0dd2012-11-26 08:50:11 +00003571static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003572{
Miao Xie905b0dd2012-11-26 08:50:11 +00003573 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003574 struct btrfs_ioctl_quota_ctl_args *sa;
3575 struct btrfs_trans_handle *trans = NULL;
3576 int ret;
3577 int err;
3578
3579 if (!capable(CAP_SYS_ADMIN))
3580 return -EPERM;
3581
Miao Xie905b0dd2012-11-26 08:50:11 +00003582 ret = mnt_want_write_file(file);
3583 if (ret)
3584 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003585
3586 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003587 if (IS_ERR(sa)) {
3588 ret = PTR_ERR(sa);
3589 goto drop_write;
3590 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003591
3592 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3593 trans = btrfs_start_transaction(root, 2);
3594 if (IS_ERR(trans)) {
3595 ret = PTR_ERR(trans);
3596 goto out;
3597 }
3598 }
3599
3600 switch (sa->cmd) {
3601 case BTRFS_QUOTA_CTL_ENABLE:
3602 ret = btrfs_quota_enable(trans, root->fs_info);
3603 break;
3604 case BTRFS_QUOTA_CTL_DISABLE:
3605 ret = btrfs_quota_disable(trans, root->fs_info);
3606 break;
3607 case BTRFS_QUOTA_CTL_RESCAN:
3608 ret = btrfs_quota_rescan(root->fs_info);
3609 break;
3610 default:
3611 ret = -EINVAL;
3612 break;
3613 }
3614
3615 if (copy_to_user(arg, sa, sizeof(*sa)))
3616 ret = -EFAULT;
3617
3618 if (trans) {
3619 err = btrfs_commit_transaction(trans, root);
3620 if (err && !ret)
3621 ret = err;
3622 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003623out:
3624 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003625drop_write:
3626 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003627 return ret;
3628}
3629
Miao Xie905b0dd2012-11-26 08:50:11 +00003630static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003631{
Miao Xie905b0dd2012-11-26 08:50:11 +00003632 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003633 struct btrfs_ioctl_qgroup_assign_args *sa;
3634 struct btrfs_trans_handle *trans;
3635 int ret;
3636 int err;
3637
3638 if (!capable(CAP_SYS_ADMIN))
3639 return -EPERM;
3640
Miao Xie905b0dd2012-11-26 08:50:11 +00003641 ret = mnt_want_write_file(file);
3642 if (ret)
3643 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003644
3645 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003646 if (IS_ERR(sa)) {
3647 ret = PTR_ERR(sa);
3648 goto drop_write;
3649 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003650
3651 trans = btrfs_join_transaction(root);
3652 if (IS_ERR(trans)) {
3653 ret = PTR_ERR(trans);
3654 goto out;
3655 }
3656
3657 /* FIXME: check if the IDs really exist */
3658 if (sa->assign) {
3659 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3660 sa->src, sa->dst);
3661 } else {
3662 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3663 sa->src, sa->dst);
3664 }
3665
3666 err = btrfs_end_transaction(trans, root);
3667 if (err && !ret)
3668 ret = err;
3669
3670out:
3671 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003672drop_write:
3673 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003674 return ret;
3675}
3676
Miao Xie905b0dd2012-11-26 08:50:11 +00003677static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003678{
Miao Xie905b0dd2012-11-26 08:50:11 +00003679 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003680 struct btrfs_ioctl_qgroup_create_args *sa;
3681 struct btrfs_trans_handle *trans;
3682 int ret;
3683 int err;
3684
3685 if (!capable(CAP_SYS_ADMIN))
3686 return -EPERM;
3687
Miao Xie905b0dd2012-11-26 08:50:11 +00003688 ret = mnt_want_write_file(file);
3689 if (ret)
3690 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003691
3692 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003693 if (IS_ERR(sa)) {
3694 ret = PTR_ERR(sa);
3695 goto drop_write;
3696 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003697
3698 trans = btrfs_join_transaction(root);
3699 if (IS_ERR(trans)) {
3700 ret = PTR_ERR(trans);
3701 goto out;
3702 }
3703
3704 /* FIXME: check if the IDs really exist */
3705 if (sa->create) {
3706 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3707 NULL);
3708 } else {
3709 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3710 }
3711
3712 err = btrfs_end_transaction(trans, root);
3713 if (err && !ret)
3714 ret = err;
3715
3716out:
3717 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003718drop_write:
3719 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003720 return ret;
3721}
3722
Miao Xie905b0dd2012-11-26 08:50:11 +00003723static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003724{
Miao Xie905b0dd2012-11-26 08:50:11 +00003725 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003726 struct btrfs_ioctl_qgroup_limit_args *sa;
3727 struct btrfs_trans_handle *trans;
3728 int ret;
3729 int err;
3730 u64 qgroupid;
3731
3732 if (!capable(CAP_SYS_ADMIN))
3733 return -EPERM;
3734
Miao Xie905b0dd2012-11-26 08:50:11 +00003735 ret = mnt_want_write_file(file);
3736 if (ret)
3737 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003738
3739 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003740 if (IS_ERR(sa)) {
3741 ret = PTR_ERR(sa);
3742 goto drop_write;
3743 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003744
3745 trans = btrfs_join_transaction(root);
3746 if (IS_ERR(trans)) {
3747 ret = PTR_ERR(trans);
3748 goto out;
3749 }
3750
3751 qgroupid = sa->qgroupid;
3752 if (!qgroupid) {
3753 /* take the current subvol as qgroup */
3754 qgroupid = root->root_key.objectid;
3755 }
3756
3757 /* FIXME: check if the IDs really exist */
3758 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3759
3760 err = btrfs_end_transaction(trans, root);
3761 if (err && !ret)
3762 ret = err;
3763
3764out:
3765 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003766drop_write:
3767 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003768 return ret;
3769}
3770
Alexander Block8ea05e32012-07-25 17:35:53 +02003771static long btrfs_ioctl_set_received_subvol(struct file *file,
3772 void __user *arg)
3773{
3774 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3775 struct inode *inode = fdentry(file)->d_inode;
3776 struct btrfs_root *root = BTRFS_I(inode)->root;
3777 struct btrfs_root_item *root_item = &root->root_item;
3778 struct btrfs_trans_handle *trans;
3779 struct timespec ct = CURRENT_TIME;
3780 int ret = 0;
3781
3782 ret = mnt_want_write_file(file);
3783 if (ret < 0)
3784 return ret;
3785
3786 down_write(&root->fs_info->subvol_sem);
3787
3788 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3789 ret = -EINVAL;
3790 goto out;
3791 }
3792
3793 if (btrfs_root_readonly(root)) {
3794 ret = -EROFS;
3795 goto out;
3796 }
3797
3798 if (!inode_owner_or_capable(inode)) {
3799 ret = -EACCES;
3800 goto out;
3801 }
3802
3803 sa = memdup_user(arg, sizeof(*sa));
3804 if (IS_ERR(sa)) {
3805 ret = PTR_ERR(sa);
3806 sa = NULL;
3807 goto out;
3808 }
3809
3810 trans = btrfs_start_transaction(root, 1);
3811 if (IS_ERR(trans)) {
3812 ret = PTR_ERR(trans);
3813 trans = NULL;
3814 goto out;
3815 }
3816
3817 sa->rtransid = trans->transid;
3818 sa->rtime.sec = ct.tv_sec;
3819 sa->rtime.nsec = ct.tv_nsec;
3820
3821 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3822 btrfs_set_root_stransid(root_item, sa->stransid);
3823 btrfs_set_root_rtransid(root_item, sa->rtransid);
3824 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3825 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3826 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3827 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3828
3829 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3830 &root->root_key, &root->root_item);
3831 if (ret < 0) {
3832 btrfs_end_transaction(trans, root);
3833 trans = NULL;
3834 goto out;
3835 } else {
3836 ret = btrfs_commit_transaction(trans, root);
3837 if (ret < 0)
3838 goto out;
3839 }
3840
3841 ret = copy_to_user(arg, sa, sizeof(*sa));
3842 if (ret)
3843 ret = -EFAULT;
3844
3845out:
3846 kfree(sa);
3847 up_write(&root->fs_info->subvol_sem);
3848 mnt_drop_write_file(file);
3849 return ret;
3850}
3851
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003852long btrfs_ioctl(struct file *file, unsigned int
3853 cmd, unsigned long arg)
3854{
3855 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003856 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003857
3858 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003859 case FS_IOC_GETFLAGS:
3860 return btrfs_ioctl_getflags(file, argp);
3861 case FS_IOC_SETFLAGS:
3862 return btrfs_ioctl_setflags(file, argp);
3863 case FS_IOC_GETVERSION:
3864 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003865 case FITRIM:
3866 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003867 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003868 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003869 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003870 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003871 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003872 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02003873 case BTRFS_IOC_SUBVOL_CREATE_V2:
3874 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003875 case BTRFS_IOC_SNAP_DESTROY:
3876 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003877 case BTRFS_IOC_SUBVOL_GETFLAGS:
3878 return btrfs_ioctl_subvol_getflags(file, argp);
3879 case BTRFS_IOC_SUBVOL_SETFLAGS:
3880 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003881 case BTRFS_IOC_DEFAULT_SUBVOL:
3882 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003883 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003884 return btrfs_ioctl_defrag(file, NULL);
3885 case BTRFS_IOC_DEFRAG_RANGE:
3886 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003887 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00003888 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003889 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003890 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003891 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00003892 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003893 case BTRFS_IOC_FS_INFO:
3894 return btrfs_ioctl_fs_info(root, argp);
3895 case BTRFS_IOC_DEV_INFO:
3896 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003897 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003898 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003899 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003900 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3901 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003902 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003903 case BTRFS_IOC_TRANS_START:
3904 return btrfs_ioctl_trans_start(file);
3905 case BTRFS_IOC_TRANS_END:
3906 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003907 case BTRFS_IOC_TREE_SEARCH:
3908 return btrfs_ioctl_tree_search(file, argp);
3909 case BTRFS_IOC_INO_LOOKUP:
3910 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003911 case BTRFS_IOC_INO_PATHS:
3912 return btrfs_ioctl_ino_to_path(root, argp);
3913 case BTRFS_IOC_LOGICAL_INO:
3914 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003915 case BTRFS_IOC_SPACE_INFO:
3916 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003917 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003918 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3919 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003920 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00003921 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04003922 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00003923 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003924 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00003925 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003926 case BTRFS_IOC_SCRUB_CANCEL:
3927 return btrfs_ioctl_scrub_cancel(root, argp);
3928 case BTRFS_IOC_SCRUB_PROGRESS:
3929 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003930 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003931 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003932 case BTRFS_IOC_BALANCE_CTL:
3933 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003934 case BTRFS_IOC_BALANCE_PROGRESS:
3935 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02003936 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3937 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02003938 case BTRFS_IOC_SEND:
3939 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003940 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06003941 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003942 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00003943 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003944 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00003945 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003946 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00003947 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003948 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00003949 return btrfs_ioctl_qgroup_limit(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003950 case BTRFS_IOC_DEV_REPLACE:
3951 return btrfs_ioctl_dev_replace(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003952 }
3953
3954 return -ENOTTY;
3955}